❓ Memory editing

Posts 1–9 of 9 · Page 1 of 1
❓ Memory editing
Hi all!

I want to learn to edit memory with c#(Game trainer) and i was wondering what i need to know in the c# language to do that.

Do i need a basic understanding of c# or is it okay that i learn it through tutorials like these https://www.youtube.com/watch?v=v9r-MpFMkGA
Do you guys have any good tutorials for learning c# and learning memory editing?
c# doesn't support memory editing but you can import c++ functions
https://msdn.microsoft.com/pl-pl/lib...(v=vs.85).aspx
https://msdn.microsoft.com/pl-pl/lib...(v=vs.85).aspx
https://msdn.microsoft.com/pl-pl/lib...(v=vs.85).aspx

Code:
        [DllImport("kernel32")]
        public static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32")]
        public static extern bool WriteProcessMemory(int hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, out int lpNumberOfBytesWritten);

        [DllImport("kernel32")]
        public static extern bool ReadProcessMemory(int hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, out int lpNumberOfBytesRead);
PS.
If you don't want to create every function manually you should check MemorySharp library out.
It's one of the best memory editing libraries for c#
Quote Originally Posted by Zaczero View Post
c# doesn't support memory editing but you can import c++ functions
OpenProcess, WriteProcessMemory and ReadProcessMemory are not C++ implementations, they are components of Windows API.
Quote Originally Posted by javalover View Post
OpenProcess, WriteProcessMemory and ReadProcessMemory are not C++ implementations, they are components of Windows API.
Yeah but they are written in native c++
Quote Originally Posted by Zaczero View Post


Yeah but they are written in native c++
Source? Last time I checked, Windows isn't open source.
Quote Originally Posted by Silent View Post


Source? Last time I checked, Windows isn't open source.
from wikipedia:
... provides the Windows API (...) which is implemented in C++ ...
c# doesn't support memory editing but you can import c++ functions
Quote Originally Posted by Zaczero View Post


Yeah but they are written in native c++
Most windows API functions are written in C too. It's not much referenced regarding on how they are written (Windows is closed-source). Either they are written in C or C++, you don't "import c++ functions", but "windows api functions". Those functions are not part of the C++ Standard Library -- they are part of the Windows API.

- - - Updated - - -

Quote Originally Posted by Zaczero View Post


from wikipedia:
... provides the Windows API (...) which is implemented in C++ ...
You're interpreting it wrongly: https://en.wikipedia.org/wiki/Windows_APIⓘ
For instance, Windows 8 provides the Windows API and the WinRT API, which is implemented in C++
"which is implemented in C++" is referring to "WinRT API".
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace System.Memory.Managment
{

    public class MemoryBase
    {
        [DllImport("kernel32.dll")]
        protected static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
        [DllImport("kernel32.dll")]
        protected static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);

        protected byte[] ReadInternal(IntPtr handle,IntPtr address,int length)
        {
            try
            {
                byte[] buf = new byte[length];
                IntPtr numOfBytes = IntPtr.Zero;
                ReadProcessMemory(handle, address, buf, (uint)buf.Length, out numOfBytes);
                return buf;
            }
            catch(Exception e)
            {
                throw new Exception("Exception in reading the memory: " + e.Message);
            }
        }

        protected void WriteInternal(IntPtr handle,IntPtr address,byte[] value)
        {
            try
            {
                IntPtr numOfBytes = IntPtr.Zero;
                WriteProcessMemory(handle, address, value, (uint)value.Length, out numOfBytes);
            }
            catch (Exception e)
            {
                throw new Exception("Exception in writing the memory: " + e.Message);
            }
        }
    }
        
    public abstract class HandlerBase<T> : MemoryBase
    {
        public abstract void Write(Pointer<T> pointer,T value);
        public abstract T Read(Pointer<T> pointer);
        
    }

    public class Pointer<T> : MemoryBase
    {
        public static Dictionary<Type, object> Handler = new Dictionary<Type, object>();

        public static IntPtr ProcessHandle;

        static Pointer()
        {
            Handler.Add(typeof(string), new StringHandler());
            Handler.Add(typeof(int), new IntHandler());
            Handler.Add(typeof(float), new FloatHandler());
            Handler.Add(typeof(double), new DoubleHandler());
            Handler.Add(typeof(long), new LongHandler());
            Handler.Add(typeof(byte[]), new ByteArrayHandler());
            Handler.Add(typeof(char), new CharHandler());
        }

        public Pointer(ulong _address)
        {
            Address = (IntPtr)_address;
        }
        public Pointer(ulong _address,int _size = 4)
        {
            Address = (IntPtr)_address;
            Size = _size;
        }

        public Pointer(IntPtr _address,int _size = 4)
        {
            Address = _address;
            Size = _size;
        }

        public IntPtr Address;

        public int Size;


        public void SetValue(T value)
        {
            ((HandlerBase<T>)Handler[typeof(T)]).Write(this,value);
        }

        public T ReadValue()
        {
            return ((HandlerBase<T>)Handler[typeof(T)]).Read(this);
        }

        public void SetNOP(int length)
        {
            byte[] buffer = new byte[length];
            for(int i = 0;i<length;i++)
            {
                buffer[i] = 0x90;
            }
            WriteInternal(Pointer<object>.ProcessHandle, Address, buffer);
        }

        public static Pointer<T> operator + (Pointer<T> p1,Pointer<T> p2) => 
            new Pointer<T>((ulong)p1.Address + (ulong)p2.Address);

        public static Pointer<T> operator -(Pointer<T> p1, Pointer<T> p2) =>
            new Pointer<T>((ulong)p1.Address - (ulong)p2.Address);

        public static Pointer<T> operator *(Pointer<T> p1, Pointer<T> p2) =>
            new Pointer<T>((ulong)p1.Address * (ulong)p2.Address);

        public static Pointer<T> operator /(Pointer<T> p1, Pointer<T> p2) =>
            new Pointer<T>((ulong)p1.Address / (ulong)p2.Address);

    }

    class StringHandler : HandlerBase<string>
    {
        public override string Read(Pointer<string> pointer)
        {
            return new ASCIIEncoding().GetString(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size));
        }

        public override void Write(Pointer<string> pointer,string value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, new ASCIIEncoding().GetBytes(value));
        }
    }

    class IntHandler : HandlerBase<int>
    {
        public override int Read(Pointer<int> pointer)
        {
            return BitConverter.ToInt32(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size),0);
        }

        public override void Write(Pointer<int> pointer, int value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, BitConverter.GetBytes(value));
        }
    }

    class FloatHandler : HandlerBase<float>
    {
        public override float Read(Pointer<float> pointer)
        {
            return BitConverter.ToSingle(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size), 0);
        }

        public override void Write(Pointer<float> pointer, float value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, BitConverter.GetBytes(value));
        }
    }

    class DoubleHandler : HandlerBase<double>
    {
        public override double Read(Pointer<double> pointer)
        {
            return BitConverter.ToDouble(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size), 0);
        }

        public override void Write(Pointer<double> pointer, double value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, BitConverter.GetBytes(value));
        }
    }

    class LongHandler : HandlerBase<long>
    {
        public override long Read(Pointer<long> pointer)
        {
            return BitConverter.ToInt64(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size), 0);
        }

        public override void Write(Pointer<long> pointer, long value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, BitConverter.GetBytes(value));
        }
    }

    class ByteArrayHandler : HandlerBase<byte[]>
    {
        public override byte[] Read(Pointer<byte[]> pointer)
        {
            return ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size);
        }

        public override void Write(Pointer<byte[]> pointer, byte[] value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, value);
        }
    }

    class CharHandler : HandlerBase<char>
    {
        public override char Read(Pointer<char> pointer)
        {
            return BitConverter.ToChar(ReadInternal(Pointer<object>.ProcessHandle, pointer.Address, pointer.Size), 0);
        }

        public override void Write(Pointer<char> pointer, char value)
        {
            WriteInternal(Pointer<object>.ProcessHandle, pointer.Address, BitConverter.GetBytes(value));
        }
    }

    public class HackProcess
    {
        public static IntPtr GetProcessHandle(string processname)
        {
            try
            {
                Process[] ProcList = Process.GetProcessesByName(processname);
                if (ProcList.Count() == 0)
                    throw new Exception("No process found");
                return ProcList.First().Handle;
            }
            catch (Exception e)
            {
                throw new Exception("Exception in getting the process handle: " + e.Message);
            }
        }
    }
}

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Memory.Managment;
using System.Diagnostics;

namespace Memory_Tester
{
    ///Example of using the abstract base to add a new datatype 
    class Vector //Our custom type
    {
        public Vector(float _x, float _y, float _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }

        public float x;
        public float y;
        public float z;
    }

    class VectorHandler : HandlerBase<Vector> //Our custom handler
    {
        public override Vector Read(Pointer<Vector> pointer)
        {
            return new Vector(new Pointer<float>(pointer.Address).ReadValue(),new Pointer<float>(pointer.Address + 4).ReadValue(),new Pointer<float>(pointer.Address + 8).ReadValue());
        }

        public override void Write(Pointer<Vector> pointer, Vector value)
        {
            new Pointer<float>(pointer.Address).SetValue(value.x);
            new Pointer<float>(pointer.Address + 4).SetValue(value.y);
            new Pointer<float>(pointer.Address + 8).SetValue(value.z);
        }
    }

    class Program
    {   
        static void Main(string[] args)
        {
            //Example of the usage:
            Pointer<object>.ProcessHandle = HackProcess.GetProcessHandle("whatever"); //Getting the process handle, it is static to the class, just using object because of lulZ
            Pointer<int> p1 = new Pointer<int>(0xDEADBEEF); //Pointer object to 0xDEADBEEF with the type int to 0xDEADBEEF
            Pointer<string> p2 = new Pointer<string>(0xDEADBEAF, 16); //Pointer object of the type string to 0xDEADBEAF with the size of 16 chars
            Pointer<object> p3 = new Pointer<object>(0xFFFFFFFF); //Pointer object with they of object , so basically for everything
            p1.SetValue(100); //Writes the certain value to the Pointer
            p2.ReadValue(); //Reads the certain value of the pointer in the size of 16 chars
            p3.SetNOP(16); //Sets 0x90 aka no executable code to the adress range -> size

            Pointer<object>.Handler.Add(typeof(Vector), new VectorHandler()); //Adding our handler for our custom type to the dictionary
            Pointer<Vector> p4 = new Pointer<Vector>(0xDEADBAAF); //Making an instance of the pointer object with our custom type
            p4.ReadValue(); 
            p4.SetValue(new Vector(0, 0, 0)); 
            Console.ReadKey();
        }
    }
}
If i have understod this right, everything that i will be using in C# is written in c++? So is it not easier for me to learn c++ instead? or should i go with c#? I just want to learn about memory editing and create my own trainers, aim bots and so on for games.
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us