-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptoCop.cpp
More file actions
271 lines (219 loc) · 7.74 KB
/
Copy pathcryptoCop.cpp
File metadata and controls
271 lines (219 loc) · 7.74 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma comment(lib, "detours/detours.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "Shlwapi.lib")
#include <mutex>
#include <vector>
#include <sstream>
#include <string>
#include <unordered_set>
#include <windows.h>
#include <shlwapi.h>
#include <Tlhelp32.h>
#include <psapi.h>
#include "detours/detours.h"
#include "cryptocop.h"
#define LOG(msg) \
do { \
std::ostringstream oss; \
oss << msg << std::endl; \
storeMsg(oss.str()); \
} while(0);
namespace {
constexpr int THRESHOLD = 100;
constexpr int HIGH = 20;
constexpr int LOW = 1;
const std::string LOG_PREFIX("\\\\%SHARED_FOLDER_PATH%\\report-");
}
HANDLE handleLogFile = NULL;
std::mutex mutex_;
int maliciousScore = 0;
std::unordered_set<std::string> fileSet;
const std::vector<std::string> highProtectedFolders = { "C:\\%USER_HOME%\\Desktop", "C:\\%USER_HOME%\\Documents", "C:\\%USER_HOME%\\Pictures" };
const std::vector<std::string> nonProtectedFolders = { "C:\\%USER_HOME%\\AppData", "C:\\Windows", "C:\\Program Files" };
const std::string getTime();
void storeMsg(std::string & msg);
bool isSuspicious(const std::string& filePath);
void gracefulExit();
BOOL WINAPI Fake_WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) {
char filePath[MAX_PATH];
DWORD dwRet = GetFinalPathNameByHandle(hFile, filePath, sizeof(filePath), FILE_NAME_NORMALIZED);
if (dwRet >= MAX_PATH || dwRet == 0)
filePath[0] = '\0';
{
std::unique_lock<std::mutex> lock(mutex_);
if (isSuspicious(filePath)) {
gracefulExit();
}
}
return Real_WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpReserved) {
switch (Reason) {
case DLL_PROCESS_ATTACH: {
DisableThreadLibraryCalls(hModule);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)Real_WriteFile, Fake_WriteFile);
DetourTransactionCommit();
LOG(getTime() << " [ATTACHED]");
break;
}
case DLL_PROCESS_DETACH: {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)Real_WriteFile, Fake_WriteFile);
DetourTransactionCommit();
LOG(getTime() << " [DETACHED]");
break;
}
case DLL_THREAD_ATTACH: // fallthrough
case DLL_THREAD_DETACH: // fallthrough
default:
break;
}
return TRUE;
}
const std::string getTime() {
unsigned __int64 currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
return std::to_string(currentTime);
}
std::string getExecName() {
char szExeName[MAX_PATH];
char* pszExeName = szExeName;
GetModuleFileNameA(0, szExeName, MAX_PATH);
PCHAR psz = szExeName;
while (*psz) {
psz++;
}
while (psz > szExeName && psz[-1] != ':' && psz[-1] != '\\' && psz[-1] != '/') {
psz--;
}
pszExeName = psz;
while (*psz && *psz != '.') {
psz++;
}
*psz = '\0';
return std::string(pszExeName);
}
void storeMsg(std::string& msg) {
if (handleLogFile == NULL || handleLogFile == INVALID_HANDLE_VALUE) {
std::ostringstream logfile;
logfile << LOG_PREFIX << getExecName() << "-" << GetCurrentProcessId() << ".dll";
handleLogFile = CreateFile(logfile.str().c_str(), FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
DWORD dwBytesWritten = 0;
Real_WriteFile(handleLogFile, msg.c_str(), msg.size(), &dwBytesWritten, NULL);
}
bool isInFolders(const std::string& filePath, const std::vector<std::string>& folders) {
const auto pos = filePath.find_first_not_of(":?/\\");
const char *begin = filePath.c_str() + pos;
for (const auto & folder : folders) {
if (PathIsPrefix(folder.c_str(), begin)) {
return true;
}
}
return false;
}
void recalculateMaliciousScore(const std::string& filePath) {
if (isInFolders(filePath, nonProtectedFolders)) {
LOG(getTime() <<" [NONPROTECTED] " << filePath);
return;
}
bool isHighProtected = isInFolders(filePath, highProtectedFolders);
auto result = fileSet.insert(filePath);
if (result.second) {
maliciousScore += isHighProtected ? HIGH : LOW;
LOG(getTime() << (isHighProtected ? " [HIGH] " : " [LOW] ") << maliciousScore << " "<<filePath);
}
}
bool isSuspicious(const std::string& filePath) {
recalculateMaliciousScore(filePath);
if (maliciousScore < THRESHOLD)
return false;
LOG(getTime() << " [MAXWRITE_EXCEEDED]");
return true;
}
std::string getProcessName(DWORD procID) {
HANDLE myProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (!myProc) {
LOG(getTime() << " " << procID << " Open Process Failed. Return empty as name. Error Code:" << GetLastError());
return std::string();
}
char buffer[MAX_PATH];
std::string result;
if (GetProcessImageFileName(myProc, buffer, MAX_PATH)) {
char* basename = strrchr(buffer, '\\');
result.assign(basename ? basename + 1: buffer);
} else {
LOG(getTime() << " " <<procID << " Process Name is assigneed empty string. Error Code:" << GetLastError());
}
CloseHandle(myProc);
return result;
}
void KillProcess(DWORD procID) {
LOG(getTime() << " [KILL PROCESS] " << procID);
HANDLE hChildProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (hChildProc) {
::TerminateProcess(hChildProc, 1);
::CloseHandle(hChildProc);
}
}
void __fastcall KillProcessTree(DWORD procID, DWORD killerID){
LOG(getTime() << " [KILL PROCESS TREE] " << getProcessName(procID) << ":" << procID << " KILLER PROCESS:" << killerID);
// this cant happen but just to be safe
if (!procID) return;
//do not kill self now.
if (procID == killerID) return;
PROCESSENTRY32 pe;
memset(&pe, 0, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
BOOL flag = ::Process32First(hSnap, &pe);
while(flag) {
if (pe.th32ParentProcessID == procID) {
KillProcessTree(pe.th32ProcessID, killerID); //recursion
}
flag = ::Process32Next(hSnap, &pe);
}
KillProcess(procID);
}
// todo : getParentProcess should consider start times in case PIDs are reused by OS
// pass start time as parameter, if I am younger than my child then I am not the parent :)
DWORD getParentProcess(DWORD procID) {
std::string exeName = getProcessName(procID);
LOG(getTime() << " [PARENT PROCESS SEARCH] " << exeName << ":" << procID);
if (strcmp(exeName.c_str(), "cmd.exe") == 0 || strcmp(exeName.c_str(), "explorer.exe") == 0 ||
strcmp(exeName.c_str(), "services.exe") == 0 || strcmp(exeName.c_str(), "wininit.exe") == 0)
{
LOG(getTime() << " Parent is CMD||Explorer||Services||wininit. Accepted 0.");
return 0;
}
PROCESSENTRY32 pe;
memset(&pe, 0, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
BOOL flag = ::Process32First(hSnap, &pe);
while (flag) {
if (pe.th32ProcessID == procID) {
if (pe.th32ParentProcessID == 0 || pe.th32ParentProcessID == 4) {
return procID;
}
LOG(getTime() << " Parent: " << pe.th32ParentProcessID << " Child: " << procID);
DWORD parentId = getParentProcess(pe.th32ParentProcessID);
return parentId ? parentId : procID;
}
flag = ::Process32Next(hSnap, &pe);
}
LOG(getTime() << " Parent not exist " << exeName << " : "<< procID);
return procID;
}
void gracefulExit() {
LOG(getTime() << " [EXIT_PROCESS]");
DWORD currentProcId = GetCurrentProcessId();
DWORD parentProcId = getParentProcess(currentProcId);
KillProcessTree(parentProcId, currentProcId);
LOG(getTime() << " [KILL MAIN]");
CloseHandle(handleLogFile);
KillProcess(currentProcId);
}