基于Linux的ARP攻击检测软件 - Python实现
基于Linux的ARP攻击检测软件 - Python实现
本文介绍使用Python实现基于Linux的ARP攻击检测软件,通过分析ARP包识别潜在的攻击行为,并记录日志。该软件支持多种攻击类型检测,包括ARP欺骗、ARP泛洪和ARP缓存中毒。
工作原理
该软件通过以下规则检测ARP攻击:
- 同一IP地址对应多个MAC地址:当发现一个IP地址对应多个MAC地址时,可能存在ARP欺骗攻击。
- 多个IP地址对应同一个MAC地址:当发现多个IP地址对应同一个MAC地址时,可能存在ARP缓存中毒攻击。
- 大量的ARP请求或响应包:当发现大量的ARP请求或响应包时,可能存在ARP泛洪攻击。
- ARP包中的源MAC地址和目标MAC地址不匹配:当发现ARP包中的源MAC地址和目标MAC地址不匹配时,可能存在ARP欺骗攻击。
代码实现
import logging
from scapy.all import *
logging.basicConfig(filename='arp_attack.log', level=logging.INFO)
def arp_analysis(pkt):
if pkt.haslayer(ARP):
arp_pkt = pkt.getlayer(ARP)
if arp_pkt.op == 1: # ARP Request
if arp_pkt.pdst in ip_mac_dict:
if arp_pkt.hwsrc != ip_mac_dict[arp_pkt.pdst]:
logging.warning(f'ARP Spoofing Detected! IP address {arp_pkt.pdst} is associated with multiple MAC addresses: {ip_mac_dict[arp_pkt.pdst]} and {arp_pkt.hwsrc}')
print(f'�33[31mARP Spoofing Detected! IP address {arp_pkt.pdst} is associated with multiple MAC addresses: {ip_mac_dict[arp_pkt.pdst]} and {arp_pkt.hwsrc}�33[0m')
else:
ip_mac_dict[arp_pkt.pdst] = arp_pkt.hwsrc
elif arp_pkt.op == 2: # ARP Reply
if arp_pkt.hwsrc in mac_ip_dict:
if arp_pkt.psrc != mac_ip_dict[arp_pkt.hwsrc]:
logging.warning(f'ARP Spoofing Detected! MAC address {arp_pkt.hwsrc} is associated with multiple IP addresses: {mac_ip_dict[arp_pkt.hwsrc]} and {arp_pkt.psrc}')
print(f'�33[31mARP Spoofing Detected! MAC address {arp_pkt.hwsrc} is associated with multiple IP addresses: {mac_ip_dict[arp_pkt.hwsrc]} and {arp_pkt.psrc}�33[0m')
else:
mac_ip_dict[arp_pkt.hwsrc] = arp_pkt.psrc
if len(ip_mac_dict) > 10:
logging.warning('Possible ARP Flooding Detected! Large number of ARP requests detected.')
print('�33[31mPossible ARP Flooding Detected! Large number of ARP requests detected.�33[0m')
if len(mac_ip_dict) > 10:
logging.warning('Possible ARP Cache Poisoning Detected! Large number of ARP replies detected.')
print('�33[31mPossible ARP Cache Poisoning Detected! Large number of ARP replies detected.�33[0m')
if arp_pkt.hwsrc != pkt[Ether].src:
logging.warning(f'ARP Spoofing Detected! Source MAC address {arp_pkt.hwsrc} does not match the actual source MAC address {pkt[Ether].src}')
print(f'�33[31mARP Spoofing Detected! Source MAC address {arp_pkt.hwsrc} does not match the actual source MAC address {pkt[Ether].src}�33[0m')
ip_mac_dict = {}
mac_ip_dict = {}
sniff(filter='arp', prn=arp_analysis)
代码说明
import logging:导入日志模块;from scapy.all import *:导入Scapy模块;logging.basicConfig(filename='arp_attack.log', level=logging.INFO):配置日志文件名和日志级别;def arp_analysis(pkt):定义分析ARP包的函数;if pkt.haslayer(ARP)::判断数据包是否为ARP包;arp_pkt = pkt.getlayer(ARP):获取ARP包;if arp_pkt.op == 1::判断ARP包是ARP请求还是ARP响应;if arp_pkt.pdst in ip_mac_dict::判断IP地址是否已经存在于字典中;if arp_pkt.hwsrc != ip_mac_dict[arp_pkt.pdst]::判断MAC地址是否与字典中存储的MAC地址不一致;logging.warning(f'ARP Spoofing Detected! IP address {arp_pkt.pdst} is associated with multiple MAC addresses: {ip_mac_dict[arp_pkt.pdst]} and {arp_pkt.hwsrc}'):记录日志;print(f'�33[31mARP Spoofing Detected! IP address {arp_pkt.pdst} is associated with multiple MAC addresses: {ip_mac_dict[arp_pkt.pdst]} and {arp_pkt.hwsrc}�33[0m'):输出警告信息,IP地址标为红色;else::如果IP地址不存在于字典中,则将IP地址和MAC地址存储到字典中;elif arp_pkt.op == 2::判断ARP包是ARP请求还是ARP响应;if arp_pkt.hwsrc in mac_ip_dict::判断MAC地址是否已经存在于字典中;if arp_pkt.psrc != mac_ip_dict[arp_pkt.hwsrc]::判断IP地址是否与字典中存储的IP地址不一致;logging.warning(f'ARP Spoofing Detected! MAC address {arp_pkt.hwsrc} is associated with multiple IP addresses: {mac_ip_dict[arp_pkt.hwsrc]} and {arp_pkt.psrc}'):记录日志;print(f'�33[31mARP Spoofing Detected! MAC address {arp_pkt.hwsrc} is associated with multiple IP addresses: {mac_ip_dict[arp_pkt.hwsrc]} and {arp_pkt.psrc}�33[0m'):输出警告信息,IP地址标为红色;else::如果MAC地址不存在于字典中,则将MAC地址和IP地址存储到字典中;if len(ip_mac_dict) > 10::判断字典中存储的IP地址和MAC地址对数是否超过10个;logging.warning('Possible ARP Flooding Detected! Large number of ARP requests detected.'):记录日志;print('�33[31mPossible ARP Flooding Detected! Large number of ARP requests detected.�33[0m'):输出警告信息;if len(mac_ip_dict) > 10::判断字典中存储的MAC地址和IP地址对数是否超过10个;logging.warning('Possible ARP Cache Poisoning Detected! Large number of ARP replies detected.'):记录日志;print('�33[31mPossible ARP Cache Poisoning Detected! Large number of ARP replies detected.�33[0m'):输出警告信息;if arp_pkt.hwsrc != pkt[Ether].src::判断ARP包中的源MAC地址是否与数据包中的源MAC地址不一致;logging.warning(f'ARP Spoofing Detected! Source MAC address {arp_pkt.hwsrc} does not match the actual source MAC address {pkt[Ether].src}'):记录日志;print(f'�33[31mARP Spoofing Detected! Source MAC address {arp_pkt.hwsrc} does not match the actual source MAC address {pkt[Ether].src}�33[0m'):输出警告信息,源MAC地址标为红色;ip_mac_dict = {}和mac_ip_dict = {}:定义两个空字典,用于存储IP地址和MAC地址的对应关系;sniff(filter='arp', prn=arp_analysis):捕获ARP包,并调用arp_analysis()函数进行分析。
使用方法
- 安装Scapy库:
pip install scapy - 将代码保存为
.py文件,例如arp_detector.py - 运行代码:
python arp_detector.py
注意
- 该软件仅供学习和研究使用,请勿用于任何非法活动。
- 使用该软件可能会影响网络性能。
- 该软件可能无法检测所有类型的ARP攻击。
- 该软件需要管理员权限运行。
原文地址: https://www.cveoy.top/t/topic/joFf 著作权归作者所有。请勿转载和采集!