# 9200 - Pentesting Elasticsearch

<details>

<summary><strong>从零开始学习 AWS 黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS 红队专家）</strong></a><strong>！</strong></summary>

支持 HackTricks 的其他方式：

* 如果您想看到您的**公司在 HackTricks 中做广告**或**下载 PDF 版的 HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索[**PEASS 家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在 **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)\*\* 上关注\*\*我们。
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来分享您的黑客技巧。

</details>

## 基本信息

Elasticsearch 是一款用于**各种类型数据**的**分布式**、**开源**搜索和分析引擎。以其**速度**、**可扩展性**和**简单的 REST API** 而闻名。它是基于 Apache Lucene 构建的，于 2010 年首次由 Elasticsearch N.V.（现在称为 Elastic）发布。Elasticsearch 是 Elastic Stack 的核心组件，这是一套用于数据摄入、丰富、存储、分析和可视化的开源工具集。这个堆栈通常被称为 ELK Stack，还包括 Logstash 和 Kibana，现在还有称为 Beats 的轻量级数据传送代理。

### 什么是 Elasticsearch 索引？

Elasticsearch **索引**是存储为 **JSON** 的**相关文档**集合。每个文档由**键**及其对应的**值**（字符串、数字、布尔值、日期、数组、地理位置等）组成。

Elasticsearch 使用一种高效的数据结构称为**倒排索引**来实现快速全文搜索。该索引列出了文档中的每个唯一单词，并标识了每个单词出现在哪些文档中。

在索引过程中，Elasticsearch 存储文档并构建倒排索引，从而实现几乎实时的搜索。**索引 API** 用于在特定索引中添加或更新 JSON 文档。

**默认端口**：9200/tcp

## 手动枚举

### 横幅

访问 Elasticsearch 的协议是 **HTTP**。当通过 HTTP 访问时，您将找到一些有趣的信息：`http://10.10.10.115:9200/`

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-ba23621e93a2f9859a76e9c47033bde63977e8ea%2Fimage%20\(264\).png?alt=media)

如果访问 `/` 时未看到该响应，请查看以下部分。

### 认证

**默认情况下，Elasticsearch 未启用身份验证**，因此默认情况下，您可以在不使用任何凭据的情况下访问数据库中的所有内容。

您可以通过请求验证身份验证是否已禁用：

```bash
curl -X GET "ELASTICSEARCH-SERVER:9200/_xpack/security/user"
{"error":{"root_cause":[{"type":"exception","reason":"Security must be explicitly enabled when using a [basic] license. Enable security by setting [xpack.security.enabled] to [true] in the elasticsearch.yml file and restart the node."}],"type":"exception","reason":"Security must be explicitly enabled when using a [basic] license. Enable security by setting [xpack.security.enabled] to [true] in the elasticsearch.yml file and restart the node."},"status":500}
```

**然而**，如果您发送一个请求到 `/` 并收到类似以下响应：

```bash
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}
```

这意味着身份验证已配置，**您需要有效的凭据**才能从elasticserach获取任何信息。然后，您可以[**尝试暴力破解**](https://hacktricks.xsx.tw/generic-methodologies-and-resources/brute-force#elasticsearch)（它使用HTTP基本身份验证，因此可以使用任何适用于BF HTTP基本身份验证的方法）。\
这里有一个**默认用户名列表**：***elastic**（超级用户），remote\_monitoring\_user，beats\_system，logstash\_system，kibana，kibana\_system，apm\_system，* \_anonymous\_.\_ Elasticsearch的旧版本使用此用户的默认密码为**changeme**。

```
curl -X GET http://user:password@IP:9200/
```

### 基本用户枚举

```bash
#List all roles on the system:
curl -X GET "ELASTICSEARCH-SERVER:9200/_security/role"

#List all users on the system:
curl -X GET "ELASTICSEARCH-SERVER:9200/_security/user"

#Get more information about the rights of an user:
curl -X GET "ELASTICSEARCH-SERVER:9200/_security/user/<USERNAME>"
```

### Elastic Info

以下是一些端点，您可以通过GET访问以获取有关elasticsearch的一些信息：

| \_cat                           | /\_cluster                    | /\_security               |
| ------------------------------- | ----------------------------- | ------------------------- |
| /\_cat/segments                 | /\_cluster/allocation/explain | /\_security/user          |
| /\_cat/shards                   | /\_cluster/settings           | /\_security/privilege     |
| /\_cat/repositories             | /\_cluster/health             | /\_security/role\_mapping |
| /\_cat/recovery                 | /\_cluster/state              | /\_security/role          |
| /\_cat/plugins                  | /\_cluster/stats              | /\_security/api\_key      |
| /\_cat/pending\_tasks           | /\_cluster/pending\_tasks     |                           |
| /\_cat/nodes                    | /\_nodes                      |                           |
| /\_cat/tasks                    | /\_nodes/usage                |                           |
| /\_cat/templates                | /\_nodes/hot\_threads         |                           |
| /\_cat/thread\_pool             | /\_nodes/stats                |                           |
| /\_cat/ml/trained\_models       | /\_tasks                      |                           |
| /\_cat/transforms/\_all         | /\_remote/info                |                           |
| /\_cat/aliases                  |                               |                           |
| /\_cat/allocation               |                               |                           |
| /\_cat/ml/anomaly\_detectors    |                               |                           |
| /\_cat/count                    |                               |                           |
| /\_cat/ml/data\_frame/analytics |                               |                           |
| /\_cat/ml/datafeeds             |                               |                           |
| /\_cat/fielddata                |                               |                           |
| /\_cat/health                   |                               |                           |
| /\_cat/indices                  |                               |                           |
| /\_cat/master                   |                               |                           |
| /\_cat/nodeattrs                |                               |                           |
| /\_cat/nodes                    |                               |                           |

这些端点来自[**文档**](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html)，您可以在那里找到更多信息。\
此外，如果访问`/_cat`，响应将包含实例支持的`/_cat/*`端点。

在`/_security/user`（如果启用了身份验证），您可以看到具有`superuser`角色的用户。

### Indices

您可以通过访问`http://10.10.10.115:9200/_cat/indices?v`来**收集所有索引**。

```
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana 6tjAYZrgQ5CwwR0g6VOoRg   1   0          1            0        4kb            4kb
yellow open   quotes  ZG2D1IqkQNiNZmi2HRImnQ   5   1        253            0    262.7kb        262.7kb
yellow open   bank    eSVpNfCfREyYoVigNWcrMw   5   1       1000            0    483.2kb        483.2kb
```

要获取有关索引中保存了哪种数据的信息，您可以访问：`http://host:9200/<index>`，例如在这种情况下是 `http://10.10.10.115:9200/bank`

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-1ad26b60603b360ca0a530b21f55b19f96da1f93%2Fimage%20\(265\).png?alt=media)

### 转储索引

如果您想**转储索引的所有内容**，您可以访问：`http://host:9200/<index>/_search?pretty=true`，如 `http://10.10.10.115:9200/bank/_search?pretty=true`

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-82b357385a723f9e1b186b95df5f5871e2c43cd4%2Fimage%20\(266\).png?alt=media)

*花点时间比较银行索引中每个文档（条目）的内容和我们在上一节中看到的该索引的字段。*

因此，此时您可能会注意到**hits**内有一个名为“total”的字段，指示在此索引中找到了**1000个文档**，但只检索了10个。这是因为**默认情况下限制为10个文档**。\
但是，现在您知道**此索引包含1000个文档**，您可以指定要在\*\*`size`\*\*参数中转储的条目数，**转储所有这些条目**：`http://10.10.10.115:9200/quotes/_search?pretty=true&size=1000`\
\&#xNAN;*注意：如果指定更大的数字，所有条目将被转储，例如，您可以指定`size=9999`，如果有更多条目，那将很奇怪（但您应该检查）。*

### 转储全部

为了转储全部内容，您可以直接转到**之前的相同路径，但不指定任何索引**`http://host:9200/_search?pretty=true`，如 `http://10.10.10.115:9200/_search?pretty=true`\
请记住，在这种情况下将应用**默认的10个**结果限制。您可以使用`size`参数来转储**更多的结果**。阅读上一节获取更多信息。

### 搜索

如果您正在寻找某些信息，您可以对**所有索引进行原始搜索**，转到 `http://host:9200/_search?pretty=true&q=<search_term>`，如 `http://10.10.10.115:9200/_search?pretty=true&q=Rockwell`

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-f35930b316d28c6b35497441eba0eebeced87446%2Fimage%20\(267\).png?alt=media)

如果您只想在**一个索引上搜索**，您可以在**路径**上**指定**它：`http://host:9200/<index>/_search?pretty=true&q=<search_term>`

*请注意，用于搜索内容的q参数**支持正则表达式***

您还可以使用类似<https://github.com/misalabs/horuz>的工具来模糊搜索elasticsearch服务。

### 写入权限

您可以通过尝试在新索引内创建新文档来检查您的写入权限，运行类似以下内容：

```bash
curl -X POST '10.10.10.115:9200/bookindex/books' -H 'Content-Type: application/json' -d'
{
"bookId" : "A00-3",
"author" : "Sankaran",
"publisher" : "Mcgrahill",
"name" : "how to get a job"
}'
```

该命令将创建一个名为`bookindex`的**新索引**，其中包含一个名为`books`的文档类型，该文档具有属性"*bookId*"、"*author*"、"*publisher*"和"*name*"

请注意**现在列表中出现的新索引**：

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-685fc62c1b1e2c8070c7edf3eb66bb791e44fcd6%2Fimage%20\(268\).png?alt=media)

并注意**自动生成的属性**：

![](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-6a17edfc6096275bad7d6bb5d91b9e1697f0a5c4%2Fimage%20\(269\).png?alt=media)

## 自动枚举

一些工具将获取之前呈现的一些数据：

```bash
msf > use auxiliary/scanner/elasticsearch/indices_enum
```

{% embed url="<https://github.com/theMiddleBlue/nmap-elasticsearch-nse>" %}

## Shodan

* `port:9200 elasticsearch`

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 发现[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
