-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWin32.cs
More file actions
1342 lines (1137 loc) · 51 KB
/
Copy pathWin32.cs
File metadata and controls
1342 lines (1137 loc) · 51 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
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
using System.Runtime.InteropServices;
using System.Text;
namespace SharpKit;
public static class Win32
{
// -------------------------------------------------------------------------
// Process access rights
// ---------------------------------------------------------------------
public const uint PROCESS_ALL_ACCESS = 0x001FFFFF;
public const uint PROCESS_VM_READ = 0x0010;
public const uint PROCESS_VM_WRITE = 0x0020;
public const uint PROCESS_VM_OPERATION = 0x0008;
public const uint PROCESS_CREATE_THREAD = 0x0002;
public const uint PROCESS_QUERY_INFORMATION = 0x0400;
public const uint PROCESS_QUERY_LIMITED = 0x1000;
public const uint PROCESS_DUP_HANDLE = 0x0040;
public const uint PROCESS_SET_INFORMATION = 0x0200;
public const uint PROCESS_SUSPEND_RESUME = 0x0800;
public const uint PROCESS_TERMINATE = 0x0001;
// ---------------------------------------------------------------
// Thread access rights
// -------------------------------------------------------------------------
public const uint THREAD_ALL_ACCESS = 0x001FFFFF;
public const uint THREAD_GET_CONTEXT = 0x0008;
public const uint THREAD_SET_CONTEXT = 0x0010;
public const uint THREAD_SUSPEND_RESUME = 0x0002;
public const uint THREAD_QUERY_INFORMATION = 0x0040;
public const uint THREAD_SET_INFORMATION = 0x0020;
public const uint THREAD_SET_THREAD_TOKEN = 0x0080;
public const uint THREAD_TERMINATE = 0x0001;
// ----------------------------------------------------------------
// Token access rights
// -------------------------------------------------------------------------
public const uint TOKEN_ALL_ACCESS = 0x000F01FF;
public const uint TOKEN_QUERY = 0x0008;
public const uint TOKEN_QUERY_SOURCE = 0x0010;
public const uint TOKEN_IMPERSONATE = 0x0004;
public const uint TOKEN_DUPLICATE = 0x0002;
public const uint TOKEN_ASSIGN_PRIMARY = 0x0001;
public const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
public const uint TOKEN_ADJUST_GROUPS = 0x0040;
public const uint TOKEN_ADJUST_DEFAULT = 0x0080;
public const uint TOKEN_ADJUST_SESSIONID = 0x0100;
// ------------------------------------------------------------------
// Memory flags
// -------------------------------------------------------------------------
public const uint MEM_COMMIT = 0x1000;
public const uint MEM_RESERVE = 0x2000;
public const uint MEM_DECOMMIT = 0x4000;
public const uint MEM_RELEASE = 0x8000;
public const uint MEM_RESET = 0x00080000;
public const uint MEM_LARGE_PAGES = 0x20000000;
public const uint MEM_MAPPED = 0x00040000;
public const uint MEM_PRIVATE = 0x00020000;
public const uint MEM_IMAGE = 0x01000000;
// ----------------------------------------------------------------------------------------
// Page protection
// -------------------------------------------------------------------------
public const uint PAGE_NOACCESS = 0x01;
public const uint PAGE_READONLY = 0x02;
public const uint PAGE_READWRITE = 0x04;
public const uint PAGE_WRITECOPY = 0x08;
public const uint PAGE_EXECUTE = 0x10;
public const uint PAGE_EXECUTE_READ = 0x20;
public const uint PAGE_EXECUTE_READWRITE = 0x40;
public const uint PAGE_EXECUTE_WRITECOPY = 0x80;
public const uint PAGE_GUARD = 0x100;
public const uint PAGE_NOCACHE = 0x200;
public const uint PAGE_WRITECOMBINE = 0x400;
// -------------------------------------------------------------------------
// Process creation flags
// -------------------------------------------------------------------------
public const uint CREATE_SUSPENDED = 0x00000004;
public const uint CREATE_NO_WINDOW = 0x08000000;
public const uint CREATE_NEW_CONSOLE = 0x00000010;
public const uint CREATE_NEW_PROCESS_GROUP = 0x00000200;
public const uint CREATE_UNICODE_ENVIRONMENT = 0x00000400;
public const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
public const uint DETACHED_PROCESS = 0x00000008;
// -------------------------------------------------------------------------
// File / named pipe / I/O
// ------------------------------------------------------------------------------------------
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint GENERIC_EXECUTE = 0x20000000;
public const uint GENERIC_ALL = 0x10000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint FILE_SHARE_DELETE = 0x00000004;
public const uint OPEN_EXISTING = 3;
public const uint CREATE_ALWAYS = 2;
public const uint CREATE_NEW = 1;
public const uint OPEN_ALWAYS = 4;
public const uint TRUNCATE_EXISTING = 5;
public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
public const uint FILE_ATTRIBUTE_HIDDEN = 0x02;
public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
public const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
public const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;
public const uint PIPE_ACCESS_DUPLEX = 0x00000003;
public const uint PIPE_ACCESS_INBOUND = 0x00000001;
public const uint PIPE_ACCESS_OUTBOUND = 0x00000002;
public const uint PIPE_TYPE_BYTE = 0x00000000;
public const uint PIPE_TYPE_MESSAGE = 0x00000004;
public const uint PIPE_READMODE_BYTE = 0x00000000;
public const uint PIPE_READMODE_MESSAGE = 0x00000002;
public const uint PIPE_WAIT = 0x00000000;
public const uint PIPE_NOWAIT = 0x00000001;
public const uint PIPE_UNLIMITED_INSTANCES = 255;
public const uint NMPWAIT_WAIT_FOREVER = 0xFFFFFFFF;
public const uint NMPWAIT_NOWAIT = 0x00000001;
// ---------------------------------------------------------------------------------
// Service control manager
// -------------------------------------------------------------------------
public const uint SC_MANAGER_ALL_ACCESS = 0xF003F;
public const uint SC_MANAGER_CONNECT = 0x0001;
public const uint SC_MANAGER_CREATE_SERVICE = 0x0002;
public const uint SC_MANAGER_ENUMERATE_SERVICE = 0x0004;
public const uint SC_MANAGER_LOCK = 0x0008;
public const uint SC_MANAGER_QUERY_LOCK_STATUS = 0x0010;
public const uint SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020;
public const uint SERVICE_ALL_ACCESS = 0xF01FF;
public const uint SERVICE_QUERY_CONFIG = 0x0001;
public const uint SERVICE_CHANGE_CONFIG = 0x0002;
public const uint SERVICE_QUERY_STATUS = 0x0004;
public const uint SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
public const uint SERVICE_START = 0x0010;
public const uint SERVICE_STOP = 0x0020;
public const uint SERVICE_PAUSE_CONTINUE = 0x0040;
public const uint SERVICE_WIN32_OWN_PROCESS = 0x00000010;
public const uint SERVICE_DEMAND_START = 0x00000003;
public const uint SERVICE_AUTO_START = 0x00000002;
public const uint SERVICE_ERROR_NORMAL = 0x00000001;
public const uint SERVICE_CONTROL_STOP = 0x00000001;
public const uint SERVICE_CONTROL_PAUSE = 0x00000002;
public const uint SERVICE_CONTROL_CONTINUE = 0x00000003;
public const uint SERVICE_RUNNING = 0x00000004;
public const uint SERVICE_STOPPED = 0x00000001;
// -----------------------------------------------------------------
// Registry
// -------------------------------------------------------------------------
public const uint KEY_READ = 0x20019;
public const uint KEY_WRITE = 0x20006;
public const uint KEY_ALL_ACCESS = 0xF003F;
public const uint KEY_QUERY_VALUE = 0x0001;
public const uint KEY_SET_VALUE = 0x0002;
public const uint KEY_CREATE_SUB_KEY = 0x0004;
public const uint KEY_ENUMERATE_SUB_KEYS = 0x0008;
public const uint KEY_WOW64_64KEY = 0x0100;
public const uint KEY_WOW64_32KEY = 0x0200;
public static readonly IntPtr HKEY_LOCAL_MACHINE = new(-2147483646);
public static readonly IntPtr HKEY_CURRENT_USER = new(-2147483647);
public static readonly IntPtr HKEY_CLASSES_ROOT = new(-2147483648);
public static readonly IntPtr HKEY_USERS = new(-2147483645);
public const uint REG_SZ = 1;
public const uint REG_EXPAND_SZ = 2;
public const uint REG_BINARY = 3;
public const uint REG_DWORD = 4;
public const uint REG_QWORD = 11;
public const uint REG_MULTI_SZ = 7;
// --------------------------------------------------------------
// Synchronisation / handle flags
// -------------------------------------------------------------------------
public const uint INFINITE = 0xFFFFFFFF;
public const uint WAIT_OBJECT_0 = 0x00000000;
public const uint WAIT_TIMEOUT = 0x00000102;
public const uint WAIT_ABANDONED = 0x00000080;
public const uint WAIT_FAILED = 0xFFFFFFFF;
public const int DUPLICATE_SAME_ACCESS = 0x2;
public const int DUPLICATE_CLOSE_SOURCE = 0x1;
public const int STARTF_USESTDHANDLES = 0x100;
public const int STARTF_USESHOWWINDOW = 0x001;
// --------------------------------------------------------------
// Token / privilege misc
// -------------------------------------------------------------------------
public const uint SE_PRIVILEGE_ENABLED = 0x00000002;
public const uint SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const uint SE_PRIVILEGE_REMOVED = 0x00000004;
public const uint TOKEN_ELEVATION_TYPE = 18;
public const int SecurityAnonymous = 0;
public const int SecurityIdentification = 1;
public const int SecurityImpersonation = 2;
public const int SecurityDelegation = 3;
public const int TokenPrimary = 1;
public const int TokenImpersonation = 2;
// ----------------------------------------------------
// Structs
// -------------------------------------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct STARTUPINFO
{
public uint cb;
public string? lpReserved;
public string? lpDesktop;
public string? lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public uint Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public IntPtr RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public ushort wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public IntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
}
[StructLayout(LayoutKind.Sequential)]
public struct OVERLAPPED
{
public IntPtr Internal;
public IntPtr InternalHigh;
public uint Offset;
public uint OffsetHigh;
public IntPtr hEvent;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SERVICE_STATUS
{
public uint dwServiceType;
public uint dwCurrentState;
public uint dwControlsAccepted;
public uint dwWin32ExitCode;
public uint dwServiceSpecificExitCode;
public uint dwCheckPoint;
public uint dwWaitHint;
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_ATTRIBUTES
{
public uint Length;
public IntPtr RootDirectory;
public IntPtr ObjectName;
public uint Attributes;
public IntPtr SecurityDescriptor;
public IntPtr SecurityQualityOfService;
}
[StructLayout(LayoutKind.Sequential)]
public struct CLIENT_ID
{
public IntPtr UniqueProcess;
public IntPtr UniqueThread;
}
[StructLayout(LayoutKind.Sequential)]
public struct UNICODE_STRING
{
public ushort Length;
public ushort MaximumLength;
public IntPtr Buffer;
}
[StructLayout(LayoutKind.Sequential)]
public struct IO_STATUS_BLOCK
{
public IntPtr Status;
public IntPtr Information;
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_HANDLE_TABLE_ENTRY_INFO
{
public ushort UniqueProcessId;
public ushort CreatorBackTraceIndex;
public byte ObjectTypeIndex;
public byte HandleAttributes;
public ushort HandleValue;
public IntPtr Object;
public uint GrantedAccess;
}
[StructLayout(LayoutKind.Sequential)]
public struct MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
}
// -------------------------------------------------------------------------
// kernel32 — process / thread
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll")]
public static extern uint GetCurrentProcessId();
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ResumeThread(IntPtr hThread);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SuspendThread(IntPtr hThread);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetExitCodeThread(IntPtr hThread, out uint lpExitCode);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateProcess(
string? lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string? lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
IntPtr lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
out uint lpThreadId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr QueueUserAPC(IntPtr pfnAPC, IntPtr hThread, IntPtr dwData);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DuplicateHandle(
IntPtr hSourceProcessHandle,
IntPtr hSourceHandle,
IntPtr hTargetProcessHandle,
out IntPtr lpTargetHandle,
uint dwDesiredAccess,
bool bInheritHandle,
uint dwOptions);
// -------------------------------------------------------------------------
// kernel32 — memory
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, out int lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, out int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualFree(IntPtr lpAddress, int dwSize, uint dwFreeType);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
[DllImport("kernel32.dll")]
public static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
// -------------------------------------------------------------------------
// kernel32 — synchronisation
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string? lpName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetEvent(IntPtr hEvent);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ResetEvent(IntPtr hEvent);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner, string? lpName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr OpenMutex(uint dwDesiredAccess, bool bInheritHandle, string lpName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReleaseMutex(IntPtr hMutex);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateSemaphore(IntPtr lpSemaphoreAttributes, int lInitialCount, int lMaximumCount, string? lpName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReleaseSemaphore(IntPtr hSemaphore, int lReleaseCount, out int lpPreviousCount);
// -------------------------------------------------------------------------
// kernel32 — named pipes
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateNamedPipe(
string lpName,
uint dwOpenMode,
uint dwPipeMode,
uint nMaxInstances,
uint nOutBufferSize,
uint nInBufferSize,
uint nDefaultTimeOut,
IntPtr lpSecurityAttributes);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DisconnectNamedPipe(IntPtr hNamedPipe);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool WaitNamedPipe(string lpNamedPipeName, uint nTimeOut);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetNamedPipeInfo(
IntPtr hNamedPipe,
out uint lpFlags,
out uint lpOutBufferSize,
out uint lpInBufferSize,
out uint lpMaxInstances);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetNamedPipeHandleState(
IntPtr hNamedPipe,
out uint lpState,
out uint lpCurInstances,
out uint lpMaxCollectionCount,
out uint lpCollectDataTimeout,
StringBuilder? lpUserName,
uint nMaxUserNameSize);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool PeekNamedPipe(
IntPtr hNamedPipe,
byte[]? lpBuffer,
uint nBufferSize,
out uint lpBytesRead,
out uint lpTotalBytesAvail,
out uint lpBytesLeftThisMessage);
// -------------------------------------------------------------------------
// kernel32 — file I/O
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FlushFileBuffers(IntPtr hFile);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetFilePointerEx(
IntPtr hFile,
long liDistanceToMove,
out long lpNewFilePointer,
uint dwMoveMethod);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetFileSizeEx(IntPtr hFile, out long lpFileSize);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, uint dwFlags);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool DeleteFile(string lpFileName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
// -------------------------------------------------------------------------
// kernel32 — library loading
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string? moduleName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, uint nSize);
// -------------------------------------------------------------------------
// kernel32 — misc
// -------------------------------------------------------------------------
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool QueryFullProcessImageName(
IntPtr hProcess,
uint dwFlags,
StringBuilder lpExeName,
ref uint lpdwSize);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool pbDebuggerPresent);
[DllImport("kernel32.dll")]
public static extern bool IsDebuggerPresent();
[DllImport("kernel32.dll")]
public static extern void OutputDebugString(string lpOutputString);
// -------------------------------------------------------------------------
// advapi32 — token
// -------------------------------------------------------------------------
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool DuplicateTokenEx(
IntPtr hExistingToken,
uint dwDesiredAccess,
IntPtr lpTokenAttributes,
int ImpersonationLevel,
int TokenType,
out IntPtr phNewToken);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool GetTokenInformation(
IntPtr hToken,
uint TokenInformationClass,
IntPtr TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool SetTokenInformation(
IntPtr hToken,
uint TokenInformationClass,
IntPtr TokenInformation,
uint TokenInformationLength);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LookupPrivilegeValue(string? lpSystemName, string lpName, out LUID lpLuid);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LookupPrivilegeName(
string? lpSystemName,
ref LUID lpLuid,
StringBuilder lpName,
ref uint cchName);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AdjustTokenPrivileges(
IntPtr TokenHandle,
bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
uint BufferLength,
IntPtr PreviousState,
IntPtr ReturnLength);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateProcessWithTokenW(
IntPtr hToken,
uint dwLogonFlags,
string? lpApplicationName,
string lpCommandLine,
uint dwCreationFlags,
IntPtr lpEnvironment,
string? lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateProcessAsUser(
IntPtr hToken,
string? lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string? lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken);
// -------------------------------------------------------------------------
// advapi32 — service control manager
// -------------------------------------------------------------------------
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr OpenSCManager(string? lpMachineName, string? lpDatabaseName, uint dwDesiredAccess);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateService(
IntPtr hSCManager,
string lpServiceName,
string lpDisplayName,
uint dwDesiredAccess,
uint dwServiceType,
uint dwStartType,
uint dwErrorControl,
string lpBinaryPathName,
string? lpLoadOrderGroup,
IntPtr lpdwTagId,
string? lpDependencies,
string? lpServiceStartName,
string? lpPassword);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool StartService(IntPtr hService, uint dwNumServiceArgs, string[]? lpServiceArgVectors);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool ControlService(IntPtr hService, uint dwControl, out SERVICE_STATUS lpServiceStatus);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool DeleteService(IntPtr hService);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool QueryServiceStatus(IntPtr hService, out SERVICE_STATUS lpServiceStatus);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool CloseServiceHandle(IntPtr hSCObject);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool ChangeServiceConfig(
IntPtr hService,
uint dwServiceType,
uint dwStartType,
uint dwErrorControl,
string? lpBinaryPathName,
string? lpLoadOrderGroup,
IntPtr lpdwTagId,
string? lpDependencies,
string? lpServiceStartName,
string? lpPassword,
string? lpDisplayName);
// -------------------------------------------------------------------------
// advapi32 — registry
// -------------------------------------------------------------------------
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, uint samDesired, out IntPtr phkResult);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegCreateKeyEx(
IntPtr hKey,
string lpSubKey,
uint Reserved,
string? lpClass,
uint dwOptions,
uint samDesired,
IntPtr lpSecurityAttributes,
out IntPtr phkResult,
out uint lpdwDisposition);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegQueryValueEx(
IntPtr hKey,
string lpValueName,
IntPtr lpReserved,
out uint lpType,
byte[]? lpData,
ref uint lpcbData);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegSetValueEx(
IntPtr hKey,
string lpValueName,
uint Reserved,
uint dwType,
byte[] lpData,
uint cbData);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegDeleteValue(IntPtr hKey, string lpValueName);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegDeleteKeyEx(IntPtr hKey, string lpSubKey, uint samDesired, uint Reserved);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegEnumKeyEx(
IntPtr hKey,
uint dwIndex,
StringBuilder lpName,
ref uint lpcchName,
IntPtr lpReserved,
StringBuilder? lpClass,
IntPtr lpcchClass,
out long lpftLastWriteTime);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegEnumValue(
IntPtr hKey,
uint dwIndex,
StringBuilder lpValueName,
ref uint lpcchValueName,
IntPtr lpReserved,
out uint lpType,
byte[]? lpData,
ref uint lpcbData);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern int RegCloseKey(IntPtr hKey);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RegConnectRegistry(string? lpMachineName, IntPtr hKey, out IntPtr phkResult);
// -------------------------------------------------------------------------
// ntdll
// -------------------------------------------------------------------------
[DllImport("ntdll.dll", SetLastError = true)]
public static extern int NtQueryInformationProcess(
IntPtr ProcessHandle,
int ProcessInformationClass,
IntPtr ProcessInformation,
uint ProcessInformationLength,
out uint ReturnLength);
[DllImport("ntdll.dll")]
public static extern int NtSetInformationProcess(
IntPtr ProcessHandle,
int ProcessInformationClass,
IntPtr ProcessInformation,
uint ProcessInformationLength);
[DllImport("ntdll.dll")]
public static extern int NtQueryInformationThread(
IntPtr ThreadHandle,
int ThreadInformationClass,
IntPtr ThreadInformation,
uint ThreadInformationLength,
out uint ReturnLength);
[DllImport("ntdll.dll")]
public static extern int NtSetInformationThread(
IntPtr ThreadHandle,
int ThreadInformationClass,
IntPtr ThreadInformation,
uint ThreadInformationLength);
[DllImport("ntdll.dll")]
public static extern int NtSuspendProcess(IntPtr ProcessHandle);
[DllImport("ntdll.dll")]
public static extern int NtResumeProcess(IntPtr ProcessHandle);
[DllImport("ntdll.dll")]
public static extern int NtTerminateProcess(IntPtr ProcessHandle, uint ExitStatus);
[DllImport("ntdll.dll")]
public static extern int NtQuerySystemInformation(
int SystemInformationClass,
IntPtr SystemInformation,
uint SystemInformationLength,
out uint ReturnLength);
[DllImport("ntdll.dll")]
public static extern int NtQueryObject(
IntPtr ObjectHandle,
int ObjectInformationClass,
IntPtr ObjectInformation,
uint ObjectInformationLength,
out uint ReturnLength);
[DllImport("ntdll.dll")]
public static extern int NtDuplicateObject(
IntPtr SourceProcessHandle,
IntPtr SourceHandle,
IntPtr TargetProcessHandle,
out IntPtr TargetHandle,
uint DesiredAccess,
uint HandleAttributes,
uint Options);
[DllImport("ntdll.dll")]
public static extern int NtClose(IntPtr Handle);
[DllImport("ntdll.dll")]
public static extern int NtOpenProcess(
out IntPtr ProcessHandle,
uint DesiredAccess,
ref OBJECT_ATTRIBUTES ObjectAttributes,
ref CLIENT_ID ClientId);
[DllImport("ntdll.dll")]
public static extern int NtOpenThread(
out IntPtr ThreadHandle,
uint DesiredAccess,
ref OBJECT_ATTRIBUTES ObjectAttributes,
ref CLIENT_ID ClientId);
[DllImport("ntdll.dll")]
public static extern int NtDelayExecution(bool Alertable, ref long DelayInterval);
[DllImport("ntdll.dll")]
public static extern int NtFlushInstructionCache(
IntPtr ProcessHandle,
IntPtr BaseAddress,
uint NumberOfBytesToFlush);
[DllImport("ntdll.dll")]
public static extern int NtQueryVirtualMemory(
IntPtr ProcessHandle,
IntPtr BaseAddress,
int MemoryInformationClass,
out MEMORY_BASIC_INFORMATION MemoryInformation,
uint MemoryInformationLength,
out uint ReturnLength);
// -------------------------------------------------------------------------