你为什么不写自己的login-shell? 为此使用Bash会非常简单,但您可以使用任何语言。
Bash中的示例
使用您喜欢的编辑器创建文件;(这可以是任何名称或路径,但应该是&和&&):
#!/bin/bash
commands=("man" "pwd" "ls" "whoami")
timestamp(){ date +'%Y-%m-%s %H:%M:%S'; }
log(){ echo -e "$(timestamp)\t$1\t$(whoami)\t$2" > /var/log/rbash.log; }
trycmd()
{
# Provide an option to exit the shell
if [[ "$ln" == "exit" ]] || [[ "$ln" == "q" ]]
then
exit
# You can do exact string matching for some alias:
elif [[ "$ln" == "help" ]]
then
echo "Type exit or q to quit."
echo "Commands you can use:"
echo " help"
echo " echo"
echo "${commands[@]}" | tr ' ' '\n' | awk '{print " " $0}'
# You can use custom regular expression matching:
elif [[ "$ln" =~ ^echo\ .*$ ]]
then
ln="${ln:5}"
echo "$ln" # Beware, these double quotes are important to prevent malicious injection
# For example, optionally you can log this command
log COMMAND "echo $ln"
# Or you could even check an array of commands:
else
ok=false
for cmd in "${commands[@]}"
do
if [[ "$cmd" == "$ln" ]]
then
ok=true
fi
done
if $ok
then
$ln
else
log DENIED "$cmd"
fi
fi
}
# Optionally show a friendly welcome-message with instructions since it is a custom shell
echo "$(timestamp) Welcome, $(whoami). Type 'help' for information."
# Optionally log the login
log LOGIN "$@"
# Optionally log the logout
trap "trap=\"\";log LOGOUT;exit" EXIT
# Optionally check for '-c custom_command' arguments passed directly to shell
# Then you can also use ssh user@host custom_command, which will execute /root/rbash.sh
if [[ "$1" == "-c" ]]
then
shift
trycmd "$@"
else
while echo -n "> " && read ln
do
trycmd "$ln"
done
fi
您所要做的就是将此可执行文件设置为登录shell。 例如,编辑;文件,并将该用户&的当前登录shell替换为&&。
这只是一个简单的例子,但您可以根据需要将其设置为高级,这个想法就在那里。 小心不要通过更改自己和唯一用户的登录shell来锁定自己。 并始终测试奇怪的符号和命令,看它是否真的安全。
你可以用以下方法测试它:;。
注意,确保匹配整个命令,并注意通配符! 最好排除Bash符号,如;,&,&&,||,$,并确认反引号。
根据您给用户的自由度,它不会比这更安全。 我发现通常我只需要让一个只能访问少量相关命令的用户,在这种情况下,这确实是更好的解决方案。但是,您是否希望提供更多自由,监狱和权限可能更合适。 很容易犯错误,只有在已经太晚的时候才会注意到。