Abstract Syntax Notation #1.
Return computed CRC-32c checksum.
GNU zip
Bases: pypacker.pypacker.Packet
Compress self.data.
Return decompressed payload.
Packet read and write routines for pcap format. See http://wiki.wireshark.org/Development/LibpcapFileFormat
Bases: pypacker.pypacker.Packet
pcap file header.
Bases: pypacker.ppcap.FileHdr
Bases: pypacker.ppcap.PktHdr
Bases: pypacker.pypacker.Packet
pcap packet header.
Bases: builtins.object
Simple pcap file reader supporting pcap and pcapng format. Using iterators this will return “timestamp, byte string”. Default timestamp resolution ist nanoseconds.
Packet read and write routines using network sockets.
Bases: builtins.object
Simple socket reader/writer.
return – bytes received from network
Receive packets from network. This does the same as calling recv() but using a receive filter and received bytes will be converted to packets using class given by lowest_layer.
lowest_layer – packet class to be used to create new packets
return – packets received from network
Send the given bytes to network.
bts – the bytes to be sent dst – destination for Layer 3 if mode is MODE_LAYER_3
Send a packet and receive answer packets. This will use information retrieved from direction() to retrieve answer packets.
packet_send – pypacker packet to be sent max_packets_recv – max packets to be received filter – filter as lambda function to match packets to be retrieved,
return True to accept a specific packet
lowest_layer – packet class to be used to create new packets
return – packets receives
Simple packet creation and parsing.
Bases: builtins.Exception
Bases: builtins.type
This Metaclass is a more efficient way of setting attributes than using __init__. This is done by reading name / format / default out of __hdr__ in every subclass. This configuration is set one time when loading the module (not at instatiation). Actual values are retrieved using “obj.field” notation. CAUTION: list et al are _SHARED_ among all classes! A copy is needed on changes to them. General note: __new__ is called before __init__
Bases: builtins.object
Base packet class, with metaclass magic to generate members from self.__hdr__. This class can be instatiated via:
Packet(byte_array) Packet(key1=val1, key2=val2, ...)
Every packet got an optional header and an optional body. Body-data can be raw byte string OR a packet itself (the body handler). which stores the data. The following schema illustrates the Packet-structure:
[Packet: [headerfield1] [headerfield2] ... [headerfieldN] [Body: Handler (Packet):
[headerfield1] ... [Body: Handler (Packet):
... [Body: Raw data]
]]]
A header definition like __hdr__ = ((“name”, “12s”, b”defaultvalue”),) will define a header field having the name “name”, format “12s” and defaultvalue “defaultvalue” as bytestring. Fields will be added in order of definition.
Auto-decoding of headers via given format-patterns (defined via __hdr__)
Auto-decoding of body-handlers like IP -> parse IP-data -> add TCP-handler to IP -> parse TCP-data..
Access of fields via “layer1.key” notation
- Some members can be set/retrieved using convenient string-represenations beneath the
byte-represenation (see Ethernet or IP). This is done by appending “_s” to the attributename: obj.key_s = “stringval” string_var = obj.key_s Convenient access should be set via: varname_s = pypacker.Packet._get_property_XXX(“varname”)
Access of higher layers via layer1.layer2.layerX or “layer1[layerX]” notation
Concatination via “layer1 + layer2 + layerX”
- There are two types of headers:
- Static (same order, pre-defined header-names, constant format,
can be extended by inserting new ones at arbitrary positions)
- Dynamic (Packet based or textual protocol-headers, changes in format, length and order)
These header got format “None” (auto-set when adding new header fields)
Usage with Packet: - Define an TriggerList as part of the value in __hdr__ (or add via _XXX_headerfield()) - Packets in this list can be added/set/removed afterwards NOTE: deep-layer packets will be omitted in Packets, adding new headers to sub-packets after adding to a TriggerList is not permitted
Usage for text-based protocols (eg when headername is given by protocol itself like “Host: xyz.org” in HTTP, usage): - define an TriggerList as part of the value in __hdr__ (or add via _XXX_headerfield()) - define _pack() in your TriggerList to reassemble packets (see HTTP). Single values in this list can be represented as tuples like [(key, value), (key, value), ...] - Values in this list can be added/set/removed afterwards
Examples can be found at the ip and tcp-implementations.
Header-values with length < 1 Byte should be set by using properties
Header formats can not be updated directly
Ability to check direction to other Packets via “direction()”
- Generic callback for rare cases eg where upper layer needs
to know about lower ones (like TCP->IP for checksum calculation)
- No correction of given raw packet-data eg checksums when creating a packet from it
(exception: if the packet can’t be build without correct data -> raise exception). The internal state will only be updated on changes to headers or data.
no plausability-checks when changing headers/date manually (type-infos have to be set manually)
checksums are auto-recalculated until set manualy
General rule: less changes to headers/body-data = more performance
New Protocols are added by subclassing Packet and defining fields via “__hdr__” as a list of (name, format, default value) tuples. __byte_order__ can be set to override the default (‘>’). Extending classes should overwrite the “_dissect”-method for diessction the given data.
- pypacker(__init__) -auto called->
- -> _dissect(): has to be overwritten, get to know/verify the real header-structure
- -> (optional): call _add/remove_headerfield() to change header structure -> (optional): call _parse_handler() setting a handler representing an upper-layer
-auto called-> _unpack(): set all header values and data using the given format.
Exceptionally a callback can be used for downward leyer signaling like eg TCP -> IP for checksum calculation.
Examples:
>>> class Foo(Packet):
... __hdr__ = (("foo", "I", 1), ("bar", "H", 2), ("baz", "4s", "quux"))
...
>>> foo = Foo(bar=3)
>>> foo
Foo(bar=3)
>>> foo.bin()
b"