Advertisement
davidramos1

memoryreader

Feb 4th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace Rbot
  8. {
  9. public class MemoryReader
  10. {
  11. const int PROCESS_WM_READ = 0x0010;
  12.  
  13. [DllImport("kernel32.dll")]
  14. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  15.  
  16. [DllImport("kernel32.dll", SetLastError = true)]
  17. public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, uint dwSize, out int lpNumberOfBytesRead);
  18.  
  19. private IntPtr processHandle;
  20.  
  21. public MemoryReader(string gameName)
  22. {
  23. Process process = Process.GetProcesses()
  24. .FirstOrDefault(p => p.ProcessName.IndexOf(gameName, StringComparison.OrdinalIgnoreCase) >= 0);
  25. if (process != null)
  26. {
  27. IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
  28. }
  29. }
  30.  
  31. [DllImport("psapi.dll", SetLastError = true)]
  32. public static extern bool EnumProcessModules(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded);
  33.  
  34. [DllImport("psapi.dll")]
  35. public static extern uint GetModuleBaseName(IntPtr hProcess, IntPtr hModule, [Out] char[] lpBaseName, uint nSize);
  36.  
  37. public IntPtr GetModuleBaseAddress(string modName)
  38. {
  39. IntPtr[] modulePointers = new IntPtr[1024];
  40. GCHandle gch = GCHandle.Alloc(modulePointers, GCHandleType.Pinned);
  41. IntPtr pModules = gch.AddrOfPinnedObject();
  42.  
  43. if (EnumProcessModules(processHandle, pModules, (uint)(modulePointers.Length * IntPtr.Size), out uint _))
  44. {
  45. char[] moduleName = new char[1024];
  46. for (int i = 0; i < modulePointers.Length; i++)
  47. {
  48. if (modulePointers[i] == IntPtr.Zero)
  49. {
  50. continue;
  51. }
  52.  
  53. GetModuleBaseName(processHandle, modulePointers[i], moduleName, (uint)moduleName.Length);
  54.  
  55. if (modName.Equals(new string(moduleName).TrimEnd('\0')))
  56. {
  57. gch.Free();
  58. return modulePointers[i];
  59. }
  60. }
  61. }
  62.  
  63. gch.Free();
  64. return IntPtr.Zero;
  65. }
  66.  
  67. public IntPtr GetAbsoluteAddress(IntPtr moduleBaseAddress, int offset)
  68. {
  69. return new IntPtr(moduleBaseAddress.ToInt32() + offset);
  70. }
  71.  
  72. public long GetFinalAddress(IntPtr baseAddress, int[] offsets)
  73. {
  74. byte[] buffer = new byte[IntPtr.Size];
  75. foreach (int offset in offsets)
  76. {
  77. IntPtr tempAddress = IntPtr.Add(baseAddress, offset);
  78. buffer = ReadMemory(tempAddress.ToInt64(), 4);
  79. if (buffer.Length >= IntPtr.Size)
  80. {
  81. baseAddress = (IntPtr.Size == 4) ? new IntPtr(BitConverter.ToInt32(buffer, 0)) : new IntPtr(BitConverter.ToInt64(buffer, 0));
  82. }
  83. }
  84. return baseAddress.ToInt64();
  85. }
  86.  
  87. public byte[] ReadMemory(long address, int size)
  88. {
  89. byte[] buffer = new byte[size];
  90. IntPtr ptr = new IntPtr(address);
  91. ReadProcessMemory(processHandle, ptr, buffer, (uint)buffer.Length, out int bytesRead);
  92. return buffer;
  93. }
  94. }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement