-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.pas
More file actions
591 lines (527 loc) · 14.5 KB
/
utils.pas
File metadata and controls
591 lines (527 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
unit utils;
{$ifdef fpc}{$mode delphi}{$endif fpc}
interface
uses
Classes, SysUtils,windows,
inifiles
{$ifdef fpc},dom,XMLRead{$endif fpc}
{$ifndef fpc},math{$endif fpc};
{$ifndef fpc}
type
TBytes = array of Byte;
//PBytes = ^tbytes;
qword=int64;
long=longint;
PSTR = PAnsiChar;
GUID = tguid;
ULONGLONG = Int64;
LPCVOID = Pointer;
LPVOID = Pointer;
lpbyte=PBYTE;
PVOID = Pointer;
ULONG_PTR = Longword;
SIZE_T = ULONG_PTR;
PtrUInt = NativeUInt;
{$endif fpc}
type
tbyte16=array[0..15] of byte;
tbyte32=array[0..31] of byte;
NTStatus = DWORD;
type fn=function(param:pointer):dword;stdcall;
type _KUHL_M_SEKURLSA_ENUM_HELPER =record
tailleStruct:nativeint; //SIZE_T
offsetToLuid:ULONG;
offsetToLogonType:ULONG;
offsetToSession:ULONG;
offsetToUsername:ULONG;
offsetToDomain:ULONG;
offsetToCredentials:ULONG;
offsetToPSid:ULONG;
offsetToCredentialManager:ULONG;
offsetToLogonTime:ULONG;
offsetToLogonServer:ULONG;
end;
KUHL_M_SEKURLSA_ENUM_HELPER=_KUHL_M_SEKURLSA_ENUM_HELPER;
PKUHL_M_SEKURLSA_ENUM_HELPER=^_KUHL_M_SEKURLSA_ENUM_HELPER;
procedure writeini(section,ident,value:string;config:string='');
function readini(section,ident,default:string;config:string=''):string;
procedure log(msg:string;status:dword=0);overload;
procedure log(msg:dword;status:dword=0);overload;
procedure log(msg:qword;status:dword=0);overload;
//function HashByteToString(hash:tbyte16):string;
function FiletoHexaString(filename:string):boolean;
function HexaStringToFile(filename:string;buffer:tbytes):boolean;
function ByteToHexaString(hash:array of byte):string;overload;
function ByteToHexaString(hash:pbyte;len:dword):string;overload;
function HexaStringToByte(hash:string):tbyte16;
function HexaStringToByte2(hash:string):tbytes;
function BytetoAnsiString(buffer:pbyte;len:dword):string;overload;
function BytetoAnsiString(input:array of byte):string;overload;
function AnsiStringtoByte(input:string;unicode:boolean=false):tbytes;
Function SplitUserSID(user:pchar;var domain:string;var rid:dword):boolean;
function LeftPad(value: string; length:integer=8; pad:char='0'): string; overload;
function ByteSwap64(Value: Int64): Int64;
function ByteSwap32(dw: cardinal): cardinal;
function ByteSwap16(w: word): word;
function MyRegQueryValue(hk:hkey;subkey:pchar;value:pchar;var data:tbytes;server:string=''):boolean;
function MyRegEnumKeys(hk:hkey;subkey:pchar;server:string=''):boolean;
{$ifdef fpc}
function parsexml(binary,key:string;var output:string):boolean;
{$endif fpc}
var
verbose:boolean=false;
lsass_pid:dword=0;
winver,osarch:string;
sysdir:pchar;
debugpriv:boolean=false;
symmode:boolean=false;
console_output_type:dword;
//
system_hive:string='system.sav';
security_hive:string='security.sav';
sam_hive:string='sam.sav';
implementation
function readini(section,ident,default:string;config:string=''):string;
var
ini:tinifile;
currentdir,fname:string;
begin
log('*********** readini **********');
//currentdir:=upsapi.GetCurrentExeDir;
//if currentdir='' then currentdir:=lib.CurrentExeDir ;
if config<>'' then fname:=config else fname:=getcurrentdir + '\config.INI';
if FileExists(fname) then
begin
ini:=tinifile.Create (fname);
result:=ini.ReadString (section,ident,default );
freeandnil(ini);
end
else result:=default;
end;
procedure writeini(section,ident,value:string;config:string='');
var
ini:tinifile;
Attrs:word;
{currentdir,}fname:string;
begin
log('*********** writeini **********');
//currentdir:=upsapi.GetCurrentExeDir;
//if currentdir='' then currentdir:=lib.CurrentExeDir ;
if config<>'' then fname:=config else fname:=getcurrentdir + '\config.INI';
if FileExists(fname) then
begin
Attrs := FileGetAttr(fname);
if Attrs and fareadonly <> 0 then exit;
end;
//if FileExists(GetCurrentDir + '\config.INI')=false then exit;
try
ini:=tinifile.Create (fname);
ini.WriteString(section,ident,value);
finally
freeandnil(ini);
end;
end;
//status : success=0
procedure log(msg:string;status:dword=0);
begin
//if (verbose=false) and (status=0) then exit;
//writeln('length(msg):'+inttostr(length(msg)));
try
if verbose=false then
if status<>0 then writeln(msg);
if (verbose=true) and (console_output_type<>FILE_TYPE_PIPE) then writeln(msg);
except
on e:exception do writeln('log:'+e.message);
end;
//writeln(status);
end;
procedure log(msg:dword;status:dword=0);overload;
begin
log(inttostr(msg),status);
end;
procedure log(msg:qword;status:dword=0);overload;
begin
log(inttostr(msg),status);
end;
function LeftPad(value: string; length:integer=8; pad:char='0'): string; overload;
begin
{$ifdef fpc} //no delphi for now
result := RightStr(StringOfChar(pad,length) + value, length );
{$endif fpc}
end;
function getoffset(var field;var rec):integer;
begin
{$ifdef fpc} //no delphi for now
result:=ptrint(pointer(@field)-ptrint(@rec));
{$endif fpc}
end;
{$ifdef fpc} //no delphi for now
function findnodes(list:tdomnodelist;search:string):tdomnode;
//*******************************************
function recursexml(n:tdomnode;search:string):tdomnode;
var
w:word;
begin
result:=nil;
//log(n.ChildNodes.Count);
if (search<>'') and (lowercase(search)=lowercase(n.NodeName)) then
begin
result:=n;
exit;
end;
if n.firstchild.NodeValue<>''
then log(n.NodeName+':'+n.firstchild.NodeValue )
else log(n.NodeName);
if n.FirstChild.nodename<>'#text' then
for w:=0 to n.ChildNodes.Count-1 do
begin
result:=recursexml(n.childnodes[w],search);
end;
end;
//*******************************************
var
w:word;
begin
result:=nil;
log('search:'+search);
for w:=0 to list.Count-1 do
begin
log('----');
result:= recursexml(list[w],search);
if result<>nil then break;
end;
end;
{$endif fpc}
{$ifdef fpc} //no delphi for now
function parsexml(binary,key:string;var output:string):boolean;
var
PassNode: TDOMNode=nil;
Doc: TXMLDocument;
w:word;
begin
result:=false;
log('binary:'+binary);
log('key:'+key);
try
// Read in xml file from disk
ReadXMLFile(Doc, binary);
//log('ReadXMLFile ok');
// Retrieve the "password" node
//PassNode := Doc.DocumentElement.FindNode(node);
//log('FindNode ok');
passnode:=findnodes(doc.DocumentElement.ChildNodes,key);
// Write out value of the selected node
if passnode<>nil then
begin
//log(PassNode.NodeValue); // will be blank
// The text of the node is actually a separate child node
log(PassNode.FirstChild.NodeValue); // correctly prints "abc"
output:=PassNode.FirstChild.NodeValue;
result:=true;
// alternatively
//log(PassNode.TextContent);
end
else log('passnode=nil');
finally
// finally, free the document
Doc.Free;
end;
end;
{$endif fpc}
function MyRegEnumKeys(hk:hkey;subkey:pchar;server:string=''):boolean;
var
ret:long;
topkey,rk:hkey;
cbdata,lptype,index:dword;
dwDisposition:dword{$ifdef fpc}=0{$endif fpc};
lpname:pchar;
begin
log('**** MyRegEnumKeys ****');
log('server:'+server);
log('subkey:'+subkey);
result:=false;
topkey:=thandle(-1);
if server<>''
then
begin
SetLastError(0) ;
ret:=RegConnectRegistry (pchar(server),hk,rk );
log('RegConnectRegistry:'+inttostr(ret),0);
//log('RegConnectRegistry:'+inttostr(getlasterror));
//KEY_QUERY_VALUE or KEY_READ? // KEY_WOW64_32KEY or
SetLastError(0) ;
if ret=0 then ret:=RegOpenKeyEx(rk, subkey,0, KEY_READ, topkey);
//log('RegOpenKeyEx:'+inttostr(getlasterror));
//if ret=0 then ret := RegCreateKeyEx(rk,subkey,0,nil,REG_OPTION_NON_VOLATILE,KEY_QUERY_VALUE,nil,topKey,@dwDisposition);
end
else ret:=RegOpenKeyEx(hk, subkey,0, KEY_READ, topkey);
if ret=0 then
begin
log('RegOpenKeyEx OK',0);
cbdata:=1024;
getmem(lpname,1024);
index:=0;ret:=0;
while ret=0 do
begin
ret:= RegEnumKey (topkey ,index,lpname,cbdata);
if ret=0 then log(lpname,1);
inc(index);
end;
RegCloseKey(topkey);
end //RegOpenKeyEx
else log('RegOpenKeyEx NOT OK:'+inttostr(ret),0);
end;
function MyRegQueryValue(hk:hkey;subkey:pchar;value:pchar;var data:tbytes;server:string=''):boolean;
var
ret:long;
topkey,rk:hkey;
cbdata,lptype:dword;
dwDisposition:dword{$ifdef fpc}=0{$endif fpc};
begin
log('**** MyRegQueryValue ****');
log('server:'+server);
log('subkey:'+subkey);
log('value:'+value);
result:=false;
topkey:=thandle(-1);
if server<>''
then
begin
SetLastError(0) ;
ret:=RegConnectRegistry (pchar(server),hk,rk );
log('RegConnectRegistry:'+inttostr(ret),0);
//log('RegConnectRegistry:'+inttostr(getlasterror));
//KEY_QUERY_VALUE or KEY_READ? // KEY_WOW64_32KEY or
SetLastError(0) ;
if ret=0 then ret:=RegOpenKeyEx(rk, subkey,0, KEY_READ, topkey);
//log('RegOpenKeyEx:'+inttostr(getlasterror));
//if ret=0 then ret := RegCreateKeyEx(rk,subkey,0,nil,REG_OPTION_NON_VOLATILE,KEY_QUERY_VALUE,nil,topKey,@dwDisposition);
end
else ret:=RegOpenKeyEx(hk, subkey,0, KEY_READ, topkey);
if ret=0 then
begin
log('RegOpenKeyEx OK',0);
cbdata:=1024;
ret := RegQueryValueex (topkey,value,nil,@lptype,nil,@cbdata);
if (ret=0) and (cbdata>0) then
begin
log('RegQueryValueex OK',0);
log('cbdata:'+inttostr(cbdata));
setlength(data,cbdata);
RegQueryValueex (topkey,value,nil,@lptype,@data[0],@cbdata);
if (ret=0) and (cbdata>0) then result:=true;
end;
RegCloseKey(topkey);
end //RegOpenKeyEx
else log('RegOpenKeyEx '+strpas(subkey)+' NOT OK:'+inttostr(ret),1);
end;
function FiletoHexaString(filename:string):boolean;
var
outfile:thandle{$ifdef fpc}=0{$endif fpc};
buffer:array[0..1023] of byte;
bytesread:cardinal;
begin
result:=false;
if not FileExists(filename) then log('filename does not exist');
outFile := CreateFile(pchar(filename), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL , 0);
bytesread:=1;
while bytesread>0 do
begin
result:=readfile(outfile ,buffer,length(buffer),bytesread,nil);
if bytesread>0 then
begin
log(ByteToHexaString (@buffer[0],bytesread),1);
result:=true;
end;
end;
closehandle(outfile);
end;
function HexaStringToFile(filename:string;buffer:tbytes):boolean;
var
outfile:thandle{$ifdef fpc}=0{$endif fpc};
byteswritten:cardinal;
begin
log('**** HexaStringToFile ****');
result:=false;
outFile := CreateFile(pchar(filename), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
byteswritten :=0;
log('length:'+inttostr(length(buffer)));
result:=writefile(outfile ,buffer[0],length(buffer),byteswritten,nil);
log('byteswritten:'+inttostr(byteswritten));
if byteswritten>0 then result:=true;
closehandle(outfile);
end;
function AnsiStringtoByte(input:string;unicode:boolean=false):tbytes;
var
i:dword;
begin
log('**** AnsiStringtoByte ****');
input:=stringreplace(input,'\0',#0,[]);
//log('input:->'+input+'<-');
log('length:'+inttostr(length(input)));
try
if unicode=false then
begin
setlength(result,length(input));
//log('AnsiStringtoByte len:'+inttostr(length(input)));
for i:=1 to length(input) do result[i-1]:=ord(input[i]);
end;
if unicode=true then
begin
setlength(result,length(input)*2);
//log('AnsiStringtoByte len:'+inttostr(length(input)));
for i:=1 to length(input) do
begin
result[(i-1)*2]:=ord(input[i]);
//Inc(PInteger(@i)^, 1);
end;
end;
except
on e:exception do log('AnsiStringtoByte:'+e.Message );
end;
end;
function BytetoAnsiString(buffer:pbyte;len:dword):string;overload;
var
tmp:tbytes;
begin
if len=0 then exit;
SetLength(tmp,len);
ZeroMemory(@tmp[0],len);
CopyMemory(@tmp[0],buffer,len) ;
result:=BytetoAnsiString(tmp);
end;
function BytetoAnsiString(input:array of byte):string;
var
i:dword;
dummy:string{$ifdef fpc}=''{$endif fpc};
begin
log('**** BytetoAnsiString ****');
log('sizeof:'+inttostr(sizeof(input)));
if sizeof(input)=0 then exit;
try
//writeln(sizeof(input));
for i:=0 to sizeof(input)-1 do dummy:=dummy+chr(input[i]);
result:=dummy;
except
on e:exception do log('BytetoAnsiString:'+e.Message );
end;
end;
function ByteToHexaString(hash:pbyte;len:dword):string;overload;
var
tmp:tbytes;
begin
SetLength(tmp,len);
ZeroMemory(@tmp[0],len);
CopyMemory(@tmp[0],hash,len) ;
result:=ByteToHexaString(tmp);
end;
//function HashByteToString(hash:tbyte16):string;
function ByteToHexaString(hash:array of byte):string;
var
i:dword;
dummy:string{$ifdef fpc}=''{$endif fpc};
begin
log('**** ByteToHexaString ****');
log('sizeof:'+inttostr(sizeof(hash)));
//setlength(dummy,sizeof(hash)*2);
try
//writeln('sizeof(hash):'+inttostr(sizeof(hash)));
//writeln('length(hash):'+inttostr(length(hash)));
for i:=0 to sizeof(hash)-1 do dummy:=dummy+inttohex(hash[i],2);
result:=dummy;
except
on e:exception do log('ByteToHexaString:'+e.Message );
end;
end;
function HexaStringToByte(hash:string):tbyte16;
var
i:dword;
tmp:string;
begin
try
i:=1;
//setlength(result,length(hash));
while I<min(32,length(hash)){sizeof(hash)*2} do
begin
tmp:=copy(hash,i,2);
result[i div 2]:=strtoint('$'+tmp);
inc(i,2);
end;
except
on e:exception do log('HexaStringToByte:'+e.Message );
end;
end;
function HexaStringToByte2(hash:string):tbytes;
var
i:dword;
tmp:string;
b:longint;
begin
log('**** HexaStringToByte2 ****');
log('length:'+inttostr(length(hash)));
try
i:=1;
//log('hash:'+hash);
//log('length(hash) div 2:'+inttostr(length(hash) div 2));
setlength(result,length(hash) div 2);
while I<length(hash) do
begin
tmp:=copy(hash,i,2);
if TryStrToInt ('$'+tmp,b) then result[i div 2]:=b;
//result[i div 2]:=strtoint('$'+tmp);
inc(i,2);
//write('.');
end;
except
on e:exception do log('HexaStringToByte2:'+e.Message );
end;
end;
Function SplitUserSID(user:pchar;var domain:string;var rid:dword):boolean;
var
elements: TStrings;
i:byte;
begin
elements := TStringList.Create;
{$ifdef fpc}ExtractStrings(['-'],[],user,elements,false);{$endif fpc}
{$ifndef fpc}ExtractStrings(['-'],[],user,elements);{$endif fpc}
for i:=0 to elements.Count-2 do domain:=domain+'-'+elements[i];
delete(domain,1,1);
log('domain:'+domain);
rid:=strtoint(elements[elements.count-1]);
log('rid:'+inttostr(rid));
elements.Free ;;
end;
{$ifdef fpc}
{$asmmode intel}
{$endif}
//support cpux86 and cpux64
function ByteSwap64(Value: Int64): Int64;
asm
{$IF Defined(CPUX86)}
mov edx, [ebp+$08]
mov eax, [ebp+$0c]
bswap edx
bswap eax
{$ELSEIF Defined(CPUX64)}
mov rax, rcx
bswap rax
//{$ELSE}
//{$Message Fatal 'ByteSwap64 has not been implemented for this architecture.'}
//{$ENDIF}
{$IFEND}
end;
function ByteSwap32(dw: cardinal): cardinal;
asm
{$IFDEF CPUX64}
mov rax, rcx
{$ENDIF}
bswap eax
end;
function ByteSwap16(w: word): word;
asm
{$IFDEF CPUX64}
mov rax, rcx
{$ENDIF}
xchg al, ah
end;
end.