Linux系统事件监控-Audit
Audit的核心是掌握它的三个层面:守护进程 (auditd)、审计规则 (auditctl) 和日志分析 (ausearch/aureport)。
📁 配置概览:核心文件与命令
| 类型 | 文件/命令 | 作用 | 关键说明 |
|---|---|---|---|
| 守护进程配置 | /etc/audit/auditd.conf |
控制auditd服务自身的行为 | 如日志位置、大小、轮转策略等 |
| 审计规则 | /etc/audit/rules.d/ 目录下的 .rules 文件 |
定义具体要监控什么事件 | 推荐使用此目录管理规则,系统会自动加载 |
/etc/audit/audit.rules |
最终生效的规则文件 | 由 augenrules 合并生成,建议不要直接编辑 |
|
| 实时控制 | auditctl 命令 |
临时增删查改审计规则 | 重启后失效,适合测试 |
| 日志分析 | ausearch 和 aureport |
查询原始日志,生成摘要报告 | 上一轮分析就用的是 aureport |
⚙️ 第一步:配置守护进程
你需要先调整守护进程本身的行为,控制日志如何保存。
使用编辑器打开配置文件(需要 root 权限):
vi /etc/audit/auditd.conf
这里有几个关键参数可以重点关注:
| 参数 | 示例值 | 作用说明 |
|---|---|---|
log_file |
/var/log/audit/audit.log |
审计日志存储路径,是分析数据的主要来源。 |
max_log_file |
50 |
单个日志文件的最大大小,单位为MB。 |
max_log_file_action |
ROTATE |
当文件达到最大大小时,执行轮转(推荐)。 |
num_logs |
5 |
轮转时保留的旧日志文件数量。 |
space_left_action |
EMAIL 或 SYSLOG |
磁盘空间低于阈值时执行的操作,如发送邮件或写入系统日志。 |
admin_space_left_action |
SUSPEND |
磁盘空间极低时的紧急操作,如暂停审计。 |
flush |
INCREMENTAL_ASYNC |
日志写入磁盘的频率,兼顾性能与数据安全。 |
修改完成后,执行以下命令让配置生效:
service auditd restart
# 或
systemctl restart auditd
📝 第二步:定义审计规则
这是最核心的一步。你需要告诉 auditd 到底要“看”什么。规则应写在 /etc/audit/rules.d/ 目录下,通常创建一个 audit.rules 文件:
vi /etc/audit/rules.d/audit.rules
以下是一些实用规则示例,你可以根据需要参考:
1. 监控关键文件(推荐方式)
vi /etc/audit/rules.d/key_files.rules
# 监控 /etc/passwd 的写入和属性变更,并打上 'passwd_changes' 标签
-w /etc/passwd -p wa -k passwd_changes
# 监控 /etc/shadow 的写入和属性变更,标签为 'shadow_changes'
-w /etc/shadow -p wa -k shadow_changes
# 监控 /root 目录下所有文件,标签为 'root_changes'(慎用,可能产生大量日志)
-w /root/ -p wa -k root_changes
参数含义:
-w:监控文件或目录-p:监控的权限(r=读,w=写,x=执行,a=属性变更)-k:自定义标签,便于搜索
2. 监控命令执行
# 监控系统上任何用户执行的任何程序(包括 32 位和 64 位),记录事件并打上 `executed_process` 标签。
-a exit,always -F arch=b64 -S execve -k executed_process
-a exit,always -F arch=b32 -S execve -k executed_process
# 专门监控 `/usr/bin/sudo` 被执行的时刻,记录事件并打上 `sudo_usage` 标签。
-w /usr/bin/sudo -p x -k sudo_usage
#总体这些规则帮助管理员追踪系统中所有进程启动情况,尤其聚焦于特权提升命令的使用。
参数含义:
-a:向指定的审计规则列表中添加一条规则。-F:设置过滤条件,只有符合该条件的系统调用才会被记录。-S:指定要监控的系统调用名称或编号。- 此处:
-S execve— 监控execve系统调用,即所有程序执行(包括脚本、二进制文件等)。
3. 排除无关噪音,让日志更干净
# 排除对系统服务频繁启动/停止的记录
-a exclude,always -F msgtype=SERVICE_START
-a exclude,always -F msgtype=SERVICE_STOP
# 排除特定程序产生的日志,例如 sudo
-a exclude,always -F exe=/usr/bin/sudo
规则写好后,用以下命令检查并加载生效:
# 检查规则有无语法错误
augenrules --check
# 加载规则,使其永久生效
augenrules --load
#重启服务
service auditd restart
🛠️ 第三步:管理规则与查询日志
临时测试规则:可以使用 auditctl 命令实时添加规则,进行测试:
# 临时监控 /tmp/test.log 文件
auditctl -w /tmp/test.log -p rwxa -k test_log
# 查看当前所有规则
auditctl -l
# 删除所有规则
auditctl -D
日志查询与分析:所有配置就绪后,你就可以像分析日志一样,用 ausearch 和 aureport 来查看结果了。
- 根据标签查询:
ausearch -k passwd_changes -i - 查询失败的登录:
ausearch -sv no -m USER_LOGIN -i - 生成登录失败报告:
aureport -au -i
查询一:查询标签为passwd_changes的日志
[root@backup ~]# ausearch -k passwd_changes -i
----
type=CONFIG_CHANGE msg=audit(03/28/2026 21:42:33.137:674) : auid=root ses=9 op=add_rule key=passwd_changes list=exit res=yes
----
type=CONFIG_CHANGE msg=audit(03/28/2026 21:45:35.806:6) : auid=unset ses=unset op=add_rule key=passwd_changes list=exit res=yes
----
type=PROCTITLE msg=audit(03/28/2026 21:48:06.832:67) : proctitle=vim /etc/passwd
type=PATH msg=audit(03/28/2026 21:48:06.832:67) : item=3 name=/etc/passwd~ inode=16781641 dev=fd:00 mode=file,644 ouid=root ogid=root rdev=00:00 objtype=CREATE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=PATH msg=audit(03/28/2026 21:48:06.832:67) : item=2 name=/etc/passwd inode=16781641 dev=fd:00 mode=file,644 ouid=root ogid=root rdev=00:00 objtype=DELETE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=PATH msg=audit(03/28/2026 21:48:06.832:67) : item=1 name=/etc/ inode=16777281 dev=fd:00 mode=dir,755 ouid=root ogid=root rdev=00:00 objtype=PARENT cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=PATH msg=audit(03/28/2026 21:48:06.832:67) : item=0 name=/etc/ inode=16777281 dev=fd:00 mode=dir,755 ouid=root ogid=root rdev=00:00 objtype=PARENT cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=CWD msg=audit(03/28/2026 21:48:06.832:67) : cwd=/root
type=SYSCALL msg=audit(03/28/2026 21:48:06.832:67) : arch=x86_64 syscall=rename success=yes exit=0 a0=0x19ee790 a1=0x1bc4150 a2=0xfffffffffffffe80 a3=0x7fffe7c5b720 items=4 ppid=1204 pid=1247 auid=root uid=root gid=root euid=root suid=root fsuid=root egid=root sgid=root fsgid=root tty=pts0 ses=1 comm=vim exe=/usr/bin/vim key=passwd_changes
----
type=PROCTITLE msg=audit(03/28/2026 21:48:06.832:68) : proctitle=vim /etc/passwd
type=PATH msg=audit(03/28/2026 21:48:06.832:68) : item=1 name=/etc/passwd inode=16780642 dev=fd:00 mode=file,644 ouid=root ogid=root rdev=00:00 objtype=CREATE cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=PATH msg=audit(03/28/2026 21:48:06.832:68) : item=0 name=/etc/ inode=16777281 dev=fd:00 mode=dir,755 ouid=root ogid=root rdev=00:00 objtype=PARENT cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=CWD msg=audit(03/28/2026 21:48:06.832:68) : cwd=/root
type=SYSCALL msg=audit(03/28/2026 21:48:06.832:68) : arch=x86_64 syscall=open success=yes exit=3 a0=0x19ee790 a1=O_WRONLY|O_CREAT|O_TRUNC a2=0644 a3=0x0 items=2 ppid=1204 pid=1247 auid=root uid=root gid=root euid=root suid=root fsuid=root egid=root sgid=root fsgid=root tty=pts0 ses=1 comm=vim exe=/usr/bin/vim key=passwd_changes
----
type=PROCTITLE msg=audit(03/28/2026 21:48:06.832:69) : proctitle=vim /etc/passwd
type=PATH msg=audit(03/28/2026 21:48:06.832:69) : item=0 name=/etc/passwd inode=16780642 dev=fd:00 mode=file,644 ouid=root ogid=root rdev=00:00 objtype=NORMAL cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=CWD msg=audit(03/28/2026 21:48:06.832:69) : cwd=/root
type=SYSCALL msg=audit(03/28/2026 21:48:06.832:69) : arch=x86_64 syscall=chmod success=yes exit=0 a0=0x19ee790 a1=0644 a2=0x7fffe7c5d3b0 a3=0x0 items=1 ppid=1204 pid=1247 auid=root uid=root gid=root euid=root suid=root fsuid=root egid=root sgid=root fsgid=root tty=pts0 ses=1 comm=vim exe=/usr/bin/vim key=passwd_changes
----
type=PROCTITLE msg=audit(03/28/2026 21:48:06.832:70) : proctitle=vim /etc/passwd
type=PATH msg=audit(03/28/2026 21:48:06.832:70) : item=0 name=/etc/passwd inode=16780642 dev=fd:00 mode=file,644 ouid=root ogid=root rdev=00:00 objtype=NORMAL cap_fp=none cap_fi=none cap_fe=0 cap_fver=0
type=CWD msg=audit(03/28/2026 21:48:06.832:70) : cwd=/root
type=SYSCALL msg=audit(03/28/2026 21:48:06.832:70) : arch=x86_64 syscall=setxattr success=yes exit=0 a0=0x19ee790 a1=0x7f6a2b8f0e2f a2=0x1bc40d0 a3=0x1c items=1 ppid=1204 pid=1247 auid=root uid=root gid=root euid=root suid=root fsuid=root egid=root sgid=root fsgid=root tty=pts0 ses=1 comm=vim exe=/usr/bin/vim key=passwd_changes
----
type=CONFIG_CHANGE msg=audit(03/28/2026 21:50:03.117:76) : auid=root ses=1 op=remove_rule key=passwd_changes list=exit res=yes
----
type=CONFIG_CHANGE msg=audit(03/28/2026 21:50:03.119:80) : auid=root ses=1 op=add_rule key=passwd_changes list=exit res=yes

浙公网安备 33010602011771号