Python编写的ARP扫描工具
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python编写的ARP扫描工具相关的知识,希望对你有一定的参考价值。
源码如下:
1 rom scapy.all import * 2 import threading 3 import argparse 4 import logging 5 import re 6 7 logging.getLogger(‘scapy.runtime‘).setLevel(logging.ERROR) 8 9 def parse_ip(targets): 10 ‘‘‘ 11 解析192.168.1.1-254形式的IP段,分解成IP列表 12 ‘‘‘ 13 _split = targets.split(‘-‘) 14 first_ip = _split[0] 15 ip_split = first_ip.split(‘.‘) 16 ipdot4 = range(int(ip_split[3]),int(_split[1])+1) 17 ipaddrs = [ip_split[0]+‘.‘+ip_split[1]+‘.‘+ip_split[2]+‘.‘+str(p) for p in ipdot4] 18 return ipaddrs 19 20 def arp_scan(target_ip): 21 ‘‘‘ 22 通过scapy的sr1函数进行ARP扫描 23 ‘‘‘ 24 try: 25 ans = sr1(ARP(pdst=target_ip),timeout=1,verbose=False) 26 if ans: 27 return ans 28 except Exception: 29 print ‘[-]发包错误‘ 30 exit(1) 31 32 def parse_arp(target_ip): 33 ‘‘‘ 34 解析收到的ARP reply包,采集IP及其对应的MAC 35 ‘‘‘ 36 ans = arp_scan(target_ip) 37 if ans: 38 if ans.haslayer(‘ARP‘) and ans.fields[‘op‘] == 2: 39 print ‘[+] IP:%s => MAC:%s‘ % (ans.fields[‘psrc‘],ans.fields[‘hwsrc‘]) 40 41 if __name__ == ‘__main__‘: 42 usage = ‘python %(prog)s -t [targets]‘ 43 parser = argparse.ArgumentParser(usage=usage,epilog=‘以上做为说明,祝好运!‘,description=‘说明:指定IP或IP段进行ARP扫描.‘,version=‘V1.0‘) 44 parser.add_argument(‘-t‘,action=‘store‘,dest=‘targets‘,help=‘targets为IP或IP段,如192.168.1.x或192.168.1.1-254‘) 45 46 args = parser.parse_args() 47 if args.targets == None: 48 parser.print_help() 49 elif (not re.match(r‘\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$‘,args.targets)) and 50 (not re.match(r‘\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-\d{1,3}$‘,args.targets)): 51 parser.print_help() 52 else: 53 targets = args.targets 54 55 56 if re.match(r‘\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$‘,targets): 57 ip = targets 58 parse_arp(ip) 59 elif re.match(r‘\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-\d{1,3}$‘,targets): 60 ips = parse_ip(targets) 61 for ip in ips: 62 t = threading.Thread(target=parse_arp,args=(ip,)) 63 t.start()
运行结果如下:
1 python exp2.py -t 192.168.1.1-254 2 [+] IP:192.168.1.1 => MAC:14:75:90:XX:XX:XX 3 [+] IP:192.168.1.111 => MAC:c6:36:55:XX:XX:XX 4 [+] IP:192.168.1.100 => MAC:68:3e:34:XX:XX:XX 5 [+] IP:192.168.1.112 => MAC:84:38:38:XX:XX:XX 6 [+] IP:192.168.1.114 => MAC:6c:8d:c1:XX:XX:XX 7 [+] IP:192.168.1.103 => MAC:84:38:38:XX:XX:XX 8 [+] IP:192.168.1.102 => MAC:58:1f:28:XX:XX:XX
以上是关于Python编写的ARP扫描工具的主要内容,如果未能解决你的问题,请参考以下文章