Invoke-Totem is a PowerShell script that allows obtaining and impersonating the access token of another process on the system, executing commands under the context of the associated user. This can be useful in post-exploitation scenarios, penetration testing, restricted network environments, or for developers interested in understanding the Windows security model.
- Open a remote process and obtain its token.
- Safely duplicate the token using native Windows API calls.
- Impersonate the target user context.
- List all processes with impersonation capability and their owners.
- Function to check the current user (
whoami). - Function to revert impersonation.
- PowerShell 5.0 or higher.
- Administrator privileges.
- Access to the target process (usually requires elevated privileges).
- The target process must have an accessible token (e.g., a process from another logged-in user).
When running the script, it is recommended to open PowerShell with the -STA flag to ensure that the script runs in a single thread:
powershell.exe -STA
. .\Invoke-Totem.ps1Invoke-TotemListThis will display a table showing all processes with:
- PID: Process ID
- ProcessName: Name of the process
- CanImpersonate: Whether the token can be impersonated
- Owner: The user who owns the process
Get-WmiObject Win32_Process | Select-Object Name, ProcessId, @{Name='User';Expression={
(Invoke-Command -ScriptBlock {
$temp = $_.GetOwner()
"$($temp.Domain)\$($temp.User)"
} -ArgumentList $_)
}} | Format-Table -AutoSizeGet-CimInstance Win32_Process | ForEach-Object {
$user = ($_ | Invoke-CimMethod -MethodName GetOwner)
[PSCustomObject]@{
Name = $_.Name
PID = $_.ProcessId
User = if ($user) { "$($user.Domain)\$($user.User)" } else { "SYSTEM" }
}
} | Format-Table -AutoSize
Invoke-Totem -processID <PID>Example:
Invoke-Totem -processID 1234This will impersonate the user who owns process 1234.
Invoke-TotemWhoamiInvoke-TotemResetThe script performs the following steps:
- Opens the target process using
OpenProcess. - Obtains its access token using
OpenProcessToken. - Duplicates the token using
DuplicateTokenEx. - Impersonates the context with
ImpersonateLoggedOnUser. - Restores the original token with
RevertToSelfwhen needed.
All calls are made via P/Invoke with Add-Type.
Invoke-Totem: Main function to impersonate a token.Invoke-TotemList: Lists all processes with their impersonation capability and owners.Invoke-TotemWhoami: Displays the currently impersonated user.Invoke-TotemReset: Reverts to the original user context.
Developed by Shac0x_
Inspired by impersonation techniques in Windows environments documented by the offensive security community.
Use this tool responsibly. Do not use it for illegal activities. The author is not responsible for any misuse.

