3 min read

RootME Script Writeup 题目 Bash-System1

RootME Script Writeup 题目 Bash-System1

题目内容:

题目链接如下:
https://www.root-me.org/en/Challenges/App-Script/ELF32-System-1#validation_challenge
靶机中示例代码如下

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
 
int main(void)
{
    setreuid(geteuid(), geteuid());
    system("ls /challenge/app-script/ch11/.passwd");
    return 0;
}

题目考察点很明确,就是 linux 系统中 ruid, euid 和 suid 的相关概念和安全用法,其中 ruid 表示真实用户 id,即一旦用户登录该 ruid 不可更改。euid 为有效用户id, linux 系统利用 euid 标记系统各种资源的权限分配,即 linux 权限检查系统会检查euid 从而确定权限。ruid 和 euid 的分开代表了用户的权限在系统中是可变更的。最后suid 用于设置用户的 euid,当 ruid 的用户执行了某个带有 suid 的程序时,自身的euid 会由 euid 变更为 suid 指示的其他 euid 权限,从而进行了权限变更。关于不同uid最直接的应用就是 /etc/shadow 文件,及其对应的 passwd 程序,以下是三种不同uid的解释:

  • RUID, 用于在系统中标识一个用户是谁,当用户使用用户名和密码成功登录后一个UNIX系统后就唯一确定了他的RUID.

  • EUID, 用于系统决定用户对系统资源的访问权限,通常情况下等于RUID。

  • SUID,用于对外权限的开放。跟RUID及EUID是用一个用户绑定不同,它是跟文件而不是跟用户绑定。

题解:

app-script-ch11@challenge02:~$ ls -al
total 36
dr-xr-x---  2 app-script-ch11-cracked app-script-ch11 4096 Dec 10  2021 .
drwxr-xr-x 24 root                    root            4096 Jun  9  2022 ..
-r--------  1 root                    root             775 Dec 10  2021 ._perms
-rw-r-----  1 root                    root              43 Dec 10  2021 .git
-r--------  1 app-script-ch11-cracked app-script-ch11   14 Dec 10  2021 .passwd
-r--r-----  1 app-script-ch11-cracked app-script-ch11  494 Dec 10  2021 Makefile
-r-sr-x---  1 app-script-ch11-cracked app-script-ch11 7252 Dec 10  2021 ch11
-r--r-----  1 app-script-ch11-cracked app-script-ch11  187 Dec 10  2021 ch11.c

检查靶机环境可以看到 ch11 可执行文件的属住为 app-script-ch11-cracked,但app-script-ch11 具有执行权限,当 app-script-ch11 执行时会通过 setreuid 方法将执行的 euid 变更为 app-scipt-ch11-cracked 用户的 euid,可以看到 .passwd 文件权限如下:

app-script-ch11@challenge02:~$ ls -al /challenge/app-script/ch11/.passwd
-r-------- 1 app-script-ch11-cracked app-script-ch11 14 Dec 10  2021 /challenge/
app-script/ch11/.passwd

因此一旦 ls 命令被某个程序替换,则 ch11 会使用 app-script-ch11-cracked 用户的读权限访问该 .passwd 秘钥文件。
所以接下来的做法是,首先在 /tmp/ 目录创建个人目录 /tmp/test
其次将 /bin/cat 命令拷贝并重命名到 /tmp/test/ls
最后将 /tmp/test 作为 PATH 路径的一部分写到当前 PATH 路径之前
最后执行 ch11 程序,由于 PATH 路径被覆盖,程序会首先从 /tmp/test 目录下寻找 ls 命令,由于 cat 命令被重命名为 ls 命令,因此实际执行的是 "cat /challenge/app-script/ch11/.passwd",从而输出本道题目 flag

SUID 安全影响

SUID 安全性参考

Dangers of SUID Shell Scripts (Administration/Unix)

SUID Privileged Programs

Tip 1: Never use C shell in suid script

Tip 2: A lways manually set t he PA TH and use absolut e pat h names.

Tip 3: Understand how the programs in your script work.

Tip 4:Don't use temporary files! If you must use temporary files, don't put them in a publicly writable area.

Tip 5:Distrust and check all user input, and strip out any meta-characters.

Tip 6:Always manually set your (Internal Field Separator)IFS.

Tip 7:Don't use SUID shell scripts.