> 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/upload.md).

# Upload

Unrestricted file upload enabled PHP reverse shell, and root was obtained via sudo env.

## 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: ports 80 is 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 ports -sS -Pn -sCV`

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2F1gkPUBLpukrslgMM3lFv%2F(Upload)%20Targeted%20Port%20Scan.png?alt=media&amp;token=5cfd8297-533a-482e-b092-2914b9ef7a81" alt=""><figcaption><p>(Upload) Targeted Port Scan</p></figcaption></figure>

We tried to find exploits or shellcodes using `searchsploit` but there wasn't any. Our next step will be directory enumeration. Using our tool `ffuf-dir`:

```
ffuf-dir () {
  ffuf -u $1 -w /usr/share/wordlists/dirb/big.txt ${@: 2} -e .html,.php,.txt
}
```

we did `ffuf-dir http://172.17.0.2/FUZZ` and we found the following routes:

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FYKqA5DPSTe4NScYjGDLR%2F(Upload)%20FFUF%20Routes.png?alt=media&amp;token=495562be-8dc1-4d37-ba97-1e9b31e62df9" alt=""><figcaption><p>(Upload) FFUF Routes</p></figcaption></figure>

## 3 - Exploitation

Accessing /upload.php we see a file upload, with no extension restriction, so we can upload a basic PHP reverse shell, as the PentestMonkey one. After browsing /uploads/revshell.php, we will activate it, and we can catch it with `nc -lvnp 3110`.

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2F01zIW9wp9JmuCgEifc5Y%2F(Upload)%20Reverse%20Shell%20Uploaded.png?alt=media&amp;token=11b1de92-03cc-401e-92ff-f3fd9af529f5" alt=""><figcaption><p>(Upload) Reverse Shell Uploaded</p></figcaption></figure>

## 4 - Privilege Escalation

After running `sudo -l` we can see that user `www-data` has sudo permissions as `root` on `/usr/bin/env`. Using this GTFOBins one-liner we can spawn a `root` shell.:

```
sudo env /bin/sh
```

<figure><img src="https://438683079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbHEScEP34huXd5EXNdb%2Fuploads%2FsxTXt5csYhHkGfnibjdi%2F(Upload)%20Root%20Shell.png?alt=media&amp;token=2a19be5f-ff06-4c06-af42-8cd3361d1ca8" alt=""><figcaption><p>(Upload) Root Shell</p></figcaption></figure>

## 5 - Summary

**Initial Access:**\
Directory enumeration revealed `/upload.php`. The upload function allowed unrestricted file types, so a PHP reverse shell was uploaded and executed, granting access as `www-data`.

**Exploitation:**\
Gained RCE by browsing the uploaded reverse shell and catching the connection with netcat.

**Privilege Escalation:**\
`www-data` had sudo rights on `/usr/bin/env`. Abusing it with GTFOBins (`sudo env /bin/sh`) provided a root shell.
