天天看點

你的Elasticsearch在“裸奔”嗎?define proxy upstream to Elasticsearch via loopback interface inenable TLSProxy for Elasticsearchuse defined upstream with the name "elasticsearch"

題記

安全事件頻發,

你的Elasticsearch在“裸奔”嗎?define proxy upstream to Elasticsearch via loopback interface inenable TLSProxy for Elasticsearchuse defined upstream with the name "elasticsearch"
你的Elasticsearch在“裸奔”嗎?define proxy upstream to Elasticsearch via loopback interface inenable TLSProxy for Elasticsearchuse defined upstream with the name "elasticsearch"

2018上半年的群友的讨論:

你的Elasticsearch在“裸奔”嗎?define proxy upstream to Elasticsearch via loopback interface inenable TLSProxy for Elasticsearchuse defined upstream with the name "elasticsearch"
http://www.safedog.cn/news.html?id=3212 https://www.easyaq.com/news/1184405110.shtml

安全隐患劃重點:

1、印度:沒有設定Elasticsearch叢集安全權限;

2、婚慶網站:Elasticsearch伺服器暴露到公網。

3、群友:9200端口映射到外網。

保障Elasticsearch單節點或者叢集網絡安全必須提上日程!!

該如何保障Elasticsearch叢集的網絡安全呢?

1、不要将Elasticsearch暴露到Internet

必須強調這一點。即使在開發和測試中,也沒有理由讓您的叢集暴露于公共IP。

異地聯調,外網通路的場景各大公司都存在,但請千萬别 “裸奔”。

1.1 防火牆:限制公共端口

限制9200—— 叢集對外通路端口

iptables -A INPUT -i eth0 -p tcp --destination-port 9200 -s {PUBLIC-IP-ADDRESS-HERE} -j DROP

1

限制9300——叢集内部通信端口

iptables -A INPUT -i eth0 -p tcp --destination-port 9300 -s {PUBLIC-IP-ADDRESS-HERE} -j DROP

限制5601——kibana通路端口

iptables -A INPUT -i eth0 -p tcp --destination-port 5601 -s {PUBLIC-IP-ADDRESS-HERE} -j DROP

在此之後你可以放松一下! Elasticsearch将無法再從Internet通路。

1.2僅将Elasticsearch端口綁定到内網專有IP位址

将elasticsearch.yml中的配置更改為僅綁定到私有IP位址或将單個節點執行個體綁定到環回接口:

network.bind_host: 127.0.0.1

1.3在Elasticsearch和用戶端服務之間添加專用網絡

如果您需要從另一台計算機通路Elasticsearch,請通過VPN或任何其他專用網絡連接配接它們。

在兩台機器之間建立安全隧道的快速方法是通過SSH隧道:

ssh -Nf -L 9200:localhost:9200 user@remote-elasticsearch-server

然後,您可以通過SSH隧道從用戶端計算機通路Elasticsearch

curl

http://localhost:9200/_search

2、使用Nginx進行身份驗證和SSL / TLS

有幾個開源和免費解決方案提供Elasticsearch通路身份驗證,但如果你想要快速和簡單的東西,這裡是如何使用Nginx自己做

2.1 Nginx 自己生成

步驟1: 生成密碼檔案

printf "esuser:$(openssl passwd -crypt MySecret)n" > /etc/nginx/passwords

步驟2:

如果您沒有官方證書,則生成自簽名SSL證書…

sudo mkdir /etc/nginx/ssl

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout

/etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

2

3

4

步驟3:

使用SSL添加代理配置并激活基本身份驗證到/etc/nginx/nginx.conf

(注意我們期望/ etc / nginx / ssl /中的SSL證書和密鑰檔案)。 例:

define proxy upstream to Elasticsearch via loopback interface in

http {

upstream elasticsearch {

server 127.0.0.1:9200;

}

server {

enable TLS

listen 0.0.0.0:443 ssl;

ssl_certificate /etc/nginx/ssl/nginx.crt;

ssl_certificate_key /etc/nginx/ssl/nginx.key

ssl_protocols TLSv1.2;

ssl_prefer_server_ciphers on;

ssl_session_timeout 5m;

ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";

Proxy for Elasticsearch

location / {

auth_basic "Login";

auth_basic_user_file passwords;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_set_header X-NginX-Proxy true;

use defined upstream with the name "elasticsearch"

proxy_pass

http://elasticsearch/;

proxy_redirect off;

if ($request_method = OPTIONS ) {

add_header Access-Control-Allow-Origin "*";

add_header Access-Control-Allow-Methods "GET, POST, , PUT, OPTIONS";

add_header Access-Control-Allow-Headers "Content-Type,Accept,Authorization, x-requested-with";

add_header Access-Control-Allow-Credentials "true";

add_header Content-Length 0;

add_header Content-Type application/json;

return 200;

重新啟動Nginx并嘗試通過通路Elasticsearch

https://localhost/_search

2.2 Elasticsearch官方安全工具xpack

6.3版本之後已經內建在一起釋出, 無需額外安裝。

但屬于收費功能,免費試用1個月。

如果是土豪使用者,不妨一買。

2.3 Elasticsearch的第三方安全插件

您可以安裝和配置Elasticsearch的幾個免費安全插件之一以啟用身份驗證:

Github上提供了Elasticsearch的 ReadonlyREST插件。

它提供不同類型的身份驗證,從基本到LDAP,以及索引和操作級通路控制。

git位址:

https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin

SearchGuard是Elasticsearch的免費安全插件(部分進階功能收費),包括基于角色的通路控制和SSL / TLS加密的節點到節點通信。

每個Elasticsearch叢集都可以使用其他企業功能,如LDAP身份驗證或JSON Web令牌身份驗證。

3、審計和警報

與儲存敏感資料的任何類型的系統一樣,您必須非常密切地 監控它。

這意味着不僅要監控其各種 名額(其突然變化可能是問題的早期迹象),還要觀察其 日志。

許多監控供應商都支援Elasticsearch。應該收集日志并将其實時發送到日志管理服務,其中需要設定 警報以監視任何 異常或可疑活動等。

對名額和日志發出警報意味着您将盡早發現安全漏洞,并采取适當的措施,希望能夠防止進一步的損害。

4、備份和恢複資料

Elasticdump是一個非常友善的工具,可以根據Elasticsearch查詢備份/恢複或重新索引資料。

要備份完整索引,

`Elasticsearch快照API ](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-snapshots.html)

是正确的工具。 快照API提供了建立和恢複整個索引,存儲在檔案或Amazon S3存儲桶中的快照的操作。

我們來看一下Elasticdump和快照備份和恢複的一些示例。

1)安裝包含 node package manager的elasticdump包。

npm i elasticdump -g

2)将查詢語句備份為zip檔案。

elasticdump --input='

http://username:password@localhost:9200/myindex

' --searchBody '{"query" : {"range" :{"timestamp" : {"lte": 1483228800000}}}}' --output=$ --limit=1000 | gzip > /backups/myindex.gz

3)從zip檔案中恢複。

zcat /backups/myindex.gz | elasticdump --input=$ --output=

http://username:password@localhost:9

5、使用最新的Elasticsearch版本。

這是一般的最佳實踐,因為在舊版本中,版本5.x中存在特定的漏洞。如果您仍在使用1.x或2.x,請務必 禁用動态腳本。 Elasticsearch允許使用腳本來評估自定義表達式,但正如Elastic所記錄的那樣,使用non-sandboxed 語言可能是一個問題。

目前最新版本6.5.x,新增了space功能,安全+角色劃分更增強一步!

在這裡插入圖檔描述

參考:

https://logz.io/blog/securing-elasticsearch-clusters/ https://sematext.com/blog/elasticsearch-security-authentication-encryption-backup/

文章轉載自阿裡雲 MVP銘毅,

檢視原文

繼續閱讀