-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Objects.Compare.pas
246 lines (200 loc) · 6.24 KB
/
NtUtils.Objects.Compare.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
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
): 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.
// Returns STATUS_SUCCESS (same), STATUS_NOT_SAME_OBJECT, or other error
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
// Try to perform hashing
Result := HashingRoutine(hxObject, Hash);
// If necessary, reopen the object and try again
if (Result.Status = STATUS_ACCESS_DENIED) and
NtxReopenHandle(hxObject, RequiredAccess).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;
Name1, Name2: String;
Handles: TArray<TSystemHandleEntry>;
i, j: Integer;
begin
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;
end;
ObjectTypeName := Type1.TypeName;
end;
// Perform type-specific comparison
if ObjectTypeName <> '' then
begin
Result.Status := STATUS_OBJECT_TYPE_MISMATCH;
if ObjectTypeName = 'Token' then
Result := NtxCompareHandlesByHash(hxObject1, hxObject2, NtxHashToken,
TOKEN_QUERY, Equal)
else
if ObjectTypeName = 'Process' then
Result := NtxCompareHandlesByHash(hxObject1, hxObject2, NtxHashProcess,
PROCESS_QUERY_LIMITED_INFORMATION, Equal)
else
if ObjectTypeName = 'Thread' then
Result := NtxCompareHandlesByHash(hxObject1, hxObject2, NtxHashThread,
THREAD_QUERY_LIMITED_INFORMATION, Equal);
if Result.IsSuccess then
Exit;
end;
// Note: desktops with the same name located in different window
// station appear the same, although they are not.
// Compare named objects
if NtxQueryNameObject(hxObject1, Name1).IsSuccess and
NtxQueryNameObject(hxObject2, Name2).IsSuccess then
if (Name1 <> Name2) then
begin
Equal := False;
Exit(NtxSuccess);
end
else if (Name1 <> '') and (ObjectTypeName <> 'Desktop') then
begin
Equal := True;
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
for j := i + 1 to High(Handles) do
if Handles[j].HandleValue = hxObject2.Handle then
begin
Equal := (Handles[i].PObject = Handles[j].PObject);
Exit;
end;
Result.Location := 'NtxCompareObjects';
Result.Status := STATUS_INVALID_HANDLE;
end;
end.