基于Linux的Python ARP攻击检测与防护软件设计

本软件使用Python语言在Linux系统上实现ARP攻击检测与防护功能。包含以下四个主要模块:

  1. 获取局域网活动主机的MAC地址、IP地址模块 (get_active_hosts.py)

    • 该模块负责扫描局域网并获取所有活动主机的MAC地址和IP地址。
  2. 检测防护ARP攻击行为模块 (arp_detection.py)

    • 该模块监控网络流量,检测ARP攻击行为。
  3. 记录保存ARP攻击行为模块 (arp_logging.py)

    • 该模块记录所有检测到的ARP攻击行为,并保存到日志文件。
  4. 显示ARP攻击源的MAC地址、IP地址模块 (arp_display.py)

    • 该模块读取日志文件,并显示所有ARP攻击源的MAC地址和IP地址。

模块实现

1. 获取局域网活动主机的MAC地址、IP地址模块 (get_active_hosts.py)

# get_active_hosts.py
import subprocess

def get_active_hosts():
    """
    获取局域网活动主机的MAC地址和IP地址
    """
    output = subprocess.check_output("arp -a", shell=True).decode("utf-8")
    hosts = {}
    for line in output.splitlines():
        if "(" in line:
            parts = line.split("(")
            ip = parts[0].strip()
            mac = parts[1].strip().replace(")", "")
            hosts[ip] = mac
    return hosts

2. 检测防护ARP攻击行为模块 (arp_detection.py)

# arp_detection.py
import time
import get_active_hosts

def detect_arp_attack():
    """
    检测ARP攻击行为
    """
    previous_hosts = get_active_hosts.get_active_hosts()
    while True:
        current_hosts = get_active_hosts.get_active_hosts()
        for ip, mac in current_hosts.items():
            if ip in previous_hosts and previous_hosts[ip] != mac:
                print(f"ARP攻击检测到:目标IP地址:{ip},攻击者MAC地址:{mac}")
                # 记录攻击行为到日志
        previous_hosts = current_hosts
        time.sleep(1)

3. 记录保存ARP攻击行为模块 (arp_logging.py)

# arp_logging.py
import datetime

def log_arp_attack(ip, attacker_mac):
    """
    记录ARP攻击行为到日志文件
    """
    with open("arp_attack_log.txt", "a") as f:
        timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        f.write(f"{timestamp}:目标IP地址:{ip},攻击者MAC地址:{attacker_mac}
")

4. 显示ARP攻击源的MAC地址、IP地址模块 (arp_display.py)

# arp_display.py

def display_arp_attacks():
    """
    从日志文件中读取并显示ARP攻击源的MAC地址和IP地址
    """
    with open("arp_attack_log.txt", "r") as f:
        for line in f:
            print(line.strip())

主程序

# main.py
import arp_detection
import arp_logging
import arp_display

if __name__ == "__main__":
    # 启动ARP攻击检测模块
    arp_detection.detect_arp_attack()

    # 显示所有ARP攻击记录
    arp_display.display_arp_attacks()

注意:

  • 以上代码仅供参考,需要根据实际情况进行修改。
  • 运行程序需要root权限。
  • 请注意,ARP攻击是一个较为复杂的网络攻击手段,本软件仅供学习交流使用,请勿用于非法目的。
基于Linux的Python ARP攻击检测与防护软件设计

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

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