import tkinter as tk import os import sys import time import threading import netifaces from scapy.all import * from scapy.layers.l2 import ARP from tkinter import messagebox from scapy.layers.l2 import getmacbyip

class ArpDetector: def init(self, interface, timeout=60): self.interface = interface self.ip_mac_map = {} self.attacker_ip = None self.attacker_mac = None self.thread = None self.stop_event = threading.Event() self.timeout = timeout self.timer = 0

def start(self):
    self.thread = threading.Thread(target=self.run)
    self.thread.start()

def stop(self):
    self.stop_event.set()
    self.thread.join()

def run(self):
    self.attacker_ip = netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr']
    self.attacker_mac = getmacbyip(self.attacker_ip)
    print(f'Attacker IP: {self.attacker_ip}, MAC: {self.attacker_mac}')

    while not self.stop_event.is_set():
        self.scan_network()
        self.detect_arp_spoofing()
        time.sleep(5)
        self.timer += 5
        if self.timer >= self.timeout:
            print('No ARP spoofing detected')
            self.stop_event.set()

def scan_network(self):
    for ip in netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr'].split('.')[:-1]:
        for i in range(1, 255):
            target_ip = f'{ip}.{i}'
            if target_ip != self.attacker_ip:
                arp_request = ARP(pdst=target_ip)
                arp_reply = sr1(arp_request, timeout=1, verbose=0)
                if arp_reply and arp_reply.hwsrc not in ('00:00:00:00:00:00', self.attacker_mac):
                    self.ip_mac_map[target_ip] = arp_reply.hwsrc

def detect_arp_spoofing(self):
    for target_ip, target_mac in self.ip_mac_map.items():
        arp_request = ARP(op=1, pdst=target_ip, hwdst=target_mac, psrc=self.attacker_ip, hwsrc=self.attacker_mac)
        arp_reply = sr1(arp_request, timeout=1, verbose=0)
        if arp_reply and arp_reply.hwsrc != target_mac:
            print(f'ARP spoofing detected: {target_ip} ({target_mac}) -> {arp_reply.hwsrc}')

class IcmpFloodDetector: def init(self, interface, timeout=60): self.interface = interface self.attacker_ip = None self.thread = None self.stop_event = threading.Event() self.timeout = timeout self.timer = 0

def start(self):
    self.thread = threading.Thread(target=self.run)
    self.thread.start()

def stop(self):
    self.stop_event.set()
    self.thread.join()

def run(self):
    self.attacker_ip = netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr']
    print(f'Attacker IP: {self.attacker_ip}')

    while not self.stop_event.is_set():
        self.detect_icmp_flood()
        time.sleep(5)
        self.timer += 5
        if self.timer >= self.timeout:
            print('No ICMP spoofing detected')
            self.stop_event.set()


def detect_icmp_flood(self):
    icmp_packets = sniff(filter=f'icmp and src host {self.attacker_ip}', timeout=1, count=10)
    if len(icmp_packets) == 10:
        print('ICMP flood detected')

class TcpAttackDetector: def init(self, interface, timeout=60): self.interface = interface self.attacker_ip = None self.thread = None self.stop_event = threading.Event() self.timeout = timeout self.timer = 0

def start(self):
    self.thread = threading.Thread(target=self.run)
    self.thread.start()

def stop(self):
    self.stop_event.set()
    self.thread.join()

def run(self):
    self.attacker_ip = netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr']
    print(f'Attacker IP: {self.attacker_ip}')

    while not self.stop_event.is_set():
        self.detect_tcp_attack()
        time.sleep(5)
        self.timer += 5
        if self.timer >= self.timeout:
            print('No TCP spoofing detected')
            self.stop_event.set()

def detect_tcp_attack(self):
    tcp_packets = sniff(filter=f'tcp and src host {self.attacker_ip}', timeout=1, count=10)
    if len(tcp_packets) == 10:
        print('TCP attack detected')

class NetworkScanner: def init(self, interface): self.interface = interface self.ip_mac_map = {} self.thread = None self.stop_event = threading.Event()

def start(self):
    self.thread = threading.Thread(target=self.run)
    self.thread.start()

def stop(self):
    self.stop_event.set()
    self.thread.join()

def run(self):
    while not self.stop_event.is_set():
        self.scan_network()
        time.sleep(10)

def scan_network(self):
    for ip in netifaces.ifaddresses(self.interface)[netifaces.AF_INET][0]['addr'].split('.')[:-1]:
        for i in range(1, 255):
            target_ip = f'{ip}.{i}'
            if target_ip not in self.ip_mac_map:
                arp_request = ARP(pdst=target_ip)
                arp_reply = sr1(arp_request, timeout=1, verbose=0)
                if arp_reply and arp_reply.hwsrc not in ('00:00:00:00:00:00', 'ff:ff:ff:ff:ff:ff'):
                    self.ip_mac_map[target_ip] = arp_reply.hwsrc
                    print(f'Found: {target_ip} ({arp_reply.hwsrc})')
                    self.save_to_file(target_ip, arp_reply.hwsrc)

def save_to_file(self, ip, mac):
    with open('network_scan.txt', 'a') as f:
        f.write(f'{ip},{mac}

')

class LoginWindow: def init(self): self.root = tk.Tk() self.root.title('Login') self.root.geometry('300x150')

    tk.Label(self.root, text='Username').place(x=50, y=30)
    self.username_entry = tk.Entry(self.root)
    self.username_entry.place(x=120, y=30)

    tk.Label(self.root, text='Password').place(x=50, y=60)
    self.password_entry = tk.Entry(self.root, show='*')
    self.password_entry.place(x=120, y=60)

    self.login_button = tk.Button(self.root, text='Login', command=self.login)
    self.login_button.place(x=100, y=100)

    self.register_button = tk.Button(self.root, text='Register', command=self.register)
    self.register_button.place(x=170, y=100)

    self.root.mainloop()

def show_main_window(self):
        self.root.destroy()
        MainWindow()

def login(self):
    username = self.username_entry.get()
    password = self.password_entry.get()
    for user in users:
        if user['username'] == username and user['password'] == password:
            # 登录成功,跳转到主界面
            self.show_main_window()
            return
        # 登录失败,弹出错误提示
    messagebox.showinfo('错误', '用户名或密码错误')

def register(self):
    username = self.username_entry.get()
    password = self.password_entry.get()
    for user in users:
        if user['username'] == username:
            # 用户名已存在,弹出错误提示
            messagebox.showerror('错误', '用户名已存在')
            return

    # 用户名不存在,将新用户添加到用户列表中
    users.append({'username': username, 'password': password})
    # 注册成功,弹出成功提示
    messagebox.showinfo('提示', '注册成功,请登录')

class MainWindow: def init(self): self.root = tk.Tk() self.root.title('ARP Detector') self.root.geometry('400x350')

    self.status_label = tk.Label(self.root, text='Idle')
    self.status_label.pack()

    self.start_button = tk.Button(self.root, text='Start All', command=self.start_detection)
    self.start_button.pack()

    self.stop_button = tk.Button(self.root, text='Stop All', command=self.stop_detection, state=tk.DISABLED)
    self.stop_button.pack()

    self.arp_button = tk.Button(self.root, text='ARP Detection', command=lambda: self.show_frame(self.arp_frame), state=tk.DISABLED)
    self.arp_button.pack()

    self.icmp_button = tk.Button(self.root, text='ICMP Flood Detection', command=lambda: self.show_frame(self.icmp_frame), state=tk.DISABLED)
    self.icmp_button.pack()

    self.tcp_button = tk.Button(self.root, text='TCP Attack Detection', command=lambda: self.show_frame(self.tcp_frame), state=tk.DISABLED)
    self.tcp_button.pack()

    self.network_button = tk.Button(self.root, text='Network Scanner', command=lambda: self.show_frame(self.network_frame), state=tk.DISABLED)
    self.network_button.pack()

    self.timeout_label = tk.Label(self.root, text='Detection Time (s)')
    self.timeout_label.pack()
    self.timeout_entry = tk.Entry(self.root)
    self.timeout_entry.pack()

    self.set_timeout_button = tk.Button(self.root, text='Set Timeout', command=self.set_timeout)
    self.set_timeout_button.pack()

    # 创建四个Frame,分别放置四个模块的控件
    self.arp_frame = tk.Frame(self.root)
    self.icmp_frame = tk.Frame(self.root)
    self.tcp_frame = tk.Frame(self.root)
    self.network_frame = tk.Frame(self.root)

    # 在Frame中添加控件
    # ARP Detection
    tk.Label(self.arp_frame, text='ARP Detection').pack()
    # ... 添加其他ARP检测相关控件

    # ICMP Flood Detection
    tk.Label(self.icmp_frame, text='ICMP Flood Detection').pack()
    # ... 添加其他ICMP洪泛攻击检测相关控件

    # TCP Attack Detection
    tk.Label(self.tcp_frame, text='TCP Attack Detection').pack()
    # ... 添加其他TCP攻击检测相关控件

    # Network Scanner
    tk.Label(self.network_frame, text='Network Scanner').pack()
    # ... 添加其他网络扫描相关控件

    # 将四个Frame放置在主页面中,但只显示其中一个Frame,其他三个Frame隐藏
    self.arp_frame.grid(row=0, column=0, sticky='nsew')
    self.icmp_frame.grid(row=0, column=0, sticky='nsew')
    self.tcp_frame.grid(row=0, column=0, sticky='nsew')
    self.network_frame.grid(row=0, column=0, sticky='nsew')
    self.show_frame(self.arp_frame)

    self.root.mainloop()

def show_frame(self, frame):
    # 隐藏当前显示的Frame
    for child in self.root.winfo_children():
        if child.winfo_class() == 'Frame':
            child.grid_forget()

    # 显示对应的Frame
    frame.grid(row=0, column=0, sticky='nsew')

def start_detection(self):
    timeout = int(self.timeout_entry.get())
    self.status_label.config(text='Running')
    self.start_button.config(state=tk.DISABLED)
    self.stop_button.config(state=tk.NORMAL)
    self.arp_button.config(state=tk.NORMAL)
    self.icmp_button.config(state=tk.NORMAL)
    self.tcp_button.config(state=tk.NORMAL)
    self.network_button.config(state=tk.NORMAL)

    self.arp_detector = ArpDetector('ens33', timeout)
    self.arp_detector.start()

    self.icmp_flood_detector = IcmpFloodDetector('ens33', timeout)
    self.icmp_flood_detector.start()

    self.tcp_attack_detector = TcpAttackDetector('ens33', timeout)
    self.tcp_attack_detector.start()

    self.network_scanner = NetworkScanner('ens33')
    self.network_scanner.start()

def set_timeout(self):
    timeout = int(self.timeout_entry.get())
    messagebox.showinfo('提示', f'检测时间已设置为{timeout}秒')

def start_detection(self):
    self.status_label.config(text='Running')
    self.start_button.config(state=tk.DISABLED)
    self.stop_button.config(state=tk.NORMAL)
    self.arp_button.config(state=tk.NORMAL)
    self.icmp_button.config(state=tk.NORMAL)
    self.tcp_button.config(state=tk.NORMAL)
    self.network_button.config(state=tk.NORMAL)

    self.arp_detector = ArpDetector('ens33',timeout)
    self.arp_detector.start()

    self.icmp_flood_detector = IcmpFloodDetector('ens33',timeout)
    self.icmp_flood_detector.start()

    self.tcp_attack_detector = TcpAttackDetector('ens33',timeout)
    self.tcp_attack_detector.start()

    self.network_scanner = NetworkScanner('ens33')
    self.network_scanner.start()

def stop_detection(self):
    self.status_label.config(text='Idle')
    self.start_button.config(state=tk.NORMAL)
    self.stop_button.config(state=tk.DISABLED)
    self.arp_button.config(state=tk.DISABLED)
    self.icmp_button.config(state=tk.DISABLED)
    self.tcp_button.config(state=tk.DISABLED)
    self.network_button.config(state=tk.DISABLED)

    self.arp_detector.stop()
    self.icmp_flood_detector.stop()
    self.tcp_attack_detector.stop()
    self.network_scanner.stop()

def toggle_arp_detection(self):
    if self.arp_detector.thread is None or not self.arp_detector.thread.is_alive():
        self.arp_detector = ArpDetector('ens33')
        self.arp_detector.start()
        self.arp_button.config(text='Stop ARP Detection')
    else:
        self.arp_detector.stop()
        self.arp_button.config(text='Start ARP Detection')

def toggle_icmp_detection(self):
    if self.icmp_flood_detector.thread is None or not self.icmp_flood_detector.thread.is_alive():
        self.icmp_flood_detector = IcmpFloodDetector('ens33')
        self.icmp_flood_detector.start()
        self.icmp_button.config(text='Stop ICMP Flood Detection')
    else:
        self.icmp_flood_detector.stop()
        self.icmp_button.config(text='Start ICMP Flood Detection')

def toggle_tcp_detection(self):
    if self.tcp_attack_detector.thread is None or not self.tcp_attack_detector.thread.is_alive():
        self.tcp_attack_detector = TcpAttackDetector('ens33')
        self.tcp_attack_detector.start()
        self.tcp_button.config(text='Stop TCP Attack Detection')
    else:
        self.tcp_attack_detector.stop()
        self.tcp_button.config(text='Start TCP Attack Detection')

def toggle_network_scanner(self):
    if self.network_scanner.thread is None or not self.network_scanner.thread.is_alive():
        self.network_scanner = NetworkScanner('ens33')
        self.network_scanner.start()
        self.network_button.config(text='Stop Network Scanner')
    else:
        self.network_scanner.stop()
        self.network_button.config(text='Start Network Scanner')

if name == 'main': users = [{'username': 'admin', 'password': 'admin'}] login_window = LoginWindow() main_window = MainWindow()

网络攻击检测工具 - ARP、ICMP、TCP攻击检测和网络扫描

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

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