项目作者: BetaS

项目描述 :
python packet parser in simple strucutured format
高级语言: Python
项目地址: git://github.com/BetaS/pypiss.git
创建时间: 2017-03-22T04:18:04Z
项目社区:https://github.com/BetaS/pypiss

开源协议:GNU General Public License v2.0

下载


PyPISS (v0.1)

Python Packet parser generator In Simple Structured format


Intro

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.


Specification

  • Support bit,byte,short,int,string,list and so on …
  • Declare packet’s structure and setting each fields default value or action easily (such as CRC, Flag)
  • Creating packet by declared packet’s structure
  • Automatic validity check from packet’s structure
  • Creating new packet structure by compositing another packet’s structure

Supporting types

1. primitives

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

2. iteratives

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

3. structures

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

Supporting operators

  • {typename}(bits=0) : set field’s type, if bits is 0 then using default bit size.
  • val(val) : set default value for {val} when build
  • val(lambda) : set default value by lambda function when build
  • check(lambda) : check value by lambda function when parse

Installation

  • Python 2.x : pip install pypiss
  • Python 3.x : pip3 install pypiss

Example

IPv4 with Custom Packets

  1. import enum.Enum
  2. from pypiss.PacketStructure import *
  3. from pypiss.PacketUtil import CRC32
  4. class EtherType(Enum):
  5. IPV4 = 0x0800,
  6. ARP = 0x0806
  7. class EthernetFrame(PacketHeader):
  8. _field = [
  9. field("dst").hmac(),
  10. field("src").hmac(),
  11. field("ether_type").u_short().enum(EtherType),
  12. field("payload").bytes(), # if skip to define size, auto calculated from rest bytes
  13. field("crc").int().val(CRC32.make(['payload'])).check() # if skip to define check function, use same logic from make
  14. ]
  15. class IPv4Frame(PacketHeader):
  16. _field = [
  17. field("src").ipv4(),
  18. field("dst").ipv4(),
  19. ...,
  20. field("len").u_short(),
  21. ...,
  22. field("payload").bytes()
  23. ]
  24. def parse(self, data):
  25. self.
  26. # In this function, all fields except payload should be assigned
  27. class PingPongFrame(PacketHeader):
  28. _field = [
  29. field(type).byte(bits=3),
  30. payload()
  31. ]

Roadmap

See in ‘Issues’

  1. make converter p4 lang to piss python structure

Contribution

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!