pypacker package

Subpackages

Submodules

pypacker.checksum module

pypacker.checksum.crc32_add(crc, buf)
pypacker.checksum.crc32_cksum(buf)

Return computed CRC-32c checksum.

pypacker.checksum.crc32_done(crc)
pypacker.checksum.fletcher32(data_to_checksum, amount_words)
pypacker.checksum.in_cksum(buf)

Return computed Internet Protocol checksum.

pypacker.checksum.in_cksum_add(s, buf)

Add checksum value to the given value s.

pypacker.checksum.in_cksum_done(s)

Complete checksum building.

pypacker.checksum.unpack_word_be()

S.unpack(buffer) -> (v1, v2, ...)

Return a tuple containing values unpacked according to the format string S.format. Requires len(buffer) == S.size. See help(struct) for more on format strings.

pypacker.pcapng module

Primaly refer:
http://www.winpcap.org/ntar/draft/PCAP-DumpFileFormat.html
TODO:
  • Writer class implementation.

  • Options getter/setter implementation.

  • Support nanosecond.

    Investigate the implementation to support multi interface.

Limitation:

Because the generally considered to only use wireshark, support header referenced to http://wiki.wireshark.org/Development/PcapNg

Mostly the following limitations
  • Only a single section
Support:
SHB v1.0 Data
Not support:
|<- 1st Section ->|<- 2nd Section ->| ... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | SHB v1.0 | Data | SHB v1.1 | Data | ... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Wireshark wiki written as “Capture file will have the following pcap-ng blocks: SHB, IDB, IDB, IDB, EPB, EPB, ..., ISB, ISB, ISB.”.

  • SHB(Section Header Block)
  • IDB(Interface Description Block)
  • EPB(Enhanced Packet Block) <– this is DUMP PACKET
  • ISB(Interface Statistics Block)
Thus, created by assuming the following figure of block format:
SHB IDB IDB EPB EPB ... EPB ISB ISB

( ‘A`)< Only file reading...

TODO: generic interface for different reader/writer

class pypacker.pcapng.EPB(*args, **kwargs)

Bases: pypacker.pypacker.Packet

Enhanced Packet Block (optional)

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

EPB.__hdr__ = (('type', 'I', 6), ('block_length', 'I', 32), ('interface_id', 'I', 0), ('ts_high', 'I', 0), ('ts_low', 'I', 0), ('cap_len', 'I', 0), ('len', 'I', 0))
EPB.block_length

Unpack field ondemand

EPB.cap_len

Unpack field ondemand

EPB.interface_id

Unpack field ondemand

EPB.len

Unpack field ondemand

EPB.ts_high

Unpack field ondemand

EPB.ts_low

Unpack field ondemand

EPB.type

Unpack field ondemand

class pypacker.pcapng.EPB_LE(*args, **kwargs)

Bases: pypacker.pcapng.EPB

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

EPB_LE.block_length

Unpack field ondemand

EPB_LE.cap_len

Unpack field ondemand

EPB_LE.interface_id

Unpack field ondemand

EPB_LE.len

Unpack field ondemand

EPB_LE.ts_high

Unpack field ondemand

EPB_LE.ts_low

Unpack field ondemand

EPB_LE.type

Unpack field ondemand

class pypacker.pcapng.IDB(*args, **kwargs)

Bases: pypacker.pypacker.Packet

Interface Description Block (mandatory)

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

IDB.__hdr__ = (('type', 'I', 1), ('block_length', 'I', 0), ('linktype', 'H', 0), ('reserved', 'H', 0), ('snaplen', 'I', 0))
IDB.block_length

Unpack field ondemand

IDB.linktype

Unpack field ondemand

IDB.reserved

Unpack field ondemand

IDB.snaplen

Unpack field ondemand

IDB.type

Unpack field ondemand

class pypacker.pcapng.IDB_LE(*args, **kwargs)

Bases: pypacker.pcapng.IDB

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

IDB_LE.block_length

Unpack field ondemand

IDB_LE.linktype

Unpack field ondemand

IDB_LE.reserved

Unpack field ondemand

IDB_LE.snaplen

Unpack field ondemand

IDB_LE.type

Unpack field ondemand

class pypacker.pcapng.ISB(*args, **kwargs)

Bases: pypacker.pypacker.Packet

Interface Statistics Block (optional)

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

ISB.__hdr__ = (('type', 'I', 5), ('block_length', 'I', 0), ('interface_id', 'I', 1), ('ts_high', 'I', 0), ('ts_low', 'I', 0))
ISB.block_length

Unpack field ondemand

ISB.interface_id

Unpack field ondemand

ISB.ts_high

Unpack field ondemand

ISB.ts_low

Unpack field ondemand

ISB.type

Unpack field ondemand

class pypacker.pcapng.ISB_LE(*args, **kwargs)

Bases: pypacker.pcapng.ISB

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

ISB_LE.block_length

Unpack field ondemand

ISB_LE.interface_id

Unpack field ondemand

ISB_LE.ts_high

Unpack field ondemand

ISB_LE.ts_low

Unpack field ondemand

ISB_LE.type

Unpack field ondemand

class pypacker.pcapng.OPT(*args, **kwargs)

Bases: pypacker.pypacker.Packet

General option format

__hdr__ = (('code', 'H', 0), ('length', 'H', 0))
code

Unpack field ondemand

length

Unpack field ondemand

class pypacker.pcapng.Reader(fileobj=None, filename=None, lowest_layer=None, filter=None, ts_conversion=True)

Bases: object

class pypacker.pcapng.SHB(*args, **kwargs)

Bases: pypacker.pypacker.Packet

Section Header Block (mandatory)

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

SHB.__hdr__ = (('type', 'I', 168627466), ('block_length', 'I', 0), ('magic', 'I', 439041101), ('v_major', 'H', 1), ('v_minor', 'H', 0), ('section_length', 'Q', 0))
SHB.block_length

Unpack field ondemand

SHB.magic

Unpack field ondemand

SHB.section_length

Unpack field ondemand

SHB.type

Unpack field ondemand

SHB.v_major

Unpack field ondemand

SHB.v_minor

Unpack field ondemand

class pypacker.pcapng.SHB_LE(*args, **kwargs)

Bases: pypacker.pcapng.SHB

class OPT(*args, **kwargs)

Bases: pypacker.pcapng.OPT

code

Unpack field ondemand

length

Unpack field ondemand

SHB_LE.block_length

Unpack field ondemand

SHB_LE.magic

Unpack field ondemand

SHB_LE.section_length

Unpack field ondemand

SHB_LE.type

Unpack field ondemand

SHB_LE.v_major

Unpack field ondemand

SHB_LE.v_minor

Unpack field ondemand

class pypacker.pcapng.SPB(*args, **kwargs)

Bases: pypacker.pypacker.Packet

Simple Packet Block (optional)

__hdr__ = (('type', 'I', 3), ('block_length', 'I', 16), ('len', 'I', 0))
block_length

Unpack field ondemand

len

Unpack field ondemand

type

Unpack field ondemand

class pypacker.pcapng.SPB_LE(*args, **kwargs)

Bases: pypacker.pcapng.SPB

block_length

Unpack field ondemand

len

Unpack field ondemand

type

Unpack field ondemand

class pypacker.pcapng.Writer

Bases: object

pypacker.pcapng._32bit_alignment(offset, length)

pypacker.ppcap module

Packet read and write routines for pcap format. See http://wiki.wireshark.org/Development/LibpcapFileFormat

class pypacker.ppcap.FileHdr(*args, **kwargs)

Bases: pypacker.pypacker.Packet

pcap file header.

__hdr__ = (('magic', 'I', 2712847316), ('v_major', 'H', 2), ('v_minor', 'H', 4), ('thiszone', 'I', 0), ('sigfigs', 'I', 0), ('snaplen', 'I', 1500), ('linktype', 'I', 1))
linktype

Unpack field ondemand

magic

Unpack field ondemand

sigfigs

Unpack field ondemand

snaplen

Unpack field ondemand

thiszone

Unpack field ondemand

v_major

Unpack field ondemand

v_minor

Unpack field ondemand

class pypacker.ppcap.LEFileHdr(*args, **kwargs)

Bases: pypacker.pypacker.Packet

pcap file header.

__hdr__ = (('magic', 'I', 2712847316), ('v_major', 'H', 2), ('v_minor', 'H', 4), ('thiszone', 'I', 0), ('sigfigs', 'I', 0), ('snaplen', 'I', 1500), ('linktype', 'I', 1))
linktype

Unpack field ondemand

magic

Unpack field ondemand

sigfigs

Unpack field ondemand

snaplen

Unpack field ondemand

thiszone

Unpack field ondemand

v_major

Unpack field ondemand

v_minor

Unpack field ondemand

class pypacker.ppcap.LEPktHdr(*args, **kwargs)

Bases: pypacker.pypacker.Packet

pcap packet header.

__hdr__ = (('tv_sec', 'I', 0), ('tv_usec', 'I', 0), ('caplen', 'I', 0), ('len', 'I', 0))
caplen

Unpack field ondemand

len

Unpack field ondemand

tv_sec

Unpack field ondemand

tv_usec

Unpack field ondemand

class pypacker.ppcap.PktHdr(*args, **kwargs)

Bases: pypacker.pypacker.Packet

pcap packet header.

__hdr__ = (('tv_sec', 'I', 0), ('tv_usec', 'I', 0), ('caplen', 'I', 0), ('len', 'I', 0))
caplen

Unpack field ondemand

len

Unpack field ondemand

tv_sec

Unpack field ondemand

tv_usec

Unpack field ondemand

class pypacker.ppcap.Reader(fileobj=None, filename=None, lowest_layer=None, filter=None, ts_conversion=True)

Bases: 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.

close()
get_by_indices(indices)

Return [(timestamp, [bytes|packets]), ...] for the specified indices in packet file starting at 0 for first packet. This method won’t change the current read-pointer.

indices – set of indices like set([0, 1, 2]). Nonexistent indices will be ignored. return – list of (timestamp, [bytes|packets]) at positions given by indices (ordered as in packet source)

is_resolution_nano()
reset()

Reset file pointer to beginning

class pypacker.ppcap.Writer(fileobj=None, filename=None, snaplen=1500, linktype=1)

Bases: object

Simple pcap writer supporting pcap format. Note: this will use nanosecond timestamp resolution.

close()
write(bts, ts=None)

Write the given packet’s bytes to file.

bts – bytes to be written ts – timestamp in Nanoseconds

pypacker.ppcap._filter_dummy(pkt)
pypacker.ppcap.unpack_IIII_be()

S.unpack(buffer) -> (v1, v2, ...)

Return a tuple containing values unpacked according to the format string S.format. Requires len(buffer) == S.size. See help(struct) for more on format strings.

pypacker.ppcap.unpack_IIII_le()

S.unpack(buffer) -> (v1, v2, ...)

Return a tuple containing values unpacked according to the format string S.format. Requires len(buffer) == S.size. See help(struct) for more on format strings.

pypacker.psocket module

Packet read and write routines using network sockets.

class pypacker.psocket.SocketHndl(iface_name='lo', mode=0, timeout=3, buffersize_recv=None, buffersize_send=None)

Bases: object

Simple socket handler for layer 2 and 3 reading/writing.

ETH_P_ALL = 3
MODE_LAYER_2 = 0
MODE_LAYER_3 = 1
close()
recv(size=65536)

return – bytes received from network

recvp(filter_match_recv=None, lowest_layer=<class 'pypacker.layer12.ethernet.Ethernet'>, max_amount=1)

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. Raises socket.timeout on timeout

filter_match_recv – filter as callback function to match packets to be retrieved.
Callback-structure: fct(packet), Return True to accept a specific packet. Raise StopIteration to stop receiving packets, max_amount will match after all.

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

recvp_iter(filter_match_recv=None, lowest_layer=<class 'pypacker.layer12.ethernet.Ethernet'>)
send(bts, dst=None)

Send the given bytes to network.

bts – the bytes to be sent dst – destination for Layer 3 if mode is MODE_LAYER_3

sr(packet_send, max_packets_recv=1, filter=None, lowest_layer=<class 'pypacker.layer12.ethernet.Ethernet'>)

Send a packet and receive answer packets. This will use information retrieved from direction() to retrieve answer packets. Raises socket.timeout on timeout

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. Set to None to accept everything.

lowest_layer – packet class to be used to create new packets

return – packets receives

pypacker.pypacker module

Simple packet creation and parsing logic.

class pypacker.pypacker.Packet(*args, **kwargs)

Bases: object

Base packet class, with metaclass magic to generate members from self.__hdr__ field. This class can be instatiated via:

Packet(byte_string) Packet(key1=val1, key2=val2, ...)

Every packet got a header and a body. Body-data can be raw byte string OR a packet itself (the body handler) which itself stores a packet etc. This continues until a packet only contains raw bytes (highest layer). The following schema illustrates the Packet-structure:

[Packet: headerfield_1 headerfield_2 ... headerfield_N [Body -> Packet:

headerfield_1 ... headerfield_N [Body: -> Packet:

headerfields ... [Body: b”some_bytes”]

]]]

A header definition like __hdr__ = ((“name”, “12s”, b”defaultvalue”),) will define a header field having the name “name”, format “12s” and default value b”defaultvalue” as bytestring. Fields will be added in order of definition. The static variable __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 higher layers via layer1.layer2.layerX or “layer1[layerX]” notation
  • There are three types of headers:
  1. Simple constant fields (constant format)

    Format for __hdr__: (“name”, “format”, value [, True])

  2. Simple dynamic fields (byte string which changes in length)

    Format for __hdr__: (“name”, None, b”bytestring” [, True]) Such types MUST get initiated in _dissect() because there is no way in guessing the correct format when unpacking values!

  3. TriggerList (List containing Packets, bytes like b”xyz” or tuples like (ID, value))

    Format for __hdr__: (“name”, None, TriggerList)

The last value for simple constant and dynamic fields indicates if this is an auto-update field. This will create a variable XXX_au_active for a field XXX which can be used activate/deactivate the auto-update externally and which is read in the bin()-method internally. This variable could be auto-set in bin() by triggering __getattr__ but then it would be triggered on every packet assembly which would be imperformant. - Convenient access for standard types (MAC, IP address) using string-representations

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”)

  • Concatination via “layer1 + layer2 + layerX”

  • Header-values with length < 1 Byte should be set by using properties

  • Activate/deactivate non-TriggerList header fields by setting values (None=deactive, value=active)

  • Checksums (static auto fields in general) are auto-recalculated when calling bin(update_auto_fields=True) (default: active)

    The update-behaviour for every single field can be controlled via “pkt.VARNAME_au_active = [True|False]

  • Ability to check direction to other Packets via “[is_]direction()”

  • Access to next lower/upper layer

  • No correction of given raw packet-data eg checksums when creating a packet from it

    If the packet can’t be parsed without correct data -> raise exception. The internal state will only be updated on changes to headers or data later on

  • General rule: less changes to headers/body-data = more performance

pypacker(bytes)
-> _dissect(): has to be overwritten, get to know/verify the real header-structure
-> (optional): call _init_handler() initiating a handler representing an upper-layer -> (optional): call _init_triggerlist(name, b”bytes”, dissect_callback) to initiate a TriggerList field

-> (optional) on access to simple headers: _unpack() sets all header values -> (optional) on access to TriggerList headers: lazy parsing gets triggered -> (optional) on access to body handler next upper layer gets initiated

pypacker(keyword1=value, ...)
-> (optional) set headers
pypacker()
-> sets standard values for simple headers
DIR_NOT_IMPLEMENTED = 255
DIR_REV = 2
DIR_SAME = 1
DIR_UNKNOWN = 4
bin(update_auto_fields=True)

Return this header and body (including all upper layers) as byte string and reset changed-status.

update_auto_fields – if True auto-update fields like checksums, else leave them be

body_bytes

Return raw data bytes or handler bytes (including all upper layers) if present. This is the same as calling bin() but excluding this header and without resetting changed-status.

body_handler

return – handler object or None if not present.

data

Return raw data bytes or handler bytes (including all upper layers) if present. This is the same as calling bin() but excluding this header and without resetting changed-status.

direction(other)

Check if this layer got a specific direction. Can be overwritten.

return – [DIR_SAME | DIR_REV | DIR_UNKNOWN | DIR_NOT_IMPLEMENTED]

direction_all(other_packet)

Check for direction on ALL layers from this one upwards. This continues upwards until no body handler can be found anymore. The extending class can overwrite direction() to implement an individual check, signature: direction(self, other_packet) return [same as direction_all]

other_packet – Packet to be compared with this Packet return – Bitwise AND-concatination of all directions of ALL layers starting from

this one upwards. Directions are: [DIR_SAME | DIR_REV | DIR_UNKNOWN]. This can be checked via eg “direction_found & DIR_SAME”
dissect_error
dissect_full()

Recursive unpack ALL data inlcuding lazy header etc up to highest layer inlcuding danymic fields.

header_bytes

Return header as byte string.

header_len
hexdump(length=16, only_header=False)

length – amount of bytes per line only_header – if True: just dump header, else header + body (default)

return – hexdump output string for this packet (header or header + body).

highest_layer
is_direction(packet2, direction)

Same as “direction_all()” but using explicit direction to be checked. As direction_all can be DIR_SAME and DIR_REV at the same time, this call is more clearly.

packet2 – packet to be compared to this packet direction – check for this direction (DIR_...) return – True if direction is found in this packet, False otherwise.

is_error_present(error)

Check if one of pypacker.ERROR_XXX is present error – the error to be check against internal error state

classmethod load_handler(clz_add, handler)

Load Packet handler using a shared dictionary.

clz_add – class for which handler has to be added handler – dict of handlers to be set like { id : class }, id can be a tuple of values

lower_layer
lowest_layer
reverse_address()

Reverse source <-> destination address of THIS packet. This is at minimum defined for: Ethernet, IP, TCP, UDP

reverse_all_address()

Reverse source <-> destination address of EVERY packet upwards including this one (reverse_address has to be implemented).

upper_layer

return – handler object or None if not present.

pypacker.pypacker.byte2hex(buf)

Convert a bytestring to a hex-represenation: b‘1234’ -> ‘1234’

pypacker.pypacker.dns_name_decode(name)

DNS domain name decoder (bytes to string)

name – example: b”wwwexamplecom” return – example: “www.example.com.”

pypacker.pypacker.dns_name_encode(name)

DNS domain name encoder (string to bytes)

name – example: “www.example.com” return – example: b’wwwexamplecom’

pypacker.pypacker.get_property_dnsname(var)

Create a get/set-property for a DNS name.

pypacker.pypacker.get_property_ip4(var)

Create a get/set-property for an IP4 address as string-representation.

pypacker.pypacker.get_property_mac(varname)

Create a get/set-property for a MAC address as string-representation.

pypacker.pypacker.get_rnd_ipv4()

Create random ipv4 adress as bytestring

pypacker.pypacker.get_rnd_mac()

Create random mac address as bytestring

pypacker.pypacker.ip4_bytes_to_str(ip_bytes)

Convert ip address from byte representation to 127.0.0.1.

pypacker.pypacker.ip4_str_to_bytes(ip_str)

Convert ip address 127.0.0.1 to byte representation.

pypacker.pypacker.mac_bytes_to_str(mac_bytes)

Convert mac address from byte representation to AA:BB:CC:DD:EE:FF.

pypacker.pypacker.mac_str_to_bytes(mac_str)

Convert mac address AA:BB:CC:DD:EE:FF to byte representation.

pypacker.pypacker.pack_ipv4()

S.pack(v1, v2, ...) -> bytes

Return a bytes object containing values v1, v2, ... packed according to the format string S.format. See help(struct) for more on format strings.

pypacker.pypacker.pack_mac()

S.pack(v1, v2, ...) -> bytes

Return a bytes object containing values v1, v2, ... packed according to the format string S.format. See help(struct) for more on format strings.

pypacker.pypacker.unpack_ipv4()

S.unpack(buffer) -> (v1, v2, ...)

Return a tuple containing values unpacked according to the format string S.format. Requires len(buffer) == S.size. See help(struct) for more on format strings.

pypacker.pypacker.unpack_mac()

S.unpack(buffer) -> (v1, v2, ...)

Return a tuple containing values unpacked according to the format string S.format. Requires len(buffer) == S.size. See help(struct) for more on format strings.

pypacker.pypacker_meta module

class pypacker.pypacker_meta.MetaPacket

Bases: type

This Metaclass is a more efficient way of setting attributes than using __init__. This is done by reading name, format and default value out of a mendatory __hdr__ tuple in every subclass. This configuration is set one time when loading the module (not at instantiation). Attributes can be normally accessed using “obj.field” notation. General note: Callflaw is: __new__ (loading module) -> __init__ (initiate class)

Header defintition example: __hdr__ = (

(“header1”, “H”, 123), # simple static field (“header2”, “H”, None), # simple static field, inactive (“header3”, None, b”xxx”), # simple dynamic field (“header4”, None, None), # simple dynamic field, inactive (“header5”, None, Triggerlist) # TriggerList field

)

For values <1 byte a subheader definition eases up setting/getting those values:

__hdr_sub__ = (
(“header1”,
lambda val: val & 1 # callback to retrieve value lambda obj, val: obj.__setattr__(val & 1) # callback to set value

)

CAUTION: - List et al are _SHARED_ among all instantiated classes! A copy is needed on changes to them - New protocols: header field names must be unique among other variable and method names

pypacker.pypacker_meta.configure_packet_header(t, hdrs, header_fmt)
pypacker.pypacker_meta.configure_packet_header_sub(t, hdrs_sub)
pypacker.pypacker_meta.get_getter(varname, is_field_type_simple=True)

varname – name of the variable to set the property for is_field_type_simple – get property for simple static or dynamic type if True, else TriggerList return – get-property for simple type or triggerlist

pypacker.pypacker_meta.get_setter(varname, is_field_type_simple=True, is_field_static=True)

varname – name of the variable to set the property for is_field_type_simple – get property for simple static or dynamic type if True, else TriggerList is_field_static – if is_field_type_simple is True: get static type (int, fixed size bytes, ...),

else dynamic (format “xs”) which can change in format (eg DNS names)

return – set-property for simple types or triggerlist

pypacker.triggerlist module

TriggerList for handling dynamic headers.

class pypacker.triggerlist.TriggerList(packet, dissect_callback=None, buffer=b'')

Bases: list

List with trigger-capabilities representing a Packet header. This list can contain one type of raw bytes, tuples or packets representing an individual header field. Using bytes or tuples “_pack()” can be overwritten to reassemble bytes.

append(v)
bin()

Output the TriggerLists elements as concatenated bytestring. Custom implementations for tuple-handling can be set by overwriting _pack().

extend(v)
find_pos(search_cb, offset=0)

Find an item-position giving search callback as search criteria.

search_cb – callback to compare values, signature: callback(value) [True|False]
Return True to return value found.

offset – start at index “offset” to search return – index of first element found or None

find_value(search_cb, offset=0)

Same as find_pos() but directly returning found value or None.

insert(pos, v)

pypacker.utils module

Utility functions.

pypacker.utils._load_mac_vendor()

Load oui.txt containing mac->vendor mappings into MAC_VENDOR dictionary. See http://standards.ieee.org/develop/regauth/oui/oui.txt

pypacker.utils.get_available_wlan_channels(iface)

Requirements: iwlist

return – channels as integer list

pypacker.utils.get_entropy(bts, granularity)
pypacker.utils.get_vendor_for_mac(mac)

mac – First bytes of mac address as “AA:BB:CC” (uppercase!) or byte representation b”ª»ÌÝîÿ” return – found vendor string or empty string

pypacker.utils.get_wlan_mode(iface)

return – [MODE_MANAGED | MODE_MONITOR | MODE_UNKNOWN]

pypacker.utils.is_interface_up(iface)

return – [True | False]

pypacker.utils.is_special_mac(mac_str)

Check if this is a special MAC adress (not a client address). Every MAC not found in the official OUI database is assumed to be non-client.

mac_str – Uppercase mac string like “AA:BB:CC[:DD:EE:FF]”, first 3 MAC-bytes are enough

pypacker.utils.set_ethernet_address(iface, ethernet_addr)

iface – interface name ethernet_addr – Ethernet address like “AA:BB:CC:DD:EE:FF”

pypacker.utils.set_interface_mode(iface, monitor_active=None, state_active=None)

Activate/deacivate monitor mode. Requirements: ifconfig, iwconfig

monitor_active – activate/deactivate monitor mode (only for wlan interfaces) active – set interface state

pypacker.utils.switch_wlan_channel(iface, channel, shutdown_prior=False)

Switch wlan channel to channel. Requirements: ifconfig, iwconfig

iface – interface name channel – channel numer to be set as number shutdown_prior – shut down interface prior to setting channel

pypacker.utils.wlan_extract_ap_macs(packet_radiotap, macs_aps)

packet_radiotap – packet macs_aps – set() return – True if an AP-MAC was extracted

pypacker.utils.wlan_extract_possible_client_macs(packet_radiotap, macs_clients)

Extracts client MACs. There is a uncertainty that the found MAC is actually a client MAC due to missing state information so better check against a known-AP list.

packet_radiotap – packet macs_clients – set() return – True if a possible Client-MAC was extracted

pypacker.utils.wlan_is_beacon(ieee80211_pkt)

Module contents