编译C以允许缓冲区溢出

car*_*lli 9 c compilation buffer-overflow

我正在学习缓冲区溢出,并试图制作一个.我有这个代码:

#include <stdio.h>

char *secret = "password";

void go_shell() {
    char *shell =  "/bin/sh";
    char *cmd[] = { "/bin/sh", 0 };
    setreuid(0);
    execve(shell,cmd,0);
}

int authorize() {
    char password[64];
    printf("Enter Password: ");
    gets(password);
    if (!strcmp(password,secret)) {
        return 1;
    }
    else {
        return 0;
    }
}

int main() {
    if (authorize()) {
        printf("login successful\n");
        go_shell();
    } else {
        printf("Incorrect password\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我用gcc编译它,然后在gdb中运行它

我输入大约100"A"作为密码,程序崩溃.

问题是没有寄存器被覆盖 0x4141414141414141

我用谷歌搜索并添加了-fno-stack-protector标志gcc,这允许RBP被覆盖0x4141414141414141但没有别的.

我想知道是否有一种编译代码的方法,以便可以覆盖RIP.

nwe*_*hof 3

如果您使用-fno-stack-protector. 您在 GDB 中看不到RIP值的原因是更新0x4141414141414141之前抛出了一般保护错误。RIP(如果发生页面错误,GPF 处理程序通常从交换区加载页面并从失败的指令开始恢复执行。)