0

下面是我尝试构建的第一个 Python 项目的 NetworkScanner 的代码。#!/usr/bin/env python

import scapy.all as scapy
import optparse


def scan(ip):
    packet1 = scapy.ARP(pdst=ip)
    etherpacket = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    broadcast_packet = etherpacket / packet1
    ans = scapy.srp(broadcast_packet, timeout=60, verbose=False)[0]
    ret_list = list()

    for item in ans:
        dic = {}
        dic['ip'] = item[1].pdst
        dic['mac'] = item[1].hwdst
        ret_list.append(dic)
    print(ret_list)
    return ret_list


def printfun(returnlist):
    print("IP\t\t\tMAC Address\n----------------------------------------------")
    for elem in returnlist:
        print(elem["ip"] + "\t\t" + elem["mac"])


def getip():

    parser = optparse.OptionParser()
    parser.add_option('-i', "--ip", dest = 'received_ip', help="Please enter the ip you want to scan")
    (option, arguments) = parser.parse_args()
    return option.received_ip


ip = getip()
if ip:
    result = scan(ip)
    printfun(result)
else:
    print("no ip given")

现在我确实遵循了一些教程并学会了并行构建,这对我来说似乎是正确的,但我并不擅长。但是当我执行程序时,它只扫描执行它的虚拟主机本身的 IP。

/PycharmProjects/Networkscanner$ sudo python networkscanner.py -i 192.168.1.1/24
[{'ip': '192.168.1.205', 'mac': '08:00:27:1f:30:76'}, {'ip': '192.168.1.205', 'mac': '08:00:27:1f:30:76'}]
IP          MAC Address
----------------------------------------------
192.168.1.205       08:00:27:1f:30:76
192.168.1.205       08:00:27:1f:30:76

当我使用 python 的内置网络扫描仪时,它会给出这些结果。

 Currently scanning: Finished!   |   Screen View: Unique Hosts                 

 5 Captured ARP Req/Rep packets, from 4 hosts.   Total size: 300               
 _____________________________________________________________________________
   IP            At MAC Address     Count     Len  MAC Vendor / Hostname      
 -----------------------------------------------------------------------------
 192.168.1.1     a0:47:d7:36:2a:c0      2     120  Best IT World (India) Pvt Lt
 192.168.1.203   e4:42:a6:30:93:64      1      60  Intel Corporate             
 192.168.1.205   30:b5:c2:10:05:3b      1      60  TP-LINK TECHNOLOGIES CO.,LTD
 192.168.1.207   30:b5:c2:10:05:3b      1      60  TP-LINK TECHNOLOGIES CO.,LTD

编辑:我尝试了监控模式,但这无济于事。我还尝试在带有外部 WiFi 适配器的主窗口上运行它,仍然是同样的问题

有人可以帮助我的代码有什么问题吗?

4

1 回答 1

0

确定问题

如果我们更恰当地命名变量,问题就很明显了:

for sent_recv in ans:
    dic = {}
    return_packet = sent_recv[1]
    dic['ip'] = return_packet.pdst
    dic['mac'] = return_packet.hwdst
    ret_list.append(dic)
  • 每个item都是发送数据包和接收数据包的元组。这样命名有助于识别它。
  • 的第二个元素sent_recv是返回的数据包。让我们这样命名。
  • 返回数据包的目标 IP 和 MAC 地址将是我们机器的目标 IP 和 MAC 地址。这在您的输出上下文中是有意义的:您为每个响应获得自己的 IP/MAC。

因此,如果我们将其更改为返回数据包的 src IP/MAC,我们将获得所需的信息:

for sent_recv in ans:
    dic = {}
    return_packet = sent_recv[1]
    # Difference is dst => src here
    dic['ip'] = return_packet.psrc
    dic['mac'] = return_packet.hwsrc
    ret_list.append(dic)

注意:一个 ARP 数据包不应需要 60 秒才能返回 - 根据网络大小,它更多的是 10-100 毫秒。2s 的超时在这里更合适。

测试修复

用修改后的代码运行它,我们得到了想要的结果:

$ python script.py -i "192.168.1.0/24"
[{'ip': '192.168.1.48', 'mac': '00:1b:78:20:ee:40'}, 
 {'ip': '192.168.1.67', 'mac': '6c:33:a9:42:6a:18'}, 
...

IP          MAC Address
----------------------------------------------
192.168.1.48        00:1b:78:20:ee:40
192.168.1.67        6c:33:a9:42:6a:18
...
于 2020-04-19T18:34:51.320 回答