是*
这称为受限外壳。
您可以使用/bin/rbash
Ubuntu中已提供的,并将其与受限制的PATH变量结合使用。该rbash
会由什么,是不是在禁止执行$PATH
。
添加受限用户:
sudo adduser --shell /bin/rbash res-user
新建一个目录,我们可以在其中链接二进制文件,该用户将被限制为:
sudo mkdir /home/res-user/bin
修改.profile
文件:
sudo vim /home/res-user/.profile
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
readonly PATH=/home/res-user/bin
export PATH
使.profile
,bashrc
和.bash_profile
不可改变的:
sudo chattr +i /home/res-user/.profile
sudo chattr +i /home/res-user/.bashrc
sudo chattr +i /home/res-user/.bash_profile
现在,我们给用户唯一可以做的事情,即打开Firefox:
sudo ln -s /usr/lib/firefox/firefox /home/res-user/bin/
现在,如果我们以登录方式登录,res-user
则只能打开Firefox:
res-user@localhost:~$ /home/res-user/bin/firefox --version
Mozilla Firefox 68.0.1
我们不能轻易逃脱我们受限制的外壳:
res-user@localhost:~$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
-su: PATH: readonly variable
受限用户无法使文件可执行或启动它们:
res-user@localhost:~$ chmod +x script.sh
Command 'chmod' is available in '/bin/chmod'
res-user@localhost:~$ bash script.sh
Command 'bash' is available in '/bin/bash'
The command could not be located because '/bin' is not included in the PATH environment variable.
bash: command not found
受限用户无法从互联网执行恶意脚本,因为该用户无法执行必要的命令:
res-user@localhost:~$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Command 'wget' is available in '/usr/bin/wget'
The command could not be located because '/usr/bin' is not included in the PATH environment variable.
wget: command not found
Command 'bash' is available in '/bin/bash'
The command could not be located because '/bin' is not included in the PATH environment variable.
bash: command not found
* 有多种方法可以突破受限的外壳,但如果您的用户有能力,则它们可能不如您想像的容易受骗。