基于Linux的ARP攻击检测软件 - Python实现

本文介绍如何使用Python开发一款基于Linux的ARP攻击检测软件,并提供代码示例。该软件具备以下功能:

  1. 数据包捕获和过滤: 捕获网络数据包并过滤出ARP包,其他包丢弃。
  2. ARP包分析: 分析ARP包,识别正常主机和疑似异常主机,并以红色标注异常主机的IP地址和MAC地址。
  3. 用户登录和注册: 支持用户登录和注册功能,登录成功后跳转至检测界面,注册信息存储在文件中。
  4. 检测按钮: 提供'开始检测'和'停止检测'按钮,开始检测时,将检测到的主机IP和MAC地址列在框中,停止检测则清空内容。
  5. 局域网扫描: 扫描局域网内活动主机,并将扫描到的主机IP地址和MAC地址存入文件。
  6. ARP攻击检测规则:
    • 检测应答报文: 判断同一IP地址是否对应不同的MAC地址,是则可能遭受ARP攻击。
    • 检测请求报文: 判断请求报文是否合法,以及是否出现不同IP地址对应同一个MAC地址,是则可能遭受ARP攻击。
    • 检测大量ARP请求或响应包: 判断大量ARP请求或响应包是否来自同一IP或MAC地址,是则可能遭受ARP攻击。
  7. 结果输出和日志记录: 根据检测规则判断是否遭受ARP攻击,并将所有信息保存在日志中。

Python实现

1. 捕获数据包并过滤ARP包

import socket
import struct

# 创建原始套接字
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0806))

# 过滤ARP包
while True:
    packet = sock.recvfrom(65535)[0]
    eth_header = struct.unpack('!6s6sH', packet[:14])
    if eth_header[2] == 0x0806:
        arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])

2. 分析ARP包,将异常主机IP和MAC地址标为红色

import tkinter as tk

# 创建主窗口和子窗口
root = tk.Tk()
root.title('ARP攻击检测软件')
root.geometry('400x300')

child_window = tk.Toplevel(root)
child_window.title('ARP攻击检测')
child_window.geometry('400x300')

# 分析ARP包,将异常主机IP和MAC地址标为红色
def analyze_arp_packet(packet):
    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
    if arp_header[6] != arp_header[8]:
        # 异常主机IP和MAC地址标为红色
        ip_label = tk.Label(child_window, text=arp_header[6], fg='red')
        mac_label = tk.Label(child_window, text=arp_header[5], fg='red')
    else:
        ip_label = tk.Label(child_window, text=arp_header[6])
        mac_label = tk.Label(child_window, text=arp_header[5])
    ip_label.pack()
    mac_label.pack()

# 循环分析ARP包
while True:
    packet = sock.recvfrom(65535)[0]
    eth_header = struct.unpack('!6s6sH', packet[:14])
    if eth_header[2] == 0x0806:
        analyze_arp_packet(packet)

3. 用户登录和注册功能,登录时调用文件中的内容查询是否有该用户

import tkinter as tk

# 创建主窗口
root = tk.Tk()
root.title('ARP攻击检测软件')
root.geometry('400x300')

# 用户登录和注册功能
def login():
    # 调用文件中的内容查询是否有该用户
    with open('user.txt', 'r') as f:
        user_list = f.readlines()
    for user in user_list:
        username, password = user.strip().split(',')
        if username == username_entry.get() and password == password_entry.get():
            # 登录成功,跳转到子窗口
            child_window.deiconify()
            root.withdraw()
            return
    # 登录失败,弹出提示框
    tk.messagebox.showerror('错误', '用户名或密码错误!')

def register():
    # 注册成功,将账号和密码存放在文件中
    with open('user.txt', 'a') as f:
        f.write(username_entry.get() + ',' + password_entry.get() + '\n')
    # 弹出提示框
    tk.messagebox.showinfo('提示', '注册成功!')

# 创建子窗口
child_window = tk.Toplevel(root)
child_window.title('ARP攻击检测')
child_window.geometry('400x300')
child_window.withdraw()

# 用户名和密码输入框
username_label = tk.Label(root, text='用户名')
username_label.pack()
username_entry = tk.Entry(root)
username_entry.pack()

password_label = tk.Label(root, text='密码')
password_label.pack()
password_entry = tk.Entry(root, show='*')
password_entry.pack()

# 登录和注册按钮
login_button = tk.Button(root, text='登录', command=login)
login_button.pack()

register_button = tk.Button(root, text='注册', command=register)
register_button.pack()

4. 开始检测和停止检测两个按钮,开始检测时将检测到的主机IP和MAC地址列在框中,停止检测则内容清零

import tkinter as tk

# 创建子窗口
child_window = tk.Toplevel(root)
child_window.title('ARP攻击检测')
child_window.geometry('400x300')
child_window.withdraw()

# 开始检测和停止检测两个按钮
def start_detect():
    # 将检测到的主机IP和MAC地址列在框中
    ip_listbox.delete(0, tk.END)
    mac_listbox.delete(0, tk.END)
    with open('active_hosts.txt', 'r') as f:
        host_list = f.readlines()
    for host in host_list:
        ip, mac = host.strip().split(',')
        ip_listbox.insert(tk.END, ip)
        mac_listbox.insert(tk.END, mac)

def stop_detect():
    # 内容清零
    ip_listbox.delete(0, tk.END)
    mac_listbox.delete(0, tk.END)

start_button = tk.Button(child_window, text='开始检测', command=start_detect)
start_button.pack()

stop_button = tk.Button(child_window, text='停止检测', command=stop_detect)
stop_button.pack()

# 主机IP和MAC地址列表框
ip_listbox = tk.Listbox(child_window)
ip_listbox.pack(side=tk.LEFT)

mac_listbox = tk.Listbox(child_window)
mac_listbox.pack(side=tk.RIGHT)

5. 扫描局域网内活动主机,将扫描到的活动主机IP地址和MAC地址存入文件中

import os
import subprocess

# 扫描局域网内活动主机
def scan_active_hosts():
    # 获取本机IP地址
    local_ip = socket.gethostbyname(socket.gethostname())
    # 获取子网掩码
    netmask = subprocess.check_output('ipconfig | findstr /i subnet', shell=True).decode().split(':')[1].strip()
    # 计算网段
    network = '.'.join(local_ip.split('.')[:3]) + '.0/' + netmask
    # 扫描活动主机
    active_hosts = []
    for i in range(1, 255):
        ip = '.'.join(network.split('/')[:-1]) + '.' + str(i)
        if ip != local_ip:
            ret = subprocess.call('ping -n 1 -w 1000 ' + ip, shell=True)
            if ret == 0:
                # 存储活动主机IP地址和MAC地址
                mac = os.popen('arp -a ' + ip + ' | findstr /i ' + ip).read().split()[1]
                active_hosts.append(ip + ',' + mac)
    with open('active_hosts.txt', 'w') as f:
        f.write('\n'.join(active_hosts))

6. 检测应答报文,判断是否出现同一IP地址对应不同的MAC地址

# 检测应答报文,判断是否出现同一IP地址对应不同的MAC地址
def check_arp_reply(packet):
    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
    if arp_header[6] in arp_reply_dict and arp_header[5] != arp_reply_dict[arp_header[6]]:
        # 遭受ARP攻击
        log_text.insert(tk.END, '遭受ARP攻击:' + arp_header[6] + '\n')
    else:
        arp_reply_dict[arp_header[6]] = arp_header[5]

arp_reply_dict = {}

7. 检测请求报文,判断是否合法且是否出现不同IP地址对应同一个MAC地址

# 检测请求报文,判断是否合法且是否出现不同IP地址对应同一个MAC地址
def check_arp_request(packet):
    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
    if arp_header[1] == b'\x00\x01' and arp_header[6] in arp_request_dict and arp_header[5] != arp_request_dict[arp_header[6]]:
        # 遭受ARP攻击
        log_text.insert(tk.END, '遭受ARP攻击:' + arp_header[6] + '\n')
    else:
        arp_request_dict[arp_header[6]] = arp_header[5]

arp_request_dict = {}

8. 检测大量的ARP请求或响应包,判断是否为同一IP或MAC地址发出

# 检测大量的ARP请求或响应包,判断是否为同一IP或MAC地址发出
def check_arp_traffic(packet):
    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
    if arp_header[6] in arp_traffic_dict and arp_header[5] != arp_traffic_dict[arp_header[6]]:
        # 遭受ARP攻击
        log_text.insert(tk.END, '遭受ARP攻击:' + arp_header[6] + '\n')
    else:
        arp_traffic_dict[arp_header[6]] = arp_header[5]

arp_traffic_dict = {}

9. 输出遭受ARP攻击或未遭受ARP攻击,并将所有信息保存在日志中

# 输出遭受ARP攻击或未遭受ARP攻击,并将所有信息保存在日志中
def detect_arp_attack():
    log_text.delete(1.0, tk.END)
    arp_reply_dict.clear()
    arp_request_dict.clear()
    arp_traffic_dict.clear()
    while True:
        packet = sock.recvfrom(65535)[0]
        eth_header = struct.unpack('!6s6sH', packet[:14])
        if eth_header[2] == 0x0806:
            check_arp_reply(packet)
            check_arp_request(packet)
            check_arp_traffic(packet)
        if len(arp_reply_dict) > 100 or len(arp_request_dict) > 100 or len(arp_traffic_dict) > 100:
            # 遭受ARP攻击
            log_text.insert(tk.END, '遭受ARP攻击\n')
        else:
            # 未遭受ARP攻击
            log_text.insert(tk.END, '未遭受ARP攻击\n')
        with open('log.txt', 'a') as f:
            f.write(log_text.get(1.0, tk.END))

# 开始检测和停止检测按钮
start_button = tk.Button(child_window, text='开始检测', command=detect_arp_attack)
start_button.pack()

stop_button = tk.Button(child_window, text='停止检测', command=stop_detect)
stop_button.pack()

# 日志文本框
log_text = tk.Text(child_window)
log_text.pack()

使用说明

  1. 将代码保存为Python文件(例如arp_detect.py)。
  2. 在Linux系统上运行该脚本:python arp_detect.py。
  3. 软件启动后,会弹出用户登录和注册窗口,输入用户名和密码进行登录或注册。
  4. 登录成功后,会跳转至ARP攻击检测窗口。
  5. 点击'开始检测'按钮,软件会开始扫描局域网内活动主机并进行ARP攻击检测。
  6. 检测结果会显示在窗口中,并同时记录在日志文件log.txt中。
  7. 点击'停止检测'按钮,停止检测并清空检测结果。

注意

  • 该软件仅供学习参考,不建议用于生产环境。
  • 请确保运行软件的系统拥有网络数据包捕获权限。
  • 代码中涉及的路径和文件名请根据实际情况修改。
  • 代码中使用的检测规则和阈值可以根据需要进行调整。
  • 该软件仅检测ARP攻击,可能无法识别其他类型的网络攻击。
基于Linux的ARP攻击检测软件-Python实现

原文地址: https://www.cveoy.top/t/topic/jpaL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录