使用Jailkit在CentOS上建立一个受限shell

使用Jailkit在CentOS上建立一个受限shell

1. 关于 Jailkit

来自Jailkit官方的介绍:Jailkit Offical Site
Jailkit is a set of utilities to limit user accounts to specific files using chroot() and or specific commands. Setting up a chroot shell, a shell limited to some specific command, or a daemon inside a chroot jail is a lot easier and can be automated using these utilities.

关于Jailkit的应用:
Jailkit is known to be used in network security appliances from several leading IT security firms, internet servers from several large enterprise organizations, internet servers from internet service providers, as well as many smaller companies and private users that need to secure cvs, sftp, shell or daemon processes.

2. 在CentOS上安装Jailkit

目前最新版:03-01-2014: Jailkit 2.17,下载地址为: download

下载jailkit

# wget http://olivier.sessink.nl/jailkit/jailkit-2.17.tar.gz

官方提供的jailkit-2.17.tar.gz的md5值为:

7b5a68abe89a65e0e29458cc1fd9ad0b

校验md5,我们下载的jailkit-2.17.tar.gz的md5和官方提供的md5一致。文件未被篡改。

# md5sum  jailkit-2.17.tar.gz 
7b5a68abe89a65e0e29458cc1fd9ad0b  jailkit-2.17.tar.gz

安装gcc和make

# yum install -y gcc make

解压

# tar zxf jailkit-2.17.tar.gz

编译安装jailkit

# cd jailkit-2.17
# ./configure 
# make
# make install

现在Jailkit已经安装完成了。Jailkit有许多命令可以用来设置一个基于chroot的受限环境,如下是这些命令:

# jk_
jk_addjailuser   jk_chrootlaunch  jk_cp            jk_jailuser      jk_lsh           jk_update
jk_check         jk_chrootsh      jk_init          jk_list          jk_socketd

3. 配置Jailed shell

3.1 配置受限环境

我们需要建立一个目录来存放所有受限环境的配置。目录随便放在什么地方,比如我们可以创建个/cache1/jail的目录。

# mkdir -p /cache1/jail

这个目录应为Root所有。用chown改变属主。

# chown root:root /cache1/jail

3.2 jk_init 设置需要在受限环境中运行的命令

我们可以通过jk_init命令来初始化jail环境中需要的命令。
jk_init 会自动在指定的chroot目录下创建Linux目录结构和把需要的库文件拷贝到对应目录。

# jk_init -v /cache1/jail netutils basicshell jk_lsh ssh id

像basicshell, editors, netutils是一些组名,其中包含多个程序。复制到jail shell中的每个组都是可执行文件、库文件等的集合。

比如basicshell就在jail提供有bash, ls, cat, chmod, mkdir, cp, cpio, date, dd, echo, egrep等程序。

完整的程序列表设置,定义在/etc/jailkit/jk_init.ini中。

[basicshell]
comment = bash based shell with several basic utilities
paths = /bin/sh, bash, ls, cat, chmod, mkdir, cp, cpio, date, dd, echo, 
        egrep, false, fgrep, grep, gunzip, gzip, ln, ls, mkdir, mktemp, more, 
        mv, pwd, rm, rmdir, sed, sh, sleep, sync, tar, touch, true, uncompress,
        zcat, /etc/motd, /etc/issue, /etc/bash.bashrc, /etc/bashrc, 
        /etc/profile, /usr/lib/locale/en_US.utf8
users = root
groups = root
includesections = uidbasics

jk_lsh (Jailkit limited shell)

jk_lsh是一个重要的部分,必须添加到受限环境中。
简单来说jk_lsh就是jailkit的受限shell,它不是用于和用户交互的shell。

jk_lsh只是执行在启动时传递的命令,类似:/bin/sh -c command

关于jk_lsh具体参考:jk_lsh 帮助文档

3.3 创建将被jailed 的用户

3.3.1 创建将被jailed 的用户

创建用户robber,并设置密码

# useradd robber
# passwd robber

PS:
目前创建的是一个在实际文件系统中的普通用户,并没有添加到受限环境中。
在下一步这个用户会被放到受限环境里。
这时候如果你查看/etc/passwd文件,你会在文件最后看到跟下面差不多的一个条目。

# grep robber /etc/passwd
robber:x:506:508::/home/robber:/bin/bash

这是新创建的用户robber,最后部分的/bin/bash
用户robber如果登入系统,所使用的shell是bash shell

3.3.2 jk_jailuser 将用户加入jail中

jk_jailuser 常用的命令参数如下

-j --jail=jail
    **The jail directory to use.** This jail should exist, 
    and should have at least <jail dir>/etc/passwd
-v --verbose
    Will give verbose output
-m --move
    **Move the contents of the home directory inside the jail.** 
    If in interactive mode, jk_jailuser will ask if the directory should be 
    moved if that is necessary. 
    In non-interactive mode this option is disabled by default.
-s --shell=shell
    The shell to use inside the jail. Defaults to /usr/sbin/jk_lsh

jk_cp的具体使用说明可以查看在线帮助文档: jk_jailuser 帮助文档

使用jk_jailuser命令,将用户robber加入jail

# jk_jailuser -m -j /cache1/jail robber

执行上列命令后,用户robber将会被限制。

如果现在再观察/etc/passwd文件,会发现类似下面的最后条目。

# grep robber /etc/passwd
robber:x:506:508::/cache1/jail/./home/robber:/usr/sbin/jk_chrootsh

PS:
最后两部分表明用户主目录和shell类型已经被改变了。
现在用户的主目录在/cache1/jail(受限环境)中。
用户的Shell是一个名叫jk_chrootsh的特殊程序,会提供Jailed Shell。
jk_chrootsh这是个特殊的shell,每当用户登入系统时,它都会将用户放入受限环境中。

到目前为止受限配置已经几乎完成了。但是试图用ssh连接,那么注定会失败,像这样:

# ssh robber@10.1.1.4
robber@10.1.1.4's password: 
Last login: Tue Sep 15 17:11:24 2015 from 10.1.1.3
Connection to 10.1.1.4 closed.

因为jk_lsh不是交互shell。
所以下面我们要修改jail环境(chroot环境)中的/etc/passwd中的用户shell

3.3.3 给在jail中的用户指定Bash Shell

在jail中的password文件中,把/usr/sbin/jk_lsh改为/bin/bash

# sed -i 's:/usr/sbin/jk_lsh:/bin/bash:' /cache1/jail/etc/passwd

NOTICE:
在3.3.2节中,在jk_jailuser的时候直接指定shell为/bin/bash,
这样可以省去手工修改$jaildir/etc/passwd。
我们把robber1用户加入jail,并指定登录shell为/bin/bash (/bin/bash其实是$jaildir/bin/bash)

# jk_jailuser -m -j /cache1/jail -s /bin/bash robber1

现在观察/cache1/jail/etc/passwd中robber1的登录shell为/bin/bash

# grep robber1 /cache1/jail/etc/passwd
robber1:x:518:518::/home/robber1:/bin/bash

3.4 通过ssh登录jailkit chroot环境

让我们再次登入受限环境

# ssh robber@10.1.1.4
robber@10.1.1.4's password:
Last login: Tue Sep 15 16:31:55 2015 from 10.1.1.3

[robber@Katie ~]$ whoami
bash: whoami: command not found

[robber@Katie ~]$ id
uid=506(robber) gid=508(robber) groups=508(robber)

如果登入失败,请检查一下/var/log/audit/audit.log的错误信息。

ubuntu /var/log/auth.log
centos /var/log/audit/audit.log

3.5 jk_cp 拷贝需要在受限环境中运行的命令

jk_cp命令的描述:

jk_cp will copy any file into a jail on the identical location, with identical permissions and (if required) including any required libraries.
It will remove any set user id (setuid) or set group id (setgid) permissions from all files and directories copied..

jk_cp的具体使用说明可以查看在线帮助文档:jk_cp 帮助文档
PS
jk_cp 操作拷贝的时候会去掉文件原本的setuid和setgid。

jk_cp的常用参数如下:

-j --jail
    The destination jail.
    If no jail is specified, the first argument is used as jail for backwards compatibility
-f --force
    Force overwriting of existing files
-v --verbose
    Will give verbose output

jk_cp 至少需要指定一个chroot directory 和 一个或多个需要拷贝的文件或命令。

jk_cp -j chroot_dir file1 [file2]

如果在chroot下需要id命令,可以使用如下命令解决:

# jk_cp -vj /cache1/jail id

4. 在jail限制环境中运行程序或服务

4.1 jk_chrootlaunch

现在配置已经完成了。可以在限制安全的环境里运行程序或服务。
要在限制环境中启动一个程序或守护进程可以用jk_chrootlaunch命令。
参考:jk_chrootlaunch 帮助文档

# jk_chrootlaunch -j /cache1/jail -u robber -x /some/command/in/jail

jk_chrootlaunch工具可以在限制环境中启动一个特殊的进程同时指定用户特权。

如果守护进程启动失败,请检查/var/log/messages错误信息。

ubuntu /var/log/syslog
centos /var/log/messages

在限制环境中运行程序之前,该程序必须已经用jk_cp命令复制到jail中。

jk_cp - 将文件包括权限信息和库文件复制到jail的工具 

4.2 在jailkit中运行Apache

Apache在jailkit中运行可以参考:

jk_chrootlaunch 帮助文档
Configuring an Apache Jail with Jailkit in CentOS 5.x
使用Jailkit建立apache 的chroot环境

5. jailkit的其他命令

5.1 jk_list 查看chroot jail中的所有进程

jk_list will show all processes that are running in a chroot jail, which jail they are running in, and which user is running the process.

# jk_list 
Pid    User    Jail         Command                                   
1584   robber  /cache1/jail /cache1/jail/bin/bash                     
1572   robber1 /cache1/jail /cache1/jail/bin/bash                     
1578   robber1 /cache1/jail /cache1/jail/usr/bin/ssh robber@localhost

6. 自动化脚本

创建测试用户create_account1.sh,创建account1.txt中指定的用户,设置用户名和密码相同

#!/bin/bash
if [ ! -f account1.txt ]; then
echo "account1.txt does not exist. one account per line."
exit 1
fi

usernames=$(cat account1.txt)

for username in $usernames; do
useradd $username
echo $username | passwd --stdin $username
done

用户名存放在account1.txt中,每行一个用户名。

将account1.txt中的用户加入jail,jk_account1.sh

<code class="bash" data-origin="
#!/bin/bash
if [ ! -f account1.txt ]; then
echo "account1.txt does not exist. one account per line."
exit 1
fi

usernames=$(cat account1.txt)
jaildir="/cache1/jail"

for username in $usernames; do
jk_jailuser -m -j $jaildir -s /bin/bash $username
done

" style="border: 0px; display: block;font-family: Consolas, Inconsolata, Courier, monospace; font-weight: bold; white-space: pre; margin: 0px;border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); padding: 0px 5px; margin: 0px 2px;font-size: 1em; letter-spacing: -1px; font-weight: bold;">#!/bin/bash
if [ ! -f account1.txt ]; then
echo "account1.txt does not exist. one account per line."
exit 1
fi

usernames=$(cat account1.txt)
jaildir="/cache1/jail"

for username in $usernames; do
jk_jailuser -m -j $jaildir -s /bin/bash $username
done

参考资料

[1]. 在Ubuntu下用jailkit建立一个受限Shell
[2]. jailkit官网
[3]. Install Jailkit on Ubuntu/Debian
[4]. Setup a jailed shell with jailkit on ubuntu
[5]. Configuring an Apache Jail with Jailkit in CentOS 5.x
[6]. 使用Jailkit建立apache 的chroot环境

posted @ 2016-01-05 21:42  seerjk  阅读(1452)  评论(0编辑  收藏  举报