RIP198001

Untitled

Dec 7th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8. using System.Numerics;
  9.  
  10. namespace MemoryScanner
  11. {
  12.  
  13.  
  14.     class Program
  15.     {
  16.         private static string ProcessName = Encoding.UTF8.GetString(new byte[] { 68, 50, 82 });
  17.         public static int UnitTable = -1;
  18.         public static int InGameMap = -1;
  19.  
  20.  
  21.         [DllImport("kernel32.dll", SetLastError = true)]
  22.         public static extern IntPtr OpenProcess(
  23.            uint processAccess,
  24.            bool bInheritHandle,
  25.            int processId
  26.        );
  27.         [DllImport("kernel32.dll", SetLastError = true)]
  28.         public static extern bool CloseHandle(
  29.             IntPtr hObject
  30.         );
  31.         [Flags]
  32.         public enum ProcessAccessFlags : uint
  33.         {
  34.             All = 0x001F0FFF,
  35.             Terminate = 0x00000001,
  36.             CreateThread = 0x00000002,
  37.             VirtualMemoryOperation = 0x00000008,
  38.             VirtualMemoryRead = 0x00000010,
  39.             VirtualMemoryWrite = 0x00000020,
  40.             DuplicateHandle = 0x00000040,
  41.             CreateProcess = 0x000000080,
  42.             SetQuota = 0x00000100,
  43.             SetInformation = 0x00000200,
  44.             QueryInformation = 0x00000400,
  45.             QueryLimitedInformation = 0x00001000,
  46.             Synchronize = 0x00100000
  47.         }
  48.  
  49.         [DllImport("kernel32.dll", SetLastError = true)]
  50.         public static extern bool ReadProcessMemory(
  51.             IntPtr hProcess,
  52.             IntPtr lpBaseAddress,
  53.             [Out] byte[] lpBuffer,
  54.             int dwSize,
  55.             out IntPtr lpNumberOfBytesRead);
  56.  
  57.         // This helper static method is required because the 32-bit version of user32.dll does not contain this API
  58.         // (on any versions of Windows), so linking the method will fail at run-time. The bridge dispatches the request
  59.         // to the correct function (GetWindowLong in 32-bit mode and GetWindowLongPtr in 64-bit mode)
  60.  
  61.         // If that doesn't work, the following signature can be used alternatively.
  62.         [DllImport("user32.dll")]
  63.         public static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
  64.  
  65.         [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
  66.         private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
  67.  
  68.         [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
  69.         private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
  70.  
  71.         // This static method is required because Win32 does not support
  72.         // GetWindowLongPtr directly
  73.         public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
  74.         {
  75.             if (IntPtr.Size == 8)
  76.                 return GetWindowLongPtr64(hWnd, nIndex);
  77.             else
  78.                 return GetWindowLongPtr32(hWnd, nIndex);
  79.         }
  80.  
  81.         [DllImport("user32.dll")]
  82.         [return: MarshalAs(UnmanagedType.Bool)]
  83.         public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
  84.             uint uFlags);
  85.  
  86.         [DllImport("user32.dll")]
  87.         public static extern IntPtr GetForegroundWindow();
  88.   static int FindPattern(in byte[] cbMemory, string szPattern)
  89.         {
  90.             byte[] pattern = szPattern.Trim().Split(' ').Select(token => token == "?" ? (byte)0x0 : Convert.ToByte(token, 16)).ToArray();
  91.             char[] szMask = szPattern.Trim().Split(' ').Select(token => token == "?" ? '?' : 'x').ToArray();
  92.             int patternStart = Array.IndexOf(szMask, '?');
  93.             byte[] patternData = cbMemory;
  94.  
  95.             for (var offset = 0; offset < patternData.Length; offset++)
  96.             {
  97.                 if (szMask.Where((m, b) => m == 'x' && pattern[b] != patternData[b + offset]).Any())
  98.                     continue;
  99.  
  100.                 int address = BitConverter.ToInt32(patternData, offset + patternStart);
  101.                 return address + offset + patternStart + sizeof(int);
  102.             }
  103.             return -1;
  104.         }
  105.  
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             Console.WriteLine("Memory Scanner For Offsets!");
  110.          
  111.             IntPtr processHandle = IntPtr.Zero;
  112.  
  113.             Process[] process = Process.GetProcessesByName(ProcessName);
  114.             Process gameProcess = process.Length > 0 ? process[0] : null;
  115.  
  116.             if (gameProcess == null)
  117.             {
  118.                 Console.WriteLine("D2 Not Open");
  119.                 return;
  120.             }
  121.  
  122.             processHandle = OpenProcess((uint)ProcessAccessFlags.VirtualMemoryRead, false, gameProcess.Id);
  123.             IntPtr processAddress = gameProcess.MainModule.BaseAddress;
  124.             byte[] moduleMemory = new byte[gameProcess.MainModule.ModuleMemorySize];
  125.             ReadProcessMemory(processHandle, processAddress, moduleMemory, gameProcess.MainModule.ModuleMemorySize, out _);
  126.  
  127.             Console.WriteLine("BaseAdress: 0x" + gameProcess.MainModule.BaseAddress.ToString("X"));
  128.             Console.WriteLine("\n");
  129.             // Find
  130.             UnitTable = FindPattern(moduleMemory, "4C 8D 05 ? ? ? ? 48 63 03");
  131.             InGameMap = FindPattern(moduleMemory, "44 0F B6 35 ? ? ? ? 33 D2 41 B0 01 8D 4A 0A");
  132.      
  133.             // Found
  134.          
  135.             if (UnitTable != -1)
  136.                 Console.WriteLine("UnitTable: 0x" + UnitTable.ToString("X"));
  137.             if (InGameMap != -1)
  138.                 Console.WriteLine("InGameMap: 0x" + InGameMap.ToString("X"));
  139.          
  140.             // Not Found
  141.             if (UnitTable == -1)
  142.                 Console.WriteLine("Failed to find UnitTable");
  143.             if (InGameMap == -1)
  144.                 Console.WriteLine("Failed to find InGameMap");
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment