«

ShellCode加载 - 回调函数加载

ljierui 发布于 阅读:121 技术杂谈


1、基于回调函数加载

1.1、EnumFontFamiliesExA 函数

int EnumFontFamiliesExA(
  [in] HDC           hdc, //用于枚举字体的设备上下文的句柄。
  [in] LPLOGFONTA    lpLogfont, // 指向 LOGFONT 结构的指针,其中包含要枚举的字体的相关信息。
  [in] FONTENUMPROCA lpProc, // 指向应用程序定义的回调函数的指针。 
  [in] LPARAM        lParam, // 应用程序定义值。 该函数将此值传递给回调函数以及字体信息。
       DWORD         dwFlags // 此参数未使用,必须为零。
);

1.2、通过该函数加载shellcode

#include <windows.h>
#include <iostream>

// 示例 shellcode,仅打印一条消息
void shellcode() {
    std::cout << "Shellcode executed!" << std::endl;
    ExitThread(0);  // 结束当前线程,以防止后续回调
}

// 字体枚举回调
int CALLBACK FontEnumProc(
    const LOGFONTA *lpelf,      // 指向逻辑字体结构的指针
    const TEXTMETRICA *lptm,    // 指向文本度量结构的指针
    DWORD FontType,             // 字体类型
    LPARAM lParam               // shellcode 地址
) {
    void (*func)() = (void (*)())lParam;
    func();
    return 1;  // 如果返回0,则枚举将在此处停止
}

int main() {
    LOGFONTA logFont = {0};
    logFont.lfCharSet = DEFAULT_CHARSET;  // 枚举所有字符集的字体

    // 调用 EnumFontFamiliesExA,并将 shellcode 地址作为回调函数的参数
    EnumFontFamiliesExA(GetDC(NULL), &logFont, FontEnumProc, (LPARAM)shellcode, 0);

    return 0;
}

免杀

推荐阅读: