编写自定义GetModuleHandle函数的原因是什么
What is the reason to write custom GetModuleHandle function?
我看到了ZeuS恶意软件,我发现了这段源代码:
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
}
逻辑如下:
- 读取
fs
段寄存器(32位Windows在其中存储TEB) - 获取指向
PEB
的指针 - 获取指向
PEB_LDR_DATA
的指针(包含进程已加载模块的信息) - 遍历
InMemoryOrder
列表 - 使用自定义自制散列函数将模块名称与
"kernel32.dll"
进行比较
为什么GetModuleHandle
的使用不合适?
代码片段正试图获取kernel32.dll的模块句柄(即基地址),可能是因为它还没有该模块的句柄。GetModuleHandle是从kernel32.dll导出的。当您不知道函数的地址时,无法调用它。
相关文章:
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 是什么让放置新调用对象的构造函数?
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 在函数中拥有函数原型的目的是什么?
- 使用基类指针调用基类的值构造函数的语法是什么?
- C++:使用方法调用析构函数的顺序是什么?
- 将此布尔值传递给此函数的最有效方法是什么?
- * 和 ** 在 C++ 函数声明中是什么意思?
- C++:Lambda 函数指针转换的用例是什么?
- C++关于指针和使用函数将它们启动到堆的行为究竟是什么?
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 在将函数声明为友元时,尖括号的含义是什么?
- 在模板类之外定义友元函数的正确方法是什么?
- 在使用包含冒号的类似函数的宏时,是什么导致了这种编译器差异?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 这里的字符串函数是什么意思
- 传递 lambda 函数的权衡是什么?
- 在C 中,对2D数组的增量是什么?函数断言(0)做什么?