2020-01-10

题记

安全事件频发,


在这里插入图片描述
在这里插入图片描述

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—— 集群对外访问端口

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

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

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

限制5601——kibana访问端口

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

在此之后你可以放松一下! Elasticsearch将无法再从Internet访问。

1.2仅将Elasticsearch端口绑定到内网专有IP地址

将elasticsearch.yml中的配置更改为仅绑定到私有IP地址或将单个节点实例绑定到环回接口:

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. network.bind_host: 127.0.0.1
    • 1

</pre>

1.3在Elasticsearch和客户端服务之间添加专用网络

如果您需要从另一台计算机访问Elasticsearch,请通过VPN或任何其他专用网络连接它们。
在两台机器之间建立安全隧道的快速方法是通过SSH隧道:

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

然后,您可以通过SSH隧道从客户端计算机访问Elasticsearch

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. curl http://localhost:9200/_search
    • 1

</pre>

2、使用Nginx进行身份验证和SSL / TLS

有几个开源和免费解决方案提供Elasticsearch访问身份验证,但如果你想要快速和简单的东西,这里是如何使用Nginx自己做

2.1 Nginx 自己生成

步骤1: 生成密码文件

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

步骤2:

如果您没有官方证书,则生成自签名SSL证书…

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. sudo mkdir /etc/nginx/ssl

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

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

    • 1
    • 2
    • 3
    • 4

</pre>

步骤3:

使用SSL添加代理配置并激活基本身份验证到/etc/nginx/nginx.conf
(注意我们期望/ etc / nginx / ssl /中的SSL证书和密钥文件)。 例:

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. # define proxy upstream to Elasticsearch via loopback interface in

  2. http {

  3. upstream elasticsearch {

  4. server 127.0.0.1:9200;

  5. }

  6. }

  7. server {

  8. # enable TLS

  9. listen 0.0.0.0:443 ssl;

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

  11. ssl_certificate_key /etc/nginx/ssl/nginx.key

  12. ssl_protocols TLSv1.2;

  13. ssl_prefer_server_ciphers on;

  14. ssl_session_timeout 5m;

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

  16. # Proxy for Elasticsearch

  17. location / {

  18. auth_basic "Login";

  19. auth_basic_user_file passwords;

  20. proxy_set_header X-Real-IP $remote_addr;

  21. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  22. proxy_set_header Host $http_host;

  23. proxy_set_header X-NginX-Proxy true;

  24. # use defined upstream with the name "elasticsearch"

  25. proxy_pass http://elasticsearch/;

  26. proxy_redirect off;

  27. if ($request_method = OPTIONS ) {

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

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

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

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

  32. add_header Content-Length 0;

  33. add_header Content-Type application/json;

  34. return 200;

  35. }

  36. }

    • 1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38 

</pre>

重新启动Nginx并尝试通过访问Elasticsearch

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. https://localhost/_search
    • 1

</pre>

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包。

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

  1. npm i elasticdump -g
    • 1

</pre>

2)将查询语句备份为zip文件。

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

3)从zip文件中恢复。

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; margin: 0px; padding: 8px 0px 6px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; background: rgb(241, 239, 238); border: 1px solid rgb(226, 226, 226) !important; display: block; border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; font-size: 10px; line-height: 12px;">

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

</pre>

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铭毅,查看原文

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容