import os
import time
import sys
import platform
from scapy.all import *
from tkinter import *
from tkinter import messagebox

# 设置界面
root = Tk()
root.title('网络安全卫士')
root.geometry('400x300')

# 注册界面
def register():
    register_window = Toplevel(root)
    register_window.title('注册')
    register_window.geometry('300x200')
    Label(register_window, text='请输入用户名:').pack()
    username = Entry(register_window)
    username.pack()
    Label(register_window, text='请输入密码:').pack()
    password = Entry(register_window, show='*')
    password.pack()
    def register_confirm():
        with open('user.txt', 'a') as f:
            f.write(username.get() + ' ' + password.get() + '\n')
        messagebox.showinfo('提示', '注册成功!')
        register_window.destroy()
    Button(register_window, text='确认', command=register_confirm).pack()

# 登录界面
def login():
    global login_window
    login_window = Toplevel(root)
    login_window.title('登录')
    login_window.geometry('300x200')
    Label(login_window, text='请输入用户名:').pack()
    username = Entry(login_window)
    username.pack()
    Label(login_window, text='请输入密码:').pack()
    password = Entry(login_window, show='*')
    password.pack()
    def login_confirm():
        with open('user.txt', 'r') as f:
            users = f.readlines()
            for user in users:
                if user.split()[0] == username.get() and user.split()[1] == password.get():
                    messagebox.showinfo('提示', '登录成功!')
                    enter_func() # 登录成功后进入功能页面
                    return
            messagebox.showerror('错误', '用户名或密码错误!')
    Button(login_window, text='确认', command=login_confirm).pack()

# ICMP flood攻击检测
def icmp_flood():
    sniff_filter = 'icmp'
    sniff_timeout = 10
    sniff_count = 0
    sniff_packets = sniff(filter=sniff_filter, timeout=sniff_timeout)
    for packet in sniff_packets:
        sniff_count += 1
    if sniff_count > 100:
        messagebox.showwarning('警告', '检测到ICMP flood攻击!')
        with open('detection_log.txt', 'a') as f:
            f.write('ICMP flood攻击检测 - 检测结果:检测到ICMP flood攻击\n')
    else:
        messagebox.showinfo('提示', '未检测到ICMP flood攻击!')
        with open('detection_log.txt', 'a') as f:
            f.write('ICMP flood攻击检测 - 检测结果:未检测到ICMP flood攻击\n')
    return

# TCP攻击检测
def tcp_attack():
    # 此处代码逻辑与ICMP flood类似,可根据实际情况修改
    sniff_filter = 'tcp'
    # ... 


# ARP欺骗攻击检测
def arp_spoofing():
    # 此处代码逻辑与ICMP flood类似,可根据实际情况修改
    sniff_filter = 'arp'
    # ...


# 扫描局域网主机
def scan_network():
    # 此处代码逻辑与示例代码相同
    network = '192.168.31.0/24'
    # ...


# 检测攻击源
def detect_attack():
    # 此处代码逻辑与示例代码相同
    with open('clients.txt', 'r') as f:
        # ...


def view_detection_log():
    with open('detection_log.txt', 'r') as f:
        detection_log = f.read()
    messagebox.showinfo('检测记录', detection_log)

# 创建用户和客户端记录文件
if not os.path.exists('user.txt'):
    open('user.txt', 'w').close()
if not os.path.exists('clients.txt'):
    open('clients.txt', 'w').close()

# 创建按钮
register_button = Button(root, text='注册', command=register)
register_button.pack(pady=10)
login_button = Button(root, text='登录', command=login)
login_button.pack(pady=10)
def enter_func():
    # 关闭登录窗口
    login_window.destroy()
    # 创建新窗口
    func_window = Toplevel(root)
    func_window.title('功能页面')
    func_window.geometry('400x300')
    # 创建四个功能按钮
    icmp_flood_button = Button(func_window, text='检测ICMP flood攻击', command=icmp_flood)
    icmp_flood_button.pack(pady=10)
    tcp_attack_button = Button(func_window, text='检测TCP攻击', command=tcp_attack)
    tcp_attack_button.pack(pady=10)
    arp_spoofing_button = Button(func_window, text='检测ARP欺骗攻击', command=arp_spoofing)
    arp_spoofing_button.pack(pady=10)
    detect_attack_button = Button(func_window, text='检测攻击源', command=detect_attack)
    detect_attack_button.pack(pady=10)
    scan_network_button = Button(func_window, text='扫描局域网主机',command=scan_network)
    scan_network_button.pack(pady=10)
    view_detection_log_button = Button(root, text='查看检测记录', command=view_detection_log)
    view_detection_log_button.pack(pady=10)


root.mainloop()
网络安全卫士:实时检测与防御ARP欺骗攻击