-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanHookTest.cpp
More file actions
53 lines (48 loc) · 1.62 KB
/
Copy pathcleanHookTest.cpp
File metadata and controls
53 lines (48 loc) · 1.62 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
#include <windows.h>
#include <stdio.h>
#include <iostream>
//TODO, check function contents against loadLibraryA first load kernel32.dll from disk
size_t GetFunctionSize(void* func) {
unsigned char* p = static_cast<unsigned char*>(func);
size_t size = 0;
while (true) {
// Common x86/x64 RET instructions: 0xC3 (RET), 0xC2 (RET imm16)
if (p[size] == 0xC3 || p[size] == 0xC2) {
return size + 1;
}
size++;
if (size > 1000) break;
}
return 0;
}
void PrintFunctionBytes(void* func, size_t length) {
unsigned char* p = static_cast<unsigned char*>(func);
printf("Function bytes: ");
for (size_t i = 0; i < length; i++) {
printf("0x%02X ", p[i]);
}
}
int main() {
void* funcAddress = reinterpret_cast<void*>(&CreateProcessA);
size_t funcSize = GetFunctionSize(funcAddress);
if (funcSize > 0) {
PrintFunctionBytes(funcAddress, funcSize);
}
else {
printf("Could not determine func size");
}
HMODULE hModule = LoadLibraryA("C:\\Windows\\System32\\kernel32.dll");
if (hModule) {
printf("Loaded copy of kernel32.dll at : %p\n", hModule);
void* funcAddress = reinterpret_cast<void*>(GetProcAddress(hModule, "CreateProcessA"));
printf("CreateProcessA address %p\n", funcAddress);
size_t funcSize = GetFunctionSize(funcAddress);
PrintFunctionBytes(funcAddress, funcSize);
printf("\nEstimated Function Size %zu\n", funcSize);
FreeLibrary(hModule);
}
else {
printf("Failed to load kernel32.dll. Error: %d\n", GetLastError());
}
return 0;
}