# 9200 - Pentesting Elasticsearch {{#include ../banners/hacktricks-training.md}} ## 基本信息 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/` ![](<../images/image (294).png>) 如果您在访问 `/` 时没有看到该响应,请参见以下部分。 ### 认证 **默认情况下,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} ``` 这意味着身份验证已配置,**您需要有效的凭据**才能从elasticsearch获取任何信息。然后,您可以[**尝试暴力破解**](../generic-hacking/brute-force.md#elasticsearch)(它使用HTTP基本身份验证,因此可以使用任何可以暴力破解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/" ``` ### 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/`,在本例中为`http://10.10.10.115:9200/bank` ![](<../images/image (342).png>) ### 转储索引 如果您想**转储索引的所有内容**,可以访问:`http://host:9200//_search?pretty=true`,例如`http://10.10.10.115:9200/bank/_search?pretty=true` ![](<../images/image (914).png>) _花点时间比较银行索引中每个文档(条目)的内容以及我们在上一节中看到的该索引的字段。_ 因此,此时您可能会注意到**在“hits”中有一个名为“total”的字段**,它表示**在此索引中找到了1000个文档**,但仅检索了10个。这是因为**默认情况下限制为10个文档**。\ 但是,现在您知道**该索引包含1000个文档**,您可以**转储所有文档**,在**`size`**参数中指明要转储的条目数:`http://10.10.10.115:9200/quotes/_search?pretty=true&size=1000`asd\ \_注意:如果您指明更大的数字,所有条目仍将被转储,例如您可以指明`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=`,例如`http://10.10.10.115:9200/_search?pretty=true&q=Rockwell` ![](<../images/image (335).png>) 如果您只想**在某个索引中搜索**,可以在**路径**中**指定**它:`http://host:9200//_search?pretty=true&q=` _注意,q参数用于搜索内容**支持正则表达式**_ 您还可以使用类似[https://github.com/misalabs/horuz](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_"。 注意 **新索引现在出现在列表中**: ![](<../images/image (130).png>) 并注意 **自动创建的属性**: ![](<../images/image (434).png>) ## 自动枚举 一些工具将获取之前呈现的一些数据: ```bash msf > use auxiliary/scanner/elasticsearch/indices_enum ``` {{#ref}} https://github.com/theMiddleBlue/nmap-elasticsearch-nse {{#endref}} ## Shodan - `port:9200 elasticsearch` {{#include ../banners/hacktricks-training.md}}