Guest User

Untitled

a guest
Feb 18th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace AllocExTest
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         #region win32 imports
  18.         [DllImport("kernel32.dll", SetLastError = true)]
  19.         private static extern bool ReadProcessMemory(
  20.             IntPtr hProcess,
  21.             IntPtr lpBaseAddress,
  22.             byte[] lpBuffer,
  23.             uint dwSize,
  24.             uint lpNumberOfBytesRead);
  25.  
  26.         [DllImport("kernel32.dll", SetLastError = true)]
  27.         private static extern bool WriteProcessMemory(
  28.             IntPtr hProcess,
  29.             IntPtr lpBaseAddress,
  30.             byte[] lpBuffer,
  31.             uint nSize,
  32.             uint lpNumberOfBytesWritten);
  33.  
  34.         [DllImport("kernel32.dll", SetLastError = true)]
  35.         public static extern IntPtr VirtualAllocEx(
  36.             IntPtr hProcess,
  37.             IntPtr lpAddress,
  38.             uint dwSize,
  39.             uint flAllocationType,
  40.             uint flProtect);
  41.  
  42.         [DllImport("kernel32.dll", SetLastError = true)]
  43.         public static extern IntPtr OpenProcess(
  44.             uint dwDesiredAccess,
  45.             bool bInheritHandle,
  46.             uint dwProcessId);
  47.         [DllImport("kernel32.dll")]
  48.         private static extern bool VirtualProtectEx(
  49.             IntPtr hProcess,
  50.             IntPtr lpAddress,
  51.             UIntPtr dwSize,
  52.             uint flNewProtect,
  53.             out uint lpflOldProtect);
  54.         #endregion
  55.  
  56.         Process gameProcess = Process.GetProcessesByName("theHunterCotW_F").FirstOrDefault();
  57.         IntPtr hProcess;
  58.         IntPtr baseaddress;
  59.         public Form1()
  60.         {
  61.             InitializeComponent();
  62.             hProcess = OpenProcess(2035711U, false, (uint)gameProcess.Id);
  63.             baseaddress = gameProcess.MainModule.BaseAddress;
  64.         }
  65.  
  66.         private void button1_Click(object sender, EventArgs e)
  67.         {
  68.             /* ignore this
  69.  
  70.             MessageBox.Show(readMoney().ToString()); // work wonders
  71.             */
  72.  
  73.             PatternScanner patternscanner = new PatternScanner(hProcess);
  74.             patternscanner.SelectModule(gameProcess.MainModule);
  75.             long timetookms;
  76.             ulong offset = patternscanner.FindPattern("F3 0F 11 84 8B 68 05 00 00 F3 41 0F 10 00 0F 2F C2", out timetookms); // finds the correct place
  77.  
  78.             IntPtr lpAddress = baseaddress - 0x10000; // seems to be the place where cheat engine puts alloc() stuff
  79.             IntPtr thing = VirtualAllocEx(hProcess, lpAddress, 4096, (uint)AllocationType.Commit, (uint)VirtualMemoryProtection.PAGE_EXECUTE_READWRITE);
  80.             MessageBox.Show(thing.ToString() + " | " + Marshal.GetLastWin32Error()); // outputs 0 | 487
  81.         }
  82.  
  83.         private int readMoney()
  84.         {
  85.             IntPtr offset1 = (IntPtr)ReadUInt64(IntPtr.Add(baseaddress, 0x01E8B7F8));
  86.             IntPtr offset2 = (IntPtr)ReadUInt64(IntPtr.Add(offset1, 0x298));
  87.             return ReadInt32(offset2 + 0xa0);
  88.         }
  89.  
  90.         private byte[] ReadByteArray(IntPtr pOffset, uint pSize)
  91.         {
  92.             if (hProcess == IntPtr.Zero)
  93.                 throw new Exception("process is fucked");
  94.             try
  95.             {
  96.                 uint lpflOldProtect;
  97.                 VirtualProtectEx(hProcess, pOffset, (UIntPtr)pSize, (uint)VirtualMemoryProtection.PAGE_READWRITE, out lpflOldProtect);
  98.                 byte[] lpBuffer = new byte[pSize];
  99.                 ReadProcessMemory(hProcess, pOffset, lpBuffer, pSize, 0U);
  100.                 VirtualProtectEx(hProcess, pOffset, (UIntPtr)pSize, lpflOldProtect, out lpflOldProtect);
  101.                 return lpBuffer;
  102.             }
  103.             catch (Exception ex)
  104.             {
  105.                 throw new Exception("it broke");
  106.             }
  107.         }
  108.  
  109.         private int ReadInt32(IntPtr pOffset)
  110.         {
  111.             if (hProcess == IntPtr.Zero)
  112.                 throw new Exception("process is fucked");
  113.             try
  114.             {
  115.                 return BitConverter.ToInt32(this.ReadByteArray(pOffset, 4U), 0);
  116.             }
  117.             catch (Exception ex)
  118.             {
  119.                 return 0;
  120.             }
  121.         }
  122.  
  123.         private ulong ReadUInt64(IntPtr pOffset)
  124.         {
  125.             if (hProcess == IntPtr.Zero)
  126.                 throw new Exception("process is fucked");
  127.             try
  128.             {
  129.                 return BitConverter.ToUInt64(this.ReadByteArray(pOffset, 8U), 0);
  130.             }
  131.             catch (Exception ex)
  132.             {
  133.                 return 0;
  134.             }
  135.         }
  136.  
  137.     }
  138.  
  139.     public enum AllocationType : uint
  140.     {
  141.         Commit = 0x1000,
  142.         Reserve = 0x2000,
  143.         Decommit = 0x4000,
  144.         Release = 0x8000,
  145.         Reset = 0x80000,
  146.         Physical = 0x400000,
  147.         TopDown = 0x100000,
  148.         WriteWatch = 0x200000,
  149.         LargePages = 0x20000000
  150.     }
  151.  
  152.     public enum ProcessAccessFlags : uint
  153.     {
  154.         All = 2035711, // 0x001F0FFF
  155.         Terminate = 1,
  156.         CreateThread = 2,
  157.         VMOperation = 8,
  158.         VMRead = 16, // 0x00000010
  159.         VMWrite = 32, // 0x00000020
  160.         DupHandle = 64, // 0x00000040
  161.         SetInformation = 512, // 0x00000200
  162.         QueryInformation = 1024, // 0x00000400
  163.         Synchronize = 1048576, // 0x00100000
  164.     }
  165.  
  166.     public enum VirtualMemoryProtection : uint
  167.     {
  168.         PAGE_NOACCESS = 1,
  169.         PAGE_READONLY = 2,
  170.         PAGE_READWRITE = 4,
  171.         PAGE_WRITECOPY = 8,
  172.         PAGE_EXECUTE = 16, // 0x00000010
  173.         PAGE_EXECUTE_READ = 32, // 0x00000020
  174.         PAGE_EXECUTE_READWRITE = 64, // 0x00000040
  175.         PAGE_EXECUTE_WRITECOPY = 128, // 0x00000080
  176.         PAGE_GUARD = 256, // 0x00000100
  177.         PAGE_NOCACHE = 512, // 0x00000200
  178.         PROCESS_ALL_ACCESS = 2035711, // 0x001F0FFF
  179.     }
  180.  
  181.     public class PatternScanner
  182.     {
  183.         private IntPtr g_hProcess { get; set; }
  184.         private byte[] g_arrModuleBuffer { get; set; }
  185.         private ulong g_lpModuleBase { get; set; }
  186.  
  187.         private Dictionary<string, string> g_dictStringPatterns { get; }
  188.  
  189.         public PatternScanner(IntPtr hProc)
  190.         {
  191.             g_hProcess = hProc;
  192.             g_dictStringPatterns = new Dictionary<string, string>();
  193.         }
  194.  
  195.         public bool SelectModule(ProcessModule targetModule)
  196.         {
  197.             g_lpModuleBase = (ulong)targetModule.BaseAddress;
  198.             g_arrModuleBuffer = new byte[targetModule.ModuleMemorySize];
  199.  
  200.             g_dictStringPatterns.Clear();
  201.  
  202.             return Win32.ReadProcessMemory(g_hProcess, g_lpModuleBase, g_arrModuleBuffer, targetModule.ModuleMemorySize);
  203.         }
  204.  
  205.         public void AddPattern(string szPatternName, string szPattern)
  206.         {
  207.             g_dictStringPatterns.Add(szPatternName, szPattern);
  208.         }
  209.  
  210.         private bool PatternCheck(int nOffset, byte[] arrPattern)
  211.         {
  212.             for (int i = 0; i < arrPattern.Length; i++)
  213.             {
  214.                 if (arrPattern[i] == 0x0)
  215.                     continue;
  216.  
  217.                 if (arrPattern[i] != this.g_arrModuleBuffer[nOffset + i])
  218.                     return false;
  219.             }
  220.  
  221.             return true;
  222.         }
  223.  
  224.         public ulong FindPattern(string szPattern, out long lTime)
  225.         {
  226.             if (g_arrModuleBuffer == null || g_lpModuleBase == 0)
  227.                 throw new Exception("Selected module is null");
  228.  
  229.             Stopwatch stopwatch = Stopwatch.StartNew();
  230.  
  231.             byte[] arrPattern = ParsePatternString(szPattern);
  232.  
  233.             for (int nModuleIndex = 0; nModuleIndex < g_arrModuleBuffer.Length; nModuleIndex++)
  234.             {
  235.                 if (this.g_arrModuleBuffer[nModuleIndex] != arrPattern[0])
  236.                     continue;
  237.  
  238.                 if (PatternCheck(nModuleIndex, arrPattern))
  239.                 {
  240.                     lTime = stopwatch.ElapsedMilliseconds;
  241.                     return g_lpModuleBase + (ulong)nModuleIndex;
  242.                 }
  243.             }
  244.  
  245.             lTime = stopwatch.ElapsedMilliseconds;
  246.             return 0;
  247.         }
  248.         public Dictionary<string, ulong> FindPatterns(out long lTime)
  249.         {
  250.             if (g_arrModuleBuffer == null || g_lpModuleBase == 0)
  251.                 throw new Exception("Selected module is null");
  252.  
  253.             Stopwatch stopwatch = Stopwatch.StartNew();
  254.  
  255.             byte[][] arrBytePatterns = new byte[g_dictStringPatterns.Count][];
  256.             ulong[] arrResult = new ulong[g_dictStringPatterns.Count];
  257.  
  258.             for (int nIndex = 0; nIndex < g_dictStringPatterns.Count; nIndex++)
  259.                 arrBytePatterns[nIndex] = ParsePatternString(g_dictStringPatterns.ElementAt(nIndex).Value);
  260.  
  261.             for (int nModuleIndex = 0; nModuleIndex < g_arrModuleBuffer.Length; nModuleIndex++)
  262.             {
  263.                 for (int nPatternIndex = 0; nPatternIndex < arrBytePatterns.Length; nPatternIndex++)
  264.                 {
  265.                     if (arrResult[nPatternIndex] != 0)
  266.                         continue;
  267.  
  268.                     if (this.PatternCheck(nModuleIndex, arrBytePatterns[nPatternIndex]))
  269.                         arrResult[nPatternIndex] = g_lpModuleBase + (ulong)nModuleIndex;
  270.                 }
  271.             }
  272.  
  273.             Dictionary<string, ulong> dictResultFormatted = new Dictionary<string, ulong>();
  274.  
  275.             for (int nPatternIndex = 0; nPatternIndex < arrBytePatterns.Length; nPatternIndex++)
  276.                 dictResultFormatted[g_dictStringPatterns.ElementAt(nPatternIndex).Key] = arrResult[nPatternIndex];
  277.  
  278.             lTime = stopwatch.ElapsedMilliseconds;
  279.             return dictResultFormatted;
  280.         }
  281.  
  282.         private byte[] ParsePatternString(string szPattern)
  283.         {
  284.             List<byte> patternbytes = new List<byte>();
  285.  
  286.             foreach (var szByte in szPattern.Split(' '))
  287.                 patternbytes.Add(szByte == "?" ? (byte)0x0 : Convert.ToByte(szByte, 16));
  288.  
  289.             return patternbytes.ToArray();
  290.         }
  291.  
  292.         private static class Win32
  293.         {
  294.             [DllImport("kernel32.dll")]
  295.             public static extern bool ReadProcessMemory(IntPtr hProcess, ulong lpBaseAddress, byte[] lpBuffer, int dwSize, int lpNumberOfBytesRead = 0);
  296.         }
  297.     }
  298. }
Add Comment
Please, Sign In to add comment