-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUtils.Objects.Compare.pas
More file actions
256 lines (210 loc) · 6.35 KB
/
NtUtils.Objects.Compare.pas
File metadata and controls
256 lines (210 loc) · 6.35 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
unit NtUtils.Objects.Compare;
{
This modules provides a routine that checks if two handles point to the same
kernel object.
}
interface
uses
Ntapi.WinNt, NtUtils;
{ Helper functions }
type
THashingRoutine = function (
const hxObject: IHandle;
out Hash: UInt64
): TNtxStatus;
// Compute an object hash. Can reopen the object for required access.
function NtxQueryHandleHash(
hxObject: IHandle;
HashingRoutine: THashingRoutine;
RequiredAccess: TAccessMask;
out Hash: UInt64;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Compare two objects by computing their hashes
function NtxCompareHandlesByHash(
const hxObject1: IHandle;
const hxObject2: IHandle;
HashingRoutine: THashingRoutine;
RequiredAccess: TAccessMask;
out Equal: Boolean
): TNtxStatus;
// Hashing routines
function NtxHashToken(const hxToken: IHandle; out Hash: UInt64): TNtxStatus;
function NtxHashProcess(const hxProcess: IHandle; out Hash: UInt64): TNtxStatus;
function NtxHashThread(const hxThread: IHandle; out Hash: UInt64): TNtxStatus;
{ Generic comparison }
// Check whether two handles point to the same kernel object
function NtxCompareObjects(
out Equal: Boolean;
hxObject1: IHandle;
hxObject2: IHandle;
[opt] ObjectTypeName: String = ''
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntdef, Ntapi.ntobapi, Ntapi.ntpsapi, Ntapi.ntseapi,
NtUtils.Objects, NtUtils.Ldr, NtUtils.Objects.Snapshots, DelphiUtils.Arrays,
NtUtils.Tokens, NtUtils.Tokens.Info, NtUtils.Processes.Info, NtUtils.Threads;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxQueryHandleHash;
begin
Result := NtxEnsureAccessHandle(hxObject, TOKEN_QUERY, 0, 0, AccessMaskType);
// Try to perform hashing
if Result.IsSuccess then
Result := HashingRoutine(hxObject, Hash);
end;
function NtxCompareHandlesByHash;
var
Hash1, Hash2: UInt64;
begin
// Hash the first handle
Result := NtxQueryHandleHash(hxObject1, HashingRoutine, RequiredAccess, Hash1);
if not Result.IsSuccess then
Exit;
// Hash the second handle
Result := NtxQueryHandleHash(hxObject2, HashingRoutine, RequiredAccess, Hash2);
if not Result.IsSuccess then
Exit;
Equal := Hash1 = Hash2;
end;
function NtxHashToken;
var
Stats: TTokenStatistics;
begin
// Use TokenId as a hash value
Result := NtxToken.Query(hxToken, TokenStatistics, Stats);
if Result.IsSuccess then
Hash := UInt64(Stats.TokenId);
end;
function NtxHashProcess;
var
Info: TProcessBasicInformation;
begin
// Use ProcessId as a hash value
Result := NtxProcess.Query(hxProcess, ProcessBasicInformation, Info);
if Result.IsSuccess then
Hash := UInt64(Info.UniqueProcessId);
end;
function NtxHashThread;
var
Info: TThreadBasicInformation;
begin
// Use ThreadId as a hash value
Result := NtxThread.Query(hxThread, ThreadBasicInformation, Info);
if Result.IsSuccess then
Hash := UInt64(Info.ClientId.UniqueThread);
end;
function ExpandCustomPseudoHandles(var hxObject: IHandle): TNtxStatus;
begin
// Only tokens for now
if (hxObject.Handle = NtCurrentProcessToken) or
(hxObject.Handle = NtCurrentThreadToken) or
(hxObject.Handle = NtCurrentEffectiveToken) then
Result := NtxExpandToken(hxObject, TOKEN_QUERY)
else
Result := NtxSuccess;
end;
function NtxCompareObjects;
var
Type1, Type2: TObjectTypeInfo;
Handles: TArray<TSystemHandleEntry>;
HashFunction: THashingRoutine;
RequiredAccess: TAccessMask;
i, j: Integer;
begin
if not Assigned(hxObject1) or not Assigned(hxObject2) then
begin
Result.Location := 'NtxCompareObjects';
Result.Status := STATUS_INVALID_HANDLE;
Exit;
end;
if hxObject1.Handle = hxObject2.Handle then
begin
Equal := True;
Exit(NtxSuccess);
end;
// Add support for token pseudo-handles
Result := ExpandCustomPseudoHandles(hxObject1);
if not Result.IsSuccess then
Exit;
Result := ExpandCustomPseudoHandles(hxObject2);
if not Result.IsSuccess then
Exit;
// Win 10 TH+ makes things way easier
if LdrxCheckDelayedImport(delayed_NtCompareObjects).IsSuccess then
begin
Result.Location := 'NtCompareObjects';
Result.Status := NtCompareObjects(hxObject1.Handle, hxObject2.Handle);
Equal := Result.Status <> STATUS_NOT_SAME_OBJECT;
Exit;
end;
// Get object's type if the caller didn't specify it
if ObjectTypeName = '' then
if NtxQueryTypeObject(hxObject1, Type1).IsSuccess and
NtxQueryTypeObject(hxObject2, Type2).IsSuccess then
begin
if Type1.TypeName <> Type2.TypeName then
begin
Equal := False;
Exit(NtxSuccess);
end;
ObjectTypeName := Type1.TypeName;
end;
// Perform type-specific comparison
if ObjectTypeName <> '' then
begin
if ObjectTypeName = 'Token' then
begin
HashFunction := NtxHashToken;
RequiredAccess := TOKEN_QUERY;
end
else if ObjectTypeName = 'Process' then
begin
HashFunction := NtxHashProcess;
RequiredAccess := PROCESS_QUERY_LIMITED_INFORMATION;
end
else if ObjectTypeName = 'Thread' then
begin
HashFunction := NtxHashThread;
RequiredAccess := THREAD_QUERY_LIMITED_INFORMATION;
end
else
begin
HashFunction := nil;
RequiredAccess := 0;
end;
if Assigned(HashFunction) and NtxCompareHandlesByHash(hxObject1, hxObject2,
HashFunction, RequiredAccess, Equal).IsSuccess then
Exit(NtxSuccess);
end;
// The last resort is to proceed via a handle snapshot
Result := NtxEnumerateHandles(Handles);
if not Result.IsSuccess then
Exit;
TArray.FilterInline<TSystemHandleEntry>(Handles,
ByProcess(NtCurrentProcessId));
for i := 0 to High(Handles) do
if Handles[i].HandleValue = hxObject1.Handle then
begin
for j := 0 to High(Handles) do
if Handles[j].HandleValue = hxObject2.Handle then
begin
if not Assigned(Handles[i].PObject) then
begin
// Kernel address leak prevention stops us from comparing pointers
Result.Location := 'NtxCompareObjects';
Result.LastCall.ExpectedPrivilege := SE_DEBUG_PRIVILEGE;
Result.Status := STATUS_PRIVILEGE_NOT_HELD;
Exit;
end;
Equal := (Handles[i].PObject = Handles[j].PObject);
Exit(NtxSuccess);
end;
Break;
end;
Result.Location := 'NtxCompareObjects';
Result.Status := STATUS_INVALID_HANDLE;
end;
end.