6.Escaping-restricted-shells
Understanding Restricted Shells
Purpose:
Limit user capabilities for security.
Prevent accidental or intentional system damage.
Control access to specific system features.
Common Types:
rbash
(Restricted Bourne shell)rksh
(Restricted Korn shell)rzsh
(Restricted Z shell)
Restrictions:
Limited command execution.
Restricted directory access.
Disabled environment variable modification.
Prevented shell function creation.
Escaping Techniques
The document outlines several methods, which can be summarized and expanded upon:
Command Injection:
Exploiting vulnerabilities where user input is directly used in command execution.
Example:
ls -l \
pwd`` (using backticks for command substitution).This is very dependent on the specific implementation of the restricted shell.
Command Substitution:
Using backticks (
) or
$(command)` to execute commands within other commands.If the restricted shell allows any form of command substitution, it can often be abused.
Command Chaining:
Using
;
,&&
,||
, or|
to execute multiple commands in sequence.If the shell allows any of these, it may be possible to chain an allowed command with an escape command.
Environment Variables:
Manipulating variables like
PATH
orSHELL
to execute unauthorized commands.If the shell allows any modification of environment variables, this is a very common method of escape.
Shell Functions:
Defining custom functions to bypass restrictions.
If function definitions are possible, this is a very powerful way to escape.
Practical Considerations
Specific Restrictions: The effectiveness of these techniques depends entirely on the specific restrictions imposed by the restricted shell.
Enumeration: Thoroughly enumerate the shell's limitations. What commands are allowed? What environment variables exist?
Binary Exploitation: If standard escape methods fail, consider if any allowed binaries have known vulnerabilities.
GTFObins: GTFObins is an excellent resource for finding ways to abuse common Linux binaries. Many of these techniques can be used to escape restricted shells.
Common Escape Commands:
sh
,bash
,python
,perl
,ruby
,awk
,vi
,nano
,tmux
,screen
and many more. If any of these are available, they are very often used to escape.
Example scenarios and commands
If vi is available:
:!/bin/sh
If Python is available:
python -c 'import os; os.system("/bin/sh")'
If Perl is available:
perl -e 'exec "/bin/sh";'
If less is available:
!/bin/sh
If nmap is available:
!sh
inside of an interactive nmap session.If find is available:
find / -exec /bin/sh \;
Last updated