基于Linux的ARP攻击检测防护软件设计
基于Linux的ARP攻击检测防护软件设计
本软件旨在为Linux用户提供一种简单的ARP攻击检测和防护方案,可以识别常见的ARP攻击类型,并记录攻击源的信息。
功能特点:
- 检测ARP欺骗、ICMP Flood和TCP攻击
- 扫描局域网内活动主机的IP地址和MAC地址
- 记录攻击源的IP地址和MAC地址
- 通过可视化界面展示检测结果
- 将所有记录保存在本地文件
软件架构:
软件主要由以下模块组成:
- 数据包监听模块: 使用Python的socket库监听网络数据包,并解析ARP、ICMP和TCP数据包。
- 攻击检测模块: 基于解析后的数据包,检测ARP欺骗、ICMP Flood和TCP攻击。
- 记录模块: 记录局域网内活动主机的IP地址和MAC地址,以及攻击源的IP地址和MAC地址。
- 可视化界面模块: 使用Tkinter库创建可视化界面,方便用户查看检测结果。
代码示例:
import os
import sys
import time
import socket
import struct
import fcntl
import tkinter as tk
from tkinter import messagebox
# 定义常量
ETH_P_ALL = 0x0003
ETH_LEN = 14
ARP_LEN = 28
ARP_REQUEST = 0x0001
ARP_REPLY = 0x0002
ICMP_ECHO_REQUEST = 0x08
TCP_SYN = 0x02
# 定义全局变量
ip_mac_dict = {} # 存储局域网内活动主机的IP地址和MAC地址
attack_source_dict = {} # 存储攻击源的IP地址和MAC地址
# 获取本机MAC地址
def get_mac_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname[:15], 'utf-8')))
return ':'.join(['%02x' % b for b in info[18:24]])
# 发送ARP请求
def send_arp_request(ifname, src_ip, src_mac, dst_ip):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind((ifname, ETH_P_ALL))
dst_mac = 'ff:ff:ff:ff:ff:ff' # 广播地址
arp_packet = struct.pack('!6s6s2s2s1s1s2s6s4s6s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 06'), bytes.fromhex('00 01'), bytes.fromhex('08'), bytes.fromhex('00'), bytes.fromhex('06 04'), bytes.fromhex('00 01'), socket.inet_aton(src_ip), bytes.fromhex('00 00 00 00 00 00'), socket.inet_aton(dst_ip))
s.send(arp_packet)
# 发送ARP响应
def send_arp_reply(ifname, src_ip, src_mac, dst_ip, dst_mac):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind((ifname, ETH_P_ALL))
arp_packet = struct.pack('!6s6s2s2s1s1s2s6s4s6s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 06'), bytes.fromhex('00 02'), bytes.fromhex('08'), bytes.fromhex('00'), bytes.fromhex('06 04'), bytes.fromhex('00 02'), socket.inet_aton(src_ip), bytes.fromhex(dst_mac.replace(':', '')), socket.inet_aton(dst_ip))
s.send(arp_packet)
# 发送ICMP请求
def send_icmp_request(ifname, src_ip, src_mac, dst_ip):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind((ifname, ETH_P_ALL))
dst_mac = ip_mac_dict[dst_ip] # 获取目标主机的MAC地址
icmp_packet = struct.pack('!6s6s2s1s1s2s4s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 00'), bytes.fromhex('00'), bytes.fromhex('00'), bytes.fromhex('00 00'), socket.inet_aton(src_ip), socket.inet_aton(dst_ip))
s.send(icmp_packet)
# 发送TCP SYN
def send_tcp_syn(ifname, src_ip, src_mac, dst_ip, dst_port):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind((ifname, ETH_P_ALL))
dst_mac = ip_mac_dict[dst_ip] # 获取目标主机的MAC地址
tcp_packet = struct.pack('!6s6s2s2s2s1s1s2s4s4s2s2s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 00'), bytes.fromhex('45'), bytes.fromhex('00 28'), bytes.fromhex('00 00'), bytes.fromhex('40 00'), bytes.fromhex('40 06'), bytes.fromhex('00 00 00 00'), socket.inet_aton(src_ip), socket.inet_aton(dst_ip), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('50 02'), bytes.fromhex('00 00'), bytes.fromhex('00 00'))
tcp_packet += struct.pack('!H', dst_port)
tcp_packet += struct.pack('!H', 0x1234)
tcp_packet += struct.pack('!I', 0)
tcp_packet += struct.pack('!I', 0)
tcp_packet += bytes.fromhex('50 02 20 00')
s.send(tcp_packet)
# 解析ARP包
def parse_arp_packet(packet):
src_mac = ':'.join(['%02x' % b for b in packet[6:12]])
src_ip = socket.inet_ntoa(packet[28:32])
dst_ip = socket.inet_ntoa(packet[38:42])
arp_op = struct.unpack('!H', packet[20:22])[0]
if arp_op == ARP_REQUEST:
send_arp_reply(ifname, dst_ip, get_mac_address(ifname), src_ip, src_mac)
elif arp_op == ARP_REPLY:
ip_mac_dict[src_ip] = src_mac
# 解析ICMP包
def parse_icmp_packet(packet):
src_mac = ':'.join(['%02x' % b for b in packet[6:12]])
src_ip = socket.inet_ntoa(packet[26:30])
dst_ip = socket.inet_ntoa(packet[30:34])
icmp_type = struct.unpack('!B', packet[34:35])[0]
icmp_code = struct.unpack('!B', packet[35:36])[0]
if icmp_type == ICMP_ECHO_REQUEST:
send_icmp_reply(ifname, dst_ip, get_mac_address(ifname), src_ip)
# 解析TCP包
def parse_tcp_packet(packet):
src_mac = ':'.join(['%02x' % b for b in packet[6:12]])
src_ip = socket.inet_ntoa(packet[26:30])
dst_ip = socket.inet_ntoa(packet[30:34])
tcp_flags = struct.unpack('!B', packet[47:48])[0]
if tcp_flags == TCP_SYN:
attack_source_dict[src_ip] = src_mac
# 监听网络数据包
def sniff_packets(ifname):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind((ifname, ETH_P_ALL))
while True:
packet = s.recvfrom(2048)[0]
if len(packet) < ETH_LEN:
continue
eth_type = struct.unpack('!H', packet[12:14])[0]
if eth_type == 0x0806:
parse_arp_packet(packet)
elif eth_type == 0x0800:
ip_proto = struct.unpack('!B', packet[23:24])[0]
if ip_proto == 0x01:
parse_icmp_packet(packet)
elif ip_proto == 0x06:
parse_tcp_packet(packet)
# 检测ICMP Flood攻击
def detect_icmp_flood():
if len(ip_mac_dict) == 0:
return '并没有遭到ICMP Flood攻击'
for ip in ip_mac_dict.keys():
count = 0
for src_ip in attack_source_dict.keys():
if src_ip == ip:
count += 1
if count > 100:
return '遭到ICMP Flood攻击'
return '并没有遭到ICMP Flood攻击'
# 检测TCP攻击
def detect_tcp_attack():
if len(ip_mac_dict) == 0:
return '并没有遭到TCP攻击'
for ip in ip_mac_dict.keys():
count = 0
for src_ip in attack_source_dict.keys():
if src_ip == ip:
count += 1
if count > 10:
return '遭到TCP攻击'
return '并没有遭到TCP攻击'
# 检测ARP欺骗攻击
def detect_arp_spoofing():
if len(ip_mac_dict) == 0:
return '并没有遭到ARP欺骗攻击'
for ip, mac in ip_mac_dict.items():
if mac != get_mac_address(ifname):
return '遭到ARP欺骗攻击'
return '并没有遭到ARP欺骗攻击'
# 扫描局域网内活动主机的IP地址和MAC地址
def scan_network(ifname):
for i in range(1, 255):
dst_ip = '192.168.1.' + str(i)
send_arp_request(ifname, '192.168.1.1', get_mac_address(ifname), dst_ip)
time.sleep(0.1)
# 保存记录到本地文件
def save_records():
with open('records.txt', 'w') as f:
f.write('IP地址 MAC地址
')
for ip, mac in ip_mac_dict.items():
f.write(ip + ' ' + mac + '
')
f.write('
攻击源IP地址 MAC地址
')
for ip, mac in attack_source_dict.items():
f.write(ip + ' ' + mac + '
')
f.write('
ICMP Flood攻击:' + detect_icmp_flood() + '
')
f.write('TCP攻击:' + detect_tcp_attack() + '
')
f.write('ARP欺骗攻击:' + detect_arp_spoofing() + '
')
# 创建可视化界面
def create_gui():
root = tk.Tk()
root.title('ARP攻击检测防护软件')
root.geometry('300x200')
# 扫描按钮
def scan_button_click():
scan_network(ifname)
messagebox.showinfo('提示', '扫描完成')
scan_button = tk.Button(root, text='扫描', command=scan_button_click)
scan_button.pack(pady=10)
# 检测按钮
def detect_button_click():
result = 'IP地址 MAC地址
'
for ip, mac in ip_mac_dict.items():
result += ip + ' ' + mac + '
'
result += '
攻击源IP地址 MAC地址
'
for ip, mac in attack_source_dict.items():
result += ip + ' ' + mac + '
'
result += '
ICMP Flood攻击:' + detect_icmp_flood() + '
'
result += 'TCP攻击:' + detect_tcp_attack() + '
'
result += 'ARP欺骗攻击:' + detect_arp_spoofing() + '
'
messagebox.showinfo('检测结果', result)
detect_button = tk.Button(root, text='检测', command=detect_button_click)
detect_button.pack(pady=10)
# 保存按钮
def save_button_click():
save_records()
messagebox.showinfo('提示', '记录已保存到本地文件')
save_button = tk.Button(root, text='保存', command=save_button_click)
save_button.pack(pady=10)
root.mainloop()
# 主函数
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: python arp_attack_detection.py <interface>')
sys.exit(1)
ifname = sys.argv[1]
create_gui()
使用方法:
- 将代码保存为
arp_attack_detection.py文件。 - 在命令行中执行该程序,并传入网卡接口名称作为参数。例如:
python arp_attack_detection.py eth0
- 点击“扫描”按钮,扫描局域网内活动主机的IP地址和MAC地址。
- 点击“检测”按钮,检测ARP攻击类型,并显示检测结果。
- 点击“保存”按钮,将检测结果保存到本地文件
records.txt。
注意:
- 该软件仅供学习和研究使用,不保证其安全性和稳定性。
- 使用该软件需要一定的网络知识,建议在使用前仔细阅读代码和文档。
- 为了更好地理解和使用该软件,建议参考相关网络安全知识和Python编程教程。
未来改进方向:
- 增加更多攻击检测类型,例如DNS欺骗攻击、SYN Flood攻击等。
- 增加攻击防御功能,例如阻止攻击源发送数据包。
- 优化代码性能,提高检测效率。
- 开发更友好的用户界面,方便用户操作。
希望本软件能够帮助您更好地了解ARP攻击,并提升您的网络安全意识。
原文地址: https://www.cveoy.top/t/topic/jm3G 著作权归作者所有。请勿转载和采集!