基于Linux的ARP攻击检测软件Python实现
基于Linux的ARP攻击检测软件 (Python实现)
本项目使用Python实现了一个运行于Linux系统下的ARP攻击检测软件,旨在帮助用户实时监测网络中的ARP攻击行为。软件主要功能包括:
- 数据包捕获与过滤: 捕获网络数据包,并从中过滤出ARP协议数据包。
- ARP协议分析: 分析ARP数据包,识别数据包类型 (请求/应答), 提取关键信息 (源IP地址, 源MAC地址, 目标IP地址, 目标MAC地址)。
- 异常主机识别:
- 检测同一IP地址对应不同MAC地址的情况, 识别ARP欺骗攻击。
- 检测不同IP地址对应同一MAC地址的情况, 识别ARP欺骗攻击。
- 分析ARP请求/应答包数量,判断是否存在ARP扫描或攻击行为。
- 用户登录注册: 提供用户登录和注册功能,并将账号密码安全地存储在本地文件中。
- 实时攻击检测: 实时监测网络中的ARP流量,并在检测到攻击行为时发出警报。
- 扫描局域网主机: 扫描局域网内的活动主机,并记录其IP地址和MAC地址。
- 信息记录: 将所有检测到的攻击信息以及系统运行日志记录到本地文件中,方便用户查看和分析。
Python代码示例:
import os
import sys
import time
import logging
from scapy.all import *
# 设置日志
logging.basicConfig(filename='arp_attack.log', level=logging.INFO, format='%(asctime)s - %(message)s')
# 用户登录和注册功能
def login():
# 读取账号密码文件
with open('account.txt', 'r') as f:
account = f.readlines()
# 用户登录
username = input('请输入用户名:')
password = input('请输入密码:')
if username+'
' in account and password+'
' in account:
print('登录成功')
return True
else:
print('用户名或密码错误')
return False
# 开始检测ARP攻击
def start_detect():
# 捕获数据包并过滤出ARP包
def arp_filter(pkt):
return ARP in pkt
# 分析ARP包,判断是否存在异常主机
def arp_analyze(pkt):
# 获取源IP地址和MAC地址
src_ip = pkt[ARP].psrc
src_mac = pkt[ARP].hwsrc
# 判断是否为本机IP地址
if src_ip == get_if_addr(conf.iface):
return
# 判断是否为已知主机,是则更新MAC地址
if src_ip in host_dict:
if host_dict[src_ip] != src_mac:
print(f'ARP攻击:同一IP地址对应不同的MAC地址 - IP地址:{src_ip},MAC地址:{src_mac}')
logging.info(f'ARP攻击:同一IP地址对应不同的MAC地址 - IP地址:{src_ip},MAC地址:{src_mac}')
host_dict[src_ip] = src_mac
# 判断是否为新主机
else:
host_dict[src_ip] = src_mac
print(f'发现新主机 - IP地址:{src_ip},MAC地址:{src_mac}')
logging.info(f'发现新主机 - IP地址:{src_ip},MAC地址:{src_mac}')
# 开始捕获数据包并分析
print('开始检测ARP攻击')
logging.info('开始检测ARP攻击')
global host_dict
host_dict = {}
sniff(filter='arp', prn=arp_analyze, stop_filter=lambda x: False)
# 停止检测ARP攻击
def stop_detect():
print('停止检测ARP攻击')
logging.info('停止检测ARP攻击')
# 扫描局域网内活动主机
def scan_hosts():
# 获取本机IP地址和子网掩码
ip_addr = get_if_addr(conf.iface)
subnet_mask = get_if_raw_hwaddr(conf.iface)[1]
subnet_mask = '.'.join([str(int(subnet_mask[i:i+2], 16)) for i in range(0, len(subnet_mask), 2)])
# 计算网段地址
ip_addr_list = ip_addr.split('.')
subnet_mask_list = subnet_mask.split('.')
net_addr_list = []
for i in range(4):
net_addr_list.append(str(int(ip_addr_list[i]) & int(subnet_mask_list[i])))
net_addr = '.'.join(net_addr_list)
# 扫描所有IP地址
print('开始扫描主机')
logging.info('开始扫描主机')
host_dict = {}
for i in range(1, 255):
dst_ip = net_addr + '.' + str(i)
# 发送ARP请求包
ans, unans = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(op=1, pdst=dst_ip), timeout=2, verbose=False)
# 判断是否有应答包
if ans:
src_mac = ans[0][1].hwsrc
host_dict[dst_ip] = src_mac
print(f'发现活动主机 - IP地址:{dst_ip},MAC地址:{src_mac}')
logging.info(f'发现活动主机 - IP地址:{dst_ip},MAC地址:{src_mac}')
# 将IP地址和MAC地址存入文件中
with open('host.txt', 'w') as f:
for ip, mac in host_dict.items():
f.write(ip + ' ' + mac + '
')
# 检测ARP攻击
def detect_attack():
# 读取主机文件
with open('host.txt', 'r') as f:
host_list = f.readlines()
# 检测ARP攻击
for host in host_list:
ip, mac = host.split()
# 检测应答报文
if ip in host_dict and host_dict[ip] != mac:
print(f'ARP攻击:同一IP地址对应不同的MAC地址 - IP地址:{ip},MAC地址:{mac}')
logging.info(f'ARP攻击:同一IP地址对应不同的MAC地址 - IP地址:{ip},MAC地址:{mac}')
# 检测请求报文
for other_ip, other_mac in host_dict.items():
if ip != other_ip and mac == other_mac:
print(f'ARP攻击:不同IP地址对应同一个MAC地址 - IP地址:{ip},MAC地址:{mac}')
logging.info(f'ARP攻击:不同IP地址对应同一个MAC地址 - IP地址:{ip},MAC地址:{mac}')
# 检测大量的ARP请求或响应包
ip_dict = {}
mac_dict = {}
for host in host_list:
ip, mac = host.split()
if ip in ip_dict:
ip_dict[ip] += 1
else:
ip_dict[ip] = 1
if mac in mac_dict:
mac_dict[mac] += 1
else:
mac_dict[mac] = 1
max_ip_count = max(ip_dict.values())
max_mac_count = max(mac_dict.values())
if max_ip_count > len(host_list) / 2 or max_mac_count > len(host_list) / 2:
print('ARP攻击:大量的ARP请求或响应包')
logging.info('ARP攻击:大量的ARP请求或响应包')
else:
print('未遭受ARP攻击')
logging.info('未遭受ARP攻击')
# 主函数
if __name__ == '__main__':
# 用户登录
if not login():
sys.exit()
# 创建窗口
window = Tk()
window.title('ARP攻击检测软件')
window.geometry('400x300')
# 创建按钮
start_button = Button(window, text='开始检测', command=start_detect)
start_button.pack(pady=10)
stop_button = Button(window, text='停止检测', command=stop_detect)
stop_button.pack(pady=10)
scan_button = Button(window, text='扫描主机', command=scan_hosts)
scan_button.pack(pady=10)
detect_button = Button(window, text='检测ARP攻击', command=detect_attack)
detect_button.pack(pady=10)
# 显示窗口
window.mainloop()
注意: 以上代码仅供参考, 实际运行还需要根据环境进行调整。
原文地址: https://www.cveoy.top/t/topic/jpbm 著作权归作者所有。请勿转载和采集!