簡體   English   中英

如何使用 Scapy 實現 ARP ping?

[英]How to implement ARP ping with Scapy?

我一直在嘗試創建一個類似於 netdiscover 的網絡掃描儀。 我使用 Python 和 Scapy 模塊來做到這一點。 我在虛擬盒子上的 Kali linux 上運行我的腳本,當我掃描由 Virtual Box 創建的 NAT 網絡時,它向我顯示已連接的設備,但是當我使用無線適配器掃描我的 wifi 網絡時,掃描儀無法找到任何設備,這很奇怪,因為 netdiscover 找到了大量的設備。 但是,當我使用 Scapy 實現的 arping function 時,設備也會顯示,但是當我運行我的代碼時,它沒有檢測到任何設備。 這是為什么?

我使用了 Scapy 文檔建議的代碼,但它仍然沒有顯示任何設備。 只有 Scapy arping function 完全檢測到任何設備

import scapy.all as scapy
import subprocess as sub
import re

def get_IP():
    output=sub.check_output("route -n",shell=True)
    ips={}
    for row in output.split("\n")[2:]:
        found=re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",row)
        device=re.findall("[a-z]{2,10}\d$",row)

        for ip in found:
            if ("0.0.0" not in ip and "255.255.255" not in ip):
                ips[device[0]]=ip
    for device,ip in ips.items():
        print("Device: {}\tIP: {}".format(device,ip))

    device = raw_input("Choose a device > ")
    return(ips[device][:-1]+"1/24")

def scan(ip):
    #My code
    print("Scanning...")
    arp_request=scapy.ARP(pdst=ip)
    brodcast=scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
    arp=brodcast/arp_request
    answered=scapy.srp(arp, timeout=1,verbose=False)[0]
    for element in answered:
        print("IP:{}".format(element[1].psrc))
        print("MAC address: {}\n".format(element[1].hwsrc))
def scan2(ip):
    #Code from scapy documentation and it's also not detecting any devices
    ans, unans = scapy.srp(scapy.Ether(dst="ff:ff:ff:ff:ff:ff")/scapy.ARP(pdst=ip),timeout=2)
    ans.summary(lambda (s,r): r.sprintf("%Ether.src% %ARP.psrc%") )
def scan3(ip):
    #This works
    scapy.arping(ip)

ip = get_IP()

scan(ip)
scan2(ip)
scan3(ip)

我只是通過停用與 NAT 網絡的連接來解決它,所以我使用ifconfig eth0 down 但是在某些情況下,這不是問題。 如果您的路由器不允許網絡掃描,您需要更改您的 MAC 地址,這意味着您需要運行這些命令系列

ifconfig wlan0 down
ifconfig wlan0 hw ether 00:22:44:66:88:33 # Ofcourse you can choose any MAC address you want
ifconfig wlan0 down
ifconfig wlan0 up
service network-manager restart

之后,網絡掃描儀將檢測當前在網絡中的設備

試試這個方法:

from scapy.all import scapy,ARP,Ether,srp,arping

或者這樣:

from scapy.layers.l2 import *

在這兩種情況下,請記住刪除“scapy.”,如下所示:

#Before
scapy.arping(ip)
#After
arping(ip)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM