This repository was archived by the owner on Jul 5, 2021. It is now read-only.
forked from duke12468/Carat-Hack
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemory.cpp
More file actions
157 lines (116 loc) · 4.03 KB
/
Copy pathmemory.cpp
File metadata and controls
157 lines (116 loc) · 4.03 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
#include "memory.h"
int __stdcall memapi::write(unsigned long ul_address, std::string write_buffer)
{
DWORD dw_old_protection_flags, dw_size;
void *pv_address = (void *)ul_address;
size_t size = std::count(write_buffer.begin(), write_buffer.end(), ' ') + 1;
utils::replace_string(write_buffer, " ", "");
std::vector<unsigned char> bytes = utils::hex_to_bytes(write_buffer);
PBYTE pb_write_buffer = bytes.data();
ul_address = ROUND_DOWN(pv_address, 0x1000);
dw_size = ROUND_UP(size, 0x1000);
if (VirtualProtect((LPVOID)ul_address, dw_size, PAGE_EXECUTE_READWRITE, &dw_old_protection_flags))
{
memcpy(pv_address, pb_write_buffer, size);
if (VirtualProtect((LPVOID)ul_address, dw_size, dw_old_protection_flags, &dw_old_protection_flags))
{
if (FlushInstructionCache(GetCurrentProcess(), (LPCVOID)ul_address, dw_size))
return 0;
return GetLastError();
}
}
return GetLastError();
}
void memapi::write_int(unsigned long ul_address, int value) {
DWORD dw_old_protection_flags, dw_size;
void *pv_address = (void *)ul_address;
size_t size = sizeof(value);
ul_address = ROUND_DOWN(pv_address, 0x1000);
dw_size = ROUND_UP(size, 0x1000);
if (VirtualProtect((LPVOID)ul_address, dw_size, PAGE_EXECUTE_READWRITE, &dw_old_protection_flags))
{
memcpy(pv_address, static_cast<char*>(static_cast<void*>(&value)), size);
if (VirtualProtect((LPVOID)ul_address, dw_size, dw_old_protection_flags, &dw_old_protection_flags))
FlushInstructionCache(GetCurrentProcess(), (LPCVOID)ul_address, dw_size);
}
}
void memapi::write_float(unsigned long ul_address, float value) {
DWORD dw_old_protection_flags, dw_size;
void *pv_address = (void *)ul_address;
size_t size = sizeof(value);
ul_address = ROUND_DOWN(pv_address, 0x1000);
dw_size = ROUND_UP(size, 0x1000);
if (VirtualProtect((LPVOID)ul_address, dw_size, PAGE_EXECUTE_READWRITE, &dw_old_protection_flags))
{
memcpy(pv_address, static_cast<char*>(static_cast<void*>(&value)), size);
if (VirtualProtect((LPVOID)ul_address, dw_size, dw_old_protection_flags, &dw_old_protection_flags))
FlushInstructionCache(GetCurrentProcess(), (LPCVOID)ul_address, dw_size);
}
}
unsigned long memapi::pointer::ptr_address(unsigned long base, unsigned long offsets[]) {
int ptr_level = sizeof(offsets);
unsigned long ptr = *(unsigned long*)(base);
if (ptr == 0)
return 0;
for (int i = 0; i < ptr_level; i++)
{
if (i == ptr_level - 1)
{
ptr = (DWORD)(ptr + offsets[i]);
if (ptr == 0)
return 0;
return ptr;
}
else
{
ptr = *(DWORD*)(ptr + offsets[i]);
if (ptr == 0)
return 0;
}
}
return ptr;
}
bool memapi::pointer::valid(unsigned long base, unsigned long offset)
{
ULONG_PTR *ul_base = (ULONG_PTR *)base;
if (!IsBadReadPtr((VOID *)ul_base, sizeof(ULONG_PTR)))
if (!IsBadReadPtr((VOID *)((*(ULONG_PTR *)ul_base) + offset), sizeof(ULONG_PTR)))
return true;
return false;
}
int memapi::pointer::read_int(unsigned long base, unsigned long offset)
{
if (valid(base, offset))
return *(int *)(*(unsigned long *)base + offset);
else
return 0;
}
void memapi::pointer::write_int(unsigned long base, unsigned long offset, int value)
{
if (valid(base, offset))
*(int *)(*(unsigned long *)base + offset) = value;
}
std::string memapi::pointer::read_str(unsigned long base, unsigned long offset)
{
if (valid(base, offset))
return *(std::string *)(*(unsigned long *)base + offset);
else
return "";
}
void memapi::pointer::write_str(unsigned long base, unsigned long offset, std::string value)
{
if (valid(base, offset))
*(std::string *)(*(unsigned long *)base + offset) = value;
}
float memapi::pointer::read_float(unsigned long base, unsigned long offset)
{
if (valid(base, offset))
return *(float *)(*(unsigned long *)base + offset);
else
return 0;
}
void memapi::pointer::write_float(unsigned long base, unsigned long offset, float value)
{
if (valid(base, offset))
*(float *)(*(unsigned long *)base + offset) = value;
}