이한솔

HCF

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
\ No newline at end of file
#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()
#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI
class LinuxRouter( Node ):
"A Node with IP forwarding enabled."
def config( self, **params ):
super( LinuxRouter, self).config( **params )
# Enable forwarding on the router
self.cmd( 'sysctl net.ipv4.ip_forward=1' )
def terminate( self ):
self.cmd( 'sysctl net.ipv4.ip_forward=0' )
super( LinuxRouter, self ).terminate()
class NetworkTopo(Topo) :
def __init__(self) :
self.routerIPlist = ['10.0.1.1/24', '10.0.2.1/24', '10.0.3.1/24']
self.n = 10 # n > 3
self.routerlist = [0] * self.n
self.switchlist = [0] * self.n
self.hostlist = [0] * self.n
Topo.__init__(self)
def CreateRouterIP(self) :
default = '10.0.'
for i in range(3, self.n) :
router_ip = default + str(i+1) + '.1/24'
self.routerIPlist.append(router_ip)
def CreateRouter(self):
for i in range(0, self.n):
name = 'r' + str(i)
self.routerlist[i] = self.addNode(name, cls=LinuxRouter, ip=self.routerIPlist[i])
def CreateSwitch(self) :
for i in range(0, self.n):
name = 's'+str(i)
self.switchlist[i] = self.addSwitch(name)
def Createlink(self):
# router <-> switch
for i in range(0, self.n):
name2 = 'r'+str(i)+'-eth1'
self.addLink(self.switchlist[i], self.routerlist[i], intfName2=name2 , params2={'ip':self.routerIPlist[i]})
# router <-> router
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'})
cnt = 1
for i in range(2, self.n):
name1 = 'r'+str(i-1)+'-eth2'
name2 = 'r'+str(i)+'-eth0'
ip1 = '10.10'+str(cnt)+'.0.1/24'
ip2 = '10.10'+str(cnt)+'.0.2/24'
cnt += 1
self.addLink(self.routerlist[i-1], self.routerlist[i], intfName1=name1, intfName2=name2, params1={'ip':ip1}, params2={'ip':ip2})
def CreateHost(self) :
for i in range(0, self.n) :
name = 'h'+str(i)
ip = '10.0.'+str(i+1)+'.100/24'
defaultroute = 'via 10.0.'+str(i+1)+'.1'
self.hostlist[i] = self.addHost(name, ip=ip, defaultRoute=defaultroute)
def build(self, **_opts) :
self.CreateRouterIP()
self.CreateRouter()
self.CreateSwitch()
self.Createlink()
self.CreateHost()
# switch <-> host
for h, s in zip(self.hostlist, self.switchlist):
self.addLink(h, s)
def run() :
topo = NetworkTopo()
net = Mininet(topo=topo)
net.start()
n = 10 # n > 3
router = [0] * n
for i in range(0, n):
name = 'r'+str(i)
router[i] = net.get(name)
router[i].cmd('python3 RIP.py&')
info(net[name].cmd('route'))
CLI(net)
net.stop()
if __name__ == '__main__' :
setLogLevel('info')
run()