forked from O-Luhishi/Python-Packet-Sniffer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPacket-Sniffer.py
More file actions
140 lines (114 loc) · 5.25 KB
/
Packet-Sniffer.py
File metadata and controls
140 lines (114 loc) · 5.25 KB
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
#! /usr/local/bin/python3.5
import socket
import struct
import textwrap
TAB_1 = '\t - '
TAB_2 = '\t\t - '
TAB_3 = '\t\t\t - '
TAB_4 = '\t\t\t\t - '
DATA_TAB_1 = '\t '
DATA_TAB_2 = '\t\t '
DATA_TAB_3 = '\t\t\t '
DATA_TAB_4 = '\t\t\t\t '
def main():
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
raw_data, addr = conn.recvfrom(65536)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print('\n Ethernet Frame: ')
print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format(dest_mac, src_mac, eth_proto))
if eth_proto == 8:
(version, header_length, ttl, proto, src, target, data) = ipv4_Packet(data)
print(TAB_1 + "IPV4 Packet:")
print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {}'.format(version, header_length, ttl))
print(TAB_3 + 'protocol: {}, Source: {}, Target: {}'.format(proto, src, target))
# ICMP
if proto == 1:
icmp_type, code, checksum, data = icmp_packet(data)
print(TAB_1 + 'ICMP Packet:')
print(TAB_2 + 'Type: {}, Code: {}, Checksum: {},'.format(icmp_type, code, checksum))
print(TAB_2 + 'ICMP Data:')
print(format_output_line(DATA_TAB_3, data))
# TCP
elif proto == 6:
src_port, dest_port, sequence, acknowledgment, flag_urg, flag_ack, flag_psh, flag_rst, flag_syn, flag_fin = struct.unpack(
'! H H L L H H H H H H', raw_data[:24])
print(TAB_1 + 'TCP Segment:')
print(TAB_2 + 'Source Port: {}, Destination Port: {}'.format(src_port, dest_port))
print(TAB_2 + 'Sequence: {}, Acknowledgment: {}'.format(sequence, acknowledgment))
print(TAB_2 + 'Flags:')
print(TAB_3 + 'URG: {}, ACK: {}, PSH: {}'.format(flag_urg, flag_ack, flag_psh))
print(TAB_3 + 'RST: {}, SYN: {}, FIN:{}'.format(flag_rst, flag_syn, flag_fin))
if len(data) > 0:
# HTTP
if src_port == 80 or dest_port == 80:
print(TAB_2 + 'HTTP Data:')
try:
http = HTTP(data)
http_info = str(http.data).split('\n')
for line in http_info:
print(DATA_TAB_3 + str(line))
except:
print(format_output_line(DATA_TAB_3, data))
else:
print(TAB_2 + 'TCP Data:')
print(format_output_line(DATA_TAB_3, data))
# UDP
elif proto == 17:
src_port, dest_port, length, data = udp_seg(data)
print(TAB_1 + 'UDP Segment:')
print(TAB_2 + 'Source Port: {}, Destination Port: {}, Length: {}'.format(src_port, dest_port, length))
# Other IPv4
else:
print(TAB_1 + 'Other IPv4 Data:')
print(format_output_line(DATA_TAB_2, data))
else:
print('Ethernet Data:')
print(format_output_line(DATA_TAB_1, data))
# Unpack Ethernet Frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(proto), data[14:]
# Format MAC Address
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
mac_addr = ':'.join(bytes_str).upper()
return mac_addr
# Unpack IPv4 Packets Recieved
def ipv4_Packet(data):
version_header_len = data[0]
version = version_header_len >> 4
header_len = (version_header_len & 15) * 4
ttl, proto, src, target = struct.unpack('! 8x B B 2x 4s 4s', data[:20])
return version, header_len, ttl, proto, ipv4(src), ipv4(target), data[header_len:]
# Returns Formatted IP Address
def ipv4(addr):
return '.'.join(map(str, addr))
# Unpacks for any ICMP Packet
def icmp_packet(data):
icmp_type, code, checksum = struct.unpack('! B B H', data[:4])
return icmp_type, code, checksum, data[4:]
# Unpacks for any TCP Packet
def tcp_seg(data):
(src_port, destination_port, sequence, acknowledgenment, offset_reserv_flag) = struct.unpack('! H H L L H', data[:14])
offset = (offset_reserv_flag >> 12) * 4
flag_urg = (offset_reserved_flag & 32) >> 5
flag_ack = (offset_reserved_flag & 32) >>4
flag_psh = (offset_reserved_flag & 32) >> 3
flag_rst = (offset_reserved_flag & 32) >> 2
flag_syn = (offset_reserved_flag & 32) >> 1
flag_fin = (offset_reserved_flag & 32) >> 1
return src_port, dest_port, sequence, acknowledgement, flag_urg, flag_ack, flag_psh, flag_rst, flag_syn, flag_fin, data[offset:]
# Unpacks for any UDP Packet
def udp_seg(data):
src_port, dest_port, size = struct.unpack('! H H 2x H', data[:8])
return src_port, dest_port, size, data[8:]
# Formats the output line
def format_output_line(prefix, string, size=80):
size -= len(prefix)
if isinstance(string, bytes):
string = ''.join(r'\x{:02x}'.format(byte) for byte in string)
if size % 2:
size-= 1
return '\n'.join([prefix + line for line in textwrap.wrap(string, size)])
main()