PowerShell Credentials
MITRE ATT&CK™ Unsecured Credentials - Technique T1552
Theory
In enterprise environments, we will often find PowerShell logging mechanisms enabled as Powershell tends to be an attractive attack surface. Although these are defensive measures, we may take advantage of them.
The PowerShell command history, PowerShell transcription, PowerShell script block Logging or XML PSCredential files may contain valuable information such as credentials, configuration settings, sensitive information that may be used as a means of privilege escalation.
PowerShell Transcript: creates a unique record of every PowerShell session, including all input and output, exactly as it appears in the session. The information is stored in transcript files, which are by default written to the user’s documents folders, but can be configured to any accessible location on the local system or on the network.
PowerShell Script Block Logging: captures commands and script blocks as events during execution, significantly expanding the scope of logged information by recording the complete content of executed code and commands. Consequently, each recorded event includes the original representation of encoded code or commands.
PowerShell Command History: enabled by default, from PowerShell v5 on Windows 10, it saves the history of user's PowerShell sessions in a file. This file does not record non-terminal PowerShell sessions (such as WinRM or reverse shells).
PowerShell PSCredentials and SecureString: When interacting with credentials in PowerShell scripts, administrators often use
PSCredential
objects andSecureString
to store sensitive data like usernames and passwords in a more secure format. However, in practice, these protections can be bypassed.
Practice
PowerShell Command History
You should always first check the Command History File before checking other registration mechanisms. It contains user's commands logged by the PSReadline module
We may also check the Get-History
cmdlet, but its contents may be deleted by administrators using Clear-History
. Note that Clear-History
does not clear the command history recorded by PSReadline.
PowerShell Transcription
PowerShell transcripts are automatically named to prevent collisions, with names beginning with PowerShell_transcript
. By default, transcripts are written to the user’s documents folder.
Thus, we can simply check files in user's folder or search for specific filenames on the target computer.
PowerShell Script Block Logging
Script block logging events are recorded using Windows EventID 4104. We may use following command to see them
PowerShell PSCredentials and SecureString
Although a SecureString
is designed to protect secrets in memory, it can be trivially decrypted by any process running under the same user context that originally created it. This is because Windows leverages DPAPI (Data Protection API) to encrypt SecureString
content, tying the encryption keys to the current user's profile. If an attacker gains access to a process or session under that user context, they inherently gain the ability to decrypt any associated SecureString
data.
For example, if a script stores a PSCredential
object in an XML file using Export-Clixml
, the resulting file may look like this:
The <SS N="Password">
field contains a DPAPI-encrypted SecureString
blob. As long as the attacker is operating under the same user context (e.g., via reverse shell, token impersonation, or interactive login), they can fully recover the plaintext password.
Resources
Last updated
Was this helpful?