没有钱的安全部之系统日志安全
Elastic Stack 5.0发布以来,基本上中小企业都可以使用开源的技术来做安全,特别是本人在一个没有钱的安全部。beats的加入让日志更加灵活,甚至可以通过beats优化部分安全运维的工作。本文尽可能还原真实部署过程中遇到的问题,也许有点杂乱,希望能对各位同学起到抛砖引玉的作用。
Elasticsearch 版本5.0.1
Logstash 版本5.1.1(暂时未发现logstash 5.x版本elkb架构的影响)
Kibana 版本5.0.1(版本尽量与elasticsearch统一)
jdk 版本8u131
Redis 版本3.0.7
Filebeat 版本5.4
Packetbeat 版本5.4
Winlogbeat 版本5.4
readonlyrest 版本1.13.0(此版本仅适合elastic5.0.1),主要功能是对elasticsearch设置角色和权限控制。
数据流:beats -> redis -> logstash -> elasticsearch
beats的数据发送到redis,redis做统一的入口,同时可做队列和缓存,本人在一个没有钱的安全部,elasticsearch都是业务系统的存储挂过来的,当io承压的时候,可以紧急kill掉redis(这里无法顾忌损失的日志,毕竟没钱)。logstash通过input获取redis中的数据,通过filter对数据做处理,最后output发送到elasticsearch。
根据官方文档说明,需要修改/etc/security/limits.conf
文件,在文件最后添加(假设运行elasticsearch的用户名为es)
#elasticsearch setting
es - nofile 65536
es - nproc 2048
es - as unlimited
执行 sysctl -p
命令加载系统参数,启用配置。
执行 sysctl -w vm.max_map_count=262144
命令设置最大值。
配置修改好后,可以使用 ulimit -a
查看当前用户配置。
修改elasticsearch配置文件config/elasticsearch.yml
配置节点名字,设置为主节点。
# Use a descriptive name for the node:
#
node.name: local-node-1
node.master: true
node.data: true
使用非root用户,启动elasticsearch(作为守护进程启动)
bin/elasticsearch -d -p pid
可查看本地是否开启9200端口,判断elasticsearch是否正常运行。
若需要对elasticsearch进行角色控制与权限控制,可以安装readonlyrest插件(这里采用离线安装)
bin/elasticsearch-plugin install file:///path/to/your/plugins
配置readonlyrest的相关权限样例
# ------------- Plugins -------------------
#readonlyrest
#
readonlyrest:
enable: true
response_if_req_forbidden: Forbidden by ReadonlyREST ES plugin
access_control_rules:
- name: "::KIBANA-SRV::"
auth_key: kibana:kibana
type: allow
- name: "::LOGSTASH::"
auth_key: logstash:logstash
type: allow
- name: app1
type: allow
kibana_access: rw
groups: ["app1team"]
indices: [".kibana", ".kibana-devnull", "app1_"]
- name: app2
type: allow
kibana_access: rw
groups: ["app2team"]
indices: [".kibana", ".kibana-devnull", "app2_"]
users:
- username: huahua
auth_key: huahua:123456
groups: ["app1team"]
- username: huahua1
auth_key: huahua1:123456
groups: ["app2team"]
修改kibana的配置文件 config/kibana.yml
配置kibana的访问地址和端口
server.port: 5601
server.host: "localhost"
配置部署的elasicsearch的数据接口(这里是localhost)
# The URL of the Elasticsearch instance to use for all your queries.
elasticsearch.url: "http://localhost:9200"
配置部署的elasicsearch基础认证(这里是kibana/kibana)
# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
elasticsearch.username: "kibana"
elasticsearch.password: "kibana"
启动kibana
bin/kibana
后台启动kibana
nohup bin/kibana &>/dev/null &
若考虑到数据安全性,可配置SSL
# Paths to the PEM-format SSL certificate and SSL key files, respectively. These
# files enable SSL for outgoing requests from the Kibana server to the browser.
server.ssl.cert: /path/to/your/cert
server.ssl.key: /path/to/your/key
logstash主要作用就是处理数据,并发给elasticsearch。
logstash关键的一环在于设计logstash配置文件。
input -获取redis数据
filter - 处理message数据
output -发送数据elasticsearch
基本上使用grok、mutate、date三个插件能完成大部分的需求。
input {
redis {
host => "localhost"
password => "000"
data_type => "list"
key => "APP1"
db => 1
}
}
filter {
if "A" in [type] {
grok {
match => {
"message" => "(?<logdate>.*)"
}
}
date {
match => [ "logdate", "YYYY-M-dd HH:mm:ss"]
}
mutate {
remove_field => ["beat","input_type","logdate"]
}
}
}
output {
elasticsearch {
hosts => ["localhost"]
index => "app1"
user => "000"
password => "000"
}
}
基本上使用grok、mutate、date三个插件能完成大部分的需求。redis主要作用就是缓存日志信息,并发给logstash。
安装redis
$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar xzf redis-3.2.8.tar.gz
$ cd redis-3.2.8
$ make
启动redis src/redis-server
可以根据 redis3.0 configure 进行相关配置
### GENERAL ###
# 以守护进程启动
daemonize yes
#
#配置PID文件路径
pidfile /to/your/path/redis.pid
#
#绑定redis服务器IP
bind 127.0.0.1
#
### SECURITY ###
## redis认证
requirepass filebeat
Beats轻量级组件,可以输出各种不通类型的数据到ELK中。
Packetbeat – 分析服务器应用程序的网络数据包 Metricbeat – 监测服务器性能 Heartbeat – 监测服务器心跳 Filebeat – 处理文件日志 Winlogbeat – 处理windows系统日志
windows安装beats组件可能会遇到的问题:
Q1 – powershell 脚本禁止执行
管理员执行powershell
执行
set-ExecutionPolicy RemoteSigned
安全起见,安装完服务后执行
set-ExecutionPolicy AllSigned
Q2 – Packetbeat服务无法启动确保计算机安装 WinPcap
根据官方文档,安装 Filebeat
修改Filebeat配置文件
输入配置中,修改需要监控的log日志路径,设置document_type(样例中设置type为tomcat),若中文日志出现乱码,可以加上encoding。
#=== input ===
- input_type: log
# Paths that should be crawled and fetched. Glob based paths.
paths:
- C:\filebeat\*
# encoding: gbk
document_type: accesslog
若日志文件需要配置多行解析的话,注意match是before还是after。
multiline:
pattern: ^#{4}
negate: true
match: after
通用配置中,附加标签,便于区分(样例中的log为APP1应用系统)
#=== General ===
tags: ["APP1"]
fields_under_root: true
fields:
level: debug
输出配置中,选择输出的目标,并修改(样例中输出目标为Redis)
#=== Outputs ===
output.redis:
hosts: ["redis_host"]
password: "filebeat"
key: "APP1"
db: 1
timeout: 5
启动Filebeat(powershell中启动) PS C:\Program Files\Filebeat> Start-Service filebeat
windows的日志主要考虑是量大的问题,这里只收取windows的安全日志,后续的量依然非常大,可以考虑通过登录类型(即event_data中LogonType字段),来收取安全日志信息。
输出配置中,选择输出的目标,并修改(样例中输出目标为Redis)
winlogbeat.event_logs:
# - name: Application
# ignore_older: 72h
- name: Security
tags: ["APP1"]
output.redis:
hosts: ["localhost"]
password: "000"
key: "app"
db: 1
timeout: 5
目前遇到的报错及异常主要有以下几种:
若发现elasticsearch生成hprof文件,则elasticsearch内存溢出。
可以通过修改jvm.options配置文件,扩大jvm内存限制
-Xms2g
-Xmx2g
Increased maximum number of open files to 10032 (it was
originally set to 1024).
解决报错:
修改配置文件/etc/security/limits.conf
sysctl -p
启动配置
WARNING: The TCP backlog setting of 511 cannot be
enforced because /proc/sys/net/core/somaxconn is set to
the lower value of 128.
somaxconn定义了系统中每一个端口最大的监听队列的长度,默认配置为128。
解决报错:echo 1024 > /proc/sys/net/core/somaxconn
WARNING overcommit_memory is set to 0! Background save
may fail under low memory condition. To fix this issue
add 'vm.overcommit_memory = 1' to /etc/sysctl.conf
and then reboot or run the command 'sysctl
vm.overcommit_memory=1' for this to take effect.
内核参数overcommit_memory 设置系统内存分配策略,可选值:0、1、2。
0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2 表示内核允许分配超过所有物理内存和交换空间总和的内存
解决报错:echo 1 > /proc/sys/vm/overcommit_memory
WARNING you have Transparent Huge Pages (THP) support
enabled in your kernel. This will create latency and
memory usage issues with Redis. To fix this issue run
the command 'echo never > /sys/kernel/mm/
transparent_hugepage/enabled' as root, and add it to
your /etc/rc.local in order to retain the setting after
a reboot. Redis must be restarted after THP is
disabled.
THP(Transparent Huge Pages)是一个使管理Huge Pages自动化的抽象层。
解决报错:echo never > /sys/kernel/mm/transparent_hugepage/enabled
百度简单处理,直接删掉.kibana的索引,但是这样会导致辛苦建立的kibana各种图形丢失。(深坑啊)
正确姿势
curl -s http://localhost:9200/.kibana/_recovery?pretty
curl -XPUT 'localhost:9200/.kibana/_settings' -d '
{
"index" : {
"number_of_replicas" : 0
}
}'
自此,没钱的安全部不怕表哥们撸完清理日志了。