-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmem.cpp
121 lines (104 loc) · 3.87 KB
/
mem.cpp
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
#include "mem.h"
#include "util.h"
#include <sstream>
uint64_t Mem::getModuleStart(std::string module)
{
uint64_t moduleStart = -1;
std::string cmd = "cat /proc/" + std::to_string(Util::getPID()) + "/maps | grep " + module + " | head -n1 | cut -d '-' -f1";
std::string output = Util::exec(cmd.c_str());
if (output.length() == 0)
{
std::cerr << "Error getting CSGO memory map" << std::endl;
exit(1);
}
// convert stirng to uint64_t
std::stringstream ss;
ss << std::hex << output.c_str();
ss >> moduleStart;
return moduleStart;
}
bool Mem::readmem(void *address, void *buf, size_t size)
{
if (PID == 0 || size == 0)
return false;
iovec iovLocalAddressSpace;
iovec iovRemoteAddressSpace;
iovLocalAddressSpace.iov_base = buf;
iovLocalAddressSpace.iov_len = size;
iovRemoteAddressSpace.iov_base = address;
iovRemoteAddressSpace.iov_len = size;
ssize_t bytes_count_read = process_vm_readv(PID,
&iovLocalAddressSpace,
1, //Size of the local iovec array
&iovRemoteAddressSpace, //Remote iovec array
1, //Size of the remote iovec array
0); //Flags, unused
return (bytes_count_read == (ssize_t)size);
}
bool Mem::writemem(void *address, void *buf, size_t size)
{
if (PID == 0 || size == 0)
return false;
iovec iovLocalAddressSpace;
iovec iovRemoteAddressSpace;
iovLocalAddressSpace.iov_base = buf;
iovLocalAddressSpace.iov_len = size;
iovRemoteAddressSpace.iov_base = address;
iovRemoteAddressSpace.iov_len = size;
ssize_t bytes_count_write = process_vm_writev(PID,
&iovLocalAddressSpace, //Local iovec array
1, //Size of the local iovec array
&iovRemoteAddressSpace, //Remote iovec array
1, //Size of the remote iovec array
0); //Flags, unused
return (bytes_count_write == (ssize_t)size);
}
uint64_t Mem::addr_from_ptr(uint64_t ptr)
{
unsigned char buffer[8];
if (!Mem::readmem((void *)ptr, buffer, sizeof(buffer)))
{
std::cerr << "Couldn't read from " << std::hex << ptr << std::endl;
exit(1);
}
// dereference where buffer points to
ptr = *(uint64_t *)(&buffer);
return ptr;
}
uint64_t Mem::addr_from_multilvl_ptr(uint64_t ptr, std::vector<unsigned int> offsets)
{
unsigned char buffer[8];
// loop through pointer levels
for (int i = 0; i < offsets.size(); i++)
{
ptr += offsets[i];
if (!Mem::readmem((void *)ptr, buffer, sizeof(buffer)))
{
std::cerr << "Couldn't read from " << std::hex << ptr << std::endl;
exit(1);
}
// don't dereference in last round
if (i == offsets.size() - 1)
{
return ptr;
}
// dereference where buffer points to
ptr = *(uint64_t *)(&buffer);
}
return ptr;
}
std::vector<unsigned char> Mem::readFromAddr(void *address, size_t size)
{
unsigned char buffer[size] = {0};
if (!Mem::readmem(address, buffer, size))
{
std::cerr << "Couldn't read from " << std::hex << address << std::endl;
exit(1);
}
std::vector<unsigned char> res(buffer, buffer + size);
return res;
}
bool Mem::writeToAddr(void *address, std::vector<unsigned char> buffer)
{
return Mem::writemem(address, reinterpret_cast<unsigned char *>(buffer.data()), buffer.size());
}