https://codemiracle.blog.csdn.net/article/details/151958547?spm=1011.2415.3001.5331
安装与启动
bash
yum install audit audit-libs
sudo systemctl enable auditd --now
sudo systemctl status auditd
组件简介
组件 | 功能 |
---|---|
auditd |
审计守护进程 |
auditctl |
控制审计规则的命令行工具 |
ausearch |
搜索审计日志的工具 |
aureport |
生成审计报告的工具 |
audit.log |
日志文件(通常位于 /var/log/audit/audit.log ) |
常用命令
bash
# 查看审计状态
sudo auditctl -s
# 查看当前规则
sudo auditctl -l
# 使用示例
## 监控rm命令
sudo auditctl -w /usr/bin/rm -p x -k command_exec
## 监控多个删除命令
sudo auditctl -w /usr/bin/rm -p x -k file_deletion
sudo auditctl -w /usr/bin/unlink -p x -k file_deletion
sudo auditctl -w /usr/bin/rmdir -p x -k file_deletion
## 监控 /usr/bin/ 目录下的所有二进制文件
sudo auditctl -w /usr/bin/ -p x -k bin_execution
参数说明
-w:监控路径
-p:权限类型
r= 读
w= 写
x= 执行
a= 属性改变
-k:自定义关键词(用于搜索过滤)
## 监控文件访问
### 监控密码文件
sudo auditctl -w /etc/passwd -p wa -k passwd_change
### 监控ssh配置文件
sudo auditctl -w /etc/ssh/sshd_config -p wa -k ssh_config
### 监控sudoers文件
sudo auditctl -w /etc/sudoers -p wa -k sudoers_change
### 监控/etc 目录
sudo auditctl -w /etc/ -p wa -k etc_changes
### 监控其他重要目录
sudo auditctl -w /var/www/html/ -p wa -k web_content
sudo auditctl -w /opt/app/ -p wa -k app_files
## 监控系统调用
### 监控所有删除文件的系统调用
sudo auditctl -a always,exit -F arch=b64 -S unlink -S unlinkat -S rmdir -k file_deletion
### 监控文件权限更改
sudo auditctl -a always,exit -F arch=b64 -S chmod -S fchmod -S fchmodat -k file_permission
### 监控特权命令执行
sudo auditctl -a always,exit -F arch=b64 -S execve -k command_exec
### 监控特定用户的所有命令
sudo auditctl -a always,exit -F arch=b64 -S execve -F auid=1000 -k user_commands
### 监控非特权用户的特权操作
sudo auditctl -a always,exit -F arch=b64 -S execve -F euid=0 -F auid!=0 -k privilege_abuse
## 搜索和分析日志
### 按照关键词搜索
sudo ausearch -k file_deletion
### 按照时间搜索
sudo ausearch -ts today
sudo ausearch -ts "10/05/2025 08:00:00" -te "10/05/2025 18:00:00"
### 按照用户信息搜索
sudo ausearch -ua 1000 # 按用户ID
sudo ausearch -ui username # 按用户名
### 生成报告
# 生成文件访问报告
sudo aureport -f -i
# 生成命令执行报告
sudo aureport -x -i
# 生成用户活动报告
sudo aureport -u -i
# 生成汇总报告
sudo aureport --summary
# 生成今天的事件报告
sudo aureport -t
### 格式化输出
# 人性化显示
sudo ausearch -k file_deletion -i
# 只显示关键信息
sudo ausearch -k file_deletion --raw | aureport -f -i
配置文件方式配置
bash
# 配置文件
主配置:
/etc/audit/auditd.conf
规则文件:
/etc/audit/rules.d/audit.rules
# 编辑配置文件/etc/audit/rules.d/audit.rules 配置永久规则
# 监控命令执行
-w /usr/bin/rm -p x -k file_deletion
-w /usr/bin/unlink -p x -k file_deletion
-w /usr/bin/rmdir -p x -k file_deletion
# 监控重要文件
-w /etc/passwd -p wa -k passwd_file
-w /etc/shadow -p wa -k shadow_file
-w /etc/sudoers -p wa -k sudoers_file
# 系统调用规则
-a always,exit -F arch=b64 -S unlink -S unlinkat -S rmdir -k file_deletion_syscall
# 重新加载规则
sudo auditctl -R /etc/audit/rules.d/audit.rules
# 或者重启服务
sudo systemctl restart auditd
脚本1:实时监控
bash
#!/bin/bash
# realtime_audit_monitor.sh
echo "开始实时监控审计日志..."
sudo tail -f /var/log/audit/audit.log | while read line; do
if echo "$line" | grep -q -E "rm|unlink|rmdir"; then
echo "⚠️ 删除操作检测: $(date)"
echo "$line" | grep -o -E 'exe=.*|auid=.*|uid=.*'
echo "---"
fi
done
脚本2:每日报告
bash
#!/bin/bash
# daily_audit_report.sh
REPORT_FILE="/var/log/audit/daily_report_$(date +%Y%m%d).txt"
{
echo "=== 审计日报 $(date) ==="
echo "1. 文件删除操作:"
sudo ausearch -k file_deletion -ts yesterday -i
echo ""
echo "2. 特权命令执行:"
sudo ausearch -k privilege_escalation -ts yesterday -i
echo ""
echo "3. 今日汇总:"
sudo aureport --start yesterday --end today -i
} > $REPORT_FILE
# 发送邮件(如果有配置邮件)
# mail -s "审计日报 $(date)" admin@company.com < $REPORT_FILE
性能优化
bash
编辑 /etc/audit/auditd.conf
# 提高性能的配置
log_file = /var/log/audit/audit.log
max_log_file = 100
max_log_file_action = ROTATE
num_logs = 5
space_left = 250
space_left_action = email
action_mail_acct = root
admin_space_left = 50
admin_space_left_action = SUSPEND
案例
bash
案例1:监控网站目录
# 永久规则添加到 /etc/audit/rules.d/web.rules
-w /var/www/html/ -p wa -k web_content
-w /etc/nginx/ -p wa -k nginx_config
-w /etc/apache2/ -p wa -k apache_config
案例2:监控数据库相关操作
# 监控数据库文件和命令
-w /var/lib/mysql/ -p wa -k mysql_data
-w /usr/bin/mysql -p x -k mysql_command
-w /usr/bin/mysqldump -p x -k mysql_backup