-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcess.cpp
More file actions
133 lines (104 loc) · 2.73 KB
/
Process.cpp
File metadata and controls
133 lines (104 loc) · 2.73 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
#include "Process.h"
#include <sstream>
#include <fstream>
#include <array>
#include <iostream>
#include <boost/regex.hpp>
Process::Process(int pId):
m_pId(pId)
{
loadMemoryRegions();
}
Process::~Process()
{
}
std::string Process::getMappingFilePath() const
{
std::stringstream mapFileName;
mapFileName << "/proc/" << m_pId << "/maps";
return mapFileName.str();
}
std::string Process::getMemoryFilePath() const
{
std::stringstream mapFileName;
mapFileName << "/proc/" << m_pId << "/mem";
return mapFileName.str();
}
void Process::loadMemoryRegions()
{
std::array<char, 1024> line;
boost::regex pattern("^([0-9a-zA-Z]+)-([0-9a-zA-Z]+) (r|-)(w|-)(x|-)(s|p) ([0-9a-zA-Z]+) [0-9a-fA-F]+:[0-9a-fA-F]+ [0-9]+[ \t]*(.*)");
std::string mapFilePath = getMappingFilePath();
std::ifstream fileReader(mapFilePath, std::ios::in);
m_regions.clear();
while(fileReader.getline(line.data(), 1024))
{
boost::cmatch match;
if(!boost::regex_match(line.data(), match, pattern))
{
std::cout << "Error: " << line.data() << std::endl;
}
uintptr_t startAddr;
uintptr_t endAddr;
uintptr_t fileOffset;
//Convert hex strings to integers
std::stringstream convStream;
convStream << std::hex << match[1] << " " << match[2] << " " << match[7];
convStream >> startAddr >> endAddr >> fileOffset;
Flags<MemoryRegion::ProtectionFlags> flags;
if(match[3] == "r")
{
flags |= MemoryRegion::ProtectionFlags::Read;
}
if(match[4] == "w")
{
flags |= MemoryRegion::ProtectionFlags::Write;
}
if(match[5] == "x")
{
flags |= MemoryRegion::ProtectionFlags::Execute;
}
if(match[6] == "s")
{
flags |= MemoryRegion::ProtectionFlags::Shared;
}
MemoryRegion region(startAddr, endAddr, fileOffset, flags, match[8]);
m_regions.push_back(region);
}
}
std::unique_ptr<uint8_t []> Process::readMemory(uintptr_t offset, size_t len) const
{
std::unique_ptr<uint8_t []> data = std::unique_ptr<uint8_t []>(new uint8_t[len]);
std::ifstream inStream(getMemoryFilePath(), std::ios::in | std::ios::binary);
inStream.seekg(offset);
inStream.read(reinterpret_cast<char*>(data.get()), len);
return data;
}
bool Process::isLibraryLoaded(const std::string& partialName) const
{
boost::regex pattern("partialName.*\.so");
for(const auto& region : m_regions)
{
boost::smatch match;
if(boost::regex_match(region.getName(), match, pattern))
{
return true;
}
}
return false;
}
std::vector<MemoryRegion> Process::getLibraryRegions(
const std::string& partialName) const
{
std::vector<MemoryRegion> regions;
boost::regex pattern(partialName + ".*\\.so");
for(const auto& region : m_regions)
{
boost::smatch match;
if(boost::regex_match(region.getName(), match, pattern))
{
regions.push_back(region);
}
}
return regions;
}