Threat Level: green Handler on Duty: Didier Stevens

SANS ISC: Did You Spot "Invoke-Expression"? - SANS Internet Storm Center SANS ISC InfoSec Forums

Watch ISC TV. Great for NOCs, SOCs and Living Rooms:

Sign Up for Free!   Forgot Password?
Log In or Sign Up for Free!
Did You Spot "Invoke-Expression"?

When a PowerShell script is obfuscated, the deobfuscation process is, most of the time, performed through the Invoke-Expression cmdlet[1]. Invoke-Expression evaluates the string passed as an argument and returns the results of the commands inside the string. Example:

PS C:\Users\xavier> $a="1+1"
PS C:\Users\xavier> Invoke-Expression $a
PS C:\Users\xavier> $a="(Invoke-WebRequest '').Content"
PS C:\Users\xavier> Invoke-Expression $a
<?xml version="1.0" encoding="UTF-8"?>
<name>Xavier Mertens</name>

Here is another version of the previous example now obfuscated and handled via Invoke-Expression:`

PS C:\Users\xavier> $a="(Invoke-WebRequest ('hXtXtXpXsX:X/X/XiXsXcX.XsXaXnXsX.XeXdXuX/XaXpXiX/XhXaXnXdXlXeXr'-replace([char]88,''))).Content"
PS C:\Users\xavier> Invoke-Expression $a
<?xml version="1.0" encoding="UTF-8"?>
<name>Xavier Mertens</name>

You understand now that the presence of Invoke-Expression in a PowerShell script can be an interesting indicator of malicious activity. You can roughly compare Invoke-Expression to eval() in JavaScript or exec() in Python and, as I like to say, eval() is evil. If Invoke-Expression is used to deobfuscate some code, it is a common string to search for. Guess what? Attackers are trying to hide the use of this cmdlet by implementing more obfuscation. Here is a list of common obfuscation tricks that I spotted while hunting for malicious PowerShell.

One of the PowerShell features is the use of compressed or abbreviated cmdlet names. Instead of using the full name, 'Invoke-Expression' is most of the time replaced by 'IEX'. This three-characters string is then replaced by something more unreadable.

Example 1: Some characters are replaced:


Example 2: Concatenation of characters, some of them extracted from a specific position in another string. $PSHome = 'C:\Windows\System32\WindowsPowerShell\v1.0'.


Example 3: Back quote pollution (simply ignored by PowerShell)


Example 4: Extraction of characters from a string with a 'join':

( $VERBOSePRefereNCe.toSTRiNG()[1,3]+'X'-join'')

Example 5: More character extraction. $env:ComSpec = 'C:\WINDOWS\system32\cmd.exe'


When having a look at the suspicious script, the first goal is to try to spot the presence of this Invoke-Expression. Once found, a quick and dirty debugging technique is to replace the 'iex' occurrence with a simple 'echo' to get access to the deobfuscated code!

The number of combinations is almost infinite but that's the ones that I spot most frequently. Did you spot other techniques? Feel free to share them!


Xavier Mertens (@xme)
Senior ISC Handler - Freelance Cyber Security Consultant

I will be teaching next: Reverse-Engineering Malware: Malware Analysis Tools and Techniques - SANS Amsterdam August 2022


697 Posts
ISC Handler
Nov 5th 2020
There are endless possibilities:

$cc = 'write-host "nice"'
.('i'+'e'+'x') $cc
&('i'+'e'+'x') $cc
.(('{2}{1}{0}' -f 'x', 'e', 'i')) $cc

$box = 'ëçú'.ToCharArray()
for ($i=0; $i -lt $box.Length; $i++) {$box[$i] = $box[$i] -bxor 0x82 }
.(-join($box)) $cc
Thanks for sharing, I like them!

697 Posts
ISC Handler
On more

$cc = 'Write-host "nice"'
sal qwe iex
$cc | qwe
qwe $cc

Sign Up for Free or Log In to start participating in the conversation!