Hai phương pháp nhúng tệp tin vào tệp EXE

Phương pháp 1: Nhúng driver dưới dạng mảng byte vào tệp thực thi

[Yêu cầu]

  • Sử dụng Visual Studio 2012, tạo dự án win32 Console rỗng
  • Thiết lập cấu hình Release cho bản build
  • Cấu hình thuộc tính dự án: C/C++ → Tạo mã → Thư viện chạy → Chọn Multi-threaded (/MT)
  • Tắt tùy chọn tạo thông tin debug trong Linker → Debug → Tạo thông tin debug

[Lợi ích] Tạo tệp EXE có kích thước nhỏ nhất có thể và loại bỏ các thông tin debug như đường dẫn biên dịch khi phân tích ngược.

[Quy trình thực hiện]

  1. Dùng WinHex mở file driver cần nhúng, chọn toàn bộ nội dung → Sao chép thành mã nguồn C
  2. Lưu kết quả vào file header "resource_data.h" với cấu trúc mảng byte

resource_data.h

unsigned char embedded_driver[3584] = {
    0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
    0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ... (dữ liệu driver)
};

main.cpp

#include <windows.h>
#include <shlwapi.h>
#include "resource_data.h"
#pragma comment(lib, "shlwapi.lib")

int extractEmbeddedFile(LPCTSTR output_path) {
    HANDLE hOutput = CreateFile(output_path, 
        GENERIC_WRITE, 
        FILE_SHARE_READ, 
        NULL, 
        CREATE_ALWAYS, 
        FILE_ATTRIBUTE_NORMAL, 
        NULL);
    
    if (hOutput == INVALID_HANDLE_VALUE) {
        printf("Lỗi tạo file: %x\n", GetLastError());
        return -1;
    }

    DWORD bytesWritten;
    if (!WriteFile(hOutput, embedded_driver, sizeof(embedded_driver), &bytesWritten, NULL)) {
        printf("Lỗi ghi file: %x\n", GetLastError());
        CloseHandle(hOutput);
        return -2;
    }
    
    CloseHandle(hOutput);
    return 0;
}

int main() {
    extractEmbeddedFile(TEXT("C:\\Windows\\System32\\drivers\\mydriver.sys"));
    return 0;
}

Phương pháp 2: Nhúng DLL dưới dạng tài nguyên vào EXE

[Chức năng]

  • DLL: Hiển thị hộp thoại và mở Word bằng lệnh CMD
  • EXE: Gắn DLL vào tiến trình explorer.exe

[Quy trình triển khai]

  1. Thêm các tập tin nguồn cần thiết (.cpp, .h) vào dự án
  2. Trong Solution Explorer:
  • Chọn Thêm → Tài nguyên → Nhập → Chọn tập tin DLL
  • Tạo tài nguyên mới với ID: DLL_RESOURCE
  1. Bổ sung header: #include "resource.h"
  2. Triển khai hàm load tài nguyên:
#include <windows.h>
#include <tlhelp32.h>
#include "resource.h"

HMODULE loadEmbeddedResource() {
    HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(DLL_RESOURCE), RT_RCDATA);
    HGLOBAL hGlobal = LoadResource(NULL, hRes);
    void* pResData = LockResource(hGlobal);
    DWORD resSize = SizeofResource(NULL, hRes);

    HANDLE hFile = CreateFile("temp.dll", 
        GENERIC_WRITE, 
        0, 
        NULL, 
        CREATE_ALWAYS, 
        FILE_ATTRIBUTE_NORMAL, 
        NULL);
    
    WriteFile(hFile, pResData, resSize, &bytesWritten, NULL);
    CloseHandle(hFile);
    
    return LoadLibrary("temp.dll");
}

[Kết quả]

Thẻ: Windows API Resource Embedding DLL Injection PE File Structure Binary Manipulation

Đăng vào ngày 14 tháng 6 lúc 07:41