Server.py 1.41 KB
from HCF import *
from struct import *
import sys
import socket

def DetectingPacket():
    IP = []
    for i in range(1, 10):
        ip = '10.0.'+str(i+1)+'.100'
        IP.append(ip)
    packetHCF = HCF(IP)

    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 = packetManagerHCF.IsSpoofed({'ttl':ttl, 'src':source_addr})
            except Exception as e :
                print(e)
                continue

if __name__ == "__main__":
    DetectingPacket()