如何在内存中查找 return 指令

How to find return instruction in memory

我有一些调用函数的 C 代码。我正在 Windows 上的 visual studio 中编译这段代码。有没有直接的方法来查看 return 指令(操作码)和 return 地址? 我试图在 Visual Studio 中使用内存 window,但我只看到我的缓冲区 "blie" 和一些十六进制解释的内存值。我认为 CC 可能是一个操作码,但我想要一个 way/software 来清楚地查看 return 指令和 return 地址。

#include <stdio.h>
#include <stdlib.h>

int foo(char *);

int main(int argc, char *argv[])
{
    if (argc != 1)
        return printf("Supply an argument, dude\n");
    foo(argv[0]);
    return 0;
}

int foo(char *input)
{
    unsigned char buffer[600] = "";

    printf("Adres: %.8X\n", &buffer);
    strcpy(buffer, input);
    return 0;
}

文档摘录:

The RET instruction transfers program control from the procedure currently being executed (the called procedure) back to the procedure that called it (the calling procedure). Transfer of control is accomplished by copying the return instruction pointer from the stack into the EIP register.

如您所见,return地址在堆栈中,因此您无法在反汇编中看到它。

关于查找 return 说明 - 并不容易。您很可能使用 x86 cpu,它是具有可变长度操作码的 CISC(与 RISC 相比)。这意味着为了找到任何操作码,您必须首先 'find' 全部在它之前。

顺便说一句:您可以在 VS 中看到代码的反汇编。

return 地址位于堆栈内存区域(由 rsp 寄存器指向,假设您在 x86_64 上),而执行函数的代码 return 位于代码存储区。 如果您想查看 return 地址,请在 RET 指令处停止进程并查看堆栈顶部。

如果您只想查看生成的代码,可以使用反汇编程序。当您使用 Windows 时,您可以尝试开源 x64dbg. Other options exist, such as IDA Pro and you can view a list of others in this question: https://reverseengineering.stackexchange.com/questions/1817/is-there-any-disassembler-to-rival-ida-pro