This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalma.cpp
More file actions
86 lines (64 loc) · 2.53 KB
/
alma.cpp
File metadata and controls
86 lines (64 loc) · 2.53 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
// alma v1.1
#include "alma.hpp"
#include <fstream>
#include <sstream>
char memPath[64];
char mapsPath[64];
unsigned int currentPID;
std::vector<unsigned char> alma::memRead(const memAddr address, const unsigned int count) {
std::ifstream mem(memPath, std::ios::in | std::ios::binary);
std::vector<unsigned char> retVec(count);
mem.seekg(address);
mem.read(reinterpret_cast<char*>(&retVec[0]), count);
return retVec;
}
void alma::memWrite(const memAddr address, const std::vector<unsigned char> values) {
std::ofstream mem(memPath, std::ios::out | std::ios::binary);
mem.seekp(address);
mem.write(reinterpret_cast<const char*>(&values[0]), values.size());
}
std::vector<memoryPage> alma::getMemoryPages(const int targetPerms, const int excludedPerms) {
std::vector<memoryPage> retVec;
std::ifstream maps_file(mapsPath);
std::string line;
// Iterate through each line
while (std::getline(maps_file, line)) {
std::istringstream sstream(line);
memAddr pageBegin, pageEnd;
char dash;
char pagePerms[4];
// Parse the line
sstream >> std::hex >> pageBegin >> dash >> pageEnd >> pagePerms;
memoryPage page {
memoryPermission_NONE,
pageBegin,
pageEnd
};
if (pagePerms[0] == 'r') page.permissions |= memoryPermission_READ;
if (pagePerms[1] == 'w') page.permissions |= memoryPermission_WRITE;
if (pagePerms[2] == 'x') page.permissions |= memoryPermission_EXECUTE;
if (pagePerms[3] == 's') page.permissions |= memoryPermission_SHARED;
if (pagePerms[3] == 'p') page.permissions |= memoryPermission_PRIVATE;
// Check for excluded permissions
if (page.permissions & excludedPerms)
continue;
// If target permissions are not specified, push back every page.
if (targetPerms == memoryPermission_NONE)
retVec.push_back(page);
// If target permissions specified, only push back pages that meet the filter
else if (targetPerms & page.permissions)
retVec.push_back(page);
}
return retVec;
}
bool alma::openProcess(const unsigned int pid) {
snprintf(memPath, 64, "/proc/%i/mem", pid);
snprintf(mapsPath, 64, "/proc/%i/maps", pid);
std::ifstream existsCheck(memPath, std::ios::in | std::ios::binary);
if (existsCheck.is_open())
currentPID = pid;
return currentPID == pid;
}
unsigned int alma::getCurrentlyOpenPID() {
return currentPID;
}