---
title: "7. Escaping restricted shells"
date: 2025-01-25
draft: false
---

Restricted shell limits ability to exec cmds. 
Restricted to specific set of cmds or only allowed in certain dirs. 
Often used to provide a safe env for users who may accdientally / intentionally damage the system or provide a way for users to access only certain system features. Common example is `rbash` in linux and `Restricted-access Shell` in windows. 

**RBASH**
Restricted Bourne shell - See above. 

**RKSH**
Restricted Korn shell - Korn shell restricted. `rksh` limits user ability to user certain features of Korn shell, like exec commands in other dirs etc. 

**RZSH**
Restricted ZSH shell `rzsh` 
- Most powerful/flexible cmdline interpreter. rzsh limits user ability to use certain features of z shell such as running shell scripts, defining aliases, modifying shell env. 

By limiting user ability to exec specific cmds or access dirs, admin ensure users cannot perform actions that could harm the system or compromise security. 

```bash
For example, external partners who need to access only certain network features, such as email and file sharing, are assigned to rbash shells, which limits their ability to execute specific commands and access certain directories. Contractors who need to access more advanced network features, such as database servers and web servers, are assigned to rksh shells, which provide them with more flexibility but still limit their abilities. Finally, employees who need to access the network for specific purposes, such as to run specific applications or scripts, are assigned to rzsh shells, which provide them with the most flexibility but still limit their ability to execute specific commands and access certain directories.
```

Several methods to escape a restricted shell. Some methods involve exploiting vulns in the shell itself, others involve using creative techniques to bypass restrictions. 

**Escaping** 
Injecting commands into cmdline or other inputs the shell accepts. For example, suppose shell allows user to exec cmds by passing them as arguments to a built-in command. In that case, we can escape by injecting additional commands into the argument. 

**Command injection** 
Imagine restricted shell that allows execution by passing them as args to `ls`. Shell only allows `ls` with specific args, like `ls -l` or `ls -a` but not allowed to exec other cmds. We can use cmd injection to escape from this shell by injecting add'tl commadns into args. 

For example, 
```bash
ls -l `pwd` 
```
Command would cause ls command to be executed with arg -l followed by output of pwd command. Since pwd is not restricted that would allow us to execute the pwd cmd and see current working dir, even though shell does not allow us to use pwd explicitly. 

**Command substitution**
Shell's cmd substition syntax used to exec a cmd. 
IE: Shell allows users to exec cmds by encldoign in backticks. Possible to escape from shell by executing a cmomand in backtick substitution 

**Command chaining**
Possible to escape restricted shell with command chaining. Commands separated by shell metacharacters like `;` or `|` 
If shell allow exec cmds separated by semicolons its possible to escape from shell by using semicolon to separate two commands, one not restricted by shell. 

**Env vars**
For escaping using envvars - modifying/creating envvars that shell uses to exec cmds not restricted by shell. IE - Shell uses envvar to specify directory in which commands are executed. May be possible to escape from a shell by modifying the value of the envvar. 

**Shell functions**
May be possible to escape using shell functions. Define and call shell functions that exec commands not restricted by shell - IE Shell allows user to define+call shell fxn - Define shell fxn that execs a cmd. 

https://book.hacktricks.wiki/en/linux-hardening/privilege-escalation/escaping-from-limited-bash.html

lessons learned from restricting from module: 
- check compgen -c and -b 
- if we have ssh access try `-t bash` to use bash profile 
