Headline
CVE-2022-46169: Unauthenticated Command Injection
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.
Summary
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.
Details
The vulnerability resides in the remote_agent.php file. This file can be accessed without authentication. In order to verify that the client is allowed the function remote_client_authorized is called:
if (!remote_client_authorized()) { print 'FATAL: You are not authorized to use this service’; exit; }
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:
function remote_client_authorized() { // … $client_addr = get_client_addr(); // …
$client\_name = gethostbyaddr($client\_addr);
// ...
$pollers = db\_fetch\_assoc('SELECT \* FROM poller', true, $poller\_db\_cnn\_id);
if (cacti\_sizeof($pollers)) {
foreach($pollers as $poller) {
if (remote\_agent\_strip\_domain($poller\['hostname'\]) == $client\_name) {
return true;
} elseif ($poller\['hostname'\] == $client\_addr) {
return true;
}
}
}
cacti\_log("Unauthorized remote agent access attempt from $client\_name ($client\_addr)");
return false;
}
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:
function get_client_addr($client_addr = false) { $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’, );
$client\_addr = false;
foreach ($http\_addr\_headers as $header) {
if (!empty($\_SERVER\[$header\])) {
$header\_ips = explode(',', $\_SERVER\[$header\]);
foreach ($header\_ips as $header\_ip) {
if (!empty($header\_ip)) {
if (!filter\_var($header\_ip, FILTER\_VALIDATE\_IP)) {
cacti\_log('ERROR: Invalid remote client IP Address found in header (' . $header . ').', false, 'AUTH', POLLER\_VERBOSITY\_DEBUG);
} else {
$client\_addr = $header\_ip;
cacti\_log('DEBUG: Using remote client IP Address found in header (' . $header . '): ' . $client\_addr . ' (' . $\_SERVER\[$header\] . ')', false, 'AUTH', POLLER\_VERBOSITY\_DEBUG);
break 2;
}
}
}
}
}
return $client\_addr;
}
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:
switch (get_request_var(‘action’)) { case 'polldata’: // … poll_for_data(); // … break;
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:
function poll_for_data() { global $config;
$local\_data\_ids = get\_nfilter\_request\_var('local\_data\_ids');
$host\_id = get\_filter\_request\_var('host\_id');
$poller\_id = get\_nfilter\_request\_var('poller\_id');
$return = array();
$i = 0;
if (cacti\_sizeof($local\_data\_ids)) {
foreach($local\_data\_ids as $local\_data\_id) {
input\_validate\_input\_number($local\_data\_id, 'local\_data\_id');
$items = db\_fetch\_assoc\_prepared('SELECT \*
FROM poller\_item
WHERE host\_id = ?
AND local\_data\_id = ?',
array($host\_id, $local\_data\_id));
// ...
if (cacti\_sizeof($items)) {
foreach($items as $item) {
switch ($item\['action'\]) {
// ...
case POLLER\_ACTION\_SCRIPT\_PHP: /\* script (php script server) \*/
// ...
$cactiphp = proc\_open(read\_config\_option('path\_php\_binary') . ' -q ' . $config\['base\_path'\] . '/script\_server.php realtime ' . $poller\_id, $cactides, $pipes);
// ...
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.
Impact
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.
Remediation
The following suggestions should be applied to prevent the described vulnerability.
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.
The command injection should be prevented by applying the following changes to the file remote_agent.php:
The variable $poller_id is supposed to be an integer and should thus be retrieved via the function get_filter_request_var instead of get_nfilter_request_var:
function poll_for_data() { // … $poller_id = get_filter_request_var(‘poller_id’); // …
For further hardening against command injections the $poller_id should be escaped with escapeshellarg before being passed to proc_open:
function poll_for_data() { // … $cactiphp = proc_open(read_config_option(‘path_php_binary’) . ' -q ' . $config[‘base_path’] . '/script_server.php realtime ' . escapeshellarg($poller_id), $cactides, $pipes); // …
Patches
Version
Patches
1.2.x
7f0e163
1.3.x
b43f13a
For instances of 1.2.x running under PHP < 7.0, a further change a8d59e8 is also required.
Related news
Critical security flaws in Cacti, Realtek, and IBM Aspera Faspex are being exploited by various threat actors in hacks targeting unpatched systems. This entails the abuse of CVE-2022-46169 (CVSS score: 9.8) and CVE-2021-35394 (CVSS score: 9.8) to deliver MooBot and ShellBot (aka PerlBot), Fortinet FortiGuard Labs said in a report published this week. CVE-2022-46169 relates to a critical
Cacti version 1.2.22 suffers from a remote command execution vulnerability.
This Metasploit module exploits an unauthenticated command injection vulnerability in Cacti versions through 1.2.22 in order to achieve unauthenticated remote code execution as the www-data user.
A majority of internet-exposed Cacti servers have not been patched against a recently patched critical security vulnerability that has come under active exploitation in the wild. That's according to attack surface management platform Censys, which found only 26 out of a total of 6,427 servers to be running a patched version of Cacti (1.2.23 and 1.3.0). The issue in question relates to
Debian Linux Security Advisory 5298-1 - Two security vulnerabilities have been discovered in Cacti, a web interface for graphing of monitoring systems, which could result in unauthenticated command injection or LDAP authentication bypass.