본문 바로가기

Reversing

반디집 API 후킹

Detours를 이용하여 반디집 API를 후킹 할 수 있다.

Detours는 원본 함수의 시작 부분에 점프 명령을 삽입하여 사용자 정의 함수로 실행 흐름을 옮기고, Trampoline 함수를 통해 원본 함수의 나머지 부분을 호출할 수 있도록 하는 방식으로 작동한다.


반디집에서 압축을 해제할 때 WinAPI인 WriteFile 함수를 사용할 것이라 추측해 볼 수 있다.

확실하게 확인해 보기 위해 해당 함수가 호출될 때 printf를 통해서 메시지를 출력해 본다.

 

// dllmain.cpp : DLL 애플리케이션의 진입점을 정의합니다.
#include "pch.h"
#include <iostream>
#include <Windows.h>
#include "C:\\~~~~~\\include\\detours.h"
#pragma comment(lib, "C:\\~~~~~\\detours\\lib.X64\\detours.lib")
#define DLLBASIC_API extern "C" __declspec(dllexport)



static BOOL(WINAPI* WriteFileOrigin)(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) =
WriteFile;
static BOOL(WINAPI* CopyFileOrigin)(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, BOOL bFailIfExists) =
CopyFile;

DLLBASIC_API void myfunc() {
}


BOOL WriteFileHook(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
{
    printf("WriteFileHook called\n");


    return WriteFileOrigin(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
}

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        if (AllocConsole()) {
            AttachConsole(GetCurrentProcessId());
            freopen("CONOUT$", "w", stdout);
            freopen("CONOUT$", "w", stderr);
        }

        // Detours 라이브러리를 사용한 훅 설정
        DetourRestoreAfterWith();
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)WriteFileOrigin, WriteFileHook);
        DetourTransactionCommit();

        break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)WriteFileOrigin, WriteFileHook);
        break;
    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

 

위는 WriteFile 함수가 호출됐을 때 WriteFileHook called를 출력하는 코드이다.

 

해당 코드를 빌드한 후 아래와 같은 명령어를 입력하여 작성한 dll을 반디집에 삽입해 준다.

.\setdll.exe /d:<dll 경로> Bandizip.exe

 

그 후, 압축을 해제하게 되면 

위와 같이 제대로 출력이 되는 것을 확인할 수 있다. 

그렇다면 실제로 반디집에서는 압축을 해제할 때 WriteFile을 사용하는 것이 확실시되었다.

 

이제 파일을 압축 해제하면 계산기가 생성되도록 구현해 보자.

 

// dllmain.cpp : DLL 애플리케이션의 진입점을 정의합니다.
#include "pch.h"
#include <iostream>
#include <Windows.h>
#include "C:\\~~~~~\\detours\\include\\detours.h"
#pragma comment(lib, "C:\\~~~~~\\detours\\lib.X64\\detours.lib")
#define DLLBASIC_API extern "C" __declspec(dllexport)



static BOOL(WINAPI* WriteFileOrigin)(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) =
WriteFile;
static BOOL(WINAPI* CopyFileOrigin)(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, BOOL bFailIfExists) =
CopyFile;

DLLBASIC_API void myfunc() {
}


BOOL WriteFileHook(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
{
    printf("WriteFileHook called\n");

    // 소스와 대상 파일 경로 출력
    LPCWSTR srcFile = L"C:\\Windows\\System32\\calc.exe";
    LPCWSTR dstFile = L"C:\\HuntingMaster\\file\\hook.exe";
    printf("Copying file from %ws to %ws\n", srcFile, dstFile);

    // 파일 복사
    BOOL success = CopyFileOrigin(srcFile, dstFile, FALSE);
    if (!success) {
        DWORD error = GetLastError();
        printf("Failed to copy file. Error code: %lu\n", error);
        return FALSE;
    }
    printf("File copied successfully.\n");

    return WriteFileOrigin(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
}

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        if (AllocConsole()) {
            AttachConsole(GetCurrentProcessId());
            freopen("CONOUT$", "w", stdout);
            freopen("CONOUT$", "w", stderr);
        }

        // Detours 라이브러리를 사용한 훅 설정
        DetourRestoreAfterWith();
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)WriteFileOrigin, WriteFileHook);
        DetourTransactionCommit();

        break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)WriteFileOrigin, WriteFileHook);
        break;
    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

 

위는 이전 코드에서 CopyFile 함수를 이용하여 계산기를 압축 해제할 파일에 복사하는 기능을 추가하였다.

 

다시 한번 빌드하고 dll을 적용시킨 후, 파일을 압축 해제하게 되면

위와 같은 메시지가 뜨면서 

hook.exe라는 계산기가 생성된 것을 확인할 수 있다.

 

'Reversing' 카테고리의 다른 글

memcpy 분석  (0) 2024.07.09
Byte Ordering  (0) 2024.05.25