Skip to content

Instantly share code, notes, and snippets.

@hetsch
Last active May 14, 2018 08:51
Show Gist options
  • Save hetsch/3256aec992a91d21763bbc2b1c386507 to your computer and use it in GitHub Desktop.
Save hetsch/3256aec992a91d21763bbc2b1c386507 to your computer and use it in GitHub Desktop.
Testing Scapy
from scapy.all import *
class Header(Packet):
name = 'Header'
fields_desc = [
ByteField('cmd', 0),
ByteField('status', 0)
]
class FooBody(Packet):
name = 'FooBody'
fields_desc = [
ByteField('foo_1', 0),
ByteField('foo_2', 0)
]
class BarBody(Packet):
name = 'BarBody'
fields_desc = [
ByteField('bar_1', 0),
ByteField('bar_2', 0)
]
class UnResult(Packet):
name = 'UnResult'
fields_desc = [
ByteField('type', 0)
]
bind_layers(UnResult, FooBody, type=0x00)
bind_layers(UnResult, BarBody, type=0x01)
class Command(Header):
name = 'Result'
fields_desc = Header.fields_desc.copy() + [
PacketField('result', 0, UnResult)
]
if __name__ == '__main__':
headerBytes = b'\x01\x02'
resultBytesFoo = b'\x00\x06\x06'
resultBytesBar = b'\x01\x06\x06'
# Dissecting
c = Command(headerBytes + resultBytesFoo)
assert c.cmd == 0x01
assert c.status == 0x02
assert c.result.type == 0x00
assert c.result.foo_1 == 0x06
assert c.result.foo_2 == 0x06
c = Command(headerBytes + resultBytesBar)
assert c.cmd == 0x01
assert c.status == 0x02
assert c.result.type == 0x01
assert c.result.bar_1 == 0x06
assert c.result.bar_2 == 0x06
# Building
# This works
c = Command(cmd=1, status=2, result=UnResult(b'\x00\x06\x06'))
print(c.show())
c = Command(cmd=1, status=2, result=(UnResult(type=1) / BarBody(bar_1=6, bar_2=6)))
print(c.show())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment