Advertisement
Guest User

Untitled

a guest
Oct 12th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace GetVehicleName
  8. {
  9.     class Program
  10.     {
  11.         struct OFFSETS
  12.         {
  13.             public const Int32 ClientGameContext = 0x02380B58;
  14.             public const Int32 ClientLevel = 0x10;
  15.             public const Int32 GameWorld = 0xC0;
  16.             public const Int32 EntityCollection = 0x820;
  17.             public const Int32 EntityWorld = 0x820;
  18.             public const Int32 EntityCollectionSegment = 0x0;
  19.             public const Int32 Entity = 0x0;
  20.             public const Int32 ClientVehicleEntity = 0xF8;
  21.             public const Int32 PhysicsEntity = 0x98;
  22.             public const Int32 PhysicsEntityData = 0x20;
  23.             public const Int32 HavokAssets = 0x0;
  24.             public const Int32 m_name = 0x8;
  25.         }
  26.  
  27.         static IntPtr processHandle;
  28.  
  29.         [DllImport("kernel32.dll")]
  30.         static extern IntPtr OpenProcess(Int32 dwDesiredAccess, bool bInheritHandle, Int32 dwProcessId);
  31.  
  32.         [DllImport("kernel32.dll")]
  33.         static extern bool CloseHandle(IntPtr handle);
  34.  
  35.         [DllImport("kernel32.dll")]
  36.         static extern bool ReadProcessMemory(IntPtr hProcess, Int32 lpBaseAddress, byte[] lpBuffer, Int32 dwSize, out Int32 lpNumberOfBytesRead);
  37.  
  38.         static bool IsValid(Int32 address)
  39.         {
  40.             return (address >= -0x7FFFFFFF && address < 0x7FFFFFFF);
  41.         }
  42.  
  43.         static void Main(string[] args)
  44.         {
  45.             Console.WriteLine("Waiting for BF3 process and module (x86)....");
  46.  
  47.             Process process;
  48.             ProcessModule processModule;
  49.  
  50.             Int32 pClientGameContext;
  51.             while (true)
  52.             {
  53.                 if (GetProcessByName("bf3", out process))
  54.                 {
  55.                     if (GetModuleByProcess(process, "bf3.exe", out processModule))
  56.                     {
  57.                         processHandle = OpenProcess(0x0010, false, process.Id);
  58.  
  59.                         pClientGameContext = ReadInt32(OFFSETS.ClientGameContext);
  60.                         if (IsValid(pClientGameContext))
  61.                         {
  62.                             break;
  63.                         }
  64.  
  65.                         CloseHandle(processHandle);
  66.                     }
  67.                 }
  68.  
  69.                 Thread.Sleep(100);
  70.             }
  71.  
  72.             Thread.Sleep(100);
  73.  
  74.             Console.WriteLine("BF3 process and module found");
  75.             Console.WriteLine("Ready and GO!");
  76.  
  77.             while (true)
  78.             {
  79.                 Int32 pClientLevel = ReadInt32(pClientGameContext + OFFSETS.ClientLevel);
  80.                 if (!IsValid(pClientLevel))
  81.                 {
  82.                     continue;
  83.                 }
  84.  
  85.                 Int32 pGameWorld = ReadInt32(pClientLevel + OFFSETS.GameWorld);
  86.                 if (!IsValid(pGameWorld))
  87.                 {
  88.                     continue;
  89.                 }
  90.  
  91.                 Int32 pEntityCollection = ReadInt32(pGameWorld + OFFSETS.EntityCollection);
  92.                 if (!IsValid(pEntityCollection))
  93.                 {
  94.                     continue;
  95.                 }
  96.  
  97.                 Int32 pEntityWorld = ReadInt32(pEntityCollection + OFFSETS.EntityWorld);
  98.                 if (!IsValid(pEntityWorld))
  99.                 {
  100.                     continue;
  101.                 }
  102.  
  103.                 Int32 size = ReadInt32(pEntityWorld + 0x1C);
  104.                 for (int i = 0; i < size; i++)
  105.                 {
  106.                     Int32 pEntityCollectionSegment = ReadInt32(pEntityWorld + OFFSETS.EntityCollectionSegment);
  107.                     if (!IsValid(pEntityCollectionSegment))
  108.                     {
  109.                         continue;
  110.                     }
  111.  
  112.                     Int32 pEntity = ReadInt32(pEntityCollectionSegment + i * sizeof(Int32));
  113.                     if (!IsValid(pEntity))
  114.                     {
  115.                         continue;
  116.                     }
  117.  
  118.                     Int32 pClientVehicleEntity = ReadInt32(pEntity + OFFSETS.ClientVehicleEntity);
  119.                     if (!IsValid(pClientVehicleEntity))
  120.                     {
  121.                         continue;
  122.                     }
  123.  
  124.                     Int32 pPhysicsEntity = ReadInt32(pClientVehicleEntity + OFFSETS.PhysicsEntity);
  125.                     if (!IsValid(pPhysicsEntity))
  126.                     {
  127.                         continue;
  128.                     }
  129.  
  130.                     Int32 pPhysicsEntityData = ReadInt32(pPhysicsEntity + OFFSETS.PhysicsEntityData);
  131.                     if (!IsValid(pPhysicsEntityData))
  132.                     {
  133.                         continue;
  134.                     }
  135.  
  136.                     Int32 pHavokAssets = ReadInt32(pPhysicsEntityData + OFFSETS.HavokAssets);
  137.                     if (!IsValid(pHavokAssets))
  138.                     {
  139.                         continue;
  140.                     }
  141.  
  142.                     Int32 pName = ReadInt32(pHavokAssets + OFFSETS.m_name);
  143.                     if (!IsValid(pName))
  144.                     {
  145.                         continue;
  146.                     }
  147.  
  148.                     Console.WriteLine(ReadString(pName, 255));
  149.                 }
  150.  
  151.                 Console.ReadLine();
  152.             }
  153.         }
  154.  
  155.         static bool GetProcessByName(string processName, out Process process)
  156.         {
  157.             Process[] processList = Process.GetProcessesByName(processName);
  158.             if (processList.Length > 0)
  159.             {
  160.                 process = processList[0];
  161.                 return true;
  162.             }
  163.  
  164.             process = null;
  165.             return false;
  166.         }
  167.  
  168.         static bool GetModuleByProcess(Process process, string moduleName, out ProcessModule processModule)
  169.         {
  170.             foreach (ProcessModule module in process.Modules)
  171.             {
  172.                 if (module.ModuleName == moduleName)
  173.                 {
  174.                     processModule = module;
  175.                     return true;
  176.                 }
  177.             }
  178.  
  179.             processModule = null;
  180.             return false;
  181.         }
  182.  
  183.         static Int32 ReadInt32(Int32 _lpBaseAddress)
  184.         {
  185.             byte[] buffer = new byte[sizeof(Int32)];
  186.  
  187.             Int32 bytesRead = 0;
  188.             ReadProcessMemory(processHandle, _lpBaseAddress, buffer, buffer.Length, out bytesRead);
  189.  
  190.             return BitConverter.ToInt32(buffer, 0);
  191.         }
  192.  
  193.         static string ReadString(Int32 _lpBaseAddress, Int32 _Size)
  194.         {
  195.             byte[] buffer = new byte[_Size];
  196.             Int32 bytesRead = 0;
  197.  
  198.             ReadProcessMemory(processHandle, _lpBaseAddress, buffer, _Size, out bytesRead);
  199.  
  200.             for (int i = 0; i < buffer.Length; i++)
  201.             {
  202.                 if (buffer[i] == 0)
  203.                 {
  204.                     byte[] _buffer = new byte[i];
  205.                     Buffer.BlockCopy(buffer, 0, _buffer, 0, i);
  206.                     return Encoding.ASCII.GetString(_buffer);
  207.                 }
  208.             }
  209.             return Encoding.ASCII.GetString(buffer);
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement