Escaping Restricted Shells
Restricted shells (e.g., rbash
, rksh
, rzsh
) limit user capabilities by restricting commands, directory access, and environment modifications. Below are various techniques to escape restricted shells.
But first, when we land on an restricted shell we can enumerate available commands and also what is accessible:
1. Command Injection
If the restricted shell allows executing certain commands with arguments, you can inject additional commands.
or using $()
:
Another example:
If pwd
or whoami
is unrestricted, they will execute.
2. Command Substitution
Using backticks (`command`
) or $()
allows command execution.
or:
This prints user information even if id
is restricted.
3. Command Chaining
Using ;
, |
, &&
, or ||
to append an unrestricted command.
or:
If /bin/sh
isn't restricted, this will drop into an unrestricted shell.
4. Environment Variables
Modifying $PATH
or $SHELL
to execute commands.
or:
This changes the default shell to /bin/sh
, possibly escaping the restricted environment.
5. Shell Functions
Defining a function to execute an unrestricted shell.
or overriding a built-in command:
If /bin/sh
is available, this escapes the restricted shell.
6. Using Built-in Commands
If vi
or nano
is allowed, they can spawn an unrestricted shell.
Example using vi
:
Inside vi
, press ESC
and type:
If vi
is available, it spawns a new unrestricted shell.
7. Using man
to Execute Commands
If man
is allowed:
Typing !/bin/sh
inside man
spawns a new shell.
8. Exploiting less
or more
If less
or more
is available:
Then type:
This launches an unrestricted shell.
9. Using SSH to Escape
If ssh
is allowed:
This spawns an unrestricted shell if SSH access is permitted.
10. Backgrounding a Process
If Ctrl + Z
works, suspend the shell and launch another shell.
Example:
Then press Ctrl + Z
to suspend it and try:
or:
Sometimes this drops you into an unrestricted shell.
These techniques vary depending on the level of restriction, but one or a combination of them often works to escape restricted shells.
Last update: 2025-02-12 Created: February 12, 2025 20:36:23