使用 Python 和 Scapy 检测 ARP 欺骗攻击
使用 Python 和 Scapy 检测 ARP 欺骗攻击
ARP 欺骗是一种网络攻击,攻击者通过发送伪造的 ARP 消息,将自己的 MAC 地址与网络上其他设备的 IP 地址相关联。这使得攻击者可以拦截或修改网络流量,从而窃取敏感信息或发动其他攻击。
本教程将介绍如何使用 Python 和 Scapy 库构建一个简单的 ARP 欺骗检测工具。Scapy 是一个强大的网络数据包处理库,允许我们发送和接收自定义网络数据包。
代码示例
以下 Python 代码使用 Scapy 库实现了一个简单的 ARP 欺骗检测工具:
import threading
import time
import netifaces
from scapy.all import *
class ArpDetectorPage:
def __init__(self, master, timeout):
self.master = master
self.timeout = timeout
self.interface = 'ens33'
self.ip_mac_map = {}
self.attacker_ip = None
self.attacker_mac = None
self.thread = None
self.stop_event = threading.Event()
self.frame = tk.Frame(self.master)
self.frame.pack()
self.status_label = tk.Label(self.frame, text='Idle')
self.status_label.pack()
self.start_button = tk.Button(self.frame, text='Start', command=self.start_detection)
self.start_button.pack()
self.stop_button = tk.Button(self.frame, text='Stop', command=self.stop_detection, state=tk.DISABLED)
self.stop_button.pack()
def start_detection(self):
self.status_label.config(text='Running')
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.thread = threading.Thread(target=self.run)
self.thread.start()
def stop_detection(self):
self.status_label.config(text='Idle')
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.stop_event.set()
self.thread.join()
def run(self):
self.attacker_ip = netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr']
self.attacker_mac = getmacbyip(self.attacker_ip)
print(f'Attacker IP: {self.attacker_ip}, MAC: {self.attacker_mac}')
while not self.stop_event.is_set():
self.scan_network()
self.detect_arp_spoofing()
time.sleep(5)
self.timer += 5
if self.timer >= self.timeout:
print('No ARP spoofing detected')
self.stop_event.set()
def scan_network(self):
for ip in netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr'].split('.')[:-1]:
for i in range(1, 255):
target_ip = f'{ip}.{i}'
if target_ip != self.attacker_ip:
arp_request = ARP(pdst=target_ip)
arp_reply = sr1(arp_request, timeout=1, verbose=0)
if arp_reply and arp_reply.hwsrc not in ('00:00:00:00:00:00', self.attacker_mac):
self.ip_mac_map[target_ip] = arp_reply.hwsrc
def detect_arp_spoofing(self):
for target_ip, target_mac in self.ip_mac_map.items():
arp_request = ARP(op=1, pdst=target_ip, hwdst=target_mac, psrc=self.attacker_ip, hwsrc=self.attacker_mac)
arp_reply = sr1(arp_request, timeout=1, verbose=0)
if arp_reply and arp_reply.hwsrc != target_mac:
print(f'ARP spoofing detected: {target_ip} ({target_mac}) -> {arp_reply.hwsrc}')
class ArpDetectorPageUI:
def __init__(self, master):
self.master = master
self.master.title('ARP Detector')
self.master.geometry('400x150')
self.timeout_label = tk.Label(self.master, text='Detection Time (s)')
self.timeout_label.pack()
self.timeout_entry = tk.Entry(self.master)
self.timeout_entry.pack()
self.start_button = tk.Button(self.master, text='Start', command=self.start_detection)
self.start_button.pack()
def start_detection(self):
timeout = int(self.timeout_entry.get())
self.master.withdraw()
ArpDetectorPage(self.master, timeout)
代码说明:
- 导入必要的库:
threading,time,netifaces,scapy.all - 创建
ArpDetectorPage类:- 初始化方法 (
__init__) 设置界面元素和初始变量。 start_detection方法启动检测线程。stop_detection方法停止检测线程。run方法循环执行网络扫描和 ARP 欺骗检测。scan_network方法发送 ARP 请求并记录响应中的 IP 地址和 MAC 地址映射关系。detect_arp_spoofing方法发送 ARP 请求,并检查响应中的 MAC 地址是否与之前记录的地址匹配。
- 初始化方法 (
- 创建
ArpDetectorPageUI类:- 初始化方法 (
__init__) 创建图形界面元素。 start_detection方法获取用户设置的超时时间,并创建ArpDetectorPage对象开始检测。
- 初始化方法 (
如何使用
- 安装 Python 和 Scapy 库。
- 将代码保存为 Python 文件(例如,
arp_detector.py)。 - 在终端中运行脚本:
python arp_detector.py。 - 在图形界面中输入检测时间(以秒为单位),然后单击“开始”按钮。
- 该工具将扫描网络并检测任何 ARP 欺骗攻击。如果检测到攻击,它将在控制台中打印相关信息。
结论
本教程介绍了如何使用 Python 和 Scapy 库构建简单的 ARP 欺骗检测工具。该工具可以帮助你了解 ARP 欺骗的工作原理,并提高你对网络安全的认识。请注意,这只是一个简单的示例,实际的 ARP 欺骗攻击可能更加复杂和难以检测。
原文地址: https://www.cveoy.top/t/topic/jnVH 著作权归作者所有。请勿转载和采集!