-
Notifications
You must be signed in to change notification settings - Fork 42
/
dissector.py
executable file
·719 lines (660 loc) · 30.9 KB
/
dissector.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import json # json formatting module
import binascii # this class to handle the hex/ascii converting
from scapy.all import Packet, rdpcap, ConditionalField, Emph, conf
'''
imported files from Scapy library
'''
from scapy.layers.dot11 import *
from scapy.layers.ir import *
from scapy.layers.ppp import *
from scapy.layers.gprs import *
from scapy.layers.mobileip import *
from scapy.layers.smb import *
from scapy.layers.bluetooth import *
from scapy.layers.isakmp import *
from scapy.layers.radius import *
from scapy.layers.hsrp import *
from scapy.layers.netbios import *
from scapy.layers.snmp import *
from scapy.layers.dhcp6 import *
from scapy.layers.l2 import *
from scapy.layers.rip import *
from scapy.layers.inet6 import *
from scapy.layers.netflow import *
from scapy.layers.tftp import *
from scapy.layers.dhcp import *
from scapy.layers.l2tp import *
from scapy.layers.rtp import *
from scapy.layers.inet import *
from scapy.layers.ntp import *
from scapy.layers.x509 import *
from scapy.layers.dns import *
from scapy.layers.llmnr import *
from scapy.layers.sebek import *
from scapy.layers.pflog import *
from scapy.layers.dot11 import *
from scapy.layers.mgcp import *
from scapy.layers.skinny import *
'''
import the protocols classes
'''
from ftp import *
from http import *
from imap import *
from irc import *
from pop import *
from sip import *
from smtp import *
from ssh import *
from telnet import *
def is_created_session(Src, Dst, SPort, DPort):
"""
this method is used for purpose of tcp stream reassemble,
for checking if this is a new session of not.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
"""
i = 0
while i < len(dissector.Dissector.preprocess_sessions):
if Src == dissector.Dissector.preprocess_sessions[i][0]\
and Dst == dissector.Dissector.preprocess_sessions[i][1]\
and SPort == dissector.Dissector.preprocess_sessions[i][2]\
and DPort == dissector.Dissector.preprocess_sessions[i][3]:
return True
i = i + 1
return False
def create_session(Src, Dst, SPort, DPort, expected_seq):
"""
this method is used for purpose of tcp stream reassemble,
for creating a new session.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param stream: the initial packet
@param expected_seq: sequence number
"""
if not is_created_session(Src, Dst, SPort, DPort):
dissector.Dissector.preprocess_sessions.append(\
[Src, Dst, SPort, DPort, expected_seq])
def build_stream(Src, Dst, SPort, DPort, stream):
"""
this method is used for purpose of tcp stream reassemble,
for appending a new packet.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param stream: the current packet
"""
i = 0
while i < len(dissector.Dissector.preprocess_sessions):
if Src == dissector.Dissector.preprocess_sessions[i][0]\
and Dst == dissector.Dissector.preprocess_sessions[i][1] and\
SPort == dissector.Dissector.preprocess_sessions[i][2] and\
DPort == dissector.Dissector.preprocess_sessions[i][3]:
dissector.Dissector.preprocess_sessions[i][4] =\
dissector.Dissector.preprocess_sessions[i][4].append_data(\
Src, Dst, SPort, DPort, stream)
break
i = i + 1
def get_stream(Src, Dst, SPort, DPort, obj):
"""
this method is used for purpose of tcp stream reassemble,
for retrieving a stream or packet.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param obj: last packet to be appended
"""
i = 0
while i < len(dissector.Dissector.sessions):
if Src == dissector.Dissector.sessions[i][0] and\
Dst == dissector.Dissector.sessions[i][1] and\
SPort == dissector.Dissector.sessions[i][2] and\
DPort == dissector.Dissector.preprocess_sessions[i][3]:
if dissector.Dissector.sessions[i][4].seq == obj.seq:
return dissector.Dissector.sessions[i][4].pkt
i = i + 1
return -1
def is_stream_end(Src, Dst, SPort, DPort, obj):
"""
this method is used for purpose of tcp stream reassemble,
for checking whether if this is the last packet in the stream or not.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param obj: last packet in stream.
"""
i = 0
while i < len(dissector.Dissector.sessions):
if Src == dissector.Dissector.sessions[i][0] and\
Dst == dissector.Dissector.sessions[i][1] and\
SPort == dissector.Dissector.sessions[i][2] and\
DPort == dissector.Dissector.sessions[i][3]:
if dissector.Dissector.sessions[i][4].seq == obj.seq:
return True
i = i + 1
return False
def check_stream(Src, Dst, SPort, DPort, Seq, s):
"""
this method is used for purpose of tcp stream reassemble,
for checking whether if this is the last packet in the stream or not.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param seq: sequence number
@param s: packet payload to create a new session or to be appended in an existed session.
"""
if not dissector.is_created_session(Src, Dst, SPort, DPort):
seqn = Seq
stream = dissector.Stream(s, seqn)
dissector.create_session(Src, Dst, SPort, DPort, stream)
elif dissector.is_created_session(Src, Dst, SPort, DPort):
seqn = Seq
stream = dissector.Stream(s, seqn)
dissector.build_stream(Src, Dst, SPort, DPort, stream)
if len(dissector.Dissector.sessions) > 0:
if dissector.is_stream_end(Src, Dst, SPort, DPort, stream):
s = dissector.get_stream(Src, Dst, SPort, DPort, stream)
if not s == -1:
return s
return -1
class Stream:
"""
this class is for tcp reassembling
"""
pkt = ""
seq = -1
length_of_last_packet = -1
stream = False
def __init__(self, pkt, seq):
"""
this constructor is used for purpose of tcp stream reassemble,
for initializing tcp packets.
@param pkt: packet payload
@param push: specify if push flag is true or false
@param seq: sequence number
"""
self.stream = False
self.pkt = pkt
self.seq = seq
self.length_of_last_packet = len(pkt)
def append_data(self, Src, Dst, SPort, DPort, obj):
"""
this method is used for purpose of tcp stream reassemble,
for appending a packet to an existing stream.
@param Src: source ip address
@param Dst: destination ip address
@param SPort: source port number
@param DPort: destination port number
@param obj: last packet in stream.
"""
if self.seq + self.length_of_last_packet == obj.seq:
self.stream = True
self.append_packet(obj.pkt)
self.change_seq(obj.seq)
self.length_of_last_packet = len(obj.pkt)
return self
def append_packet(self, pkt):
"""
this method is used for purpose of tcp stream reassemble,
for appending a packet payload to an existing stream.
@param pkt: packet payload.
"""
self.pkt = self.pkt + pkt
def change_seq(self, seq):
"""
this method is used for purpose of tcp stream reassemble,
for the last packet sequence in the stream.
@param seq: sequence number.
"""
self.seq = seq
def int2bin(n, count=16):
"""
this method converts integer numbers to binary numbers
@param n: the number to be converted
@param count: the number of binary digits
"""
return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
class Dissector(Packet):
"""
this is the main class of this library
Note:
implemented protocols like http,sip, (usually or sometimes) return binary
data, and in some other cases return human readable data,
so i have decided to make these protocols return the data represented as
a hex values, you may want to have its payload in ascii too if so then
use get_ascii()
"""
packet = None
type = 0
preprocess_sessions = []
sessions = []
preprocess_done = False
default_download_folder_changed = False
path = ""
def change_dfolder(self, path):
dissector.Dissector.default_download_folder_changed = True
if not path[len(path) - 1] == "/" and not path[len(path) - 1] == "\\":
path = path + "/"
dissector.Dissector.path = path
def recalculate_seq(self):
i = 0
while i < len(dissector.Dissector.sessions):
Dissector.sessions[i][4].seq = Dissector.sessions[i][4].seq -\
Dissector.sessions[i][4].length_of_last_packet
i = i + 1
def get_ascii(self, hexstr):
"""
get hex string and returns ascii chars
@param hexstr: hex value in str format
"""
return binascii.unhexlify(hexstr)
def defined_protocol(self, name):
if name.startswith("tcp") and name.endswith("tcp") or\
name.startswith("udp") and name.endswith("udp") or\
name.startswith("icmp") and name.endswith("icmp") or\
name.startswith("dns") and name.endswith("dns") or\
name.startswith("http") and name.endswith("http") or\
name.startswith("ftp") and name.endswith("ftp") or\
name.startswith("irc") and name.endswith("irc") or\
name.startswith("smb") and name.endswith("smb") or\
name.startswith("sip") and name.endswith("sip") or\
name.startswith("telnet") and name.endswith("telnet") or\
name.startswith("smtp") or name.startswith("ssh") or\
name.startswith("imap") and name.endswith("imap") or\
name.startswith("pop") and name.endswith("pop"):
return True
def clean_out(self, value):
value = value.rstrip()
value = value.lstrip()
if value.startswith("'") and value.endswith("'"):
return value[1:-1]
elif value.startswith("'") and not value.endswith("'"):
return value[1:]
elif value.endswith("'") and not value.startswith("'"):
return value[:-1]
else:
return value
def dissect(self, packet):
"""
this is the main method in the library, which dissects packets and
returns them as a list of protocols' fields.
@param pcapfile: path to a pcap/cap library
"""
ct = conf.color_theme
flds = []
flds.append(ct.layer_name(packet.name))
for f in packet.fields_desc:
if isinstance(f, ConditionalField) and not f._evalcond(self):
continue
if isinstance(f, Emph) or f in conf.emph:
ncol = ct.emph_field_name
vcol = ct.emph_field_value
else:
ncol = ct.field_name
vcol = ct.field_value
fvalue = packet.getfieldval(f.name)
flds.append((ncol(f.name), vcol(f.i2repr(self, fvalue))))
return flds
def is_printable(self, f):
if isinstance(f, tuple) and not f[1] == "''" and not\
f[1] == '' and not f[1] == "" and not f[1] == [] and not\
f[1] == '[]' and not f[1] == "[]" and len(f[1]) > 0:
return True
return False
def __getattr__(self, attr):
if self.initialized:
fld, v = self.getfield_and_val(attr)
if fld is not None:
return fld.i2h(self, v)
return v
def seq_analysis(self, pcapfile):
"""
this method act as an interface for the dissect() method.
and to represents the data in the required format.
@param pcapfile: path to a pcap/cap library
"""
packetslist = rdpcap(pcapfile)
pktsfields = []
protocols = []
entry = {}
recognized = False
for pkt in packetslist:
firstlayer = True
if pkt:
if firstlayer:
firstlayer = False
self.packet = pkt
fields = self.dissect(self.packet)
load = pkt
while load.payload:
load = load.payload
self.packet = load
fields = self.dissect(self.packet)
if fields[0]:
if fields[0] == "NoPayload":
break
def dissect_pkts(self, pcapfile):
"""
this method act as an interface for the dissect() method.
and to represents the data in the required format.
@param pcapfile: path to a pcap/cap library
"""
self.seq_analysis(pcapfile)
Dissector.sessions = Dissector.preprocess_sessions
Dissector.preprocess_sessions = []
Dissector.preprocess_done = True
packetslist = rdpcap(pcapfile)
pktsfields = []
protocols = []
entry = {}
recognized = False
for pkt in packetslist:
firstlayer = True
if pkt:
if firstlayer:
firstlayer = False
self.packet = pkt
fields = self.dissect(self.packet)
j = 1
entry = {}
while j < len(fields):
if self.is_printable(fields[j]):
entry[fields[j][0]] = fields[j][1]
j = j + 1
i = 0
while i < len(protocols):
if fields[0] in protocols[i]:
protocols[i].append(entry)
break
elif fields[0] not in protocols[i] and \
i == len(protocols) - 1:
protocols.append([fields[0]])
protocols[i + 1].append(entry)
break
i = i + 1
if len(protocols) == 0:
protocols.append([fields[0]])
protocols[0].append(entry)
load = pkt
while load.payload:
load = load.payload
self.packet = load
fields = self.dissect(self.packet)
entry = {}
if fields[0]:
if fields[0] == "NoPayload":
break
j = 1
first = True
if not recognized:
entry = {}
while j < len(fields):
if self.is_printable(fields[j]):
if fields[0] == "UDP":
recognized = True
entry["src"] = load.underlayer.fields["src"]
entry["dst"] = load.underlayer.fields["dst"]
entry["sdport"] = load.fields["sport"]
entry["dport"] = load.fields["dport"]
if fields[0] == "TCP":
recognized = True
entry["src"] = load.underlayer.fields["src"]
entry["dst"] = load.underlayer.fields["dst"]
entry["sdport"] = load.fields["sport"]
entry["dport"] = load.fields["dport"]
if fields[0] == "DNS":
recognized = True
qdfield = None
anfield = None
type = None
name = None
pname = None
found = False
entry = []
if load.fields["qd"]:
for element in fields:
if "qd" in element:
qdfield = element[1]
if qdfield.count("|") == 1:
line = qdfield.split()
for t in line:
if t.startswith("qname="):
found = True
name = t[6:]
if t.startswith("qtype="):
found = True
type = t[6:]
if found:
entry.append(\
{"name": name, "type": type})
found = False
if qdfield.count("|") > 1:
entry["name"] = []
qlist = qdfield.split(" |")
for record in qlist:
line = record.split()
for t in line:
if t.startswith("qname="):
found = True
name = t[6:]
if t.startswith("qtype="):
found = True
type = t[6:]
if found:
entry.append(\
{"name": name, "type": type})
found = False
if load.fields["an"]:
for element in fields:
if "an" in element:
anfield = element[1]
if anfield.count("|") == 1:
line = anfield.split()
for t in line:
if t.startswith("rrname="):
found = True
name = t[7:]
if t.startswith("type="):
found = True
type = t[5:]
if t.startswith("rdata="):
found = True
pname = t[6:]
if found:
entry.append(\
{"name": name, "type": type, "pname": pname})
found = False
if anfield.count("|") > 1:
alist = anfield.split(" |")
for record in alist:
line = record.split()
for t in line:
if t.startswith("rrname="):
found = True
name = t[7:]
if t.startswith("type="):
found = True
type = t[5:]
if t.startswith("rdata="):
found = True
pname = t[6:]
if found:
entry.append(\
{"name": name[1:-2], "type": type, "pname": pname[1:-1]})
found = False
if isinstance(fields[0], str) and\
fields[0].startswith("http"):
recognized = True
if isinstance(fields[j][1], str):
if first and not fields[j][0][:-2] ==\
"unknown-header(s)" and\
not fields[j][0][:-2] == "message-body":
entry[fields[j][0][:-2]] =\
self.clean_out(fields[j][1][len(fields[j][0]) + 1:-1])
elif first and fields[j][0][:-2] ==\
"unknown-header(s)":
entry[fields[j][0][:-2]] =\
self.clean_out(fields[j][1])
elif first and fields[j][0][:-2] ==\
"message-body":
entry[fields[j][0][:-2]] = fields[j][1]
else:
entry[fields[j][0]] =\
self.clean_out(fields[j][1])
if isinstance(fields[0], str) and\
fields[0].startswith("sip"):
recognized = True
if isinstance(fields[j][1], str):
if first and not fields[j][0][:-2] ==\
"unknown-header(s)" and not\
fields[j][0][:-2] == "message-body":
entry[fields[j][0][:-2]] =\
self.clean_out(fields[j][1][len(fields[j][0]) + 1:-1])
elif first and fields[j][0][:-2] ==\
"unknown-header(s)":
entry[fields[j][0][:-2]] =\
self.clean_out(fields[j][1])
elif first and fields[j][0][:-2] ==\
"message-body":
entry[fields[j][0][:-2]] =\
fields[j][1][1:-1]
else:
entry[fields[j][0]] = self.clean_out(\
self.clean_out(fields[j][1]))
if isinstance(fields[0], str) and\
fields[0].startswith("smtp"):
recognized = True
if fields[j][0].startswith("command") and\
fields[j][1].startswith("['DATA', '") and\
fields[j][1].endswith("']"):
entry["data"] = fields[j][1][10:-2]
entry["type"] = "data"
elif fields[j][0].startswith("response") and\
fields[j][0].endswith("response"):
result = fields[j][1]
result = "[" +\
result[1:-1].replace("'", '"') + "]"
try:
result = json.loads(result)
except Exception:
None
entry[fields[j][0]] = result
entry["type"] = "response"
elif fields[j][0].startswith("command") or\
fields[j][0].startswith("argument"):
if isinstance(entry, dict):
entry[fields[j][0]] =\
fields[j][1][1:-1]
if not "type" in entry:
entry["type"] = "request"
if j == len(fields) - 1:
temp = ""
if entry["type"] == "request":
if "command" in entry:
temp = "command: " +\
entry["command"]
if entry["command"] ==\
"DATA":
None
if "argument" in entry:
temp = temp +\
", argument: " + entry["argument"]
if "type" in entry:
temp = temp +\
", type: " + entry["type"]
entry = temp
elif isinstance(entry, str):
if len(entry) > 0:
entry = entry + ", " +\
fields[j][0] + ": " + fields[j][1][1:-1]
else:
entry = fields[j][0] + ": " +\
fields[j][1][1:-1]
if j == len(fields) - 1:
entry = entry + ", type: request"
else:
entry = entry + fields[j][0] +\
": " + fields[j][1][1:-1]
if j == len(fields) - 1:
entry = entry + ", type: request"
if isinstance(fields[0], str) and\
fields[0].startswith("ftp"):
recognized = True
if isinstance(entry, dict):
entry = ""
if len(entry) > 0:
entry = entry + ", " +\
fields[j][0] + ": " + self.clean_out(fields[j][1][1:-1])
else:
entry = entry + fields[j][0] +\
": " + self.clean_out(fields[j][1][1:-1])
if j == len(fields) - 1 and\
pkt.payload.payload.fields["sport"] == 21 or\
pkt.payload.payload.fields["sport"] == 20:
entry = entry + ", type: response"
elif j == len(fields) - 1 and\
pkt.payload.payload.fields["dport"] == 21 or\
pkt.payload.payload.fields["dport"] == 20:
entry = entry + ", type: request"
if isinstance(fields[0], str) and\
fields[0].startswith("imap"):
recognized = True
entry = entry + fields[j][1] + " "
if isinstance(fields[0], str) and\
fields[0].startswith("pop"):
recognized = True
entry = entry + fields[j][1] + " "
if isinstance(fields[0], str) and\
fields[0].startswith("irc"):
recognized = True
entry = fields[j][1][1:-1]
if isinstance(fields[0], str) and\
fields[0].startswith("telnet"):
recognized = True
entry = fields[j][1][:-1]
if isinstance(fields[0], str) and\
fields[0].startswith("ssh"):
recognized = True
entry = fields[j][1]
if j == len(fields) - 1 and\
pkt.payload.payload.fields["sport"] == 22:
entry = entry + ", type: response"
elif j == len(fields) - 1 and\
pkt.payload.payload.fields["dport"] == 22:
entry = entry + ", type: request"
if not recognized:
entry[fields[j][0]] =\
self.clean_out(fields[j][1])
recognized = False
j = j + 1
i = 0
while i < len(protocols):
if fields[0].lower() in protocols[i]:
if len(entry) > 0:
protocols[i].append(entry)
break
elif fields[0].lower() not in protocols[i] and \
i == len(protocols) - 1:
protocols.append([fields[0].lower()])
if len(entry) > 0:
protocols[i + 1].append(entry)
break
i = i + 1
if len(protocols) == 0:
protocols.append([fields[0].lower()])
if len(entry) > 0:
protocols[0].append(entry)
dproto = {}
i = 0
for proto in protocols:
if self.defined_protocol(proto[0].lower()):
dproto[proto[0].lower()] = proto[1:]
return dproto