基于Linux的ARP攻击检测软件开发:Python实现
基于Linux的ARP攻击检测软件开发:Python实现
本项目旨在使用Python语言和Scapy库,开发一款功能完善的ARP攻击检测软件。该软件将在Linux系统上运行,实时监控网络流量,并根据预设规则识别潜在的ARP攻击行为。
软件功能
- 用户登录注册: 允许用户创建账号并登录,确保系统安全性。
- 实时流量监控: 利用Scapy库捕获网络数据包,并过滤出ARP协议相关数据包。
- ARP攻击识别: 基于以下规则,识别疑似ARP攻击行为:
- 同一IP对应不同MAC地址: 检测应答报文时,如果同一个IP地址对应不同的MAC地址,则可能遭受ARP攻击。
- 非法请求报文: 检测请求报文时,判断是否合法,以及是否出现不同IP地址对应同一个MAC地址,如存在则可能遭受ARP攻击。
- 大量ARP请求/应答: 检测是否存在来自同一个IP地址或同一个MAC地址的大量ARP请求或应答,如存在则可能遭受ARP攻击。
- 攻击结果输出: 将检测到的疑似攻击信息以红色字体显示,并在日志文件中详细记录攻击时间、IP地址、MAC地址等信息。
- 开始/停止检测: 提供开始和停止检测按钮,方便用户控制检测进程。
Python实现
以下代码使用Python实现上述功能:
import os
import re
import time
import tkinter as tk
from scapy.all import *
from threading import Thread
# 定义常量
ARP_OP_REQUEST = 1
ARP_OP_REPLY = 2
ARP_CACHE_FILE = 'arp_cache.txt'
ARP_ATTACK_LOG = 'arp_attack.log'
class ARPDetect:
def __init__(self):
self.root = tk.Tk()
self.root.title('ARP攻击检测软件')
self.root.geometry('500x400')
self.create_widgets()
self.users = {'admin': '123456'} # 存储用户账号和密码
self.arp_cache = {} # 存储ARP缓存表
self.arp_attack_log = [] # 存储ARP攻击日志
def create_widgets(self):
# 登录界面
self.login_frame = tk.Frame(self.root)
self.login_frame.pack(padx=20, pady=20)
self.username_label = tk.Label(self.login_frame, text='用户名:')
self.username_label.grid(row=0, column=0)
self.username_entry = tk.Entry(self.login_frame, width=20)
self.username_entry.grid(row=0, column=1)
self.password_label = tk.Label(self.login_frame, text='密码:')
self.password_label.grid(row=1, column=0)
self.password_entry = tk.Entry(self.login_frame, width=20, show='*')
self.password_entry.grid(row=1, column=1)
self.login_button = tk.Button(self.login_frame, text='登录', command=self.login)
self.login_button.grid(row=2, column=0, pady=10)
self.register_button = tk.Button(self.login_frame, text='注册', command=self.register)
self.register_button.grid(row=2, column=1, pady=10)
# 功能界面
self.detect_frame = tk.Frame(self.root)
self.detect_frame.pack(padx=20, pady=20)
self.start_button = tk.Button(self.detect_frame, text='开始检测', command=self.start_detect)
self.start_button.pack(side=tk.LEFT)
self.stop_button = tk.Button(self.detect_frame, text='停止检测', command=self.stop_detect)
self.stop_button.pack(side=tk.LEFT, padx=10)
self.result_label = tk.Label(self.detect_frame, text='检测结果:')
self.result_label.pack(pady=10)
self.result_text = tk.Text(self.detect_frame, width=40, height=10)
self.result_text.pack()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
if username in self.users and self.users[username] == password:
self.login_frame.pack_forget()
self.detect_frame.pack()
else:
tk.messagebox.showerror('错误', '用户名或密码错误!')
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
if username in self.users:
tk.messagebox.showerror('错误', '该用户名已被注册!')
else:
self.users[username] = password
with open('users.txt', 'a') as f:
f.write(username + ',' + password + '
')
tk.messagebox.showinfo('提示', '注册成功!')
def start_detect(self):
self.result_text.delete('1.0', tk.END)
self.arp_cache = self.get_arp_cache()
self.detect_thread = Thread(target=self.detect_arp_attack)
self.detect_thread.start()
def stop_detect(self):
self.detect_thread.stop()
self.result_text.delete('1.0', tk.END)
def get_arp_cache(self):
arp_cache = {}
with open(ARP_CACHE_FILE, 'r') as f:
lines = f.readlines()
for line in lines:
ip, mac = line.strip().split(',')
arp_cache[ip] = mac
return arp_cache
def detect_arp_attack(self):
while True:
sniff(prn=self.handle_arp_packet, filter='arp', count=10, timeout=1)
def handle_arp_packet(self, packet):
if packet[ARP].op == ARP_OP_REQUEST:
# 检测请求报文是否合法
if packet[ARP].psrc != packet[ARP].pdst and packet[ARP].hwsrc != packet[ARP].hwdst:
# 检测是否出现不同IP地址对应同一个MAC地址
if packet[ARP].psrc in self.arp_cache and packet[ARP].hwsrc != self.arp_cache[packet[ARP].psrc]:
self.log_arp_attack(packet[ARP].psrc, packet[ARP].hwsrc, self.arp_cache[packet[ARP].psrc])
self.show_arp_attack(packet[ARP].psrc, packet[ARP].hwsrc, self.arp_cache[packet[ARP].psrc])
else:
self.arp_cache[packet[ARP].psrc] = packet[ARP].hwsrc
elif packet[ARP].op == ARP_OP_REPLY:
# 检测应答报文是否出现同一IP地址对应不同的MAC地址
if packet[ARP].psrc in self.arp_cache and packet[ARP].hwsrc != self.arp_cache[packet[ARP].psrc]:
self.log_arp_attack(packet[ARP].psrc, packet[ARP].hwsrc, self.arp_cache[packet[ARP].psrc])
self.show_arp_attack(packet[ARP].psrc, packet[ARP].hwsrc, self.arp_cache[packet[ARP].psrc])
else:
self.arp_cache[packet[ARP].psrc] = packet[ARP].hwsrc
def log_arp_attack(self, ip, mac1, mac2):
attack_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
log = f'ARP攻击:{ip},MAC地址:{mac1}和{mac2},时间:{attack_time}
'
self.arp_attack_log.append(log)
with open(ARP_ATTACK_LOG, 'a') as f:
f.write(log)
def show_arp_attack(self, ip, mac1, mac2):
self.result_text.insert(tk.END, f'ARP攻击:{ip},MAC地址:{mac1}和{mac2}
')
self.result_text.tag_add('red', f'{self.result_text.index(tk.END)}-17c', tk.END)
self.result_text.tag_config('red', foreground='red')
if __name__ == '__main__':
if not os.path.exists(ARP_CACHE_FILE):
os.system('arp -a > ' + ARP_CACHE_FILE)
ARPDetect().root.mainloop()
使用说明
- 确保系统安装了Scapy库,可使用
pip install scapy命令安装。 - 将代码保存为
arp_detect.py文件。 - 运行程序:
python arp_detect.py - 输入用户名和密码登录,或注册新用户。
- 点击“开始检测”按钮启动检测程序,点击“停止检测”按钮停止检测。
注意
- 该软件需要在具有root权限的环境下运行,以便获取网络流量信息。
- 软件仅供学习交流使用,不建议用于商业或其他非授权用途。
- 由于网络环境和攻击方式的多样性,该软件无法完全保证识别所有ARP攻击行为。
未来改进
- 增加对ARP攻击类型更细致的识别。
- 完善用户管理功能,实现用户权限控制。
- 增加攻击防御功能,例如阻止攻击者伪造ARP信息。
- 开发图形用户界面,提升用户体验。
总结
该项目提供了一个基于Linux的ARP攻击检测软件的Python实现方案,并详细解释了软件的功能、代码实现和使用方式。希望能够帮助读者更好地理解ARP攻击的原理和防范方法,并为网络安全学习提供一些参考。
原文地址: https://www.cveoy.top/t/topic/jpaH 著作权归作者所有。请勿转载和采集!