使用少量命令进行Bash,将少量内置命令列入白名单

问题描述 投票:-2回答:1

尝试打开具有有限命令功能的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。这种shell的使用仅用于目录导航,列表和文件查看目的的只读目的

bash shell built-in
1个回答
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

然后我打开一个新shell并执行:

$ 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限制,很难脱身。

© www.soinside.com 2019 - 2024. All rights reserved.