重击一些命令,将一些内置命令列入白名单

时间:2019-04-20 02:02:32

标签: bash shell built-in

尝试以有限的命令功能打开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。 此类外壳的使用仅出于目录导航,列表和文件查看的目的

1 个答案:

答案 0 :(得分:0)

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

我是这样做的:

文件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 "%s\n" "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限制一起使用,并且很难摆脱。