使用cout

时间:2016-04-09 23:36:14

标签: c++ winapi x86 hook inline-assembly

我正在尝试使用其地址挂钩函数,到目前为止整体工作正常!

唯一的问题是当将std::cout与WinAPI挂钩的MessageBoxA结合使用时,它会崩溃!奇怪的是,它只会在特定情况下崩溃,而不是与printfint i = MessageBoxA(...);32一起调用

为了进行测试,我做了这样,函数地址的指令直接返回// mov eax, 32 // ret const DWORD instructionCount = 6; BYTE instructions[instructionCount] = { 0xB8, 0x20, 0x00, 0x00, 0x00, 0xC3 }; 我知道的钩子并不多,但这仅用于测试。

VirtualProtect()

除了必须更改memcpy(MessageBoxA, instructions, instructionCount); 区域的保护之外,那么 现在我基本上只做

int i = MessageBoxA(NULL, "Hello World", "Injector", MB_OK);

printf("Works: %d\n", i);
printf("Works: %d\n", MessageBoxA(NULL, "Hello World", "Injector", MB_OK));

std::cout << "Works: " << i << std::endl;
std::cout << "Doesn't: " << MessageBoxA(NULL, "Hello World", "Injector", MB_OK) << std::endl;

printf("Hello World\n");

现在使用它进行测试:

std::cout << MessageBoxA(...)

然后它在32之后崩溃。删除该行,一切正常!

请注意,它成功打印__declspec(noinline) int testFunction(int i) { return i; } ,在到达下一个语句时崩溃。

Application Crash

同样只是在那种情况下它不起作用,所以使用它:

MessageBoxA

然后重复使用上面的代码并将testFunction更改为jsonData = """{ "products" : { "DQ578CGN99KG6ECF" : { "sku" : "DQ578CGN99KG6ECF", "productFamily" : "Compute", "attributes" : { "location" : "US East (N. Virginia)", "instanceType" : "hs1.8xlarge", "tenancy" : "Shared", "operatingSystem" : "Windows", "licenseModel" : "License Included", "preInstalledSw" : "NA" } }, "G2N9F3PVUVK8ZTGP" : { "sku" : "G2N9F3PVUVK8ZTGP", "productFamily" : "Instance", "attributes" : { "location" : "Asia Pacific (Seoul)", "instanceType" : "i2.xlarge", "tenancy" : "Host", "operatingSystem" : "Windows", "licenseModel" : "License Included", "preInstalledSw" : "SQL Server Enterprise" } }, "FBZZ2TKXWWY5HZRX" : { "sku" : "FBZZ2TKXWWY5HZRX", "productFamily" : "Compute", "attributes" : { "location" : "Asia Pacific (Seoul)", "instanceType" : "i2.4xlarge", "tenancy" : "Dedicated", "operatingSystem" : "SUSE", "licenseModel" : "No License required", "preInstalledSw" : "NA" } } } }""" (以及参数),现在所有4个语句都有效!

最重要的是,在特定情况下,是否有任何人对于导致崩溃的原因和原因有什么想法?当其他案件完美无缺时。

1 个答案:

答案 0 :(得分:3)

我认为问题是您正在破坏堆栈指针。请尝试使用以下内容:

const DWORD instructionCount = 8; BYTE instructions[instructionCount] = { 0xB8, 0x20, 0x00, 0x00, 0x00, 0xC2, 0x10, 0x0 };

如同彼得·科德斯所提到的那样,这将会从堆栈中弹出。希望有所帮助。

相关问题