blob: 9b6830c22f8699af7f73b18ea6345a5e09fc4a51 [file] [log] [blame]
Alex9e4bfaf2019-06-11 15:21:59 -05001import binascii
2import socket
3import struct
Alex Savatieiev73aa99a2019-03-06 10:02:43 -06004
Alex9e4bfaf2019-06-11 15:21:59 -05005
6class unpack:
7 def __init__(self):
8 self.data = None
9
10 # Ethernet Header
11 def eth_header(self, data):
12 storeobj = data
13 storeobj = struct.unpack("!6s6sH", storeobj)
14 destination_mac = binascii.hexlify(storeobj[0])
15 source_mac = binascii.hexlify(storeobj[1])
16 eth_protocol = storeobj[2]
17 data = {"Destination Mac": destination_mac,
18 "Source Mac": source_mac,
19 "Protocol": eth_protocol}
20 return data
21
22 # ICMP HEADER Extraction
23 def icmp_header(self, data):
24 icmph = struct.unpack('!BBH', data)
25 icmp_type = icmph[0]
26 code = icmph[1]
27 checksum = icmph[2]
28 data = {'ICMP Type': icmp_type,
29 "Code": code,
30 "CheckSum": checksum}
31 return data
32
33 # UDP Header Extraction
34 def udp_header(self, data):
35 storeobj = struct.unpack('!HHHH', data)
36 source_port = storeobj[0]
37 dest_port = storeobj[1]
38 length = storeobj[2]
39 checksum = storeobj[3]
40 data = {"Source Port": source_port,
41 "Destination Port": dest_port,
42 "Length": length,
43 "CheckSum": checksum}
44 return data
45
46 # IP Header Extraction
47 def ip_header(self, data):
48 storeobj = struct.unpack("!BBHHHBBH4s4s", data)
49 _version = storeobj[0]
50 _tos = storeobj[1]
51 _total_length = storeobj[2]
52 _identification = storeobj[3]
53 _fragment_Offset = storeobj[4]
54 _ttl = storeobj[5]
55 _protocol = storeobj[6]
56 _header_checksum = storeobj[7]
57 _source_address = socket.inet_ntoa(storeobj[8])
58 _destination_address = socket.inet_ntoa(storeobj[9])
59
60 data = {'Version': _version,
61 "Tos": _tos,
62 "Total Length": _total_length,
63 "Identification": _identification,
64 "Fragment": _fragment_Offset,
65 "TTL": _ttl,
66 "Protocol": _protocol,
67 "Header CheckSum": _header_checksum,
68 "Source Address": _source_address,
69 "Destination Address": _destination_address}
70 return data
71
72 # Tcp Header Extraction
73 def tcp_header(self, data):
74 storeobj = struct.unpack('!HHLLBBHHH', data)
75 _source_port = storeobj[0]
76 _destination_port = storeobj[1]
77 _sequence_number = storeobj[2]
78 _acknowledge_number = storeobj[3]
79 _offset_reserved = storeobj[4]
80 _tcp_flag = storeobj[5]
81 _window = storeobj[6]
82 _checksum = storeobj[7]
83 _urgent_pointer = storeobj[8]
84 data = {"Source Port": _source_port,
85 "Destination Port": _destination_port,
86 "Sequence Number": _sequence_number,
87 "Acknowledge Number": _acknowledge_number,
88 "Offset & Reserved": _offset_reserved,
89 "Tcp Flag": _tcp_flag,
90 "Window": _window,
91 "CheckSum": _checksum,
92 "Urgent Pointer": _urgent_pointer
93 }
94 return data
95
96# Mac Address Formating
97
98
99def mac_formater(a):
100 b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(
101 a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5]))
102 return b
103
104
105def get_host(q):
106 try:
107 k = socket.gethostbyaddr(q)
108 except Exception:
109 k = 'Unknown'
110 return k
111
112
113s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
114u = unpack()
Alex836fac82019-08-22 13:36:16 -0500115count = 32
Alex9e4bfaf2019-06-11 15:21:59 -0500116while count > 0:
117 count -= 1
118 # Capture packets from network
119 pkt = s.recvfrom(65565)
120
Alex836fac82019-08-22 13:36:16 -0500121 print "\n\n=== [+] ------------ Ethernet Header----- [+]"
Alex9e4bfaf2019-06-11 15:21:59 -0500122
123 # print data on terminal
124 for i in u.eth_header(pkt[0][0:14]).iteritems():
125 a, b = i
126 print "{} : {} | ".format(a, b),
Alex836fac82019-08-22 13:36:16 -0500127 print "\n=== [+] ------------ IP Header ------------[+]"
Alex9e4bfaf2019-06-11 15:21:59 -0500128 for i in u.ip_header(pkt[0][14:34]).iteritems():
129 a, b = i
130 print "{} : {} | ".format(a, b),
Alex836fac82019-08-22 13:36:16 -0500131 print "\n== [+] ------------ Tcp Header ----------- [+]"
Alex9e4bfaf2019-06-11 15:21:59 -0500132 for i in u.tcp_header(pkt[0][34:54]).iteritems():
133 a, b = i
134 print "{} : {} | ".format(a, b),
Alex836fac82019-08-22 13:36:16 -0500135 print "\n===== Data ===="
136 print pkt[0][54:]
137 print "\n======="
138 print pkt[1:]