这段代码旨在检测ARP欺骗攻击,原始代码使用arping命令,但因缺少网络接口参数导致错误。为了解决此问题,代码已经进行了改进,主要内容如下:

  1. 使用Scapy库替代arping命令,Scapy是一个强大的网络数据包处理库,可以更稳定地进行ARP操作。

  2. 使用Scapy的srp函数发送ARP请求,并根据返回结果分析ARP表变化,识别攻击源。

  3. 使用Scapy的sendp函数发送ARP欺骗攻击数据包,用于防御攻击。

修改后的代码如下:

import time
from scapy.all import *

GATEWAY_IP = '192.168.1.1'
GATEWAY_MAC = '00:11:22:33:44:55'
ATTACK_SOURCE = {}

def get_mac_by_arp(ip):
    ans, unans = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip), timeout=2, retry=2)
    for s, r in ans:
        return r[Ether].src

def send_arp_poisoning(target_ip, target_mac, source_ip):
    sendp(Ether(dst=target_mac)/ARP(op='is-at', psrc=source_ip, pdst=target_ip), verbose=0)

def detect_arp_poisoning():
    while True:
        arp_table = ARP(pdst='192.168.1.0/24')
        answered, unanswered = srp(arp_table, timeout=2, retry=2)
        for send, recv in answered:
            if GATEWAY_IP in recv.psrc and GATEWAY_MAC != recv.hwsrc:
                source_ip = recv.psrc
                source_mac = recv.hwsrc
                if source_mac not in ATTACK_SOURCE:
                    ATTACK_SOURCE[source_mac] = {'type': 'ARP欺骗攻击', 'count': 1}
                else:
                    ATTACK_SOURCE[source_mac]['count'] += 1
                    if ATTACK_SOURCE[source_mac]['count'] >= 10:
                        print('检测到ARP欺骗攻击,攻击源IP地址为', source_ip, 'MAC地址为', source_mac)
                        ATTACK_SOURCE[source_mac]['count'] = 0
                send_arp_poisoning(GATEWAY_IP, GATEWAY_MAC, source_ip)
        time.sleep(1)

if __name__ == '__main__':
    detect_arp_poisoning()

这段代码通过不断扫描ARP表,判断网关的MAC地址是否被篡改,从而检测ARP欺骗攻击。同时,代码还会对攻击源进行记录,并进行ARP欺骗攻击防御。

需要注意的是,代码中需要根据实际网络环境修改网关IP地址和MAC地址,以及攻击源记录的阈值等参数。

希望这段代码能够帮助你更好地理解和检测ARP欺骗攻击。

ARP欺骗攻击检测工具:使用Scapy库替代arping命令

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

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