Server.py
1.48 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
#from hopCountFiltering import HCFStateManager
from HCF import *
from struct import *
import sys
import socket
def PacketMonitoring():
IP = []
for i in range(1, 10):
ip = '10.0.'+str(i+1)+'.100'
IP.append(ip)
packetManager = HCF(IP)
# set up the socket
try:
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
except socket.error as e:
err = e.args[0]
print ("Error setting up socket: ", e)
sys.exit()
while True:
packet = sock.recvfrom(65535)
packet = packet[0]
eth_length = 14
eth_header = packet[:eth_length]
eth = unpack('!6s6sH', eth_header)
eth_protocol = socket.ntohs(eth[2])
if eth_protocol == 8 : # IP protocol
ip_header = packet[eth_length:20+eth_length]
iphead = unpack("!BBHHHBBH4s4s", ip_header)
version_ihl = iphead[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iphead[5]
source_addr = socket.inet_ntoa(iphead[8])
dest_addr = socket.inet_ntoa(iphead[9])
print("\nSource IP address: ", source_addr)
print("TTL: ", ttl)
try :
packetTest = packetManager.IsSpoofed({'ttl':ttl, 'src':source_addr})
except Exception as e :
print(e)
continue
if __name__ == "__main__":
PacketMonitoring()