基于Linux的ARP攻击检测软件Python实现

本篇文章介绍如何使用Python在Linux系统下开发一款ARP攻击检测软件。该软件主要功能包括:

  1. 数据包捕获与过滤: 使用scapy库捕获网络数据包,并过滤出ARP包进行分析。
  2. ARP包分析: 分析ARP请求和应答包,识别可疑行为,例如:
    • 同一IP地址对应多个MAC地址
    • 不同IP地址对应同一MAC地址
    • 同一IP或MAC地址发送大量ARP包
  3. 用户注册与登录: 实现简单的用户注册和登录功能,增强软件安全性。
  4. 攻击检测: 实时检测ARP攻击,并将检测结果输出到日志文件。
  5. 局域网主机扫描: 扫描局域网内的活动主机,并输出其IP地址和MAC地址信息。

Python代码实现

以下Python代码演示了如何实现上述功能:

import os
import sys
import time
import datetime
from scapy.all import *

# 捕获数据包并过滤出ARP包
def arp_capture():
    arp_packets = sniff(filter='arp', count=0)
    return arp_packets

# 分析ARP包,判断是否存在异常主机
def analyze_arp_packets(arp_packets):
    normal_hosts = []
    suspicious_hosts = []
    ip_mac_map = {}
    for arp_packet in arp_packets:
        if arp_packet.op == 1:  # ARP请求包
            if arp_packet.psrc in ip_mac_map and ip_mac_map[arp_packet.psrc] != arp_packet.hwsrc:
                suspicious_hosts.append({'ip': arp_packet.psrc, 'mac': arp_packet.hwsrc})
            else:
                ip_mac_map[arp_packet.psrc] = arp_packet.hwsrc
                normal_hosts.append({'ip': arp_packet.psrc, 'mac': arp_packet.hwsrc})
        elif arp_packet.op == 2:  # ARP应答包
            if arp_packet.psrc in ip_mac_map and ip_mac_map[arp_packet.psrc] != arp_packet.hwsrc:
                suspicious_hosts.append({'ip': arp_packet.psrc, 'mac': arp_packet.hwsrc})
            else:
                ip_mac_map[arp_packet.psrc] = arp_packet.hwsrc
                normal_hosts.append({'ip': arp_packet.psrc, 'mac': arp_packet.hwsrc})
    return normal_hosts, suspicious_hosts

# 用户注册功能
def user_register(username, password):
    user_file = 'user.txt'
    if os.path.isfile(user_file):
        with open(user_file, 'r') as f:
            for line in f:
                if line.strip().startswith(username):
                    print('该用户已注册')
                    return
    with open(user_file, 'a') as f:
        f.write(username + ' ' + password + '
')
    print('用户注册成功')

# 用户登录功能
def user_login(username, password):
    user_file = 'user.txt'
    if os.path.isfile(user_file):
        with open(user_file, 'r') as f:
            for line in f:
                if line.strip().startswith(username):
                    if line.strip().endswith(password):
                        print('登录成功')
                        return True
                    else:
                        print('用户名或密码错误')
                        return False
    else:
        print('查无该用户')
        return False

# 开始攻击检测
def start_detection():
    log_file = 'arp_attack.log'
    with open(log_file, 'a') as f:
        f.write('开始攻击检测:' + str(datetime.datetime.now()) + '
')
    arp_packets = arp_capture()
    normal_hosts, suspicious_hosts = analyze_arp_packets(arp_packets)
    if len(suspicious_hosts) > 0:
        print('遭受ARP攻击')
        with open(log_file, 'a') as f:
            f.write('遭受ARP攻击,异常主机如下:' + str(suspicious_hosts) + '
')
    else:
        print('未遭受ARP攻击')

# 停止攻击检测
def stop_detection():
    log_file = 'arp_attack.log'
    with open(log_file, 'a') as f:
        f.write('停止攻击检测:' + str(datetime.datetime.now()) + '
')

# 扫描局域网主机
def scan_hosts():
    log_file = 'arp_attack.log'
    with open(log_file, 'a') as f:
        f.write('扫描局域网主机:' + str(datetime.datetime.now()) + '
')
    arp_packets = arp_capture()
    normal_hosts, suspicious_hosts = analyze_arp_packets(arp_packets)
    all_hosts = normal_hosts + suspicious_hosts
    if len(all_hosts) > 0:
        print('局域网主机如下:')
        for host in all_hosts:
            print(host['ip'], host['mac'])
        with open(log_file, 'a') as f:
            f.write('局域网主机如下:' + str(all_hosts) + '
')
    else:
        print('未发现局域网主机')

# 用户注册和登录
def user_manage():
    while True:
        choice = input('请选择功能:1.注册 2.登录 3.退出
')
        if choice == '1':
            username = input('请输入用户名:')
            password = input('请输入密码:')
            user_register(username, password)
        elif choice == '2':
            username = input('请输入用户名:')
            password = input('请输入密码:')
            if user_login(username, password):
                break
        elif choice == '3':
            sys.exit()

# 功能页面
def function_page():
    while True:
        choice = input('请选择功能:1.开始攻击检测 2.停止攻击检测 3.扫描局域网主机 4.退出
')
        if choice == '1':
            start_detection()
        elif choice == '2':
            stop_detection()
        elif choice == '3':
            scan_hosts()
        elif choice == '4':
            sys.exit()

# 主函数
if __name__ == '__main__':
    user_manage()
    function_page()

代码说明

  1. 导入库: 代码首先导入了所需的库,包括 os, sys, time, datetimescapy
  2. 函数定义: 代码定义了多个函数,用于实现不同的功能,例如捕获ARP包、分析ARP包、用户注册、用户登录、开始攻击检测、停止攻击检测、扫描局域网主机等。
  3. 主函数: 主函数首先调用 user_manage() 函数进行用户管理,然后调用 function_page() 函数进入功能页面。

总结

本文介绍了如何使用Python在Linux系统下实现一个简单的ARP攻击检测软件。该软件可以帮助用户及时发现并防御ARP攻击,提高网络安全。需要注意的是,这只是一个简单的示例代码,实际应用中需要根据具体情况进行修改和完善。

基于Linux的ARP攻击检测软件Python实现

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

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