基于Python的网络安全检测工具
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):
self.interface = interface
self.ip_mac_map = {}
self.attacker_ip = None
self.attacker_mac = None
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):
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)
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:
# ...(代码内容与之前相同,此处省略)
class TcpAttackDetector:
# ...(代码内容与之前相同,此处省略)
class NetworkScanner:
# ...(代码内容与之前相同,此处省略)
class LoginWindow:
# ...(代码内容与之前相同,此处省略)
class MainWindow:
# ...(代码内容与之前相同,此处省略)
if __name__ == '__main__':
users = [{'username': 'admin', 'password': 'admin'}]
login_window = LoginWindow()
# 在登录成功后再创建主窗口
if login_window.login_success:
main_window = MainWindow()
# 关于ARP Detection按钮没有反应的说明
**可能原因:**
1. **多线程延迟:** 程序使用了多线程,ARP Detection 的结果可能需要等待一段时间才能显示出来。
2. **网络接口名称错误:** 请确保程序中使用的网络接口名称(例如 'ens33')与您的实际网络接口名称一致。可以使用 `netifaces.interfaces()` 命令查看可用的网络接口名称。
3. **代码逻辑错误:** 检查 ARP Detection 相关的代码逻辑,例如 `ArpDetector` 类中的 `scan_network()` 和 `detect_arp_spoofing()` 方法,确保其能够正常工作。
4. **其他错误:** 尝试在命令行中运行程序,查看是否有任何错误提示,以便进行调试。
**建议:**
* 在 `ArpDetector` 类的方法中添加打印语句,以便跟踪程序执行流程和变量值,帮助定位问题。
* 使用调试器逐步执行代码,观察程序行为并查找错误。
* 查阅 Scapy 文档,了解 ARP 数据包的结构和发送方式,确保代码正确构建和发送 ARP 请求。
原文地址: https://www.cveoy.top/t/topic/jnUo 著作权归作者所有。请勿转载和采集!