Linux 系统中有时会遇到需要将部分用户的活动范围限制在某个特定区域的需求,jail工具包
Jailkit
可以解决相关问题,本文记录使用方法。
wget https://olivier.sessink.nl/jailkit/jailkit-2.23.tar.gz
tar zxvf jailkit-2.23.tar.gz
cd jailkit-2.23
./configure && make && make install
如果报错:
-bash: make: command not found
需要安装几个包
apt install gcc
apt install automake
apt install make
我们需要建立一个目录来存放所有受限环境的配置。目录随便放在什么地方
指定 jail 环境目录,以
/opt/jail
为例
sudo mkdir /opt/jail
sudo chown root:root /opt/jail
设置在受限环境中可用的程序,根据允许用户使用什么命令来初始化环境,任何程序想要在受限环境中执行则必须用jk_init命令拷贝到目录中。
代码如下:
sudo jk_init -v /opt/jail basicshell
sudo jk_init -v /opt/jail editors
sudo jk_init -v /opt/jail extendedshell
sudo jk_init -v /opt/jail netutils
sudo jk_init -v /opt/jail ssh
sudo jk_init -v /opt/jail sftp
sudo jk_init -v /opt/jail jk_lsh
或结合成一个命令,例如:
jk_init -v -j /opt/jail basicshell editors extendedshell netutils ssh sftp scp jk_lsh
# 或
jk_init -v -j /opt/jail sftp scp jk_lsh netutils extendedshell
# 或
jk_init -v /opt/jail netutils basicshell jk_lsh openvpn ssh sftp
其中像basicshell
, editors
, netutils
是一些组名,其中包含多个程序。复制到jail shell中的每个组都是可执行文件、库文件等的集合。
jk_lsh (Jailkit limited shell) - 这是一个重要的部分,必须添加到受限环境中。
完整的程序列表设置,可以在/etc/jailkit/jk_init.ini中查看。
比如basicshell就在jail提供有bash, ls, cat, chmod, mkdir, cp, cpio, date, dd, echo, egrep等程序。
命令执行完成后,会看到/jail下生成了 很多类似SHELL的文件
/opt/jail# ls
bin dev etc lib lib64 root usr
开启 jailkit 服务
service jailkit start
sudo adduser robber
目前创建的是一个在实际文件系统中的普通用户,并没有添加到受限环境中。
/etc/passwd
文件,你会在文件最后看到跟下面差不多的一个条目。robber:x:1006:1005:,,,:/home/robber:/bin/bash
这是我们新创建的用户,最后部分的/bin/bash指示了这个用户如果登入了那么它可以在系统上正常的Shell访问
sudo jk_jailuser -m -j /opt/jail/ robber
执行上列命令后,用户robber将会被限制。
robber:x:1006:1005:,,,:/opt/jail/./home/robber:/usr/sbin/jk_chrootsh
/opt/jail
(受限环境)中。用户的Shell是一个名叫jk_chrootsh
的特殊程序,会提供Jailed Shell。jk_chrootsh这是个特殊的shell,每当用户登入系统时,它都会将用户放入受限环境中。
到目前为止受限配置已经几乎完成了。但是如果你试图用ssh连接,那么注定会失败,像这样:
# ssh robber@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:B81LxQ3aPl/0Gz0taJGAdJrV5TgZmX8hzEqrzmCwbSI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
robber@localhost's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-90-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Connection to localhost closed.
连接会立马关闭,这意味着用户已经活动在一个受限制的shell中。
下个重要的事情是给用户在限制环境中的一个正确的bash shell。
/opt/jail/etc/passwd
:root:x:1:0:root:/root:/bin/bash
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
robber:x:1000:1000:,,,:/home/robber:/usr/sbin/jk_lsh
/usr/sbin/jk_lsh
改为/bin/bash
root:x:1:0:root:/root:/bin/bash
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
robber:x:1000:1000:,,,:/home/robber:/bin/bash
保存文件并退出。
现在让我们再次登入受限环境
# ssh robber@localhost
robber@localhost's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-90-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
Last login: Mon Jan 10 20:58:39 2022 from 127.0.0.1
bash: groups: command not found
robber@8196b962a2db:~$
受限环境中的根目录实际就是真实文件系统中的/opt/jail
。但这只有我们自己知道,受限用户并不知情。
而且只有我们通过jk_cp拷贝到jail中的命令能使用。
robber@8196b962a2db:~$ touch test
robber@8196b962a2db:~$ ls
test
(base) root@8196b962a2db:/opt/jail/home/robber# ls
test
现在配置已经完成了。可以在限制/安全的环境里运行程序或服务。要在限制环境中启动一个程序或守护进程可以用jk_chrootlaunch
命令。
$ sudo jk_chrootlaunch -j /opt/jail -u robber -x /some/command/in/jail
jk_chrootlaunch
工具可以在限制环境中启动一个特殊的进程同时指定用户特权。
如果守护进程启动失败,请检查/var/log/syslog/错误信息。
在限制环境中运行程序之前,该程序必须已经用jk_cp命令复制到jail中。