Flask 网络攻击检测系统 - 基于Scapy的ICMP、TCP、ARP攻击检测与防御
Flask 网络攻击检测系统 - 基于 Scapy 的 ICMP、TCP、ARP 攻击检测与防御
本项目使用 Flask 框架和 Scapy 库实现了一个简单的网络攻击检测系统,支持检测 ICMP Flood、TCP 攻击、ARP 欺骗等常见攻击方式,并提供攻击源 IP 和 MAC 地址的识别功能。
项目功能:
- 用户登录与注册: 使用简单的用户认证机制,用户需要注册并登录才能访问攻击检测功能。
- ICMP Flood 攻击检测: 监控网络中的 ICMP 数据包,当 ICMP 数据包数量超过阈值时,系统将提示检测到 ICMP Flood 攻击。
- TCP 攻击检测: 监控网络中的 TCP 数据包,当 TCP 数据包数量超过阈值时,系统将提示检测到 TCP 攻击。
- ARP 欺骗攻击检测: 监控网络中的 ARP 数据包,当 ARP 数据包数量超过阈值时,系统将提示检测到 ARP 欺骗攻击。
- 攻击源识别: 系统会记录网络中的客户端 IP 和 MAC 地址,并根据攻击检测结果识别攻击源 IP 和 MAC 地址。
- 网络扫描: 系统可以扫描指定的网络段,识别网络中的所有设备,并记录其 IP 和 MAC 地址。
- 攻击日志: 系统会记录所有攻击检测结果,方便用户查看和分析。
代码结构:
from flask import Flask, render_template, request, redirect, url_for, jsonify
import os
import time
import sys
import platform
from scapy.all import *
app = Flask(__name__,template_folder='templates')
app.secret_key = 'secret_key'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
with open('user.txt', 'a') as f:
f.write(request.form['username'] + ' ' + request.form['password'] + '\n')
flash('注册成功!')
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
with open('user.txt', 'r') as f:
users = f.readlines()
for user in users:
if user.split()[0] == request.form['username'] and user.split()[1] == request.form['password']:
session['username'] = request.form['username']
flash('登录成功!')
return redirect(url_for('func.html'))
flash('用户名或密码错误!')
return render_template('login.html')
@app.route('/func')
def func():
if 'username' not in session:
return redirect(url_for('login'))
return render_template('func.html')
@app.route('/icmp_flood')
def icmp_flood():
sniff_filter = 'icmp'
sniff_timeout = 10
sniff_count = 0
sniff_packets = sniff(filter=sniff_filter, timeout=sniff_timeout)
for packet in sniff_packets:
sniff_count += 1
if sniff_count > 100:
flash('检测到ICMP flood攻击!')
with open('detection_log.txt', 'a') as f:
f.write('ICMP flood攻击检测 - 检测结果:检测到ICMP flood攻击\n')
else:
flash('未检测到ICMP flood攻击!')
with open('detection_log.txt', 'a') as f:
f.write('ICMP flood攻击检测 - 检测结果:未检测到ICMP flood攻击\n')
return redirect(url_for('func'))
@app.route('/tcp_attack')
def tcp_attack():
sniff_filter = 'tcp'
sniff_timeout = 10
sniff_count = 0
sniff_packets = sniff(filter=sniff_filter, timeout=sniff_timeout)
for packet in sniff_packets:
sniff_count += 1
if sniff_count > 100:
flash('检测到TCP攻击!')
with open('detection_log.txt', 'a') as f:
f.write('TCP攻击检测 - 检测结果:检测到TCP攻击\n')
else:
flash('未检测到TCP攻击!')
with open('detection_log.txt', 'a') as f:
f.write('TCP攻击检测 - 检测结果:未检测到TCP攻击\n')
return redirect(url_for('func'))
@app.route('/arp_spoofing')
def arp_spoofing():
sniff_filter = 'arp'
sniff_timeout = 10
sniff_count = 0
sniff_packets = sniff(filter=sniff_filter, timeout=sniff_timeout)
for packet in sniff_packets:
sniff_count += 1
if sniff_count > 100:
flash('检测到ARP欺骗攻击!')
with open('detection_log.txt', 'a') as f:
f.write('ARP欺骗攻击检测 - 检测结果:检测到ARP欺骗攻击\n')
else:
flash('未检测到ARP欺骗攻击!')
with open('detection_log.txt', 'a') as f:
f.write('ARP欺骗攻击检测 - 检测结果:未检测到ARP欺骗攻击\n')
return redirect(url_for('func'))
@app.route('/detect_attack')
def detect_attack():
with open('clients.txt', 'r') as f:
clients = f.readlines()
for client in clients:
ip = client.split()[0]
mac = client.split()[1]
sniff_filter = 'arp and src host ' + ip
sniff_timeout = 10
sniff_count = 0
sniff_packets = sniff(filter=sniff_filter, timeout=sniff_timeout)
for packet in sniff_packets:
sniff_count += 1
if sniff_count > 100:
flash('检测到攻击源IP地址为' + ip + ',MAC地址为' + mac + '!')
with open('detection_log.txt', 'a') as f:
f.write('警告' + '检测到攻击源IP地址为' + ip + ',MAC地址为' + mac + '!\n')
else:
flash('未检测到攻击源!')
with open('detection_log.txt', 'a') as f:
f.write('提示' + '未检测到攻击源!\n')
return redirect(url_for('func'))
@app.route('/scan_network')
def scan_network():
network = '192.168.31.0/24'
arp_request = ARP(pdst=network)
broadcast = Ether(dst='ff:ff:ff:ff:ff:ff')
arp_broadcast = broadcast/arp_request
answered_list = srp(arp_broadcast, timeout=1, verbose=False)[0]
clients = []
for element in answered_list:
client = {'ip': element[1].psrc, 'mac': element[1].hwsrc}
clients.append(client)
with open('clients.txt', 'w') as f:
for client in clients:
f.write(client['ip'] + ' ' + client['mac'] + '\n')
flash('扫描完成!')
return redirect(url_for('func.html'))
@app.route('/view_detection_log')
def view_detection_log():
with open('detection_log.txt', 'r') as f:
detection_log = f.read()
flash(detection_log)
return redirect(url_for('func.html'))
if __name__ == '__main__':
app.run(debug=True)
使用方法:
- 安装依赖库:
pip install flask scapy - 运行程序:
python app.py - 访问 http://127.0.0.1:5000/ 注册用户并登录
- 点击攻击检测按钮,系统会开始监控网络数据包,并在检测到攻击时提示用户。
- 点击网络扫描按钮,系统会扫描指定的网络段,识别网络中的所有设备。
- 点击查看攻击日志按钮,可以查看系统记录的攻击检测结果。
注意事项:
- 本项目只是一个简单的网络攻击检测系统,可能无法检测所有类型的攻击。
- 系统需要管理员权限才能运行,因为需要访问网络数据包。
- 系统可能会占用一定的系统资源,尤其是在扫描网络时。
未来改进方向:
- 添加更多攻击检测规则,支持更多类型的攻击。
- 实现自动防御机制,例如在检测到攻击时自动封锁攻击源 IP。
- 添加用户界面,方便用户管理系统和查看检测结果。
版权声明:
本项目仅供学习交流使用,禁止用于任何非法活动。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/jocv 著作权归作者所有。请勿转载和采集!