编写自定义GetModuleHandle函数的原因是什么?

时间:2016-05-04 12:03:14

标签: c++ windows winapi assembly malware

我正在查看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. 的指针
  4. 获取指向PEB_LDR_DATA的指针(包含有关已加载的流程模块的信息)
  5. 遍历InMemoryOrder列表
  6. 使用自定义自制哈希函数
  7. 将模块名称与"kernel32.dll"进行比较

    为什么GetModuleHandle不适合使用?

1 个答案:

答案 0 :(得分:4)

代码片段试图获取 kernel32.dll 的模块句柄(即基地址),可能是因为它还没有这个模块的句柄。从{em> kernel32.dll 导出GetModuleHandle。当你不知道它的地址时,你不能打电话给你。