-
Notifications
You must be signed in to change notification settings - Fork 0
/
packets_sniffer.py
167 lines (135 loc) · 4.57 KB
/
packets_sniffer.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
#!/usr/bin/python
import socket
import os, sys
import struct
import binascii
import colorama
colorama.init()
class bcolors:
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
OKRED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
sock_created = False
sniffer_socket = 0
def analyze_udp_header(data_recv):
udp_hdr = struct.unpack('!4H', data_recv[:8])
src_port = udp_hdr[0]
dst_port = udp_hdr[1]
length = udp_hdr[2]
checksum = udp_hdr[3]
data = data_recv[8:]
print(bcolors.BOLD + bcolors.OKBLUE + "____________________UDP HEADER____________________" + bcolors.ENDC)
print("Source: %hu " % (src_port))
print("Destination: %hu" % (dst_port))
print("Length: %hu " % (length))
print("Checksum: %hu" % (checksum))
return data
def analyze_tcp_header(data_recv):
tcp_hdr = struct.unpack('2H2I4H', data_recv[:20])
scr_port = tcp_hdr[0]
dst_port = tcp_hdr[1]
seq_num = tcp_hdr[2]
ack_num = tcp_hdr[3]
data_offset = tcp_hdr[4] >> 12
reserved = (tcp_hdr[5] >> 6) & 0x03ff
flags = tcp_hdr[4] & 0x003f
window = tcp_hdr[5]
checksum = tcp_hdr[6]
urg_ptr = tcp_hdr[7]
data = data_recv[20:]
urg = bool(flags & 0x0020)
ack = bool(flags & 0x0010)
psh = bool(flags & 0x0008)
rst = bool(flags & 0x0004)
syn = bool(flags & 0x0002)
fin = bool(flags & 0x0001)
print(bcolors.BOLD + bcolors.OKBLUE+"____________________TCP HEADER____________________" + bcolors.ENDC)
print("Source: %hu " % (scr_port))
print("Destination: %hu " % (dst_port))
print("Seq: %u " % (seq_num))
print("Ack: %u " % (ack_num))
print(bcolors.OKRED+"#### Flags: ####"+bcolors.ENDC)
print("URG: %d " % (urg))
print("ACK: %d " % (ack))
print("PSH: %d " % (psh))
print("RST: %d " % (rst))
print("SYN: %d " % (syn))
print("FIN: %d " % fin)
print("Winsize: %hu " % (window))
print("Checksum %hu " % (checksum))
return data
def analyze_ip_header(data_recv):
ip_hdr = struct.unpack('!6H4s4s', data_recv[:20])
ver = ip_hdr[0] >> 12
ihl = (ip_hdr[0] >> 8) & 0x0f
tos = ip_hdr[0] & 0x00ff
tot_len = ip_hdr[1]
ip_id = ip_hdr[2]
flags = ip_hdr[3] >> 13
frag_offset = ip_hdr[3] & 0x1fff
ip_ttl = ip_hdr[4] >> 8
ip_proto = ip_hdr[4] & 0x00ff
checksum = ip_hdr[5]
src_adress = socket.inet_ntoa(ip_hdr[6])
dst_adress = socket.inet_ntoa(ip_hdr[7])
data = data_recv[20:]
print(bcolors.BOLD + bcolors.OKBLUE +"____________________IP HEADER____________________" + bcolors.ENDC)
print("Version: %hu " % (ver))
print("IHL: %hu " % (ihl))
print("TOS: %hu " % (tos))
print("Length: %hu" % (tot_len))
print("ID: %hu" % (ip_id))
print("Offset: %hu" % (frag_offset))
print("TTL: %hu" % (ip_ttl))
print("Proto: %hu" % (ip_proto))
print("Checksum: %hu" % (checksum))
print("Source IP: %s" % (src_adress))
print("Destination IP: %s" % (dst_adress))
if ip_proto == 6:
tcp_udp = "TCP"
elif ip_proto == 17:
tcp_udp = "UDP"
else:
tcp_udp = "OTHER"
return data, tcp_udp
def analyze_ether_header(data_recv):
ip_bool = False
eth_hdr = struct.unpack('!6s6sH', data_recv[:14])
dest_mac = binascii.hexlify(eth_hdr[0])
src_mac = binascii.hexlify(eth_hdr[1])
proto = eth_hdr[2] >> 8
data = data_recv[14:]
print(bcolors.BOLD + bcolors.OKBLUE + "____________________ETHERNET HEADER____________________" + bcolors.ENDC)
print("Destination MAC: %s:%s:%s:%s:%s:%s " % (dest_mac[0:2], dest_mac[2:4], dest_mac[4:6], dest_mac[6:8], dest_mac[8:10], dest_mac[10:12]))
print("Source MAC: %s:%s:%s:%s:%s:%s " % (src_mac[0:2], src_mac[2:4], src_mac[4:6], src_mac[6:8], src_mac[8:10], src_mac[10:12]))
print("PROTOCOL: %hu" % proto)
if proto == 0x08:
ip_bool = True
return data, ip_bool
def main():
global sock_created
global sniffer_socket
if sock_created == False:
sniffer_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
sock_created = True
data_recv = sniffer_socket.recv(2048)
os.system("clear")
data_recv, ip_bool = analyze_ether_header(data_recv)
if ip_bool:
data_recv, tcp_udp = analyze_ip_header(data_recv)
else:
return
if tcp_udp == "TCP":
data_recv = analyze_tcp_header(data_recv)
elif tcp_udp == "UDP":
data_recv = analyze_udp_header(data_recv)
else:
return
print(bcolors.OKGREEN+bcolors.BOLD + "Packet sniffer running...."+ bcolors.ENDC)
while True:
try:
main()
except KeyboardInterrupt:
exit(0)