CentOS下用Python打造ARP欺骗检测软件

本教程将引导您使用Python在CentOS系统上开发一款ARP欺骗检测软件。该软件不仅能够检测ARP欺骗,还能识别ICMP Flood和TCP攻击,扫描并记录局域网活动主机,并提供用户登录和注册功能。

获取本机MAC地址

import subprocess

def get_mac_address():
    output = subprocess.check_output(['ifconfig'])
    output = output.decode('utf-8')
    index = output.find('ether ')
    mac_address = output[index+6:index+23]
    return mac_address

此函数返回本机的MAC地址,例如:'00:11:22:33:44:55'。

ARP欺骗检测

from scapy.all import *

ip_mac_dict = {}

def arp_monitor_callback(pkt):
    if ARP in pkt and pkt[ARP].op in (1,2): 
        mac_address = pkt[ARP].hwsrc
        ip_address = pkt[ARP].psrc
        if mac_address != get_mac_address() and ip_address in ip_mac_dict:
            if ip_mac_dict[ip_address] != mac_address:
                print(f'检测到ARP欺骗攻击,攻击源:{ip_address} ({mac_address})')
                return

def arp_spoof_detect():
    sniff(prn=arp_monitor_callback, filter='arp', store=0)

这段代码监听ARP响应包,如果发现重复的IP地址对应不同的MAC地址,则输出警告信息。

ICMP Flood和TCP攻击检测

from scapy.all import *

def icmp_flood_detect(ip_address):
    icmp_packets = IP(dst=ip_address)/ICMP()
    ans, unans = sr(icmp_packets, timeout=2)
    if len(ans) == 0:
        print(f'检测到ICMP Flood攻击,攻击源:{ip_address}')

def tcp_attack_detect(ip_address):
    tcp_packets = IP(dst=ip_address)/TCP(flags='S')
    ans, unans = sr(tcp_packets, timeout=2)
    if len(ans) == 0:
        print(f'检测到TCP攻击,攻击源:{ip_address}')

上述代码分别发送ICMP Echo请求包和TCP连接请求包,若未收到任何响应,则输出警告信息。

扫描和记录局域网活动主机

from scapy.all import *

def arp_scan():
    arp_packets = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(op=1)
    ans, unans = srp(arp_packets, timeout=2)
    for send, recv in ans:
        ip_address = recv[ARP].psrc
        mac_address = recv[Ether].src
        ip_mac_dict[ip_address] = mac_address
        print(f'{ip_address} ({mac_address})')

这段代码循环执行ARP扫描,记录局域网内的IP地址和MAC地址。

用户登录和注册界面 (示例)

from tkinter import *

# ... (登录和注册逻辑,请参考之前的代码) 

login_window = Tk()
# ... (界面元素和布局)
login_window.mainloop()

使用Tkinter库可以创建用户登录和注册界面。

整合代码

将以上模块整合,即可构建完整的ARP欺骗检测软件。

注意:

  • 以上代码仅供参考,实际开发中需要根据需求进行调整和完善。
  • 请确保在合法环境下使用该软件,并遵守相关法律法规。
CentOS ARP欺骗检测软件Python实现

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

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