2. Windows Exploitation
Last updated
Last updated
Living Off the Land Binaries And Scripts
Uses Microsoft-signed and built-in tools used as Living Off the Land techniques, including binaries, scripts, and libraries.
C:\Windows\explorer.exe
C:\Windows\SysWOW64\explorer.exe
C:\Windows\System32\rundll32.exe
C:\Windows\SysWOW64\rundll32.exe
regsvr32.exe binary can also be used to execute arbitrary binaries and bypass the Windows Application Whitelisting
C:\Windows\System32\regsvr32.exe
C:\Windows\SysWOW64\regsvr32.exe
If the machine has WSL installed, we can execute bash.exe -c "path-to-payload"
to run any unsigned payload.
PowerLessShell is a Python-based tool that generates malicious code to run on a target machine without showing an instance of the PowerShell process. PowerLessShell relies on abusing the Microsoft Build Engine (MSBuild), a platform for building Windows applications, to execute remote code.
If we can execute command with admin privileges, we can use msfvenom
to generate a reverse shell, upload it to the victim and execute it.
This reverse shell can be caught using netcat
or metasploit’s multi/handler
If RDP is available we can add a low privileged user to the Administrator
group and spawn an admin prompt via the GUI
To escalate from Administator
user to SYSTEM
user, we can use PsExec
tool from Windows Sysinternals
Like Linux, Windows files has permissions. We can check them with icacls
F - Full access
M Modify access
RX - Read and execute access
R - Read-only access
W - Write-only access
Allows creating non administrator accounts and groups on the domain
Allows logging in to the DC locally
This membership allows users to configure Domain Controllers with the following privileges:
Allows logging in to the DC locally
Back up files and directories
Change the system time
Change the time zone
Force shutdown from a remote system
Restore files and directories
Shut down the system
As with Server Operators
membership, we can access the DC01
file system if we belong to Backup Operators
Checking your privileges
The SeBackup
and SeRestore
privileges allow users to read and write to any file in the system, ignoring any DACL in place.
To backup the SAM and SYSTEM hashes
Dump the admin hash with impacket
PTH with impacket
The SeTakeOwnership
privilege allows a user to take ownership of any object on the system, including files and registry keys
Being the owner of a file doesn't necessarily mean that you have privileges over it, but being the owner you can assign yourself any privileges you need. To give your user full permissions over utilman.exe you can use the following command:
After this, we will replace utilman.exe with a copy of cmd.exe:
Trigger utilman
by locking the screen, and clicking on Ease of Access
These privileges allow a process to impersonate other users and act on their behalf. Impersonation usually consists of being able to spawn a process or thread under the security context of another user.
If we manage to take control of a process with SeImpersonate
or SeAssignPrimaryToken
privileges, we can impersonate any user connecting and authenticating to that process.
LOCAL SERVICE
and NETWORK SERVICE ACCOUNTS
already have such privileges.
Spawn a process so that users can connect and authenticate to it for impersonation to occur.
Find a way to force privileged users to connect and authenticate to the spawned malicious process.
Use RogueWinRM
exploit to accomplish both conditions.
If WinRM service isn't running on the victim server, an attacker can start a fake WinRM service on port 5985 and catch the authentication attempt made by the BITS service when starting. If the connecting account has SeImpersonate
privileges, he can execute any command on behalf of the connecting user, which is SYSTEM.
Starting RogueWinRM
If services they are being run with SYSTEM
level privileges and are misconfigured, we can try to exploit them
Useful permissions
SERVICE_QUERY_CONFIG
SERVICE_QUERY_STATUS
SERVICE_STOP
SERVICE_START
Dangerous Permissions
SERVICE_CHANGE_CONFIG
If the service is running as SYSTEM
and you can change the config, you can change the executable the service uses to our own
HOWEVER, you must be able to restart the service with SERVICE_STOP
/ SERVICE_START
If not you have to restart the entire machine, but that could reset the exploit
SERVICE_ALL_ACCESS
Example
When we run winPEAS, we see that daclsvc
is modifiable
Running checks on it, we see that it runs as system, and has a Binary path
We can therefore change the binary path to our own executable, and restart the service to run our payload as system
Executables in windows can be run without their extensions, e.g. whoami.exe
can be executed with whoami
This leads to ambiguity when using absolute paths that are unquoted and contain spaces
Example
This could be interpreted as
C:\\Program Files\\Some Dir\\SomProg.exe
with no arguments
C:\\Program.exe
with File\\Some
and Dir\\SomeProg.exe
as arguments.
C:\\Program File\\Some.exe
with Dir\\SomeProg.exe
as arguments
Windows resolves this ambiguity by checking all possible combinations
To exploit this, we can create a file called C:\\Program.exe
that takes in two inputs, and this command will execute our payload
Windows Registry stores entries for each service
If the Registry entries have misconfigured ACLs, we can change the service configurations without directly changing the service
Example
winPEAS shows that we can change the registry of a service called regsvc
We then run reg query
to see what values we can change in the registry, and see that we can change the ImagePath
to point to our payload.
ObjectName=LocalSystem
also tells us that the service should execute with system level privileges.
We modify the registry for ImagePath
as such, and start the service
If the binary used by the service is modifiable by the user, we can simply replace it with our our payload to be executed
Example
filepermsvc
service has an executable that can be modified by anyone.
If we replace filepermservice.exe
with our own payload and start the service, we can execute our payload
If a DLL is loaded with an absolute path, we can escalate privileges if that DLL is modifiable
If a DLL is missing from the system, and we can write to the PATH that Windows searches for to load libraries, we can load our own library
Example
We see that C:\\Temp
directory is writable, and it’s also in the PATH
variable. This means that Windows will search for libraries present in C:\\Temp
and load them if it’s there.
We then need to find a service that we can stop/start, find what libraries the service is using, and create a payload with the same name in C:/Temp
In this example, dllsvc
service executes dllhijackservice.exe
We then need to analyze what libraries dllhijackservice.exe
is loaded when it’s executed. We can do this with Procmon.exe
Now when we execute dllhijackservice.exe
, we see a number NAME NOT FOUND
associated with hijackme.dll
. On the last 4th line, we can see it check in the directory C:\\Temp
, which know it’s writable from before.
Now all we need to do create our payload C:\\Temp\\hijack.dll
, and our payload will be executed.
Windows can be configured to run commands at startup with elevated privileges
These AutoRuns are configured in the Registry
If we can write to an AutoRun executable and restart the system, we can escalate privileges
Example
Running winPEAS, we see that C:\\Program File\\Autorun Program\\program.exe
is writable by everyone. we can overwrite this file with our payload, restart the system to execute it.
MSI files are package files used to install applications
MSI files run with permissions of the user executing it
Windows allows these installers to be run with elevated privileges with a prompt
For this to happen, two AlwaysInstallElevated
needs to be set to 1 in
HKLM\\SOFTWARE\\Policies\\Microsoft\\Windows\\Installer
HKCU\\SOFTWARE\\Policies\\Microsoft\\Windows\\Installer
We can generate a malicious MSI file containing our payload using msfvenom
When running winPEAS, we see that its one of the things thats being checked
Like Linux cronjobs, Windows can be configured to run specific tasks periodically, or triggered by a certain event
Tasks usually run at the privilege level of the user that created it, but administrators can configure them to run as other users, including SYSTEM
If we can modify these scripts, we can run commands as admin
Users can be granted permissions to run some GUI apps with admin privileges
Using these GUI apps, we may spawn command prompts which will inherit these admin privileges
Example
Check if the app is running as admin using the command
We see that mspaint is running as admin. We then navigate to mspaint and open up a command prompt. We do this by going to File -> Open -> file://c:/windows/system32/cmd.exe
Triggers in MSSQL allow you to bind actions to be performed when specific events occur in the database.
Before creating the trigger, we must first reconfigure a few things on the database. First, we need to enable the xp_cmdshell
stored procedure. xp_cmdshell
is a stored procedure that is provided by default in any MSSQL installation and allows you to run commands directly in the system's console but comes disabled by default.
Enabling xp_cmdshell
Because only the system administrator can run xp_shell
, we need to configure all connections to impersonate the system administrator account.
Configure the trigger on the database
The xp_cmdshell
will download evilscript.ps
from our server and execute it.