以下是一个基于Python的ARP攻击检测防护软件的简单实现,包括MAC flood、DHCP flood、CC攻击和ARP欺骗攻击检测,并根据攻击类型输出信息,扫描和记录局域网内活动主机的IP地址和MAC地址,记录和保存攻击源的IP地址和MAC地址,具有一定的防御功能,通过中文按钮实现可视化界面,并将结果保存在日志中。

import os
import time
import threading
from scapy.all import *
from tkinter import *

# 设置全局变量
ip_list = []
mac_list = []
attack_list = []

# 定义函数:扫描局域网内活动主机的IP地址和MAC地址
def scan_hosts():
    global ip_list, mac_list
    ip_list = []
    mac_list = []
    for i in range(1, 255):
        ip = '192.168.1.' + str(i)
        ans, unans = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip), timeout=2, verbose=False)
        if ans:
            for snd, rcv in ans:
                mac_list.append(rcv[Ether].src)
                ip_list.append(rcv[ARP].psrc)

# 定义函数:检测MAC flood攻击
def detect_mac_flood():
    global mac_list, attack_list
    mac_count = {}
    for mac in mac_list:
        if mac not in mac_count:
            mac_count[mac] = 1
        else:
            mac_count[mac] += 1
    for mac, count in mac_count.items():
        if count > 100:
            attack_list.append(('MAC flood', mac))

# 定义函数:检测DHCP flood攻击
def detect_dhcp_flood():
    global attack_list
    sniff(filter='udp and (port 67 or port 68)', prn=lambda x: attack_list.append(('DHCP flood', x[Ether].src)), timeout=60)

# 定义函数:检测CC攻击
def detect_cc_attack():
    global attack_list
    packet_count = {}
    while True:
        time.sleep(1)
        packet_count[time.time()] = len(sniff(count=100, timeout=1))
        if len(packet_count) > 10:
            packet_count.pop(min(packet_count.keys()))
        if len(packet_count) == 10 and sum(packet_count.values()) > 1000:
            attack_list.append(('CC attack', None))

# 定义函数:检测ARP欺骗攻击
def detect_arp_spoof():
    global ip_list, mac_list, attack_list
    for i in range(len(ip_list)):
        if ARP(pdst=ip_list[i], hwdst='ff:ff:ff:ff:ff:ff') in sniff(filter='arp', count=10):
            for j in range(i+1, len(ip_list)):
                if ip_list[i] == ip_list[j] and mac_list[i] != mac_list[j]:
                    attack_list.append(('ARP spoof', mac_list[j]))

# 定义函数:防御ARP欺骗攻击
def prevent_arp_spoof():
    while True:
        time.sleep(1)
        for i in range(len(ip_list)):
            arp = ARP(pdst=ip_list[i], hwdst=mac_list[i])
            send(arp, verbose=False)

# 定义函数:输出结果到日志
def log_results():
    global attack_list
    while True:
        time.sleep(10)
        if attack_list:
            with open('log.txt', 'a') as f:
                f.write(time.strftime('%Y-%m-%d %H:%M:%S') + '\n')
                for attack in attack_list:
                    f.write(str(attack) + '\n')
                f.write('\n')
            attack_list = []

# 定义函数:启动检测和防御线程
def start_threads():
    t1 = threading.Thread(target=detect_mac_flood)
    t2 = threading.Thread(target=detect_dhcp_flood)
    t3 = threading.Thread(target=detect_cc_attack)
    t4 = threading.Thread(target=detect_arp_spoof)
    t5 = threading.Thread(target=prevent_arp_spoof)
    t6 = threading.Thread(target=log_results)
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()
    t6.start()

# 定义函数:显示可视化界面
def show_gui():
    root = Tk()
    root.title('ARP攻击检测防护软件')
    root.geometry('400x300')

    # 添加按钮
    scan_button = Button(root, text='扫描主机', command=scan_hosts)
    scan_button.pack(pady=10)

    start_button = Button(root, text='开始检测', command=start_threads)
    start_button.pack(pady=10)

    log_button = Button(root, text='查看日志', command=lambda: os.system('notepad log.txt'))
    log_button.pack(pady=10)

    root.mainloop()

if __name__ == '__main__':
    show_gui()

该程序使用了Scapy库来进行网络数据包的捕获和分析,使用了多线程来同时检测和防御多种攻击类型,并使用了Tkinter库来实现可视化界面。程序在启动后,可以通过点击“扫描主机”按钮来扫描局域网内的活动主机,然后点击“开始检测”按钮来开始检测和防御攻击,最后可以通过点击“查看日志”按钮来查看攻击日志。程序还可以自动将攻击信息保存在日志文件中,以便后续分析和处理

基于Linux的ARP攻击检测防护软件的设计模块包括MAC flood、DHCP flood、CC攻击和ARP欺骗攻击检测并且根据攻击类型输出信息扫描和记录局域网内活动主机的IP地址和MAC地址能够记录和保存攻击源的IP地址和MAC地址有一定的防御功能通过中文按钮实现可视化界面将结果保存在日志中。python基本代码实现将模块有机结合起来

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

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