-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUtils.Processes.Snapshots.pas
More file actions
417 lines (346 loc) · 11 KB
/
NtUtils.Processes.Snapshots.pas
File metadata and controls
417 lines (346 loc) · 11 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
unit NtUtils.Processes.Snapshots;
{
This module provides several modes of process enumeration.
}
interface
uses
Ntapi.WinNt, Ntapi.ntexapi, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntseapi,
Ntapi.Versions, DelphiApi.Reflection, DelphiUtils.Arrays, NtUtils;
type
TProcessOpenByNameOptions = set of (
pnCurrentSessionOnly,
pnAllowAmbiguousMatch,
pnAllowShortNames,
pnAllowPIDs,
pnCaseSensitive
);
TProcessImageFilterOptions = set of (
pfAllowShortNames,
pfCaseSensitive
);
// Process snapshotting mode
TPsSnapshotMode = (
psNormal, // Basic info about processes & threads
psSession, // Same as normal, but only within one session
psExtended, // Some additional info about threads
psFull // Everything (requires Administrator or NT SERVICE\DPS)
);
TProcessFullExtension = record
[Aggregate] DiskCounters: TProcessDiskCounters;
ContextSwitches: UInt64;
Flags: TProcessExtFlags;
Classification: TSystemProcessClassification;
User: ISid;
[MinOSVersion(OsWin10RS2)] PackageFullName: String;
[MinOSVersion(OsWin10RS2)] EnergyValues: TProcessEnergyValues;
[MinOSVersion(OsWin10RS2)] AppID: String;
[MinOSVersion(OsWin10RS2)] SharedCommitCharge: NativeUInt;
[MinOSVersion(OsWin10RS2)] JobObjectID: Cardinal;
[MinOSVersion(OsWin10RS2)] ProcessSequenceNumber: UInt64;
end;
TThreadEntry = record
[Aggregate] Basic: TSystemThreadInformation;
[Aggregate] Extended: TSystemThreadInformationExtension; // extended & full
end;
TProcessEntry = record
ImageName: String; // including path in case of full mode
Basic: TSystemProcessInformationFixed;
Full: TProcessFullExtension; // full only
Threads: TArray<TThreadEntry>; // see above
end;
PProcessEntry = ^TProcessEntry;
// Snapshot processes on the system
function NtxEnumerateProcesses(
out Processes: TArray<TProcessEntry>;
Mode: TPsSnapshotMode = psNormal;
SessionId: TSessionId = TSessionId(-1)
): TNtxStatus;
// Open a process by an image name
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpForBypassingChecks)]
function NtxOpenProcessByName(
out hxProcess: IHandle;
const ImageName: String;
DesiredAccess: TProcessAccessMask;
Options: TProcessOpenByNameOptions = [];
HandleAttributes: TObjectAttributesFlags = 0
): TNtxStatus;
{ Helper functions }
// Filter processes by image
function ByImage(
const ImageName: String;
Options: TProcessImageFilterOptions = []
): TCondition<TProcessEntry>;
// Filter processes by ID
function ByPid(
PID: TProcessId
): TCondition<TProcessEntry>;
// Find a process in the snapshot using a process ID
function NtxFindProcessById(
const Processes: TArray<TProcessEntry>;
PID: TProcessId
): PProcessEntry;
// Find a process in the snapshot using a thread ID
function NtxFindProcessByThreadId(
const Processes: TArray<TProcessEntry>;
TID: TThreadId
): PProcessEntry;
// A parent checker to use with TArrayHelper.BuildTree<TProcessEntry>
function ParentProcessChecker(
const Parent: TProcessEntry;
const Child: TProcessEntry
): Boolean;
implementation
uses
Ntapi.ntstatus, Ntapi.ntpebteb, NtUtils.Security.Sid, NtUtils.System,
NtUtils.Processes, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxpExtractProcesses(
[in] Buffer: PSystemProcessInformationFixed
): TArray<Pointer>;
var
pProcess: PSystemProcessInformationFixed;
Count, i: Integer;
begin
// Count processes
Count := 0;
pProcess := Buffer;
repeat
Inc(Count);
if pProcess.NextEntryOffset = 0 then
Break
else
pProcess := Pointer(UIntPtr(pProcess) + pProcess.NextEntryOffset);
until False;
SetLength(Result, Count);
// Iterate
i := 0;
pProcess := Buffer;
repeat
// Save
Result[i] := pProcess;
// Find the next entry
if pProcess.NextEntryOffset = 0 then
Break
else
pProcess := Pointer(UIntPtr(pProcess) + pProcess.NextEntryOffset);
Inc(i);
until False;
end;
procedure NtxpParseProcessExtension(
out Extension: TProcessFullExtension;
Buffer: PSystemProcessInformationExtension
);
begin
Extension.DiskCounters := Buffer.DiskCounters;
Extension.ContextSwitches := Buffer.ContextSwitches;
Extension.Flags := Buffer.Flags and SYSTEM_PROCESS_VALID_MASK;
Extension.Classification := Buffer.Classification;
if Buffer.UserSidOffset <> 0 then
RtlxCopySid(Buffer.UserSid, Extension.User);
if RtlOsVersionAtLeast(OsWin10RS2) then
begin
Extension.PackageFullName := Buffer.PackageFullName;
Extension.EnergyValues := Buffer.EnergyValues;
Extension.AppId := Buffer.AppId;
Extension.SharedCommitCharge := Buffer.SharedCommitCharge;
Extension.JobObjectId := Buffer.JobObjectId;
Extension.ProcessSequenceNumber := Buffer.ProcessSequenceNumber;
end;
end;
function NtxpParseProcesses(
[in] Buffer: PSystemProcessInformationFixed;
Mode: TPsSnapshotMode
): TArray<TProcessEntry>;
var
Processes: TArray<Pointer>;
pProcess: PSystemProcessInformation;
pProcessExtended: PSystemExtendedProcessInformation;
pThreadExtended: PSystemExtendedThreadInformation;
i, j: Integer;
begin
Processes := NtxpExtractProcesses(Buffer);
SetLength(Result, Length(Processes));
for i := 0 to High(Processes) do
begin
pProcess := Processes[i];
pProcessExtended := Processes[i];
// Save process (the structure is the same)
Result[i].Basic := pProcess.Process;
Result[i].ImageName := pProcess.Process.ImageName.ToString;
Result[i].Basic.ImageName.Buffer := PWideChar(Result[i].ImageName);
if pProcess.Process.ProcessId = 0 then
Result[i].ImageName := 'System Idle Process';
// Save threads
SetLength(Result[i].Threads, pProcess.Process.NumberOfThreads);
case Mode of
psNormal, psSession:
for j := 0 to High(Result[i].Threads) do
// Basic only
Result[i].Threads[j].Basic := pProcess
.Threads{$R-}[j]{$IFDEF R+}{$R+}{$ENDIF};
psExtended, psFull:
begin
pThreadExtended := @pProcessExtended.Threads[0];
for j := 0 to High(Result[i].Threads) do
begin
// Save both basic and extended
Result[i].Threads[j].Basic := pThreadExtended.ThreadInfo;
Result[i].Threads[j].Extended := pThreadExtended.Extension;
Inc(pThreadExtended);
end;
// Full information follows the threads
if Mode = psFull then
NtxpParseProcessExtension(Result[i].Full, Pointer(pThreadExtended));
end;
end;
end;
end;
function NtxEnumerateSessionProcesses(
SessionId: TSessionId;
out Processes: TArray<TProcessEntry>
): TNtxStatus;
var
xMemory: IMemory;
Required: Cardinal;
Data: TSystemSessionProcessInformation;
begin
Result.Location := 'NtQuerySystemInformation';
Result.LastCall.UsesInfoClass(SystemSessionProcessInformation, icQuery);
// Use provided session or fallback to the current one
if SessionId = TSessionId(-1) then
Data.SessionId := RtlGetCurrentPeb.SessionID
else
Data.SessionId := SessionId;
xMemory := Auto.AllocateDynamic(Data.SizeOfBuf);
repeat
// Prepare the request that we pass as an input.
// It describes the buffer to fill and contains the session ID.
Data.SizeOfBuf := xMemory.Size;
Data.Buffer := xMemory.Data;
Required := 0;
Result.Status := NtQuerySystemInformation(SystemSessionProcessInformation,
@Data, SizeOf(Data), @Required);
until not NtxExpandBufferEx(Result, xMemory, Required, Grow12Percent);
if Result.IsSuccess then
Processes := NtxpParseProcesses(xMemory.Data, psSession);
end;
function NtxEnumerateProcesses;
const
// We don't want to use a huge initial buffer since system spends
// more time probing it rather than enumerating processes.
InitialBuffer: array [TPsSnapshotMode] of Cardinal = (
384 * 1024, 192 * 1024, 576 * 1024, 640 * 1024);
var
InfoClass: TSystemInformationClass;
Memory: IMemory;
begin
case Mode of
psNormal: InfoClass := SystemProcessInformation;
psExtended: InfoClass := SystemExtendedProcessInformation;
psFull: InfoClass := SystemFullProcessInformation;
else
Result := NtxEnumerateSessionProcesses(SessionId, Processes);
Exit;
end;
Result := NtxQuerySystem(InfoClass, Memory, InitialBuffer[Mode],
Grow12Percent);
if Result.IsSuccess then
Processes := NtxpParseProcesses(Memory.Data, Mode);
end;
function NtxOpenProcessByName;
var
Mode: TPsSnapshotMode;
Processes: TArray<TProcessEntry>;
FilterOptions: TProcessImageFilterOptions;
i: Integer;
PID: TProcessId;
begin
if (pnAllowPIDs in Options) and RtlxStrToUIntPtr(ImageName, UIntPtr(PID)) then
begin
Result := NtxOpenProcess(hxProcess, PID, DesiredAccess, HandleAttributes);
Exit;
end;
if pnCurrentSessionOnly in Options then
Mode := psSession
else
Mode := psNormal;
// Find all processes
Result := NtxEnumerateProcesses(Processes, Mode);
FilterOptions := [];
if pnAllowShortNames in Options then
Include(FilterOptions, pfAllowShortNames);
if pnCaseSensitive in Options then
Include(FilterOptions, pfCaseSensitive);
// Keep only matching image names
TArray.FilterInline<TProcessEntry>(Processes, ByImage(ImageName,
FilterOptions));
if Length(Processes) <= 0 then
begin
Result.Location := 'NtxOpenProcessByName';
Result.Status := STATUS_NOT_FOUND;
Exit;
end;
if not (pnAllowAmbiguousMatch in Options) and (Length(Processes) > 1) then
begin
// Ambiguous match is not allowed
Result.Location := 'NtxOpenProcessByName';
Result.Status := STATUS_OBJECT_NAME_COLLISION;
Exit;
end;
// Open the first one we can access
for i := 0 to High(Processes) do
begin
Result := NtxOpenProcess(hxProcess, Processes[i].Basic.ProcessID,
DesiredAccess, HandleAttributes);
if Result.IsSuccess then
Break;
end
end;
{ Helper functions }
function ByPid;
begin
Result := function (const ProcessEntry: TProcessEntry): Boolean
begin
Result := ProcessEntry.Basic.ProcessID = PID;
end;
end;
function ByImage;
begin
Result := function (const ProcessEntry: TProcessEntry): Boolean
begin
Result := RtlxCompareStrings(ProcessEntry.ImageName, ImageName,
pfCaseSensitive in Options) = 0;
if not Result and (pfAllowShortNames in Options) then
Result := RtlxCompareStrings(ProcessEntry.ImageName, ImageName + '.exe',
pfCaseSensitive in Options) = 0;
end;
end;
function NtxFindProcessById;
var
i: Integer;
begin
for i := 0 to High(Processes) do
if Processes[i].Basic.ProcessId = PID then
Exit(@Processes[i]);
Result := nil;
end;
function NtxFindProcessByThreadId;
var
i, j: Integer;
begin
for i := 0 to High(Processes) do
for j := 0 to High(Processes[i].Threads) do
if Processes[i].Threads[j].Basic.ClientID.UniqueThread = TID then
Exit(@Processes[i]);
Result := nil;
end;
function ParentProcessChecker;
begin
// Note: since PIDs can be reused we need to ensure
// that parents were created earlier than children.
Result := (Child.Basic.InheritedFromProcessId = Parent.Basic.ProcessId)
and (Child.Basic.CreateTime > Parent.Basic.CreateTime)
end;
end.