-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemdata.cpp
261 lines (239 loc) · 8.85 KB
/
memdata.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "memdata.h"
#include <tlhelp32.h>
#include <QDebug.h>
std::vector<std::wstring> MemData::proc_namelist;
std::vector<DWORD> MemData::proc_idlist;
MemData::MemData(DWORD pid) :
buf(NULL),
size(0),
regions(NULL),
region_sizes(NULL)
{
readMem(pid);
}
MemData::~MemData() {
free(buf);
free(regions);
free(region_sizes);
}
void MemData::cmpBytes(MemData *md, bool diff)
{
size = 0;
int new_n_regions = 0;
int max_n_regions = n_regions;
void **new_regions = (void**) malloc(max_n_regions * sizeof(void*));
if (new_regions == NULL) {
std::cerr << "Error: malloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
SIZE_T *new_region_sizes = (SIZE_T*) malloc(max_n_regions * sizeof(SIZE_T));
if (new_region_sizes == NULL) {
std::cerr << "Error: malloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
long index = 0;
for (int i = 0; i < n_regions; i++) {
bool newblock = true;
for (SIZE_T j = 0; j < region_sizes[i]; j++) {
void *addr = (unsigned char*) regions[i] + j;
unsigned char byte = buf[index + j];
unsigned char newbyte = byte;
bool equal = true;
long cmp_index = 0;
for (int k = 0; k < md->getNRegions(); k++) {
if (addr >= md->getRegions()[k] && addr < (unsigned char*) md->getRegions()[k] + md->getRegionSizes()[k]) {
long long offset = (unsigned char*) addr - (unsigned char*) md->getRegions()[k];
if (diff) {
if (byte != md->getBuffer()[cmp_index + offset]) {
/*
qDebug() << "byte:" << (int)byte
<< " cmpbyte:" << (int)md->getBuffer()[cmp_index + offset]
<< " pos:" << index + j
<< " cmppos:" << cmp_index + offset
<< " addr:" << addr
<< " cmpaddr:" << (unsigned char*) md->getRegions()[k] + offset;
*/
newbyte = (int)md->getBuffer()[cmp_index + offset];
equal = false;
}
}
else {
if (byte == md->getBuffer()[cmp_index + offset]) {
newbyte = (int)md->getBuffer()[cmp_index + offset];
equal = false;
}
}
break;
}
cmp_index += md->getRegionSizes()[k];
}
if (!equal) {
if (newblock) {
new_n_regions++;
if (new_n_regions > max_n_regions) {
max_n_regions *= 2;
new_regions = (void**) realloc(new_regions, max_n_regions * sizeof(void*));
if (new_regions == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
new_region_sizes = (SIZE_T*) realloc(new_region_sizes, max_n_regions * sizeof(SIZE_T));
if (new_region_sizes == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
}
new_regions[new_n_regions-1] = addr;
new_region_sizes[new_n_regions-1] = 1;
}
else {
new_region_sizes[new_n_regions-1]++;
}
buf[size] = newbyte;
size++;
newblock = false;
}
else {
newblock = true;
}
}
index += region_sizes[i];
}
free(regions); regions = new_regions;
free(region_sizes); region_sizes = new_region_sizes;
n_regions = new_n_regions;
if (size != 0) {
buf = (unsigned char*) realloc(buf, size*sizeof(unsigned char));
if (buf == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
}
else {
buf = NULL;
}
}
void MemData::padBuffer(int padding)
{
if (padding > 0) {
buf = (unsigned char*) realloc(buf, (size+padding)*sizeof(unsigned char));
if (buf == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
memset(buf+size, 0, padding);
}
}
void MemData::fillProcLists()
{
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hsnap == INVALID_HANDLE_VALUE) {
std::cerr << "CreateToolhelp32Snapshot() failed with error " + std::to_string(GetLastError())<< std::endl;
exit(EXIT_FAILURE);
}
PROCESSENTRY32 proc_entry;
proc_entry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hsnap, &proc_entry)) {
std::cerr << "Process32First() failed with error " + std::to_string(GetLastError()) << std::endl;
exit(EXIT_FAILURE);
}
proc_namelist.clear();
proc_idlist.clear();
do {
std::wstring ws(proc_entry.szExeFile);
proc_namelist.push_back(ws);
proc_idlist.push_back(proc_entry.th32ProcessID);
} while (Process32Next(hsnap, &proc_entry));
}
void* MemData::findAddr(long index)
{
int running_total = 0;
for (int i = 0; i < n_regions; i++) {
running_total += region_sizes[i];
if (index < running_total) {
long offset = index - (running_total - region_sizes[i]);
return ((unsigned char*)regions[i]) + offset;
}
}
return NULL;
}
void MemData::readMem(DWORD pid)
{
HANDLE hproc = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, false, pid);
if (hproc == NULL) {
std::cerr << "OpenProcess() failed with error " + std::to_string(GetLastError()) << std::endl;
free(buf); buf = NULL;
return;
}
MEMORY_BASIC_INFORMATION info;
unsigned char *addr = NULL;
long est_size = 0;
int est_regions = 0;
while (VirtualQueryEx(hproc, addr, &info, sizeof(info)) == sizeof(info)) {
if (info.Protect == PAGE_READWRITE && info.Type == MEM_PRIVATE) {
est_size += info.RegionSize;
}
addr += info.RegionSize;
est_regions++;
}
if (est_regions == 0) {
free(buf); buf = NULL;
return;
}
buf = (unsigned char*) malloc(est_size * sizeof(unsigned char));
if (buf == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
regions = (void**) malloc(est_regions * sizeof(void*));
if (regions == NULL) {
std::cerr << "Error: malloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
region_sizes = (SIZE_T*) malloc(est_regions * sizeof(SIZE_T));
if (region_sizes == NULL) {
std::cerr << "Error: malloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
addr = NULL;
n_regions = 0;
while (VirtualQueryEx(hproc, addr, &info, sizeof(info)) == sizeof(info)) {
if (info.Protect == PAGE_READWRITE && info.Type == MEM_PRIVATE) {
size += info.RegionSize;
if (size > maxsize) {
std::cerr << "Warning: Memory limit reached" << std::endl;
size -= info.RegionSize;
break;
}
if (size > est_size) {
buf = (unsigned char*) realloc(buf, size * sizeof(unsigned char));
if (buf == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
}
SIZE_T bytes_read;
if (ReadProcessMemory(hproc, addr, buf + (size-info.RegionSize), info.RegionSize, &bytes_read) == 0) {
std::cerr << "Warning: ReadProcessMemory() failed with error " + std::to_string(GetLastError()) << std::endl;
size -= info.RegionSize;
continue;
}
n_regions++;
if (n_regions > est_regions) {
regions = (void**) realloc(regions, n_regions * sizeof(void*));
if (regions == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
region_sizes = (SIZE_T*) realloc(region_sizes, n_regions * sizeof(SIZE_T));
if (regions == NULL) {
std::cerr << "Error: realloc() failed" << std::endl;
exit(EXIT_FAILURE);
}
}
regions[n_regions-1] = addr;
region_sizes[n_regions-1] = info.RegionSize;
}
addr += info.RegionSize;
}
}