c - 加载 execl() 函数时无法获取 root shell

标签 c buffer-overflow stack-smash

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int good(int addr) {
    printf("Address of hmm: %p\n", addr);
}

int hmm() {
    printf("Win.\n");
    execl("/bin/sh", "sh", NULL);
}

extern char **environ;

int main(int argc, char **argv) {

    int i, limit;

    for(i = 0; environ[i] != NULL; i++) 
        memset(environ[i], 0x00, strlen(environ[i]));

    int (*fptr)(int) = good;
    char buf[32];

    if(strlen(argv[1]) <= 40) limit = strlen(argv[1]);

    for(i = 0; i <= limit; i++) {
        buf[i] = argv[1][i];
        if(i < 36) buf[i] = 0x41;
    }

    int (*hmmptr)(int) = hmm;

    (*fptr)((int)hmmptr);

    return 0;

}

我在没有任何类型的堆栈保护 (gcc -fno-stack-protector -o out test.c) 的情况下以 root 身份编译了上述 C 程序,并以普通用户身份进行了利用。我未能获得 root shell。

这与我从“smashthestack”中利用的代码相同。

最佳答案

二进制 suid 是你做的吗?

以 root 身份工作:

# cd /your/working/directory/
# chmod +s ./out

如果所有堆栈粉碎保护都已关闭并且您的代码是正确的,您将获得一个 root shell。否则(如果保护已关闭且代码正确)您将只会获得一个用户外壳。

关于c - 加载 execl() 函数时无法获取 root shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8655393/

相关文章:

linux - 在启用 NX (DEP) 和 ASLR 的 x86-64 上利用基于字符串的溢出

python - 为什么我的堆栈缓冲区溢出漏洞利用不起作用?

c - 缓冲区溢出作为家庭作业

c - 允许某些进程读写目录并拒绝其余所有进程

c - 项目中是否可以隐藏声明?

c - printf api 的这种使用形式是否更安全?

c - 对于我的C作业,我不明白使用重定向来测试程序是什么意思?

c - 如何使用 pari 库和 gcc 运行 C 程序?

c++ - COM方法调用意外破坏了堆栈