-
Notifications
You must be signed in to change notification settings - Fork 2
/
remote.cpp
417 lines (323 loc) · 10.2 KB
/
remote.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
** This code was copied from https://github.com/bluuman/jwaim
** and sightly modified.
*/
#include "remote.h"
#define FINDPATTERN_CHUNKSIZE 0x1000
namespace remote {
// Map Module
void* MapModuleMemoryRegion::find(Handle handle, const char* data, const char* pattern) {
char buffer[FINDPATTERN_CHUNKSIZE];
size_t len = strlen(pattern);
size_t chunksize = sizeof(buffer);
size_t totalsize = this->end - this->start;
size_t chunknum = 0;
while (totalsize) {
size_t readsize = (totalsize < chunksize) ? totalsize : chunksize;
size_t readaddr = this->start + (chunksize * chunknum);
bzero(buffer, chunksize);
if (handle.Read((void*) readaddr, buffer, readsize)) {
for (size_t b = 0; b < readsize; b++) {
size_t matches = 0;
while (buffer[b + matches] == data[matches] || pattern[matches] != 'x') {
matches++;
if (matches == len) {
/*
printf("Debug Output:\n");
for (int i = 0; i < readsize-b; i++)
{
if (i != 0 && i % 8 == 0) {
printf("\n");
}
printf("%02x ",(unsigned char)buffer[b+i]);
}
printf("\n");
*/
return (char*) (readaddr + b);
}
}
}
}
totalsize -= readsize;
chunknum++;
}
return NULL;
}
void* MapModuleMemoryRegion::print(Handle handle)
{
char buffer[FINDPATTERN_CHUNKSIZE];
size_t chunksize = sizeof(buffer);
size_t totalsize = this->end - this->start;
size_t chunknum = 0;
while (totalsize)
{
size_t readsize = (totalsize < chunksize) ? totalsize : chunksize;
size_t readaddr = this->start + (chunksize * chunknum);
bzero(buffer, chunksize);
if (handle.Read((void*) readaddr, buffer, readsize))
{
for (size_t b = 0; b < readsize; b+=8)
{
printf("%lx : ",readaddr+b);
for (int i = 0; i < 8; ++i)
{
printf("%02x ",(unsigned char)buffer[b+i]);
}
printf("| ");
for (int i = 0; i < 8; ++i)
{
unsigned char c = (unsigned char)buffer[b+i];
if (c>31 && c<127) printf("%c ",c);
else printf(". ");
}
printf("\n");
}
printf("\n");
}
totalsize -= readsize;
chunknum++;
}
return NULL;
}
void* MapModuleMemoryRegion::print(Handle handle, void* address)
{
char buffer[FINDPATTERN_CHUNKSIZE];
size_t chunksize = sizeof(buffer);
size_t totalsize = this->end - this->start;
size_t chunknum = 0;
while (totalsize)
{
size_t readsize = (totalsize < chunksize) ? totalsize : chunksize;
size_t readaddr = this->start + (chunksize * chunknum);
if (readaddr<(size_t)address && readaddr+readsize>size_t(address))
{
bzero(buffer, chunksize);
if (handle.Read((void*) readaddr, buffer, readsize))
{
for (size_t b = 0; b < readsize; b+=8)
{
printf("%lx : ",readaddr+b);
for (int i = 0; i < 8; ++i)
{
printf("%02x ",(unsigned char)buffer[b+i]);
}
printf("| ");
for (int i = 0; i < 8; ++i)
{
unsigned char c = (unsigned char)buffer[b+i];
if (c>31 && c<127) printf("%c ",c);
else printf(". ");
}
printf("\n");
}
printf("\n");
}
}
totalsize -= readsize;
chunknum++;
}
return NULL;
}
// Handle
Handle::Handle(pid_t target) {
std::stringstream buffer;
buffer << target;
pid = target;
pidStr = buffer.str();
}
Handle::Handle(std::string target) {
// Check to see if the string is numeric (no negatives or dec allowed, which makes this function usable)
if (strspn(target.c_str(), "0123456789") != target.size()) {
pid = -1;
pidStr.clear();
} else {
std::istringstream buffer(target);
pidStr = target;
buffer >> pid;
}
}
std::string Handle::GetPath() {
return GetSymbolicLinkTarget(("/proc/" + pidStr + "/exe"));
}
std::string Handle::GetWorkingDirectory() {
return GetSymbolicLinkTarget(("/proc/" + pidStr + "/cwd"));
}
bool Handle::IsValid() {
return (pid != -1);
}
bool Handle::IsRunning() {
if (!IsValid())
return false;
struct stat sts;
return !(stat(("/proc/" + pidStr).c_str(), &sts) == -1 && errno == ENOENT);
}
bool Handle::Write(void* address, void* buffer, size_t size) {
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = buffer;
local[0].iov_len = size;
remote[0].iov_base = address;
remote[0].iov_len = size;
return (process_vm_writev(pid, local, 1, remote, 1, 0) == size);
}
bool Handle::Read(void* address, void* buffer, size_t size) {
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = buffer;
local[0].iov_len = size;
remote[0].iov_base = address;
remote[0].iov_len = size;
return (process_vm_readv(pid, local, 1, remote, 1, 0) == size);
}
unsigned long Handle::GetAbsoluteAddress(void* address, int offset, int size)
{
int code = 0;
if (Read((char*) ((unsigned long) address + offset), &code, sizeof(unsigned int)))
{
return code + (unsigned long) address + size;
}
return 0;
}
unsigned long Handle::GetCallAddress(void* address) {
int code = 0;
if (Read((char*) address, &code, sizeof(unsigned int))) {
return (unsigned long)code + (unsigned long) address + 4;
}
return 0;
}
unsigned long Handle::GetShortJmpAddress(void* address) { //give me the address after the jump
int code = 0;
if (Read((char*) address, &code, sizeof(unsigned int))) {
if(code<=0x7f){
return (unsigned long)code + (unsigned long) address + 1;
}
else if(code>=0x80){
return (unsigned long)code-0xFF + (unsigned long) address;
}
}
return 0;
}
MapModuleMemoryRegion* Handle::GetRegionOfAddress(void* address) {
for (size_t i = 0; i < regions.size(); i++) {
if (regions[i].start <= (unsigned long) address && regions[i].end > (unsigned long) address) {
return ®ions[i];
}
}
return NULL;
}
void Handle::ParseMaps() {
regions.clear();
std::ifstream maps("/proc/" + pidStr + "/maps");
std::string line;
while (std::getline(maps, line)) {
std::istringstream iss(line);
std::string memorySpace, permissions, offset, device, inode;
if (iss >> memorySpace >> permissions >> offset >> device >> inode) {
std::string pathname;
for (size_t ls = 0, i = 0; i < line.length(); i++) {
if (line.substr(i, 1).compare(" ") == 0) {
ls++;
if (ls == 5) {
size_t begin = line.substr(i, line.size()).find_first_not_of(' ');
if (begin != -1) {
pathname = line.substr(begin + i, line.size());
} else {
pathname.clear();
}
}
}
}
MapModuleMemoryRegion region;
size_t memorySplit = memorySpace.find_first_of('-');
size_t deviceSplit = device.find_first_of(':');
std::stringstream ss;
if (memorySplit != -1) {
ss << std::hex << memorySpace.substr(0, memorySplit);
ss >> region.start;
ss.clear();
ss << std::hex << memorySpace.substr(memorySplit + 1, memorySpace.size());
ss >> region.end;
ss.clear();
}
if (deviceSplit != -1) {
ss << std::hex << device.substr(0, deviceSplit);
ss >> region.deviceMajor;
ss.clear();
ss << std::hex << device.substr(deviceSplit + 1, device.size());
ss >> region.deviceMinor;
ss.clear();
}
ss << std::hex << offset;
ss >> region.offset;
ss.clear();
ss << inode;
ss >> region.inodeFileNumber;
region.readable = (permissions[0] == 'r');
region.writable = (permissions[1] == 'w');
region.executable = (permissions[2] == 'x');
region.shared = (permissions[3] != '-');
if (!pathname.empty()) {
region.pathname = pathname;
size_t fileNameSplit = pathname.find_last_of('/');
if (fileNameSplit != -1) {
region.filename = pathname.substr(fileNameSplit + 1, pathname.size());
}
}
regions.push_back(region);
}
}
}
std::string Handle::GetSymbolicLinkTarget(std::string target) {
char buf[PATH_MAX];
ssize_t len = ::readlink(target.c_str(), buf, sizeof(buf) - 1);
if (len != -1) {
buf[len] = 0;
return std::string(buf);
}
return std::string();
}
};
unsigned long remote::getModule(const char * moduleName, pid_t pid) {
char cmd[256];
FILE *maps;
unsigned long result = 0;
snprintf(cmd, 256, "grep \"%s\" /proc/%i/maps | head -n 1 | cut -d \"-\" -f1", moduleName, pid);
maps = popen(cmd, "r");
if (maps) {
if (fscanf(maps, "%" SCNx64, &result));
}
pclose(maps);
return result;
}
// Functions Exported
bool remote::FindProcessByName(std::string name, remote::Handle* out) {
if (out == NULL || name.empty())
return false;
struct dirent *dire;
DIR *dir = opendir("/proc/");
if (dir) {
while ((dire = readdir(dir)) != NULL) {
if (dire->d_type != DT_DIR)
continue;
std::string mapsPath = ("/proc/" + std::string(dire->d_name) + "/maps");
if (access(mapsPath.c_str(), F_OK) == -1)
continue;
remote::Handle proc(dire->d_name);
if (!proc.IsValid() || !proc.IsRunning())
continue;
std::string procPath = proc.GetPath();
if (procPath.empty())
continue;
size_t namePos = procPath.find_last_of('/');
if (namePos == -1)
continue; // what?
std::string exeName = procPath.substr(namePos + 1);
if (exeName.compare(name) == 0) {
*out = proc;
return true;
}
}
closedir(dir);
}
return false;
}