Advertisement
Guest User

AOBScan

a guest
Feb 2nd, 2016
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace HackMemory
  10. {
  11.     public class AOBScan
  12.     {
  13.         //ReadProcessMemory
  14.         [DllImport("kernel32.dll")]
  15.         protected static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, int lpNumberOfBytesRead);
  16.  
  17.         //VirtualQueryEx
  18.         [DllImport("kernel32.dll")]
  19.         protected static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
  20.  
  21.         [StructLayout(LayoutKind.Sequential)]
  22.         protected struct MEMORY_BASIC_INFORMATION
  23.         {
  24.             public IntPtr BaseAddress;
  25.             public IntPtr AllocationBase;
  26.             public uint AllocationProtect;
  27.             public uint RegionSize;
  28.             public uint State;
  29.             public uint Protect;
  30.             public uint Type;
  31.         }
  32.  
  33.         protected List<MEMORY_BASIC_INFORMATION> MemoryRegion { get; set; }
  34.  
  35.         protected void MemInfo(IntPtr pHandle)
  36.         {
  37.             IntPtr Addy = new IntPtr();
  38.             while (true)
  39.             {
  40.                 MEMORY_BASIC_INFORMATION MemInfo = new MEMORY_BASIC_INFORMATION();
  41.                 int MemDump = VirtualQueryEx(pHandle, Addy, out  MemInfo, Marshal.SizeOf(MemInfo));
  42.                 if (MemDump == 0) break;
  43.                 if ((MemInfo.State & 0x1000) != 0 && (MemInfo.Protect & 0x100) == 0)
  44.                     MemoryRegion.Add(MemInfo);
  45.                 Addy = new IntPtr(MemInfo.BaseAddress.ToInt32() + (int)MemInfo.RegionSize);
  46.             }
  47.         }
  48.         protected IntPtr Scan(byte[] sIn, byte[] sFor)
  49.         {
  50.             int[] sBytes = new int[256]; int Pool = 0;
  51.             int End = sFor.Length - 1;
  52.             for (int i = 0; i < 256; i++)
  53.                 sBytes[i] = sFor.Length;
  54.             for (int i = 0; i < End; i++)
  55.                 sBytes[sFor[i]] = End - i;
  56.             while (Pool <= sIn.Length - sFor.Length)
  57.             {
  58.                 for (int i = End; sIn[Pool + i] == sFor[i]; i--)
  59.                     if (i == 0) return new IntPtr(Pool);
  60.                 Pool += sBytes[sIn[Pool + End]];
  61.             }
  62.             return IntPtr.Zero;
  63.         }
  64.         public IntPtr AobScan(byte[] Pattern)
  65.         {
  66.             MemoryRegion = new List<MEMORY_BASIC_INFORMATION>();
  67.             MemInfo(ProcessСonnection.handle);//сюда записываем handle процесса
  68.             for (int i = 0; i < MemoryRegion.Count; i++)
  69.             {
  70.                 byte[] buff = new byte[MemoryRegion[i].RegionSize];
  71.                 ReadProcessMemory(ProcessСonnection.handle, MemoryRegion[i].BaseAddress, buff, MemoryRegion[i].RegionSize, 0);//сюда записываем тоже handle
  72.  
  73.                 IntPtr Result = Scan(buff, Pattern);
  74.                 if (Result != IntPtr.Zero)
  75.                     return new IntPtr(MemoryRegion[i].BaseAddress.ToInt32() + Result.ToInt32());
  76.             }
  77.             return IntPtr.Zero;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement