-
Notifications
You must be signed in to change notification settings - Fork 1
/
types_const_u.pas
551 lines (503 loc) · 14 KB
/
types_const_u.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
unit types_const_u;
interface
uses
Windows, Classes, SysUtils;
type
NTSTATUS = UINT;
USHORT = Word;
LONG = Longint;
PVOID = Pointer;
ULONGLONG = UInt64;
ULONG_PTR = NativeUInt;
SIZE_T = ULONG_PTR;
UNICODE_STRING = packed record
Length: USHORT;
MaximumLength: USHORT;
Buffer: PWideChar;
end;
PUNICODE_STRING = ^UNICODE_STRING;
KPRIORITY = LONG;
CLIENT_ID = packed record
UniqueProcess: THandle;
UniqueThread: THandle;
end;
_KWAIT_REASON = (
Executive,
FreePage,
PageIn,
PoolAllocation,
DelayExecution,
Suspended,
UserRequest,
WrExecutive,
WrFreePage,
WrPageIn,
WrPoolAllocation,
WrDelayExecution,
WrSuspended,
WrUserRequest,
WrEventPair,
WrQueue,
WrLpcReceive,
WrLpcReply,
WrVirtualMemory,
WrPageOut,
WrRendezvous,
WrKeyedEvent,
WrTerminated,
WrProcessInSwap,
WrCpuRateControl,
WrCalloutStack,
WrKernel,
WrResource,
WrPushLock,
WrMutex,
WrQuantumEnd,
WrDispatchInt,
WrPreempted,
WrYieldExecution,
WrFastMutex,
WrGuardedMutex,
WrRundown,
MaximumWaitReason
);
KWAIT_REASON = UINT;
SYSTEM_THREADS = packed record
KernelTime: FILETIME;
UserTime: FILETIME;
CreateTime: FILETIME;
WaitTime: ULONG;
StartAddress: PVOID;
ClientId: CLIENT_ID;
Priority: KPRIORITY;
BasePriority: LONG;
ContextSwitches: ULONG;
ThreadState: ULONG;
WaitReason: KWAIT_REASON;
Reserved: ULONG;
end;
SYSTEM_THREADS_ARRAY = array[0..1024] of SYSTEM_THREADS;
PSYSTEM_THREADS_ARRAY = ^SYSTEM_THREADS_ARRAY;
SYSTEM_PROCESS_INFORMATION = packed record
NextEntryOffset: ULONG;
NumberOfThreads: ULONG;
WorkingSetPrivateSize: LARGE_INTEGER;
HardFaultCount: ULONG;
NumberOfThreadsHighWatermark: ULONG;
CycleTime: ULONGLONG;
CreateTime: FILETIME;
UserTime: FILETIME;
KernelTime: FILETIME;
ImageName: UNICODE_STRING;
BasePriority: KPRIORITY;
ProcessId: THandle;
InheritedFromProcessId: THandle;
HandleCount: ULONG;
SessionId: ULONG;
UniqueProcessKey: ULONG_PTR;
PeakVirtualSize: SIZE_T;
VirtualSize: SIZE_T;
PageFaultCount: ULONG;
PeakWorkingSetSize: SIZE_T;
WorkingSetSize: SIZE_T;
QuotaPeakPagedPoolUsage: SIZE_T;
QuotaPagedPoolUsage: SIZE_T;
QuotaPeakNonPagedPoolUsage: SIZE_T;
QuotaNonPagedPoolUsage: SIZE_T;
PageFileUsage: SIZE_T;
PeakPageFileUsage: SIZE_T;
PrivatePageCount: SIZE_T;
ReadOperationCount: Int64;
WriteOperationCount: Int64;
OtherOperationCount: Int64;
ReadTransferCount: Int64;
WriteTransferCount: Int64;
OtherTransferCount: Int64;
// Threads: array [0 .. 0] of SYSTEM_THREADS;
end;
PSYSTEM_PROCESS_INFORMATION = ^SYSTEM_PROCESS_INFORMATION;
PDebugModule = ^TDebugModule;
TDebugModule = packed record
Reserved: array [0..1] of Cardinal;
Base: Cardinal;
Size: Cardinal;
Flags: Cardinal;
Index: Word;
Unknown: Word;
LoadCount: Word;
ModuleNameOffset: Word;
ImageName: array [0..$FF] of AnsiChar;
end;
PDebugModuleInformation = ^TDebugModuleInformation;
TDebugModuleInformation = {packed?} record
Count: Cardinal;
Modules: array [0..0] of TDebugModule;
end;
PDebugBuffer = ^TDebugBuffer;
TDebugBuffer = {packed?} record
SectionHandle: THandle;
SectionBase: Pointer;
RemoteSectionBase: Pointer;
SectionBaseDelta: Cardinal;
EventPairHandle: THandle;
Unknown: array [0..1] of Cardinal;
RemoteThreadHandle: THandle;
InfoClassMask: Cardinal;
SizeOfInfo: Cardinal;
AllocatedSize: Cardinal;
SectionSize: Cardinal;
ModuleInformation: PDebugModuleInformation;
BackTraceInformation: Pointer;
HeapInformation: Pointer;
LockInformation: Pointer;
Reserved: array [0..7] of Pointer;
end;
TNtQuerySystemInformation = function(
SystemInformationClass: ULONG;
SystemInformation: PVOID;
SystemInformationLength: ULONG;
ReturnLength: PULONG
): NTSTATUS; stdcall;
TNtQueryInformationProcess = function(
ProcessHandle: THandle;
ProcessInformationClass: ULONG;
ProcessInformation: PVOID;
ProcessInformationLength: ULONG;
ReturnLength: PULONG
): NTSTATUS; stdcall;
TNtRemoveProcessDebug = function(
ProcessHandle: THandle;
DebugObjectHandle: THandle
): NTSTATUS; stdcall;
TNtSetInformationDebugObject = function(
DebugObjectHandle: THandle;
DebugObjectInformationClass: ULONG;
DebugInformation: PVOID;
DebugInformationLength: ULONG;
ReturnLength: PULONG
): NTSTATUS; stdcall;
TNtClose = function(
Handle: THandle
): NTSTATUS; stdcall;
TFNRtlCreateQueryDebugBuffer = function(
Size: Cardinal;
EventPair: Boolean
): PDebugBuffer; stdcall;
TFNRtlQueryProcessDebugInformation = function(
ProcessId,
DebugInfoClassMask: Cardinal;
var DebugBuffer: TDebugBuffer
): NTSTATUS; stdcall;
TFNRtlDestroyQueryDebugBuffer = function(
DebugBuffer: PDebugBuffer
): NTSTATUS; stdcall;
TNtDelayExecution = procedure(
Alertable: boolean;
Interval: PInt64
); stdcall;
PNT_TIB = ^NT_TIB;
NT_TIB = packed record
ExceptionList: Pointer;
StackBase: Pointer;
StackLimit: Pointer;
SubSystemTib: Pointer;
FiberData: Pointer;
ArbitraryUserPointer: Pointer;
Self: PNT_TIB;
end;
VM_COUNTERS = packed record
PeakVirtualSize : ULONG;
VirtualSize : ULONG;
PageFaultCount : ULONG;
PeakWorkingSetSize : ULONG;
WorkingSetSize : ULONG;
QuotaPeakPagedPoolUsage : ULONG;
QuotaPagedPoolUsage : ULONG;
QuotaPeakNonPagedPoolUsage : ULONG;
QuotaNonPagedPoolUsage : ULONG;
PageFileUsage : ULONG;
PeakPageFileUsage : ULONG;
end;
IO_COUNTERS = packed record
ReadOperationCount : LARGE_INTEGER;
WriteOperationCount : LARGE_INTEGER;
OtherOperationCount : LARGE_INTEGER;
ReadTransferCount : LARGE_INTEGER;
WriteTransferCount : LARGE_INTEGER;
OtherTransferCount : LARGE_INTEGER;
end;
SYSTEM_PROCESSES = packed record
NextEntryDelta : ULONG;
ThreadCount : ULONG;
Reserved1 : array[0..5] of ULONG;
CreateTime : LARGE_INTEGER;
UserTime : LARGE_INTEGER;
KernelTime : LARGE_INTEGER;
ProcessName : UNICODE_STRING;
BasePriority : KPRIORITY;
ProcessId : ULONG;
InheritedFromProcessId : ULONG;
HandleCount : ULONG;
Reserved2 : array[0..1] of ULONG;
VmCounters : VM_COUNTERS;
PrivatePageCount : ULONG;
//
IoCounters : IO_COUNTERS;
end;
PSYSTEM_PROCESSES = ^SYSTEM_PROCESSES;
TModuleInfoObj = class
public
Base: Cardinal;
Size: Cardinal;
Flags: Cardinal;
Index: Word;
LoadCount: Word;
ImageName: string;
// th32ModuleID: Cardinal;
// th32ProcessID: Cardinal;
// GlblcntUsage: Cardinal;
// ProccntUsage: Cardinal;
// modBaseAddr: Pointer;
// modBaseSize: Cardinal;
// hModule: Cardinal;
// szModule: string;
// szExePath: string;
end;
TThreadInfoObj = class
public
ThreadID: string;
// BasePri: Cardinal;
// DeltaPri: Cardinal;
// Flags: Cardinal;
KernelTime: string;
UserTime: string;
CreateTime: string;
StartAddress: string;
Priority: string;
BasePriority: string;
ContextSwitches: string;
ThreadState: string;
WaitReason: string;
Context: _CONTEXT;
end;
const
STATUS_SUCCESS = NTSTATUS($00000000);
STATUS_INFO_LENGTH_MISMATCH = NTSTATUS($C0000004);
PDI_MODULES = $01;
THREAD_ALL_ACCESS = $1F03FF;
WAIT_REASON_STR: array [_KWAIT_REASON] of string = (
'Executive',
'Free Page',
'Page In',
'Pool Allocation',
'Delay Execution',
'Suspended',
'User Request',
'Executive',
'FreePage',
'Page In',
'Pool Allocation',
'Delay Execution',
'Suspended',
'User Request',
'Event Pair',
'Queue',
'Lpc Receive',
'Lpc Reply',
'Virtual Memory',
'Page Out',
'Rendezvous',
'Keyed Event',
'Terminated',
'Process In Swap',
'Cpu Rate Control',
'Callout Stack',
'Kernel',
'Resource',
'Push Lock',
'Mutex',
'Quantum End',
'Dispatch Int',
'Preempted',
'Yield Execution',
'Fast Mutex',
'Guarded Mutex',
'Rundown',
'Maximum Wait Reason'
);
function OpenThread(dwDesiredAccess: cardinal; bInheritHandle: boolean;
dwThreadId: cardinal): cardinal; stdcall; external 'kernel32.dll';
function FileTimeToDateTime(const AFileTime: TFileTime): string;
function FileTimeToTime(const AFileTime: TFileTime): string;
function ContextToStr(const AContext: _CONTEXT): string;
implementation
const
TIME_FORMAT_STR: string = '%d:%d:%d.%d';
function FileTimeToDateTime(const AFileTime: TFileTime): string;
var
ModifiedTime: TFileTime;
SystemTime: TSystemTime;
begin
if (AFileTime.dwLowDateTime = 0) and (AFileTime.dwHighDateTime = 0) then
Exit;
FileTimeToLocalFileTime(AFileTime, ModifiedTime);
FileTimeToSystemTime(ModifiedTime, SystemTime);
Result := Format(TIME_FORMAT_STR, [SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond,
SystemTime.wMilliseconds
]
);
end;
function FileTimeToTime(const AFileTime: TFileTime): string;
var
local: TSystemTime;
begin
FileTimeToSystemTime(AFileTime, local);
Result := Format(TIME_FORMAT_STR, [local.wHour,
local.wMinute,
local.wSecond,
local.wMilliseconds
]
);
end;
function ContextToStr(const AContext: _CONTEXT): string;
begin
Result := Format('DR0: %x'#13#10+
'DR1: %x'#13#10+
'DR2: %x'#13#10+
'DR3: %x'#13#10+
'DR6: %x'#13#10+
'DR7: %x'#13#10#13#10+
'GS: %x'#13#10+
'FS: %x'#13#10+
'ES: %x'#13#10+
'DS: %x'#13#10#13#10+
'EDI: %x'#13#10+
'ESI: %x'#13#10+
'EBX: %x'#13#10+
'EDX: %x'#13#10+
'ECX: %x'#13#10+
'EAX: %x'#13#10#13#10+
'EBP: %x'#13#10+
'EIP: %x'#13#10+
'CS: %x'#13#10+
'FLAGS: %x'#13#10+
'ESP: %x'#13#10+
'SS: %x',
[AContext.Dr0,
AContext.Dr1,
AContext.Dr2,
AContext.Dr3,
AContext.Dr6,
AContext.Dr7,
AContext.SegGs,
AContext.SegFs,
AContext.SegEs,
AContext.SegDs,
AContext.Edi,
AContext.Esi,
AContext.Ebx,
AContext.Edx,
AContext.Ecx,
AContext.Eax,
AContext.Ebp,
AContext.Eip,
AContext.SegCs,
AContext.EFlags,
AContext.Esp,
AContext.SegSs
]);
end;
end.
//uses SysConst, JwaNative, JwaWinType, JwaNtStatus;
//
//function NtErrorMessage(Code: NTSTATUS): String;
//var
// hMod: Cardinal;
// Buffer: array[0..255] of Char;
// Len: Integer;
//begin
// hMod:=GetModuleHandle(ntdll);
// Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or
// FORMAT_MESSAGE_ARGUMENT_ARRAY or FORMAT_MESSAGE_FROM_HMODULE, Pointer(hMod), Code, 0, Buffer,
// SizeOf(Buffer), nil);
// while (Len > 0) and (Buffer[Len - 1] in [#0..#32, '.']) do Dec(Len);
// SetString(Result, Buffer, Len);
//end;
//
//function NtSuccess(Code: NTSTATUS): Boolean;
//begin
// Result:=Code >= 0;
//end;
//
//procedure NtCheck(Code: NTSTATUS);
//begin
// if not NtSuccess(Code) then begin
// raise EOSError.CreateResFmt(@SOSError, [Code,NtErrorMessage(Code)]);
// end;
//end;
//
//function GetSystemInfoTable(ASystemInformationClass: SYSTEM_INFORMATION_CLASS;
// out Info: Pointer; out Size: Cardinal): NTSTATUS;
//var
// SystemInfo: TSystemInfo;
// ReturnSize: Cardinal;
// ContinueFlag: Boolean;
//begin
// GetSystemInfo(SystemInfo);
// Size:=SystemInfo.dwPageSize;
// repeat
// Info:=VirtualAlloc(nil,Size,MEM_COMMIT or MEM_RESERVE,PAGE_READWRITE);
// if Info = nil then
// RaiseLastOSError;
// Result:=NtQuerySystemInformation(ASystemInformationClass,Info,Size,@ReturnSize);
// ContinueFlag:=Result = STATUS_INFO_LENGTH_MISMATCH;
// if ContinueFlag then begin
// Win32Check(VirtualFree(Info,0,MEM_RELEASE));
// if ReturnSize = 0 then
// Size:=Size + SystemInfo.dwPageSize
// else
// Size:=ReturnSize;
// end;
// until not ContinueFlag;
//end;
//
//function GetThreadIdByHandle(Handle: THandle): Cardinal;
//var
// Info: _THREAD_BASIC_INFORMATION;
//begin
// NtCheck(NtQueryInformationThread(Handle,ThreadBasicInformation,@Info,
// SizeOf(_THREAD_BASIC_INFORMATION),nil));
// Result:=Info.ClientId.UniqueThread;
//end;
//
//function IsThreadSuspended(ThreadID: Cardinal): Boolean;
//var
// Info, Entry: Pointer;
// Size, i: Cardinal;
// ContinueFlag: Boolean;
//begin
// Result:=false;
// NtCheck(GetSystemInfoTable(SystemProcessesAndThreadsInformation,Info,Size));
// try
// Entry:=Info;
// ContinueFlag:=true;
// while ContinueFlag do begin
// for i:=0 to PSYSTEM_PROCESSES(Entry)^.ThreadCount - 1 do begin
// if PSYSTEM_PROCESSES(Entry)^.Threads[i].ClientId.UniqueThread = ThreadID
// then begin
// Result:=PSYSTEM_PROCESSES(Entry)^.Threads[i].WaitReason = Suspended;
// ContinueFlag:=false;
// Break;
// end;
// end;
// if (PSYSTEM_PROCESSES(Entry)^.NextEntryDelta <> 0) then
// Cardinal(Entry):=Cardinal(Entry) + PSYSTEM_PROCESSES(Entry)^.NextEntryDelta
// else
// ContinueFlag:=false;
// end;
// finally
// VirtualFree(Info,0,MEM_RELEASE);
// end;
//end;