> For the complete documentation index, see [llms.txt](https://itszaiden.gitbook.io/pentesting/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://itszaiden.gitbook.io/pentesting/writeups/dockerlabs/easy/backend.md).

# Backend

Fast port scanning, service enumeration, SQL injection to extract credentials, SSH login as user pepe, and privilege escalation via SUID abuse on /usr/bin/grep and /usr/bin/ls.

## 1 - Reconnaissance

We begin with a fast Nmap scan to identify open ports:

`nmap 172.17.0.2 -p- --open -sS -Pn --min-rate 5000 -T5`

**Result: port 22 and 80 are open**

## 2 - Enumeration

Now we run a service/version detection scan, targeting only the open ports we found on the previous scan:

`nmap 172.17.0.2 -p 22,80 -sS -Pn -sCV`

**Result:**

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FPfNXlUQrWPaPYN5WA4o3%2F(Backend)%20Targeted%20Port%20Scan.png?alt=media&amp;token=36285167-def7-401b-bae0-9d1c614f6c9a" alt=""><figcaption><p>(Backend) Targeted Port Scan</p></figcaption></figure>

We tried to find exploits or shellcodes using `searchsploit Apache httpd 2.4.61` and `searchsploit OpenSSH 9.2p1` but there wasn't any. Our next step will be directory enumeration.

With the command&#x20;

```
ffuf -w /path/to/wordlist/directory-list-2.3-medium.txt:FUZZ -u http://10.129.129.108/FUZZ -e .html,.php we didn't find anything useful, so we will browse the website.
```

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FqYxo6dxAnqrcEV8FzNfX%2F(Backend)%20Web%20Content.png?alt=media&amp;token=9438a84c-9019-4481-bc74-c031d8567405" alt="" width="375"><figcaption><p>(Backend) Web Content</p></figcaption></figure>

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FsScdtBpWM00cXyV9tga0%2F(Backend)%20Source%20Code.png?alt=media&amp;token=edbe592a-455e-4780-b9ca-f3ce4831d0a0" alt="" width="303"><figcaption><p>(Backend) Source Code</p></figcaption></figure>

As we can see, there isn't anything really useful on this, so we will try **SQLi** on it. Manual **SQL** is not working, so we will run **SQLmap** on it.

## 3 - Exploitation

For running **SQLMap**, we will capture the request with **Burpsuite**

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FecveHGMUWKLPHXT6ohU1%2F(Backend)%20Login%20Request.png?alt=media&amp;token=bb5c02eb-75ec-4bc2-b8fc-f18242ddc2ee" alt=""><figcaption><p>(Backend) Login Request Intercepted</p></figcaption></figure>

We will add it into a request.txt and using the following command we will get an username list, if **SQLmap** succeeds:

```bash
sqlmap -r request.txt --level=5 --risk=3 --dump
```

After some minutes, we got 3 valid usernames and passwords:

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FGjaHq3zOuFyAvBj42vTs%2F(Backend)%20SQLMap%20Results.png?alt=media&amp;token=9863e559-6f87-47e7-918b-833f101fb4ed" alt=""><figcaption><p>(Backend) SQLMap Results</p></figcaption></figure>

None of those 3 users let us login on the website, so we will try them on **SSH** since it was also opened. User `pepe` lets us login.

## 4 - Privilege Escalation

With `find / -perm -4000 2</dev/null` we can find the files with **SUID**, so we can run them as **root**. Browsing **GTFOBins** we can find an exploit for `/usr/bin/grep` and `/usr/bin/ls`. We will try to list the contents of `/root` with `ls`, and read any interesting file with `grep`.

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2F0PdAfH21sGFoTKJgPL3u%2F(Backend)%20SUID%20Files.png?alt=media&amp;token=cb16aeba-1a88-4adc-af2a-3342775eb1af" alt=""><figcaption><p>(Backend) SUID Files</p></figcaption></figure>

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FVQIJzldVJq769IsrdlkM%2F(Backend)%20Root%20Home%20Content.png?alt=media&amp;token=5782f162-e424-4055-94be-59117c0ffb5c" alt=""><figcaption><p>(Backend) Root Home Content</p></figcaption></figure>

We will now read it using the `/usr/bin/grep` exploit.

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FYX4fi64fpyrKxESL4zed%2F(Backend)%20Pass.hash%20Content.png?alt=media&amp;token=69d2b86d-b74d-46bb-8306-c29e91a442aa" alt=""><figcaption><p>(Backend) Pass.hash Content</p></figcaption></figure>

Analyzing the hash with `hashid -m "hash"` we found it's an **MD5** hash, so we will run:

```
hashcat -m0 -a0 hash /usr/share/wordlists/rockyou.txt
```

We found the password is `spongebob34` and trying to do `su root` with that password will end up on a successful login.

## 4 - Summary

**Initial Access:**\
Accessed the machine via **SSH** using credentials obtained through **SQL injection** on the website's login form, automated with **SQLMap**.

**Exploitation:**\
Enumerated **SUID** binaries and leveraged `/usr/bin/grep` and `/usr/bin/ls` to access the **root** directory and read a hashed password. Cracked the **MD5** hash with **Hashcat** to retrieve the **root** password.

**Privilege Escalation:**\
Gained **root** access by switching users with the cracked **root** password using `su`.
