gpt4 book ai didi

c++ - x64 MOV、JMP 指令使程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:50:07 25 4
gpt4 key购买 nike

我正在学习如何热补丁函数,我有以下代码,它在 32 位程序中运行良好。但是,我也试图让它在 64 位程序中工作,但它只是崩溃了。

#ifndef __x86_64
std::uint8_t Store[8] = {0};
#else
std::uint8_t Store[15] = {0};
#endif

void Patch(std::uint8_t* OriginalAddress, std::uint8_t* ReplacementAddress)
{
#ifndef __x86_64
const static std::uint8_t jmp[] = {0xb8, 0x00, 0x00, 0x00, 0x00, 0xff, 0xe0}; /** movl $0x0, %eax ;;; jmp *%eax **/
#else
const static std::uint8_t jmp[] = {0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xe0, 0x90, 0x90}; /** movq $0x0, %rax ;;; jmp *%rax **/
#endif

DWORD dwProtect = 0;
const static std::int8_t jmp_size = sizeof(jmp) / sizeof(std::uint8_t);
VirtualProtect(OriginalAddress, jmp_size, PAGE_EXECUTE_READWRITE, &dwProtect);
memcpy(Store, OriginalAddress, jmp_size);
memcpy(OriginalAddress, jmp, jmp_size);
memcpy(OriginalAddress + 1, &ReplacementAddress, sizeof(void*));
VirtualProtect(OriginalAddress, jmp_size, dwProtect, &dwProtect);
}

知道代码有什么问题吗?

最佳答案

const static std::uint8_t jmp[] = {0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xe0, 0x90, 0x90};

...

memcpy(OriginalAddress + 1, &ReplacementAddress, sizeof(void*));

对于您的 x86-64 指令,要跳转到的地址,jmp 数组中的 0x0000000000000000,从第三个字节开始,而不是第二个字节。您正在覆盖 mov 指令的一部分,如果幸运的话,您最终得到的是一条无效指令。

作为旁注,我怀疑像那样覆盖 %eax/%rax 是否安全。您确定这些寄存器永远不会包含任何感兴趣的值吗?

关于c++ - x64 MOV、JMP 指令使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com