使用 chroot 创建沙盒环境

使用 chroot 创建沙盒环境

chroot 提供了更改当前进程及其子进程的可见根目录的操做,运行在此隔离环境中的应用程序没法访问新的目录树以外的文件和命令。这样的隔离环境称做 chroot 监狱chroot jail)。一般,chroot 用于创建沙盒环境,以及在单用户模式或救援模式下进行系统维护或重置密码等操做。
更多资料参见 chroot - ArchWikiphp

Usage

chroot 命令的语法以下:linux

chroot [option] newroot [command [args]]
# 以 ~/tmp 为新的根目录,执行 /bin/bash
# 这里 /bin/bash 是新目录下的路径
chroot ~/tmp /bin/bash

资源准备

显然,仅仅使用 mkdir 命令建立一个空目录就想在其中执行 chroot 进程是不可行的,由于咱们尚未在新的目录下准备必要的资源。首先咱们须要把 shell 工具装入该目录——固然,若是只是运行某一条命令,确实没必要为其准备终端工具。此外,为了顺利执行大部分终端操做,咱们还应将 /usr/bin 目录下的必要文件及其依赖库复制到新的根目录下,一般包括 ls/mkdir/mv/cp 等基本命令和其余一些可能须要用到的命令。git

mkdir ~/tmp/{bin,lib,lib64}
cp /bin/* ~/tmp/bin -r
cp /lib/* ~/tmp/lib -r
cp /lib64/* ~/tmp/lib64 -r

除此以外,在/dev 目录下的一些必要节点有时也须要手动创建:github

mkdir ~/tmp/dev
cd ~/tmp/dev
mknod -m 666 null c 1 3
mknod -m 666 tty c 5 0
mknod -m 666 zero c 1 5
mknod -m 666 random c 1 8

注:shell

  1. 要查看一个可执行文件的依赖库,执行 ldd 命令,如 ldd /bin/sh
  2. 请结合使用 chown/chmod 命令保证这些文件和目录的所属和权限与原文件系统一致。
  3. 这里不可以使用软连接,由于在新的根目录下,软连接没法定位到外部原文件。
  4. 除了用于本地主机的维护外,chroot 监狱还可用于限制 SSH 用户根目录。

例题

题目来源:USTC Hackergame 2019
有一天,C 同窗作了一个梦,他居然搬进了大房子,只是彷佛有些地方 C 同窗不太满意……
注意:bash

  1. 此题考察的是对于 Linux 基础知识的掌握。尽管能够,但不建议使用逆向工程的方式完成。
  2. 在根目录(/)下的文件夹对 Linux 系统的运行十分重要,请不要为了完成此题目删除本身的 /usr/bin 等文件夹!

附件请从官方 GitHub 页面下载源代码编译。app

这里我以 Alpine Linux 为例,简要介绍此题的一种解法。dom

下载题目源文件并简要分析

结合所给提示,使用 curl/wget 等工具下载题目附件,用 readelf/objdump 等工具分析得知该文件是一个 Unix 可执行文件,OK 先给上可执行权限并运行一下:curl

# 下载
wget http://url/IWantAHome
# 查看 elf 文件头
readelf -h IWantAHome
# 添加可执行权限
chmod +x IWantAHome
# 执行
./IWantAHome

输出:工具

$ ./IWantAHome
I just want a home. Please do what I say and I will give you a flag
Make sure I am running on Linux(Unix).
I want these directories in / : [/Kitchen /Lavatory /Bedroom /Living_Room]
Oh I can not find /Kitchen ,goodbye.

看来要建立这些目录,建立完再运行:

$ mkdir /Kitchen /Lavatory /Bedroom /Living_Room
$ ./IWantAHome
I just want a home. Please do what I say and I will give you a flag
Make sure I am running on Linux(Unix).
I want these directories in / : [/Kitchen /Lavatory /Bedroom /Living_Room]
Thanks, I find these directories.
I hate these directories  [/home /root /boot /proc /sys /etc /bin] , Please delete them all!
Oh I found /home ,goodbye.

得,这是想让我删系统呢。

创建沙盒

删系统?抱歉,今天不行!咱们直接挑个好地方创建起一个 newroot 目录,好比在 /root 下创建 tmp,而后先将 /bin 下的文件复制进 /root/tmp 中(注,这里要求删除 /bin 目录,故复制到 tmp/b 目录下):

# 建立目录
mkdir /root/tmp
mkdir /root/tmp/b
# 复制文件
cp /bin/* /root/tmp/b
cp IWantAHome /root/tmp
# 查看 /bin/sh 的依赖
ldd /bin/sh
# 复制依赖库
cp /lib /root/tmp -r
# 顺便把刚才创建的 Kitchen 等目录移进去
mv /Kitchen /Lavatory /Bedroom /Living_Room tmp

进入沙盒环境并再次执行文件:

# 进入 chroot 环境
chroot /root/tmp /b/sh
# 更改环境变量
export HOME=/
export PATH=/b
# 执行文件
./IWantAHome

这时程序输出表示要在 /Bedroom/Microphone 中写入文件并从 /Bedroom/Headset 中读出,这个容易,新建其中一个文件,而后在另外一个位置创建一个连接就行。

# 创建新文件
touch /Bedroom/Microphone
# 创建符号连接(硬连接也可)
ln -s /Bedroom/Microphone /Bedroom/Headset

写时间脚本

再运行程序,提示要从 /Living_Room/Clock 中读取到 20:15:30 形式的北京时间,那只好写一个后台脚本不断向文件中写入当前时间……因此我选择先退出 chroot 环境(亦可直接在 chroot 进程内执行),先设置一下时区。

# 安装 tzdata 包以提供时区信息
apk add tzdata
# 在 /etc 下创建 /usr/share/zoneinfo/Asia/Shanghai 的连接
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 查看时间是否准确(如不许确,使用 date -s "20:15:30" 命令更改)
date
# 新建 time.sh 文件并赋予可执行权限
touch time.sh && chmod +x time.sh  
# 编辑 time.sh
vi time.sh

写入如下脚本:

#!/bin/sh
while true
do
    date +"%H:%M:%S" > /root/tmp/Living_Room/Clock
    sleep 0.5
done

并让它在后台运行:

./time.sh &

从新进入 chroot 环境,设置好环境变量,再次运行 IWantAHome

$ ./IWantAHome 
# 略去部分输出
Time is important, I need a clock in living_room
I will read  Beijing time (eg: '20:15:30') in /Living_Room/Clock
Good, the clock works well.
It is late, tell me how to sleep 10 seconds in shell
>

要求输入 “sleep 10 seconds” 的命令……嗯,输入 sleep 10。这里有两个点,一是 sleep 命令并不必定须要用 shell 指令,能够本身写一个定时的程序;二是这里要用到一个 /dev/null 的设备,这个设备能够用 mknod -m 666 /dev/null c 1 3 建立,也能够只创建一个普通文件——反正附件程序不检查。

最终输出

完成这些操做后,再执行 IWantAHoome

$ ./IWantAHome 
I just want a home. Please do what I say and I will give you a flag
Make sure I am running on Linux(Unix).
I want these directories in / : [/Kitchen /Lavatory /Bedroom /Living_Room]
Thanks, I find these directories.
I hate these directories  [/home /root /boot /proc /sys /etc /bin] , Please delete them all!
Well done.
Now I want a telephone in Bedroom
I will write something to /Bedroom/Microphone and read the same thing in /Bedroom/Headset
Good, telephone works well.
Time is important, I need a clock in living_room
I will read  Beijing time (eg: '20:15:30') in /Living_Room/Clock
Good, the clock works well.
It is late, tell me how to sleep 10 seconds in shell
> sleep 10
command is:'sleep 10'
I slept for  10.00281629s
flag{I_am_happy_now}

至此,flag 到手!

相关文章
相关标签/搜索