基于Linux的ARP攻击检测防护软件的设计独立的模块包括icmp flood、tcp攻击和ARP欺骗攻击检测若有则输出该攻击的类型若没有则输出并没有遭到该类攻击扫描和记录局域网内活动主机的IP地址和MAC地址能够记录攻击源的IP地址和MAC地址通过中文按钮分别实现可视化界面将所以记录保存在本地文件上。将模块结合起来通过python代码详细实现
该软件的设计和功能。
- ICMP Flood攻击检测模块
ICMP Flood攻击是一种常见的DoS攻击方式,通过发送大量的ICMP数据包来占用目标主机的网络带宽,导致目标主机无法正常工作。本模块通过监听网络流量,统计ICMP数据包的数量和频率,如果超过一定阈值,则判断为ICMP Flood攻击。
- TCP攻击检测模块
TCP攻击是一种针对TCP协议的DoS攻击方式,通过发送大量的TCP连接请求来占用目标主机的资源,导致目标主机无法正常工作。本模块通过监听网络流量,统计TCP连接请求的数量和频率,如果超过一定阈值,则判断为TCP攻击。
- ARP欺骗攻击检测模块
ARP欺骗攻击是一种常见的网络攻击方式,攻击者通过伪造ARP协议的欺骗信息,将目标主机的IP地址和MAC地址映射到攻击者的MAC地址上,从而实现网络欺骗和数据窃取。本模块通过监听网络流量,检测是否存在ARP欺骗攻击的欺骗信息,如果存在,则判断为ARP欺骗攻击。
- 主机扫描和记录模块
本模块通过扫描局域网内的活动主机,获取其IP地址和MAC地址,并将其记录在本地文件中,以便后续的攻击源追踪和记录。
- 攻击源追踪和记录模块
本模块通过记录攻击源的IP地址和MAC地址,可以快速追踪和记录攻击者的信息,并通过本地文件保存,以便后续的分析和处理。
- 可视化界面模块
本模块通过中文按钮实现可视化界面,方便用户使用和操作。
Python代码实现
以下是基于Python的Linux ARP攻击检测防护软件的代码实现:
import os
import sys
import time
import socket
import struct
import fcntl
import threading
import tkinter as tk
from tkinter import messagebox
# 定义全局变量
icmp_count = 0
tcp_count = 0
arp_count = 0
ip_mac_dict = {}
attack_dict = {}
# 获取本机IP地址和MAC地址
def get_ip_mac(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname[:15], 'utf-8')))
ip = socket.inet_ntoa(info[20:24])
mac = ':'.join('%02x' % b for b in info[18:24])
return ip, mac
# 监听网络流量
def sniff_packet(ifname):
global icmp_count, tcp_count, arp_count, ip_mac_dict, attack_dict
# 创建原始套接字
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
# 绑定到指定网络接口
sock.bind((ifname, 0))
# 循环监听网络流量
while True:
try:
packet = sock.recvfrom(2048)
# 解析数据包
eth_header = packet[0][:14]
eth_dst_mac = ':'.join('%02x' % b for b in eth_header[0:6])
eth_src_mac = ':'.join('%02x' % b for b in eth_header[6:12])
eth_type = struct.unpack('!H', eth_header[12:14])[0]
# 处理IP数据包
if eth_type == 0x0800:
ip_header = packet[0][14:34]
ip_src_addr = socket.inet_ntoa(ip_header[12:16])
ip_dst_addr = socket.inet_ntoa(ip_header[16:20])
# 处理ICMP数据包
if ip_header[9] == 1:
icmp_count += 1
if icmp_count > 100:
attack_dict[ip_src_addr] = 'ICMP Flood'
icmp_count = 0
messagebox.showwarning('警告', '检测到ICMP Flood攻击,攻击源IP地址为:' + ip_src_addr)
# 处理TCP数据包
elif ip_header[9] == 6:
tcp_count += 1
if tcp_count > 100:
attack_dict[ip_src_addr] = 'TCP Flood'
tcp_count = 0
messagebox.showwarning('警告', '检测到TCP Flood攻击,攻击源IP地址为:' + ip_src_addr)
# 处理ARP数据包
elif eth_type == 0x0806:
arp_count += 1
if arp_count > 100:
arp_header = packet[0][14:42]
arp_src_ip = socket.inet_ntoa(arp_header[14:18])
arp_src_mac = ':'.join('%02x' % b for b in arp_header[8:14])
arp_dst_ip = socket.inet_ntoa(arp_header[24:28])
arp_dst_mac = ':'.join('%02x' % b for b in arp_header[18:24])
if arp_dst_ip == '0.0.0.0':
if arp_src_ip in ip_mac_dict and ip_mac_dict[arp_src_ip] != arp_src_mac:
attack_dict[arp_src_ip] = 'ARP Spoofing'
messagebox.showwarning('警告', '检测到ARP欺骗攻击,攻击源IP地址为:' + arp_src_ip)
else:
ip_mac_dict[arp_src_ip] = arp_src_mac
arp_count = 0
except:
pass
# 扫描和记录局域网内的主机
def scan_hosts(ifname):
global ip_mac_dict
# 获取本机IP地址和MAC地址
ip, mac = get_ip_mac(ifname)
# 获取本机网段
netmask = socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', bytes(ifname[:15], 'utf-8')))[20:24])
netaddr = '.'.join(str(int(ipaddr) & int(netmask.split('.')[i])) for i, ipaddr in enumerate(ip.split('.')))
# 扫描主机
for i in range(1, 255):
host = netaddr + '.' + str(i)
if host != ip:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
s.connect((host, 135))
s.send(b'\x00')
data = s.recv(1024)
mac = ':'.join('%02x' % b for b in data[94:100])
ip_mac_dict[host] = mac
s.close()
except:
pass
# 保存主机列表到本地文件
with open('hosts.txt', 'w') as f:
for ip, mac in ip_mac_dict.items():
f.write(ip + ' ' + mac + '\n')
# 初始化界面
def init_gui():
global root
root = tk.Tk()
root.title('ARP攻击检测防护软件')
root.geometry('400x300')
# 添加按钮
btn_scan = tk.Button(root, text='扫描主机', command=lambda: threading.Thread(target=scan_hosts, args=(ifname,)).start())
btn_scan.place(relx=0.3, rely=0.3, relwidth=0.4, relheight=0.1)
btn_start = tk.Button(root, text='开始监控', command=lambda: threading.Thread(target=sniff_packet, args=(ifname,)).start())
btn_start.place(relx=0.3, rely=0.5, relwidth=0.4, relheight=0.1)
btn_exit = tk.Button(root, text='退出程序', command=sys.exit)
btn_exit.place(relx=0.3, rely=0.7, relwidth=0.4, relheight=0.1)
root.mainloop()
# 主函数
if __name__ == '__main__':
# 获取网络接口名称
ifname = os.popen('ip route | grep default | awk \'{print $5}\'').read().strip()
if not ifname:
messagebox.showerror('错误', '无法获取网络接口名称')
sys.exit()
# 初始化界面
init_gui()
``
原文地址: http://www.cveoy.top/t/topic/fokX 著作权归作者所有。请勿转载和采集!