x64 Native Tools Command Prompt for VS 2022 실행
git clone https://github.com/Microsoft/Detours.git
cd detours
nmakeVS 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
'Windows > Dev' 카테고리의 다른 글
| [MSVC] 클래스 고찰 & 디컴파일 (0) | 2024.08.08 | 
|---|---|
| [VC++] 문자열 복사, 이동 (0) | 2024.05.09 | 
| [Visual Studio 2022] curl 빌드 (0) | 2023.01.30 | 
| [VC++] string deallocate (0) | 2022.05.23 | 
| get EIP (gcc / vc) (0) | 2019.04.11 | 
