HCF.py
1.43 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
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