Bash使用很少的命令,将很少的内置命令列入白名单



尝试打开命令功能有限的 bash shell。

尝试了命令行选项,如 -r 限制,但没有给出预期的结果。还尝试了shopt和unset命令。

bash --noprofile --noediting --verbose --version --init-file test.sh
unset ls
shopt -u -o history

启动一个只有几个内置命令的 bash shell。例如 cd、ls、cat only。这种shell的使用将仅用于目录导航,列表和文件查看目的的只读目的

您可以获取所有内置函数的列表并声明具有相同名称的函数。

我是这样做的:

文件bash_limited.sh

#!/bin/bash

export PATH=
eval "$(
echo '
:
.
[
alias
bg
bind
break
builtin
caller
cd
command
compgen
complete
compopt
continue
declare
dirs
disown
echo
enable
eval
exec
exit
export
fc
fg
getopts
hash
help
history
jobs
kill
let
local
logout
mapfile
popd
printf
pushd
pwd
read
readarray
readonly
return
set
shift
shopt
source
test
times
trap
type
typeset
ulimit
umask
unalias
unset
wait
' |
while IFS= read -r line; do
    case "$line" in
    ''|ls|cat|cd|return|printf) continue; ;; 
    esac
    printf "%sn" "function $line () { /bin/printf -- 'bash: $line: Command not found.n' >&2; return 127; }"
done
echo 'function ls() { /bin/ls "$@"; }'
echo 'function cat() { /bin/cat "$@"; }'
)" ## eval

然后我打开一个新的外壳并执行以下操作:

$ source bash_limited.sh

之后只是:

$ .
bash: .: Command not found.
$ :
bash: :: Command not found.
$ source
bash: source: Command not found.
$ declare
bash: declare: Command not found.

您还可以使用一些chroot技术以及其他一些 PATH 限制,这将很难摆脱。

最新更新