lshell

參考文檔:
http://linux.it.net.cn/e/shell/2015/0117/12168.html
http://haibing.org/?p=181
https://github.com/ghantoos/lshell/issues/144

1.安裝lshell,以及配置config文件

[root@ChrisTest ~]# cd downloads/lshell/lshell-master/

[root@ChrisTest ~]# python setup.py install --no-compile --install-scripts=/usr/bin/

[root@ChrisTest lshell-master]# vi /etc/lshell.conf
aliases : {'ll':'ls -l':'ls':'cd':'cd /home'}
allowed : ['ls', 'echo','ll','grep','cd','cp','sed','pwd']
forbidden : [';', '&', '|','`','>','<', '$(', '${']
home_path : '/home/test/'
path : ['/home/test/','/etc','usr']

2.改變test用戶的shell爲lshell
[root@ChrisTest ~]# chsh -s /usr/bin/lshell test

但是改變後出現Warning提示,"usr/bin/lshell" is not listed in /etc/shells.
但是也提示Shell Changed。

嘗試用putty登錄,發現輸入密碼後閃退。

再用SecureCRT登錄,輸入密碼後提示
CONF: Incomplete aliases filed in configuration file.

網上一搜,發現也有人遇到諸如此類的報錯,但錯誤信息裏不是alias,而是其他的allowed,path之類的。
從而也能得知,我的這個錯誤是由錯誤的alias導致的。

想到剛纔根據文檔修改的alias,可能存在問題,就把conf裏的alias改成了默認的
aliases : {'ll':'ls -l'}

結果用putty和SecureCRT就直接可以登錄了。

Last login: Mon Dec 31 13:05:38 2018 from 10.110.106.23
You are in a limited shell.
Type '?' or 'help' to get the list of allowed commands
test:~$

3.接下來需要驗證是否可以執行受限範圍外的命令。
test:~$ df -h
forbidden command: df
test:~$ pwd
/home/test
test:~$ cd ..
forbidden path: /home/
test:~$ cd script/

看來是有效果了。

4.但是遇到個問題,敲擊tab鍵無法自動補全,敲兩下tab鍵也沒法顯示符合要求的項。
我在allowed里加入了'bash'後,這些問題得以解決,這才讓我想起,這些都是bash的功能。
同時,不能執行bash的腳本的問題也解決了。(不過要記得重新用test用戶登錄才能生效)

5.緊接着,就是執行腳本時,提示的沒有權限執行的命令,一個個補到conf裏了。
main_doshowall.sh: line 70: /bin/cat: Permission denied
main_doshowall.sh: line 32: /usr/bin/wc: Permission denied
main_doshowall.sh: line 5: /bin/date: Permission denied
main_doshowall.sh: line 8: /bin/mkdir: Permission denied

我發現,用了lshell後,雖然直接執行allowed裏面的命令是可以的,但在腳本里執行就不行了。
有人說,如果把bash命令加入allowed,會有用戶escape的風險。本身就不合理,我覺得也有些道理。
https://github.com/ghantoos/lshell/issues/144

是否需要繼續深究,還需考慮下。

按照網上的說法,需要避免把bash命令加到allowed清單,所以我先去掉它吧。
同時作爲測試,把一個名爲111.sh的腳本加入到了allowed清單裏。
allowed : ['ls', 'echo','ll','grep','cd','cp','sed','pwd','awk','wc','cat','date','mkdir','more','./111.sh']

以下爲腳本內容
[root@ChrisTest get_port_num_by_MAC_addr]# more 111.sh
#!/bin/bash
date

結果依然提示權限不夠,甚至於在root下也提示。
[root@ChrisTest get_port_num_by_MAC_addr]# ./111.sh
bash: ./111.sh: 權限不夠
[root@ChrisTest get_port_num_by_MAC_addr]# bash 111.sh
2018年 12月 31日 星期一 14:19:00 CST

這其中的不同,就在於111.sh我沒有chmod 755,感覺是這個原因。
[root@ChrisTest get_port_num_by_MAC_addr]# chmod a+x 111.sh
[root@ChrisTest get_port_num_by_MAC_addr]# ll 111.sh
-rwxr-xr-x. 1 test test 18 12月 31 13:49 111.sh

結果可以在root下用./111.sh執行了,但依然不能在test下執行。

最後發現,最終正確的做法是:
allowed_shell_escape : ['man','zcat','./111.sh']
在這個裏面加入./111.sh整個腳本的執行相對路徑。
而allowed裏不需要加入這段,
而至於在allowed里加入date這個111.sh裏包含的命令,還是在allowed_shell_escape,其實都有效果,但至少要加一個。
我加入到了allowed清單那裏。

而且還發現,main_doshowall.sh裏嵌套的expect腳本doshow.sh,卻不需要加入到escape的list裏。很神奇。
腳本執行成功。

就此,問題完全得到解決

6.[root@ChrisTest ~]# grep "alias" /home/test/.bashrc

User specific aliases and functions

alias 1="bash $HOME/script/Handy.sh"

另外,希望在test帳號下,也能使用alias功能來實現按下數字1,就可以進入對應的Handy.sh腳本。
我一開始嘗試在root下編輯test的bashrc文件,但似乎不能起作用。

後來嘗試編輯了lshell.conf文件,
[root@ChrisTest ~]# vi /etc/lshell.conf
allowed : ['ls', 'echo','ll','grep','cd','cp','sed','pwd','awk','wc','cat','mkdir','more','date','alias','1']
在allowed裏要加入alias名字1,否則也會被認爲被禁止的命令而無法使用alias代號。
aliases : {'ls':'ls --color=auto','ll':'ls -l','1':'bash $HOME/script/Handy.sh'}
接着,在aliases一欄,加入這組alias定義。“:”冒號左右分別爲alias和alias代表的內容,而每一組由“,”逗號隔開。

這時再登錄,就可以正常使用alias的定義了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章