-
Notifications
You must be signed in to change notification settings - Fork 1
/
sniffer-txt.py
1361 lines (1173 loc) · 50 KB
/
sniffer-txt.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# http://seclist.us/simple-network-packet-sniffer-written-in-python.html
import socket, sys, time, platform, struct
# Special lists used for calculations.
lengthList = []
diameterList = []
# Special lists used to hold unpacked info and calculation results.
unpackedInfo = []
calculationList = []
# Check the OS the application is running on.
os = platform.system()
linux = 'Linux'
windows = 'Windows'
def eth(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
# Get Ethernet header using begin and end.
# No need for windows calibration, Ethernet support under Linux only.
begin = 0
end = begin + ethHeaderLength
ethHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# 6s signifies we are unpacking a string of size 6 bytes.
# H signifies we are unpacking an integer of size 2 bytes.
ethHeaderUnpacked = struct.unpack('!6s6sH', ethHeader)
# The first 6s is 6 bytes and contains the destination address.
ethDestAddress = ethHeaderUnpacked[0]
# The second 6s is 6 bytes and contains the source address.
ethSourceAddress = ethHeaderUnpacked[1]
# The first H is 2 bytes and contains the packet length.
ethType = socket.ntohs(ethHeaderUnpacked[2])
# Properly unpack and format the destination address.
ethDestAddress = '%.2x:%.2x:%.2x:%.2x:%.2x:%.2x' % (ord(ethDestAddress[0]), ord(ethDestAddress[1]), ord(ethDestAddress[2]), ord(ethDestAddress[3]), ord(ethDestAddress[4]), ord(ethDestAddress[5]))
# Properly unpack and format the source address.
ethSourceAddress = '%.2x:%.2x:%.2x:%.2x:%.2x:%.2x' % (ord(ethSourceAddress[0]), ord(ethSourceAddress[1]), ord(ethSourceAddress[2]), ord(ethSourceAddress[3]), ord(ethSourceAddress[4]), ord(ethSourceAddress[5]))
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
# Print Ethernet Header
unpackedInfo.append('\n********************\n** Ethernet (MAC) **\n********************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Destination Address: ' + str(ethDestAddress))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Source Address: ' + str(ethSourceAddress))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('EtherType: ' + str(ethType))
else:
if (extractedAttIndex == 1):
return str(ethDestAddress)
if (extractedAttIndex == 2):
return str(ethSourceAddress)
if (extractedAttIndex == 3):
return str(ethType)
def arp(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
arpHeaderLength = 28
# Get ARP header using begin and end.
begin = ethHeaderLength
end = begin + arpHeaderLength
arpHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# H signifies we are unpacking an integer of size 2 bytes.
# B signifies we are unpacking an integer of size 1 byte.
# 6s signifies we are unpacking a string of size 6 bytes.
# 4s signifies we are unpacking a string of size 4 bytes.
arpHeaderUnpacked = struct.unpack('!HHBBH6s4s6s4s', arpHeader)
# The first H is 2 bytes and contains the hardware type.
arpHardwareType = socket.ntohs(arpHeaderUnpacked[0])
# The second H is 2 bytes and contains the protocol type.
arpProtocolType = socket.ntohs(arpHeaderUnpacked[1])
# The first B is 1 byte and contains the hardware address length.
arpHardAddressLength = arpHeaderUnpacked[2]
# The second B is 1 byte and contains the protocol address length.
arpProtAddressLength = arpHeaderUnpacked[3]
# The third H is 2 bytes and contains the operation.
arpOperation = arpHeaderUnpacked[4]
# The first 6s is 6 bytes and contains the sender hardware address.
arpSenderHardAddress = arpHeaderUnpacked[5]
# The first 4s is 4 bytes and contains the sender protocol address.
arpSenderProtAddress = socket.inet_ntoa(arpHeaderUnpacked[6])
# The second 6s is 6 bytes and contains the target hardware address.
arpTargetHardAddress = arpHeaderUnpacked[7]
# The second 4s is 4 bytes and contains the target protocol address.
arpTargetProtAddress = socket.inet_ntoa(arpHeaderUnpacked[8])
# Properly unpack and format the source MAC address.
arpSenderHardAddress = '%.2x:%.2x:%.2x:%.2x:%.2x:%.2x' % (ord(arpSenderHardAddress[0]), ord(arpSenderHardAddress[1]), ord(arpSenderHardAddress[2]), ord(arpSenderHardAddress[3]), ord(arpSenderHardAddress[4]), ord(arpSenderHardAddress[5]))
# Properly unpack and format the destination MAC address.
arpTargetHardAddress = '%.2x:%.2x:%.2x:%.2x:%.2x:%.2x' % (ord(arpTargetHardAddress[0]), ord(arpTargetHardAddress[1]), ord(arpTargetHardAddress[2]), ord(arpTargetHardAddress[3]), ord(arpTargetHardAddress[4]), ord(arpTargetHardAddress[5]))
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
# Print ARP Header
unpackedInfo.append('\n*******************\n******* ARP *******\n*******************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Hardware Type: ' + str(arpHardwareType))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Protocol Type: ' + str(arpProtocolType))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Hardware Address Length: ' + str(arpHardAddressLength))
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Protocol Address Length: ' + str(arpProtAddressLength))
if (extractedAttIndex == 5) or (extractedAttIndex == 0):
unpackedInfo.append('Operation: ' + str(arpOperation))
if (extractedAttIndex == 6) or (extractedAttIndex == 0):
unpackedInfo.append('Sender Hardware Address: ' + str(arpSenderHardAddress))
if (extractedAttIndex == 7) or (extractedAttIndex == 0):
unpackedInfo.append('Sender Protocol Address: ' + str(arpSenderProtAddress))
if (extractedAttIndex == 8) or (extractedAttIndex == 0):
unpackedInfo.append('Target Hardware Address: ' + str(arpTargetHardAddress))
if (extractedAttIndex == 9) or (extractedAttIndex == 0):
unpackedInfo.append('Target Protocol Address: ' + str(arpTargetProtAddress))
# Separator
unpackedInfo.append('\n----------------------------------------')
else:
if (extractedAttIndex == 1):
return str(arpHardwareType)
if (extractedAttIndex == 2):
return str(arpProtocolType)
if (extractedAttIndex == 3):
return str(arpHardAddressLength)
if (extractedAttIndex == 4):
return str(arpProtAddressLength)
if (extractedAttIndex == 5):
return str(arpOperation)
if (extractedAttIndex == 6):
return str(arpSenderHardAddress)
if (extractedAttIndex == 7):
return str(arpSenderProtAddress)
if (extractedAttIndex == 8):
return str(arpTargetHardAddress)
if (extractedAttIndex == 9):
return str(arpTargetProtAddress)
def ip(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
ipHeaderLength = 20
# Get IP header using begin and end.
# Specific Linux and Windows calibration is needed.
if os == linux:
begin = ethHeaderLength
end = begin + ipHeaderLength
elif os == windows:
begin = 0
end = begin + ipHeaderLength
ipHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# B signifies we are unpacking an integer of size 1 byte.
# H signifies we are unpacking an integer of size 2 bytes.
# 4s signifies we are unpacking a string of size 4 bytes.
ipHeaderUnpacked = struct.unpack('!BBHHHBBH4s4s' , ipHeader)
# The first B is 1 byte and contains the version and header length.
# Both are 4 bits each, split ipHeaderUnpacked[0] in "half".
ipVersionAndHeaderLength = ipHeaderUnpacked[0]
ipVersion = ipVersionAndHeaderLength >> 4
ipHeaderLength = ipVersionAndHeaderLength & 0xF
# The second B is 1 byte and contains the service type and ECN.
ipDSCPAndECN = ipHeaderUnpacked[1]
ipDSCP = ipDSCPAndECN >> 2
ipECN = ipDSCPAndECN & 0x3
# The first H is 2 bytes and contains the total length.
ipTotalLength = ipHeaderUnpacked[2]
# The second H is 2 bytes and contains the total length.
ipIdentification = ipHeaderUnpacked[3]
# The third H is 2 bytes and contains the flags and fragment offset.
# Flags is 3 bits and fragment offset is 13 bits.
# Split ipHeaderUnpacked[4].
ipFlagsAndFragmentOffset = ipHeaderUnpacked[4]
ipFlags = ipFlagsAndFragmentOffset >> 13
ipFragmentOffset = ipFlagsAndFragmentOffset & 0x1FFF
# The third B is 1 byte and contains the time to live.
ipTimeToLive = ipHeaderUnpacked[5]
# Our fourth B is 1 byte and contains the protocol.
ipProtocol = ipHeaderUnpacked[6]
# The fourth H is 2 bytes and contains the header checksum.
ipHeaderChecksum = ipHeaderUnpacked[7]
# The first 4s is 4 bytes and contains the source address.
ipSourceAddress = socket.inet_ntoa(ipHeaderUnpacked[8]);
# The second 4s is 4 bytes and contains the dest address.
ipDestAddress = socket.inet_ntoa(ipHeaderUnpacked[9]);
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
# Print IP Header
# Some segments of the header are switched back to hex form because that
# is the format wireshark has it.
unpackedInfo.append('\n********************\n******** IP ********\n********************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Version: ' + str(ipVersion))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Header Length: ' + str(ipHeaderLength) + ' 32-bit words')
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Differentiated Services Code Point: ' + format(ipDSCP, '#04X') + ' , ' + str(ipDSCP))
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Explicit Congestion Notification: ' + format(ipECN, '#04X') + ' , ' + str(ipECN))
if (extractedAttIndex == 5) or (extractedAttIndex == 0):
unpackedInfo.append('Total Length: ' + str(ipTotalLength) + ' bytes')
if (extractedAttIndex == 6) or (extractedAttIndex == 0):
unpackedInfo.append('Identification: ' + format(ipIdentification, '#04X') + ' , ' + str(ipIdentification))
if (extractedAttIndex == 7) or (extractedAttIndex == 0):
unpackedInfo.append('Flags: ' + format(ipFlags, '#04X') + ' , ' + str(ipFlags))
if (extractedAttIndex == 8) or (extractedAttIndex == 0):
unpackedInfo.append('Fragment Offset: ' + str(ipFragmentOffset) + ' eight-byte blocks')
if (extractedAttIndex == 9) or (extractedAttIndex == 0):
unpackedInfo.append('Time to Live: ' + str(ipTimeToLive) + ' hops')
if (extractedAttIndex == 10) or (extractedAttIndex == 0):
unpackedInfo.append('Protocol: ' + str(ipProtocol))
if (extractedAttIndex == 11) or (extractedAttIndex == 0):
unpackedInfo.append('Header Checksum: ' + format(ipHeaderChecksum, '#04X'))
if (extractedAttIndex == 12) or (extractedAttIndex == 0):
unpackedInfo.append('Source Address: ' + str(ipSourceAddress))
if (extractedAttIndex == 13) or (extractedAttIndex == 0):
unpackedInfo.append('Destination Address: ' + str(ipDestAddress))
else:
if (extractedAttIndex == 1):
return str(ipVersion)
if (extractedAttIndex == 2):
return str(ipHeaderLength)
if (extractedAttIndex == 3):
return format(ipDSCP, '#04X')
if (extractedAttIndex == 4):
return format(ipECN, '#04X')
if (extractedAttIndex == 5):
return str(ipTotalLength)
if (extractedAttIndex == 6):
return format(ipIdentification, '#04X')
if (extractedAttIndex == 7):
return format(ipFlags, '#04X')
if (extractedAttIndex == 8):
return str(ipFragmentOffset)
if (extractedAttIndex == 9):
return str(ipTimeToLive)
if (extractedAttIndex == 10):
return str(ipProtocol)
if (extractedAttIndex == 11):
return format(ipHeaderChecksum, '#04X')
if (extractedAttIndex == 12):
return str(ipSourceAddress)
if (extractedAttIndex == 13):
return str(ipDestAddress)
def icmp(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
ipHeaderLength = 20
icmpHeaderLength = 8
# Get ICMP header using begin and end.
# Specific Linux and Windows calibration is needed.
if os == linux:
begin = ethHeaderLength + ipHeaderLength
end = begin + icmpHeaderLength
elif os == windows:
begin = ipHeaderLength
end = begin + icmpHeaderLength
icmpHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# B signifies we are unpacking an integer of size 1 byte.
# H signifies we are unpacking an integer of size 2 bytes.
# L signifies we are unpacking a long of size 4 bytes.
icmpHeaderUnpacked = struct.unpack('!BBHL', icmpHeader)
# The first B is 1 byte and contains the type.
icmpType = icmpHeaderUnpacked[0]
# The second B is 1 byte and contains the code.
icmpCode = icmpHeaderUnpacked[1]
# The first H is 2 bytes and contains the checksum.
icmpChecksum = icmpHeaderUnpacked[2]
# Check if the type is 1 or 8, if so, unpack the identifier and sequence number.
if (icmpType == 0) or (icmpType == 8):
# The first L is 4 bytes and contains the rest of the header.
icmpIdentifier = icmpHeaderUnpacked[3] >> 16
icmpSeqNumber = icmpHeaderUnpacked[3] & 0xFFFF
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
if (icmpType == 0) or (icmpType == 8):
# Print ICMP Header
# Some segments of the header are switched back to hex form because that
# is the format wireshark has it.
unpackedInfo.append('\n********************\n******* ICMP *******\n********************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Type: ' + str(icmpType))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Code: ' + str(icmpCode))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Checksum: ' + format(icmpChecksum, '#04X'))
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Identifier: ' + str(icmpIdentifier))
if (extractedAttIndex == 5) or (extractedAttIndex == 0):
unpackedInfo.append('Sequence Number: ' + str(icmpSeqNumber))
else:
unpackedInfo.append('\n********************\n******* ICMP *******\n********************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Type: ' + str(icmpType))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Code: ' + str(icmpCode))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Checksum: ' + format(icmpChecksum, '#04X'))
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Attribute not available.')
if (extractedAttIndex == 5) or (extractedAttIndex == 0):
unpackedInfo.append('Attribute not available.')
# Separator
unpackedInfo.append('\n----------------------------------------')
else:
if (icmpType == 0) or (icmpType == 8):
if (extractedAttIndex == 1):
return str(icmpType)
if (extractedAttIndex == 2):
return str(icmpCode)
if (extractedAttIndex == 3):
return format(icmpChecksum, '#04X')
if (extractedAttIndex == 4):
return str(icmpIdentifier)
if (extractedAttIndex == 5):
return str(icmpSeqNumber)
else:
if (extractedAttIndex == 1):
return str(icmpType)
if (extractedAttIndex == 2):
return str(icmpCode)
if (extractedAttIndex == 3):
return format(icmpChecksum, '#04X')
if (extractedAttIndex == 4):
return 'Attribute not available.'
if (extractedAttIndex == 5):
return 'Attribute not available.'
def tcp(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
ipHeaderLength = 20
tcpHeaderLength = 20
# Get TCP header using begin and end.
# Specific Linux and Windows calibration is needed.
if os == linux:
begin = ethHeaderLength + ipHeaderLength
end = begin + tcpHeaderLength
elif os == windows:
begin = ipHeaderLength
end = begin + tcpHeaderLength
tcpHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# H signifies we are unpacking an integer of size 2 bytes.
# L signifies we are unpacking a long of size 4 bytes.
# B signifies we are unpacking an integer of size 1 byte.
tcpHeaderUnpacked = struct.unpack('!HHLLBBHHH', tcpHeader)
# The first H is 2 bytes and contains the source port.
tcpSourcePort = tcpHeaderUnpacked[0]
# The second H is 2 bytes and contains the destination port.
tcpDestPort = tcpHeaderUnpacked[1]
# The first L is 2 bytes and contains the sequence number.
tcpSeqNumber = tcpHeaderUnpacked[2]
# The second L is 4 bytes and contains the acknowledgement number.
tcpAckNumber = tcpHeaderUnpacked[3]
# The first B is 1 byte and contains the data offset, reserved bits, and NS flag.
# Split tcpHeaderUnpacked[4]
tcpDataOffsetAndReserved = tcpHeaderUnpacked[4]
tcpDataOffset = tcpDataOffsetAndReserved >> 4
tcpReserved = (tcpDataOffsetAndReserved >> 1) & 0x7
tcpNSFlag = tcpDataOffsetAndReserved & 0x1
# The second B is 1 byte and contains the rest of the flags.
# Split tcpHeaderUnpacked[5].
tcpRestOfFLags = tcpHeaderUnpacked[5]
tcpCWRFlag = tcpRestOfFLags >> 7
tcpECEFlag = (tcpRestOfFLags >> 6) & 0x1
tcpURGFlag = (tcpRestOfFLags >> 5) & 0x1
tcpACKFlag = (tcpRestOfFLags >> 4) & 0x1
tcpPSHFlag = (tcpRestOfFLags >> 3) & 0x1
tcpRSTFlag = (tcpRestOfFLags >> 2) & 0x1
tcpSYNFlag = (tcpRestOfFLags >> 1) & 0x1
tcpFINFlag = tcpRestOfFLags & 0x1
# The third H is 2 bytes and contains the window size.
tcpWindowSize = tcpHeaderUnpacked[6]
# The fourth H is 2 byte and conntains the checksum.
tcpChecksum = tcpHeaderUnpacked[7]
# The fifth H is 2 bytes and constains the urgent pointer.
tcpUrgentPointer = tcpHeaderUnpacked[8]
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
# Print TCP Header
# Some segments of the header are switched back to hex form because that
# is the format wireshark has it.
unpackedInfo.append('\n*******************\n******* TCP *******\n*******************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Source Port: ' + str(tcpSourcePort))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Destination Port: ' + str(tcpDestPort))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Sequence Number: ' + str(tcpSeqNumber))
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Acknowledgment Number: ' + str(tcpAckNumber))
if (extractedAttIndex == 5) or (extractedAttIndex == 0):
unpackedInfo.append('Data Offset: ' + str(tcpDataOffset) + ' 32-bit words')
if (extractedAttIndex == 6) or (extractedAttIndex == 0):
unpackedInfo.append('Reserved: ' + format(tcpReserved, '03b') + '. .... ....')
if (extractedAttIndex == 7) or (extractedAttIndex == 0):
unpackedInfo.append('NS Flag: ' + '...' + format(tcpNSFlag, '01b') + ' .... ....')
if (extractedAttIndex == 8) or (extractedAttIndex == 0):
unpackedInfo.append('CWR Flag: ' + '.... ' + format(tcpCWRFlag, '01b') + '... ....')
if (extractedAttIndex == 9) or (extractedAttIndex == 0):
unpackedInfo.append('ECE Flag: ' + '.... .' + format(tcpECEFlag, '01b') + '.. ....')
if (extractedAttIndex == 10) or (extractedAttIndex == 0):
unpackedInfo.append('URG Flag: ' + '.... ..' + format(tcpURGFlag, '01b') + '. ....')
if (extractedAttIndex == 11) or (extractedAttIndex == 0):
unpackedInfo.append('ACK Flag: ' + '.... ...' + format(tcpACKFlag, '01b') + ' ....')
if (extractedAttIndex == 12) or (extractedAttIndex == 0):
unpackedInfo.append('PSH Flag: ' + '.... .... ' + format(tcpPSHFlag, '01b') + '...')
if (extractedAttIndex == 13) or (extractedAttIndex == 0):
unpackedInfo.append('RST Flag: ' + '.... .... .' + format(tcpRSTFlag, '01b') + '..')
if (extractedAttIndex == 14) or (extractedAttIndex == 0):
unpackedInfo.append('SYN Flag: ' + '.... .... ..' + format(tcpSYNFlag, '01b') + '.')
if (extractedAttIndex == 15) or (extractedAttIndex == 0):
unpackedInfo.append('FIN Flag: ' + '.... .... ...' + format(tcpFINFlag, '01b'))
if (extractedAttIndex == 16) or (extractedAttIndex == 0):
unpackedInfo.append('Window Size: ' + str(tcpWindowSize) + ' bytes')
if (extractedAttIndex == 17) or (extractedAttIndex == 0):
unpackedInfo.append('Urgent Pointer: ' + str(tcpUrgentPointer))
if (extractedAttIndex == 18) or (extractedAttIndex == 0):
unpackedInfo.append('Checksum: ' + format(tcpChecksum, '#04X'))
# Separator
unpackedInfo.append('\n----------------------------------------')
else:
if (extractedAttIndex == 1):
return str(tcpSourcePort)
if (extractedAttIndex == 2):
return str(tcpDestPort)
if (extractedAttIndex == 3):
return str(tcpSeqNumber)
if (extractedAttIndex == 4):
return str(tcpAckNumber)
if (extractedAttIndex == 5):
return str(tcpDataOffset)
if (extractedAttIndex == 6):
return format(tcpReserved, '03b')
if (extractedAttIndex == 7):
return format(tcpNSFlag, '01b')
if (extractedAttIndex == 8):
return format(tcpCWRFlag, '01b')
if (extractedAttIndex == 9):
return format(tcpECEFlag, '01b')
if (extractedAttIndex == 10):
return format(tcpURGFlag, '01b')
if (extractedAttIndex == 11):
return format(tcpACKFlag, '01b')
if (extractedAttIndex == 12):
return format(tcpPSHFlag, '01b')
if (extractedAttIndex == 13):
return format(tcpRSTFlag, '01b')
if (extractedAttIndex == 14):
return format(tcpSYNFlag, '01b')
if (extractedAttIndex == 15):
return format(tcpFINFlag, '01b')
if (extractedAttIndex == 16):
return str(tcpWindowSize)
if (extractedAttIndex == 17):
return str(tcpUrgentPointer)
if (extractedAttIndex == 18):
return format(tcpChecksum, '#04X')
def udp(packet, extractedAttIndex, printKey):
# Header lengths.
ethHeaderLength = 14
ipHeaderLength = 20
udpHeaderLength = 8
# Get UDP header using begin and end.
# Specific Linux and Windows calibration is needed.
if os == linux:
begin = ethHeaderLength + ipHeaderLength
end = begin + udpHeaderLength
elif os == windows:
begin = ipHeaderLength
end = begin + udpHeaderLength
udpHeader = packet[begin:end]
# Unpack the header because it originally in hex.
# The regular expression helps unpack the header.
# ! signifies we are unpacking a network endian.
# H signifies we are unpacking an integer of size 2 bytes.
udpHeaderUnpacked = struct.unpack('!HHHH', udpHeader)
# The first H is 2 bytes and contains the source port.
udpSourcePort = udpHeaderUnpacked[0]
# The second H is 2 bytes and contains the destination port.
udpDestPort = udpHeaderUnpacked[1]
# The third H is 2 bytes and contains the packet length.
udpLength = udpHeaderUnpacked[2]
# The fourth H is 2 bytes and contains the header checksum.
udpChecksum = udpHeaderUnpacked[3]
# Check if the print key is 0.
# If true, header information will be printed.
# Check if the user selected extracted attribute index is 0.
# If true, all attributes will be printed.
# If false, the attribute the user selected extracted attribute index corresponds to will be printed.
# If false, the attribute the user selected attribute index corresponds to will be returned.
if printKey == 0:
# Print UDP Header
unpackedInfo.append('\n*******************\n******* UDP *******\n*******************')
if (extractedAttIndex == 1) or (extractedAttIndex == 0):
unpackedInfo.append('Source Port: ' + str(udpSourcePort))
if (extractedAttIndex == 2) or (extractedAttIndex == 0):
unpackedInfo.append('Destination Port: ' + str(udpDestPort))
if (extractedAttIndex == 3) or (extractedAttIndex == 0):
unpackedInfo.append('Length: ' + str(udpLength) + ' bytes')
if (extractedAttIndex == 4) or (extractedAttIndex == 0):
unpackedInfo.append('Checksum: ' + format(udpChecksum, '#04X'))
# Separator
unpackedInfo.append('\n----------------------------------------')
else:
if (extractedAttIndex == 1):
return str(udpSourcePort)
if (extractedAttIndex == 2):
return str(udpDestPort)
if (extractedAttIndex == 3):
return str(udpLength)
if (extractedAttIndex == 4):
return format(udpChecksum, '#04X')
def findProtocol(packet):
# Will hold the packet protocol.
packetProtocol = ''
# If the OS is Linux, unpack Ethernet's protocol.
# If the OS is Windows, mimic unpacking Ethernet's protocol.
if os == linux:
ethProtocol = eth(packet, 3, 1)
ethProtocol = int(ethProtocol)
elif os == windows:
ethProtocol = 8
# Find if the Ethernet protocol is ARP or IP.
# If the protocol is 1544, meaning ARP, then set packetProtocol to 0.
# If the protocol is 8, meaning IP, find the protocol within IP.
if ethProtocol == 1544:
packetProtocol = 1
elif ethProtocol == 8:
# Unpack IP's protocol.
ipProtocol = ip(packet, 10, 1)
ipProtocol = int(ipProtocol)
# If the protocol is 1, meaning ICMP, then set packetProtocol to 2 (Linux) or 1 (Windows).
# If the protocol is 6, meaning TCP, then set packetProtocol to 3 (Linux) or 2 (Windows).
# If the protocol is 17, meaning UDP, then set packetProtocol to 4 (Linux) or 3 (Windows).
if os == linux:
if ipProtocol == 1:
packetProtocol = 2
elif ipProtocol == 6:
packetProtocol = 3
elif ipProtocol == 17:
packetProtocol = 4
elif os == windows:
if ipProtocol == 1:
packetProtocol = 1
elif ipProtocol == 6:
packetProtocol = 2
elif ipProtocol == 17:
packetProtocol = 3
# Return the packet protocol.
return packetProtocol
def extractAllAtt(packet):
# All attributes for each protocol will be displayed.
extractedAttIndex = 0
# Attributes will be printed.
printKey = 0
# If the OS is Linux, unpack Ethernet's protocol.
# If the OS is Windows, mimic unpacking Ethernet's protocol.
if os == linux:
# Unpack the Ethernet (MAC) information.
eth(packet, extractedAttIndex, printKey)
# Find the packet's Ethernet protocol.
ethProtocol = eth(packet, 3, 1)
ethProtocol = int(ethProtocol)
elif os == windows:
ethProtocol = 8
# Find if the Ethernet protocol is ARP or IP.
if ethProtocol == 1544:
# Unpack the ARP information.
arp(packet, extractedAttIndex, printKey)
elif ethProtocol == 8:
# Unpack IP's information.
ip(packet, extractedAttIndex, printKey)
# Find the packet's IP protocol.
ipProtocol = ip(packet, 10, 1)
ipProtocol = int(ipProtocol)
# If the protocol is 1, meaning ICMP, then unpack the ICMP information.
# If the protocol is 6, meaning TCP, then unpack the TCP information.
# If the protocol is 17, meaning UDP, then unpack the UDP information.
if ipProtocol == 1:
icmp(packet, extractedAttIndex, printKey)
elif ipProtocol == 6:
tcp(packet, extractedAttIndex, printKey)
elif ipProtocol == 17:
udp(packet, extractedAttIndex, printKey)
def filterAndExtract(packet, filteredProtocolIndex, extractedAttIndex):
# Get the protocol index of the packet.
protocolIndex = findProtocol(packet)
if os == linux:
if (filteredProtocolIndex == protocolIndex) or (filteredProtocolIndex == 0):
# Attributes will be printed.
printKey = 0
# Find the user selected filtered protocol index.
if filteredProtocolIndex == 0:
if extractedAttIndex >= 1:
eth(packet, extractedAttIndex, printKey)
# Separator
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 1:
# The user selected extracted attribute index will be calibrated (if needed) to specify which attribute to extract.
if extractedAttIndex >= 4:
arp(packet, extractedAttIndex - 3, printKey)
elif extractedAttIndex >= 1:
eth(packet, extractedAttIndex, printKey)
# Separator
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 2:
if extractedAttIndex >= 17:
icmp(packet, extractedAttIndex - 16, printKey)
elif extractedAttIndex >= 4:
ip(packet, extractedAttIndex - 3, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex >= 1:
eth(packet, extractedAttIndex, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 3:
if extractedAttIndex >= 17:
tcp(packet, extractedAttIndex - 16, printKey)
elif extractedAttIndex >= 4:
ip(packet, extractedAttIndex - 3, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex >= 1:
eth(packet, extractedAttIndex, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 4:
if extractedAttIndex >= 17:
udp(packet, extractedAttIndex - 16, printKey)
elif extractedAttIndex >= 4:
ip(packet, extractedAttIndex - 3, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex >= 1:
eth(packet, extractedAttIndex, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
return 0
else:
return 1
elif os == windows:
if (filteredProtocolIndex == protocolIndex) or (filteredProtocolIndex == 0):
# Attributes will be printed.
printKey = 0
# Find the user selected filtered protocol index.
if filteredProtocolIndex == 0:
if extractedAttIndex >= 1:
ip(packet, extractedAttIndex, printKey)
# Separator
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 1:
# The user selected extracted attribute index will be calibrated (if needed) to specify which attribute to extract.
if extractedAttIndex >= 14:
icmp(packet, extractedAttIndex - 13, printKey)
elif extractedAttIndex >= 1:
ip(packet, extractedAttIndex, printKey)
# Separator
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 2:
if extractedAttIndex >= 14:
tcp(packet, extractedAttIndex - 13, printKey)
elif extractedAttIndex >= 1:
ip(packet, extractedAttIndex, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
elif filteredProtocolIndex == 3:
if extractedAttIndex >= 14:
udp(packet, extractedAttIndex - 13, printKey)
elif extractedAttIndex >= 1:
ip(packet, extractedAttIndex, printKey)
unpackedInfo.append('\n----------------------------------------')
elif extractedAttIndex == 0:
extractAllAtt(packet)
return 0
else:
return 1
def findMaxDiameter():
# Find the maximum diameter of all the packets.
maxDiameter = max(diameterList)
# Print the maximum diameter.
calculationList.append('Max Diameter: ' + str(maxDiameter) + ' hops')
def findMaxPacketLength():
# Find the maximum packet length of all the packets.
maxLength = max(lengthList)
# Print the maximum packet length.
calculationList.append('Max Packet Length: ' + str(maxLength) + ' bytes')
def findAvgDiameter():
# Hold the sum and the count of the diameters.
diameterSum = 0
count = 0
avgDiameter = 0
# Add all of the diameters together.
for diameter in diameterList:
diameterSum = diameterSum + diameter
count = count + 1
# Divide diameterSum by count to give average.
avgDiameter = diameterSum / count
calculationList.append('Avg Diameter: ' + str(avgDiameter) + ' hops')
def findAvgPacketLength():
# Hold the sum and the count of the packet lengths.
lengthSum = 0
count = 0
avgLength = 0
# Add all of the lengths together.
for length in lengthList:
lengthSum = lengthSum + length
count = count + 1
# Divide lengthSum by count to give average.
avgLength = lengthSum / count
calculationList.append('Avg Packet Length: ' + str(avgLength) + ' bytes')
def startSniff():
try:
while True:
# Ask the user if they would like to begin sniffing.
decision = raw_input('Sniff the network? Y/N: ')
# Y runs the rest of the application.
# N exits the application.
if (decision == 'Y') or (decision == 'y'):
break
elif (decision == 'N') or (decision == 'n'):
close()
else:
print('\nUnsupported input...')
except KeyboardInterrupt:
print('')
close()
def startFilter():
try:
while True:
# Ask the user if they would like to filter the packets.
decision = raw_input('Filter the packets by a specific protocol? Y/N: ')
# Y runs the rest of the application.
# N exits the application.
if (decision == 'Y') or (decision == 'y'):
print('Select a protocol...')
return 0
elif (decision == 'N') or (decision == 'n'):
return 1
else:
print('\nUnsupported input...')
except KeyboardInterrupt:
print('')
return 1
def startExtract():
try:
while True:
# Ask the user if they would like to extract attributes.
decision = raw_input('Extract a specific attribute from the protocol? Y/N: ')
# Y runs the rest of the application.
# N exits the application.
if (decision == 'Y') or (decision == 'y'):
print('Select an attribute...')
return 0
elif (decision == 'N') or (decision == 'n'):
return 1
else:
print('\nUnsupported input...')
except KeyboardInterrupt:
print('')
return 1
def startCalculations():
try:
while True:
# Ask the user if they would like to start calculations.
decision = raw_input('Would you like to do some calculations? Y/N: ')
# Y runs the rest of the application.
# N exits the application.
if (decision == 'Y') or (decision == 'y'):
return 0
elif (decision == 'N') or (decision == 'n'):
return 1
else:
print('\nUnsupported input...')
except KeyboardInterrupt:
print('')
return 1
def filterOptions():
if os == linux:
# Loop if unsupported input.
while True:
# Display protocols and their index.
filteredProtocolIndex = raw_input('0: All\n1: ARP\n2: ICMP\n3: TCP\n4: UDP\nSelection: ')
# Check if the user selected filtered protocol index is supported input.
try:
filteredProtocolIndex = int(filteredProtocolIndex)
except ValueError:
print('\nUnsupported input, try again...')
continue
# Check if the user selected filtered protocol index is in the index range.
# If true, return filteredProtocolIndex.
if (filteredProtocolIndex >= 0) and (filteredProtocolIndex <= 4):
return filteredProtocolIndex
else:
print('\nUnsupported input, try again...')
continue
elif os == windows:
# Loop if unsupported input.
while True:
# Display protocols and their index.
filteredProtocolIndex = raw_input('0: All\n1: ICMP\n2: TCP\n3: UDP\nSelection: ')
# Check if the user selected filtered protocol index is supported input.
try:
filteredProtocolIndex = int(filteredProtocolIndex)
except ValueError:
print('\nUnsupported input, try again...')
continue
# Check if the user selected filtered protocol index is in the index range.
# If true, return filteredProtocolIndex.
if (filteredProtocolIndex >= 0) and (filteredProtocolIndex <= 3):
return filteredProtocolIndex
else:
print('\nUnsupported input, try again...')
continue
def extractOptions(filteredProtocolIndex):
if os == linux:
# Establish the prompts for each protocol's attributes.
allAttributes = '0: All'
ethAttributes = '1: Destination Address\n2: Source Address\n3: EtherType'
arpAttributes = '4: Hardware Type\n5: Protocol Type\n6: Hardware Address Length\n7: Protocol Address Length\n8: Operation\n9: Sender Hardware Address\n10: Sender Protocol Address\n11: Target Hardware Address\n12: Target Protocol Address'
ipAttributes = '4: Version\n5: Header Length\n6: Differentiated Services Code Point\n7: Explicit Congestion Notification\n8: Total Length\n9: Identification\n10: Flags\n11: Fragment Offset\n12: Time to Live\n13: Protocol\n14: Header Checksum\n15: Source Address\n16: Destination Address'
icmpAttributes = '17: Type\n18: Code\n19: Checksum\n20: Identifier (If available)\n21: Sequence Number (If available)'
tcpAttributes = '17: Source Port\n18: Destination Port\n19: Sequence Number\n20: Acknowledgment Number\n21: Data Offset\n22: Reserved\n23: NS Flag\n24: CWR Flag\n25: ECE Flag\n26: URG Flag\n27: ACK Flag\n28: PSH Flag\n29: RST Flag\n30: SYN Flag\n31: FIN Flag\n32: Window Size\n33: Urgent Pointer\n34: Checksum'
udpAttributes = '17: Source Port\n18: Destination Port\n19: Length\n20: Checksum'
# Loop if unsupported input.
while True:
# Find the user selected filtered protocol index.