-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
300 lines (244 loc) · 16.7 KB
/
app.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
from Headers import *
#====================================== main ===================================
if __name__=='__main__':
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
try:
pcap = Pcap('capture.pcap')
while True:
raw_data, addr = conn.recvfrom(65535)
pcap.write(raw_data)
eth = Ethernet(raw_data)
print('\n\n' + "="*100 + '\n')
print(f' Ethernet Frame: \t\t\t\t\t\t {time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())}')
if (eth.proto == 0x0806):
ether_type = 'ARP (0x0806)'
elif (eth.proto == 0x800):
ether_type = 'IPv4 (0x0800)'
elif (eth.proto == 0x86DD):
ether_type = 'IPv6 (0x086DD)'
else:
ether_type = hex(eth.proto)
print('\t - ' + 'Destination: {}, Source: {}, Type: {}'.format(eth.dest_mac, eth.src_mac, ether_type))
if eth.proto == 0x800: # IP/IPV4
proto = 'Other'
ipv4 = IPv4(eth.data)
if ipv4.protocol == 1:
proto = 'ICMP'
if ipv4.protocol == 6:
proto = 'TCP'
if ipv4.protocol == 17:
proto = 'UDP'
print('\t - ' + "Internet Protocol Version 4:")
h_len = str(ipv4.header_length) + ' bytes (' + str(int(ipv4.header_length / 4)) + ')'
print('\t\t - ' + 'Version: {}, Header Length: {}, Type Of Service: {}, Total Lenght: {}'.format(
ipv4.version, h_len, ipv4.tos, ipv4.total_length))
print('\t\t - ' + 'Identification: {} ({})'.format(hex(ipv4.ID), ipv4.ID))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Reserved bit: {}, Dont Fragment: {}, More Fragment: {}'.format(
ipv4.Rb, ipv4.DF, ipv4.MF))
print('\t\t - '+'Fragment Offset: {}', ipv4.fragment_Offset)
print('\t\t - ' + 'Time to live: {}, Protocol: {} ({}), Header checksum: {}'.format(ipv4.ttl, proto,
ipv4.protocol,
ipv4.header_checksum))
print('\t\t - ' + 'Source address: {}'.format(ipv4.source_address))
print('\t\t - ' + 'Destination address: {}'.format(ipv4.destination_address))
print('\t\t - ' + 'Options: {}'.format(ipv4.options))
# ICMP
if ipv4.protocol == 1:
icmp = ICMP(ipv4.data)
print('\t - ' + 'Intenet Control Message Protocol:')
print('\t\t - ' + 'Type: {}, Code: {}, Checksum: {}'.format(icmp.icmp_type, icmp.code, icmp.checksum))
print('\t\t - ' + 'Identifier: {}, Sequence number: {}'.format(icmp.id, icmp.sequence))
print('\t\t - ' + 'Data:')
print(print_data('\t\t\t ', icmp.data))
# TCP
elif ipv4.protocol == 6:
tcp = TCP(ipv4.data)
print('\t - ' + 'Transmission Control Protocol:')
print('\t\t - ' + 'Source Port: {}, Destination Port: {}'.format(tcp.src_port, tcp.dest_port))
print('\t\t - ' + 'Sequence number: {}, Acknowledgment number: {}'.format(tcp.sequence,tcp.acknowledgment))
h_len = str(tcp.offset) + ' bytes (' + str(int(tcp.offset / 4)) + ')'
print('\t\t - ' + 'Header Length: {}'.format(h_len))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Reserved: {}, NS: {}, CWR: {}'.format(tcp.Reserved, tcp.NS, tcp.CWR))
print('\t\t\t - ' + 'ECE: {}, URG: {}, ACK: {}, PSH: {}'.format(tcp.ECE, tcp.URG, tcp.ACK, tcp.PSH))
print('\t\t\t - ' + 'RST: {}, SYN: {}, FIN: {}'.format(tcp.RST, tcp.SYN, tcp.FIN))
print('\t\t - ' + 'Window sizr: {}, Checksum: {}'.format(tcp.window, tcp.checksum))
print('\t\t - ' + 'Urgent pointer: {}, Options: {}'.format(tcp.urgent, tcp.options))
# HTTP
if tcp.src_port == 80 or tcp.dest_port == 80:
print('\t\t - ' + 'Hypertext Transfer Protocol:')
try:
http = HTTP(tcp.data)
http_info = str(http.data).split('\n')
for line in http_info:
print('\t\t\t ' + str(line))
except:
print(print_data('\t\t\t ', tcp.data))
# DNS
elif tcp.src_port == 53 or tcp.dest_port == 53:
try:
dns = DNS(tcp.data)
print('\t - ' + 'Domain Name System:')
print('\t\t - ' + 'Identifier: {}'.format(hex(dns.id)))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Query/Response(QR): {}, Operation Code(Opcode): {}'.format(dns.QR, dns.opcode))
print('\t\t\t - ' + 'Authoritative Answer(AA): {}, Truncated(TC): {}, Recursion Desired(RD): {}'.format(
dns.AA, dns.TC, dns.RD, dns.RA))
print('\t\t\t - ' + 'Recursion Available(RA): {}, Z: {}, Authenticated data(AD): {}'.format(dns.AA, dns.Z, dns.AD))
print('\t\t\t - ' + 'Checking Disabled(CD): {}, Return code(Rcode)'.format(dns.CD, dns.Rcode))
print('\t\t - ' + 'Questions: {}, Answer RRs: {}, Authority RRs: {}, Additional RRs: {}'.format(
dns.question_count, dns.answer_count, dns.name_server_count, dns.additional_record_count))
dns.dns_details()
dns.print_details()
except:
print(tcp.data)
else:
print('\t\t - ' + 'TCP Data:')
#print(print_data('\t\t\t ', tcp.data)) # uncomment to print tcp data
# UDP
elif ipv4.protocol == 17:
udp = UDP(ipv4.data)
print('\t - ' + 'User Datagram Protocol:')
print('\t\t - ' + 'Source Port: {}, Destination Port: {}, Length: {},Checksum: {}'.format(udp.src_port,
udp.dest_port,
udp.length,
udp.checksum))
# DNS
if udp.src_port == 53 or udp.dest_port == 53:
dns = DNS(udp.data)
print('\t - ' + 'Domain Name System:')
print('\t\t - ' + 'Identifier: {}'.format(hex(dns.id)))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Query/Response(QR): {}, Operation Code(Opcode): {}'.format(dns.QR, dns.opcode))
print('\t\t\t - ' + 'Authoritative Answer(AA): {}, Truncated(TC): {}, Recursion Desired(RD): {}'.format(
dns.AA, dns.TC, dns.RD, dns.RA))
print('\t\t\t - ' + 'Recursion Available(RA): {}, Z: {}, Authenticated data(AD): {}'.format(dns.AA,dns.Z,dns.AD))
print('\t\t\t - ' + 'Checking Disabled(CD): {}, Return code(Rcode)'.format(dns.CD,dns.Rcode))
print('\t\t - ' + 'Questions: {}, Answer RRs: {}, Authority RRs: {}, Additional RRs: {}'.format(
dns.question_count,dns.answer_count,dns.name_server_count,dns.additional_record_count))
dns.dns_details()
dns.print_details()
else:
print('\t\t - ' + 'UDP Data:')
#print(print_data('\t\t\t ', udp.data)) # uncomment to print udp data
else:
print('\t - ' + 'Other IPv4 Data:')
print(print_data('\t\t ', ipv4.data))
# IP/IPV6
elif eth.proto == 0x86DD:
ipv6 = IPv6(eth.data)
print('\t - ' + "Internet Protocol Version 6:")
print('\t\t - ' + 'Version: {}, Traffic Class: {}, Flow Label: {}'.format(ipv6.version,ipv6.traffic_class,
ipv6.flow_label))
print('\t\t - ' + 'Payload length: {}, Next header: {}, Hop limit: {}'.format(ipv6.payload_length,
ipv6.next_header,ipv6.hop_limit))
print('\t\t - ' + 'Source address: {}, Destination address: {}'.format(ipv6.source_address,
ipv6.destination_address))
# ICMPv6
if ipv6.next_header == 58:
icmp = ICMP(ipv6.data)
print('\t - ' + 'Intenet Control Message Protocol:')
print('\t\t - ' + 'Type: {}, Code: {}, Checksum: {}'.format(icmp.icmp_type, icmp.code, icmp.checksum))
print('\t\t - ' + 'Identifier: {}, Sequence number: {}'.format(icmp.id, icmp.sequence))
print('\t\t - ' + 'Data:')
print(print_data('\t\t\t ', icmp.data))
# TCP
elif ipv6.next_header == 6:
tcp = TCP(ipv6.data)
print('\t - ' + 'Transmission Control Protocol:')
print('\t\t - ' + 'Source Port: {}, Destination Port: {}'.format(tcp.src_port, tcp.dest_port))
print('\t\t - ' + 'Sequence number: {}, Acknowledgment number: {}'.format(tcp.sequence,
tcp.acknowledgment))
h_len = str(tcp.offset) + ' bytes (' + str(int(tcp.offset / 4)) + ')'
print('\t\t - ' + 'Header Length: {}'.format(h_len))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Reserved: {}, NS: {}, CWR: {}'.format(tcp.Reserved, tcp.NS, tcp.CWR))
print('\t\t\t - ' + 'ECE: {}, URG: {}, ACK: {}, PSH: {}'.format(tcp.ECE, tcp.URG, tcp.ACK,tcp.PSH))
print('\t\t\t - ' + 'RST: {}, SYN: {}, FIN: {}'.format(tcp.RST, tcp.SYN, tcp.FIN))
print('\t\t - ' + 'Window sizr: {}, Checksum: {}'.format(tcp.window, tcp.checksum))
print('\t\t - ' + 'Urgent pointer: {}, Options: {}'.format(tcp.urgent, tcp.options))
# HTTP
if tcp.src_port == 80 or tcp.dest_port == 80:
print('\t\t - ' + 'Hypertext Transfer Protocol:')
try:
http = HTTP(tcp.data)
http_info = str(http.data).split('\n')
for line in http_info:
print('\t\t\t ' + str(line))
except:
print(print_data('\t\t\t ', tcp.data))
# DNS
if tcp.src_port == 53 or tcp.dest_port == 53:
try:
dns = DNS(tcp.data)
print('\t - ' + 'Domain Name System:')
print('\t\t - ' + 'Identifier: {}'.format(hex(dns.id)))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Query/Response(QR): {}, Operation Code(Opcode): {}'.format(dns.QR, dns.opcode))
print('\t\t\t - ' + 'Authoritative Answer(AA): {}, Truncated(TC): {}, Recursion Desired(RD): {}'.format(
dns.AA, dns.TC, dns.RD, dns.RA))
print('\t\t\t - ' + 'Recursion Available(RA): {}, Z: {}, Authenticated data(AD): {}'.format(dns.AA, dns.Z, dns.AD))
print('\t\t\t - ' + 'Checking Disabled(CD): {}, Return code(Rcode)'.format(dns.CD, dns.Rcode))
print('\t\t - ' + 'Questions: {}, Answer RRs: {}, Authority RRs: {}, Additional RRs: {}'.format(
dns.question_count, dns.answer_count, dns.name_server_count, dns.additional_record_count))
dns.dns_details()
dns.print_details()
except:
print(tcp.data)
else:
print('\t\t - ' + 'TCP Data:')
#print(print_data('\t\t\t ', tcp.data))
# UDP
elif ipv6.next_header == 17:
udp = UDP(ipv6.data)
print('\t - ' + 'User Datagram Protocol:')
print('\t\t - ' + 'Source Port: {}, Destination Port: {}, Length: {},Checksum: {}'.format(udp.src_port,
udp.dest_port,
udp.length,
udp.checksum))
# DNS
if udp.src_port == 53 or udp.dest_port == 53:
dns = DNS(udp.data)
print('\t - ' + 'Domain Name System:')
print('\t\t - ' + 'Identifier: {}'.format(hex(dns.id)))
print('\t\t - ' + 'Flags:')
print('\t\t\t - ' + 'Query/Response(QR): {}, Operation Code(Opcode): {}'.format(dns.QR, dns.opcode))
print('\t\t\t - ' + 'Authoritative Answer(AA): {}, Truncated(TC): {}, Recursion Desired(RD): {}'.format(
dns.AA, dns.TC, dns.RD, dns.RA))
print('\t\t\t - ' + 'Recursion Available(RA): {}, Z: {}, Authenticated data(AD): {}'.format(dns.AA, dns.Z, dns.AD))
print('\t\t\t - ' + 'Checking Disabled(CD): {}, Return code(Rcode)'.format(dns.CD, dns.Rcode))
print('\t\t - ' + 'Questions: {}, Answer RRs: {}, Authority RRs: {}, Additional RRs: {}'.format(
dns.question_count, dns.answer_count, dns.name_server_count, dns.additional_record_count))
dns.dns_details()
dns.print_details()
else:
print('\t\t - ' + 'UDP Data:')
#print(print_data('\t\t\t ', udp.data))
else:
print('\t - ' + 'Other IPv6 Data:')
print(print_data('\t\t ', ipv6.data))
# ARP
elif eth.proto == 0x0806:
arp = ARP(eth.data)
print('\t - ' + "Address Resolution Protocol:")
h_type = arp.hardware_type
if h_type == 1:
h_type = 'Ethernet (1)'
print(
'\t\t - ' + 'Hardware type: {}, Protocol type: {}'.format(
h_type, hex(arp.protocol_type)))
print(
'\t\t - ' + 'Hardware size: {}, Protocol size: {}, Opcode: {}'.format(arp.hardware_addr_len, arp.protocol_addr_len,
arp.opcode))
print('\t\t - ' + 'Sender MAC address: {}, Sender IP address: {}'.format(arp.sender_hardware_address,
arp.sender_protocol_addr))
print('\t\t - ' + 'Target MAC address: {}, Target IP address: {}'.format(
arp.target_hardware_address,
arp.target_protocol_addr))
else:
print(' Ethernet Data:')
print(print_data('\t ', eth.data))
except KeyboardInterrupt:
print('\033[32m' + '\n Packet capturing was stopped. Packets were saved in capture.pcap \n')
pcap.close()