如何在CentOS上使用Filebeat进行系统审计
在CentOS系统上,可通过YUM仓库或RPM包安装Filebeat:
sudo yum install -y filebeat
filebeat-7.14.0-x86_64.rpm),然后执行:wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-x86_64.rpm
sudo rpm -ivh filebeat-7.14.0-x86_64.rpm
编辑Filebeat主配置文件/etc/filebeat/filebeat.yml,重点设置输入源、输出目标及审计相关参数:
/var/log/secure、系统日志/var/log/messages),避免收集无关日志:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/secure # CentOS认证日志(记录登录、sudo使用等)
- /var/log/messages # 系统级日志(记录内核、服务关键事件)
exclude_lines: ["DBG"] # 排除调试信息,减少噪音
exclude_files: [".gz"] # 忽略压缩文件
fields参数为日志添加type(日志类型)和log_topic(日志主题),便于后续在Elasticsearch中分类检索:fields:
type: "system_audit" # 自定义日志类型标识
log_topic: "security" # 日志主题(如安全审计)
fields_under_root: true # 将fields提升至日志顶层(方便ES检索)
localhost):output.elasticsearch:
hosts: ["localhost:9200"] # Elasticsearch地址
index: "system-audit-%{yyyy.MM.dd}" # 按日期分割索引(便于管理)
output.elasticsearch:
hosts: ["https://elasticsearch-host:9200"]
username: "elastic" # Elasticsearch用户名(如elastic)
password: "your_password" # 对应密码
配置完成后,启动Filebeat并设置为开机自启动:
sudo systemctl daemon-reload # 重新加载systemd配置
sudo systemctl enable filebeat.service # 开机自启动
sudo systemctl start filebeat.service # 启动服务
验证服务状态:
sudo systemctl status filebeat.service
# 正常运行时应显示"active (running)"
检查Filebeat是否正常收集系统日志:
sudo tail -f /var/log/filebeat/filebeat # 查看Filebeat自身日志
若日志中出现Successfully sent log lines或Publish event等关键词,说明日志已成功发送至Elasticsearch。
http://your-kibana-host:5601),进入Stack Management > Index Patterns,创建名为system-audit-*的索引模式(匹配Filebeat输出的索引名称)。system-audit-*索引模式,通过message字段过滤关键词(如Failed password、sudo),快速定位安全事件。elkuser)运行Filebeat,降低安全风险:sudo useradd -r -s /sbin/nologin elkuser
sudo chown -R elkuser:elkuser /etc/filebeat /var/log/filebeat
sudo systemctl edit filebeat.service
# 添加以下内容(覆盖默认配置)
[Service]
User=elkuser
Group=elkuser
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="9200" protocol="tcp" accept'
sudo firewall-cmd --reload
processors对日志进行结构化处理(如提取timestamp、user字段),提升检索效率:filebeat.inputs:
- type: log
paths: ["/var/log/secure"]
processors:
- dissect: # 解析secure日志中的时间、用户、IP等信息
tokenizer: "%{timestamp} %{user} %{action} %{ip}"
field: "message"
target_prefix: "" # 将解析结果添加至日志顶层
output.logstash:
hosts: ["localhost:5044"]
output.elasticsearch:
hosts: ["localhost:9200"]
通过以上步骤,即可在CentOS上使用Filebeat高效收集系统日志,并通过Elasticsearch+Kibana实现安全审计分析,及时发现潜在的安全威胁。