Python3 Scapy实现ARP扫描

#!/usr/bin/python3
# -*- coding: utf-8 -*- 
# --author:valecalida--
from scapy.layers.inet import Ether
from scapy.layers.l2 import ARP
from scapy.sendrecv import srp
from threading import Thread
from ipaddress import ip_network
from time import time
import sys


def scan_single(host):
    packet = Ether(dst='FF:FF:FF:FF:FF:FF')/ARP(op=1, pdst=host, hwdst='00:00:00:00:00:00')
    response, _ = srp(packet, timeout=1, verbose=False)
    if response:
        print("IP: %s\t\tMac:%s" % (str.ljust(host, 15), response[0][1].getlayer(ARP).fields['hwsrc']))


def scan_arp(network):
    ip_list = ip_network(network)
    for ip in ip_list:
        t = Thread(target=scan_single, args=[str(ip)])
        t.start()


if __name__ == '__main__':
    t1 = time()
    if len(sys.argv) == 2:
        obj = sys.argv[1]
        scan_arp(obj)
        t2 = time()
        print("[+] 本次扫描共花费 %s 秒" % (t2 - t1))
    else:
        print("Usage:\n\tpython3 Arp_Scan.py 192.168.1.0/24")
        sys.exit()


运行时长在1.3-1.5秒左右

更多请关注:

https://valecasec.github.io/

 

你可能感兴趣的:(python)