【问题标题】:Extract the address from a "mov rcx, qword ptr [0xAddress]" x86 instruction with C++使用 C++ 从“mov rcx,qword ptr [0xAddress]”x86 指令中提取地址
【发布时间】:2018-05-06 10:00:51
【问题描述】:

我还没有找到解决问题的方法。我想知道的是如何在 C++ 中做到这一点。

我有一个指向mov rcx, qword ptr [0xAddress] 的地址。 然后我需要找到一种方法,仅使用 C++ 从该内存位置获取 [0xAddress] 指针,而不使用内联 asm。

//I want something like this, but I don't get it working.
DWORD64 PatchAddress = FindAddressLocation(); //This finds the mov rcx, qword ptr [0xAddress]. location.
uint64_t rcx = *(volatile uint64_t*)PatchAddress;//This is supposed to give me the [0xAddress] address
*(BYTE*)(rcx) = 0;//Then write 0 to the pointer 0xAddress

【问题讨论】:

  • 它返回一个不同于 [] 内的地址:/
  • 即使在 64 位代码中,偏移量也是 32 位。您需要使用uint32_t
  • 如果PatchAddress指向指令,则mov rcx,部分首先出现,[memory_address]在一个或两个字节之后出现(查看x86指令格式了解详情)。
  • @BoPersson 我如何在 C++ 中做到这一点?
  • @gofmode - 如果PatchAddress 是内存中的特定地址,请考虑PatchAddress + 1 可能是什么。

标签: c++ pointers assembly x86-64 machine-code


【解决方案1】:

mov rcx, [a] 的通常编码是 rip-relative:

48 8b 0d DD CC BB AA

带符号的偏移量 AABBCCDD 与下一条指令相关。如果这是正在使用的编码,您的 C++ 代码应该是:

DWORD64 PatchAddress = FindAddressLocation();
uint64_t addr = PatchAddress + 7 + *(int32_t *)(PatchAddress + 3);
*(BYTE*)addr = 0;

另一种编码,不是 RIP 相对的,使用 SIB 字节:

48 8b 0c 25 DD CC BB AA

在这种情况下,地址是 32 位有符号地址。 C++ 代码将是:

DWORD64 PatchAddress = FindAddressLocation();
uint64_t addr = *(int32_t *)(PatchAddress + 4);
*(BYTE*)addr = 0;

【讨论】:

  • 那么FindAddressLocation 又是一个误导性名称,如果它返回指令的开头而不是地址部分。 OP 应该更具体。
  • @Jester,是的,一开始我对他的描述感到很困惑。但我认为这就是他想要的。
猜你喜欢
  • 2015-06-25
  • 2015-06-13
  • 2017-04-07
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多