要判断对方是否为正常主机,需要进行ARP欺骗检测。下面是一个使用Python代码实现的方法:

  1. 导入必要的模块:
import scapy.all as scapy
import time
  1. 定义一个函数来获取本地MAC地址:
def get_mac(ip):
    arp_request = scapy.ARP(pdst=ip)
    broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
    arp_request_broadcast = broadcast/arp_request
    answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
    return answered_list[0][1].hwsrc
  1. 定义一个函数来检测ARP欺骗:
def arp_spoofing_detection(target_ip):
    target_mac = get_mac(target_ip)
    arp_packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc="192.168.1.1")
    send_packet = scapy.send(arp_packet, verbose=False)
    time.sleep(1)
    arp_table = scapy.ARP(pdst=target_ip)
    arp_res = scapy.srp(arp_table, timeout=1, verbose=False)[0]
    if len(arp_res) == 0:
        return "ARP欺骗检测:主机未响应"
    else:
        return "ARP欺骗检测:主机已响应"
  1. 调用函数进行检测:
target_ip = "192.168.1.2"
result = arp_spoofing_detection(target_ip)
print(result)

在上面的代码中,我们首先使用get_mac函数获取目标主机的MAC地址,然后构造一个ARP包,并发送给目标主机。接着,我们使用time.sleep函数等待1秒钟,以便目标主机有足够的时间来响应ARP请求。最后,我们再次发送一个ARP请求,来获取目标主机的ARP表。如果目标主机未响应ARP请求,则认为目标主机可能被ARP欺骗了。如果目标主机响应了ARP请求,则认为目标主机正常

如何用Python代码实现当接收到广播时如何判断对方是否为正常主机其ARP表中没有被篡改为伪MAC地址或者是伪IP地址

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

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