-2

Trying to open a bash shell with limited command capability.

Tried command line options like -r restriction but doesn't give intended result. Also tried shopt & unset commands.

bash --noprofile --noediting --verbose --version --init-file test.sh

unset ls

shopt -u -o history

Start a bash shell with only few built-in commands. For example cd, ls, cat only. The usage of such a shell would be for read only purposes for Directory Navigation, Listing & File viewing purpose

Ignatius
  • 2,745
  • 2
  • 20
  • 32
Kunal G
  • 1
  • 3
  • Idea - you can compile your own version of [busybox](https://en.wikipedia.org/wiki/BusyBox) and restrict the command set. – UtLox Apr 20 '19 at 06:39

1 Answers1

1

You can take the list of all builtins and declare functions with the same name.

I did it like this:

File 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

Then I open a new shell and do:

$ source bash_limited.sh

after that it's just:

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

You can also use some chroot techniques with some other PATH restriction and it will be hard to get out.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111