-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory.hpp
134 lines (107 loc) · 3.28 KB
/
memory.hpp
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
#pragma once
#include <sys/uio.h>
#include <iostream>
#include <cerrno>
#include <sstream>
#include <fstream>
#include <cstddef>
namespace Memory {
//Write memory
inline bool Write(pid_t pid, unsigned int address, void *buf, size_t size) {
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = buf;
local[0].iov_len = size;
remote[0].iov_base = (void*)address;
remote[0].iov_len = size;
// if (process_vm_writev(pid, local, 1, remote, 1, 0) == -1) {
// std::cout << "Error write: " << errno << '\n';
// std::cout << "Error write address: " << std::hex << address << '\n';
// }
return (process_vm_writev(pid, local, 1, remote, 1, 0) == size);
}
//Read memory
inline bool Read(pid_t pid, unsigned int address, void *buf, size_t size) {
struct iovec local[1];
struct iovec remote[1];
local[0].iov_base = buf;
local[0].iov_len = size;
remote[0].iov_base = (void*)address;
remote[0].iov_len = size;
// if (process_vm_readv(pid, local, 1, remote, 1, 0) == -1) {
// std::cout << "Error read: " << errno << '\n';
// std::cout << "Error read address: " << std::hex << address << '\n';
// }
return (process_vm_readv(pid, local, 1, remote, 1, 0) == size);
}
inline unsigned int hexToInt(const std::string &hexString) {
// Use stringstream for conversion
std::stringstream ss;
ss << std::hex << hexString;
// Store the result
unsigned int result;
ss >> result;
return result;
}
inline unsigned int getModuleBaseAddress(pid_t procPid, std::string moduleName) {
std::ifstream infile(("/proc/" + std::to_string(procPid) + "/maps").c_str());
std::string line;
bool found = false;
std::string concatTemp;
while (std::getline(infile, line)) {
for (int i = 0; i <= line.length(); i++) {
concatTemp = "";
for (int h = 0; h < moduleName.length(); h++) {
concatTemp += line[i+h];
}
if (concatTemp == moduleName) { found = true; break; }
}
if (found == true) {break;}
}
concatTemp = "";
for (int i = 0; i <= line.length(); i++) {
if (line[i] == '-') { break; }
concatTemp += line[i];
}
return hexToInt("0x"+concatTemp);
}
/*
inline unsigned int getModuleSize(pid_t procPid, std::string moduleName) {
std::ifstream infile(("/proc/" + std::to_string(procPid) + "/maps").c_str());
std::string line;
std::string found[256];
int foundIter = 0;
std::string concatTemp;
while (std::getline(infile, line)) {
for (int i = 0; i <= line.length(); i++) {
concatTemp = "";
for (int h = 0; h < moduleName.length(); h++) {
concatTemp += line[i+h];
}
if (concatTemp == moduleName) { found[foundIter] = line; foundIter++; }
}
}
concatTemp = "";
std::string concatTemp2 = "";
int curIter = 1;
int i;
for (std::string s : found) {
if (s == "") { continue; }
if (curIter == 1) {
for (i = 0; i < s.length(); i++) {
if (s[i] == '-') { break; }
concatTemp += s[i];
}
}
if (curIter == foundIter) {
for (int h = i+1; h < s.length(); h++) {
if (s[h] == ' ') { break; }
concatTemp2 += s[h];
}
}
curIter++;
}
return (hexToInt("0x"+concatTemp2) - hexToInt("0x"+concatTemp));
}
*/
};