基于Elastalert的安全告警剖析
作者:媒体转发 时间:2018-03-17 21:24
elastalert 是一款基于elasticsearch的开源告警产品(官方说明文档)。相信许多人都会使用ELK做日志收集系统,但是产生一个基于日志的“优秀”的安全告警确是一个难题。告警规则难编写,告警规则难管理等。本文是作者探索的安全告警的一些思路,希望能帮助到有需要的人。
本人对ELK告警处理思路:
elastalert 通过post的告警模式,post一个告警数据包到服务端,通过服务端匹配需要告警的对象,告警的方式,最终将安全告警发出。
告警对象(企业人员) 怎么来? 来源调用钉钉API、CMDB、LDAP。
告警方式 怎么选择?根据告警级别、告警来源(wazuh、驭龙HIDS、elastalert规则)采用不同的告警方式。

Elastic Stack v6.2.2 (适用于6.0+)
Elastalert v0.1.29
下载 elastalert 源码
git clone https://github.com/Yelp/elastalert.git安装依赖
pip install -r requirements.txt pip install "elasticsearch>=6.0.0"创建elastalert索引(Index)&映射(Mapping)
python elastalert/create_index.py --host localhost --port 9200 --index elastalert创建elastalert的配置文件 config.yaml :
# 告警规则存放的文件夹 rules_folder: myrules # 每2分钟查询一次elasticsearch run_every: minutes: 2 # 查询时间范围5分钟 buffer_time: minutes: 5 # 连接elasticsearch配置 es_host: localhost es_port: 9200 # elasticsearch认证,如果未使用可注释 es_username: kibana es_password: kibana # elastalert状态索引 writeback_index: elastalert开启elastalert
python elastalert/elastalert.py --config config.yaml elastalert规则类型官方规则类型描述并不是太清晰,以下给出alert方式为post的json数据,便于后续大家速查速写。
以下的规则类型均使用以下文档样本作触发告警:
doc = { "@timestamp": get_now(), "codec": "nodejs", "tags": "31", "level": "high", "server": "nginx", "status": "anystatus", "message": ">>> [ xxx ]: valid id error ." }elastalert索引中,hits表示规则命中条数;matches表示规则命中条数,并且匹配规则触发告警数量。
any类型说明:任何规则都会匹配, 查询返回的每个命中将生成一个警报。
规则:当匹配status字段为anystatus,触发告警。
# rule名称 name: any_rule # 规则类型 type: any # 监控索引 index: testalert # 监控时间1分钟内 timeframe: minutes: 1 # Elastic DSL语法 filter: - term: status: "anystatus" # 告警方式 alert: post # 服务端接口 http_post_url: "http://localhost:8088/alertapi" http_post_static_payload: # 添加到post包中的数据,规则名称 rule_name: any_rule # 添加到post包中的数据,告警级别 rule_level: mediumpost结果:
{ "status": "anystatus", "_type": "mydata", "level": "high", "num_hits": 5, "@timestamp": "2018-01-31T02:26:52.268477Z", "rule_level": "medium", "server": "nginx", "rule_name": "any_rule", "_index": "testalert", "num_matches": 5, "message": ">>> [ xxx ]: valid id error .", "_id": "AWFKCd4a5xzN_sFQhZgO", "codec": "nodejs", "tags": "31" } blacklist类型说明:黑名单规则将检查黑名单中的某个字段,如果它在黑名单中则匹配。
规则:当字段status匹配到关键字hacker、huahua,触发告警
name: blacklist_rule type: blacklist index: testalert timeframe: minutes: 1 compare_key: status blacklist: - "hacker" - "huahua" alert: post http_post_url: "http://localhost:8088/alertapi" http_post_static_payload: rule_name: blacklist_rule rule_level: medium若关键字在文件中,可用 - "!file /path/to/file",目测关键字不支持正则(未测过)。


