리눅스에서는 patchelf, 맥에서는 optool을 이용해서 라이브러리를 추가로 로드시키도록,
바이너리 실행 파일을 패치시킬 수 있었다.
그럼 윈도우는 없을까?
구글에서 찾아보니 CFF Explorer 툴로 DLL을 추가시키는 방법이 있었다.
https://github.com/ko4life-net/KOHook
Use CFF Explorer for each executable (AIServer.exe, Ebenezer.exe and KnightOnLine.exe) to add the exported functions of the custom DLLs into the executables’ Import Table by following these steps:
- Open CFF Explorer.
- Drag the target executable into the GUI.
- Click on
Import Addron the left pane and then clickAddfor adding a new module/dll. - Select the corresponding DLL for the loaded executable. e.g.
AIServer.exeneedsAIServer.dll. - In the
Exported Functionspane select all of them andImport by Oridinal. - Final step is to click
Rebuild Import Tableand thenSave.
If everything is done correctly, the modified executable should automatically load the custom DLL.
위 방법 그대로 진행하면 된다.
다만, 최소 하나 이상은 dll에서 export된 함수가 있어야 되는 것 같더라…
실제로 잘 작동하는지,
puts 함수를 후킹하는 dll 라이브러리 파일이 로드되도록 패치시켜보았다.

잘된다!
MyProcess.exe
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int main() {
printf("Hello, This is MyProcess!\n");
puts("Is hook working?");
return 0;
}
MyProcessHooker.exe
#include "framework.h"
#include "MinHook.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
static int (*orig_puts)(const char* str);
int hook_puts(const char* str) {
printf("[+] hook_puts str: %s\n", str);
return orig_puts(str);
}
__declspec(dllexport) BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
printf("[i] Loaded MyProcessHooker!!!\n");
if (MH_Initialize() != MH_OK) return false;
HMODULE ucrtModule = GetModuleHandleA("ucrtbased.dll");
LPVOID puts_func = GetProcAddress(ucrtModule, "puts");
if (MH_CreateHook(puts_func, &hook_puts, reinterpret_cast<void**>((LPVOID)&orig_puts)) != MH_OK) return false;
if (MH_EnableHook(puts_func) != MH_OK) return false;
}
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}