过滤/标记打印结果,以便在Python / Scapy网络扫描仪中通过MAC地址识别设备

问题描述 投票:1回答:1

我是一个初学者,但我正在尝试制作一个网络扫描实用程序,该程序可以过滤特定需求的MAC地址;我工作的公司有网络连接的设备,这些设备根据序列号分配了MAC地址。我发现MAC地址的前6位数字是我们品牌的标志。我在下面做了一个str。 MAC地址的第4个字段是一小段常量,用于指示设备的型号。我已经准备好这些,但实际上是一些数字,例如“ 14”,“ 17”等。

我正在努力寻找一种方法,以“过滤”从扫描中检索出的MAC地址,并根据地址的字段对其进行标记。甚至更好的是,仅打印与startswith(mac_key)匹配的IP和Mac地址,并根据其MAC地址的第4个字段[9:11]标记其余对象。

[通过大量的阅读和帮助,到目前为止,我已经掌握了这一点:

    #!/usr/bin/env python
from scapy.all import ARP, Ether, srp
import socket
# importing main functions from Scapy and Socket

mac_key = '04:24:2f'
# Target value for first three fields of MAC address

hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
target_ip = ("192.168.{}.0/24".format(IPAddr[6]))
# Assigning index value for third section of IP address
# To make third section of target_ip a variable determined by host
# "/24" denotes IP address spectrum for the ARP packet destination

arp = ARP (pdst=target_ip)
# Creating ARP packet assigned to "target_ip"

ether = Ether(dst="ff:ff:ff:ff:ff:ff")
# Creating Ether broadcast packet
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting

packet = ether/arp
# Stacking

result = srp(packet, timeout=5, verbose=3)[0]
# Defining result with timeout parameter

clients= []
# Client list to be finished below

for sent, received in result:
    clients.append({'ip': received.psrc, 'mac': received.hwsrc})
    # For each response, append ip and mac address to 'clients' list

print("Devices On This Network:")
print("IP" + " "*18+"MAC")
# Print table of accumulated data

for client in clients:
    print("{:24}    {}".format(client['ip'], client['mac'].startswith(mac_key)))
# Printing IP addresses and assosciated MACs from clients list
# With bool checking against mac_key

下图是终端中的结果;想法是只打印显示TRUE值的行,并根据MAC地址的字段[9:11]添加标签,例如:出现TRUE bool的“ Network Device Pro”,并完全忽略该行, FALSE bool被触发。

编辑:好,我现在已将其转变为博客。我确实做了我想做的事,并且我将为尝试做类似事情的任何人提供以下代码。我乐于接受任何建议,以使其更加“ pythonic”并改善性能/语法。对于任何可以提供建议的人,我确实有一个问题。我想循环这段代码并将信息附加到呈现的列表中,并包括一个用户输入kill开关来完成它。这样,如果数据包在第一次扫描时未到达,则仍将其添加到列表中。奖励积分(如果您可以为此提供建议)以及一种删除在连续循环后无响应的条目的方法!

#!/usr/bin/env python3
# coding: utf-8
#
#
#//////////////////////////////////
#----------------------------------
# ippy Network Scanning Utility
#   ~ Daniel Johnston 2020 ~
#----------------------------------
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#
#
print("Initializing Scan...")
from scapy.all import ARP, Ether, srp
import socket
# importing main functions from Scapy and Socket

mac_key = ('00:25:2f')
# Target value for first three fields of MAC address (Brand Identifier)

MTU_key = ('13','15','16','17')
GW_key = ('21')
ECC_key = ('26')

#------serial numbers)------#

#('13','15','16','17','21','26'#

#---------LEGEND------------#
#serial numbers[0:3] = 'MTU'
#serial numbers[4] = 'Gateway'
#serial numbers[5] = 'ECC'
#---------------------------#

hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
target_ip = ("192.168.{}.0/24".format(IPAddr[6]))
# Assigning index value for third section of IP address
# To make third section of target_ip a variable determined by host
# "/24" denotes IP address spectrum for the ARP packet destination

def devsub(x):
        if x.startswith(MTU_key, 9, 11):
            print("{}   {}".format('MTU', client['ip'],))
        if x.startswith(GW_key, 9, 11):
            print("{}   {}".format('Gateway', client['ip'],))
        if x.startswith(ECC_key, 9, 11):
            print("{}   {}".format('ECC', client['ip'],))
# Defining function to print associated IP addresses, of MAC address    
# Matches(done later), and assigning a device name based on     
# index[9:11] of MAC

arp = ARP (pdst=target_ip)
# Creating ARP packet assigned to "target_ip"

ether = Ether(dst="ff:ff:ff:ff:ff:ff")
# Creating Ether broadcast packet
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting

packet = ether/arp
# Stacking

result = srp(packet, timeout=5, verbose=3)[0]
# Defining result with timeout parameter

clients= []
# Client list to be finished below

for sent, received in result:
    clients.append({'ip': received.psrc, 'mac': received.hwsrc})
# For each response, append ip and mac address to 'clients' list

print("~"*20)
print("-"*20)
print("Devices On This Network:")
print("-"*20)
print("Device" + " " * 4 + "IP Address")
#Text formatting
for client in clients:
    if client['mac'].startswith(mac_key):
        devsub(client['mac'])
#Running primary filter to only include MAC addresses that pass
# .startswith(mac_key) into devsub function to print device name 
# Associated with MAC[9:11] variables, and the appropriate IP address

所以这是当前输出,带有1个匹配设备

enter image description here

我是一个初学者,但我正在尝试制作一个网络扫描实用程序,该程序可以过滤特定需求的MAC地址;我工作的公司的网络连接设备分配了MAC ...

python networking scapy
1个回答
0
投票

使用Scapy内置函数比重写它们更有意义。在这种情况下,请使用arping。为了节省时间(如此处),请阅读the manual

© www.soinside.com 2019 - 2024. All rights reserved.