【问题标题】:What is the reason to write custom GetModuleHandle function?编写自定义 GetModuleHandle 函数的原因是什么?
【发布时间】:2016-05-04 12:03:13
【问题描述】:

我在看 ZeuS 恶意软件,我遇到了这个source code

HMODULE _getKernel32Handle(void)
{
#if defined _WIN64
  return NULL; //FIXME
#else  
  __asm
  {
    cld                    //clear the direction flag for the loop

    mov edx, fs:[0x30]     //get a pointer to the PEB
    mov edx, [edx + 0x0C]  //get PEB-> Ldr
    mov edx, [edx + 0x14]  //get the first module from the InMemoryOrder module list

  next_mod:
    mov esi, [edx + 0x28]  //get pointer to modules name (unicode string)
    mov ecx, 24            //the length we want to check
    xor edi, edi           //clear edi which will store the hash of the module name

  loop_modname:
    xor eax, eax           //clear eax
    lodsb                  //read in the next byte of the name
    cmp al, 'a'            //some versions of Windows use lower case module names
    jl not_lowercase
    sub al, 0x20           //if so normalise to uppercase

  not_lowercase:
    ror edi, 13            //rotate right our hash value
    add edi, eax           //add the next byte of the name to the hash
    loop loop_modname      //loop until we have read enough

    cmp edi, 0x6A4ABC5B    //compare the hash with that of KERNEL32.DLL
    mov eax, [edx + 0x10]  //get this modules base address
    mov edx, [edx]         //get the next module
    jne next_mod           //if it doesn't match, process the next module
  };
#endif
}

逻辑如下:

  1. 读取fs段寄存器(32位Windows在那里存储TEB)
  2. 获取指向PEB的指针
  3. 获取指向PEB_LDR_DATA的指针(包含有关进程已加载模块的信息)
  4. 遍历InMemoryOrder 列表
  5. 使用自定义自制哈希函数将模块名称与"kernel32.dll" 进行比较

为什么这里不适合使用GetModuleHandle

【问题讨论】:

  • 恶意软件通常以难以从 Windows DLL 链接到外部函数的方式注入进程。可以编写这样的代码以避免必须链接到外部函数。
  • @DavidHeffernan 另一方面,依靠如此繁琐的代码可能会因为重要进程的崩溃而发现恶意软件。他的“哈希”功能不是很好,如果进程加载了很多(小)库,那么就会有很高的冲突风险。
  • 无论如何,这个问题可能是基于意见的,应该关闭,尤其是当你似乎想把它变成讨论时。
  • IInspectable 在你接受的答案中和我说的一样......
  • 链接 Windows DLL 对我来说似乎很清楚。

标签: c++ windows winapi assembly malware


【解决方案1】:

代码 sn-p 试图获取 kernel32.dll 的模块句柄(即基地址),可能是因为它还没有该模块的句柄。 GetModuleHandle 是从 kernel32.dll 导出的。当你不知道函数的地址时,你不能调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 2011-01-21
    • 2021-06-09
    • 1970-01-01
    • 2021-01-26
    • 2019-07-27
    • 2017-04-10
    相关资源
    最近更新 更多