-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeAPI.pas
855 lines (723 loc) · 26.8 KB
/
NativeAPI.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
{
Îïèñàíèå ôóíêöèé è ñòðóêòóð Native API äëÿ Delphi by Ms-Rem.
Last update: 19.04.2005
}
unit NativeAPI;
interface
uses windows;
type
NTStatus = cardinal;
PVOID = pointer;
USHORT = WORD;
UCHAR = byte;
PWSTR = PWideChar;
CONST
NTDLL = 'ntdll.dll';
FILE_ANY_ACCESS = $0000; // any type
FILE_READ_ACCESS = $0001; // file & pipe
FILE_READ_DATA = $0001; // file & pipe
FILE_WRITE_ACCESS = $0002; // file & pipe
FILE_WRITE_DATA = $0002; // file & pipe
FILE_CREATE_PIPE_INSTANCE = $0004; // named pipe
FILE_READ_ATTRIBUTES = $0080; // all types
FILE_WRITE_ATTRIBUTES = $0100; // all types
STANDARD_RIGHTS_ALL = $001F0000;
FILE_ALL_ACCESS = FILE_READ_ACCESS or
FILE_WRITE_ACCESS or
FILE_CREATE_PIPE_INSTANCE or
FILE_READ_ATTRIBUTES or
FILE_WRITE_ATTRIBUTES or
STANDARD_RIGHTS_ALL;
CONST //Ñòàòóñ êîíñòàíòû
STATUS_SUCCESS = NTStatus($00000000);
STATUS_ACCESS_DENIED = NTStatus($C0000022);
STATUS_INFO_LENGTH_MISMATCH = NTStatus($C0000004);
SEVERITY_ERROR = NTStatus($C0000000);
const
FILE_DEVICE_UNKNOWN = $00000022;
const
METHOD_BUFFERED = 0;
METHOD_IN_DIRECT = 1;
METHOD_OUT_DIRECT = 2;
METHOD_NEITHER = 3;
const// SYSTEM_INFORMATION_CLASS
SystemBasicInformation = 0;
SystemProcessorInformation = 1;
SystemPerformanceInformation = 2;
SystemTimeOfDayInformation = 3;
SystemNotImplemented1 = 4;
SystemProcessesAndThreadsInformation = 5;
SystemCallCounts = 6;
SystemConfigurationInformation = 7;
SystemProcessorTimes = 8;
SystemGlobalFlag = 9;
SystemNotImplemented2 = 10;
SystemModuleInformation = 11;
SystemLockInformation = 12;
SystemNotImplemented3 = 13;
SystemNotImplemented4 = 14;
SystemNotImplemented5 = 15;
SystemHandleInformation = 16;
SystemObjectInformation = 17;
SystemPagefileInformation = 18;
SystemInstructionEmulationCounts = 19;
SystemInvalidInfoClass = 20;
SystemCacheInformation = 21;
SystemPoolTagInformation = 22;
SystemProcessorStatistics = 23;
SystemDpcInformation = 24;
SystemNotImplemented6 = 25;
SystemLoadImage = 26;
SystemUnloadImage = 27;
SystemTimeAdjustment = 28;
SystemNotImplemented7 = 29;
SystemNotImplemented8 = 30;
SystemNotImplemented9 = 31;
SystemCrashDumpInformation = 32;
SystemExceptionInformation = 33;
SystemCrashDumpStateInformation = 34;
SystemKernelDebuggerInformation = 35;
SystemContextSwitchInformation = 36;
SystemRegistryQuotaInformation = 37;
SystemLoadAndCallImage = 38;
SystemPrioritySeparation = 39;
SystemNotImplemented10 = 40;
SystemNotImplemented11 = 41;
SystemInvalidInfoClass2 = 42;
SystemInvalidInfoClass3 = 43;
SystemTimeZoneInformation = 44;
SystemLookasideInformation = 45;
SystemSetTimeSlipEvent = 46;
SystemCreateSession = 47;
SystemDeleteSession = 48;
SystemInvalidInfoClass4 = 49;
SystemRangeStartInformation = 50;
SystemVerifierInformation = 51;
SystemAddVerifier = 52;
SystemSessionProcessesInformation = 53;
const {ObjectTypeNumber const}
OB_TYPE_UNKNOWN = 00;
OB_TYPE_TYPE = 01;
OB_TYPE_DIRECTORY = 02;
OB_TYPE_SYMBOLIC_LINK = 03;
OB_TYPE_TOKEN = 04;
OB_TYPE_PROCESS = 05;
OB_TYPE_THREAD = 06;
OB_TYPE_JOB = 07;
OB_TYPE_EVENT = 08;
OB_TYPE_EVENT_PAIR = 09;
OB_TYPE_MUTANT = 10;
OB_TYPE_SEMAPHORE = 12;
OB_TYPE_TIMER = 13;
OB_TYPE_PROFILE = 14;
OB_TYPE_WINDOW_STATION = 15;
OB_TYPE_DESKTOP = 16;
OB_TYPE_SECTION = 17;
OB_TYPE_KEY = 18;
OB_TYPE_PORT = 19;
OB_TYPE_WAITABLE_PORT = 20;
OB_TYPE_IO_COMPLETION = 25;
OB_TYPE_FILE = 28;
type
PClientID = ^TClientID;
TClientID = packed record
UniqueProcess:cardinal;
UniqueThread:cardinal;
end;
PUnicodeString = ^TUnicodeString;
TUnicodeString = packed record
Length: Word;
MaximumLength: Word;
Buffer: PWideChar;
end;
PFILE_NAME_INFORMATION = ^FILE_NAME_INFORMATION;
FILE_NAME_INFORMATION = packed record
FileNameLength: dword;
FileName: array [0..0] of WideChar;
end;
PAnsiString = ^TAnsiString;
TAnsiString = packed record
Length: Word;
MaximumLength: Word;
Buffer: PChar;
end;
TPoolType = (
NonPagedPool,
PagedPool,
NonPagedPoolMustSucceed,
DontUseThisType,
NonPagedPoolCacheAligned,
PagedPoolCacheAligned,
NonPagedPoolCacheAlignedMustS,
MaxPoolType,
NonPagedPoolSession, // !!! NonPagedPoolSession = 32
PagedPoolSession,
NonPagedPoolMustSucceedSession,
DontUseThisTypeSession,
NonPagedPoolCacheAlignedSession,
PagedPoolCacheAlignedSession,
NonPagedPoolCacheAlignedMustSSession
);
{ // Value Query Set
TObjectInformationClass = (
ObjectBasicInformation, // 0 Y N
ObjectNameInformation, // 1 Y N
ObjectTypeInformation, // 2 Y N
ObjectAllTypesInformation, // 3 Y N
ObjectHandleInformation // 4 Y Y
);
}
// Information Class 0
PObjectBasicInformation = ^TObjectBasicInformation;
TObjectBasicInformation = packed record
Attributes: ULONG;
GrantedAccess: ACCESS_MASK;
HandleCount: ULONG;
PointerCount: ULONG;
PagedPoolUsage: ULONG;
NonPagedPoolUsage: ULONG;
Reserved: array[0..2] of ULONG;
NameInformationLength: ULONG;
TypeInformationLength: ULONG;
SecurityDescriptorLength: ULONG;
CreateTime: LARGE_INTEGER;
end;
// Information Class 1
PObjectNameInformation = ^TObjectNameInformation;
TObjectNameInformation = packed record
Name: TUnicodeString;
end;
// Information Class 2
PObjectTypeInformation = ^TObjectTypeInformation;
TObjectTypeInformation = packed record
Name: TUnicodeString;
ObjectCount: ULONG;
HandleCount: ULONG;
Reserved1: array[0..3] of ULONG;
PeakObjectCount: ULONG;
PeakHandleCount: ULONG;
Reserved2: array[0..3] of ULONG;
InvalidAttributes: ULONG;
GenericMapping: GENERIC_MAPPING;
ValidAccess: ULONG;
Unknown: UCHAR;
MaintainHandleDatabase: Boolean;
PoolType: TPoolType;
PagedPoolUsage: ULONG;
NonPagedPoolUsage: ULONG;
end;
// Information Class 3
PObjectAllTypesInformation = ^TObjectAllTypesInformation;
TObjectAllTypesInformation = packed record
NumberOfTypes: ULONG;
TypeInformation: TObjectTypeInformation;
end;
// Information Class 4
PObjectHandleInformation = ^TObjectHandleInformation;
TObjectHandleInformation = packed record
Inherit: Boolean;
ProtectFromClose: Boolean;
end;
PTHREAD_BASIC_INFORMATION = ^THREAD_BASIC_INFORMATION;
THREAD_BASIC_INFORMATION = packed record
ExitStatus: BOOL;
TebBaseAddress: pointer;
ClientId: TClientID;
AffinityMask: DWORD;
Priority: dword;
BasePriority: dword;
end;
PSYSTEM_HANDLE_INFORMATION = ^SYSTEM_HANDLE_INFORMATION;
SYSTEM_HANDLE_INFORMATION = packed record
ProcessId: dword;
ObjectTypeNumber: byte;
Flags: byte;
Handle: word;
pObject: pointer;
GrantedAccess: dword;
end;
PSYSTEM_HANDLE_INFORMATION_EX = ^SYSTEM_HANDLE_INFORMATION_EX;
SYSTEM_HANDLE_INFORMATION_EX = packed record
NumberOfHandles: dword;
Information: array [0..0] of SYSTEM_HANDLE_INFORMATION;
end;
PSYSTEM_LOAD_IMAGE = ^SYSTEM_LOAD_IMAGE;
SYSTEM_LOAD_IMAGE = packed record
ModuleName: TUnicodeString;
ModuleBase: pointer;
Unknown: pointer;
EntryPoint: pointer;
ExportDirectory: pointer;
end;
PVM_COUNTERS = ^VM_COUNTERS;
VM_COUNTERS = packed record
PeakVirtualSize,
VirtualSize,
PageFaultCount,
PeakWorkingSetSize,
WorkingSetSize,
QuotaPeakPagedPoolUsage,
QuotaPagedPoolUsage,
QuotaPeakNonPagedPoolUsage,
QuotaNonPagedPoolUsage,
PagefileUsage,
PeakPagefileUsage: dword;
end;
PIO_COUNTERS = ^IO_COUNTERS;
IO_COUNTERS = packed record
ReadOperationCount,
WriteOperationCount,
OtherOperationCount,
ReadTransferCount,
WriteTransferCount,
OtherTransferCount: LARGE_INTEGER;
end;
PSYSTEM_THREADS = ^SYSTEM_THREADS;
SYSTEM_THREADS = packed record
KernelTime,
UserTime,
CreateTime: LARGE_INTEGER;
WaitTime: dword;
StartAddress: pointer;
ClientId: TClientId;
Priority,
BasePriority,
ContextSwitchCount: dword;
State: dword;
WaitReason: dword;
end;
PSYSTEM_PROCESSES = ^SYSTEM_PROCESSES;
SYSTEM_PROCESSES = packed record
NextEntryDelta,
ThreadCount: dword;
Reserved1 : array [0..5] of dword;
CreateTime,
UserTime,
KernelTime: LARGE_INTEGER;
ProcessName: TUnicodeString;
BasePriority: dword;
ProcessId,
InheritedFromProcessId,
HandleCount: dword;
Reserved2: array [0..1] of dword;
VmCounters: VM_COUNTERS;
IoCounters: IO_COUNTERS; // Windows 2000 only
Threads: array [0..0] of SYSTEM_THREADS;
end;
PObjectAttributes = ^TObjectAttributes;
TObjectAttributes = packed record
Length: DWORD;
RootDirectory: THandle;
ObjectName: PUnicodeString;
Attributes: DWORD;
SecurityDescriptor: Pointer;
SecurityQualityOfService: Pointer;
end;
PPROCESS_BASIC_INFORMATION = ^PROCESS_BASIC_INFORMATION;
PROCESS_BASIC_INFORMATION = packed record
ExitStatus: BOOL;
PebBaseAddress: pointer;
AffinityMask: PULONG;
BasePriority: dword;
UniqueProcessId: ULONG;
InheritedFromUniqueProcessId: ULONG;
end;
//LPC structures
PPORT_MESSAGE = ^_PORT_MESSAGE;
_PORT_MESSAGE = packed record
DataSize,
MessageSize,
MessageType,
VirtualRangesOffset:dword;
ClientId:TClientID;
MessageId,
SectionSize:dword;
Data:array[0..0] of dword;
end;
PSECURITY_QUALITY_OF_SERVICE = ^_SECURITY_QUALITY_OF_SERVICE;
_SECURITY_QUALITY_OF_SERVICE =packed record
Length:dword;
ImpersonationLevel:TSecurityImpersonationLevel;
ContextTrackingMode:bool;
EffectiveOnly:bool;
end;
PPORT_SECTION_WRITE = ^_PORT_SECTION_WRITE;
_PORT_SECTION_WRITE = packed record
Length:dword;
SectionHandle:THandle;
SectionOffset,
ViewSize:dword;
ViewBase:pointer;
TargetViewBase:pointer;
end;
PPORT_SECTION_READ = ^_PORT_SECTION_READ;
_PORT_SECTION_READ = packed record
Length,
ViewSize,
ViewBase:dword;
end;
PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;
IO_STATUS_BLOCK = packed record // ; sizeof = 08h
Status : DWORD;// ? ; 0000h NTSTATUS
Information : DWORD;//
end;
PFILE_FULL_EA_INFORMATION = ^PFILE_FULL_EA_INFORMATION;
FILE_FULL_EA_INFORMATION = packed record
NextEntryOffset: dword;
Flags: byte;
EaNameLength: byte;
EaValueLength: word;
EaName: array [0..0] of Char;
end;
const
LOCK_VM_IN_WSL = $01;
LOCK_VM_IN_RAM = $02;
type
FILE_INFORMATION_CLASS =
(
FileDirectoryInformation = 1,
FileFullDirectoryInformation,
FileBothDirectoryInformation,
FileBasicInformation,
FileStandardInformation,
FileInternalInformation,
FileEaInformation,
FileAccessInformation,
FileNameInformation,
FileRenameInformation,
FileLinkInformation,
FileNamesInformation,
FileDispositionInformation,
FilePositionInformation,
FileModeInformation = 16,
FileAlignmentInformation,
FileAllInformation,
FileAllocationInformation,
FileEndOfFileInformation,
FileAlternateNameInformation,
FileStreamInformation,
FilePipeInformation,
FilePipeLocalInformation,
FilePipeRemoteInformation,
FileMailslotQueryInformation,
FileMailslotSetInformation,
FileCompressionInformation,
FileObjectIdInformation,
FileCompletionInformation,
FileMoveClusterInformation,
FileQuotaInformation,
FileReparsePointInformation,
FileNetworkOpenInformation,
FileAttributeTagInformation,
FileTrackingInformation
);
////////////////////////// Ntdll.dll Functions ///////////////////////
Function ZwCreateThread(ThreadHandle: pdword;
DesiredAccess: ACCESS_MASK;
ObjectAttributes: pointer;
ProcessHandle: THandle;
ClientId: PClientID;
ThreadContext: pointer;
UserStack: pointer;
CreateSuspended: boolean):NTStatus;
stdcall;external 'ntdll.dll';
function ZwQueryInformationFile(FileHandle: dword; IoStatusBlock: PIO_STATUS_BLOCK;
FileInformation: pointer; FileInformationLength: dword;
FileInformationClass: FILE_INFORMATION_CLASS): NTStatus;
stdcall; external 'ntdll.dll';
Function ZwResumeThread(ThreadHandle: dword;
PreviousSuspendCount: pdword): NTStatus;
stdcall; external 'ntdll.dll';
Function ZwQueryInformationThread(ThreadHandle: dword;
ThreadInformationClass: dword;
ThreadInformation: pointer;
ThreadInformationLength: dword;
ReturnLength: pdword): NTStatus;
stdcall; external 'ntdll.dll';
Function ZwOpenProcess(phProcess: PDWORD; AccessMask:DWORD;
ObjectAttributes: PObjectAttributes;
ClientID: PClientID): NTStatus; stdcall; external 'ntdll.dll';
function ZwOpenThread(
ThreadHandle:PHANDLE;
DesiredAccess:ACCESS_MASK;
ObjectAttributes:PObjectAttributes;
ClientId:PClientID):NTStatus;stdcall;external 'ntdll.dll';
function ZwReadVirtualMemory(ProcessHandle: dword; BaseAddress: pointer;
Buffer: pointer; BufferLength: dword;
ReturnLength: pdword): NTStatus; stdcall; external 'ntdll.dll';
Function ZwQueryInformationProcess(
ProcessHandle:THANDLE;
ProcessInformationClass:DWORD;
ProcessInformation:pointer;
ProcessInformationLength:ULONG;
ReturnLength: PULONG):NTStatus;stdcall;
external 'ntdll.dll';
Function ZwWriteVirtualMemory(ProcessHandle: dword; BaseAddress: pointer;
Buffer: pointer; BufferLength: dword;
ReturnLength: pdword): NTStatus; stdcall; external 'ntdll.dll';
Function ZwProtectVirtualMemory(
ProcessHandle:THANDLE;
BaseAddress:pointer;
ProtectSize:PULONG;
NewProtect:dword;
OldProtect:pulong):NTStatus;stdcall;external 'ntdll.dll';
Function ZwQuerySystemInformation(ASystemInformationClass: dword;
ASystemInformation: Pointer;
ASystemInformationLength: dword;
AReturnLength:PCardinal): NTStatus;
stdcall;external 'ntdll.dll';
Function ZwTerminateProcess(ProcessHandle:dword;
ExitStatus:dword):NTStatus;stdcall;external 'ntdll.dll';
Function ZwAllocateVirtualMemory(
ProcessHandle:THANDLE;
BaseAddress:pointer;
ZeroBits:dword;
AllocationSize:pdword;
AllocationType:dword;
Protect:dword):NTStatus;stdcall;external 'ntdll.dll';
Procedure KiFastSystemCall;stdcall;external 'ntdll.dll';
Function ZwClose(Handle:dword):NTStatus;stdcall;external 'ntdll.dll';
function ZwOpenSection(SectionHandle: PHandle;
AccessMask: DWORD;
ObjectAttributes: PObjectAttributes): DWORD;
stdcall; external 'NTDLL.DLL';
procedure RtlInitUnicodeString(DestinationString: PUnicodeString;
SourceString: PWideChar);
stdcall; external 'ntdll.dll';
procedure RtlInitAnsiString(DestinationString: PAnsiString;
SourceString: PChar);
stdcall; external 'ntdll.dll';
function RtlAnsiStringToUnicodeString(
DestinationString: PUnicodeString;
SourceString: PAnsiString;
AllocateDestinationString: Boolean
): NTSTATUS; stdcall external 'ntdll.dll';
function RtlUnicodeStringToAnsiString(
DestinationString: PAnsiString;
SourceString: PUnicodeString;
AllocateDestinationString: boolean
): NTSTATUS; stdcall external 'ntdll.dll';
procedure RtlFreeAnsiString(
AnsiString: PAnsiString
); stdcall external 'ntdll.dll';
procedure RtlFreeUnicodeString(
UnicodeString: PUnicodeString
); stdcall external 'ntdll.dll';
function RtlAppendUnicodeStringToString(
Destination: PUnicodeString;
Source: PUnicodeString
): NTSTATUS; stdcall external NTDLL;
function RtlAppendUnicodeToString(
Destination: PUnicodeString;
Source: PWideChar
): NTSTATUS; stdcall external NTDLL;
Function ZwMapViewOfSection(SectionHandle:dword;
ProcessHandle:dword;
BaseAddress:PPointer;
ZeroBits,
CommitSize:dword;
SectionOffset:PInt64;
ViewSize:pdword;
InheritDisposition:dword;
AllocationType,Protect:dword):NTStatus;
stdcall; external 'ntdll.dll';
Function ZwUnmapViewOfSection(ProcessHandle:dword;
BaseAddress:pointer):NTStatus;
stdcall; external 'ntdll.dll';
Function ZwCreateNamedPipeFile(
FileHandle:PHandle;
DesiredAccess:ACCESS_MASK;
ObjectAttributes:POBJECTATTRIBUTES;
IoStatusBlock:pointer;
ShareAccess,
CreateDisposition,
CreateOptions:dword;
TypeMessage,
ReadmodeMessage,
Nonblocking:boolean;
MaxInstances,
InBufferSize,
OutBufferSize:dword;
DefaultTimeout: PDword):NTStatus;
stdcall; external 'ntdll.dll';
//LPC functions
Function ZwCreatePort(PortHandle:PDWORD;
ObjectAttributes:PObjectAttributes;
MaxDataSize,MaxMessageSize,
Reserved:dword): NTStatus;stdcall;external 'ntdll.dll';
Function ZwQueryDirectoryFile(FileHandle: dword;
Event: dword;
ApcRoutine: pointer;
ApcContext: pointer;
IoStatusBlock: pointer;
FileInformation: pointer;
FileInformationLength: dword;
FileInformationClass: dword;
ReturnSingleEntry: bool;
FileName: PUnicodeString;
RestartScan: bool): NTStatus;
stdcall; external 'ntdll.dll';
Function ZwConnectPort(PortHandle:PDWORD;
PortName:PUnicodeString;
SecurityQos:PSECURITY_QUALITY_OF_SERVICE;
WriteSection:PPORT_SECTION_WRITE;
ReadSection:PPORT_SECTION_READ;
MaxMessageSize:PULONG;
ConnectData :pointer;
ConnectDataLength :PULONG):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwListenPort(PortHandle:THandle;
var Msg:PPORT_MESSAGE):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwRequestWaitReplyPort(PortHandle:THandle;
RequestMessage:PPORT_MESSAGE;
var ReplyMessage:PPORT_MESSAGE):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwAcceptConnectPort(PortHandle:PHANDLE;
PortIdentifier:dword;
PortMessage:PPORT_MESSAGE;
Accept:bool;
WriteSection:PPORT_SECTION_WRITE;
ReadSection:PPORT_SECTION_READ):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwCompleteConnectPort(PortHandle:THandle):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwRequestPort(PortHandle:THandle;RequestMessage:PPORT_MESSAGE):NTStatus;
stdcall;external 'ntdll.dll';
Function ZwReplyPort(PortHandle:THandle;RequestMessage:PPORT_MESSAGE):NTStatus;
stdcall;external 'ntdll.dll';
function ZwSetSystemInformation(SystemInformationClass: dword;
SystemInformation: pointer;
SystemInformationLength: dword): NTStatus;
stdcall;external 'ntdll.dll';
function ZwLoadDriver(DriverServiceName: PUnicodeString): NTStatus;
stdcall;external 'ntdll.dll';
function ZwUnloadDriver(DriverServiceName: PUnicodeString): NTStatus;
stdcall;external 'ntdll.dll';
function DbgPrint(
const Format : PAnsiChar
) : NTStatus; cdecl; external NTDLL;
//**** Registry ******
function ZwCreateKey(
KeyHandle : pdword;
DesiredAccess : ACCESS_MASK;
ObjectAttributes : PObjectAttributes;
TitleIndex:ULONG;
ObjectClass : PUnicodeString;
CreateOptions:ULONG;
Disposition:PULONG) : NTSTATUS; stdcall; external 'ntdll.dll';
function ZwDeleteKey(KeyHandle: THandle): NTSTATUS;
stdcall; external 'ntdll.dll'
function LdrLoadDll(szcwPath: PWideChar;
pdwLdrErr: dword;
pUniModuleName: PUnicodeString;
pResultInstance: PDWORD): NTSTATUS;
stdcall; external 'ntdll.dll';
function LdrGetProcedureAddress(hModule: dword;
dOrdinal: DWORD;
psName: PUnicodeString;
ppProcedure: ppointer): NTStatus;
stdcall; external 'ntdll.dll';
function ZwLockVirtualMemory(ProcessHandle: dword;
BaseAddress: ppointer;
LockSize: pdword;
LockType: dword): NTStatus;
stdcall; external 'ntdll.dll';
Function DbgUiDebugActiveProcess(pHandle: dword): NTStatus;stdcall;external 'ntdll.dll';
Function DbgUiConnectToDbg(): NTStatus;stdcall;external 'ntdll.dll';
function ZwQueryEaFile(FileHandle: dword;
IoStatusBlock: PIO_STATUS_BLOCK;
Buffer: pointer;
BufferLength: dword;
ReturnSingleEntry: bool;
EaList: pointer;// OPTIONAL,
EaListLength: dword;
EaIndex: pdword;// OPTIONAL,
RestartScan: bool):NTStatus;
stdcall;external 'ntdll.dll';
type
PSYSTEM_MODULE_INFORMATION = ^SYSTEM_MODULE_INFORMATION;
SYSTEM_MODULE_INFORMATION = packed record // Information Class 11
Reserved: array[0..1] of ULONG;
Base: PVOID;
Size: ULONG;
Flags: ULONG;
Index: USHORT;
Unknown: USHORT;
LoadCount: USHORT;
ModuleNameOffset: USHORT;
ImageName: array [0..255] of Char;
end;
PSYSTEM_MODULE_INFORMATION_EX = ^SYSTEM_MODULE_INFORMATION_EX;
SYSTEM_MODULE_INFORMATION_EX = packed record
ModulesCount: dword;
Modules: array[0..0] of SYSTEM_MODULE_INFORMATION;
end;
const
THREAD_BASIC_INFO = $0;
THREAD_QUERY_INFORMATION = $40;
ProcessBasicInformation = 0;
OBJ_CASE_INSENSITIVE = $00000040;
OBJ_KERNEL_HANDLE = $00000200;
//LPC constants
LPC_NEW_MESSAGE = 1;
LPC_REQUEST = 2;
LPC_REPLY = 3;
LPC_DATAGRAM = 4;
LPC_LOST_REPLY = 5;
LPC_PORT_CLOSED = 6;
LPC_CLIENT_DIED = 7;
LPC_EXCEPTION = 8;
LPC_DEBUG_EVENT = 9;
LPC_ERROR_EVENT = 10;
LPC_CONNECTION_REQUEST = 11;
procedure InitializeObjectAttributes(
InitializedAttributes : PObjectAttributes;
pObjectName : PUnicodeString;
const uAttributes : ULONG;
const hRootDirectory : THandle;
pSecurityDescriptor : PSECURITY_DESCRIPTOR);
Function GetInfoTable(ATableType:dword):Pointer;
implementation
{ èíèöèàëèçàöèÿ ñòðóêòóðû TObjectAttributes }
procedure InitializeObjectAttributes(
InitializedAttributes : PObjectAttributes;
pObjectName : PUnicodeString;
const uAttributes : ULONG;
const hRootDirectory : THandle;
pSecurityDescriptor : PSECURITY_DESCRIPTOR);
begin
with InitializedAttributes^ do
begin
Length := SizeOf(TObjectAttributes);
ObjectName := pObjectName;
Attributes := uAttributes;
RootDirectory := hRootDirectory;
SecurityDescriptor := pSecurityDescriptor;
SecurityQualityOfService := nil;
end;
end;
{ Ïîëó÷åíèå áóôåðà ñ ñèñòåìíîé èíôîðìàöèåé }
Function GetInfoTable(ATableType:dword):Pointer;
var
mSize: dword;
mPtr: pointer;
St: NTStatus;
begin
Result := nil;
mSize := $4000; //íà÷àëüíûé ðàçìåð áóôôåðà
repeat
mPtr := VirtualAlloc(nil, mSize, MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE);
if mPtr = nil then Exit;
St := ZwQuerySystemInformation(ATableType, mPtr, mSize, nil);
if St = STATUS_INFO_LENGTH_MISMATCH then
begin //íàäî áîëüøå ïàìÿòè
VirtualFree(mPtr, 0, MEM_RELEASE);
mSize := mSize * 2;
end;
until St <> STATUS_INFO_LENGTH_MISMATCH;
if St = STATUS_SUCCESS
then Result := mPtr
else VirtualFree(mPtr, 0, MEM_RELEASE);
end;
end.