我正在寻找一种方式来扫描程序的特定模式记忆。 在程序加载我们的代码库( .so
)。
这里是我的尝试:
unsigned long FindPattern(char *pattern, char *mask)
{
void *address;
unsigned long size, i;
// NULL = We want the base address of the process we are loaded in
address = dlopen(NULL, 0); // Would be GetModuleHandle(NULL) on Windows
// The size of the program, would be GetModuleInformation.SizeOfImage on Windows
size = 0x128000; // Didn't find a way for Linux
for(i = 0; i < size; i++)
{
if(_compare((unsigned char *)(address + i), (unsigned char *)pattern, mask))
return (unsigned long)(address + i);
}
return 0;
}
int _compare(unsigned char *data, unsigned char *pattern, char *mask)
{
for(; *mask; ++mask, ++data, ++pattern)
{
if(*mask == 'x' && *data != *pattern) // Crashes here according to gdb
return 0;
}
return (*mask) == 0;
}
但是,所有的这不起作用。 在开始dlopen
,它不返回我们在加载程序的正确的基址。我也曾尝试link_map作为解释在这里 。 我知道从IDA地址和GDB这就是为什么我知道dlopen
返回错误的值。
使用在CentOS 6.5 64位GCC-4.4.7。 该计划是一个32位二进制可执行文件。