-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcMemory.cpp
189 lines (149 loc) · 4.24 KB
/
cMemory.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
#include "cMemory.h"
cMemory_t cMemory::_instance = nullptr;
cMemory_t cMemory::Get()
{
if (_instance == nullptr)
_instance = std::make_shared<cMemory>();
return _instance;
}
cMemory::cMemory()
{
if (_logger == nullptr)
_logger = cLogger::Get();
}
cMemory::~cMemory()
{
if (_processHandle != INVALID_HANDLE_VALUE)
CloseHandle(_processHandle);
}
void cMemory::DumpAllProcesses()
{
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (snapshot != INVALID_HANDLE_VALUE)
{
size_t counter = 0;
_logger->PrintVar("BEGIN PROCESS DUMP");
{
PROCESSENTRY32 entry = {};
entry.dwSize = sizeof(PROCESSENTRY32);
char szProcessName[MAXCHAR];
Process32First(snapshot, &entry);
do
{
std::wcstombs(szProcessName, entry.szExeFile, sizeof(szProcessName));
std::string msg = std::to_string(++counter)
+ " - Name:" + szProcessName
+ " - ID:" + std::to_string(entry.th32ProcessID)
+ " - Parent ID:" + std::to_string(entry.th32ParentProcessID);
_logger->PrintVar(msg);
} while (Process32Next(snapshot, &entry));
}
_logger->PrintVar("PROCESS DUMP COMPLETE", true);
}
}
void cMemory::DumpModulesFromProcess(const DWORD& processID)
{
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processID);
if (snapshot != INVALID_HANDLE_VALUE)
{
size_t counter = 0;
_logger->PrintVar("BEGIN MODULE DUMP");
{
MODULEENTRY32 entry = {};
entry.dwSize = sizeof(MODULEENTRY32);
char szModulePath[MAXCHAR];
char szModuleName[MAXCHAR];
Module32First(snapshot, &entry);
do
{
std::wcstombs(szModulePath, entry.szExePath, sizeof(szModulePath));
std::wcstombs(szModuleName, entry.szModule, sizeof(szModuleName));
std::string msg = std::to_string(++counter)
+ " - Name:" + szModuleName
+ " - Path:" + szModulePath;
_logger->PrintVar(msg);
msg = std::to_string(counter)
+ " - Address:" + std::to_string((DWORD64)entry.modBaseAddr)
+ " - Size:" + std::to_string((DWORD)entry.modBaseSize)
+ " - Module ID:" + std::to_string((DWORD)entry.th32ModuleID)
+ " - Process ID:" + std::to_string((DWORD)entry.th32ModuleID);
_logger->PrintVar(msg);
} while (Module32Next(snapshot, &entry));
}
_logger->PrintVar("MODULE DUMP COMPLETE");
}
}
std::string cMemory::GetCurrentProcessName() const
{
return _processName;
}
DWORD cMemory::GetCurrentProcessID() const
{
return _processID;
}
HANDLE cMemory::GetCurrentProcessHandle() const
{
return _processHandle;
}
bool cMemory::IsProcessAttatched() const
{
return _attatched;
}
bool cMemory::AttatchProcess(const std::string& processName)
{
PROCESSENTRY32 entry = {};
entry.dwSize = sizeof(PROCESSENTRY32);
char convertedName[MAXCHAR];
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (snapshot != INVALID_HANDLE_VALUE)
{
Process32First(snapshot, &entry);
do
{
std::wcstombs(convertedName, entry.szExeFile, sizeof(convertedName));
if (!processName.compare(convertedName))
{
_processName = processName;
_processID = entry.th32ProcessID;
_processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, _processID);
CloseHandle(snapshot);
_logger->PrintVar("Process " + processName + " HANDLE found. ID:" + std::to_string(_processID), true);
_attatched = true;
return true;
}
} while (Process32Next(snapshot, &entry));
}
_logger->PrintVar("Process " + processName + " not found", true);
CloseHandle(snapshot);
return false;
}
BYTE* cMemory::GetModuleAddress(const std::string& moduleName)
{
MODULEENTRY32 entry = {};
entry.dwSize = sizeof(MODULEENTRY32);
char _moduleName[MAXCHAR];
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, _processID);
if (snapshot != INVALID_HANDLE_VALUE)
{
if (Module32First(snapshot, &entry))
{
std::wcstombs(_moduleName, entry.szModule, sizeof(_moduleName));
if (!moduleName.compare(_moduleName))
{
CloseHandle(snapshot);
return entry.modBaseAddr;
}
while (Module32Next(snapshot, &entry))
{
std::wcstombs(_moduleName, entry.szModule, sizeof(_moduleName));
if (!moduleName.compare(_moduleName))
{
CloseHandle(snapshot);
return entry.modBaseAddr;
}
}
}
}
CloseHandle(snapshot);
return nullptr;
}