import osimport timeimport sysimport platformfrom scapyall import from tkinter import from tkinter import messageboxfrom scapylayersl2 import Etherfrom scapylayersl2 import ARP# 设置界面root = Tkroottitle
要将这个代码修改成Flask框架,需要进行以下步骤:
- 导入必要的Flask库和其他依赖库:
from flask import Flask, render_template, request, redirect, url_for, flash
from scapy.all import *
from scapy.layers.l2 import Ether
from scapy.layers.l2 import ARP
- 创建Flask应用程序:
app = Flask(__name__)
app.secret_key = 'some_secret_key'
- 创建路由和视图函数:
@app.route('/')
def index():
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
with open("user.txt", "a") as f:
f.write(username + " " + 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':
username = request.form['username']
password = request.form['password']
with open("user.txt", "r") as f:
users = f.readlines()
for user in users:
if user.split()[0] == username and user.split()[1] == password:
flash('登录成功!')
return redirect(url_for('func'))
flash('用户名或密码错误!')
return render_template('login.html')
@app.route('/func')
def func():
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.197.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'))
@app.route('/view_detection_log')
def view_detection_log():
with open("detection_log.txt", "r") as f:
detection_log = f.read()
return render_template('detection_log.html', detection_log=detection_log)
- 创建HTML模板文件:
index.html
<!DOCTYPE html>
<html>
<head>
<title>ARP欺骗检测软件</title>
</head>
<body>
<h1>ARP欺骗检测软件</h1>
<p>请选择注册或登录:</p>
<a href="{{ url_for('register') }}">注册</a> | <a href="{{ url_for('login') }}">登录</a>
</body>
</html>
register.html
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
</head>
<body>
<h1>注册</h1>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<p style="color: red">{{ message }}</p>
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="注册">
</form>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<p style="color: red">{{ message }}</p>
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
func.html
<!DOCTYPE html>
<html>
<head>
<title>功能页面</title>
</head>
<body>
<h1>功能页面</h1>
<ul>
<li><a href="{{ url_for('icmp_flood') }}">检测ICMP flood攻击</a></li>
<li><a href="{{ url_for('tcp_attack') }}">检测TCP攻击</a></li>
<li><a href="{{ url_for('arp_spoofing') }}">检测ARP欺骗攻击</a></li>
<li><a href="{{ url_for('detect_attack') }}">检测攻击源</a></li>
<li><a href="{{ url_fo
原文地址: https://www.cveoy.top/t/topic/fstq 著作权归作者所有。请勿转载和采集!