python packet parser in simple strucutured format
PyPISS is a simple packet parser generator framework inspired by P4 Lang
If you tired about making every packet formats or,
If you want to make your own packet format (such as researching, game socket io, etc…)
This framework may be helpful for creating packet builder/parser.
name | c-style type name | size(bytes) | note |
---|---|---|---|
bool | bool (not same size) | 1bit | True / False |
char | signed char | 1 | |
byte | unsigned char | 1 | |
short | signed short | 2 | |
u_short | unsigned short | 2 | |
int | signed long int | 4 | |
u_int | unsigned long int | 4 | |
long | signed long long int | 8 | |
u_long | unsigned long long int | 8 | |
float | float | 4 | |
double | double | 8 |
name | c-style type name | size(bytes) | note |
---|---|---|---|
str(size) | char[] | vary | return in python string |
bytes(size) | unsigned char[] | vary | return in byte array |
arr(size) | array of type | vary | return in list, must use with type |
name | c-style type name | size(bytes) | note |
---|---|---|---|
ipv4 | byte[4] | 4 | return in IPv4 python class |
ipv6 | short[8] | 16 | return in IPv6 python class |
hmac | byte[6] | 6 | return in HMAC python class |
pip install pypiss
pip3 install pypiss
import enum.Enum
from pypiss.PacketStructure import *
from pypiss.PacketUtil import CRC32
class EtherType(Enum):
IPV4 = 0x0800,
ARP = 0x0806
class EthernetFrame(PacketHeader):
_field = [
field("dst").hmac(),
field("src").hmac(),
field("ether_type").u_short().enum(EtherType),
field("payload").bytes(), # if skip to define size, auto calculated from rest bytes
field("crc").int().val(CRC32.make(['payload'])).check() # if skip to define check function, use same logic from make
]
class IPv4Frame(PacketHeader):
_field = [
field("src").ipv4(),
field("dst").ipv4(),
...,
field("len").u_short(),
...,
field("payload").bytes()
]
def parse(self, data):
self.
# In this function, all fields except payload should be assigned
class PingPongFrame(PacketHeader):
_field = [
field(type).byte(bits=3),
payload()
]
See in ‘Issues’
I always need your helps.
don’t be shy, don’t hesitate, just fork & pull request thx!
If you have any suggestion (feature request, etc…), feel free to contact me!