forked from 6St6Jimmy6/PhysicsDataDebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryHelper.cs
More file actions
213 lines (184 loc) · 6.77 KB
/
Copy pathMemoryHelper.cs
File metadata and controls
213 lines (184 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Memory.Utils;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
//Taken from https://pastebin.com/0nCpkrit by NATANM. Check his github https://github.com/N4T4NM even though he doesn't have this anymore in there.
//Usage tutorial https://www.youtube.com/watch?v=aXKEWa0_YJg but not used for malicious intents in this program.
namespace Memory.Win32
{
class MemoryHelper32
{
readonly Process process;
public MemoryHelper32(Process TargetProcess)
{
process = TargetProcess;
}
public uint GetBaseAddress(uint StartingAddress)
{
return (uint)process.MainModule.BaseAddress + StartingAddress;
}
public byte[] ReadMemoryBytes(uint MemoryAddress, uint Bytes)
{
byte[] data = new byte[Bytes];
ReadProcessMemory(process.Handle, MemoryAddress, data, data.Length, IntPtr.Zero);
return data;
}
public T ReadMemory<T>(uint MemoryAddress)
{
byte[] data = ReadMemoryBytes(MemoryAddress, (uint)Marshal.SizeOf(typeof(T)));
T t;
GCHandle PinnedStruct = GCHandle.Alloc(data, GCHandleType.Pinned);
try { t = (T)Marshal.PtrToStructure(PinnedStruct.AddrOfPinnedObject(), typeof(T)); }
catch (Exception ex) { throw ex; }
finally { PinnedStruct.Free(); }
return t;
}
public bool WriteMemory<T>(uint MemoryAddress, T Value)
{
IntPtr bw = IntPtr.Zero;
int sz = ObjectType.GetSize<T>();
byte[] data = ObjectType.GetBytes<T>(Value);
bool result = WriteProcessMemory(process.Handle, MemoryAddress, data, sz, out bw);
return result && bw != IntPtr.Zero;
}
public void Close()
{
CloseHandle(process.Handle);
}
#region PInvoke
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadProcessMemory(
IntPtr hProcess,
uint lpBaseAddress,
byte[] lpBuffer,
int nSize,
IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(
IntPtr hProcess,
uint lpBaseAddress,
byte[] lpBuffer,
int nSize,
out IntPtr lpNumberOfBytesWritten
);
[DllImport("kernel32.dll")]
private static extern Int32 CloseHandle(IntPtr hProcess);
#endregion
}
}
namespace Memory.Win64
{
class MemoryHelper64
{
readonly Process process;
public MemoryHelper64(Process TargetProcess)
{
process = TargetProcess;
}
public ulong GetBaseAddress(ulong StartingAddress)
{
return (ulong)process.MainModule.BaseAddress.ToInt64() + StartingAddress;
}
public byte[] ReadMemoryBytes(ulong MemoryAddress, int Bytes)
{
byte[] data = new byte[Bytes];
ReadProcessMemory(process.Handle, MemoryAddress, data, data.Length, IntPtr.Zero);
return data;
}
public T ReadMemory<T>(ulong MemoryAddress)
{
byte[] data = ReadMemoryBytes(MemoryAddress, Marshal.SizeOf(typeof(T)));
T t;
GCHandle PinnedStruct = GCHandle.Alloc(data, GCHandleType.Pinned);
try { t = (T)Marshal.PtrToStructure(PinnedStruct.AddrOfPinnedObject(), typeof(T)); }
catch (Exception ex) { throw ex; }
finally { PinnedStruct.Free(); }
return t;
}
public bool WriteMemory<T>(ulong MemoryAddress, T Value)
{
IntPtr bw = IntPtr.Zero;
int sz = ObjectType.GetSize<T>();
byte[] data = ObjectType.GetBytes<T>(Value);
bool result = WriteProcessMemory(process.Handle, MemoryAddress, data, sz, out bw);
return result && bw != IntPtr.Zero;
}
public void Close()
{
CloseHandle(process.Handle);
}
#region PInvoke
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadProcessMemory(
IntPtr hProcess,
ulong lpBaseAddress,
byte[] lpBuffer,
int nSize,
IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(
IntPtr hProcess,
ulong lpBaseAddress,
byte[] lpBuffer,
int nSize,
out IntPtr lpNumberOfBytesWritten
);
[DllImport("kernel32.dll")]
private static extern Int32 CloseHandle(IntPtr hProcess);
#endregion
}
}
namespace Memory.Utils
{
static class MemoryUtils
{
public static uint OffsetCalculator(Win32.MemoryHelper32 TargetMemory, uint BaseAddress, int[] Offsets)
{
var address = BaseAddress;
foreach (uint offset in Offsets)
{
address = TargetMemory.ReadMemory<uint>(address) + offset;
}
return address;
}
public static ulong OffsetCalculator(Win64.MemoryHelper64 TargetMemory, ulong BaseAddress, int[] Offsets)
{
var address = BaseAddress;
foreach (uint offset in Offsets)
{
address = TargetMemory.ReadMemory<ulong>(address) + offset;
}
return address;
}
}
public static class ObjectType
{
public static int GetSize<T>()
{
return Marshal.SizeOf(typeof(T));
}
public static byte[] GetBytes<T>(T Value)
{
string typename = typeof(T).ToString();
Console.WriteLine(typename);
switch (typename)
{
case "System.Single":
return BitConverter.GetBytes((float)Convert.ChangeType(Value, typeof(float)));
case "System.Int32":
return BitConverter.GetBytes((int)Convert.ChangeType(Value, typeof(int)));
case "System.Int64":
return BitConverter.GetBytes((long)Convert.ChangeType(Value, typeof(long)));
case "System.Double":
return BitConverter.GetBytes((double)Convert.ChangeType(Value, typeof(double)));
case "System.Byte":
return BitConverter.GetBytes((byte)Convert.ChangeType(Value, typeof(byte)));
case "System.String":
return Encoding.Unicode.GetBytes((string)Convert.ChangeType(Value, typeof(string)));
default:
return new byte[0];
}
}
}
}