功能需求:

  1. 捕获数据包,并过滤出ARP包。
  2. 分析ARP包,检查同一IP地址对应多个MAC地址、多个IP地址对应同一个MAC地址、大量的ARP请求或响应包、源MAC地址和目标MAC地址不匹配等情况。
  3. 输出遭受ARP攻击的信息,并将所有信息保存在日志中。

Python实现:

  1. 使用scapy库捕获数据包,并过滤出ARP包。
from scapy.all import *

def arp_sniffer():
    # 过滤出ARP包
    arp_filter = "arp"
    # 捕获数据包
    sniff(filter=arp_filter, prn=arp_handler, store=0)

def arp_handler(pkt):
    # 对ARP包进行分析
    # ...
  1. 对ARP包进行分析,检查同一IP地址对应多个MAC地址、多个IP地址对应同一个MAC地址、大量的ARP请求或响应包、源MAC地址和目标MAC地址不匹配等情况。
def arp_handler(pkt):
    # 获取源IP地址、源MAC地址、目标IP地址、目标MAC地址
    src_ip = pkt[ARP].psrc
    src_mac = pkt[ARP].hwsrc
    dst_ip = pkt[ARP].pdst
    dst_mac = pkt[ARP].hwdst

    # 检查同一IP地址对应多个MAC地址
    if src_ip in arp_table and src_mac not in arp_table[src_ip]:
        print("ARP spoofing detected! Multiple MAC addresses detected for IP address: {}".format(src_ip))
        log("ARP spoofing detected! Multiple MAC addresses detected for IP address: {}".format(src_ip))

    # 检查多个IP地址对应同一个MAC地址
    if src_mac in arp_table and src_ip not in arp_table[src_mac]:
        print("ARP spoofing detected! Multiple IP addresses detected for MAC address: {}".format(src_mac))
        log("ARP spoofing detected! Multiple IP addresses detected for MAC address: {}".format(src_mac))

    # 更新ARP表
    arp_table[src_ip] = src_mac

    # 统计ARP请求和响应包的数量
    if pkt[ARP].op == ARP.who_has:
        arp_request_count[src_ip] += 1
    elif pkt[ARP].op == ARP.is_at:
        arp_reply_count[src_ip] += 1

    # 检查大量的ARP请求或响应包
    if arp_request_count[src_ip] > ARP_REQUEST_THRESHOLD or arp_reply_count[src_ip] > ARP_REPLY_THRESHOLD:
        print("ARP spoofing detected! Abnormal number of ARP requests or replies detected for IP address: {}".format(src_ip))
        log("ARP spoofing detected! Abnormal number of ARP requests or replies detected for IP address: {}".format(src_ip))

    # 检查源MAC地址和目标MAC地址不匹配
    if src_mac != arp_table[dst_ip]:
        print("ARP spoofing detected! Source MAC address and destination MAC address do not match for IP address: {}".format(dst_ip))
        log("ARP spoofing detected! Source MAC address and destination MAC address do not match for IP address: {}".format(dst_ip))
  1. 输出遭受ARP攻击的信息,并将所有信息保存在日志中。
def log(msg):
    # 将信息保存在日志中
    with open("arp_spoofing.log", "a") as f:
        f.write(msg + "\n")

if __name__ == "__main__":
    # ARP表
    arp_table = {}
    # 统计ARP请求和响应包的数量
    arp_request_count = defaultdict(int)
    arp_reply_count = defaultdict(int)
    # ARP请求和响应包的阈值
    ARP_REQUEST_THRESHOLD = 10
    ARP_REPLY_THRESHOLD = 10

    # 捕获数据包并进行分析
    arp_sniffer()

完整代码如下:

from scapy.all import *
from collections import defaultdict

def arp_sniffer():
    # 过滤出ARP包
    arp_filter = "arp"
    # 捕获数据包
    sniff(filter=arp_filter, prn=arp_handler, store=0)

def arp_handler(pkt):
    # 获取源IP地址、源MAC地址、目标IP地址、目标MAC地址
    src_ip = pkt[ARP].psrc
    src_mac = pkt[ARP].hwsrc
    dst_ip = pkt[ARP].pdst
    dst_mac = pkt[ARP].hwdst

    # 检查同一IP地址对应多个MAC地址
    if src_ip in arp_table and src_mac not in arp_table[src_ip]:
        print("ARP spoofing detected! Multiple MAC addresses detected for IP address: {}".format(src_ip))
        log("ARP spoofing detected! Multiple MAC addresses detected for IP address: {}".format(src_ip))

    # 检查多个IP地址对应同一个MAC地址
    if src_mac in arp_table and src_ip not in arp_table[src_mac]:
        print("ARP spoofing detected! Multiple IP addresses detected for MAC address: {}".format(src_mac))
        log("ARP spoofing detected! Multiple IP addresses detected for MAC address: {}".format(src_mac))

    # 更新ARP表
    arp_table[src_ip] = src_mac

    # 统计ARP请求和响应包的数量
    if pkt[ARP].op == ARP.who_has:
        arp_request_count[src_ip] += 1
    elif pkt[ARP].op == ARP.is_at:
        arp_reply_count[src_ip] += 1

    # 检查大量的ARP请求或响应包
    if arp_request_count[src_ip] > ARP_REQUEST_THRESHOLD or arp_reply_count[src_ip] > ARP_REPLY_THRESHOLD:
        print("ARP spoofing detected! Abnormal number of ARP requests or replies detected for IP address: {}".format(src_ip))
        log("ARP spoofing detected! Abnormal number of ARP requests or replies detected for IP address: {}".format(src_ip))

    # 检查源MAC地址和目标MAC地址不匹配
    if src_mac != arp_table[dst_ip]:
        print("ARP spoofing detected! Source MAC address and destination MAC address do not match for IP address: {}".format(dst_ip))
        log("ARP spoofing detected! Source MAC address and destination MAC address do not match for IP address: {}".format(dst_ip))

def log(msg):
    # 将信息保存在日志中
    with open("arp_spoofing.log", "a") as f:
        f.write(msg + "\n")

if __name__ == "__main__":
    # ARP表
    arp_table = {}
    # 统计ARP请求和响应包的数量
    arp_request_count = defaultdict(int)
    arp_reply_count = defaultdict(int)
    # ARP请求和响应包的阈值
    ARP_REQUEST_THRESHOLD = 10
    ARP_REPLY_THRESHOLD = 10

    # 捕获数据包并进行分析
    arp_sniffer()
``

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

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