-
Notifications
You must be signed in to change notification settings - Fork 25
/
community-id.py
executable file
·388 lines (324 loc) · 14.3 KB
/
community-id.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#! /usr/bin/python
"""
This is a reference implementation of community ID hashes on network
flows. Pass any pcaps (not pcapng) to the script and it will report
the following for each packet in the traces, separated by "|":
- the timestamp
- the community ID
- a flow tuple summary
Currently supported protocols include IP, IPv6, ICMP, ICMPv6, TCP,
UDP, SCTP.
Please note: the protocol parsing implemented in this script relies
on the dpkt module and is somewhat simplistic:
- dpkt seems to struggle with some SCTP packets, for which it fails
to register SCTP even though its header is correctly present.
- The script doesn't try to get nested network layers (IP over IPv6,
IP in IP, etc) right. It expects either IP or IPv6, and it expects
a transport-layer protocol (including the ICMPs here) as the
immediate next layer.
"""
import argparse
import base64
import hashlib
import socket
import struct
import sys
try:
import dpkt
except ImportError:
print('This needs the dpkt Python module')
sys.exit(1)
from dpkt.ethernet import Ethernet #pylint: disable=import-error
from dpkt.ip import IP #pylint: disable=import-error
from dpkt.ip6 import IP6 #pylint: disable=import-error
from dpkt.icmp import ICMP #pylint: disable=import-error
from dpkt.icmp6 import ICMP6 #pylint: disable=import-error
from dpkt.tcp import TCP #pylint: disable=import-error
from dpkt.udp import UDP #pylint: disable=import-error
from dpkt.sctp import SCTP #pylint: disable=import-error
TRANSPORT_PROTOS = [ICMP, ICMP6, TCP, UDP, SCTP]
# ---- Code ------------------------------------------------------------
class ICMPHelper(object):
"""
Helper class for ICMP-related port mappings.
"""
@staticmethod
def packet_get_v4_port_equivalents(pkt):
"""
This function takes the type (and code) of an ICMP packet and
derives transport layer port-pair equivalents. See the
analyzer::icmp::ICMP4_counterpart() function (and its IPv6
equivalent) in the Bro source tree (ICMP.cc) for additional
detail.
Returns a triplet of (source port, dest port, is_one_way).
"""
mapper = {
dpkt.icmp.ICMP_ECHO: dpkt.icmp.ICMP_ECHOREPLY,
dpkt.icmp.ICMP_ECHOREPLY: dpkt.icmp.ICMP_ECHO,
dpkt.icmp.ICMP_TSTAMP: dpkt.icmp.ICMP_TSTAMPREPLY,
dpkt.icmp.ICMP_TSTAMPREPLY: dpkt.icmp.ICMP_TSTAMP,
dpkt.icmp.ICMP_INFO: dpkt.icmp.ICMP_INFOREPLY,
dpkt.icmp.ICMP_INFOREPLY: dpkt.icmp.ICMP_INFO,
dpkt.icmp.ICMP_RTRSOLICIT: dpkt.icmp.ICMP_RTRADVERT,
dpkt.icmp.ICMP_RTRADVERT: dpkt.icmp.ICMP_RTRSOLICIT,
dpkt.icmp.ICMP_MASK: dpkt.icmp.ICMP_MASKREPLY,
dpkt.icmp.ICMP_MASKREPLY: dpkt.icmp.ICMP_MASK,
}
try:
return pkt[ICMP].type, mapper[pkt[ICMP].type], False
except KeyError:
return pkt[ICMP].type, pkt[ICMP].code, True
@staticmethod
def packet_get_v6_port_equivalents(pkt):
"""
This is the same as packet_get_v4_port_equivalents, but for ICMPv6.
"""
mapper = {
dpkt.icmp6.ICMP6_ECHO_REQUEST: dpkt.icmp6.ICMP6_ECHO_REPLY,
dpkt.icmp6.ICMP6_ECHO_REPLY: dpkt.icmp6.ICMP6_ECHO_REQUEST,
dpkt.icmp6.ND_ROUTER_SOLICIT: dpkt.icmp6.ND_ROUTER_ADVERT,
dpkt.icmp6.ND_ROUTER_ADVERT: dpkt.icmp6.ND_ROUTER_SOLICIT,
dpkt.icmp6.ND_NEIGHBOR_SOLICIT: dpkt.icmp6.ND_NEIGHBOR_ADVERT,
dpkt.icmp6.ND_NEIGHBOR_ADVERT: dpkt.icmp6.ND_NEIGHBOR_SOLICIT,
dpkt.icmp6.MLD_LISTENER_QUERY: dpkt.icmp6.MLD_LISTENER_REPORT,
dpkt.icmp6.MLD_LISTENER_REPORT: dpkt.icmp6.MLD_LISTENER_QUERY,
dpkt.icmp6.ICMP6_WRUREQUEST: dpkt.icmp6.ICMP6_WRUREPLY,
dpkt.icmp6.ICMP6_WRUREPLY: dpkt.icmp6.ICMP6_WRUREQUEST,
# Home Agent Address Discovery Request Message and reply
144: 145,
145: 144,
}
try:
return pkt[ICMP6].type, mapper[pkt[ICMP6].type], False
except KeyError:
return pkt[ICMP6].type, pkt[ICMP6].code, True
class PacketKey(object):
"""
A pseudo-struct, since we're in Python. dpkt already ensures that
the network addresses coming in are in network byte order. The
constructor ensures that the additional values become NBO values
as well.
"""
def __init__(self, src_addr, dst_addr, proto, src_port=0, dst_port=0):
self.src_addr = src_addr
self.dst_addr = dst_addr
self.proto = proto
self.src_port = src_port
self.dst_port = dst_port
# Store the protocol number as 8-bit int
if isinstance(self.proto, int):
self.proto = struct.pack('B', self.proto)
# Store the port numbers as 16-bit, NBO
if isinstance(src_port, int):
self.src_port = struct.pack('!H', self.src_port)
if isinstance(dst_port, int):
self.dst_port = struct.pack('!H', self.dst_port)
class CommunityIDHasher(object):
"""
Toplevel class for the functionality in this module.
"""
def __init__(self, comm_id_seed=0, use_base64=True, verbose=False):
self.comm_id_seed = comm_id_seed
self.use_base64 = use_base64
self.verbose = verbose
def pcap_handle(self, pcapfile):
"""
This function processes the given pcap file name, reporting the
community ID string for every packet in the trace.
"""
with open(pcapfile, 'rb') as hdl:
reader = dpkt.pcap.Reader(hdl)
for tstamp, pktdata in reader:
self._packet_handle(tstamp, pktdata)
def _packet_get_key(self, pkt):
"""
Returns a populated hashable "struct" (a PacketKey instance) for
the given packet.
"""
def is_lt(addr1, addr2, port1=0, port2=0):
return addr1 < addr2 or (addr1 == addr2 and port1 < port2)
if IP in pkt:
saddr = pkt[IP].src
daddr = pkt[IP].dst
elif IP6 in pkt:
saddr = pkt[IP6].src
daddr = pkt[IP6].dst
else:
return None
if TCP in pkt:
if is_lt(saddr, daddr, pkt[TCP].sport, pkt[TCP].dport):
return PacketKey(saddr, daddr, dpkt.ip.IP_PROTO_TCP,
pkt[TCP].sport, pkt[TCP].dport)
return PacketKey(daddr, saddr, dpkt.ip.IP_PROTO_TCP,
pkt[TCP].dport, pkt[TCP].sport)
if UDP in pkt:
if is_lt(saddr, daddr, pkt[UDP].sport, pkt[UDP].dport):
return PacketKey(saddr, daddr, dpkt.ip.IP_PROTO_UDP,
pkt[UDP].sport, pkt[UDP].dport)
return PacketKey(daddr, saddr, dpkt.ip.IP_PROTO_UDP,
pkt[UDP].dport, pkt[UDP].sport)
if SCTP in pkt:
if is_lt(saddr, daddr, pkt[SCTP].sport, pkt[SCTP].dport):
return PacketKey(saddr, daddr, dpkt.ip.IP_PROTO_SCTP,
pkt[SCTP].sport, pkt[SCTP].dport)
return PacketKey(daddr, saddr, dpkt.ip.IP_PROTO_SCTP,
pkt[SCTP].dport, pkt[SCTP].sport)
if ICMP in pkt:
port1, port2, is_one_way = ICMPHelper.packet_get_v4_port_equivalents(pkt)
if is_one_way or is_lt(saddr, daddr, port1, port2):
return PacketKey(saddr, daddr, dpkt.ip.IP_PROTO_ICMP, port1, port2)
return PacketKey(daddr, saddr, dpkt.ip.IP_PROTO_ICMP, port2, port1)
if ICMP6 in pkt:
port1, port2, is_one_way = ICMPHelper.packet_get_v6_port_equivalents(pkt)
if is_one_way or is_lt(saddr, daddr, port1, port2):
return PacketKey(saddr, daddr, dpkt.ip.IP_PROTO_ICMP6, port1, port2)
return PacketKey(daddr, saddr, dpkt.ip.IP_PROTO_ICMP6, port2, port1)
if IP in pkt:
if is_lt(saddr, daddr):
return PacketKey(saddr, daddr, pkt[IP].p)
return PacketKey(daddr, saddr, pkt[IP].p)
if IP6 in pkt:
if is_lt(pkt[IP6].src, pkt[IP6].dst):
return PacketKey(pkt[IP6].src, pkt[IP6].dst, pkt[IP].nxt)
return PacketKey(pkt[IP6].dst, pkt[IP6].src, pkt[IP].nxt)
return None
def _packet_parse(self, pktdata):
"""
Parses the protocols in the given packet data and returns the
resulting packet (here, as a dict indexed by the protocol layers
in form of dpkt classes).
"""
layer = Ethernet(pktdata)
pkt = {}
if isinstance(layer.data, IP):
pkt[IP] = layer = layer.data
elif isinstance(layer.data, IP6):
# XXX This does not correctly skip IPv6 extension headers
pkt[IP6] = layer = layer.data
else:
return pkt
if isinstance(layer.data, ICMP):
pkt[ICMP] = layer.data
elif isinstance(layer.data, ICMP6):
pkt[ICMP6] = layer.data
elif isinstance(layer.data, TCP):
pkt[TCP] = layer.data
elif isinstance(layer.data, UDP):
pkt[UDP] = layer.data
elif isinstance(layer.data, SCTP):
pkt[SCTP] = layer.data
return pkt
def _packet_to_str(self, pkt):
"""
Helper that returns flow tuple string of given packet, as-is (no
canonicalization).
"""
parts = []
if IP in pkt:
parts.append(socket.inet_ntop(socket.AF_INET, pkt[IP].src))
parts.append(socket.inet_ntop(socket.AF_INET, pkt[IP].dst))
parts.append(pkt[IP].p)
elif IP6 in pkt:
parts.append(socket.inet_ntop(socket.AF_INET6, pkt[IP6].src))
parts.append(socket.inet_ntop(socket.AF_INET6, pkt[IP6].dst))
parts.append(pkt[IP6].nxt)
if ICMP in pkt:
parts.append(pkt[ICMP].type)
parts.append(pkt[ICMP].code)
elif ICMP6 in pkt:
parts.append(pkt[ICMP6].type)
parts.append(pkt[ICMP6].code)
elif TCP in pkt:
parts.append(pkt[TCP].sport)
parts.append(pkt[TCP].dport)
elif UDP in pkt:
parts.append(pkt[UDP].sport)
parts.append(pkt[UDP].dport)
elif SCTP in pkt:
parts.append(pkt[SCTP].sport)
parts.append(pkt[SCTP].dport)
return ' '.join(str(part) for part in parts)
def _packet_get_comm_id(self, pkt, key):
"""
The actual community ID hash logic. Given the packet and the
"struct" of relevant values, feeds the NBO-ordered values into the
hash, gets the result in base64, prepends the version string,
and returns the result.
"""
hashstate = hashlib.sha1()
def hash_update(data):
if self.verbose:
# Bytes in Python 2.7 is just str and elements are
# also str, so need to get the byte value via ord.
if data and isinstance(data[0], str):
bdata = [ord(b) for b in data]
else:
# Python 3 bytes yield ints
bdata = data
hexbytes = ':'.join('%02x' % b for b in bdata)
self._log(hexbytes + ' ')
hashstate.update(data)
return len(data)
dlen = hash_update(struct.pack('!H', self.comm_id_seed)) # 2-byte seed
dlen += hash_update(key.src_addr) # 4 bytes (v4 addr) or 16 bytes (v6 addr)
dlen += hash_update(key.dst_addr) # 4 bytes (v4 addr) or 16 bytes (v6 addr)
dlen += hash_update(key.proto) # 1 byte for transport proto
dlen += hash_update(struct.pack('B', 0)) # 1 byte padding
if any(proto in pkt for proto in TRANSPORT_PROTOS):
dlen += hash_update(key.src_port) # 2 bytes
dlen += hash_update(key.dst_port) # 2 bytes
self._log('(%d bytes) ' % dlen)
# The data structure we hash should always align on 32-bit
# boundaries.
assert dlen % 4 == 0, 'Hash input not aligned on 32-bit (%d bytes)' % dlen
# The versioning mechanism is currently very simple: we're at
# version one, which doesn't allow customization.
version = '1:'
# Unless the user disabled the feature, base64-encode the
# (binary) hash digest. Otherwise, print the ASCII digest.
if self.use_base64:
res = "{}{}".format(version, base64.b64encode(hashstate.digest()).decode('utf-8'))
else:
res = "{}{}".format(version, hashstate.hexdigest())
self._log('-> {}\n'.format(res))
return res
def _log(self, msg):
"""
Logging helper: in verbose mode, writes given message string to stderr.
"""
if self.verbose:
sys.stderr.write(msg)
def _packet_handle(self, tstamp, pktdata):
"""
All per-packet processing: parsing, key data extraction, hashing,
and community ID production.
"""
def print_result(tstamp, pkt, res):
print('%10.6f | %s | %s' % (tstamp, res, self._packet_to_str(pkt)))
pkt = self._packet_parse(pktdata)
if not pkt:
print_result(tstamp, pkt, '<not IP>')
return
key = self._packet_get_key(pkt)
if key is None:
# Should not happen, caught above!
print_result(tstamp, pkt, '<not IP (???)>')
return
print_result(tstamp, pkt, self._packet_get_comm_id(pkt, key))
def main():
parser = argparse.ArgumentParser(description='Community flow ID reference')
parser.add_argument('pcaps', metavar='PCAP', nargs='+',
help='PCAP packet capture files')
parser.add_argument('--seed', type=int, default=0, metavar='NUM',
help='Seed value for hash operations')
parser.add_argument('--no-base64', action='store_true', default=False,
help="Don't base64-encode the SHA1 binary value")
parser.add_argument('--verbose', action='store_true', default=False,
help='Show verbose output on stderr')
args = parser.parse_args()
hasher = CommunityIDHasher(args.seed, not args.no_base64, args.verbose)
for pcapfile in args.pcaps:
hasher.pcap_handle(pcapfile)
return 0
if __name__ == '__main__':
sys.exit(main())