hacktricks/network-services-pentesting/9200-pentesting-elasticsearch.md

209 lines
13 KiB
Markdown
Raw Normal View History

2022-05-01 13:25:53 +00:00
# 9200 - Pentesting Elasticsearch
2022-04-28 16:01:33 +00:00
<details>
2024-01-05 22:36:48 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-05 22:36:48 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-05 22:36:48 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 12:24:06 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-05 22:36:48 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2022-05-01 13:25:53 +00:00
## Basic information
2024-02-08 21:36:35 +00:00
Elasticsearch is a **distributed**, **open source** search and analytics engine for **all types of data**. It is known for its **speed**, **scalability**, and **simple REST APIs**. Built on Apache Lucene, it was first released in 2010 by Elasticsearch N.V. (now known as Elastic). Elasticsearch is the core component of the Elastic Stack, a collection of open source tools for data ingestion, enrichment, storage, analysis, and visualization. This stack, commonly referred to as the ELK Stack, also includes Logstash and Kibana, and now has lightweight data shipping agents called Beats.
2022-05-01 13:25:53 +00:00
### What is an Elasticsearch index?
2024-02-08 21:36:35 +00:00
An Elasticsearch **index** is a collection of **related documents** stored as **JSON**. Each document consists of **keys** and their corresponding **values** (strings, numbers, booleans, dates, arrays, geolocations, etc.).
2024-02-08 21:36:35 +00:00
Elasticsearch uses an efficient data structure called an **inverted index** to facilitate fast full-text searches. This index lists every unique word in the documents and identifies the documents in which each word appears.
2024-02-08 21:36:35 +00:00
During the indexing process, Elasticsearch stores the documents and constructs the inverted index, allowing for near real-time searching. The **index API** is used to add or update JSON documents within a specific index.
**Default port**: 9200/tcp
2022-05-01 13:25:53 +00:00
## Manual Enumeration
2022-05-01 13:25:53 +00:00
### Banner
The protocol used to access Elasticsearch is **HTTP**. When you access it via HTTP you will find some interesting information: `http://10.10.10.115:9200/`
![](<../.gitbook/assets/image (264).png>)
2020-07-17 23:09:57 +00:00
If you don't see that response accessing `/` see the following section.
2022-05-01 13:25:53 +00:00
### Authentication
2020-07-17 23:09:57 +00:00
2021-01-28 13:40:17 +00:00
**By default Elasticsearch doesn't have authentication enabled**, so by default you can access everything inside the database without using any credentials.
You can verify that authentication is disabled with a request to:
```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}
```
2020-07-17 23:09:57 +00:00
**However**, if you send a request to `/` and receives a response like the following one:
```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}
```
2022-05-01 13:25:53 +00:00
That will means that authentication is configured an **you need valid credentials** to obtain any info from elasticserach. Then, you can [**try to bruteforce it**](../generic-methodologies-and-resources/brute-force.md#elasticsearch) (it uses HTTP basic auth, so anything that BF HTTP basic auth can be used).\
Here you have a **list default usernames**: _**elastic** (superuser), remote\_monitoring\_user, beats\_system, logstash\_system, kibana, kibana\_system, apm\_system,_ \_anonymous\_.\_ Older versions of Elasticsearch have the default password **changeme** for this user
2020-07-17 23:09:57 +00:00
```
2020-10-05 11:46:57 +00:00
curl -X GET http://user:password@IP:9200/
```
2022-05-01 13:25:53 +00:00
### Basic User Enumeration
2021-01-28 13:40:17 +00:00
```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>"
```
2022-05-01 13:25:53 +00:00
### Elastic Info
2020-07-17 23:09:57 +00:00
2021-11-30 16:46:07 +00:00
Here are some endpoints that you can **access via GET** to **obtain** some **information** about 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 | | |
These endpoints were [**taken from the documentation**](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html) where you can **find more**.\
2020-07-17 23:09:57 +00:00
Also, if you access `/_cat` the response will contain the `/_cat/*` endpoints supported by the instance.
2022-04-05 22:24:52 +00:00
In `/_security/user` (if auth enabled) you can see which user has role `superuser`.
2020-07-17 23:09:57 +00:00
2022-05-01 13:25:53 +00:00
### Indices
You can **gather all the indices** accessing `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
```
2021-11-30 16:46:07 +00:00
To obtain **information about which kind of data is saved inside an index** you can access: `http://host:9200/<index>` from example in this case `http://10.10.10.115:9200/bank`
![](<../.gitbook/assets/image (265).png>)
2022-05-01 13:25:53 +00:00
### Dump index
2021-11-30 16:46:07 +00:00
If you want to **dump all the contents** of an index you can access: `http://host:9200/<index>/_search?pretty=true` like `http://10.10.10.115:9200/bank/_search?pretty=true`
![](<../.gitbook/assets/image (266).png>)
_Take a moment to compare the contents of the each document (entry) inside the bank index and the fields of this index that we saw in the previous section._
2021-11-30 16:46:07 +00:00
So, at this point you may notice that **there is a field called "total" inside "hits"** that indicates that **1000 documents were found** inside this index but only 10 were retried. This is because **by default there is a limit of 10 documents**.\
But, now that you know that **this index contains 1000 documents**, you can **dump all of them** indicating the number of entries you want to dump in the **`size`** parameter: `http://10.10.10.115:9200/quotes/_search?pretty=true&size=1000`asd\
_Note: If you indicate bigger number all the entries will be dumped anyway, for example you could indicate `size=9999` and it will be weird if there were more entries (but you should check)._
2022-05-01 13:25:53 +00:00
### Dump all
In order to dump all you can just go to the **same path as before but without indicating any index**`http://host:9200/_search?pretty=true` like `http://10.10.10.115:9200/_search?pretty=true`\
Remember that in this case the **default limit of 10** results will be applied. You can use the `size` parameter to dump a **bigger amount of results**. Read the previous section for more information.
2022-05-01 13:25:53 +00:00
### Search
2021-11-30 16:46:07 +00:00
If you are looking for some information you can do a **raw search on all the indices** going to `http://host:9200/_search?pretty=true&q=<search_term>` like in `http://10.10.10.115:9200/_search?pretty=true&q=Rockwell`
![](<../.gitbook/assets/image (267).png>)
2021-11-30 16:46:07 +00:00
If you want just to **search on an index** you can just **specify** it on the **path**: `http://host:9200/<index>/_search?pretty=true&q=<search_term>`
_Note that the q parameter used to search content **supports regular expressions**_
2020-07-27 15:27:48 +00:00
You can also use something like [https://github.com/misalabs/horuz](https://github.com/misalabs/horuz) to fuzz an elasticsearch service.
2022-05-01 13:25:53 +00:00
### Write permissions
You can check your write permissions trying to create a new document inside a new index running something like the following:
```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"
}'
```
2021-11-30 16:46:07 +00:00
That cmd will create a **new index** called `bookindex` with a document of type `books` that has the attributes "_bookId_", "_author_", "_publisher_" and "_name_"
Notice how the **new index appears now in the list**:
![](<../.gitbook/assets/image (268).png>)
And note the **automatically created properties**:
![](<../.gitbook/assets/image (269).png>)
2022-05-01 13:25:53 +00:00
## Automatic Enumeration
2020-07-17 14:32:55 +00:00
Some tools will obtain some of the data presented before:
```bash
msf > use auxiliary/scanner/elasticsearch/indices_enum
```
2020-10-05 10:44:06 +00:00
{% embed url="https://github.com/theMiddleBlue/nmap-elasticsearch-nse" %}
2022-05-01 13:25:53 +00:00
## Shodan
2020-10-05 10:44:06 +00:00
* `port:9200 elasticsearch`
2022-04-28 16:01:33 +00:00
<details>
2024-01-05 22:36:48 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-05 22:36:48 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-05 22:36:48 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 12:24:06 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-05 22:36:48 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>