-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.hpp
More file actions
167 lines (136 loc) · 4.48 KB
/
Process.hpp
File metadata and controls
167 lines (136 loc) · 4.48 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
158
159
160
161
162
163
164
165
166
167
#ifndef _PROCESS_HPP_
#define _PROCESS_HPP_
#include <unistd.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <dirent.h>
#include <sys/uio.h>
class ReadWriteFactory
{
public:
void ReadMemoryChunk(uint32_t processid, uint64_t address, void* receiver ,size_t size){
struct iovec local;
struct iovec remote;
local.iov_base = (void *) receiver;
local.iov_len = size;
remote.iov_base = (void *) address;
remote.iov_len = size;
ssize_t nread = process_vm_readv(processid, &local, 2, &remote, 1, 0);
}
template <typename T>
T ReadMemory(uint32_t processid, uint64_t address){
T buf = 0;
struct iovec local;
struct iovec remote;
local.iov_base = &buf;
local.iov_len = sizeof(T);
remote.iov_base = (void *) address;
remote.iov_len = sizeof(T);
ssize_t nread = process_vm_readv(processid, &local, 2, &remote, 1, 0);
return buf;
}
void WriteBytes(uint32_t processId, uint64_t address, std::vector<uint8_t> patch){
struct iovec local;
struct iovec remote;
local.iov_base = &patch;
local.iov_len = patch.size();
remote.iov_base = (void *) address;
remote.iov_len = sizeof(address);
process_vm_writev(processId, &local, 2, &remote, 1, 0);
}
template <typename T>
void WriteMemory(uint32_t processId, uint64_t address, T value){
T buf = 0;
T newVal = value;
struct iovec local;
struct iovec remote;
local.iov_base = &newVal;
local.iov_len = sizeof(T);
remote.iov_base = (void *) address;
remote.iov_len = sizeof(T);
process_vm_writev(processId, &local, 2, &remote, 1, 0);
}
};
class Process : public ReadWriteFactory
{
public:
pid_t procId = 0;
pid_t base = 0;
void getProcess(std::string procName)
{
int pid = -1;
DIR *dp = opendir("/proc");
if (dp != NULL)
{
struct dirent *dirp;
while (pid < 0 && (dirp = readdir(dp)))
{
int id = atoi(dirp->d_name);
if (id > 0)
{
std::string cmdPath = std::string("/proc/") + dirp->d_name + "/cmdline";
std::ifstream cmdFile(cmdPath.c_str());
std::string cmdLine;
std::getline(cmdFile, cmdLine);
if (!cmdLine.empty())
{
size_t pos = cmdLine.find('\0');
if (pos != std::string::npos)
cmdLine = cmdLine.substr(0, pos);
pos = cmdLine.rfind('/');
if (pos != std::string::npos)
cmdLine = cmdLine.substr(pos + 1);
if (procName == cmdLine)
pid = id;
}
}
}
}
closedir(dp);
this->procId = pid;
char *cstr = new char[procName.length() + 1];
strcpy(cstr, procName.c_str());
this->base = this->getModule(cstr);
delete [] cstr;
}
uint64_t getModule(char* name) {
char cmd[200];
sprintf(cmd, "cat /proc/%d/maps | grep %s -m1 | cut -d '-' -f1", this->procId, name);
FILE *f = popen(cmd, "r");
char line[20];
fgets(line, 20, f);
pclose(f);
return strtoul(line, NULL, 16);
}
template <typename T>
T read(uint64_t addr){
return ReadMemory<T>(procId, addr);
}
template <typename T>
void write(uint64_t addr, T value){
WriteMemory<T>(procId, addr, value);
}
void writeBytes(uint64_t address, std::vector<uint8_t> patch){
WriteBytes(this->procId, address, patch);
}
template <typename T>
T readMulti(uint64_t base, std::vector<uint32_t> offsets){
uint64_t buffer = base;
for (int i = 0; i < offsets.size() - 1; i++)
{
buffer = this->read<uint64_t>(buffer + offsets[i]);
}
return this->read<T>(buffer + offsets.back());
}
uint64_t readMultiAddr(uint64_t base, std::vector<uint32_t> offsets){
uint64_t addr = base;
for (unsigned int i = 0; i < offsets.size() - 1; i++)
{
addr = ReadMemory<uint64_t>(this->procId, addr + offsets[i]);
}
return addr + offsets.back();
}
};
#endif