`
cppmule
  • 浏览: 449727 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Creating chroot sftp Jails with Jailkit

 
阅读更多

Creating chroot sftp Jails with Jailkit

<iframe style="border-style: none; overflow: hidden; width: 250px; height: 55px;" src="http://www.facebook.com/plugins/like.php?href=www.linuxjournal.com/content/creating-chroot-sftp-jails-jailkit&amp;layout=standard&amp;show_faces=false&amp;width=250&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=55" frameborder="0" scrolling="no"></iframe>
<iframe id="I0_1366041017164" style="position: static; top: 0px; width: 106px; margin: 0px; border-style: none; left: 0px; visibility: visible; height: 24px;" title="+1" name="I0_1366041017164" src="https://plusone.google.com/_/+1/fastbutton?bsv&amp;hl=en-US&amp;origin=http%3A%2F%2Fwww.linuxjournal.com&amp;url=http%3A%2F%2Fwww.linuxjournal.com%2Fcontent%2Fcreating-chroot-sftp-jails-jailkit&amp;ic=1&amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.CN4PBoybVBw.O%2Fm%3D__features__%2Fam%3DQQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAItRSTNbg_Pvkm_eupJzRD3A6aE99f73eA#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled&amp;id=I0_1366041017164&amp;parent=http%3A%2F%2Fwww.linuxjournal.com&amp;rpctoken=35808805" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="100%" data-gapiattached="true"></iframe>

One of the things I both love and hate about my job is getting assigned new projects. They can be about anything and everything. A few months back I was given an assignment to create some chroot jails for a group of customers so that they could securely upload files with sftp. The requirement was that the customers needed to be able to upload file, but in a secure and private way. Customer One should not be able to see Customer Two's files, for example. And neither customer should be able to browse the filesystem of the server. I was also asked to define a process whereby our support staff could add new jails as needed.

I've used the chroot command many times, it's a very handy way of fixing servers that can't otherwise be fixed --- like when no one can remember the root password on some box that no one has touched for years. Simply boot the server with a live CD (Knoppix is a personal favorite, I have it loaded on a usb key that I carry with me everywhere), mount the main drive and then issue a command along the lines of:


chroot /mnt/hda1

Suddenly it's like you are root on the local filesystem and not on the live CD. I can then change passwords, run programs, and generally get myself into trouble. :-)

You can do a lot more with chroot than simply use it as part of your system recovery toolkit though. Chroot is _very_ useful on a day-to-day basis for jailing applications and users that should (or need) to be isolated from the rest of the system (and from each other).

To create a jail you create a folder that has a replication of the directory structure of a normal Linux box. The difference is that you only copy in the bare minimum of what you need into the directory structure. The way you find out what libraries and files an application needs is by using ldd or strace. If I can get away with it, I prefer using ldd since its output is much more readable, but there are times when both are needed to track down elusive libraries.

As an example, lets say I want to have a jail that contains the bash shell, just that and nothing more. It's a stupid example, but very easy.

I first need to create the directory that will house the jail:


daniel@bart:~$ mkdir /opt/jail

It's just an empty directory now, so we now need to add some apps to it. Bash lives in the /bin directory so I first recreate the /bin directory and then copy the bash binary into it like so:


daniel@bart:~$ mkdir /opt/jail/bin
daniel@bart:~$ cp /bin/bash /opt/jail/bin/

Then I run ldd on bash to find out what libraries it uses like so:


daniel@bart:~$ ldd /bin/bash
linux-gate.so.1 => (0xffffe000)
libncurses.so.5 => /lib/libncurses.so.5 (0xb7ec2000)
libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7ebe000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7d7d000)
/lib/ld-linux.so.2 (0xb7f1f000)
daniel@bart:~$

I can ignore the linux-gate.so.1 file, it's a virtual object created by the Linux Kernel, but I need the others so I create the directories and then copy them over:


daniel@bart:~$ mkdir -p /opt/jail/lib/tls/i686/cmov
daniel@bart:~$ cp /lib/libncurses.so.5 /opt/jail/lib/
daniel@bart:~$ cp /lib/tls/i686/cmov/libdl.so.2 /opt/jail/lib/tls/i686/cmov/
daniel@bart:~$ cp /lib/tls/i686/cmov/libc.so.6 /opt/jail/lib/tls/i686/cmov/
daniel@bart:~$ cp /lib/ld-linux.so.2 /opt/jail/lib/

Now I can test the jail like so:


daniel@bart:~$ sudo chroot /opt/jail /bin/bash
bash-3.2#

The presence of the bash-3.2# prompt tells me I was successful.

This is a pretty useless jail right now, with only bash. I can 'cd' and 'pwd' and 'exit' and do other bash built-in commands, but I can't 'ls' or 'less' or 'cp' or 'mv' or anything else that is external to bash. Adding more binaries is the same as adding bash: copy the binary in and then ldd it to find its libraries and then copy them in. A lot of programs use the exact same libraries, so often you'll find that copying the binary in to an existing jail is all you need to do since the libraries will already be there.

Once I copy in a few binaries this could turn into a very nice sandbox. However, I had some specific needs when I started out building custom jails, and there are a few gotchas that the above jail doesn't come close to meeting:

1. Users assigned to a jail need to be automatically chrooted into their jail when they login
2. There should be no shell access --- we don't want them to be able to ssh in, they should only be able to use sftp and scp
3. This needs to be done yesterday, and it needs to be repeatable.

Number one is easy enough in theory, just create a false shell whose sole purpose is to chroot you into your jail. Yeah. Easy. Oh, and do it securely with no shortcuts that might open up security holes. Yup, definitely easy.

Number two actually is easy in theory and practice, just copy in the sftp and scp binaries and leave out the ssh binary and any shell binaries.

Number three is the real kicker. I needed a quick, repeatable process. Especially since once things are up and running the plan was to turn everything over to the support group so that they could create new jails as needed.

So what did I do? I went looking for something someone had already built and I found Jailkit.

There are other tools out there for automatically creating jails, so please do some searching and explore all options when choosing your own solution. Some of the things I like about jailkit are:

1. It has many pre-configured collections that you can mix and match to build the perfect jail. I call them jail-sets.
2. If none of the existing jail-sets meets your needs you can customize them or create new ones.
3. Creating multiple jails is easy and each jail can have multiple users assigned to it --- users can't belong to more than one jail though, but that's a good thing IMO.
4. Adding users to jails is very easy, almost to the point of "that's all there is to it?"
5. The fake shell that Jailkit uses to chroot users when they log in checks for potential security risks every time it is run and will disconnect the user if it notices anything bad (like the jail directory being setuid root, for example).
6. The Jailkit website has clear, easy to follow directions that walk you through several potential jail setups and it has a good troubleshooting section to help you when things don't work for some reason or other.

Here's what I did to create the jails on my server:

As far as the actual server is concerned, I didn't do much to it apart from installing a base ubuntu-server OS with the ssh-server option. Oh, and I also installed the build-essential meta-package so that I could compile and install Jailkit.

After downloading the tarball from the Jailkit website, Jailkit is installed from source like so:


daniel@bart:~$ wget http://olivier.sessink.nl/jailkit/jailkit-2.5.tar.gz
daniel@bart:~$ tar -zxvf jailkit-2.5.tar.gz
daniel@bart:~$ cd jailkit-2.5
daniel@bart:~/jailkit-2.5$ ./configure
daniel@bart:~/jailkit-2.5$ make
daniel@bart:~/jailkit-2.5$ sudo make install

The whole unpack-compile-install process is really quick. By default, Jailkit installs its binaries into /usr/sbin/ and the configuration and template files into /etc/jailkit/.

Once installed, creating jails is easy. As I said above, Jailkit comes with several default jail-sets. These are defined in /etc/jailkit/jk_init.ini and they include things like scp, sftp, apache, basicshell, and so on. As far as learning about what jail-sets are available and what each jail-set includes, the best thing I can say is to look through the /etc/jailkit/jk_init.ini file and familiarize yourself with them. This file is also where you can define your own custom sets.

You create a jail using the jk_init script and specifying where you want the jail to be located and what jail-sets you want included in the jail. The example below is what I use to create a jail that has sftp and scp functionality:


daniel@bart:~$ sudo jk_init -v /path/to/jail sftp scp jk_lsh

The jk_lsh jail-set provides a special shell that limits the binaries it will execute via a config file. That way, even if a user uploads their own binary file, they won't be able to execute it.

The jail at this point (assuming nothing went wrong) is technically a valid jail, but there aren't any users in it yet and because this jail has no 'shell', it is pretty useless to try and chroot into it manually to test it. The next order of business therefore is to create some users and add them to the jail. Once a user has been created and added to a jail using the method below, they will always be put in that shell whenever they log in. So you absolutely need to create special jail-only users. Do not put regular users into jails!


daniel@bart:~$ sudo groupadd companyname
daniel@bart:~$ sudo useradd -g companyname -m username
daniel@bart:~$ sudo passwd username
daniel@bart:~$ sudo jk_jailuser -m -j /path/to/jail username

The reason I create a group is because I am creating jails per client company on this server and it made sense to group (no pun intended) all the users that way. I also threw in the passwd command into the example above since the useradd command (on Ubuntu at least) doesn't set a password by default. Things may be different on your Linux distro of choice.

The jk_jailuser utility, when run as in the example above, changes the shell of an existing user to jk_chrootsh in the /etc/passwd file and it changes the home directory to /path/to/jail/./home/username. It also adds the user to a stripped down passwd file located at /path/to/jail/etc/passwd and adds the user's groups to a stripped down group file located at /path/to/jail/etc/group.

If you want to edit the two passwd files yourself you can do so. Just change the user's line in /etc/passwd to something resembling this:


username:x:1010:1010::/path/to/jail/./home/username:/usr/sbin/jk_chrootsh

and change the user's line in /path/to/jail/etc/passwd to something resembling this:


username:x:1010:1010::/home/username:/usr/sbin/jk_lsh

(I'm afraid of messing things up if I do it manually, so I use the jk_jailuser utility whenever possible.)

Now we need to allow the user to execute the scp and sftp programs. Edit the /path/to/jail/etc/jailkit/jk_lsh.ini file and add in some lines like so:


[group companyname]
paths= /usr/bin, /usr/lib/
executables= /usr/bin/scp, /usr/lib/sftp-server, /usr/lib/openssh/sftp-server, /usr/libexec/sftp-server

That's all there is to it. I now have a fully functioning jail and I can test it by connecting to the server using sftp or scp. When I sftp in and look around, I only see the directories below the jail directory, exactly the behavior I wanted.

Should you find later on that the jail needs to have some other binary in it, Jailkit also includes a handy copy command that you can use to copy additional files into jails like so:


daniel@bart:~$ sudo jk_cp /path/to/jail /path/to/command

Very simple, and it handles all of the locating of dependencies, libraries, and whatnot. The only gripe I have about the jk_cp command is that you have to specify jk_cp<source></source>instead of doing it the way the cp command works, which is cp <source></source>.

On occasion I have run into issues where Jailkit doesn't copy over everything properly, or it simply misses some libraries. In those cases I had to experiment with copying over additional libraries. Rather than reinvent the wheel, I'll just point you to the Jailkit website, which has a very nice page on troubleshooting jails. (it's in the links)

Once I had jailkit working on my jail server and I had a repeatable process for adding users, my next step was to create two _very_ simple shell scripts for creating jails and creating jailed users. These scripts are used by our support staff, who have limited sudo access to run only these scripts as root along with the passwd command (so that they can change jailed users' passwords, if need be).

The two scripts are called mkjail and jailuser. They're not all that pretty, and they could be considered dangerous if used by the wrong people. But for internal use by a select group of specifically authorized and trained people, they work fine.

With the server set up and the scripts in place and the staff trained my job was done. The server is under constant use and new jails are created whenever they are needed with no intervention needed on my part. Of course, the minute the project was completed I was immediately assigned three more . . . but that's what's great about working in technology, right? It's never boring, that's for sure. :-)

Admittedly, creating an sftp jail is probably not something that everyone will need to do, but jailkit has found its way into my sysadmin bag of tricks. Whenever I need to isolate a user or a process into a sandbox, and a Xen virtual machine is way too much overkill, the first thing I reach for is jailkit.

分享到:
评论

相关推荐

    linux系统创建SFTP用户及权限限制实战步骤

    接下来,需要为SFTP用户配置一个chroot环境,这样用户登录后就只能访问该环境下的目录。 ```bash # mkdir /chroot # chown root:root /chroot # 如果使用root用户创建,这一步可以省略 # chmod 755 /chroot ``` ...

    Centos配置SFTP用户

    - 对于SFTP用户,使用`chroot`指令限制其只能在其主目录下活动,通过指定`ChrootDirectory`来实现。 - 设置SFTP用户的密码,并通过SSH登录测试用户权限。 4. 权限控制: - “Sftponly”表示用户仅拥有SFTP服务的...

    用ChrootDirectory限制SFTP登录的用户只能访问指定目录且不能进.docx

    "使用 ChrootDirectory 限制 SFTP 登录的用户只能访问指定目录且不能进行 SSH 登录" 在 Linux 系统中,使用 ChrootDirectory 可以限制 SFTP 登录的用户只能访问指定目录且不能进行 SSH 登录。下面是实现这一功能的...

    Centos下ssh以及sftp的配置以及权限设置

    - `ChrootDirectory /data/sftp/%u` 使用 chroot 将用户的根目录指定到 `/data/sftp/%u`,其中 `%u` 代表用户名。 - `ForceCommand internal-sftp` 指定使用 SFTP 命令。 - `AllowTcpForwarding no` 和 `X11...

    sftp一键安装脚本

    8. **SFTP chroot**:通过配置`sshd_config`,可以将用户限制在他们的家目录,防止他们访问系统其他部分。 通过使用这个一键安装脚本,用户可以避免手动执行这些步骤,简化了安装过程,同时减少了出错的可能性。...

    Linux中chroot的使用.pdf

    "Linux中chroot的使用" Linux 操作系统中,chroot 是一个非常重要的命令,用于改变当前的根目录,以限制某些进程或用户的访问权限。今天,我们将详细介绍 Linux 中 chroot 的使用,包括其原理、应用场景和实践步骤...

    sftp服务器.zip

    SFTP(Secure File Transfer Protocol)是一种安全的文件传输协议,常用于在远程主机之间安全地传输文件。在本文中,我们将深入探讨如何搭建一个SFTP服务器,以确保数据传输的安全性和可靠性。 首先,理解SFTP的...

    sftp服务软件

    SFTP(Secure File Transfer Protocol)服务软件是一种在Linux操作系统中广泛使用的文件传输协议,它是FTP(File Transfer Protocol)的一种安全增强版本。SFTP利用SSH(Secure Shell)协议进行数据传输,确保了文件...

    docker-sftp-google-storage-s3:这个docker容器将您的GoogleStorageS3存储桶安装到通过SFTP公开的文件夹中。 之后,您可以直接 SFTP 您的存储桶!

    docker-sftp-google-storage-s3 这个 docker 容器将您的 GoogleStorage/S3 存储桶安装到通过 chroot SFTP 公开的文件夹中。 之后,您可以直接 SFTP 您的存储桶!建立形象 git clone ...

    linux-chroot-jail:为 ssh、sftp 和 vsftp 监禁 Linux 用户的脚本

    linux-chroot-监狱用于 ssh、sftp 和 vsftp 监禁 Linux 用户的脚本监狱用户.sh jailuser.sh 此脚本会在指定用户此后登录服务器时强制执行监禁的 ssh 会话。 用户仅限于$APPS 中指定的命令列表。 它应该适用于大多数 ...

    Linux系统下jailkit的安装与使用示例.docx

    2. **Jailkit**:用于创建和管理chroot jail环境的工具集,包括限制用户Shell和配置安全日志。 3. **jk_init**:初始化chroot环境,复制必要的命令和库到指定目录。 4. **jk_addjailuser**:创建新的受限用户,或...

    pam_chroot.zip_linux pam _pam模块

    `pam_chroot`模块是PAM的一个组件,它的主要功能是在用户登录会话期间限制用户对系统的访问权限,将用户的根目录(或称为“chroot jail”)设置为一个特定的目录。这样,用户只能看到和操作这个受限的环境,无法访问...

    Chroot进入linux根文件系统以执行修复_Shell_下载.zip

    在Linux操作系统中,Chroot(Change Root)是一个强大的工具,它允许系统管理员创建一个隔离的环境,这个环境看起来就像是一个独立的操作系统实例,但实际上仍然是在原系统的权限范围内运行。Chroot常常用于系统维护...

    chroot在Ubuntulinux下的配置.pdf

    "Chroot 在 Ubuntu Linux 下的配置" Chroot 是一种安全机制,用于将进程或用户限定在某个目录下,以防止他们访问或修改系统的其他部分。在 Ubuntu Linux 下,配置 Chroot 环境需要一些步骤和技巧。 环境情况 在 ...

    centos7 搭建sftp服务器以及开启日志

    - **Chroot**:在 Linux 系统中,chroot 是一个将进程及其后代置于不同的根目录之下的操作,常用于限制用户访问特定目录。 #### 二、安装与配置 SFTP 服务 ##### 1. 安装必要的软件包 确保已安装 OpenSSH 服务器...

    使用chroot监狱限制SSH用户访问指定目录.docx

    使用 chroot 监狱限制 SSH 用户访问指定目录 本文档将向您展示如何使用 chroot 监狱限制 SSH 用户访问指定目录。在 Linux 系统中,chroot 监狱是一种限制用户访问的机制,可以将用户会话限制在特定的目录内,以提高...

    Ansible-ansible-role-ssh-chroot-jail.zip

    Ansible-ansible-role-ssh-chroot-jail.zip,ansible role-ssh chroot jail configible role:ssh chroot jail config,ansible是一个简单而强大的自动化引擎。它用于帮助配置管理、应用程序部署和任务自动化。

    如何通过chroot构建Linux操作系统.txt

    如何通过chroot构建Linux操作系统.txt

    功能齐全且高度可配置的SFTP服务器-Golang开发

    SFTPGo使用Go Features编写的功能齐全且可高度​​配置的SFTP服务器,每个帐户都被chroot到其主目录。 SFTP帐户是存储在“数据提供者”中的虚拟帐户。 SQLite,我的SFTPGo使用Go功能编写的功能齐全且可高度​​配置...

    sftp配置全过程[参照].pdf

    3. **限制用户目录**:通过设置`chroot_list_enable=YES`并创建`/etc/vsftpd/chroot_list`文件,将特定用户(如peter、john)限制在他们的主目录内,不允许他们切换到其他目录。 4. **禁用匿名登录**:将`anonymous_...

Global site tag (gtag.js) - Google Analytics