AnonymousBOL

64bit C# Process.Modules

Feb 24th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1.     public class MemoryManager
  2.     {
  3.         #region DLLImports
  4.         [DllImport("kernel32.dll")]
  5.         public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  6.         [DllImport("kernel32.dll")]
  7.         public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);
  8.         [DllImport("kernel32.dll", SetLastError = true)]
  9.         public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);
  10.         [DllImport("kernel32.dll", SetLastError = true)]
  11.         public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);
  12.         [DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  13.         public static extern int EnumProcessModulesEx(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded, UIntPtr dwFilterFlags);
  14.         [DllImport("psapi.dll")]
  15.         static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] uint nSize);
  16.         [DllImport("psapi.dll", SetLastError = true)]
  17.         static extern bool GetModuleInformation(IntPtr hProcess, IntPtr hModule, out MODULEINFO lpmodinfo, uint cb);
  18.         public struct MODULEINFO
  19.         {
  20.             public IntPtr lpBaseOfDll;
  21.             public uint SizeOfImage;
  22.             public IntPtr EntryPoint;
  23.         }
  24.         #endregion
  25.         #region Offsets
  26.         #endregion
  27.         public static int processID = 0;
  28.         public static IntPtr processHandle = (IntPtr)0;
  29.         public static IntPtr clientPosition = (IntPtr)0;
  30.         public static IntPtr enginePosition = (IntPtr)0;
  31.         public static IntPtr hWnd { get; set; }
  32.  
  33.         public static void Init(int pid)
  34.         {
  35.             processID = pid;
  36.             processHandle = OpenProcess(2035711, false, pid);
  37.             Process csProc = Process.GetProcessById(pid);
  38.             hWnd = csProc.MainWindowHandle;
  39.             IntPtr[] hMods = new IntPtr[1024];
  40.             GCHandle gch = GCHandle.Alloc(hMods, GCHandleType.Pinned);
  41.             IntPtr pModules = gch.AddrOfPinnedObject();
  42.             uint uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
  43.             uint cbNeeded = 0;
  44.             if (EnumProcessModulesEx((IntPtr)csProc.Handle, pModules, uiSize, out cbNeeded, (UIntPtr)0x01) == 1)
  45.             {
  46.                 Int32 uiTotalNumberofModules = (Int32)(cbNeeded / (Marshal.SizeOf(typeof(IntPtr))));
  47.                 for (int i = 0; i < (int)uiTotalNumberofModules; i++)
  48.                 {
  49.                     StringBuilder strbld = new StringBuilder(1024);
  50.                     GetModuleFileNameEx((IntPtr)csProc.Handle, hMods[i], strbld, (uint)(strbld.Capacity));
  51.                     if (strbld.ToString().Substring(strbld.ToString().Length - DecodeB64("Y2xpZW50LmRsbA==").Length, DecodeB64("Y2xpZW50LmRsbA==").Length).Equals(DecodeB64("Y2xpZW50LmRsbA==")))
  52.                     {
  53.                         MODULEINFO ClientModule = new MODULEINFO();
  54.                         GetModuleInformation((IntPtr)csProc.Handle, hMods[i], out ClientModule, uiSize);
  55.                         clientPosition = ClientModule.lpBaseOfDll;
  56.                     }
  57.                     if (strbld.ToString().Substring(strbld.ToString().Length - DecodeB64("ZW5naW5lLmRsbA==").Length, DecodeB64("ZW5naW5lLmRsbA==").Length).Equals(DecodeB64("ZW5naW5lLmRsbA==")))
  58.                     {
  59.                         MODULEINFO ClientModule = new MODULEINFO();
  60.                         GetModuleInformation((IntPtr)csProc.Handle, hMods[i], out ClientModule, uiSize);
  61.                         enginePosition = ClientModule.lpBaseOfDll;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment