Press enter or click to view image in full size
The Antimalware Scan Interface (AMSI) is a robust security feature in Windows that enhances endpoint protection by scanning scripts and code for malicious behavior. Integrated with tools like PowerShell and VBScript, it acts as a proactive defense mechanism. However, attackers have developed sophisticated techniques to bypass AMSI, highlighting the need for a comprehensive understanding of these methods and the strategies to mitigate them. This blog explores key AMSI bypass techniques, their mechanisms, and effective countermeasures.
AMSI is a security interface introduced by Microsoft to provide antivirus and endpoint solutions with a standard API for detecting malicious behavior in scripts. It operates through three primary mechanisms:
amsi.dll. The engine evaluates the content for malicious patterns and suspicious behaviors.AMSI integrates seamlessly with Windows Defender Antivirus and third-party endpoint protection tools. By leveraging heuristic and signature-based detection methods, it provides a comprehensive layer of security. However, AMSI’s reliance on exposed APIs and memory processes introduces exploitable vulnerabilities, making it a frequent target for sophisticated attackers.
1. Memory Patching
Memory patching involves directly modifying AMSI-related functions in memory to disable its functionality.
Mechanism: Attackers overwrite the amsiScanBuffer function to always return a success status, effectively bypassing detection.
Code Snippet:
$amsi = [Ref].Assembly.GetType('System.Management.Automation.AmsiUtils');
$amsi.GetField('amsiInitFailed', 'NonPublic, Static').SetValue($null, $true);Real-World Example: This method is frequently used in malware like Cobalt Strike, which patches memory to avoid AMSI scans.
2. String Obfuscation
String obfuscation alters recognizable patterns in malicious code, making it difficult for AMSI to detect threats.
Techniques:
Example:
$cmd = "c" + "a" + "l" + "c";
Invoke-Expression ($cmd);Why It Works: AMSI scans plain text content. By obfuscating the payload, attackers evade these checks.
3. Hooking
Hooking intercepts AMSI API calls and redirects them to custom functions that neutralize the scan.
Implementation:
Detours in C++ or similar .NET frameworks are used to modify function pointers.amsiScanBuffer is redirected to always return a benign response.Challenges: Requires precise knowledge of memory structures and can be detected by advanced monitoring tools.
4. Environment-Based Evasion
Attackers exploit system configurations or user privileges to disable AMSI or weaken its impact.
Methods:
Set-ExecutionPolicy to lower script execution restrictions:Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass5. DLL Hijacking/Replacement
Replacing the amsi.dll file with a tampered or benign version to disable its functionality.
Execution:
System32 directory.Drawbacks:
6. Reflection/Unmanaged Code
Attackers use reflective DLL injection or unmanaged code to bypass AMSI by avoiding direct interaction with its functions.
Example:
7. PowerShell Downgrade Attacks
Downgrading PowerShell to an older version (like v2.0) that does not support AMSI.
Command:
powershell.exe -Version 2Caveats: Older PowerShell versions lack modern features and might be flagged by monitoring tools.
Join Medium for free to get updates from this writer.
8. Exploiting Weak AMSI Integration
Attackers exploit incomplete or improperly implemented AMSI hooks in third-party applications.
Example:
To counter AMSI bypass techniques, defenders can implement the following measures:
amsi.dll.Here’s a more detailed explanation of the real-world case studies related to AMSI bypass techniques:
Overview: Cobalt Strike is a popular penetration testing tool that is also widely used by cybercriminals and threat actors for exploitation, lateral movement, and post-exploitation activities. It is often associated with advanced persistent threat (APT) groups and is commonly used in sophisticated cyber-attacks.
AMSI Bypass Technique: Cobalt Strike is known for leveraging memory patching to bypass AMSI detection. The tool’s Beacon payload can be injected into the target system, and it has built-in functionality to disable AMSI by overwriting or manipulating the amsiScanBuffer function in memory. This effectively disables AMSI's ability to scan and detect malicious scripts or commands, allowing Cobalt Strike to run undetected.
How it Works:
amsiScanBuffer, which is used by AMSI to scan script content.Real-World Impact:
Defense Recommendations:
Overview: Invoke-Obfuscation is a popular PowerShell obfuscation framework that is often used to generate obfuscated PowerShell scripts, making them harder for security tools, including AMSI, to detect.
AMSI Bypass Technique: Invoke-Obfuscation relies heavily on string obfuscation and dynamic payload encoding to avoid detection by AMSI. It allows attackers to encode or obfuscate PowerShell commands, payloads, and strings, making it more difficult for AMSI to recognize and flag the scripts.
How it Works:
Real-World Impact:
Defense Recommendations:
Overview: Emotet is a well-known banking trojan that evolved into a highly modular and polymorphic malware family. It is often distributed through phishing emails containing malicious attachments or links. Once executed, Emotet can download additional payloads, including ransomware or information stealers.
AMSI Bypass Technique: Emotet employs a combination of string obfuscation and reflection/unmanaged code techniques to bypass AMSI. The trojan may use PowerShell scripts to execute its payloads, and these scripts are often obfuscated using techniques that evade AMSI’s scanning capabilities.
How it Works:
Real-World Impact:
Defense Recommendations:
AMSI bypass techniques showcase the continuous arms race between attackers and defenders. While attackers innovate new methods to evade detection, understanding these techniques empowers defenders to strengthen their security posture. By combining proactive monitoring, robust configurations, and continuous education, organizations can mitigate the risk posed by AMSI bypasses.