在 Windows 系统中,没有 fcntl 模块,因此需要使用其他模块来实现 ARP 检测防护软件设计。可以使用 scapy 模块来实现 ARP 检测,使用 netifaces 模块来获取本机的 IP 地址和 MAC 地址。

下面是一个基本的实现示例:

import netifaces
from scapy.all import *


def get_local_mac_address():
    '获取本机 MAC 地址'
    for interface in netifaces.interfaces():
        if interface == 'lo':
            continue
        mac = netifaces.ifaddresses(interface).get(netifaces.AF_LINK)
        if mac:
            return mac[0]['addr']


def get_local_ip_address():
    '获取本机 IP 地址'
    for interface in netifaces.interfaces():
        if interface == 'lo':
            continue
        ip = netifaces.ifaddresses(interface).get(netifaces.AF_INET)
        if ip:
            return ip[0]['addr']


def arp_scan(ip_range):
    '扫描指定 IP 范围内的主机'
    local_mac = get_local_mac_address()
    local_ip = get_local_ip_address()
    arp_request = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(op='who-has', hwsrc=local_mac, psrc=local_ip, pdst=ip_range)
    ans, _ = srp(arp_request, timeout=2, verbose=False)
    return [res[1].psrc for res in ans]


if __name__ == '__main__':
    ip_range = '192.168.1.0/24'
    active_hosts = arp_scan(ip_range)
    print(active_hosts)

这个示例中,使用 netifaces 模块获取本机的 MAC 地址和 IP 地址,使用 scapy 模块发送 ARP 请求来扫描指定 IP 范围内的主机,并返回活动主机的 IP 地址列表。可以根据需要对返回的主机列表进行进一步处理,例如与已知的主机列表比较,或者进行其他操作来保护网络安全。

Windows 下 Python ARP 检测防护软件设计:获取局域网主机信息

原文地址: https://www.cveoy.top/t/topic/jkZ9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录