你的Elasticsearch在裸奔嗎?

題記
安全事件頻發,node

你的Elasticsearch在裸奔嗎?

你的Elasticsearch在裸奔嗎?
2018上半年的羣友的討論:nginx

你的Elasticsearch在裸奔嗎?
安全隱患劃重點:
一、印度:沒有設置Elasticsearch集羣安全權限;
二、婚慶網站:Elasticsearch服務器暴露到公網。
三、羣友:9200端口映射到外網。git

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

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

一、不要將Elasticsearch暴露到Internet
必須強調這一點。即便在開發和測試中,也沒有理由讓您的集羣暴露於公共IP。安全

異地聯調,外網訪問的場景各大公司都存在,但請千萬別「裸奔」。服務器

1.1 防火牆:限制公共端口
限制9200—— 集羣對外訪問端口網絡

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

限制9300——集羣內部通訊端口app

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

限制5601——kibana訪問端口

1iptables -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地址或將單個節點實例綁定到環回接口:

1network.bind_host: 127.0.0.1

1.3在Elasticsearch和客戶端服務之間添加專用網絡
若是您須要從另外一臺計算機訪問Elasticsearch,請經過***或任何其餘專用網絡鏈接它們。

在兩臺機器之間創建安全隧道的快速方法是經過SSH隧道:

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

而後,您能夠經過SSH隧道從客戶端計算機訪問Elasticsearch

1curl http://localhost:9200/_search

二、將身份驗證和SSL / TLS添加到Elasticsearch
有幾個開源和免費解決方案提供Elasticsearch訪問身份驗證,但若是你想要快速和簡單的東西,這裏是如何使用Nginx本身作

2.1 Nginx 本身生成
步驟1: 生成密碼文件
1printf "esuser:$(openssl passwd -crypt MySecret)\n" > /etc/nginx/passwords

步驟2:
若是您沒有官方證書,則生成自簽名SSL證書…

1sudo mkdir /etc/nginx/ssl
2
3sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout
4/etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

步驟3:
使用SSL添加代理配置並激活基自己份驗證到/etc/nginx/nginx.conf
(注意咱們指望/ etc / nginx / ssl /中的SSL證書和密鑰文件)。 例:

1# define proxy upstream to Elasticsearch via loopback interface in
2http {
3 upstream elasticsearch {
4 server 127.0.0.1:9200;
5 }
6}
7
8server {
9 # enable TLS
10 listen 0.0.0.0:443 ssl;
11 ssl_certificate /etc/nginx/ssl/nginx.crt;
12 ssl_certificate_key /etc/nginx/ssl/nginx.key
13 ssl_protocols TLSv1.2;
14 ssl_prefer_server_ciphers on;
15 ssl_session_timeout 5m;
16 ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
17
18 # Proxy for Elasticsearch
19 location / {
20 auth_basic "Login";
21 auth_basic_user_file passwords;
22 proxy_set_header X-Real-IP $remote_addr;
23 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
24 proxy_set_header Host $http_host;
25 proxy_set_header X-NginX-Proxy true;
26 # use defined upstream with the name "elasticsearch"
27 proxy_pass http://elasticsearch/;
28 proxy_redirect off;
29 if ($request_method = OPTIONS ) {
30 add_header Access-Control-Allow-Origin "*";
31 add_header Access-Control-Allow-Methods "GET, POST, , PUT, OPTIONS";
32 add_header Access-Control-Allow-Headers "Content-Type,Accept,Authorization, x-requested-with";
33 add_header Access-Control-Allow-Credentials "true";
34 add_header Content-Length 0;
35 add_header Content-Type application/json;
36 return 200;
37 }
38}

從新啓動Nginx並嘗試經過訪問Elasticsearch

1https://localhost/_search

2.2 Elasticsearch官方安全工具xpack
6.3版本以後已經集成在一塊兒發佈,無需額外安裝。
但屬於收費功能,免費試用1個月。
若是是土豪用戶,不妨一買。

2.3 Elasticsearch的第三方安全插件
您能夠安裝和配置Elasticsearch的幾個免費安全插件之一以啓用身份驗證:

Github上提供了Elasticsearch的ReadonlyREST插件。
它提供不一樣類型的身份驗證,從基本到LDAP,以及索引和操做級訪問控制。
git地址:http://t.cn/RZpF03g

SearchGuard是Elasticsearch的免費安全插件(部分高級功能收費),包括基於角色的訪問控制和SSL / TLS加密的節點到節點通訊。
每一個Elasticsearch集羣均可以使用其餘企業功能,如LDAP身份驗證或JSON Web令牌身份驗證。

三、審計和警報
與保存敏感數據的任何類型的系統同樣,您必須很是密切地監控它。
這意味着不只要監控其各類指標(其忽然變化多是問題的早期跡象),還要觀察其日誌。

許多監控供應商都支持Elasticsearch。應該收集日誌並將其實時發送到日誌管理服務,其中須要設置警報以監視任何異常或可疑活動等。

對指標和日誌發出警報意味着您將盡早發現安全漏洞,並採起適當的措施,但願可以防止進一步的損害。

四、備份和恢復數據
Elasticdump是一個很是方便的工具,能夠根據Elasticsearch查詢備份/恢復或從新索引數據。

要備份完整索引,Elasticsearch快照API`是正確的工具。 快照API提供了建立和恢復整個索引,存儲在文件或Amazon S3存儲桶中的快照的操做。

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

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

1 npm i elasticdump -g

2)將查詢語句備份爲zip文件。

1elasticdump --input='http://username:password@localhost:9200/myindex' --searchBody '{"query" : {"range" :{"timestamp" : {"lte": 1483228800000}}}}' --output=$ --limit=1000 | gzip > /backups/myindex.gz

3)從zip文件中恢復。

1zcat /backups/myindex.gz | elasticdump --input=$ --output=http://username:password@localhost:9

五、使用最新的Elasticsearch版本
這是通常的最佳實踐,由於在舊版本中,版本5.x中存在特定的漏洞。若是您仍在使用1.x或2.x,請務必禁用動態腳本。 Elasticsearch容許使用腳原本評估自定義表達式,但正如Elastic所記錄的那樣,使用non-sandboxed 語言多是一個問題。

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

你的Elasticsearch在裸奔嗎?

參考:
http://t.cn/EqqpjRw

http://t.cn/RDWl15I

http://t.cn/EGp38hU

http://t.cn/EGrHXkV

相關文章
相關標籤/搜索