-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUiLib.Reflection.Types.pas
757 lines (633 loc) · 19 KB
/
NtUiLib.Reflection.Types.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
unit NtUiLib.Reflection.Types;
{
This module provides RTTI representers for many commonly used types among
NtUtils. See DelphiUiLib.Reflection for using and selectively registering
them with the RTTI system.
}
interface
uses
Ntapi.WinNt, Ntapi.ntseapi, NtUtils, DelphiUiLib.Reflection;
type
// TNtUnicodeString
TNtUnicodeStringRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TNtAnsiString
TNtAnsiStringRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TClientId
TClientIdRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TProcessId
TProcessIdRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TProcessId32
TProcessId32Representer = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TThreadId
TThreadIdRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TThreadId32
TThreadId32Representer = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// NTSTATUS
TNtStatusRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// HRESULT
THResultRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TWin32Error
TWin32ErrorRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TNtxStatus
TNtxStatusRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TLargeInteger
TLargeIntegerRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TULargeInteger
TULargeIntegerRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TUnixTime
TUnixTimeRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// PSid
TSidRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TSidAndAttributes
TSidAndAttributesRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// ISid
TISidRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TGroup
TGroupRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TLogonId
TLogonIdRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TSessionId
TSessionIdRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// TRect
TRectRepresenter = class abstract (TRepresenter)
class function GetType: Pointer; override;
class function Represent(
const Instance;
[opt] const Attributes: TArray<TCustomAttribute>
): TRepresentation; override;
end;
// A worker function that represents SIDs and attributes
function RepresentSidWorker(
[in, opt] Sid: PSid;
Attributes: TGroupAttributes;
AttributesPresent: Boolean;
[opt] const hxPolicy: IHandle = nil
): TRepresentation;
implementation
uses
System.SysUtils, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntpebteb, Ntapi.WinUser,
Ntapi.winsta, Ntapi.ntstatus, DelphiApi.Reflection, NtUtils.Security.Sid,
NtUtils.Lsa.Sid, NtUtils.Processes, NtUtils.Processes.Info, NtUtils.Threads,
NtUtils.Errors, NtUiLib.Errors, NtUtils.Lsa.Logon, NtUtils.WinStation,
NtUtils.Synchronization, DelphiUiLib.Strings, DelphiUiLib.Reflection.Strings;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function RepresentSidWorker;
var
SidCopy: ISid;
HintSections: TArray<THintSection>;
Lookup: TTranslatedName;
Success: Boolean;
i: Integer;
begin
if not Assigned(Sid) then
begin
Result.Text := '(nil)';
Exit;
end;
if not RtlxCopySid(Sid, SidCopy).IsSuccess then
begin
Result.Text := '(invalid)';
Exit;
end;
Success := LsaxLookupSid(SidCopy, Lookup, hxPolicy).IsSuccess;
// Choose the best option for the main view
if Success and Lookup.IsValid then
Result.Text := Lookup.FullName
else
Result.Text := RtlxSidToString(SidCopy);
// Build the hint with what we have
i := 0;
SetLength(HintSections, 5);
if Success and Lookup.IsValid then
begin
HintSections[i] := THintSection.New('Friendly Name', Result.Text);
Inc(i);
end;
HintSections[i] := THintSection.New('SID', RtlxSidToString(SidCopy));
Inc(i);
if Success then
begin
HintSections[i] := THintSection.New('Type', TType.Represent(
Lookup.SidType).Text);
Inc(i);
end;
if AttributesPresent then
begin
HintSections[i] := THintSection.New('State', TType.Represent
<TGroupAttributes>(Attributes and SE_GROUP_STATE_MASK).Text);
Inc(i);
HintSections[i] := THintSection.New('Flags', TType.Represent
<TGroupAttributes>(Attributes and not SE_GROUP_STATE_MASK,
[Auto.From(IgnoreSubEnumsAttribute.Create).Self]).Text);
Inc(i);
end;
SetLength(HintSections, i);
Result.Hint := BuildHint(HintSections);
end;
function RepresentThreadWorker(
Tid: TThreadId;
Pid: TProcessId;
IsKnownPid: Boolean
): TRepresentation;
var
hxThread: IHandle;
BasicInfo: TThreadBasicInformation;
IsKnownName, IsKnownTerminated: LongBool;
ThreadName: String;
begin
IsKnownName := False;
IsKnownTerminated := False;
if NtxOpenThread(hxThread, Tid, THREAD_QUERY_LIMITED_INFORMATION).IsSuccess then
begin
// Query the PID
if not IsKnownPid and NtxThread.Query(hxThread, ThreadBasicInformation,
BasicInfo).IsSuccess then
begin
IsKnownPid := True;
Pid := BasicInfo.ClientId.UniqueProcess;
end;
// Query the name
if NtxQueryNameThread(hxThread, ThreadName).IsSuccess then
begin
IsKnownName := True;
if ThreadName = '' then
ThreadName := 'unnamed thread';
end;
// Query the termination status
NtxThread.Query(hxThread, ThreadIsTerminated, IsKnownTerminated);
end;
// Format the process representation
if IsKnownPid then
Result := TProcessIdRepresenter.Represent(Pid, nil)
else
begin
Result := Default(TRepresentation);
Result.Text := 'Unknown Process';
end;
if not IsKnownName then
ThreadName := 'thread';
if IsKnownTerminated then
ThreadName := 'terminated ' + ThreadName;
Result.Text := Format('%s: %s [%d]', [Result.Text, ThreadName, TID]);
end;
{ TNtUnicodeStringRepresenter }
class function TNtUnicodeStringRepresenter.GetType;
begin
Result := TypeInfo(TNtUnicodeString);
end;
class function TNtUnicodeStringRepresenter.Represent;
var
Value: TNtUnicodeString absolute Instance;
begin
Result.Text := Value.ToString;
end;
{ TNtAnsiStringRepresenter }
class function TNtAnsiStringRepresenter.GetType;
begin
Result := TypeInfo(TNtAnsiString);
end;
class function TNtAnsiStringRepresenter.Represent;
var
Value: TNtAnsiString absolute Instance;
begin
Result.Text := String(Value.ToString);
end;
{ TClientIdRepresenter }
class function TClientIdRepresenter.GetType;
begin
Result := TypeInfo(TClientId);
end;
class function TClientIdRepresenter.Represent;
var
CID: TClientId absolute Instance;
begin
Result := RepresentThreadWorker(CID.UniqueThread, CID.UniqueProcess, True);
end;
{ TProcessIdRepresenter }
class function TProcessIdRepresenter.GetType;
begin
Result := TypeInfo(TProcessId);
end;
class function TProcessIdRepresenter.Represent;
var
PID: TProcessId absolute Instance;
ImageName: String;
hxProcess: IHandle;
begin
if PID = 0 then
ImageName := 'System Idle Process'
else if PID = 4 then
ImageName := 'System'
else if NtxQueryImageNameProcessId(PID, ImageName).IsSuccess then
begin
if ImageName <> '' then
begin
Result.Hint := BuildHint('NT Image Name', ImageName);
ImageName := ExtractFileName(ImageName);
end
else
ImageName := 'Unnamed Process';
if NtxOpenProcess(hxProcess, PID, SYNCHRONIZE).IsSuccess and
(NtxWaitForSingleObject(hxProcess, 0).Status = STATUS_WAIT_0) then
ImageName := 'Terminated ' + ImageName;
end
else
ImageName := 'Unknown Process';
Result.Text := Format('%s [%d]', [ImageName, PID]);
end;
{ TProcessId32Representer }
class function TProcessId32Representer.GetType;
begin
Result := TypeInfo(TProcessId32);
end;
class function TProcessId32Representer.Represent;
var
PID32: TProcessId32 absolute Instance;
PID: TProcessId;
begin
PID := PID32;
Result := TProcessIdRepresenter.Represent(PID, Attributes);
end;
{ TThreadIdRepresenter }
class function TThreadIdRepresenter.GetType;
begin
Result := TypeInfo(TThreadId);
end;
class function TThreadIdRepresenter.Represent;
var
TID: TThreadId absolute Instance;
begin
Result := RepresentThreadWorker(TID, 0, False);
end;
{ TProcessId32Representer }
class function TThreadId32Representer.GetType;
begin
Result := TypeInfo(TThreadId32);
end;
class function TThreadId32Representer.Represent;
var
TID32: TThreadId32 absolute Instance;
TID: TProcessId;
begin
TID := TID32;
Result := TThreadIdRepresenter.Represent(TID, Attributes);
end;
{ TNtStatusRepresenter }
class function TNtStatusRepresenter.GetType;
begin
Result := TypeInfo(NTSTATUS);
end;
class function TNtStatusRepresenter.Represent;
var
Status: NTSTATUS absolute Instance;
begin
Result.Text := RtlxNtStatusName(Status);
Result.Hint := RtlxNtStatusMessage(Status);
end;
{ THResultRepresenter }
class function THResultRepresenter.GetType: Pointer;
begin
Result := TypeInfo(HResult);
end;
class function THResultRepresenter.Represent;
var
Value: HResult absolute Instance;
begin
Result.Text := RtlxNtStatusName(Value.ToNtStatus);
Result.Hint := RtlxNtStatusMessage(Value.ToNtStatus);
end;
{ TWin32ErrorRepresenter }
class function TWin32ErrorRepresenter.GetType;
begin
Result := TypeInfo(TWin32Error);
end;
class function TWin32ErrorRepresenter.Represent;
var
Error: TWin32Error absolute Instance;
begin
Result.Text := RtlxNtStatusName(Error.ToNtStatus);
Result.Hint := RtlxNtStatusMessage(Error.ToNtStatus);
end;
{ TNtxStatusRepresenter }
class function TNtxStatusRepresenter.GetType;
begin
Result := TypeInfo(TNtxStatus);
end;
class function TNtxStatusRepresenter.Represent;
var
Status: TNtxStatus absolute Instance;
begin
Result.Text := Status.ToString;
Result.Hint := Status.Description;
end;
{ TLargeIntegerRepresenter }
class function TLargeIntegerRepresenter.GetType;
begin
Result := TypeInfo(TLargeInteger);
end;
class function TLargeIntegerRepresenter.Represent;
var
Value: TLargeInteger absolute Instance;
begin
if Value = 0 then
Result.Text := 'Never'
else if Value = Int64.MaxValue then
Result.Text := 'Infinite'
else
Result.Text := DateTimeToStr(LargeIntegerToDateTime(Value));
Result.Hint := BuildHint('Raw value', IntToStrEx(UInt64(Value)));
end;
{ TULargeIntegerRepresenter }
class function TULargeIntegerRepresenter.GetType;
begin
Result := TypeInfo(TULargeInteger);
end;
class function TULargeIntegerRepresenter.Represent;
var
Value: TULargeInteger absolute Instance;
begin
Result.Text := TimeIntervalToString(Value div NATIVE_TIME_SECOND);
Result.Hint := BuildHint('Raw value', IntToStrEx(Value));
end;
{ TUnixTimeRepresenter }
class function TUnixTimeRepresenter.GetType;
begin
Result := TypeInfo(TUnixTime);
end;
class function TUnixTimeRepresenter.Represent;
var
Value: TUnixTime absolute Instance;
begin
Result.Text := DateTimeToStr(UnixTimeToDateTime(Value));
Result.Hint := BuildHint('Raw value', IntToStrEx(Value));
end;
{ TSidRepresenter }
class function TSidRepresenter.GetType;
begin
Result := TypeInfo(PSid);
end;
class function TSidRepresenter.Represent;
var
Sid: PSid absolute Instance;
begin
Result := RepresentSidWorker(Sid, 0, False);
end;
{ TSidAndAttributesRepresenter }
class function TSidAndAttributesRepresenter.GetType;
begin
Result := TypeInfo(TSidAndAttributes);
end;
class function TSidAndAttributesRepresenter.Represent;
var
Value: TSidAndAttributes absolute Instance;
begin
Result := RepresentSidWorker(Value.Sid, Value.Attributes, True);
end;
{ TISidRepresenter }
class function TISidRepresenter.GetType;
begin
Result := TypeInfo(ISid);
end;
class function TISidRepresenter.Represent;
var
Sid: ISid absolute Instance;
begin
Result := RepresentSidWorker(Auto.RefOrNil<PSid>(Sid), 0, False);
end;
{ TGroupRepresenter }
class function TGroupRepresenter.GetType;
begin
Result := TypeInfo(TGroup);
end;
class function TGroupRepresenter.Represent;
var
Group: TGroup absolute Instance;
begin
Result := RepresentSidWorker(Auto.RefOrNil<PSid>(Group.SID),
Group.Attributes, True);
end;
{ TLogonIdRepresenter }
class function TLogonIdRepresenter.GetType;
begin
Result := TypeInfo(TLogonId);
end;
class function TLogonIdRepresenter.Represent;
var
LogonId: TLogonId absolute Instance;
UserName: String;
LogonData: ILogonSession;
begin
LsaxQueryLogonSession(LogonId, LogonData);
case LogonId of
SYSTEM_LUID: UserName := 'SYSTEM';
ANONYMOUS_LOGON_LUID: UserName := 'ANONYMOUS LOGON';
LOCALSERVICE_LUID: UserName := 'LOCAL SERVICE';
NETWORKSERVICE_LUID: UserName := 'NETWORK SERVICE';
IUSER_LUID: UserName := 'IUSR';
else
if Assigned(LogonData) then
if LogonData.Data.UserName.Length > 0 then
UserName := LogonData.Data.UserName.ToString
else
UserName := 'No user'
else
UserName := '';
end;
Result.Text := IntToHexEx(LogonId);
if Assigned(LogonData) then
begin
Result.Text := Format('%s (%s @ %d)', [Result.Text, UserName,
LogonData.Data.Session]);
Result.Hint := BuildHint([
THintSection.New('Logon ID', IntToHexEx(LogonId)),
THintSection.New('Logon Time', TLargeIntegerRepresenter.Represent(
LogonData.Data.LogonTime, nil).Text),
THintSection.New('User', TSidRepresenter.Represent(
LogonData.Data.SID, nil).Text),
THintSection.New('Session', TSessionIdRepresenter.Represent(
LogonData.Data.Session, nil).Text)
]);
end
else if UserName <> '' then
Result.Text := Format('%s (%s)', [Result.Text, UserName])
end;
{ TSessionIdRepresenter }
class function TSessionIdRepresenter.GetType;
begin
Result := TypeInfo(TSessionId);
end;
class function TSessionIdRepresenter.Represent;
var
SessionId: TSessionId absolute Instance;
Info: TWinStationInformation;
begin
Result.Text := IntToStrEx(SessionId);
if not WsxWinStation.Query(SessionId, WinStationInformation, Info).IsSuccess then
Exit;
if Info.WinStationName <> '' then
Result.Text := Format('%s: %s', [Result.Text, Info.WinStationName]);
Result.Text := Format('%s (%s)', [Result.Text, Info.FullUserName]);
Result.Hint := BuildHint([
THintSection.New('ID', IntToStrEx(Info.LogonID)),
THintSection.New('Name', Info.WinStationName),
THintSection.New('User', Info.FullUserName)
]);
end;
{ TRectRepresenter }
class function TRectRepresenter.GetType;
begin
Result := TypeInfo(TRect);
end;
class function TRectRepresenter.Represent;
var
Rect: TRect absolute Instance;
begin
Result.Text := Format('(%d, %d) - (%d, %d) [%dx%d]', [Rect.Left, Rect.Top,
Rect.Right, Rect.Bottom, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top]);
end;
initialization
// Make all representers available at runtime for RTTI
CompileTimeInclude(TNtUnicodeStringRepresenter);
CompileTimeInclude(TNtAnsiStringRepresenter);
CompileTimeInclude(TClientIdRepresenter);
CompileTimeInclude(TProcessIdRepresenter);
CompileTimeInclude(TProcessId32Representer);
CompileTimeInclude(TThreadIdRepresenter);
CompileTimeInclude(TThreadId32Representer);
CompileTimeInclude(TNtStatusRepresenter);
CompileTimeInclude(THResultRepresenter);
CompileTimeInclude(TWin32ErrorRepresenter);
CompileTimeInclude(TNtxStatusRepresenter);
CompileTimeInclude(TLargeIntegerRepresenter);
CompileTimeInclude(TULargeIntegerRepresenter);
CompileTimeInclude(TUnixTimeRepresenter);
CompileTimeInclude(TSidRepresenter);
CompileTimeInclude(TSidAndAttributesRepresenter);
CompileTimeInclude(TISidRepresenter);
CompileTimeInclude(TGroupRepresenter);
CompileTimeInclude(TLogonIdRepresenter);
CompileTimeInclude(TSessionIdRepresenter);
CompileTimeInclude(TRectRepresenter);
end.