HCF.py 1.43 KB
import os

class HCF():
    def __init__(self, ipaddr) :
        self.IPaddresses = ipaddr
        self.IPaddressdict = {}
        self.InitialTTL = [2, 10, 30, 32, 60, 64, 128, 255]

        self.DoTraceroute(self.IPaddresses)


    # Traceroute 
    def DoTraceroute(self, IPaddress) :
        traceroute = []
        for ip in IPaddress :
            traceroute.append(os.popen("traceroute " + ip).read())

        # parsing
        for trace, ip in zip(traceroute, IPaddress) :
            splitline = trace.split('\n')
            lastline = splitline[len(splitline)-2]

            if len(splitline) < 2 : # no result
                lastline = splitline[len(splitline)-2]
            
            hc = lastline.split(' ')[0] 
            try:
                int(hc)
            except:
                hc = lastline.split(" ")[1]

            self.IPaddressdict[ip] = str(int(hc)-1)
        print(self.IPaddressdict)

    def CheckHopCount(self, IP, HopCount):
        if str(self.IPaddressdict[IP]) != str(HopCount) :
            return True
        else :
            return False
    

    def IsSpoofed(self, packet):
        TTL = packet['ttl']
        src = packet['src']

        initialTTL = 2

        for e in self.InitialTTL :
            if TTL < e :
                initialTTL = e
                break
        hc = initialTTL - TTL

        flag = self.CheckHopCount(src, hc)

        print("IP Spoofing " + str(flag))

        return flag