Gill Bates (09-01-2017),hongbii (07-04-2018),Hydra (01-27-2017),Lucky8888 (09-09-2018),mjts140914 (09-29-2018),mosinu (02-17-2017),reaperscythe02 (04-16-2017),someone0815 (05-25-2017),Ussser23231 (01-22-2017)
Hello MPGH, Today I'm going to be uploading a basic memory class for C# that I created in my spare time.
Features:
WriteBytes
WriteByte
WriteInt
WriteString
WriteFloat
ReadBytes
ReadByte
ReadInt
ReadString
ReadString Advanced
ReadFloat
PatternScan
Source:
usage:Code:using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; class Memory { private IntPtr pHandel = IntPtr.Zero; private Process attachedProcess = null; private byte[] buffer; public bool Open_pHandel(string processName) { Process[] proc = Process.GetProcessesByName(processName); if (proc.Length == 0) return false; else if (proc.Length > 1) throw new Exception("More then one process found."); else { attachedProcess = proc[0]; pHandel = proc[0].Handle; return true; } } #region x86 #region Write public bool WriteBytes(int address, byte[] value, bool virtualProtect = false) { int countOfUsed = 0; bool result = false; uint oldProtection = 0; if (virtualProtect) Win32.x86.VirtualProtectEx(pHandel, address, (uint)value.Length, 0x40, out oldProtection); result = Win32.x86.WriteProcessMemory(pHandel, address, value, (uint)value.Length, out countOfUsed); if (virtualProtect) Win32.x86.VirtualProtectEx(pHandel, address, (uint)value.Length, oldProtection, out oldProtection); return result; } public bool WriteInt(int address, int value, bool virtualProtect = false) { buffer = BitConverter.GetBytes(value); return WriteBytes(address, buffer, virtualProtect); } public bool WriteByte(int address, byte value, bool virtualProtect = false) { buffer = new byte[] { value }; return WriteBytes(address, buffer, virtualProtect); } public bool WriteString(int address, string value, bool virtualProtect = false) { buffer = Encoding.ASCII.GetBytes(value);//No unicode support atm return WriteBytes(address, buffer, virtualProtect); } public bool WriteFloat(int address, float value, bool virtualProtect = false) { buffer = BitConverter.GetBytes(value); return WriteBytes(address, buffer, virtualProtect); } #endregion #region Read public byte[] ReadBytes(int address, int length) { byte[] readBytes = new byte[length]; int numBytesChanged = 0; Win32.x86.ReadProcessMemory(pHandel, address, readBytes, (uint)length, out numBytesChanged); return readBytes; } public int ReadInt(int address) { return BitConverter.ToInt32(ReadBytes(address, 4), 0); } public byte ReadByte(int address) { return ReadBytes(address, 1)[0]; } public string ReadString(int address, int length) {//No unicode support return Encoding.ASCII.GetString(ReadBytes(address, length)); } public string ReadStringAdvanced(int address, int maxStringLength = 1000) {//No unicode support string result = null; byte currentByte = 0; for (int i = 0; i < maxStringLength; i++) { currentByte = ReadByte(address + i); if (currentByte == 0x00) break; else result += (char)currentByte; } return result; } public float ReadFloat(int address) { return BitConverter.ToSingle(ReadBytes(address, 4), 0); } #endregion #region Scans public int PatternScan(string pattern) { string[] splitPattern = pattern.Split(' '); bool[] indexValid = new bool[splitPattern.Length]; byte[] indexValue = new byte[splitPattern.Length]; byte tempByte = (byte)0x00; for (int i = 0; i < splitPattern.Length; i++) { indexValid[i] = !(splitPattern[i] == "??" || splitPattern[i] == "?"); if (Byte.TryParse(splitPattern[i], out tempByte)) indexValue[i] = tempByte; else indexValid[i] = false; } int startOfMemory = attachedProcess.MainModule.BaseAddress.ToInt32(); int endOfMemory = attachedProcess.MainModule.ModuleMemorySize; for (int currentMemAddy = startOfMemory; currentMemAddy < endOfMemory; currentMemAddy++) { bool complete = false; for (int i = 0; i < splitPattern.Length; i++) { if (!indexValid[i]) continue; tempByte = ReadByte(currentMemAddy + i); if (tempByte != indexValue[i]) break; if (i == splitPattern.Length - 1) complete = true; if (complete) break; } if (complete) return currentMemAddy; } throw new Exception("Pattern not found!"); return 0; } #endregion #endregion private static class Win32 { public static class NativeMethods { #region IsWow64Process public static bool IsWow64Process(IntPtr handel) { bool isTarget64Bit = false; IsWow64Process(handel, out isTarget64Bit); return isTarget64Bit; } [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process); #endregion } public static class x64 { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool VirtualProtectEx(IntPtr hProcess, long lpAddress, UInt32 dwSize, uint flNewProtect, out uint lpflOldProtect); [DllImport("kernel32.dll")] public static extern bool ReadProcessMemory(IntPtr hProcess, long lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out int lpNumberOfBytesWritten); [DllImport("kernel32.dll")] public static extern bool WriteProcessMemory(IntPtr hProcess, long lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out int lpNumberOfBytesWritten); } public static class x86 { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool VirtualProtectEx(IntPtr hProcess, int lpAddress, UInt32 dwSize, uint flNewProtect, out uint lpflOldProtect); [DllImport("kernel32.dll")] public static extern bool ReadProcessMemory(IntPtr hProcess, int lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out int lpNumberOfBytesWritten); [DllImport("kernel32.dll")] public static extern bool WriteProcessMemory(IntPtr hProcess, int lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out int lpNumberOfBytesWritten); } } }
Midway typing this I noticed I left the gas on, on the stove and I got a massive headache xd whoopsCode:if(Open_pHandel("processName")) { int address = PatternScan("02 255 12 41 ?? 54 ? 12 ? 52 ?? 23"); WriteBytes(address, new byte[] {0x00, 0x00}); }
Features I will add later:
x64 support.
idk what else
Press thanks if this helps you!
Last edited by Hero; 02-03-2017 at 08:13 PM.
Click Here to visit the official MPGH wiki! Keep up with the latest news and information on games and MPGH! To check out pages dedicated to games, see the links below!
dd/mm/yyyy
Member - 31/01/2015
Premium - 12/09/2016
Call of Duty minion - 05/11/2016 - 05/11/2019
BattleOn minion - 28/02/2017 - 05/11/2019
Battlefield minion - 30/05/2017 - 05/11/2019
Other Semi-Popular First Person Shooter Hacks minion - 21/09/2017 - 17/09/2019
Publicist - 07/11/2017 - 02/08/2018
Cock Sucker - 01/12/2017 - Unknown
Minion+ - 06/03/2018 - 05/11/2019
Fortnite minion - 08/05/2018 - 05/11/2019
Head Publicist - 08/10/2018 - 10/01/2020
Developer Team - 26/10/2019 - 10/01/2020
Former Staff - 10/01/2020
Gill Bates (09-01-2017),hongbii (07-04-2018),Hydra (01-27-2017),Lucky8888 (09-09-2018),mjts140914 (09-29-2018),mosinu (02-17-2017),reaperscythe02 (04-16-2017),someone0815 (05-25-2017),Ussser23231 (01-22-2017)
Thanks James, greet work,helpfull for newbie
Silent (01-22-2017)
Silent (01-27-2017)
THANK YOU @JamesBond
[ • ] [ • ] [ • ] [ • ][ • ]
Editor from 06•14•2011 • 2014
Donator since 09•16•2011
Minion from 10•10•2011 • 01•06•2011
Minion+ from 01•06•2012 • 08•08•2012
Moderator from 08•08•2012 • 10•06•2012
Global Moderator from 10•06•2012 • 12•05•2017
Staff Administrator from 12•05•2017 • 05•01•2019
Trusted Member since 07•13•2019
Global Moderator since 09•11•2020
Silent (02-03-2017)
Last edited by Silent; 02-04-2017 at 10:29 PM.
Click Here to visit the official MPGH wiki! Keep up with the latest news and information on games and MPGH! To check out pages dedicated to games, see the links below!
dd/mm/yyyy
Member - 31/01/2015
Premium - 12/09/2016
Call of Duty minion - 05/11/2016 - 05/11/2019
BattleOn minion - 28/02/2017 - 05/11/2019
Battlefield minion - 30/05/2017 - 05/11/2019
Other Semi-Popular First Person Shooter Hacks minion - 21/09/2017 - 17/09/2019
Publicist - 07/11/2017 - 02/08/2018
Cock Sucker - 01/12/2017 - Unknown
Minion+ - 06/03/2018 - 05/11/2019
Fortnite minion - 08/05/2018 - 05/11/2019
Head Publicist - 08/10/2018 - 10/01/2020
Developer Team - 26/10/2019 - 10/01/2020
Former Staff - 10/01/2020
Recigy (02-05-2017)
Silent (02-05-2017)
Why did you decide not to include unicode support? Literally the same thing as your ascii code...
Code:public string ReadStringUnicode(int address, int length) {//Now with unicode support. return Encoding.Unicode.GetString(ReadBytes(address, length)); }
Silent (03-06-2017)
Click Here to visit the official MPGH wiki! Keep up with the latest news and information on games and MPGH! To check out pages dedicated to games, see the links below!
dd/mm/yyyy
Member - 31/01/2015
Premium - 12/09/2016
Call of Duty minion - 05/11/2016 - 05/11/2019
BattleOn minion - 28/02/2017 - 05/11/2019
Battlefield minion - 30/05/2017 - 05/11/2019
Other Semi-Popular First Person Shooter Hacks minion - 21/09/2017 - 17/09/2019
Publicist - 07/11/2017 - 02/08/2018
Cock Sucker - 01/12/2017 - Unknown
Minion+ - 06/03/2018 - 05/11/2019
Fortnite minion - 08/05/2018 - 05/11/2019
Head Publicist - 08/10/2018 - 10/01/2020
Developer Team - 26/10/2019 - 10/01/2020
Former Staff - 10/01/2020
[MPGH]Mayion (03-13-2017)
Some general improvements I'd recommend for this class are:
- Use the sealed (and optionally internal) keyword unless you explicitly want people to overwrite your class (which doesn't make sense in this case imo)
- Make the class open the handle on creation of the instance.
- Use Win32 calls to get the handle and pass the VMRead and VMWrite access rights.
As Microsoft states- Implement IDisposable and a finalizer and close the handle when the class gets finalized or disposed (in case you're opening a native handle yourself)Only processes started through a call to M:System.Diagnostics.Process.Start set the P:System.Diagnostics.Process.Handle property of the corresponding T:System.Diagnostics.Process instances.
- Implement support for Encodings (best would be to passed as a parameter in the constructor and then used for read and write operations)
- Fix your code-style and naming to obey the general rules that apply for C#
- Don't use constant numbers for your calls to VirtualProtectEx, create an enum (using the Flags attribute) for that
- Throw a Win32Exception depending on Marshal.GetLastWin32Error
- Do not throw exceptions of type Exception (that's considered bad practice since they're a pain in the ass to catch)
- Use method overloading rather than optional parameters
I hope you won't consider this as hate or bashing in any way. I just want you to improve your coding style (regarding C# and .NET) to things that might be considered best practice
I'd also recommend you to get your hands on an educational copy of ReSharper or use the Visual Studio Code Analysis (I don't know whether this is part of the community edition) as they provide great features to analyze your code and recommend (or enforce) changes
Feel free to contact me for resources or questions regarding the suggestions I made. I'm always glad to help people who are interested in learning
Last edited by Biesi; 03-08-2017 at 01:36 PM.
Silent (03-08-2017)