NativeAPI.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace WechatBakTool.Helpers
  4. {
  5. public class NativeAPI
  6. {
  7. // Constants
  8. //=================================================
  9. internal static uint NTSTATUS_STATUS_SUCCESS = 0x0;
  10. internal static uint NTSTATUS_STATUS_INFO_LENGTH_MISMATCH = 0xC0000004;
  11. internal static uint NTSTATUS_STATUS_ACCESS_DENIED = 0xC0000022;
  12. // API Constants
  13. internal static uint SystemExtendedHandleInformation = 0x40;
  14. internal static uint DUPLICATE_SAME_ACCESS = 0x2;
  15. // Structs
  16. //=================================================
  17. [StructLayout(LayoutKind.Sequential)]
  18. internal struct OBJECT_NAME_INFORMATION
  19. {
  20. public UNICODE_STRING Name;
  21. }
  22. [StructLayout(LayoutKind.Sequential)]
  23. internal struct OSVERSIONINFOEX
  24. {
  25. public uint OSVersionInfoSize;
  26. public uint MajorVersion;
  27. public uint MinorVersion;
  28. public uint BuildNumber;
  29. public uint PlatformId;
  30. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  31. public string CSDVersion;
  32. public ushort ServicePackMajor;
  33. public ushort ServicePackMinor;
  34. public ushort SuiteMask;
  35. public byte ProductType;
  36. public byte Reserved;
  37. }
  38. [StructLayout(LayoutKind.Sequential)]
  39. internal struct UNICODE_STRING
  40. {
  41. public ushort Length;
  42. public ushort MaximumLength;
  43. public IntPtr Buffer;
  44. }
  45. [StructLayout(LayoutKind.Sequential)]
  46. internal struct GENERIC_MAPPING
  47. {
  48. public uint GenericRead;
  49. public uint GenericWrite;
  50. public uint GenericExecute;
  51. public uint GenericAll;
  52. }
  53. [StructLayout(LayoutKind.Sequential)]
  54. internal struct OBJECT_TYPE_INFORMATION
  55. {
  56. public UNICODE_STRING TypeName;
  57. public uint TotalNumberOfObjects;
  58. public uint TotalNumberOfHandles;
  59. public uint TotalPagedPoolUsage;
  60. public uint TotalNonPagedPoolUsage;
  61. public uint TotalNamePoolUsage;
  62. public uint TotalHandleTableUsage;
  63. public uint HighWaterNumberOfObjects;
  64. public uint HighWaterNumberOfHandles;
  65. public uint HighWaterPagedPoolUsage;
  66. public uint HighWaterNonPagedPoolUsage;
  67. public uint HighWaterNamePoolUsage;
  68. public uint HighWaterHandleTableUsage;
  69. public uint InvalidAttributes;
  70. public GENERIC_MAPPING GenericMapping;
  71. public uint ValidAccessMask;
  72. public byte SecurityRequired;
  73. public byte MaintainHandleCount;
  74. public byte TypeIndex;
  75. public byte ReservedByte;
  76. public uint PoolType;
  77. public uint DefaultPagedPoolCharge;
  78. public uint DefaultNonPagedPoolCharge;
  79. }
  80. [StructLayout(LayoutKind.Sequential)]
  81. internal struct OBJECT_ALL_TYPES_INFORMATION
  82. {
  83. public uint NumberOfObjectTypes;
  84. }
  85. [StructLayout(LayoutKind.Sequential)]
  86. internal struct SYSTEM_HANDLE_INFORMATION_EX
  87. {
  88. public IntPtr NumberOfHandles;
  89. public IntPtr Reserved;
  90. public SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX[] Handles;
  91. }
  92. [StructLayout(LayoutKind.Sequential)]
  93. internal struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX
  94. {
  95. public IntPtr Object;
  96. public IntPtr UniqueProcessId;
  97. public IntPtr HandleValue;
  98. public uint GrantedAccess;
  99. public ushort CreatorBackTraceIndex;
  100. public ushort ObjectTypeIndex;
  101. public uint HandleAttributes;
  102. public uint Reserved;
  103. }
  104. // Enums
  105. //=================================================
  106. internal enum OBJECT_INFORMATION_CLASS
  107. {
  108. ObjectBasicInformation = 0,
  109. ObjectNameInformation = 1,
  110. ObjectTypeInformation = 2,
  111. ObjectAllTypesInformation = 3,
  112. ObjectHandleInformation = 4
  113. }
  114. internal enum POOL_TYPE
  115. {
  116. NonPagedPool,
  117. NonPagedPoolExecute = NonPagedPool,
  118. PagedPool,
  119. NonPagedPoolMustSucceed = NonPagedPool + 2,
  120. DontUseThisType,
  121. NonPagedPoolCacheAligned = NonPagedPool + 4,
  122. PagedPoolCacheAligned,
  123. NonPagedPoolCacheAlignedMustS = NonPagedPool + 6,
  124. MaxPoolType,
  125. NonPagedPoolBase = 0,
  126. NonPagedPoolBaseMustSucceed = NonPagedPoolBase + 2,
  127. NonPagedPoolBaseCacheAligned = NonPagedPoolBase + 4,
  128. NonPagedPoolBaseCacheAlignedMustS = NonPagedPoolBase + 6,
  129. NonPagedPoolSession = 32,
  130. PagedPoolSession = NonPagedPoolSession + 1,
  131. NonPagedPoolMustSucceedSession = PagedPoolSession + 1,
  132. DontUseThisTypeSession = NonPagedPoolMustSucceedSession + 1,
  133. NonPagedPoolCacheAlignedSession = DontUseThisTypeSession + 1,
  134. PagedPoolCacheAlignedSession = NonPagedPoolCacheAlignedSession + 1,
  135. NonPagedPoolCacheAlignedMustSSession = PagedPoolCacheAlignedSession + 1,
  136. NonPagedPoolNx = 512,
  137. NonPagedPoolNxCacheAligned = NonPagedPoolNx + 4,
  138. NonPagedPoolSessionNx = NonPagedPoolNx + 32,
  139. }
  140. internal enum PROCESS_ACCESS_FLAGS : uint
  141. {
  142. All = 0x001F0FFF,
  143. Terminate = 0x00000001,
  144. CreateThread = 0x00000002,
  145. VMOperation = 0x00000008,
  146. VMRead = 0x00000010,
  147. VMWrite = 0x00000020,
  148. DupHandle = 0x00000040,
  149. SetInformation = 0x00000200,
  150. QueryInformation = 0x00000400,
  151. Synchronize = 0x00100000
  152. }
  153. // API
  154. //=================================================
  155. [DllImport("kernel32.dll")]
  156. internal static extern bool CloseHandle(IntPtr hObject);
  157. [DllImport("ntdll.dll")]
  158. internal static extern uint RtlGetVersion(
  159. ref OSVERSIONINFOEX VersionInformation);
  160. [DllImport("ntdll.dll")]
  161. internal static extern void RtlZeroMemory(
  162. IntPtr Destination,
  163. uint length);
  164. [DllImport("ntdll.dll")]
  165. internal static extern uint NtQueryObject(
  166. IntPtr objectHandle,
  167. OBJECT_INFORMATION_CLASS informationClass,
  168. IntPtr informationPtr,
  169. uint informationLength,
  170. ref uint returnLength);
  171. [DllImport("ntdll.dll")]
  172. internal static extern uint NtQuerySystemInformation(
  173. uint SystemInformationClass,
  174. IntPtr SystemInformation,
  175. uint SystemInformationLength,
  176. ref uint ReturnLength);
  177. [DllImport("kernel32.dll")]
  178. internal static extern IntPtr OpenProcess(PROCESS_ACCESS_FLAGS dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
  179. [DllImport("kernel32.dll", SetLastError = true)]
  180. [return: MarshalAs(UnmanagedType.Bool)]
  181. internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
  182. [DllImport("kernel32.dll")]
  183. internal static extern IntPtr GetCurrentProcess();
  184. }
  185. }