2
0

NativeAPI.cs 8.5 KB

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