你的Elasticsearch在“裸奔”嗎?

題記

安全事件頻發,
在這裏插入圖片描述
在這裏插入圖片描述

2018上半年的羣友的討論:
在這裏插入圖片描述
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

限制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

步驟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的幾個免費安全插件之一以啓用身份驗證:

  1. Github上提供了Elasticsearch的ReadonlyREST插件。
    它提供不同類型的身份驗證,從基本到LDAP,以及索引和操作級訪問控制。
    git地址:https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin

  2. 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/

Elasticsearch基礎
Elasticsearch基礎、進階、實戰第一公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章