Return computed CRC-32c checksum.
Return computed Internet Protocol checksum.
Add checksum value to the given value s.
Complete checksum building.
GNU zip
Bases: pypacker.pypacker.Packet
Compress self.data.
Return decompressed payload.
Bases: pypacker.pypacker.Packet
Packet read and write routines for pcap format. See http://wiki.wireshark.org/Development/LibpcapFileFormat
Bases: pypacker.pypacker.Packet
pcap file header.
Bases: pypacker.pypacker.Packet
pcap file header.
Bases: pypacker.pypacker.Packet
pcap packet header.
Bases: pypacker.pypacker.Packet
pcap packet header.
Bases: builtins.object
Simple pcap file reader supporting pcap format. Using iterators this will return (timestamp, bytes) on standard mode and (timestamp, packet) on packet mode. Default timestamp resolution ist nanoseconds.
Bases: builtins.object
Simple pcap writer supporting pcap format. Note: this will use nanosecond timestamp resolution.
Write the given packet’s bytes to file.
bts – bytes to be written ts – timestamp in 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 max_amount – maximum amount of packets to be fetched return – packets received from network as list
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 logic.
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__ - new protocols: don’t use header fields having same names as methods in Packet class
Bases: builtins.Exception
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. __byte_order__ can be set to override the default value ‘>’. Extending classes should overwrite the “_dissect”-method in order to dissect given data.
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
- Convenient access for standard types (MAC, IP address) using string-represenations
This is done by appending “_s” to the attributename: ip.src_s = “127.0.0.1” ip_src_bytes = ip.src
Implementation info: 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)
Format for __hdr__: (“name”, “format”, value)
- Dynamic (Packet based or textual protocol-headers, changes in format, length and order)
Format for __hdr__: (“name”, None, TriggerList) Allowed contents (mutual exclusive): raw bytes, tuples like (key, value), Packets
For raw bytes or tuple-based TriggerLists, _pack() can be overwritten to reassemble the whole header (see ip.py and tcp.py). For changes on other fields resulting from TriggerList-changes, _handle_mod(value) can be overwritten (see ip.py)
Header-values with length < 1 Byte should be set by using properties
Header formats can not be updated directly
Static fields can be deactivated via setting None “obj.field = None”, re-activate by setting a value
Ability to check direction to other Packets via “direction()”
- Generic callback to lower layers for rare cases eg where upper layer needs
to know about lower ones (like TCP (higher layer) ->IP (lower layer) 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.
Checksums are auto-recalculated until set manualy
General rule: less changes to headers/body-data = more performance
- pypacker(__init__) -auto called->
- -> _dissect(): has to be overwritten, get to know/verify the real 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.
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"