2013年我写过一篇博文《linux下自建ssh堡垒机》,其核以是通过chroot和google Authenticator实现的。最近在研究一些web ssh平台时,无意中注意到一个python写的类似于chroot限定shell产品lshell 。本篇就记录下该工具的安装和使用。

一、lshell的安装

安装非常简单,几条命令的事,如下:

1[root@361way srv]# git clone https://github.com/ghantoos/lshell
2[root@361way srv]# cd lshell/
3[root@361way lshell]# python setup.py install --no-compile --install-scripts=/usr/bin/

默认的配置文件是 /etc/lshell.conf文件,在任一用户下使用如下命令执行后就会进行受限shell下:

lshell –config /etc/lshell.conf

lshell
lshell

二、lshell的使用

对于一个已经存在的用户,可以通过修改shell类型的方式,将其shell类型修改为lshell,如下:

 1[root@361way lshell]# cat /etc/passwd|grep test
 2test:x:500:500::/home/test:/bin/bash
 3[root@361way lshell]# chsh -s /usr/bin/lshell test
 4Changing shell for test.
 5Warning: "/usr/bin/lshell" is not listed in /etc/shells.
 6Shell changed.
 7[root@361way lshell]# su - test
 8You are in a limited shell.
 9Type '?' or 'help' to get the list of allowed commands
10test:~$ ?
11cd  clear  echo  exit  help  history  ll  lpath  ls  lsudo
12test:~$ 

使用问号输出的命令是test用户可以使用的命令,对于新建的用户,也可以直接将其shell设置为lshell,如下:

1[root@361way lshell]# useradd  test1 -s /usr/bin/lshell
2[root@361way lshell]# su - test1
3You are in a limited shell.
4Type '?' or 'help' to get the list of allowed commands
5test1:~$ ?
6cd  clear  echo  exit  help  history  ll  lpath  ls  lsudo

三、lshell的配置

lshell.conf中有四部分配置,除了global全局配置外,根据配置的优先级又有如下三部分配置:

User配置
Group配置
Default配置

1、global全局配置

全局配置主要配置日志信息,日志信息可以记录用户执行过的所有操作:

  • logpath : config path (default is /var/log/lshell/)
  • loglevel : 0, 1, 2, 3 or 4 (0: no logs -> 4: logs everything)
  • logfilename : set log file name, e.g. %u-%y%m%d (i.e foo-20091009.log)

2、user、group、default配置

这三部分配置是,如果能在用户配置下匹配的,会到用户配置下先匹配,匹配不到的,再找组配置,组配置也匹配不上的,就使用默认配置。其参数有如下部分:

 1aliases : command aliases list (similar to bash's alias directive)
 2allowed : a list of the allowed commands or 'all' to allow all commands in user's PATH
 3env_path : update the environment variable $PATH of the user
 4forbidden : a list of forbidden character or commands
 5history_file : set the history filename. A wildcard can be used: %u -> username (e.g. '/home/%u/.lhistory')
 6history_size : set the maximum size (in lines) of the history file
 7home_path : set the home folder of your user. If not specified, the home_path is set to the $HOME environment variable. A wildcard can be used: %u -> username (e.g. '/home/%u') This variable will be removed in the next version of lshell, please use your system's tools to set a user's home directory.
 8intro : set the introduction to print at login
 9passwd : password of specific user
10path : list of path to restrict the user geographically
11overssh : list of command allowed to execute over ssh (e.g. rsync, rdiff-backup, scp, etc.)
12scp : allow or forbid the use of scp connection - set to 1 or 0
13scpforce : force files sent through scp to a specific directory
14scp_download : set to 0 to forbid scp downloads (default is 1)
15scp_upload : set to 0 to forbid scp uploads (default is 1)
16sftp : allow or forbid the use of sftp connection - set to 1 or 0
17sudo_commands : a list of the allowed commands that can be used with sudo(8)
18timer : a value in seconds for the session timer
19strict : logging strictness. If set to 1, any unknown command is considered as forbidden, and user's warning counter is decreased. If set to 0, command is considered as unknown, and user is only warned (i.e. *** unknown synthax)
20warning_counter : number of warnings when user enters a forbidden value before getting exited from lshell.

这部分具体可以参看/etc/lshell.conf里的配置示例。

四、配置示例

这里使用的是官方给出的示例,为了便于查看,这里把需求翻译成中文,如下:

用户foo:
1)可以访问/usr和/var,无法访问/usr/local
2)可以执行除了su以外的命令
3)家目录/home/users
用户bar:
1)可以访问/usr和/etc,无法访问/usr/local
2)除了default的命令还可以执行ping,无法执行ls
3)开启strict(1:表示每个unknown命令都会减少warning counter的数量;0:针对unknown命令只是提醒,不会减少warning counter的数量)

配置文件示例如下:

 1# CONFIGURATION START
 2[global]
 3logpath         : /var/log/lshell/
 4loglevel        : 2
 5[default]
 6allowed         : ['ls','pwd']
 7forbidden       : [';', '&', '|']
 8warning_counter : 2
 9timer           : 0
10path            : ['/etc', '/usr']
11env_path        : ':/sbin:/usr/foo'
12scp             : 1 # or 0
13sftp            : 1 # or 0
14overssh         : ['rsync','ls']
15aliases         : {'ls':'ls --color=auto','ll':'ls -l'}
16[grp:users]
17warning_counter : 5
18overssh         : - ['ls']
19[foo]
20allowed         : 'all' - ['su']
21path            : ['/var', '/usr'] - ['/usr/local']
22home_path       : '/home/users'
23[bar]
24allowed         : + ['ping'] - ['ls']
25path            : - ['/usr/local']
26strict          : 1
27scpforce        : '/home/bar/uploads/'

CONFIGURATION END

五、注意事项

lshell功能总体上来说还是比较人性化和好用的,不过在使用的时候需要注意,不要把bash 、csh这类的命令赋予给用户,因为有chsh -l 输出的shell类型的权限时,其是可以进行越权的。其通过执行bash就跳到bash shell里了,lshell里设置的各项限制就对其不生效了。

参考配置文件:lshell官方wiki页