基于Linux的Python ARP攻击检测软件
基于Linux的Python ARP攻击检测软件
这是一款运行于Linux系统,使用Python语言编写的ARP攻击检测软件。它可以帮助你监控网络中的ARP数据包,并识别潜在的ARP攻击行为,保障网络安全。
软件功能
-
用户登录和注册: 用户需要登录才能使用软件的检测功能。软件提供注册功能,并将账号和密码信息安全地存储在本地文件中。
-
实时ARP数据包捕获: 软件使用Scapy库捕获网络中的ARP数据包,并进行实时分析。
-
可疑主机识别: 软件根据预设的规则,例如同一IP地址对应多个MAC地址、不同IP地址对应相同MAC地址等,识别网络中可能遭受ARP攻击的主机。
-
IP和MAC地址标记: 软件将疑似遭受攻击的主机的IP地址和MAC地址在界面中以红色字体显示,方便用户识别。
-
攻击判定: 软件根据多种规则判定网络中是否存在ARP攻击行为,并将结果输出到界面和日志文件中。
-
日志记录: 软件将所有检测到的ARP数据包信息和攻击判定结果记录到日志文件中,方便用户进行事后分析。
代码实现
以下是用Python实现上述功能的代码:
import tkinter as tk
import os
import time
from scapy.all import *
# 定义全局变量
root = tk.Tk()
root.title('ARP Attack Detection')
root.geometry('600x400')
user_file = 'user.txt'
log_file = 'log.txt'
running = False
# 用户登录和注册功能
def login():
user = user_entry.get()
password = password_entry.get()
if os.path.exists(user_file):
with open(user_file, 'r') as f:
for line in f:
line = line.strip()
if line == user + ':' + password:
tk.messagebox.showinfo('Login Success', 'Welcome, ' + user + '!')
return
tk.messagebox.showerror('Login Failed', 'Invalid username or password!')
else:
with open(user_file, 'w') as f:
f.write(user + ':' + password + '
')
tk.messagebox.showinfo('Register Success', 'Welcome, ' + user + '!')
# 开始检测ARP攻击
def start():
global running
running = True
result_text.delete('1.0', tk.END)
sniff(filter='arp', prn=analyze_arp_packet)
# 停止检测ARP攻击
def stop():
global running
running = False
# 分析ARP包
def analyze_arp_packet(packet):
if not running:
return
if packet[ARP].op == 1: # ARP请求报文
if packet[ARP].hwsrc != '00:00:00:00:00:00' and packet[ARP].psrc != packet[ARP].pdst:
if check_duplicate_mac(packet[ARP].hwsrc):
log('ARP Attack Detected: Duplicate MAC Address (' + packet[ARP].hwsrc + ')')
mark_suspicious_host(packet[ARP].psrc)
elif packet[ARP].op == 2: # ARP应答报文
if check_duplicate_ip(packet[ARP].psrc, packet[ARP].hwsrc):
log('ARP Attack Detected: Duplicate IP Address (' + packet[ARP].psrc + ')')
mark_suspicious_host(packet[ARP].psrc)
# 可以根据需要添加对RARP报文的处理
# 判断是否出现同一IP地址对应不同的MAC地址
def check_duplicate_mac(mac):
for packet in sniffed_packets:
if packet[ARP].psrc == packet[ARP].pdst and packet[ARP].hwsrc != mac:
return True
return False
# 判断是否合法且是否出现不同IP地址对应同一个MAC地址
def check_duplicate_ip(ip, mac):
for packet in sniffed_packets:
if packet[ARP].psrc == ip and packet[ARP].hwsrc != mac:
return True
return False
# 标记疑似异常主机的IP地址和MAC地址为红色
def mark_suspicious_host(ip):
for host in host_list:
if host[0] == ip:
host[1].config(fg='red')
# 输出检测结果并保存到日志
def log(text):
result_text.insert(tk.END, text + '
')
with open(log_file, 'a') as f:
f.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' ' + text + '
')
# 创建登录和注册界面
login_frame = tk.Frame(root)
login_frame.pack(pady=20)
tk.Label(login_frame, text='Username:').grid(row=0, column=0)
user_entry = tk.Entry(login_frame)
user_entry.grid(row=0, column=1)
tk.Label(login_frame, text='Password:').grid(row=1, column=0)
password_entry = tk.Entry(login_frame, show='*')
password_entry.grid(row=1, column=1)
tk.Button(login_frame, text='Login', command=login).grid(row=2, column=0, pady=10)
tk.Button(login_frame, text='Register', command=login).grid(row=2, column=1, pady=10)
# 创建检测界面
detect_frame = tk.Frame(root)
detect_frame.pack(pady=20)
tk.Button(detect_frame, text='Start Detection', command=start).grid(row=0, column=0, pady=10)
tk.Button(detect_frame, text='Stop Detection', command=stop).grid(row=0, column=1, pady=10)
result_text = tk.Text(detect_frame, height=10, width=50)
result_text.grid(row=1, column=0, columnspan=2)
host_list = []
sniffed_packets = []
# 启动主循环
root.mainloop()
注意事项
- 使用该软件需要在Linux系统下安装Python3和Scapy库。
- 该软件仅供学习和研究使用,请勿用于非法用途。
原文地址: https://www.cveoy.top/t/topic/jo7O 著作权归作者所有。请勿转载和采集!