Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using VWeaponUtil.Utils;
- namespace VWeaponUtil.ConsoleTesting {
- internal class Program {
- public class MEM64 {
- [DllImport("kernel32.dll")]
- public static extern int WriteProcessMemory(IntPtr Handle, long Address, byte[] buffer, int Size, int BytesWritten = 0);
- [DllImport("kernel32.dll")]
- public static extern int ReadProcessMemory(IntPtr Handle, long Address, byte[] buffer, int Size, int BytesRead = 0);
- [DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
- public static extern unsafe int ReadProcessMemory_Unsafe(IntPtr hProcess, long address, byte* buffer, int bufferSize, int bytesRead = 0);
- public static unsafe long ToInt64(byte[] value, int startIndex) {
- fixed (byte* numPtr = &value[startIndex]) {
- return *(long*) numPtr;
- }
- }
- public static unsafe long GetPointerAddress(IntPtr hProcess, long pointer, int[] offsets = null) {
- // pointer should contain the BaseAddress of the module being targeted
- if (offsets == null || offsets.Length < 1) {
- return pointer;
- }
- byte* buf = stackalloc byte[8];
- ReadProcessMemory_Unsafe(hProcess, pointer, buf, 8);
- for (int i = 0, j = offsets.Length - 1; i < j; i++) {
- pointer = *(long*) buf + offsets[i];
- ReadProcessMemory_Unsafe(hProcess, pointer, buf, 8);
- }
- pointer = *(long*) buf + offsets[offsets.Length - 1];
- return pointer;
- }
- public static void WriteBytes(IntPtr hProcess, long Address, byte[] Bytes) {
- WriteProcessMemory(hProcess, Address, Bytes, Bytes.Length);
- }
- public static void WriteFloat(IntPtr hProcess, long Address, float Value) {
- WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), 4);
- }
- public static void WriteDouble(IntPtr hProcess, long Address, double Value) {
- WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), 8);
- }
- public static void WriteInteger(IntPtr hProcess, long Address, int Value, int size) {
- WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), size);
- }
- public static void WriteString(IntPtr hProcess, long Address, string String) {
- byte[] Buffer = new ASCIIEncoding().GetBytes(String);
- WriteProcessMemory(hProcess, Address, Buffer, Buffer.Length);
- }
- public static byte[] ReadBytes(IntPtr hProcess, long Address, int Length) {
- byte[] Buffer = new byte[Length];
- ReadProcessMemory(hProcess, Address, Buffer, Length);
- return Buffer;
- }
- public static float ReadFloat(IntPtr hProcess, long Address) {
- byte[] Buffer = new byte[4];
- ReadProcessMemory(hProcess, Address, Buffer, 4);
- return BitConverter.ToSingle(Buffer, 0);
- }
- public static double ReadDouble(IntPtr hProcess, long Address) {
- byte[] Buffer = new byte[8];
- ReadProcessMemory(hProcess, Address, Buffer, 8);
- return BitConverter.ToDouble(Buffer, 0);
- }
- public static int ReadInteger(IntPtr hProcess, long Address, int Length) {
- byte[] Buffer = new byte[Length];
- ReadProcessMemory(hProcess, Address, Buffer, Length);
- return BitConverter.ToInt32(Buffer, 0);
- }
- public static string ReadString(IntPtr hProcess, long Address, int size) {
- byte[] Buffer = new byte[size];
- ReadProcessMemory(hProcess, Address, Buffer, size);
- return new ASCIIEncoding().GetString(Buffer);
- }
- public static long ReadPointer(IntPtr hProcess, long Address) {
- byte[] Buffer = new byte[8];
- ReadProcessMemory(hProcess, Address, Buffer, Buffer.Length);
- return BitConverter.ToInt64(Buffer, 0);
- }
- }
- public readonly struct PointerOffset {
- public readonly long baseAddress;
- public readonly int[] offsets;
- public readonly int trailOffset;
- public PointerOffset(long baseAddress, int[] offsets, int trailingOffset = 0) {
- this.baseAddress = baseAddress;
- this.offsets = offsets;
- this.trailOffset = trailingOffset;
- }
- public PointerOffset(PointerOffset offset, int trailingOffset = 0) : this(offset.baseAddress, offset.offsets, trailingOffset) {
- }
- /// <summary>
- /// Gets the address of the underlying pointer. A value may be readable from the returned address
- /// </summary>
- /// <param name="hProcess">The target process</param>
- /// <param name="hProcessBaseAddress">The process' base address</param>
- /// <returns>An address in which this offset points to, relative to the given process and base address</returns>
- public long GetPointerAddress(IntPtr hProcess, long hProcessBaseAddress) {
- return MEM64.GetPointerAddress(hProcess, hProcessBaseAddress + this.baseAddress, this.offsets) - this.trailOffset;
- }
- }
- public static readonly PointerOffset AWT_POINTER_1 = new PointerOffset(0x2949870, new int[] {0xA40, 0x150});
- public static readonly PointerOffset DELUXO_CWEAPON_INFO_MISSILE = new PointerOffset(AWT_POINTER_1, 0x150);
- public static readonly PointerOffset AWT_POINTER_2 = new PointerOffset(0x2949850, new int[] {0x2D0, 0x978, 0x18, 0x150});
- static Program() {
- }
- static void Main(string[] args) {
- Console.WriteLine("Opening GTA5 process...");
- Process process = null;
- try {
- process = Process.GetProcessesByName("GTA5").FirstOrDefault();
- if (process == null) {
- Console.WriteLine("Could not find GTA5 proces");
- return;
- }
- IntPtr hProcess = process.Handle;
- ProcessModule module = process.Modules[0];
- long hProcessBaseAddress = module.BaseAddress.ToInt64();
- {
- long ptr = AWT_POINTER_1.GetPointerAddress(hProcess, hProcessBaseAddress);
- float value = MEM64.ReadFloat(hProcess, ptr);
- Console.WriteLine("Deluxo Alternate Wait Time value: " + value);
- }
- {
- long cWeaponInfoAddress = DELUXO_CWEAPON_INFO_MISSILE.GetPointerAddress(hProcess, hProcessBaseAddress);
- unsafe {
- byte[] x = MEM64.ReadBytes(hProcess, cWeaponInfoAddress, sizeof(CWeaponInfo));
- fixed (byte* ptr = x) {
- CWeaponInfo weaponInfo = *(CWeaponInfo*) ptr;
- Console.WriteLine(weaponInfo.ToString());
- }
- }
- }
- module.Dispose();
- }
- catch (Exception e) {
- Console.WriteLine("Exception doing shit: " + e.GetToString());
- }
- finally {
- try {
- Console.WriteLine("Disposing process...");
- process?.Dispose();
- }
- catch (Exception e) {
- Console.WriteLine("Failed to close process: " + e.GetToString());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement