CVE-2022-46169 (PHP, published 2022-12-05T21:15Z)
Description: value: Cacti is an open source platform which provides a robust and extensible operational monitoring and fault management framework for users. In affected versions a command injection vulnerability allows an unauthenticated user to execute arbitrary code on a server running Cacti, if a specific data source was selected for any monitored device. The vulnerability resides in the `remote_agent.php` file. This file can be accessed without authentication. This function retrieves the IP address of the client via `get_client_addr` and resolves this IP address to the corresponding hostname via `gethostbyaddr`. After this, it is verified that an entry within the `poller` table exists, where the hostname corresponds to the resolved hostname. If such an entry was found, the function returns `true` and the client is authorized. This authorization can be bypassed due to the implementation of the `get_client_addr` function. The function is defined in the file `lib/functions.php` and checks serval `$_SERVER` variables to determine the IP address of the client. The variables beginning with `HTTP_` can be arbitrarily set by an attacker. Since there is a default entry in the `poller` table with the hostname of the server running Cacti, an attacker can bypass the authentication e.g. by providing the header `Forwarded-For: <TARGETIP>`. This way the function `get_client_addr` returns the IP address of the server running Cacti. The following call to `gethostbyaddr` will resolve this IP address to the hostname of the server, which will pass the `poller` hostname check because of the default entry. After the authorization of the `remote_agent.php` file is bypassed, an attacker can trigger different actions. One of these actions is called `polldata`. The called function `poll_for_data` retrieves a few request parameters and loads the corresponding `poller_item` entries from the database. If the `action` of a `poller_item` equals `POLLER_ACTION_SCRIPT_PHP`, the function `proc_open` is used to execute a PHP script. The attacker-controlled parameter `$poller_id` is retrieved via the function `get_nfilter_request_var`, which allows arbitrary strings. This variable is later inserted into the string passed to `proc_open`, which leads to a command injection vulnerability. By e.g. providing the `poller_id=;id` the `id` command is executed. In order to reach the vulnerable call, the attacker must provide a `host_id` and `local_data_id`, where the `action` of the corresponding `poller_item` is set to `POLLER_ACTION_SCRIPT_PHP`. Both of these ids (`host_id` and `local_data_id`) can easily be bruteforced. The only requirement is that a `poller_item` with an `POLLER_ACTION_SCRIPT_PHP` action exists. This is very likely on a productive instance because this action is added by some predefined templates like `Device - Uptime` or `Device - Polling Time`.

This command injection vulnerability allows an unauthenticated user to execute arbitrary commands if a `poller_item` with the `action` type `POLLER_ACTION_SCRIPT_PHP` (`2`) is configured. The authorization bypass should be prevented by not allowing an attacker to make `get_client_addr` (file `lib/functions.php`) return an arbitrary IP address. This could be done by not honoring the `HTTP_...` `$_SERVER` variables. If these should be kept for compatibility reasons it should at least be prevented to fake the IP address of the server running Cacti. This vulnerability has been addressed in both the 1.2.x and 1.3.x release branches with `1.2.23` being the first release containing the patch.
Severity: nan (CVSS v3 base score: 9.8)
Weakness: CWE-863 \u2014 Incorrect Authorization
The product performs an authorization check when an actor attempts to access a resource or perform an action, but it does not correctly perform the check. This allows attackers to bypass intended access restrictions.
Repository: https://github.com/Cacti/cacti
Files changed:
CHANGELOG
auth_login.php
include/config.php.dist
include/global.php
include/global_arrays.php
lib/auth.php
lib/functions.php
remote_agent.php
Commit message: Merge pull request from GHSA-6p93-p743-35gf

* Resolving  CVE-2022-46169

* Provide a restrictive option

* QA: Further restrict allowed headers to those defined in config only

Co-authored-by: TheWitness <thewitness@cacti.net>

Vulnerable code:
		$client_addr = get_client_addr('');
			array($username, !empty($id) ? $id : 0, get_client_addr(''))
		cacti_log('LOGIN FAILED: ' . $realm_name . " Login Failed for user '" . $username . "' from IP Address '" . get_client_addr('') . "'.", false, 'AUTH');
		$nssecret = md5($_SERVER['REQUEST_TIME'] .  mt_rand(10000,10000000)) . md5(get_client_addr(''));
			array($user['id'], get_client_addr(''), $secret));
						array($user_info['username'], $user_info['id'], get_client_addr(''))
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr('') . "'.  User account Disabled.", false, 'AUTH');
					array($username, isset($user['id']) ? $user['id']:0, get_client_addr('')));
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr('') . "'.  Account is locked out.", false, 'AUTH');
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr('') . "'.", false, 'AUTH');
				cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr('') . "'.", false, 'AUTH');
function get_client_addr($client_addr = false, $restrictive = false) {
	if (!$restrictive) {
		$http_addr_headers = array(
			'X-Forwarded-For',
			'X-Client-IP',
			'X-Real-IP',
			'X-ProxyUser-Ip',
			'CF-Connecting-IP',
			'True-Client-IP',
			'HTTP_X_FORWARDED',
			'HTTP_X_FORWARDED_FOR',
			'HTTP_X_CLUSTER_CLIENT_IP',
			'HTTP_FORWARDED_FOR',
			'HTTP_FORWARDED',
			'HTTP_CLIENT_IP',
			'REMOTE_ADDR',
		);
	} else {
		$http_addr_headers = array(
			'X-Forwarded-For',
			'X-Client-IP',
			'X-Real-IP',
			'X-ProxyUser-Ip',
			'CF-Connecting-IP',
			'True-Client-IP',
			'REMOTE_ADDR',
		);
	foreach ($http_addr_headers as $header) {
	$poller_id      = get_nfilter_request_var('poller_id');
							$cactiphp = proc_open(read_config_option('path_php_binary') . ' -q ' . $config['base_path'] . '/script_server.php realtime ' . $poller_id, $cactides, $pipes);
	$poller_id = $config['poller_id'];
	$network   = get_filter_request_var('network');

Fixed code:
-security#XXXX: CVE-2022-46169 Unauthenticated Command Injection in Remote Agent
		$client_addr = get_client_addr();
			array($username, !empty($id) ? $id : 0, get_client_addr())
		cacti_log('LOGIN FAILED: ' . $realm_name . " Login Failed for user '" . $username . "' from IP Address '" . get_client_addr() . "'.", false, 'AUTH');

/*
 * Allow the use of Proxy IPs when searching for client
 * IP to be used
 *
 * This can be set to one of the following:
 *   - false: to use only REMOTE_ADDR
 *   - true: to use all allowed headers (not advised)
 *   - array of one or more the following:
 *		'X-Forwarded-For',
 *		'X-Client-IP',
 *		'X-Real-IP',
 *		'X-ProxyUser-Ip',
 *		'CF-Connecting-IP',
 *		'True-Client-IP',
 *		'HTTP_X_FORWARDED',
 *		'HTTP_X_FORWARDED_FOR',
 *		'HTTP_X_CLUSTER_CLIENT_IP',
 *		'HTTP_FORWARDED_FOR',
 *		'HTTP_FORWARDED',
 *		'HTTP_CLIENT_IP',
 *
 * NOTE: The following will always be checked:
 *		'REMOTE_ADDR',
 */
$proxy_headers = null;
/* Should we allow proxy ip headers? */
$config['proxy_headers'] = $proxy_headers ?? null;

$allowed_proxy_headers =	array(
	'X-Forwarded-For',
	'X-Client-IP',
	'X-Real-IP',
	'X-ProxyUser-Ip',
	'CF-Connecting-IP',
	'True-Client-IP',
	'HTTP_X_FORWARDED',
	'HTTP_X_FORWARDED_FOR',
	'HTTP_X_CLUSTER_CLIENT_IP',
	'HTTP_FORWARDED_FOR',
	'HTTP_FORWARDED',
	'HTTP_CLIENT_IP',
	'REMOTE_ADDR',
);

		$nssecret = md5($_SERVER['REQUEST_TIME'] .  mt_rand(10000,10000000)) . md5(get_client_addr());
			array($user['id'], get_client_addr(), $secret));
						array($user_info['username'], $user_info['id'], get_client_addr())
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr() . "'.  User account Disabled.", false, 'AUTH');
					array($username, isset($user['id']) ? $user['id']:0, get_client_addr()));
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr() . "'.  Account is locked out.", false, 'AUTH');
					cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr() . "'.", false, 'AUTH');
				cacti_log("LOGIN FAILED: Local Login Failed for user '" . $username . "' from IP Address '" . get_client_addr() . "'.", false, 'AUTH');
function get_client_addr() {
	global $config, $allowed_proxy_headers;

	$proxy_headers = $config['proxy_headers'] ?? null;
	if ($proxy_headers === null) {
		$last_time = read_config_option('proxy_alert');
		if (empty($last_time)) {
			$last_time = date('Y-m-d');
		}

		$last_date = new DateTime($last_time);
		$this_date = new Datetime();

		$this_diff = $this_date->diff($last_date);
		$this_days = $this_diff->format('%a');

		if ($this_days) {
			cacti_log('WARNING: Configuration option "proxy_headers" will be automatically false in future releases.  Please set if you require proxy IPs to be used', false, 'AUTH');
			set_config_option('proxy_alert', date('Y-m-d'));
		}

		$proxy_headers = true;
	}

	/* If proxy_headers is true, allow all known headers -- NOT advised
	 * If proxy_headers is false, allow only REMOTE_ADDR
	 * IF proxy_headers is an array, filter by known headers
	 */
	if ($proxy_headers === true) {
		$proxy_headers = $allowed_proxy_headers;
	} elseif (is_array($proxy_headers)) {
		$proxy_headers = array_intersect($proxy_headers, $allowed_proxy_headers);
	}

	if (!is_array($proxy_headers)) {
		$proxy_headers = [];
	}

	if (!in_array('REMOTE_ADDR', $proxy_headers)) {
		$proxy_headers[] = 'REMOTE_ADDR';
	foreach ($proxy_headers as $header) {
	$poller_id      = get_filter_request_var('poller_id');
							$cactiphp = proc_open(read_config_option('path_php_binary') . ' -q ' . $config['base_path'] . '/script_server.php realtime ' . cacti_escapeshellarg($poller_id), $cactides, $pipes);
	$poller_id = cacti_escapeshellarg($config['poller_id']);
	$network   = cacti_escapeshellarg(get_filter_request_var('network'));