基于Linux的ARP攻击检测防护软件的设计模块包括MAC flood、DHCP flood、CC攻击和ARP欺骗攻击检测并且根据攻击类型输出信息扫描和记录局域网内活动主机的IP地址和MAC地址有一定的防御功能通过html页面实现可视化界面将结果保存在日志中。python基本代码实现将模块有机结合起来
以下是一个基于Python的ARP攻击检测防护软件的代码示例,其中包括MAC flood、DHCP flood、CC攻击和ARP欺骗攻击检测,并且具有一定的防御功能和可视化界面。请注意,本示例代码仅供参考,实际应用中可能需要根据具体需求进行修改和优化。
import os
import sys
import time
import threading
import logging
from scapy.all import *
from flask import Flask, render_template, request
app = Flask(__name__)
# 定义日志文件
logging.basicConfig(filename='arp_defender.log', level=logging.INFO)
# 定义全局变量
arp_table = {}
ip_list = []
mac_list = []
attack_type = ''
# 定义函数:扫描局域网内的活动主机
def scan_network():
global arp_table, ip_list, mac_list
ip_list = []
mac_list = []
arp_table = {}
ans, unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst="192.168.1.0/24"), timeout=2, verbose=False)
for snd, rcv in ans:
arp_table[rcv[ARP].psrc] = rcv[Ether].src
ip_list.append(rcv[ARP].psrc)
mac_list.append(rcv[Ether].src)
# 定义函数:检测MAC flood攻击
def detect_mac_flood():
global attack_type
mac_counts = {}
for mac in mac_list:
if mac not in mac_counts:
mac_counts[mac] = 1
else:
mac_counts[mac] += 1
for mac, count in mac_counts.items():
if count > 50:
attack_type = 'MAC flood'
logging.warning('MAC flood attack detected from MAC address: %s' % mac)
# 定义函数:检测DHCP flood攻击
def detect_dhcp_flood():
global attack_type
dhcp_counts = {}
for pkt in sniff(filter="udp and (port 67 or port 68)", timeout=2, iface="eth0"):
if DHCP in pkt:
mac = pkt[Ether].src
if mac not in dhcp_counts:
dhcp_counts[mac] = 1
else:
dhcp_counts[mac] += 1
for mac, count in dhcp_counts.items():
if count > 50:
attack_type = 'DHCP flood'
logging.warning('DHCP flood attack detected from MAC address: %s' % mac)
# 定义函数:检测CC攻击
def detect_cc_attack():
global attack_type
cc_counts = {}
for ip in ip_list:
if ip not in arp_table:
continue
mac = arp_table[ip]
if mac not in cc_counts:
cc_counts[mac] = 1
else:
cc_counts[mac] += 1
for mac, count in cc_counts.items():
if count > 50:
attack_type = 'CC attack'
logging.warning('CC attack detected from MAC address: %s' % mac)
# 定义函数:检测ARP欺骗攻击
def detect_arp_spoofing():
global attack_type
for pkt in sniff(filter="arp", timeout=2, iface="eth0"):
if ARP in pkt and pkt[ARP].op == 2:
if pkt[ARP].psrc in arp_table and arp_table[pkt[ARP].psrc] != pkt[Ether].src:
attack_type = 'ARP spoofing'
logging.warning('ARP spoofing attack detected from MAC address: %s' % pkt[Ether].src)
# 定义函数:启动检测线程
def start_detection():
while True:
time.sleep(10)
scan_network()
detect_mac_flood()
detect_dhcp_flood()
detect_cc_attack()
detect_arp_spoofing()
# 定义函数:防御ARP欺骗攻击
def defend_arp_spoofing(ip, mac):
send(ARP(op=2, pdst=ip, hwdst=mac, psrc='192.168.1.1', hwsrc='00:11:22:33:44:55'))
# 定义路由:主页
@app.route('/')
def index():
global attack_type
return render_template('index.html', attack_type=attack_type)
# 定义路由:防御ARP欺骗攻击
@app.route('/defend', methods=['POST'])
def defend():
ip = request.form['ip']
mac = request.form['mac']
defend_arp_spoofing(ip, mac)
logging.info('ARP spoofing attack defended from IP address: %s, MAC address: %s' % (ip, mac))
return 'OK'
if __name__ == '__main__':
# 启动检测线程
detection_thread = threading.Thread(target=start_detection)
detection_thread.setDaemon(True)
detection_thread.start()
# 启动Flask应用
app.run(host='0.0.0.0', port=80)
在上述代码中,首先定义了全局变量 arp_table、ip_list 和 mac_list,分别用于记录局域网内的IP地址和MAC地址,以及ARP缓存表。然后定义了四个函数,分别用于扫描局域网内的活动主机、检测MAC flood攻击、检测DHCP flood攻击、检测CC攻击和检测ARP欺骗攻击。其中,MAC flood攻击的检测使用了MAC地址计数的方法,DHCP flood攻击的检测使用了抓取DHCP报文的方法,CC攻击的检测使用了IP地址和MAC地址的映射关系计数的方法,ARP欺骗攻击的检测使用了ARP报文的监听和ARP缓存表的比对的方法。最后,定义了一个启动检测线程的函数 start_detection,并在主程序中启动了一个Flask应用,用于展示检测结果和提供防御ARP欺骗攻击的功能。在Flask应用中,使用了一个HTML模板 index.html,用于展示检测结果和提供防御ARP欺骗攻击的表单。在防御ARP欺骗攻击的函数 defend_arp_spoofing 中,使用了Scapy库发送了一个伪造的ARP响应报文,将攻击者的MAC地址映射到了网关的IP地址上,从而让攻击者无法继续进行ARP欺骗攻击。最后,使用了Python的日志模块记录了检测结果和防御操作的日志
原文地址: http://www.cveoy.top/t/topic/fmeQ 著作权归作者所有。请勿转载和采集!