auditd 是 Linux 系统的内核级审计工具,用于系统安全审计、合规性监控和故障排查。sudo apt update && sudo apt install auditd audispd-plugins
sudo yum install audit audit-libs# 或sudo dnf install audit audit-libs
sudo systemctl enable auditdsudo systemctl start auditdsudo systemctl status auditd
auditd | |
auditctl | |
ausearch | |
aureport | |
audit.log | /var/log/audit/audit.log) |
sudo auditctl -s
sudo auditctl -l
sudo auditctl -w /usr/bin/rm -p x -k command_exec
sudo auditctl -w /usr/bin/rm -p x -k file_deletionsudo auditctl -w /usr/bin/unlink -p x -k file_deletionsudo auditctl -w /usr/bin/rmdir -p x -k file_deletion
sudo auditctl -w /usr/bin/passwd -p x -k password_changesudo auditctl -w /usr/bin/sudo -p x -k privilege_escalation
# 监控 /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
sudo auditctl -w /etc/ssh/sshd_config -p wa -k ssh_config
sudo auditctl -w /etc/sudoers -p wa -k sudoers_change
sudo auditctl -w /etc/ -p wa -k etc_changes
sudo auditctl -w /var/www/html/ -p wa -k web_contentsudo 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 todaysudo ausearch -ts "10/05/2025 08:00:00" -te "10/05/2025 18:00:00"
sudo ausearch -ua 1000 # 按用户IDsudo 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
主配置:
/etc/audit/auditd.conf规则文件:
/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
#!/bin/bash# realtime_audit_monitor.shecho "开始实时监控审计日志..."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 "---" fidone#!/bin/bash# daily_audit_report.shREPORT_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_FILEsudo systemctl status auditdsudo auditctl -s
sudo tail -f /var/log/audit/audit.log
# 添加测试规则sudo auditctl -w /tmp/test -p rwa -k test# 触发测试touch /tmp/testfilecat /tmp/testfilerm /tmp/testfile# 查看日志sudo ausearch -k test -i
编辑 /etc/audit/auditd.conf
# 提高性能的配置log_file = /var/log/audit/audit.logmax_log_file = 100max_log_file_action = ROTATEnum_logs = 5space_left = 250space_left_action = emailaction_mail_acct = rootadmin_space_left = 50admin_space_left_action = SUSPEND
# 永久规则添加到 /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
# 监控数据库文件和命令-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
auditd 是 Linux 内核的审计框架用户空间组件,在系统安全审计、合规性监控和故障排查等方向发挥重要作用。大家可以自行去实践更详细用法,一般遵循以下几点进行,发挥其更大作用。
先测试规则再应用到生产环境
定期清理和归档审计日志
设置磁盘空间告警
结合日志分析工具(如 ELK Stack)
本文链接:https://www.jingber.cn/post/3977.html 转载需授权!

微信扫一扫,打赏作者吧~