基于Python的ARP攻击检测软件 - 实时监控局域网安全

本软件使用Python实现ARP攻击检测功能,通过分析ARP请求和响应包,判断是否存在异常情况,并提供用户登录、注册、攻击检测、局域网主机扫描等功能,可实时监控局域网安全。

功能概述

  • 用户登录和注册: 用户可注册账号并登录系统,进行后续操作。

  • ARP攻击检测: 软件会实时分析ARP请求和响应包,判断是否存在以下异常情况:

    • 检测应答报文时,判断是否出现同一IP地址对应不同的MAC地址;是则可能遭受ARP攻击
    • 检测请求报文时,判断是否合法且是否出现不同IP地址对应同一个MAC地址;是则可能遭受ARP攻击
    • 检测ARP请求或响应包,检测是否存在同一IP或者是同一MAC地址发出大量数据包,是则可能遭受ARP攻击,不是则未遭受ARP攻击

    同时满足两种规则或以上的就输出遭受ARP攻击,不满足则输出未遭受ARP攻击,并且将所有信息保存在日志中。

  • 扫描局域网主机活动: 软件可扫描局域网内的活动主机,并显示其IP地址和MAC地址。

实现思路

  1. 使用Python的Scapy库进行ARP包的捕获和分析
  2. 实现用户登录和注册功能,使用文件存储用户信息
  3. 实现攻击检测功能,分析ARP请求和响应包,判断是否存在异常情况
  4. 实现扫描局域网主机活动功能,获取局域网内活动主机的IP和MAC地址
  5. 将所有信息保存在日志文件中

代码示例

from scapy.all import *
import time
import os
import logging

# 配置日志
logging.basicConfig(filename='arp_attack.log', level=logging.INFO)

# 捕获ARP包
def sniff_arp_packets():
    arp_packets = sniff(filter='arp', count=100)
    return arp_packets

# 分析ARP包
def analyze_arp_packets(arp_packets):
    arp_request_count = 0
    arp_response_count = 0
    src_ip_mac_dict = {}
    dst_ip_mac_dict = {}
    for packet in arp_packets:
        if packet.op == 1:  # ARP请求包
            arp_request_count += 1
            if packet.psrc in src_ip_mac_dict and src_ip_mac_dict[packet.psrc] != packet.hwsrc:
                print('可能遭受ARP攻击:IP地址' + packet.psrc + '对应多个MAC地址')
        elif packet.op == 2:  # ARP响应包
            arp_response_count += 1
            if packet.psrc in src_ip_mac_dict and src_ip_mac_dict[packet.psrc] != packet.hwsrc:
                print('可能遭受ARP攻击:IP地址' + packet.psrc + '对应多个MAC地址')
            else:
                src_ip_mac_dict[packet.psrc] = packet.hwsrc
            if packet.pdst in dst_ip_mac_dict and dst_ip_mac_dict[packet.pdst] != packet.hwdst:
                print('可能遭受ARP攻击:MAC地址' + packet.hwdst + '对应多个IP地址')
            else:
                dst_ip_mac_dict[packet.pdst] = packet.hwdst
    return arp_request_count, arp_response_count

# 用户注册
def register(username, password):
    filename = 'users.txt'
    if os.path.exists(filename):
        with open(filename, 'r') as f:
            for line in f:
                if line.strip().split(':')[0] == username:
                    print('该用户已注册')
                    return False
    with open(filename, 'a') as f:
        f.write(username + ':' + password + '\n')
    print('注册成功')
    return True

# 用户登录
def login(username, password):
    filename = 'users.txt'
    if os.path.exists(filename):
        with open(filename, 'r') as f:
            for line in f:
                if line.strip().split(':')[0] == username:
                    if line.strip().split(':')[1] == password:
                        print('登录成功')
                        return True
                    else:
                        print('用户名或密码错误')
                        return False
    print('查无该用户')
    return False

# 判断是否遭受ARP攻击
def detect_arp_attack():
    arp_packets = sniff_arp_packets()
    arp_request_count, arp_response_count = analyze_arp_packets(arp_packets)
    if arp_request_count > 100 and arp_response_count > 100:
        print('遭受ARP攻击')
    else:
        print('未遭受ARP攻击')

# 获取局域网内活动主机的IP和MAC地址
def scan_active_hosts():
    arp_packets = ARP(pdst='192.168.1.0/24')
    result = arp_packets.psr()
    active_hosts = []
    for sent, received in result:
        active_hosts.append({'ip': received.psrc, 'mac': received.hwsrc})
    return active_hosts

# 将信息保存到日志文件中
def save_to_log(msg):
    logging.info(msg)

# 主程序
# ... (后续代码实现用户界面等)

使用说明

  1. 确保系统已安装Python和Scapy库。
  2. 将代码保存为.py文件。
  3. 运行程序。
  4. 登录或注册用户。
  5. 点击“开始攻击检测”按钮进行ARP攻击检测。
  6. 点击“扫描局域网主机活动”按钮扫描局域网内的活动主机。

注意事项

  • 本软件仅供学习和研究使用,请勿用于非法目的。
  • 在使用本软件之前,请确保您已获得授权。
  • 由于网络环境的复杂性,本软件无法保证完全检测出所有ARP攻击。

总结

本软件使用Python实现ARP攻击检测功能,可有效提高局域网的安全防护能力,帮助用户及时发现和处理ARP攻击事件。

其他

  • 软件界面可根据实际需求进行设计。
  • 软件功能可根据实际需求进行扩展。
  • 用户可根据实际网络环境修改代码中的IP地址和子网掩码。
基于Python的ARP攻击检测软件 - 实时监控局域网安全

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

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