Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Security.Cryptography;
- using System.Numerics;
- namespace MemoryScanner
- {
- class Program
- {
- private static string ProcessName = Encoding.UTF8.GetString(new byte[] { 68, 50, 82 });
- public static int UnitTable = -1;
- public static int InGameMap = -1;
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr OpenProcess(
- uint processAccess,
- bool bInheritHandle,
- int processId
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool CloseHandle(
- IntPtr hObject
- );
- [Flags]
- public enum ProcessAccessFlags : uint
- {
- All = 0x001F0FFF,
- Terminate = 0x00000001,
- CreateThread = 0x00000002,
- VirtualMemoryOperation = 0x00000008,
- VirtualMemoryRead = 0x00000010,
- VirtualMemoryWrite = 0x00000020,
- DuplicateHandle = 0x00000040,
- CreateProcess = 0x000000080,
- SetQuota = 0x00000100,
- SetInformation = 0x00000200,
- QueryInformation = 0x00000400,
- QueryLimitedInformation = 0x00001000,
- Synchronize = 0x00100000
- }
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool ReadProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- [Out] byte[] lpBuffer,
- int dwSize,
- out IntPtr lpNumberOfBytesRead);
- // This helper static method is required because the 32-bit version of user32.dll does not contain this API
- // (on any versions of Windows), so linking the method will fail at run-time. The bridge dispatches the request
- // to the correct function (GetWindowLong in 32-bit mode and GetWindowLongPtr in 64-bit mode)
- // If that doesn't work, the following signature can be used alternatively.
- [DllImport("user32.dll")]
- public static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
- [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
- private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
- [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
- private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
- // This static method is required because Win32 does not support
- // GetWindowLongPtr directly
- public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
- {
- if (IntPtr.Size == 8)
- return GetWindowLongPtr64(hWnd, nIndex);
- else
- return GetWindowLongPtr32(hWnd, nIndex);
- }
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
- uint uFlags);
- [DllImport("user32.dll")]
- public static extern IntPtr GetForegroundWindow();
- static int FindPattern(in byte[] cbMemory, string szPattern)
- {
- byte[] pattern = szPattern.Trim().Split(' ').Select(token => token == "?" ? (byte)0x0 : Convert.ToByte(token, 16)).ToArray();
- char[] szMask = szPattern.Trim().Split(' ').Select(token => token == "?" ? '?' : 'x').ToArray();
- int patternStart = Array.IndexOf(szMask, '?');
- byte[] patternData = cbMemory;
- for (var offset = 0; offset < patternData.Length; offset++)
- {
- if (szMask.Where((m, b) => m == 'x' && pattern[b] != patternData[b + offset]).Any())
- continue;
- int address = BitConverter.ToInt32(patternData, offset + patternStart);
- return address + offset + patternStart + sizeof(int);
- }
- return -1;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Memory Scanner For Offsets!");
- IntPtr processHandle = IntPtr.Zero;
- Process[] process = Process.GetProcessesByName(ProcessName);
- Process gameProcess = process.Length > 0 ? process[0] : null;
- if (gameProcess == null)
- {
- Console.WriteLine("D2 Not Open");
- return;
- }
- processHandle = OpenProcess((uint)ProcessAccessFlags.VirtualMemoryRead, false, gameProcess.Id);
- IntPtr processAddress = gameProcess.MainModule.BaseAddress;
- byte[] moduleMemory = new byte[gameProcess.MainModule.ModuleMemorySize];
- ReadProcessMemory(processHandle, processAddress, moduleMemory, gameProcess.MainModule.ModuleMemorySize, out _);
- Console.WriteLine("BaseAdress: 0x" + gameProcess.MainModule.BaseAddress.ToString("X"));
- Console.WriteLine("\n");
- // Find
- UnitTable = FindPattern(moduleMemory, "4C 8D 05 ? ? ? ? 48 63 03");
- InGameMap = FindPattern(moduleMemory, "44 0F B6 35 ? ? ? ? 33 D2 41 B0 01 8D 4A 0A");
- // Found
- if (UnitTable != -1)
- Console.WriteLine("UnitTable: 0x" + UnitTable.ToString("X"));
- if (InGameMap != -1)
- Console.WriteLine("InGameMap: 0x" + InGameMap.ToString("X"));
- // Not Found
- if (UnitTable == -1)
- Console.WriteLine("Failed to find UnitTable");
- if (InGameMap == -1)
- Console.WriteLine("Failed to find InGameMap");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment