【发布时间】:2016-05-18 01:43:49
【问题描述】:
我目前正在尝试调用这个名为“super_secret_function”的函数,该函数在文件 main.c 中的 main 之外定义。我正在使用 ASM,无法使用 jmp 或 call 来访问此“super_secret_function”。 main 内部是一个名为 stack_hack 的函数,我可以通过该函数更改地址以达到超级机密函数。
使用 GDB,我已经能够确定“super_secret_function”的地址,并通过从函数中跳转到它来成功调用它。如何操作指针的返回值以返回该地址?
.globl stack_hack
stack_hack:
pushq %rbp # push the base pointer on the stack
movq %rsp, %rbp # move the previous stack pointer to the new base poi
##MyCode
movq $0x00000000004005b4, %rbp
jmp *%rbp
##EndMyCode
movq %rbp, %rsp # move the stack pointer to the base pointer
popq %rbp # pop the base pointer and load it into %rbp
ret # pop the instruction pointer into %rip
【问题讨论】:
-
什么是“指针的返回值”?
-
至少,
ret从堆栈中获取一个值并使用它来更改 IP。您如何更改堆栈上的内容,是否可以利用它将您 to 带到某个地方,而不是 从 某个地方返回?提示:实际上并没有你想象的那么难。 :) -
非常感谢。我将目的地移动到 %rax 中,在弹出 rbp 后,我将 %rax 推入堆栈并返回
-
不要这样做。它搞砸了返回地址预测器堆栈,并将降低您的性能。只需使用 call/jmp 指令。 CPU 知道这意味着什么,并针对该场景进行了优化。
-
@RaymondChen 我很确定这是家庭作业,而不是实际练习;性能很可能不是问题。 :)