Advertisement
Guest User

Memory.cs

a guest
Sep 30th, 2015
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1.     public class Memory
  2.     {
  3.         private Process m_ReadProcess = null;
  4.         private IntPtr m_hProcess = IntPtr.Zero;
  5.  
  6.         public Process ReadProcess
  7.         {
  8.             get
  9.             {
  10.                 return m_ReadProcess;
  11.             }
  12.             set
  13.             {
  14.                 m_ReadProcess = value;
  15.             }
  16.         }
  17.  
  18.         public void Open()
  19.         {
  20.             ProcessAccessType access = ProcessAccessType.PROCESS_VM_READ
  21.             | ProcessAccessType.PROCESS_VM_WRITE
  22.             | ProcessAccessType.PROCESS_VM_OPERATION;
  23.             m_hProcess = OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
  24.         }
  25.  
  26.         public void CloseHandle()
  27.         {
  28.             int iRetValue;
  29.             iRetValue = CloseHandle(m_hProcess);
  30.             if (iRetValue == 0)
  31.             {
  32.                 throw new Exception("CloseHandle Failed");
  33.             }
  34.         }
  35.  
  36.         public byte[] Read(IntPtr MemoryAddress, uint bytesToRead, out int bytesRead)
  37.         {
  38.             byte[] buffer = new byte[bytesToRead];
  39.             IntPtr ptrBytesRead;
  40.             ReadProcessMemory(m_hProcess, MemoryAddress, buffer, bytesToRead, out ptrBytesRead);
  41.             bytesRead = ptrBytesRead.ToInt32();
  42.             return buffer;
  43.         }
  44.  
  45.         public byte[] PointerRead(IntPtr MemoryAddress, uint bytesToRead, int[] Offset, out int bytesRead)
  46.         {
  47.             int iPointerCount = Offset.Length - 1;
  48.             IntPtr ptrBytesRead;
  49.             bytesRead = 0;
  50.             byte[] buffer = new byte[4]; //DWORD to hold an Address
  51.             int tempAddress = 0;
  52.  
  53.             if (iPointerCount == 0)
  54.             {
  55.  
  56.                
  57.                 ReadProcessMemory(m_hProcess, MemoryAddress, buffer, 4, out ptrBytesRead);
  58.                 tempAddress = ToDec(Make(buffer)) + Offset[0]; //Final Address
  59.  
  60.                 buffer = new byte[bytesToRead];
  61.                 ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, bytesToRead, out ptrBytesRead);
  62.  
  63.                 bytesRead = ptrBytesRead.ToInt32();
  64.                
  65.                 return buffer;
  66.             }
  67.  
  68.             for (int i = 0; i <= iPointerCount; i++)
  69.             {
  70.                 if (i == iPointerCount)
  71.                 {
  72.                     ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, 4, out ptrBytesRead);
  73.                     tempAddress = ToDec(Make(buffer)) + Offset[i]; //Final Address
  74.  
  75.                     buffer = new byte[bytesToRead];
  76.                     ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, bytesToRead, out ptrBytesRead);
  77.  
  78.                     bytesRead = ptrBytesRead.ToInt32();
  79.                     return buffer;
  80.                 }
  81.                 else if (i == 0)
  82.                 {
  83.                     ReadProcessMemory(m_hProcess, MemoryAddress, buffer, 4, out ptrBytesRead);
  84.                     tempAddress = ToDec(Make(buffer)) + Offset[i];
  85.                 }
  86.                 else
  87.                 {
  88.                    ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, 4, out ptrBytesRead);
  89.                     tempAddress = ToDec(Make(buffer)) + Offset[i];
  90.                 }
  91.             }
  92.  
  93.             return buffer;
  94.         }
  95.  
  96.         public void Write(IntPtr MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
  97.         {
  98.             IntPtr ptrBytesWritten;
  99.             WriteProcessMemory(m_hProcess, MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
  100.             bytesWritten = ptrBytesWritten.ToInt32();
  101.         }
  102.  
  103.         public string PointerWrite(IntPtr MemoryAddress, byte[] bytesToWrite, int[] Offset, out int bytesWritten)
  104.         {
  105.             int iPointerCount = Offset.Length - 1;
  106.             IntPtr ptrBytesWritten;
  107.             bytesWritten = 0;
  108.             byte[] buffer = new byte[4]; //DWORD to hold an Address
  109.             int tempAddress = 0;
  110.  
  111.             if (iPointerCount == 0)
  112.             {
  113.                 ReadProcessMemory(m_hProcess, MemoryAddress, buffer, 4, out ptrBytesWritten);
  114.                 tempAddress = ToDec(Make(buffer)) + Offset[0]; //Final Address
  115.                 WriteProcessMemory(m_hProcess, (IntPtr)tempAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
  116.  
  117.                 bytesWritten = ptrBytesWritten.ToInt32();
  118.                 return ToHex(tempAddress);
  119.             }
  120.  
  121.             //only necessary for multiple OFFSETS
  122.             for (int i = 0; i <= iPointerCount; i++)
  123.             {
  124.                 if (i == iPointerCount)
  125.                 {
  126.                     ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, 4, out ptrBytesWritten);
  127.                     tempAddress = ToDec(Make(buffer)) + Offset[i]; //Final Address
  128.                     WriteProcessMemory(m_hProcess, (IntPtr)tempAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
  129.  
  130.                     bytesWritten = ptrBytesWritten.ToInt32();
  131.                     return ToHex(tempAddress);
  132.                 }
  133.                 else if (i == 0)
  134.                 {
  135.                     ReadProcessMemory(m_hProcess, MemoryAddress, buffer, 4, out ptrBytesWritten);
  136.                     tempAddress = ToDec(Make(buffer)) + Offset[i];
  137.                 }
  138.                 else
  139.                 {
  140.                     ReadProcessMemory(m_hProcess, (IntPtr)tempAddress, buffer, 4, out ptrBytesWritten);
  141.                     tempAddress = ToDec(Make(buffer)) + Offset[i];
  142.                 }
  143.             }
  144.  
  145.             return ToHex(tempAddress);
  146.         }
  147.  
  148.         public int PID()
  149.         {
  150.             return m_ReadProcess.Id;
  151.         }
  152.  
  153.         public string BaseAddressH()
  154.         {
  155.             return ToHex(m_ReadProcess.MainModule.BaseAddress.ToInt32());
  156.         }
  157.  
  158.         public int BaseAddressD()
  159.         {
  160.             return m_ReadProcess.MainModule.BaseAddress.ToInt32();
  161.         }
  162.  
  163.         [Flags]
  164.         public enum ProcessAccessType
  165.         {
  166.             PROCESS_TERMINATE = (0x0001),
  167.             PROCESS_CREATE_THREAD = (0x0002),
  168.             PROCESS_SET_SESSIONID = (0x0004),
  169.             PROCESS_VM_OPERATION = (0x0008),
  170.             PROCESS_VM_READ = (0x0010),
  171.             PROCESS_VM_WRITE = (0x0020),
  172.             PROCESS_DUP_HANDLE = (0x0040),
  173.             PROCESS_CREATE_PROCESS = (0x0080),
  174.             PROCESS_SET_QUOTA = (0x0100),
  175.             PROCESS_SET_INFORMATION = (0x0200),
  176.             PROCESS_QUERY_INFORMATION = (0x0400)
  177.         }
  178.  
  179.         public static string Make(byte[] buffer)
  180.         {
  181.             string sTemp = "";
  182.  
  183.             for (int i = 0; i < buffer.Length; i++)
  184.             {
  185.                 if (Convert.ToInt16(buffer[i]) < 16)
  186.                 {
  187.                     sTemp = "0" + ToHex(buffer[i]) + sTemp;
  188.                 }
  189.                 else
  190.                 {
  191.                     sTemp = ToHex(buffer[i]) + sTemp;
  192.                 }
  193.             }
  194.             return sTemp;
  195.         }
  196.  
  197.         public static string ToHex(int Decimal)
  198.         {
  199.             return Decimal.ToString("X"); //Convert Decimal to Hexadecimal
  200.         }
  201.  
  202.         public static int ToDec(string Hex)
  203.         {
  204.             return int.Parse(Hex, NumberStyles.HexNumber); //Convert Hexadecimal to Decimal
  205.         }
  206.  
  207.         [DllImport("kernel32.dll")]
  208.         public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
  209.  
  210.         [DllImport("kernel32.dll")]
  211.         public static extern Int32 CloseHandle(IntPtr hObject);
  212.  
  213.         [DllImport("kernel32.dll")]
  214.         public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
  215.  
  216.         [DllImport("kernel32.dll")]
  217.         public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
  218.  
  219.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement