Access Token Manipulation
|Access Token Manipulation|
|Tactic||Defense Evasion, Privilege Escalation|
|Permissions Required||User, Administrator|
|Data Sources||API monitoring, Access Tokens|
|Contributors||Tom Ueltschi @c_APT_ure, Travis Smith, Tripwire, Jared Atkinson, @jaredcatkinson, Robby Winchester, @robwinchester3|
Windows uses access tokens to determine the ownership of a running process. A user can manipulate access tokens to make a running process appear as though it belongs to someone other than the user that started the process. When this occurs, the process also takes on the security context associated with the new token. For example, Microsoft promotes the use of access tokens as a security best practice. Administrators should log in as a standard user but run their tools with administrator privileges using the built-in access token manipulation command
Adversaries may use access tokens to operate under a different user or system security context to perform actions and evade detection. An adversary can use built-in Windows API functions to copy access tokens from existing processes; this is known as token stealing. An adversary must already be in a privileged user context (i.e. administrator) to steal a token. However, adversaries commonly use token stealing to elevate their security context from the administrator level to the SYSTEM level. An adversary can use a token to authenticate to a remote system as the account for that token if the account has appropriate permissions on the remote system.2
Access tokens can be leveraged by adversaries through three methods:3
Token Impersonation/Theft - An adversary creates a new access token that duplicates an existing token using
DuplicateToken(Ex). The token can then be used with
ImpersonateLoggedOnUser to allow the calling thread to impersonate a logged on user's security context, or with
SetThreadToken to assign the impersonated token to a thread. This is useful for when the target user has a non-network logon session on the system.
Create Process with a Token - An adversary creates a new access token with
DuplicateToken(Ex) and uses it with
CreateProcessWithTokenW to create a new process running under the security context of the impersonated user. This is useful for creating a new process under the security context of a different user.
Make and Impersonate Token - An adversary has a username and password but the user is not logged onto the system. The adversary can then create a logon session for the user using the
LogonUser function. The function will return a copy of the new session's access token and the adversary can use
SetThreadToken to assign the token to a thread.
Any standard user can use the
runas command, and the Windows API functions, to create impersonation tokens; it does not require access to an administrator account.
Metasploit’s Meterpreter payload allows arbitrary token manipulation and uses token impersonation to escalate privileges. 4 The Cobalt Strike beacon payload allows arbitrary token impersonation and can also create tokens. 5
- APT28 has used CVE-2015-1701 to access the SYSTEM token and copy it into the current process as part of privilege escalation.6
- Lazarus Group keylogger KiloAlfa obtains user tokens from interactive sessions to execute itself with API call
CreateProcessAsUserAunder that user's context.7
- Cobalt Strike can steal access tokens from exiting processes and make tokens from known credentials.8
- Duqu examines running system processes for tokens that have specific system privileges. If it finds one, it will copy the token and store it for later use. Eventually it will start new processes with the stored token attached. It can also steal tokens to acquire administrative privileges.9
- SslMM contains a feature to manipulate process privileges and tokens.10
Access tokens are an integral part of the security system within Windows and cannot be turned off. However, an attacker must already have administrator level access on the local system to make full use of this technique; be sure to restrict users and accounts to the least privileges they require to do their job.
Any user can also spoof access tokens if they have legitimate credentials. Follow mitigation guidelines for preventing adversary use of Valid Accounts. Limit permissions so that users and user groups cannot create tokens. This setting should be defined for the local system account only. GPO: Computer Configuration > [Policies] > Windows Settings > Security Settings > Local Policies > User Rights Assignment: Create a token object.11 Also define who can create a process level token to only the local and network service through GPO: Computer Configuration > [Policies] > Windows Settings > Security Settings > Local Policies > User Rights Assignment: Replace a process level token.12
Also limit opportunities for adversaries to increase privileges by limiting Privilege Escalation opportunities.
If an adversary is using a standard command-line shell, analysts can detect token manipulation by auditing command-line activity. Specifically, analysts should look for use of the
runas command. Detailed command-line logging is not enabled by default in Windows.13
If an adversary is using a payload that calls the Windows token APIs directly, analysts can detect token manipulation only through careful analysis of user network activity, examination of running processes, and correlation with other endpoint and network behavior.
There are many Windows API calls a payload can take advantage of to manipulate access tokens (e.g.,
ImpersonateLoggedOnUser16). Please see the referenced Windows API pages for more information.
Query systems for process and thread token information and look for inconsistencies such as user owns processes impersonating the local SYSTEM account.3