This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathMemory.cpp
157 lines (155 loc) · 5.44 KB
/
Memory.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
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
#pragma once
#include <iostream>
#include <sstream>
#include <sys/uio.h>
#include <math.h>
namespace mem
{
pid_t m_pid = 0;
pid_t GetPID()
{
if (m_pid > 0)
return m_pid;
char buf[512];
FILE *cmd_pipe = popen("pidof -s r5apex.exe", "r");
fgets(buf, 512, cmd_pipe);
pid_t pid = strtoul(buf, NULL, 10);
pclose(cmd_pipe);
m_pid = pid;
return pid;
}
bool Read(long address, void *pBuff, size_t size)
{
if (size == 0)
return false;
void *pAddress = (void *)address;
pid_t pid = GetPID();
struct iovec iovLocalAddressSpace[1]{0};
struct iovec iovRemoteAddressSpace[1]{0};
iovLocalAddressSpace[0].iov_base = pBuff; // Store data in this buffer
iovLocalAddressSpace[0].iov_len = size; // which has this size.
iovRemoteAddressSpace[0].iov_base = pAddress; // The data comes from here
iovRemoteAddressSpace[0].iov_len = size; // and has this size.
ssize_t sSize = process_vm_readv(
pid, // Remote process id
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
if (sSize == (ssize_t)size)
return true;
else if (sSize == 0)
return false;
return false;
}
bool Write(long address, void *pBuff, size_t size)
{
if (size == 0)
return false;
void *pAddress = (void *)address;
pid_t pid = GetPID();
struct iovec iovLocalAddressSpace[1]{0};
struct iovec iovRemoteAddressSpace[1]{0};
iovLocalAddressSpace[0].iov_base = pBuff; // Store data in this buffer
iovLocalAddressSpace[0].iov_len = size; // which has this size.
iovRemoteAddressSpace[0].iov_base = pAddress; // The data will be writted here
iovRemoteAddressSpace[0].iov_len = size; // and has this size.
ssize_t sSize = process_vm_writev(
pid, // Remote process id
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
if (sSize == (ssize_t)size)
return true;
else if (sSize == 0)
return false;
return false;
}
std::string ReadString(long address)
{
int size = sizeof(std::string);
char buffer[size] = {0};
bool success = Read(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to read String at address: " + address);
return std::string(buffer);
}
short ReadShort(long address)
{
int size = sizeof(short);
short buffer;
bool success = Read(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to read short at address: " + address);
return buffer;
}
void WriteShort(long address, short num)
{
int size = sizeof(short);
short buffer = num;
bool success = Write(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to write short at address: " + address);
}
int ReadInt(long address)
{
int size = sizeof(int);
int buffer;
bool success = Read(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to read int at address: " + address);
return buffer;
}
void WriteInt(long address, int num)
{
int size = sizeof(int);
int buffer = num;
bool success = Write(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to write int at address: " + address);
}
float ReadFloat(long address)
{
int size = sizeof(float);
float buffer;
bool success = Read(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to read float at address: " + address);
return buffer;
}
void WriteFloat(long address, float num)
{
int size = sizeof(float);
float buffer = num;
bool success = Write(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to write float at address: " + address);
}
long ReadLong(long address)
{
int size = sizeof(long);
long buffer;
bool success = Read(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to read long at address: " + address);
return buffer;
}
void WriteLong(long address, long num)
{
int size = sizeof(long);
long buffer = num;
bool success = Write(address, &buffer, size);
if (!success)
throw new std::invalid_argument("Failed to write long at address: " + address);
}
std::string convertPointerToHexString(long pointer)
{
std::stringstream stream;
stream << "0x" << std::hex << pointer;
std::string result(stream.str());
return result;
}
}