C# 制作外挂常用的API

  1. C#做外挂的常用API,本人用了很久,基本没发现问题
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Runtime.InteropServices;  //这个肯定要的
  6. namespace WindowsApplication1
  7. {
  8. class win32API
  9. {
  10. public const int OPEN_PROCESS_ALL = 2035711;
  11. public const int PAGE_READWRITE = 4;
  12. public const int PROCESS_CREATE_THREAD = 2;
  13. public const int PROCESS_HEAP_ENTRY_BUSY = 4;
  14. public const int PROCESS_VM_OPERATION = 8;
  15. public const int PROCESS_VM_READ = 256;
  16. public const int PROCESS_VM_WRITE = 32;
  17. private const int PAGE_EXECUTE_READWRITE = 0x4;
  18. private const int MEM_COMMIT = 4096;
  19. private const int MEM_RELEASE = 0x8000;
  20. private const int MEM_DECOMMIT = 0x4000;
  21. private const int PROCESS_ALL_ACCESS = 0x1F0FFF;
  22. //查找窗体
  23. [DllImport("User32.dll", EntryPoint = "FindWindow")]
  24. public extern static IntPtr FindWindow(
  25. string lpClassName,
  26. string lpWindowName
  27. );
  28. //得到目标进程句柄的函数
  29. [DllImport("USER32.DLL")]
  30. public extern static int GetWindowThreadProcessId(
  31. int hwnd,
  32. ref int lpdwProcessId
  33. );
  34. [DllImport("USER32.DLL")]
  35. public extern static int GetWindowThreadProcessId(
  36. IntPtr hwnd,
  37. ref int lpdwProcessId
  38. );
  39. //打开进程
  40. [DllImport("kernel32.dll")]
  41. public extern static int OpenProcess(
  42. int dwDesiredAccess,
  43. int bInheritHandle,
  44. int dwProcessId
  45. );
  46. [DllImport("kernel32.dll")]
  47. public extern static IntPtr OpenProcess(
  48. uint dwDesiredAccess,
  49. int bInheritHandle,
  50. uint dwProcessId
  51. );
  52. //关闭句柄的函数
  53. [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
  54. public static extern int CloseHandle(
  55. int hObject
  56. );
  57. //读内存
  58. [DllImport("Kernel32.dll ")]
  59. public static extern Int32 ReadProcessMemory(
  60. IntPtr hProcess,
  61. IntPtr lpBaseAddress,
  62. [In, Out] byte[] buffer,
  63. int size,
  64. out IntPtr lpNumberOfBytesWritten
  65. );
  66. [DllImport("Kernel32.dll ")]
  67. public static extern Int32 ReadProcessMemory(
  68. int hProcess,
  69. int lpBaseAddress,
  70. ref int buffer,
  71. //byte[] buffer,
  72. int size,
  73. int lpNumberOfBytesWritten
  74. );
  75. [DllImport("Kernel32.dll ")]
  76. public static extern Int32 ReadProcessMemory(
  77. int hProcess,
  78. int lpBaseAddress,
  79. byte[] buffer,
  80. int size,
  81. int lpNumberOfBytesWritten
  82. );
  83. //写内存
  84. [DllImport("kernel32.dll")]
  85. public static extern Int32 WriteProcessMemory(
  86. IntPtr hProcess,
  87. IntPtr lpBaseAddress,
  88. [In, Out] byte[] buffer,
  89. int size,
  90. out IntPtr lpNumberOfBytesWritten
  91. );
  92. [DllImport("kernel32.dll")]
  93. public static extern Int32 WriteProcessMemory(
  94. int hProcess,
  95. int lpBaseAddress,
  96. byte[] buffer,
  97. int size,
  98. int lpNumberOfBytesWritten
  99. );
  100. //创建线程
  101. [DllImport("kernel32", EntryPoint = "CreateRemoteThread")]
  102. public static extern int CreateRemoteThread(
  103. int hProcess,
  104. int lpThreadAttributes,
  105. int dwStackSize,
  106. int lpStartAddress,
  107. int lpParameter,
  108. int dwCreationFlags,
  109. ref int lpThreadId
  110. );
  111. //开辟指定进程的内存空间
  112. [DllImport("Kernel32.dll")]
  113. public static extern System.Int32 VirtualAllocEx(
  114. System.IntPtr hProcess,
  115. System.Int32 lpAddress,
  116. System.Int32 dwSize,
  117. System.Int16 flAllocationType,
  118. System.Int16 flProtect
  119. );
  120. [DllImport("Kernel32.dll")]
  121. public static extern System.Int32 VirtualAllocEx(
  122. int hProcess,
  123. int lpAddress,
  124. int dwSize,
  125. int flAllocationType,
  126. int flProtect
  127. );
  128. //释放内存空间
  129. [DllImport("Kernel32.dll")]
  130. public static extern System.Int32 VirtualFreeEx(
  131. int hProcess,
  132. int lpAddress,
  133. int dwSize,
  134. int flAllocationType
  135. );
  136. }
  137. }
上一篇:【原创】《算法导论》链表一章带星习题试解——附C语言实现


下一篇:大数据篇:Zookeeper