This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathml.cpp
14544 lines (11784 loc) · 389 KB
/
ml.cpp
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ml.h"
#if ML_KERNEL_MODE
// #include "Source\MyLibraryKernel.cpp"
ML_NAMESPACE
LONG_PTR MlInitialize()
{
return 0;
}
LONG_PTR MlUnInitialize()
{
return 0;
}
MY_NAMESPACE_END
#else
_ML_C_HEAD_
#if ML_KERNEL_MODE
PVOID AllocateMemory(ULONG_PTR Size, POOL_TYPE PoolType /* = PagedPool */)
{
return MemoryAllocator::AllocateMemory(Size, PoolType);
}
PVOID AllocateMemoryP(ULONG_PTR Size, POOL_TYPE PoolType)
{
return AllocateMemory(Size, PoolType);
}
BOOL FreeMemory(PVOID Memory, ULONG Flags)
{
return Memory != NULL ? MemoryAllocator::FreeMemory(Memory) : FALSE;
}
BOOL FreeMemoryP(PVOID Memory, ULONG Flags)
{
return FreeMemory(Memory, Flags);
}
#else // user mode
PVOID AllocateMemoryP(ULONG_PTR Size, ULONG Flags)
{
return MemoryAllocator::AllocateMemory(Size, Flags);
}
PVOID ReAllocateMemoryP(PVOID Memory, ULONG_PTR Size, ULONG Flags)
{
return MemoryAllocator::ReAllocateMemory(Memory, Size, Flags);
}
PVOID AllocateMemory(ULONG_PTR Size, ULONG Flags)
{
#if USE_CRT_VER
return malloc(Size);
#elif USE_NT_VER
return RtlAllocateHeap(CurrentPeb()->ProcessHeap, Flags, Size);
#else
return HeapAlloc(GetProcessHeap(), Flags, Size);
#endif
}
PVOID ReAllocateMemory(PVOID Memory, ULONG_PTR Size, ULONG Flags)
{
PVOID Block;
if (Memory == NULL)
return AllocateMemory(Size, Flags);
#if USE_CRT_VER
Block = realloc(Memory, Size);
#elif USE_NT_VER
Block = RtlReAllocateHeap(CurrentPeb()->ProcessHeap, Flags, Memory, Size);
#else
Block = HeapReAlloc(GetProcessHeap(), Flags, Memory, Size);
#endif
if (Block == nullptr)
{
FreeMemory(Memory);
return nullptr;
}
return Block;
}
BOOL FreeMemory(PVOID Memory, ULONG Flags)
{
#if USE_CRT_VER
free(Memory);
return TRUE;
#elif USE_NT_VER
return Memory != NULL ? RtlFreeHeap(CurrentPeb()->ProcessHeap, Flags, Memory) : FALSE;
#else
return Memory != NULL ? HeapFree(GetProcessHeap(), Flags, Memory) : FALSE;
#endif
}
BOOL FreeMemoryP(PVOID Memory, ULONG Flags)
{
return MemoryAllocator::FreeMemory(Memory, Flags);
}
PVOID AllocateVirtualMemory(ULONG_PTR Size, ULONG Protect, HANDLE ProcessHandle)
{
#if USE_NT_VER
PVOID BaseAddress = NULL;
NTSTATUS Status;
Status = Nt_AllocateMemory(ProcessHandle, &BaseAddress, Size, Protect);
return NT_SUCCESS(Status) ? BaseAddress : NULL;
#else
return VirtualAllocEx(ProcessHandle, NULL, Size, MEM_RESERVE | MEM_COMMIT, Protect);
#endif
}
BOOL FreeVirtualMemory(PVOID Memory, HANDLE ProcessHandle)
{
#if USE_NT_VER
return NT_SUCCESS(Nt_FreeMemory(ProcessHandle, Memory));
#else
return VirtualFreeEx(ProcessHandle, Memory, 0, MEM_RELEASE);
#endif
}
#endif // ML_KERNEL_MODE
_ML_C_TAIL_
#pragma warning(disable:4245)
#pragma warning(disable:4750)
_ML_C_HEAD_
/************************************************************************
strings
************************************************************************/
ULONG
InternalCopyUnicodeString(
PUNICODE_STRING Unicode,
PWCHAR Buffer,
ULONG_PTR BufferCount,
BOOL IsDirectory = FALSE
)
{
ULONG_PTR Length;
Length = MY_MIN(Unicode->Length, BufferCount * sizeof(WCHAR));
CopyMemory(Buffer, Unicode->Buffer, Length);
Length /= sizeof(WCHAR);
if (IsDirectory && Length < BufferCount && Buffer[Length - 1] != '\\')
Buffer[Length++] = '\\';
if (Length < BufferCount)
Buffer[Length] = 0;
return Length;
}
NTSTATUS
FASTCALL
Nt_AnsiToUnicode(
PWCHAR UnicodeBuffer,
ULONG_PTR BufferCount,
LPCSTR AnsiString,
LONG_PTR AnsiLength,
PULONG_PTR BytesInUnicode
)
{
return ml::Native::Nls::AnsiToUnicode(UnicodeBuffer, BufferCount, AnsiString, AnsiLength, BytesInUnicode);
}
NTSTATUS
Nt_UnicodeToAnsi(
PCHAR AnsiBuffer,
ULONG_PTR BufferCount,
LPCWSTR UnicodeString,
LONG_PTR UnicodeLength,
PULONG_PTR BytesInAnsi
)
{
return ml::Native::Nls::UnicodeToAnsi(AnsiBuffer, BufferCount, UnicodeString, UnicodeLength, BytesInAnsi);
}
NTSTATUS
Nt_AnsiToUnicodeString(
PUNICODE_STRING Unicode,
PCHAR AnsiString,
LONG_PTR AnsiLength,
BOOL AllocateDestinationString
)
{
return ml::Native::Nls::AnsiToUnicodeString(Unicode, AnsiString, AnsiLength, AllocateDestinationString);
}
NTSTATUS
Nt_UnicodeToAnsiString(
PANSI_STRING Ansi,
LPCWSTR UnicodeString,
LONG_PTR UnicodeLength,
BOOL AllocateDestinationString
)
{
return ml::Native::Nls::UnicodeToAnsiString(Ansi, UnicodeString, UnicodeLength, AllocateDestinationString);
}
/************************************************************************
process api
************************************************************************/
BOOL
Nt_IsWow64Process(
HANDLE Process /* = NtCurrentProcess() */
)
{
PVOID Wow64Information;
NTSTATUS Status;
Status = ZwQueryInformationProcess(
Process,
ProcessWow64Information,
&Wow64Information,
sizeof(Wow64Information),
NULL
);
return NT_SUCCESS(Status) && Wow64Information != NULL;
}
ULONG Nt_GetErrorMode()
{
ULONG HardErrorMode;
if (!NT_SUCCESS(NtQueryInformationProcess(NtCurrentProcess(), ProcessDefaultHardErrorMode, &HardErrorMode, sizeof(HardErrorMode), NULL)))
HardErrorMode = 0;
return HardErrorMode;
}
ULONG Nt_SetErrorMode(ULONG Mode)
{
ULONG HardErrorMode, PreviousHardErrorMode;
NTSTATUS Status;
PreviousHardErrorMode = Nt_GetErrorMode();
HardErrorMode = PreviousHardErrorMode | Mode;
NtSetInformationProcess(NtCurrentProcess(), ProcessDefaultHardErrorMode, &HardErrorMode, sizeof(HardErrorMode));
return PreviousHardErrorMode;
}
/************************************************************************
common file io
************************************************************************/
NTSTATUS Nt_DeleteFile(PCWSTR FileName)
{
return Io::DeleteFile(FileName);
}
/************************************************************************
common path
************************************************************************/
ULONG_PTR
Nt_QueryDosDevice(
PCWSTR DeviceName,
PWSTR TargetPath,
ULONG_PTR PathSize
)
{
NTSTATUS Status;
UNICODE_STRING Target;
Status = Io::QueryDosDevice(DeviceName, &Target);
if (NT_FAILED(Status))
return 0;
PathSize = InternalCopyUnicodeString(&Target, TargetPath, PathSize);
RtlFreeUnicodeString(&Target);
return PathSize;
}
/************************************************************************
common query
************************************************************************/
POBJECT_TYPES_INFORMATION QuerySystemObjectTypes()
{
ULONG Size, Length;
NTSTATUS Status;
POBJECT_TYPES_INFORMATION ObjectTypes, StackPointer;
Size = sizeof(*ObjectTypes);
ObjectTypes = (POBJECT_TYPES_INFORMATION)AllocStack(Size);
StackPointer = ObjectTypes;
LOOP_FOREVER
{
#if ML_KERNEL_MODE
Status = ZwQueryObject(NULL, ObjectTypesInformation, ObjectTypes, Size, &Length);
if (Status != STATUS_INFO_LENGTH_MISMATCH)
break;
if (ObjectTypes == StackPointer)
ObjectTypes = NULL;
FreeMemoryP(ObjectTypes);
ObjectTypes = (POBJECT_TYPES_INFORMATION)AllocateMemoryP(Length);
#else
Status = NtQueryObject(NULL, ObjectTypesInformation, ObjectTypes, Size, &Length);
if (Status != STATUS_INFO_LENGTH_MISMATCH)
break;
if (ObjectTypes == StackPointer)
ObjectTypes = NULL;
ObjectTypes = (POBJECT_TYPES_INFORMATION)ReAllocateMemoryP(ObjectTypes, Length);
#endif
if (ObjectTypes == NULL)
return NULL;
Size = Length;
}
if (!NT_SUCCESS(Status))
{
FreeMemoryP(ObjectTypes);
ObjectTypes = NULL;
}
return ObjectTypes;
}
PSYSTEM_HANDLE_INFORMATION_EX QuerySystemHandles()
{
ULONG Size, Length;
NTSTATUS Status;
PSYSTEM_HANDLE_INFORMATION_EX SystemHandles;
Size = sizeof(*SystemHandles) * 2;
SystemHandles = (PSYSTEM_HANDLE_INFORMATION_EX)AllocateMemoryP(Size);
if (SystemHandles == nullptr)
return nullptr;
LOOP_FOREVER
{
#if ML_KERNEL_MODE
Status = ZwQuerySystemInformation(SystemExtendedHandleInformation, SystemHandles, Size, &Length);
if (Status != STATUS_INFO_LENGTH_MISMATCH)
break;
FreeMemoryP(SystemHandles);
SystemHandles = (PSYSTEM_HANDLE_INFORMATION_EX)AllocateMemoryP(Length);
#else
Status = NtQuerySystemInformation(SystemExtendedHandleInformation, SystemHandles, Size, &Length);
if (Status != STATUS_INFO_LENGTH_MISMATCH)
break;
SystemHandles = (PSYSTEM_HANDLE_INFORMATION_EX)ReAllocateMemoryP(SystemHandles, Length);
#endif
if (SystemHandles == nullptr)
return nullptr;
Size = Length;
}
if (!NT_SUCCESS(Status))
{
FreeMemoryP(SystemHandles);
SystemHandles = nullptr;
}
return SystemHandles;
}
BOOL ReleaseSystemInformation(PVOID Processes)
{
return FreeMemoryP(Processes);
}
HANDLE QueryCsrssProcessId(ULONG_PTR Session)
{
return NULL;
}
/************************************************************************
common process
************************************************************************/
/************************************************************************
common token
************************************************************************/
ULONG_PTR
Nt_GetSessionId(
HANDLE Process
)
{
return ml::Native::Ps::GetSessionId(Process);
}
#if ML_KERNEL_MODE
/************************************************************************
kernel mode
************************************************************************/
PLDR_MODULE LookupPsLoadedModuleList(PLDR_MODULE LdrModule, PVOID CallDriverEntry)
{
PLDR_MODULE PsLoadedModuleList;
UNICODE_STRING NtName;
RTL_CONST_STRING(NtName, L"ntoskrnl.exe");
PsLoadedModuleList = LdrModule;
do
{
PsLoadedModuleList = PsLoadedModuleList->NextLoadOrder();
/*
if (PsLoadedModuleList->SizeOfImage != 0 && (PsLoadedModuleList->DllBase != NULL || PsLoadedModuleList->EntryPoint != NULL))
{
continue;
}
*/
if (!IN_RANGEEX(PsLoadedModuleList->DllBase, CallDriverEntry, PtrAdd(PsLoadedModuleList->DllBase, PsLoadedModuleList->SizeOfImage)))
continue;
if (!RtlEqualUnicodeString(&PsLoadedModuleList->BaseDllName, &NtName, TRUE))
continue;
return PsLoadedModuleList->PrevLoadOrder();
} while (LdrModule != PsLoadedModuleList);
return NULL;
}
NTSTATUS QuerySystemModuleByHandle(PVOID ImageBase, PRTL_PROCESS_MODULE_INFORMATION Module)
{
NTSTATUS Status;
ULONG ReturnedSize;
ULONG_PTR Size, NumberOfModules;
PRTL_PROCESS_MODULE_INFORMATION SystemModule;
PRTL_PROCESS_MODULES ModuleInfo;
Size = 0;
ModuleInfo = NULL;
LOOP_FOREVER
{
Status = ZwQuerySystemInformation(SystemModuleInformation, ModuleInfo, Size, &ReturnedSize);
if (Status != STATUS_INFO_LENGTH_MISMATCH)
break;
FreeMemory(ModuleInfo);
ModuleInfo = (PRTL_PROCESS_MODULES)AllocateMemory(ReturnedSize - Size);
Size = ReturnedSize;
}
SCOPE_EXIT
{
FreeMemory(ModuleInfo);
}
SCOPE_EXIT_END;
if (!NT_SUCCESS(Status))
return Status;
if (ImageBase == NULL)
{
*Module = ModuleInfo->Modules[0];
return STATUS_SUCCESS;
}
SystemModule = ModuleInfo->Modules;
for (NumberOfModules = ModuleInfo->NumberOfModules; NumberOfModules != 0; ++SystemModule, --NumberOfModules)
{
if (IN_RANGE((ULONG_PTR)SystemModule->ImageBase, (ULONG_PTR)ImageBase, (ULONG_PTR)SystemModule->ImageBase + SystemModule->ImageSize))
{
*Module = *SystemModule;
return STATUS_SUCCESS;
}
}
return STATUS_DLL_NOT_FOUND;
}
NTSTATUS QueryModuleNameByHandle(PVOID ImageBase, PUNICODE_STRING ModuleName)
{
NTSTATUS Status;
ANSI_STRING ModuleAnsiName;
RTL_PROCESS_MODULE_INFORMATION SystemModule;
Status = QuerySystemModuleByHandle(ImageBase, &SystemModule);
if (!NT_SUCCESS(Status))
return Status;
RtlInitAnsiString(&ModuleAnsiName, (PSTR)SystemModule.FullPathName);
return RtlAnsiStringToUnicodeString(ModuleName, &ModuleAnsiName, TRUE);
}
NTSTATUS
KiQueueUserApc(
PETHREAD Thread,
PKNORMAL_ROUTINE ApcRoutine,
PVOID ApcRoutineContext,
PVOID Argument1,
PVOID Argument2
)
{
PKAPC Apc;
NTSTATUS Status;
Apc = (PKAPC)AllocateMemoryP(sizeof(*Apc));
if (Apc == NULL)
return STATUS_NO_MEMORY;
KeInitializeApc(
Apc,
Thread,
OriginalApcEnvironment,
[] (PKAPC Apc, PKNORMAL_ROUTINE *NormalRoutine, PVOID *NormalContext, PVOID *SystemArgument1, PVOID *SystemArgument2)
{
FreeMemoryP(Apc);
},
NULL,
ApcRoutine,
UserMode,
ApcRoutineContext
);
Status = KeInsertQueueApc(Apc, Argument1, Argument2, 0);
return Status ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
}
#else // r3
/************************************************************************
user mode
************************************************************************/
/************************************************************************
user token
************************************************************************/
NTSTATUS
Nt_GetTokenInfo(
HANDLE TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass,
PVOID Information,
PULONG_PTR Size
)
{
NTSTATUS Status;
PVOID TokenInformation;
ULONG Length;
TokenInformation = NULL;
Length = 0;
LOOP_FOREVER
{
Status = NtQueryInformationToken(TokenHandle, TokenInformationClass, TokenInformation, Length, &Length);
if (Status != STATUS_BUFFER_TOO_SMALL)
break;
TokenInformation = ReAllocateMemory(TokenInformation, Length);
}
if (NT_FAILED(Status))
{
FreeMemory(TokenInformation);
TokenInformation = NULL;
}
else
{
*(PVOID *)Information = TokenInformation;
if (Size != NULL)
*Size= Length;
}
return Status;
}
VOID ReleaseTokenInfo(PVOID TokenInfo)
{
FreeMemory(TokenInfo);
}
NTSTATUS
Nt_SetProcessThreadToken(
HANDLE Process,
HANDLE Thread,
HANDLE Token
)
{
BOOLEAN Enabled;
NTSTATUS Status;
OBJECT_ATTRIBUTES ObjectAttributes;
PROCESS_ACCESS_TOKEN ProcessToken;
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
Status = NtDuplicateToken(Token, 0, &ObjectAttributes, FALSE, TokenPrimary, &ProcessToken.Token);
FAIL_RETURN(Status);
if (NT_FAILED(RtlAdjustPrivilege(SE_ASSIGNPRIMARYTOKEN_PRIVILEGE, TRUE, TRUE, &Enabled)))
Enabled = FALSE;
ProcessToken.Thread = Thread;
Status = NtSetInformationProcess(Process, ProcessAccessToken, &ProcessToken, sizeof(ProcessToken));
if (Enabled)
RtlAdjustPrivilege(SE_ASSIGNPRIMARYTOKEN_PRIVILEGE, FALSE, TRUE, &Enabled);
NtClose(ProcessToken.Token);
return Status;
}
NTSTATUS
Nt_AdjustPrivilege(
ULONG_PTR Privilege,
BOOL Enable,
BOOL CurrentThread
)
{
BOOLEAN Enabled;
return RtlAdjustPrivilege(Privilege, (BOOLEAN)Enable, (BOOLEAN)CurrentThread, &Enabled);
}
/************************************************************************
debug api
************************************************************************/
NTSTATUS Nt_DebugActiveProcess(ULONG_PTR ProcessId)
{
HANDLE ProcessHandle;
NTSTATUS Status;
Status = DbgUiConnectToDbg();
if (!NT_SUCCESS(Status))
return Status;
ProcessHandle = ml::Native::Ps::ProcessIdToHandle(ProcessId);
if (ProcessHandle == NULL)
return STATUS_UNSUCCESSFUL;
Status = DbgUiDebugActiveProcess(ProcessHandle);
NtClose(ProcessHandle);
return Status;
}
NTSTATUS Nt_DebugActiveProcessStop(ULONG_PTR ProcessId)
{
HANDLE Process;
NTSTATUS Status;
Process = ml::Native::Ps::ProcessIdToHandle(ProcessId);
if (Process == NULL)
return STATUS_UNSUCCESSFUL;
Status = DbgUiStopDebugging(Process);
NtClose(Process);
return Status;
}
NTSTATUS Nt_WaitForDebugEvent(PDBGUI_WAIT_STATE_CHANGE WaitState, ULONG Timeout, HANDLE DebugObject)
{
NTSTATUS Status;
LARGE_INTEGER TimeOut;
FormatTimeOut(&TimeOut, Timeout);
LOOP_FOREVER
{
Status = NtWaitForDebugEvent(DebugObject, TRUE, &TimeOut, WaitState);
switch (Status)
{
case STATUS_USER_APC:
case STATUS_ALERTED:
continue;
}
if (!NT_SUCCESS(Status) || Status == STATUS_TIMEOUT)
return Status;
break;
}
return Status;
}
/************************************************************************
list entry
************************************************************************/
VOID
InitializeListHead(
PLIST_ENTRY ListHead
)
{
ListHead->Flink = ListHead->Blink = ListHead;
}
BOOL
IsListEmpty(
LIST_ENTRY *ListHead
)
{
return (BOOL)(ListHead->Flink == ListHead);
}
BOOL
RemoveEntryList(
PLIST_ENTRY Entry
)
{
PLIST_ENTRY Blink;
PLIST_ENTRY Flink;
Flink = Entry->Flink;
Blink = Entry->Blink;
Blink->Flink = Flink;
Flink->Blink = Blink;
return (BOOL)(Flink == Blink);
}
PLIST_ENTRY
RemoveHeadList(
PLIST_ENTRY ListHead
)
{
PLIST_ENTRY Flink;
PLIST_ENTRY Entry;
Entry = ListHead->Flink;
Flink = Entry->Flink;
ListHead->Flink = Flink;
Flink->Blink = ListHead;
return Entry;
}
PLIST_ENTRY
RemoveTailList(
PLIST_ENTRY ListHead
)
{
PLIST_ENTRY Blink;
PLIST_ENTRY Entry;
Entry = ListHead->Blink;
Blink = Entry->Blink;
ListHead->Blink = Blink;
Blink->Flink = ListHead;
return Entry;
}
VOID
InsertTailList(
PLIST_ENTRY ListHead,
PLIST_ENTRY Entry
)
{
PLIST_ENTRY Blink;
Blink = ListHead->Blink;
Entry->Flink = ListHead;
Entry->Blink = Blink;
Blink->Flink = Entry;
ListHead->Blink = Entry;
}
VOID
InsertHeadList(
PLIST_ENTRY ListHead,
PLIST_ENTRY Entry
)
{
PLIST_ENTRY Flink;
Flink = ListHead->Flink;
Entry->Flink = Flink;
Entry->Blink = ListHead;
Flink->Blink = Entry;
ListHead->Flink = Entry;
}
VOID
AppendTailList(
PLIST_ENTRY ListHead,
PLIST_ENTRY ListToAppend
)
{
PLIST_ENTRY ListEnd = ListHead->Blink;
ListHead->Blink->Flink = ListToAppend;
ListHead->Blink = ListToAppend->Blink;
ListToAppend->Blink->Flink = ListHead;
ListToAppend->Blink = ListEnd;
}
PSINGLE_LIST_ENTRY
PopEntryList(
PSINGLE_LIST_ENTRY ListHead
)
{
PSINGLE_LIST_ENTRY FirstEntry;
FirstEntry = ListHead->Next;
if (FirstEntry != NULL) {
ListHead->Next = FirstEntry->Next;
}
return FirstEntry;
}
VOID
PushEntryList(
PSINGLE_LIST_ENTRY ListHead,
PSINGLE_LIST_ENTRY Entry
)
{
Entry->Next = ListHead->Next;
ListHead->Next = Entry;
}
VOID Nt_ListLock(PCRITICAL_SECTION Lock)
{
RtlEnterCriticalSection(Lock);
}
VOID Nt_ListUnlock(PCRITICAL_SECTION Lock)
{
RtlLeaveCriticalSection(Lock);
}
PLIST_ENTRY
ExInterlockedInsertHeadList(
PLIST_ENTRY ListHead,
PLIST_ENTRY Entry,
PCRITICAL_SECTION Lock
)
{
Nt_ListLock(Lock);
InsertHeadList(ListHead, Entry);
Nt_ListUnlock(Lock);
return ListHead;
}
PLIST_ENTRY
ExInterlockedInsertTailList(
PLIST_ENTRY ListHead,
PLIST_ENTRY Entry,
PCRITICAL_SECTION Lock
)
{
PLIST_ENTRY TailList;
Nt_ListLock(Lock);
TailList = ListHead->Blink;
InsertTailList(ListHead, Entry);
Nt_ListUnlock(Lock);
return TailList;
}
PLIST_ENTRY
ExInterlockedRemoveHeadList(
PLIST_ENTRY ListHead,
PCRITICAL_SECTION Lock
)
{
Nt_ListLock(Lock);
ListHead = RemoveHeadList(ListHead);
Nt_ListUnlock(Lock);
return ListHead;
}
PSINGLE_LIST_ENTRY
ExInterlockedPopEntryList(
PSINGLE_LIST_ENTRY ListHead,
PCRITICAL_SECTION Lock
)
{
Nt_ListLock(Lock);
ListHead = PopEntryList(ListHead);
Nt_ListUnlock(Lock);
return ListHead;
}
PSINGLE_LIST_ENTRY
ExInterlockedPushEntryList(
PSINGLE_LIST_ENTRY ListHead,
PSINGLE_LIST_ENTRY Entry,
PCRITICAL_SECTION Lock
)
{
Nt_ListLock(Lock);
PushEntryList(ListHead, Entry);
Nt_ListUnlock(Lock);
return ListHead;
}
VOID
Nt_InitializeSListHead(
PSLIST_HEADER SListHead
)
{
RtlZeroMemory(SListHead, sizeof(SLIST_HEADER));
}
/************************************************************************
registry api
************************************************************************/
#if !defined(HKEY_CURRENT_USER_LOCAL_SETTINGS)
#define HKEY_CURRENT_USER_LOCAL_SETTINGS (( HKEY ) (ULONG_PTR)((LONG)0x80000007) )
#endif
NTSTATUS Nt_OpenPredefinedKeyHandle(PHANDLE KeyHandle, HKEY PredefinedKey, ACCESS_MASK DesiredAccess)
{
return Reg::OpenPredefinedKeyHandle(KeyHandle, PredefinedKey, DesiredAccess);
}
NTSTATUS
Nt_RegGetValue(
HANDLE hKey,
PCWSTR SubKey,
PCWSTR ValueName,
KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
PVOID KeyValueInformation,
ULONG Length,
PULONG ResultLength,
ULONG Flags
)
{
return Reg::GetKeyValue(hKey, SubKey, ValueName, KeyValueInformationClass, KeyValueInformation, Length, ResultLength, Flags);
}
NTSTATUS
Nt_RegSetValue(
HANDLE hKey,
PCWSTR SubKey,
PCWSTR ValueName,
ULONG ValueType,
LPCVOID ValueData,
ULONG ValueDataLength
)
{
return Reg::SetKeyValue(hKey, SubKey, ValueName, ValueType, ValueData, ValueDataLength);
}
NTSTATUS
Nt_RegDeleteValue(
HANDLE hKey,
PCWSTR SubKey,
PCWSTR ValueName
)
{
return Reg::DeleteKeyValue(hKey, SubKey, ValueName);
}
/************************************************************************
memory
************************************************************************/
NTSTATUS
Nt_ProtectMemory(
HANDLE ProcessHandle,
PVOID BaseAddress,
ULONG_PTR Size,
ULONG NewProtect,
PULONG OldProtect