1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
"""
Ethernet (TCP/IP protocol stack)
"""
from construct import *
from ethernet import MacAddressAdapter
from construct.protocols.layer3.ipv4 import IpAddressAdapter
from binascii import unhexlify
import six
def HwAddress(name):
return IfThenElse(name, lambda ctx: ctx.hardware_type == "ETHERNET",
MacAddressAdapter(Field("data", lambda ctx: ctx.hwaddr_length)),
Field("data", lambda ctx: ctx.hwaddr_length)
)
def ProtoAddress(name):
return IfThenElse(name, lambda ctx: ctx.protocol_type == "IP",
IpAddressAdapter(Field("data", lambda ctx: ctx.protoaddr_length)),
Field("data", lambda ctx: ctx.protoaddr_length)
)
arp_header = Struct("arp_header",
Enum(UBInt16("hardware_type"),
ETHERNET = 1,
EXPERIMENTAL_ETHERNET = 2,
ProNET_TOKEN_RING = 4,
CHAOS = 5,
IEEE802 = 6,
ARCNET = 7,
HYPERCHANNEL = 8,
ULTRALINK = 13,
FRAME_RELAY = 15,
FIBRE_CHANNEL = 18,
IEEE1394 = 24,
HIPARP = 28,
ISO7816_3 = 29,
ARPSEC = 30,
IPSEC_TUNNEL = 31,
INFINIBAND = 32,
),
Enum(UBInt16("protocol_type"),
IP = 0x0800,
),
UBInt8("hwaddr_length"),
UBInt8("protoaddr_length"),
Enum(UBInt16("opcode"),
REQUEST = 1,
REPLY = 2,
REQUEST_REVERSE = 3,
REPLY_REVERSE = 4,
DRARP_REQUEST = 5,
DRARP_REPLY = 6,
DRARP_ERROR = 7,
InARP_REQUEST = 8,
InARP_REPLY = 9,
ARP_NAK = 10
),
HwAddress("source_hwaddr"),
ProtoAddress("source_protoaddr"),
HwAddress("dest_hwaddr"),
ProtoAddress("dest_protoaddr"),
)
rarp_header = Rename("rarp_header", arp_header)
if __name__ == "__main__":
cap1 = unhexlify(six.b("00010800060400010002e3426009c0a80204000000000000c0a80201"))
obj = arp_header.parse(cap1)
print (obj)
print (repr(arp_header.build(obj)))
print ("-" * 80)
cap2 = unhexlify(six.b("00010800060400020011508c283cc0a802010002e3426009c0a80204"))
obj = arp_header.parse(cap2)
print (obj)
print (repr(arp_header.build(obj)))
|