基于Linux的ARP攻击检测与防护软件

本软件基于Python实现,能够检测局域网内的ARP攻击,并根据攻击类型输出信息。同时,软件还具备扫描局域网内活动主机、记录IP地址和MAC地址、进行一定程度的防御以及可视化界面等功能,并将结果保存在日志文件中。

软件功能模块

  • ARP攻击检测模块: 通过监听网络流量,检测是否存在ARP欺骗攻击,并且根据攻击类型输出相关信息。
  • IP和MAC地址扫描模块: 通过发送ARP请求,扫描局域网内的活动主机,并记录其IP地址和MAC地址。
  • 防御模块: 根据检测到的ARP攻击类型,采取相应的防御措施,例如发送警告信息、禁止ARP缓存更新等。
  • 可视化界面模块: 通过多个中文按钮实现可视化界面,方便用户操作和查看结果。
  • 日志记录模块: 将程序运行过程中的信息保存在日志文件中,方便用户查看和分析。

基本代码实现

import os
import sys
import time
import socket
import struct
import select
import threading
import tkinter as tk

# 定义常量
ARP_REQUEST = 1
ARP_REPLY = 2
ARP_PROBE = 3
ARP_OP = {ARP_REQUEST: 'ARP Request', ARP_REPLY: 'ARP Reply', ARP_PROBE: 'ARP Probe'}
ARP_CACHE = {}  # ARP缓存
ARP_LOCK = threading.Lock()  # ARP锁
IP_MAC_MAP = {}  # IP地址和MAC地址的映射
SCAN_IP_RANGE = '192.168.1.1/24'  # 扫描的IP地址范围
SCAN_TIMEOUT = 1  # 扫描超时时间
SCAN_INTERVAL = 0.1  # 扫描间隔时间
SCAN_LOCK = threading.Lock()  # 扫描锁
LOG_FILE = 'arp.log'  # 日志文件名

# 定义GUI界面
class ARP_GUI:
    def __init__(self, root):
        self.root = root
        self.root.title('ARP攻击检测和防护软件')
        self.root.geometry('600x400')
        self.root.resizable(False, False)

        self.label1 = tk.Label(self.root, text='ARP攻击检测和防护软件', font=('Arial', 20))
        self.label1.pack(pady=20)

        self.button1 = tk.Button(self.root, text='开始检测', font=('Arial', 16), command=self.start_detect)
        self.button1.pack(pady=20)

        self.button2 = tk.Button(self.root, text='停止检测', font=('Arial', 16), command=self.stop_detect)
        self.button2.pack(pady=20)

        self.text1 = tk.Text(self.root, font=('Arial', 12))
        self.text1.pack(pady=20)

        self.label2 = tk.Label(self.root, text='扫描局域网内的活动主机', font=('Arial', 16))
        self.label2.pack(pady=20)

        self.button3 = tk.Button(self.root, text='开始扫描', font=('Arial', 16), command=self.start_scan)
        self.button3.pack(pady=20)

        self.button4 = tk.Button(self.root, text='停止扫描', font=('Arial', 16), command=self.stop_scan)
        self.button4.pack(pady=20)

        self.text2 = tk.Text(self.root, font=('Arial', 12))
        self.text2.pack(pady=20)

        self.label3 = tk.Label(self.root, text='日志记录', font=('Arial', 16))
        self.label3.pack(pady=20)

        self.button5 = tk.Button(self.root, text='清空日志', font=('Arial', 16), command=self.clear_log)
        self.button5.pack(pady=20)

        self.text3 = tk.Text(self.root, font=('Arial', 12))
        self.text3.pack(pady=20)

        self.label4 = tk.Label(self.root, text='作者:XXX', font=('Arial', 12))
        self.label4.pack(pady=20)

        self.label5 = tk.Label(self.root, text='版权所有:XXX', font=('Arial', 12))
        self.label5.pack(pady=20)

    def start_detect(self):
        self.button1.config(state='disabled')
        self.button2.config(state='normal')
        self.text1.delete('1.0', tk.END)
        self.text1.insert('1.0', '开始检测ARP攻击...
')
        threading.Thread(target=self.detect_arp_attack).start()

    def stop_detect(self):
        self.button1.config(state='normal')
        self.button2.config(state='disabled')
        self.text1.insert(tk.END, '停止检测ARP攻击
')

    def start_scan(self):
        self.button3.config(state='disabled')
        self.button4.config(state='normal')
        self.text2.delete('1.0', tk.END)
        self.text2.insert('1.0', '开始扫描局域网内的活动主机...
')
        threading.Thread(target=self.scan_ip_mac).start()

    def stop_scan(self):
        self.button3.config(state='normal')
        self.button4.config(state='disabled')
        self.text2.insert(tk.END, '停止扫描局域网内的活动主机
')

    def clear_log(self):
        self.text3.delete('1.0', tk.END)
        with open(LOG_FILE, 'w') as f:
            f.write('')

    def detect_arp_attack(self):
        # 监听网络流量
        try:
            s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
        except:
            self.text1.insert(tk.END, '无法监听网络流量
')
            return

        while True:
            try:
                packet = s.recvfrom(2048)[0]
                eth_header = struct.unpack('!6s6sH', packet[:14])
                if eth_header[2] == 0x0806:  # ARP协议
                    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
                    arp_src_ip = socket.inet_ntoa(arp_header[6])
                    arp_src_mac = ':'.join(['{:02x}'.format(x) for x in arp_header[5]])
                    arp_dst_ip = socket.inet_ntoa(arp_header[8])
                    arp_dst_mac = ':'.join(['{:02x}'.format(x) for x in arp_header[7]])
                    arp_op = struct.unpack('!H', arp_header[4])[0]
                    arp_type = None
                    if arp_src_ip != arp_dst_ip:
                        # 检测是否为ARP欺骗攻击
                        if arp_dst_ip in ARP_CACHE and ARP_CACHE[arp_dst_ip] != arp_dst_mac:
                            arp_type = 'ARP欺骗攻击'
                        elif arp_src_ip in IP_MAC_MAP and IP_MAC_MAP[arp_src_ip] != arp_src_mac:
                            arp_type = 'ARP欺骗攻击'
                        if arp_type:
                            # 发送警告信息
                            self.text1.insert(tk.END, '检测到{},源IP地址:{},源MAC地址:{},目标IP地址:{},目标MAC地址:{}
'.format(arp_type, arp_src_ip, arp_src_mac, arp_dst_ip, arp_dst_mac))
                            with ARP_LOCK:
                                ARP_CACHE[arp_dst_ip] = arp_dst_mac
                                ARP_CACHE[arp_src_ip] = arp_src_mac
                                self.send_arp_request(arp_dst_ip, arp_src_ip)
                                time.sleep(0.1)
                                self.send_arp_request(arp_src_ip, arp_dst_ip)
                    # 更新ARP缓存
                    with ARP_LOCK:
                        ARP_CACHE[arp_src_ip] = arp_src_mac
                        ARP_CACHE[arp_dst_ip] = arp_dst_mac
            except:
                pass

    def send_arp_request(self, src_ip, dst_ip):
        # 发送ARP请求
        with socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0806)) as s:
            s.bind(('eth0', 0))
            src_mac = s.getsockname()[4]
            dst_mac = b'ÿÿÿÿÿÿ'
            arp_header = struct.pack('!HHBBH6s4s6s4s', 0x0001, 0x0800, 6, 4, 0x0001, src_mac, socket.inet_aton(src_ip), dst_mac, socket.inet_aton(dst_ip))
            eth_header = struct.pack('!6s6sH', dst_mac, src_mac, 0x0806)
            s.send(eth_header + arp_header)

    def scan_ip_mac(self):
        # 扫描局域网内的活动主机
        with SCAN_LOCK:
            for i in range(1, 255):
                ip = SCAN_IP_RANGE.replace('1', str(i))
                threading.Thread(target=self.send_arp_probe, args=(ip,)).start()
                time.sleep(SCAN_INTERVAL)
        self.text2.insert(tk.END, '扫描完成
')

    def send_arp_probe(self, ip):
        # 发送ARP探测
        with socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0806)) as s:
            s.bind(('eth0', 0))
            src_mac = s.getsockname()[4]
            dst_mac = b'ÿÿÿÿÿÿ'
            arp_header = struct.pack('!HHBBH6s4s6s4s', 0x0001, 0x0800, 6, 4, 0x0003, src_mac, socket.inet_aton('0.0.0.0'), dst_mac, socket.inet_aton(ip))
            eth_header = struct.pack('!6s6sH', dst_mac, src_mac, 0x0806)
            s.send(eth_header + arp_header)
            try:
                s.settimeout(SCAN_TIMEOUT)
                packet = s.recvfrom(2048)[0]
                eth_header = struct.unpack('!6s6sH', packet[:14])
                if eth_header[2] == 0x0806:  # ARP协议
                    arp_header = struct.unpack('2s2s1s1s2s6s4s6s4s', packet[14:42])
                    arp_src_ip = socket.inet_ntoa(arp_header[6])
                    arp_src_mac = ':'.join(['{:02x}'.format(x) for x in arp_header[5]])
                    with SCAN_LOCK:
                        IP_MAC_MAP[arp_src_ip] = arp_src_mac
                        self.text2.insert(tk.END, 'IP地址:{},MAC地址:{}
'.format(arp_src_ip, arp_src_mac))
            except:
                pass

    def write_log(self, msg):
        # 记录日志
        with open(LOG_FILE, 'a') as f:
            f.write('[{}] {}
'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), msg))

    def run(self):
        self.root.mainloop()

# 主函数
if __name__ == '__main__':
    gui = ARP_GUI(tk.Tk())
    gui.run()

注意:

  • 上述代码仅为基本实现,需要根据实际情况进行调整。
  • 代码中的IP地址范围、扫描超时时间、扫描间隔时间等参数需要根据网络环境进行调整。
  • 为了方便理解,代码中已省略了一些错误处理和安全性检查。在实际应用中,需要进行完善。
  • 代码中的作者和版权信息需要根据实际情况进行修改。

使用方法:

  1. 将代码保存为Python文件(例如:arp.py)。
  2. 在Linux系统中运行该文件:python arp.py
  3. 运行后会弹出一个窗口,点击“开始检测”按钮开始检测ARP攻击。
  4. 点击“开始扫描”按钮开始扫描局域网内的活动主机。
  5. 点击“清空日志”按钮清空日志文件。

相关知识:

  • ARP攻击:ARP攻击是一种网络攻击,攻击者通过伪造ARP协议包来欺骗网络设备,使网络设备将流量转发到攻击者的机器上。
  • ARP协议:ARP协议(地址解析协议)用于将IP地址映射到MAC地址,是网络通信中不可或缺的一部分。
  • Python:Python是一种高级编程语言,拥有丰富的库和工具,可以用于开发各种应用程序,包括网络安全工具。

版权说明:

本软件仅供学习和研究之用,请勿用于任何非法活动。作者不对任何因使用本软件而造成的损失承担责任。

基于Linux的ARP攻击检测与防护软件

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

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