PowerShell Obfuscation via Backtick-Escaped Variable Expansion
PowerShell scripts use backtick-escaped characters inside `${}` variable expansion to reconstruct strings at runtime, enabling attackers to split keywords, hide commands, and evade static analysis and AMSI.
This rule detects PowerShell scripts employing backtick-escaped characters within ${} variable expansion, a technique used to reconstruct strings at runtime. Attackers leverage variable-expansion obfuscation to split keywords, conceal commands, and bypass static analysis and AMSI (Antimalware Scan Interface). This obfuscation method involves inserting multiple backticks between word characters inside ${} blocks. Detecting this behavior is crucial as it signifies attempts to evade security measures and potentially execute malicious code on compromised systems. The rule focuses on identifying scripts with a length exceeding 500 characters to minimize false positives and targets PowerShell event code 4104.
Attack Chain
- An attacker gains initial access to a Windows system, potentially through phishing or exploiting a software vulnerability.
- The attacker uploads or creates a PowerShell script on the target system.
- The PowerShell script employs backtick-escaped variable expansion (e.g.,
$env:use``r``na``me) to obfuscate its contents. - The obfuscated script is executed using powershell.exe.
- The script dynamically reconstructs commands and strings by evaluating the backtick-escaped variables.
- The reconstructed commands perform malicious activities, such as downloading additional payloads or modifying system configurations.
- The script attempts to evade detection by AMSI and other security tools.
- The attacker achieves persistence and control over the compromised system, potentially leading to data exfiltration or further lateral movement.
Impact
Successful exploitation can lead to arbitrary code execution, system compromise, and data theft. While the number of victims is unknown, PowerShell is a common attack vector on Windows environments. The sectors most affected are organizations relying on Windows infrastructure without adequate PowerShell monitoring and security controls. Failure to detect and prevent this technique allows attackers to bypass security measures and gain unauthorized access to sensitive data.
Recommendation
- Enable PowerShell Script Block Logging to generate event code 4104. (Reference: Setup section)
- Deploy the Sigma rule
Detect PowerShell Backtick Variable Obfuscationto identify scripts using backtick-escaped variable expansion. - Investigate any alerts generated by the Sigma rule, focusing on scripts with a high
Esql.script_block_pattern_countvalue. - Monitor for process creation events where powershell.exe executes obfuscated commands as detected by the Sigma rule
Detect Suspicious PowerShell Encoded Commands. - Review PowerShell logs for event code 4104 and examine
powershell.file.script_block_textfor suspicious patterns.
Detection coverage 2
Detect PowerShell Backtick Variable Obfuscation
highDetects PowerShell scripts that use backtick-escaped characters inside `${}` variable expansion.
Detect Suspicious PowerShell Encoded Commands
highDetects PowerShell scripts that use Base64 encoded commands
Detection queries are kept inside the platform. Get full rules →