Windows/Dev
detours 빌드 및 적용
Codetronik
2024. 4. 11. 11:51
x64 Native Tools Command Prompt for VS 2022 실행
git clone https://github.com/Microsoft/Detours.git
cd detours
nmake
VS 2022로 Detours\vc\Detours.sln 오픈 후, x64, x86 각각 빌드
VS에서 새 DLL 프로젝트를 만들고, 해당 디렉토리에 아래의 파일을 복사
Detours\lib.X??\*.lib
Detours\include\*.h
속성 -> 링커 -> 입력 -> 추가 종속성에 아래의 파일을 추가
detours.lib
syelog.lib
아래와 같이 후킹 코드를 작성하고 빌드한다.
#include "pch.h"
#include <stdio.h>
#include "detours.h"
HANDLE(WINAPI* OrgCreateFileW)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE) = CreateFileW;
HANDLE WINAPI MyCreateFileW(
_In_ LPCWSTR lpFileName,
_In_ DWORD dwDesiredAccess,
_In_ DWORD dwShareMode,
_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
_In_ DWORD dwCreationDisposition,
_In_ DWORD dwFlagsAndAttributes,
_In_opt_ HANDLE hTemplateFile
)
{
wprintf(L"%s\n", lpFileName);
return OrgCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}
BOOL Start()
{
AllocConsole();
FILE* fp;
freopen_s(&fp, "CONOUT$", "w", stdout);
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)OrgCreateFileW, MyCreateFileW);
DetourTransactionCommit();
return TRUE;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Start();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
아래의 DLL Injector로 테스트가 가능하다.
GitHub - TarekExister/UWP-Dll-Injector-32bit-64bit: universal windows platform (uwp) apps Dll injector [32bit-64bit]
universal windows platform (uwp) apps Dll injector [32bit-64bit] - TarekExister/UWP-Dll-Injector-32bit-64bit
github.com