This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryHelper.cs
139 lines (128 loc) · 4.94 KB
/
MemoryHelper.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using SysProcess = System.Diagnostics.Process;
namespace GTA5_Casino_Helper
{
public static class MemoryHelper
{
// TODO Support x86 ( Fork me plz,i'm lazy. )
[DllImport("kernel32.dll")]
public static extern IntPtr ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
public static byte[] ReadBytes(IntPtr Handle, IntPtr Address, uint BytesToRead)
{
IntPtr ptrBytesRead;
byte[] buffer = new byte[BytesToRead];
ReadProcessMemory(Handle, Address, buffer, BytesToRead, out ptrBytesRead);
return buffer;
}
public static Int64 ReadInt64(IntPtr Address, uint length = 8, IntPtr? Handle = null)
{
return (BitConverter.ToInt64(ReadBytes((IntPtr)Handle, Address, length), 0));
}
static IntPtr GetBase(SysProcess handle, string module = null)
{
ProcessModuleCollection modules = handle.Modules;
if (module != null)
{
for (int i = 0; i <= modules.Count - 1; i++)
{
if (modules[i].ModuleName == module)
{
return (IntPtr)modules[i].BaseAddress;
}
}
Console.WriteLine("Module Not Found");
}
else
{
return (IntPtr)handle.MainModule.BaseAddress;
}
return (IntPtr)0;
}
static IntPtr GetBase(string pname)
{
SysProcess handle = SysProcess.GetProcessesByName(pname)[0];
return handle.MainModule.BaseAddress;
}
public static IntPtr ReadValueByPrt(SysProcess process, IntPtr[] offsets, bool debug = false, string module = null)
{
IntPtr tmpptr = (IntPtr)0;
IntPtr Base = GetBase(process);
Console.WriteLine("Original base: " + Base);
if (module != null)
{
Base = GetBase(process, module);
Console.WriteLine("Module base: " + Base);
Console.WriteLine("");
}
for (int i = 0; i <= offsets.Length - 1; i++)
{
if (i == 0)
{
if (debug)
Console.Write(Base + "[Base] + " + offsets[i] + "[OFFSET 0]");
IntPtr ptr = IntPtr.Add(Base, (int)offsets[i]);
tmpptr = (IntPtr)ReadInt64(ptr, 8, process.Handle);
if (debug)
Console.WriteLine(" is " + tmpptr);
}
else
{
if (debug)
Console.Write(tmpptr + " + " + offsets[i] + "[OFFSET " + i + "]");
IntPtr ptr2 = IntPtr.Add(tmpptr, (int)offsets[i]);
tmpptr = (IntPtr)ReadInt64(ptr2, 8, process.Handle); //last position's value
if (debug)
Console.WriteLine(" is " + tmpptr);
}
}
return tmpptr;
}
public static IntPtr GetPtr(SysProcess process, IntPtr[] offsets, bool debug = false, string module = null)
{
IntPtr tmpptr = (IntPtr)0;
IntPtr Base = GetBase(process);
Console.WriteLine("Original base: " + Base);
if (module != null)
{
Base = GetBase(process, module);
Console.WriteLine("Module base: " + Base);
Console.WriteLine("");
}
for (int i = 0; i <= offsets.Length - 1; i++)
{
if (i == 0)
{
if (debug)
Console.Write(Base + "[Base] + " + offsets[i] + "[OFFSET 0]");
IntPtr ptr = IntPtr.Add(Base, (int)offsets[i]);
tmpptr = (IntPtr)ReadInt64(ptr, 8, process.Handle);
if (debug)
Console.WriteLine(" is " + tmpptr);
}
else
{
if (debug)
Console.Write(tmpptr + " + " + offsets[i] + "[OFFSET " + i + "]");
IntPtr ptr2 = IntPtr.Add(tmpptr, (int)offsets[i]);
if (i == offsets.Length - 1)
{
if (debug)
Console.WriteLine(" is " + ptr2);
return ptr2;
}
tmpptr = (IntPtr)ReadInt64(ptr2, 8, process.Handle);
if (debug)
Console.WriteLine(" is " + tmpptr);
}
}
return tmpptr;
}
}
}