标题: Linux auditd 使用 分类: 工具 创建: 2022-12-10 00:23 修改: 链接: http://0x2531.tech/tools/202212100023.txt -------------------------------------------------------------------------------- Linux auditd 是 Linux 系统中的审计机制,用于监控系统内的活动,并记录所有重要的事件。 auditd 允许系统管理员对系统内的活动进行跟踪,包括文件的访问、用户的登录和注销、系统资源的使用 等。它可以帮助系统管理员更好地了解系统内部的情况,以便及时发现和解决问题。此外,auditd还可以记 录系统中所有重要的安全事件,便于后期安全分析和审计。 -- 由 ChatGPT 生成 目录 1. 安装 2. 管理 3. 使用 3.1 配置审计规则 3.2 搜索审计日志 3.3 创建审计报表 4. 总结 5. 参考资料 1. 安装 在 RHEL 7 及以上版本中,默认已安装 audit 包。使用 yum info audit 确认是否已安装 audit 包 。如果未安装,使用 yum install audit 安装。 audit 的配置文件默认为 /etc/audit/auditd.conf,其中,包含改变 auditd 守护进程行为的各配 置项,audit 日志文件也在其中配置,默认为 /var/log/audit/audit.log。 2. 管理 成功安装和配置好 audit 后,就可以启用 auditd 服务 $ sudo service auditd start 查看 auditd 服务状态 $ sudo service auditd status 最后,为了使得 auditd 开机自启动,执行如下命令: $ sudo systemctl enable auditd 3. 使用 此时,auditd 已经开始收集审计日志了。为了实现个性化的审计需求,需通过 auditctl 工具配置审计 规则。 3.1 配置审计规则 有针对文件的监控规则(watch rule)和系统调用的审计规则。 3.1.1 文件监控 文件的监控规则可以用来监控特定文件的读、写、执行和属性修改。如:配置一个监控 /etc/passwd 文件 写和属性修改的规则,用来审计系统账号的变更事件。使用如下命令: $ sudo auditctl -w /etc/passwd -p wa -k user-modify -w 选项插桩监控指定文件;-p 选项在监控上设置权限过滤器,可选的值有 [r|w|x|a],多个值可随机组 合;-k 选项设置过滤器 key,在搜索日志时非常好用。 通过如下命令查看规则列表: $ sudo auditctl -l 将输出已配置的审计规则 -w /etc/passwd -p wa -k user-modify 接着,新增一个账号改变 /etc/passwd 文件 $ sudo useradd testuser 然后,查看日志文件确定有无生成相关的日志 $ sudo grep user-modify /var/log/audit/audit.log type=SYSCALL msg=audit(1670564769.079:3494151): arch=c000003e syscall=2 success=yes exit=8 a0=55b1e2a67ce0 a1=20902 a2=0 a3=8 items=1 ppid=21309 pid=32042 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts2 ses=444080 comm="useradd" exe="/usr/sbin/useradd" key="user-modify" type=CONFIG_CHANGE msg=audit(1670564769.092:3494155): auid=0 ses=444080 op=updated_rules path="/etc/passwd" key="user-modify" list=4 res=1 type=SYSCALL msg=audit(1670564769.092:3494156): arch=c000003e syscall=82 success=yes exit=0 a0=7ffdd1cad8b0 a1=55b1e2a67ce0 a2=7ffdd1cad820 a3=55b1e2bcb190 items=5 ppid=21309 pid=32042 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts2 ses=444080 comm="useradd" exe="/usr/sbin/ useradd" key="user-modify" 日志中包含操作类型、执行操作的用户的 UID 和 GID 以及执行命令等信息。 3.1.2 系统调用监控 系统调用监控规则用来记录系统调用的事件日志。如:定位哪个进程在高并发的发送网络连接,就可以使用系 统调用监控规则来实现。 $ sudo auditctl -a exit,always -F arch=b64 -S connect -k connect-io -a 选项追加一条规则,-F arch=b64 选项在 64 位平台上构建规则,-S 选项指定系统调用。 type=SYSCALL msg=audit(1670566610.465:3512088): arch=c000003e syscall=42 success=yes exit=0 a0=19 a1=7fe4dd57fdcc a2=10 a3=7fe4d82a4940 items=0 ppid=4787 pid=18643 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=4294967295 comm="gunicorn" exe="/usr/local/bin/ python3.8" key="connect-io" type=SYSCALL msg=audit(1670566610.465:3512089): arch=c000003e syscall=42 success=no exit=-115 a0=19 a1=7fe4d82f40f0 a2=10 a3=4 items=0 ppid=4787 pid=18643 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=4294967295 comm="gunicorn" exe="/usr/local/bin/python3.8" key="connect-io" 以上配置的审计规则属于软规则,在 auditd 服务重启后会被删除。如果需要配置持久化规则,需将规则写 入规则文件,默认路径为 /etc/audit/rules.d/audit.rules。 打开规则文件,在文件末尾追加规则,如: -w /etc/passwd -p wa -k user-modify 不要带 auditctl 命令,保存文件。接着,执行如下命令重载规则,使规则生效: $ sudo service auditd reload 这样即使 auditd 服务重启或机器重启后,审计规则都还在。 3.2 搜索审计日志 使用 grep 命令搜索出来的日志可读性较差,且搜索效率也不好。其实,audit 包提供了内置的 audit 日志搜索工具 ausearch。 如:以键值搜索日志: $ sudo ausearch -i -k user-modify -i 选项指示以可读性高的方式输出结果 ---- type=CONFIG_CHANGE msg=audit(2022年12月09日 14:16:11.895:3512068) : auid=unset ses=unset op=remove_rule key=user-modify list=exit res=yes ---- type=CONFIG_CHANGE msg=audit(2022年12月09日 14:16:11.895:3512073) : auid=unset ses=unset op=add_rule key=user-modify list=exit res=yes 查看帮助文档以了解更多的选项 $ sudo ausearch -h 3.3 创建审计报表 使用 aureport 工具查询和创建审计报表 $ sudo aureport 不指定报表类型,将输出报表概要 Summary Report ====================== Range of time in logs: 1970年01月01日 08:00:00.000 - 2022年12月09日 14:48:06.010 Selected time for report: 1970年01月01日 08:00:00 - 2022年12月09日 14:48:06.010 Number of changes in configuration: 9 Number of changes to accounts, groups, or roles: 2 Number of logins: 0 Number of failed logins: 0 Number of authentications: 0 Number of failed authentications: 0 Number of users: 2 Number of terminals: 9 Number of host names: 15 Number of executables: 41 Number of commands: 48 Number of files: 20 Number of AVC's: 0 Number of MAC events: 0 Number of failed syscalls: 28302 Number of anomaly events: 0 Number of responses to anomaly events: 0 Number of crypto events: 4 Number of integrity events: 0 Number of virt events: 0 Number of keys: 3 Number of process IDs: 7166 Number of events: 46280 生成包含所有可执行程序事件的报告 $ sudo aureport -x ...... 48642. 2022年12月09日 14:52:23 /usr/local/bin/python3.8 pts0 127.0.0.11 -1 3533225 48643. 2022年12月09日 14:52:23 /usr/local/bin/python3.8 pts0 192.168.250.5 -1 3533226 48644. 2022年12月09日 14:52:23 /usr/local/bin/redis-cli (none) 127.0.0.1 -1 3533227 48645. 2022年12月09日 14:52:23 /usr/local/bin/redis-cli (none) 127.0.0.1 -1 3533228 48646. 2022年12月09日 14:52:23 /usr/bin/docker-containerd (none) ? -1 3533229 48647. 2022年12月09日 14:52:23 /usr/bin/docker-containerd (none) ? -1 3533230 48648. 2022年12月09日 14:52:23 /usr/bin/docker-containerd (none) ? -1 3533231 ...... 查看帮助文档以了解更多的报表类型 $ sudo aureport --help 4. 总结 audit 是排查故障和安全审计的得力助手,通过配置合适的审计规则,能使我们的工作事半功倍。 5. 参考资料 https://www.redhat.com/sysadmin/configure-linux-auditing-auditd https://blog.51cto.com/zxdlife/2117712