欢迎光临,赤鹿小组
记录过程,分享经验

ELK6.x—SearchGuard安全加固

概念

SearchGuard使用基于角色的访问控制(RBAC)在您的集群、索引、字段和文档上保护您最敏感的数据;它涵盖了所有的内容,增加了加密、认证、授权、审计记录、多租户和扩展功能,以满足您的合规性需求;它还可以让你完全控制整个Elastisearch环境,保护Elastic堆栈的所有组件,包括Kibana、Logstash和Beats。

其实ELK本身也有一个安全加固的插件包叫xpack,之前它是需要付费的,后来官方从6.87.1版本开始默认免费提供了,但还是会有公司继续用着其他6.x的旧版本,这时候就可以使用searchguard社区版来代替它做安全加固,防止ELK服务裸奔在互联网上。


环境说明

设备IP 系统版本
192.168.199.66 CentOS 7+
192.168.199.67 CentOS 7+
192.168.199.68 CentOS 7+

笔者已经在这三台设备上搭建好了ELK v6.6.1版本!

笔者在本文中所用到的searchguard插件包都在链接中,有需要的小伙伴可以自行下载!
链接:https://pan.baidu.com/s/1FrbbY8ma9-UO66Bl59Dp1Q
提取码:i03d


安装Elasticsearch安全模块

可以参考官网 https://docs.search-guard.com/6.x-24/search-guard-versions ,需要在每台节点上都下载对应es版本的search-guard插件包!

[[email protected] ~]# cd /home/elkuser/elasticsearch-6.6.1/
[[email protected] elasticsearch-6.6.1]# chown elkuser:elkuser search-guard-6-6.6.1-24.1.zip
[[email protected] elasticsearch-6.6.1]# su elkuser
[[email protected] elasticsearch-6.6.1]$ ./bin/elasticsearch-plugin install -b file:///home/elkuser/elasticsearch-6.6.1/search-guard-6-6.6.1-24.1.zip

安装Kibana安全模块

请自行下载对应kibana版本的search-guard-kibana-plugin插件包!

[[email protected] ~]# cd /home/elkuser/kibana-6.6.1-linux-x86_64
[[email protected] kibana-6.6.1-linux-x86_64]# ./bin/kibana-plugin install file:///home/elkuser/kibana-6.6.1-linux-x86_64/search-guard-kibana-plugin-6.6.1-18.1.zip

生成证书

笔者在这里推荐使用离线的SearchGuard TLS工具 https://releases.floragunn.com/search-guard-tlstool/1.8/search-guard-tlstool-1.8.zip 生成SearchGuard所需的所有证书!
解压后主要看tools目录:

只需要修改my.yml文件,修改nodes下的name和ip地址,其他无需改动,注意缩进!
下图则是笔者修改后的样子,简单明了!

执行生成证书的命令后,会输出下方的一些信息:

[[email protected] tools]# ./sgtlstool.sh -c my.yml -crt
WARNING: JAVA_HOME not set, will use /usr/bin/java
Using signing certificate: /home/certtools/tools/out/root-ca.pem
Created 6 node certificates.
Passwords for the private keys of the node certificates have been auto-generated. The passwords are stored in the config snippet files.
Created 2 client certificates.
Passwords for the private keys of the client certificates have been auto-generated. The passwords are stored in the file "client-certificates.readme"

此时生成的证书会在out目录下:

将这些文件全部cp到elasticsearch的config目录下:

[[email protected] tools]# cd out/
[[email protected] out]# cp *.* /home/elkuser/elasticsearch-6.6.1/config/

Elasticsearch TLS配置

每台节点使用自己的证书配置文件内容,例如笔者在修改my.yml文件时,写了name: es1的是ip: 192.168.199.66这台节点,所以修改es配置文件时,使用的就是es1_elasticsearch_config_snippet.yml这份文件的内容,以此类推!

下图是66节点上的配置,首先查看es1_elasticsearch_config_snippet内容,然后复制到elasticsearch.yml中,然后再加一些配置参数和修改searchguard.nodes_dn,直接配置为CN带*号,可以在后续扩容时不再需要每台es重启,实现无缝扩容!

修改后,配置文件的部分内容如下(其他节点操作雷同):

[[email protected] config]# cat elasticsearch.yml
......
searchguard.enterprise_modules_enabled: false
searchguard.audit.type: internal_elasticsearch
searchguard.enable_snapshot_restore_privilege: true
searchguard.check_snapshot_restore_write_privileges: true
searchguard.restapi.roles_enabled: ["sg_all_access"]
cluster.routing.allocation.disk.threshold_enabled: false
xpack.security.enabled: false

searchguard.ssl.transport.pemcert_filepath: es1.pem
searchguard.ssl.transport.pemkey_filepath: es1.key
searchguard.ssl.transport.pemkey_password: WekKRmly4J473ZFaYW24
searchguard.ssl.transport.pemtrustedcas_filepath: root-ca.pem
searchguard.ssl.transport.enforce_hostname_verification: false
searchguard.ssl.transport.resolve_hostname: false
searchguard.ssl.http.enabled: true
searchguard.ssl.http.pemcert_filepath: es1_http.pem
searchguard.ssl.http.pemkey_filepath: es1_http.key
searchguard.ssl.http.pemkey_password: 2O3XxjYJiNxKmWGah4Se
searchguard.ssl.http.pemtrustedcas_filepath: root-ca.pem
searchguard.nodes_dn:
- CN=*,OU=gx,O=gx
searchguard.authcz.admin_dn:
- CN=sgadmin

当全部节点都修改完毕后,就可以重启elasticsearch服务了,然后使用浏览器去访问es,会弹出授权窗口;至于用户名和密码,继续往下看吧!


修改超级用户密码

searchguard主要有5个配置文件,在plugins/search-guard-版本号/sgconfig目录下:
sg_action_groups.yml:定义权限,建议熟悉后修改
sg_config.yml:主配置文件,不建议修改
sg_internal_users.yml:用户密码以及对应的权限,需要修改
sg_roles_mapping.yml:定义用户的映射关系,不建议修改
sg_roles.yml:权限配置文件,建议熟悉后修改

默认有几个帐号需要修改密码:
一个是es的超级密码,这个用来登陆Kibana的,用户名:admin
一个是给Kibana连接ES的密码 (内部互联) 用户名:kibanaserver
一个是给logstash到ES的密码(内部互联)用户名:logstash

查看sg_internal_users.yml文件中的默认用户名和密码:

[[email protected] sgconfig]# pwd
/home/elkuser/elasticsearch-6.6.1/plugins/search-guard-6/sgconfig
[[email protected] sgconfig]# cat sg_internal_users.yml 
# This is the internal user database
# The hash value is a bcrypt hash and can be generated with plugin/tools/hash.sh

#password is: admin
admin:
  readonly: true
  hash: $2y$12$xsUoXMgO8PicUJYj0htFjeQv.PLWlCnDiHbZC/7c/b6FkYznv6hBO
  roles:
    - admin
  attributes:
    #no dots allowed in attribute names
    attribute1: value1
    attribute2: value2
    attribute3: value3

#password is: logstash
logstash:
  hash: $2y$12$xsUoXMgO8PicUJYj0htFjeQv.PLWlCnDiHbZC/7c/b6FkYznv6hBO
  roles:
    - logstash

#password is: kibanaserver
kibanaserver:
  readonly: true
  hash: $2y$12$xsUoXMgO8PicUJYj0htFjeQv.PLWlCnDiHbZC/7c/b6FkYznv6hBO

#password is: kibanaro
kibanaro:  
  hash: $2a$12$JJSXNfTowz7Uu5ttXfeYpeYE0arACvcwlPBStB1F.MI7f0U9Z4DGC
  roles:
    - kibanauser
    - readall

#password is: readall
readall:
  hash: $2a$12$ae4ycwzwvLtZxwZ82RmiEunBbIPiAmGZduBAjKN0TXdwQFtCwARz2
  #password is: readall
  roles:
    - readall

#password is: snapshotrestore
snapshotrestore:
  hash: $2y$12$DpwmetHKwgYnorbgdvORCenv4NAK8cPUg8AI6pxLCuWf/ALc0.v7W
  roles:
    - snapshotrestore

默认情况下,用户名与密码都是一样的,为了安全起见需要修改,其中内部互联的用户密码可以看情况修改,但是admin的账号密码必须修改,可以使用自带的hash.sh工具来修改,假设我要修改密码为123qweAAA,会输出一串字符串,操作如下:

[[email protected] tools]# pwd
/home/elkuser/elasticsearch-6.6.1/plugins/search-guard-6/tools
[[email protected] tools]# ./hash.sh -p 123qweAAA
$2y$12$FEORHsvp0JkqFVXrkbsrpugBL9UrC1p6ogYrGujWMIX0t8.tNv8w2

然后我们将这个字符串替换在sg_internal_users.yml文件中,用于admin的密码:

另外,由于笔者的es索引名是自定义的,所以默认情况下logstash用户对非logstash或beat开头的索引只有只读权限,需要将logstash-*改为*;而且如果需要监控logstash,还得为此添加监控的权限,因此sg_roles.yml的部分配置如下:

[[email protected] sgconfig]# pwd
/home/elkuser/elasticsearch-6.6.1/plugins/search-guard-6/sgconfig
[[email protected] tools]# cat sg_roles.yml
......
sg_logstash:
  cluster:
    - CLUSTER_MONITOR
    - CLUSTER_COMPOSITE_OPS
    - indices:admin/template/get
    - indices:admin/template/put
    - cluster:monitor/xpack/info
    - cluster:monitor/main
    - cluster:admin/xpack/monitoring/bulk
  indices:
    '*':
      '*':
        - CRUD
        - CREATE_INDEX
    '*beat*':
      '*':
        - CRUD
        - CREATE_INDEX
    '?monitor*':
      '*':
        - INDICES_ALL

写入配置

SearchGuard插件至此已全部配置完成,用户密码也修改了,但是此时是不生效的,因为我们还没有把配置写入到Elasticsearch中去,需要将之前的几个证书cp到plugins/search-guard-6/tools/目录下,涉及到的证书有:
root-ca.pem #CA公钥
sgadmin.key #管理员私钥
sgadmin.pem #管理员公钥

写入配置需用到sgadmin.sh,而该命令的-keypass参数的值是client-certificates.readme文件里CN=sgadmin Password的值:

[[email protected] config]# pwd
/home/elkuser/elasticsearch-6.6.1/config
[[email protected] config]# cp root-ca.pem sgadmin.key sgadmin.pem ../plugins/search-guard-6/tools/
[[email protected] config]# cd ../plugins/search-guard-6/tools/
[[email protected] tools]# chmod +x sgadmin.sh
[[email protected] tools]# cat /home/elkuser/elasticsearch-6.6.1/config/client-certificates.readme 
......
CN=sgadmin Password: sEqVJAbjsnTWXo5WXval
CN=intrauser Password: aIJiUOjuLM6oZttLVJ61
[[email protected] tools]# ./sgadmin.sh -cacert root-ca.pem -cert sgadmin.pem -key sgadmin.key -keypass sEqVJAbjsnTWXo5WXval -nhnv -icl -h 192.168.199.66 -p 9300 -cd ../sgconfig/

以上的写入配置操作,只需要在es主节点上运行一次即可,不需要在每台节点上都运行,因为插件的密码权限等配置信息其实是存储在Elasticsearch上的,运行命令后会在Elasticsearch上建立一个searchguard的索引。
说明:如果修改了用户名密码等信息,每一次修改都要重复使用这个写入命令让配置生效!


Kibana TLS配置

主要就是修改kibana的配置文件,修改的内容有连接es的账号密码,还有就是es做了加密,因此url得写成https,还有一些ssl证书的内容,修改完毕后重启kibana服务即可。

[[email protected] kibana-6.6.1-linux-x86_64]# cat config/kibana.yml
server.port: 5601
server.host: "192.168.199.66"
elasticsearch.url: "https://192.168.199.66:9200"
elasticsearch.username: "admin"
elasticsearch.password: "123qweAAA"
elasticsearch.ssl.certificateAuthorities: [ "/home/elkuser/elasticsearch-6.6.1/config/root-ca.pem" ]
elasticsearch.ssl.verificationMode: certificate
xpack.security.enabled: false
xpack.reporting.encryptionKey: "122333444455555666666777777788888888"
xpack.reporting.capture.browser.chromium.disableSandbox: false

Logstash TLS配置

logstash的output配置就是添加TLS证书认证,而user选择内部互联的logstash,默认密码与用户名一致,还要带上ca公钥,以下只展示输出插件这块的内容。

[[email protected] logstash-6.6.1]# cat config/chilu.cfg
......
output {
   elasticsearch {
      hosts => ["192.168.199.66:9200","192.168.199.67:9200","192.168.199.68:9200"]
      user => "logstash"
      password => "logstash"
      ssl => true
      cacert => "/home/elkuser/elasticsearch-6.6.1/config/root-ca.pem"
      index => "chilu%{index_day}"
   }
}

如果需要监控logstash,需要将ca公钥证书拷贝到logstash的目录下,而且还需要修改logstash.yml配置文件,操作即配置内容如下。

[[email protected] logstash-6.6.1]# cp /home/elkuser/elasticsearch-6.6.1/config/root-ca.pem ./
[[email protected] logstash-6.6.1]# cat config/logstash.yml
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: "logstash"
xpack.monitoring.elasticsearch.password: "logstash"
xpack.monitoring.elasticsearch.url: ["https://192.168.199.66:9200"]
xpack.monitoring.elasticsearch.ssl.ca: root-ca.pem
xpack.monitoring.elasticsearch.sniffing: true
xpack.monitoring.collection.interval: 30s
xpack.monitoring.collection.pipeline.details.enabled: true

Curl写法

大家都知道elasticsearch是支持RESTful API的,因此我们可以直接通过curl命令来操作elasticsearch,但是当使用searchguard加密后,curl请求它则需要带上ca公钥证书和用户名密码信息。

例如我们查看下es集群的健康状态,输出如下内容中可以看出状态为green

[[email protected] ~]# curl --tlsv1 -XGET 'https://192.168.199.66:9200/_cluster/health?pretty' --cacert '/home/elkuser/elasticsearch-6.6.1/config/root-ca.pem' --user admin:123qweAAA
{
  "cluster_name" : "gzes",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 36,
  "active_shards" : 73,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

SearchGuard加密ELK的内容到此为止咯,有疑问的小伙伴欢迎留言评论,觉得实用的小伙伴也欢迎点赞👍!

赞(2)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址