-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_sniffer.py
More file actions
45 lines (31 loc) · 1011 Bytes
/
port_sniffer.py
File metadata and controls
45 lines (31 loc) · 1011 Bytes
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
# Packet sniffer in python for Linux
# Sniffs only incoming TCP packet
import socket
import time
from struct import *
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
print("Packet sniffer is running...")
# receive a packet
while True:
packet = s.recvfrom(65565)
# packet string from tuple
packet = packet[0]
# take first 20 characters for the ip header
ip_header = packet[0:20]
# unpack ip header
iph = unpack('!BBHHHBBH4s4s', ip_header)
version_ihl = iph[0]
ihl = version_ihl & 0xF
iph_length = ihl * 4
tcp_header = packet[iph_length:iph_length + 20]
# unpack header
tcph = unpack('!HHLLBBHHH', tcp_header)
source_port = tcph[0]
dest_port = tcph[1]
# set port here.
if dest_port == 8000:
print("RECEIVED from port : {}".format(dest_port))
# sleep for 1 second
time.sleep(1)
main()