Showing
3 changed files
with
213 additions
and
0 deletions
Code/detection/Hop Count Filtering/HCF.py
0 → 100644
1 | +import os | ||
2 | + | ||
3 | +class HCF(): | ||
4 | + def __init__(self, ipaddr) : | ||
5 | + self.IPaddresses = ipaddr | ||
6 | + self.IPaddressdict = {} | ||
7 | + self.InitialTTL = [2, 10, 30, 32, 60, 64, 128, 255] | ||
8 | + | ||
9 | + self.DoTraceroute(self.IPaddresses) | ||
10 | + | ||
11 | + | ||
12 | + # Traceroute | ||
13 | + def DoTraceroute(self, IPaddress) : | ||
14 | + traceroute = [] | ||
15 | + for ip in IPaddress : | ||
16 | + traceroute.append(os.popen("traceroute " + ip).read()) | ||
17 | + | ||
18 | + # parsing | ||
19 | + for trace, ip in zip(traceroute, IPaddress) : | ||
20 | + splitline = trace.split('\n') | ||
21 | + lastline = splitline[len(splitline)-2] | ||
22 | + | ||
23 | + if len(splitline) < 2 : # no result | ||
24 | + lastline = splitline[len(splitline)-2] | ||
25 | + | ||
26 | + hc = lastline.split(' ')[0] | ||
27 | + try: | ||
28 | + int(hc) | ||
29 | + except: | ||
30 | + hc = lastline.split(" ")[1] | ||
31 | + | ||
32 | + self.IPaddressdict[ip] = str(int(hc)-1) | ||
33 | + print(self.IPaddressdict) | ||
34 | + | ||
35 | + def CheckHopCount(self, IP, HopCount): | ||
36 | + if str(self.IPaddressdict[IP]) != str(HopCount) : | ||
37 | + return True | ||
38 | + else : | ||
39 | + return False | ||
40 | + | ||
41 | + | ||
42 | + def IsSpoofed(self, packet): | ||
43 | + TTL = packet['ttl'] | ||
44 | + src = packet['src'] | ||
45 | + | ||
46 | + initialTTL = 2 | ||
47 | + | ||
48 | + for e in self.InitialTTL : | ||
49 | + if TTL < e : | ||
50 | + initialTTL = e | ||
51 | + break | ||
52 | + hc = initialTTL - TTL | ||
53 | + | ||
54 | + flag = self.CheckHopCount(src, hc) | ||
55 | + | ||
56 | + print("IP Spoofing " + str(flag)) | ||
57 | + | ||
58 | + return flag | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
Code/detection/Hop Count Filtering/Server.py
0 → 100644
1 | +#from hopCountFiltering import HCFStateManager | ||
2 | +from HCF import * | ||
3 | +from struct import * | ||
4 | +import sys | ||
5 | +import socket | ||
6 | + | ||
7 | +def PacketMonitoring(): | ||
8 | + IP = [] | ||
9 | + for i in range(1, 10): | ||
10 | + ip = '10.0.'+str(i+1)+'.100' | ||
11 | + IP.append(ip) | ||
12 | + packetManager = HCF(IP) | ||
13 | + | ||
14 | + # set up the socket | ||
15 | + try: | ||
16 | + sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) | ||
17 | + | ||
18 | + except socket.error as e: | ||
19 | + err = e.args[0] | ||
20 | + print ("Error setting up socket: ", e) | ||
21 | + sys.exit() | ||
22 | + | ||
23 | + while True: | ||
24 | + packet = sock.recvfrom(65535) | ||
25 | + packet = packet[0] | ||
26 | + eth_length = 14 | ||
27 | + | ||
28 | + eth_header = packet[:eth_length] | ||
29 | + eth = unpack('!6s6sH', eth_header) | ||
30 | + eth_protocol = socket.ntohs(eth[2]) | ||
31 | + | ||
32 | + if eth_protocol == 8 : # IP protocol | ||
33 | + ip_header = packet[eth_length:20+eth_length] | ||
34 | + iphead = unpack("!BBHHHBBH4s4s", ip_header) | ||
35 | + version_ihl = iphead[0] | ||
36 | + version = version_ihl >> 4 | ||
37 | + ihl = version_ihl & 0xF | ||
38 | + iph_length = ihl * 4 | ||
39 | + ttl = iphead[5] | ||
40 | + source_addr = socket.inet_ntoa(iphead[8]) | ||
41 | + dest_addr = socket.inet_ntoa(iphead[9]) | ||
42 | + | ||
43 | + print("\nSource IP address: ", source_addr) | ||
44 | + print("TTL: ", ttl) | ||
45 | + | ||
46 | + try : | ||
47 | + packetTest = packetManager.IsSpoofed({'ttl':ttl, 'src':source_addr}) | ||
48 | + except Exception as e : | ||
49 | + print(e) | ||
50 | + continue | ||
51 | + | ||
52 | +if __name__ == "__main__": | ||
53 | + PacketMonitoring() |
1 | +#!/usr/bin/python | ||
2 | +from mininet.topo import Topo | ||
3 | +from mininet.net import Mininet | ||
4 | +from mininet.node import Node | ||
5 | +from mininet.log import setLogLevel, info | ||
6 | +from mininet.cli import CLI | ||
7 | + | ||
8 | +class LinuxRouter( Node ): | ||
9 | + "A Node with IP forwarding enabled." | ||
10 | + | ||
11 | + def config( self, **params ): | ||
12 | + super( LinuxRouter, self).config( **params ) | ||
13 | + # Enable forwarding on the router | ||
14 | + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) | ||
15 | + | ||
16 | + def terminate( self ): | ||
17 | + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) | ||
18 | + super( LinuxRouter, self ).terminate() | ||
19 | + | ||
20 | + | ||
21 | +class NetworkTopo(Topo) : | ||
22 | + def __init__(self) : | ||
23 | + self.routerIPlist = ['10.0.1.1/24', '10.0.2.1/24', '10.0.3.1/24'] | ||
24 | + self.n = 10 # n > 3 | ||
25 | + self.routerlist = [0] * self.n | ||
26 | + self.switchlist = [0] * self.n | ||
27 | + self.hostlist = [0] * self.n | ||
28 | + Topo.__init__(self) | ||
29 | + | ||
30 | + def CreateRouterIP(self) : | ||
31 | + default = '10.0.' | ||
32 | + for i in range(3, self.n) : | ||
33 | + router_ip = default + str(i+1) + '.1/24' | ||
34 | + self.routerIPlist.append(router_ip) | ||
35 | + | ||
36 | + def CreateRouter(self): | ||
37 | + for i in range(0, self.n): | ||
38 | + name = 'r' + str(i) | ||
39 | + self.routerlist[i] = self.addNode(name, cls=LinuxRouter, ip=self.routerIPlist[i]) | ||
40 | + | ||
41 | + def CreateSwitch(self) : | ||
42 | + for i in range(0, self.n): | ||
43 | + name = 's'+str(i) | ||
44 | + self.switchlist[i] = self.addSwitch(name) | ||
45 | + | ||
46 | + def Createlink(self): | ||
47 | + # router <-> switch | ||
48 | + for i in range(0, self.n): | ||
49 | + name2 = 'r'+str(i)+'-eth1' | ||
50 | + self.addLink(self.switchlist[i], self.routerlist[i], intfName2=name2 , params2={'ip':self.routerIPlist[i]}) | ||
51 | + | ||
52 | + # router <-> router | ||
53 | + self.addLink(self.routerlist[0], self.routerlist[1], intfName1='r0-eth0', intfName2='r1-eth0', params1={'ip':'10.100.0.1/24'}, params2={'ip':'10.100.0.2/24'}) | ||
54 | + | ||
55 | + cnt = 1 | ||
56 | + for i in range(2, self.n): | ||
57 | + name1 = 'r'+str(i-1)+'-eth2' | ||
58 | + name2 = 'r'+str(i)+'-eth0' | ||
59 | + ip1 = '10.10'+str(cnt)+'.0.1/24' | ||
60 | + ip2 = '10.10'+str(cnt)+'.0.2/24' | ||
61 | + cnt += 1 | ||
62 | + self.addLink(self.routerlist[i-1], self.routerlist[i], intfName1=name1, intfName2=name2, params1={'ip':ip1}, params2={'ip':ip2}) | ||
63 | + | ||
64 | + def CreateHost(self) : | ||
65 | + for i in range(0, self.n) : | ||
66 | + name = 'h'+str(i) | ||
67 | + ip = '10.0.'+str(i+1)+'.100/24' | ||
68 | + defaultroute = 'via 10.0.'+str(i+1)+'.1' | ||
69 | + self.hostlist[i] = self.addHost(name, ip=ip, defaultRoute=defaultroute) | ||
70 | + | ||
71 | + def build(self, **_opts) : | ||
72 | + self.CreateRouterIP() | ||
73 | + self.CreateRouter() | ||
74 | + self.CreateSwitch() | ||
75 | + self.Createlink() | ||
76 | + self.CreateHost() | ||
77 | + | ||
78 | + # switch <-> host | ||
79 | + for h, s in zip(self.hostlist, self.switchlist): | ||
80 | + self.addLink(h, s) | ||
81 | + | ||
82 | + | ||
83 | +def run() : | ||
84 | + topo = NetworkTopo() | ||
85 | + net = Mininet(topo=topo) | ||
86 | + net.start() | ||
87 | + n = 10 # n > 3 | ||
88 | + router = [0] * n | ||
89 | + | ||
90 | + for i in range(0, n): | ||
91 | + name = 'r'+str(i) | ||
92 | + router[i] = net.get(name) | ||
93 | + router[i].cmd('python3 RIP.py&') | ||
94 | + | ||
95 | + info(net[name].cmd('route')) | ||
96 | + | ||
97 | + CLI(net) | ||
98 | + net.stop() | ||
99 | + | ||
100 | +if __name__ == '__main__' : | ||
101 | + setLogLevel('info') | ||
102 | + run() |
-
Please register or login to post a comment