Skip to content

Lesson 12 of 13 · 16m

Secrets Without Plaintext

Export-CliXml binds credentials to user and machine via DPAPI, gMSA removes stored passwords entirely, and JEA shrinks what accounts can do.

The September 2022 Uber breach ran on a PowerShell script. The attacker bought contractor VPN creds, got in, scanned the network, and found a share containing a .ps1 with hard-coded admin credentials for the PAM vault — which contained everything else. One file, plaintext, on a readable share. Every automation environment produces the same object the moment someone writes ConvertTo-SecureString 'P@ssw0rd!' -AsPlainText -Force and saves it next to the script. That cmdlet encrypts nothing — AsPlainText is in the name. It builds a SecureString wrapper around a secret that is sitting right there in your source, grep-able, indexed, committed to git.

PSCredential and what actually protects it

Get-Credential prompts; New-Object PSCredential('user', $secure) builds from an existing SecureString. The object travels through APIs that want it — -Credential on Invoke-Command, New-PSSession, Start-Job, scheduled-task registration — without the password ever becoming a command-line string.

The persistence mechanism everyone reaches for is Export-CliXml: $cred | Export-CliXml C:\ops\svc.cred. It serializes the credential with the password encrypted by DPAPI — and DPAPI scope is the catch that generates a thousand forum posts. The file decrypts only for the same user on the same machine. Copy it to another server: Import-CliXml fails with a decrypt error. Run it under a different account on the same box: fails. So the export must happen as the account that will run the task — which is why the standard recipe is "log on as the service account once, or run the export inside a task running as that account." Nobody warns you about this until your credential file is a useless blob on a different host.

Read-Host -AsSecureString is the human-input path. ConvertFrom-SecureString -Key (1..16) encrypts with an explicit AES key instead of DPAPI — portable across machines, and now you have a key-management problem instead of a credential problem, which is only progress if the key lives somewhere better than the script.

The vault pattern

Microsoft.PowerShell.SecretManagement is the abstraction layer: Set-Secret, Get-Secret, Register-SecretVault against backends — the built-in Microsoft.PowerShell.SecretStore (a local encrypted store; Set-SecretStoreConfiguration -Authentication Password or None controls unlocking), KeePass via SecretManagement.KeePass, Azure Key Vault via Az.KeyVault extension, HashiCorp Vault via community modules. Automation calls Get-Secret -Name svc-report -AsPlainText at runtime and the plaintext exists for the duration of one variable's lifetime, never on disk. The SecretStore None authentication variant is the scheduled-task answer — it unlocks without a password, and the honest accounting is that None takes DPAPI out of the picture entirely: the store's own encryption key is recoverable from the store files by anyone who can read them, so the boundary is just the NTFS ACLs on the profile directory — fine as long as the store lives under the service account's profile and nothing else runs as that account. (The only DPAPI in this pattern is the Export-CliXml'd vault-password file some setups use to unlock the Password variant unattended.)

gMSA: let AD hold the secret

For services and tasks on domain machines, a group Managed Service Account deletes the credential entirely. New-ADServiceAccount -Name svc-tasks -DNSHostName svc-tasks.contoso.com -PrincipalsAllowedToRetrieveManagedPassword 'TaskServers$' plus Install-ADServiceAccount on each host, then the scheduled task principal is 'CONTOSO\svc-tasks$' with -LogonType Password and no stored password — the OS fetches the current secret from AD, and AD rotates it on a 30-day cycle. (The KDS root key must exist first: Add-KdsRootKey -EffectiveTime ((Get-Date).AddHours(-10)) in prod — the immediate-effective variant exists for labs because key propagation takes hours.) gMSA is the answer to "the task needs a network-reachable identity and I refuse to store a password," which is most scheduled automation in a domain.

JEA: shrink what the credential can do

Just Enough Administration answers the harder question: not "where does the password live" but "why does the account have rights at all." A JEA endpoint is a .pssc session-configuration file plus .psrc role-capability files: New-PSRoleCapabilityFile -VisibleCmdlets 'Restart-Service','Get-Service' -VisibleFunctions ..., New-PSSessionConfigurationFile -SessionType RestrictedRemoteServer -RunAsVirtualAccount -RoleDefinitions @{...}, then Register-PSSessionConfiguration creates the endpoint on 5985/5986. A helpdesk user connects to it — Enter-PSSession -ComputerName web01 -ConfigurationName HelpdeskOps — and inside runs only the whitelisted commands, executing as a local virtual admin account. No password is shared, no domain admin is borrowed, and the endpoint logs every command to a transcript directory you specify. The double-hop resolves itself: the virtual account's onward hops run as the machine account, which you can scope on the far side.

The practice

No secret lives in a .ps1, a task argument, or a CSV — grep your own repo for -AsPlainText and password before the next pentester does. Interactive: Get-Credential. Script-on-one-box: Export-CliXml created as the run account, or SecretStore under that account's profile. Domain automation: gMSA. Cross-team privilege: JEA. The credential file copied to a second machine is not a deployment mechanism — it's a decrypt error and an audit finding.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.