Python-compiled ARP scanning tool and pythonarp scanning tool
The source code is as follows:
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 resolve the IP address segment in the format of 192.168.1.1-254, and break it into the IP address list 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_spli T [1] + '. '+ ip_split [2] + '. '+ str (p) for p in ipdot4] 18 return ipaddrs19 20 def arp_scan (target_ip ): 21 ''' 22 ARP scan through scapy's sr1 function 23 ''' 24 try: 25 ans = sr1 (ARP (pdst = target_ip), timeout = 1, verbose = False) 26 if ans: 27 return ans28 failed t Exception: 29 print '[-] packet sending error '30 exit (1) 31 32 def parse_arp (target_ip ): 33 ''' 34 parse the received ARP reply package, and collect the IP address and its corresponding mac35''' 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. good luck! ', Description =' description: Specifies the IP address or IP segment for ARP scanning. ', version = 'v1. 0 ') 44 parser. add_argument ('-t', action = 'store', dest = 'targets', help = 'Targets is an IP address or IP segment, such as 192.168.1.x or 192.168.1.1-254 ') 45 46 args = parser. parse_args () 47 if args.tar gets = None: 48 parser. print_help () 49 elif (not re. match (R' \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d00001, 3100000000', args.tar gets) and \ 50 (not re. match (R' \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3}-\ d1_1, 3} ', args.tar gets): 51 parser. print_help () 52 else: 53 targets = args.tar gets54 55 56 if re. match (R' \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3} $ ', targets): 57 ip = targets58 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 ()
The running result is as follows:
1 python exp2.py -t 192.168.1.1-2542 [+] IP:192.168.1.1 => MAC:14:75:90:XX:XX:XX3 [+] IP:192.168.1.111 => MAC:c6:36:55:XX:XX:XX4 [+] IP:192.168.1.100 => MAC:68:3e:34:XX:XX:XX5 [+] IP:192.168.1.112 => MAC:84:38:38:XX:XX:XX6 [+] IP:192.168.1.114 => MAC:6c:8d:c1:XX:XX:XX7 [+] IP:192.168.1.103 => MAC:84:38:38:XX:XX:XX8 [+] IP:192.168.1.102 => MAC:58:1f:28:XX:XX:XX