目录
4.2 规则永久生效 /etc/audit/audit.rules
1 audit 功能引入
linux 系统中有着大量的配置文件,有些文件涉及和内核驱动的交互,一旦被异常修改会导致一些未知的错误,如何对文件实施行为监控是一个问题。通过配置 message 的等级可以抓到前台终端的命令,这样可以间接查看文件是否被“动过”,但是如果修改文件的逻辑在某些脚本中执行,就无法定位出事谁在修改文件了,具体操作如下:
[root@localhost auditd_test]# pwd
/wq/auditd_test
[root@localhost auditd_test]# ll
总用量 8
-rwxrwxrwx 1 root root 39 10月 15 18:19 run.sh
-rw-r--r-- 1 root root 62 10月 15 19:38 test.txt
[root@localhost auditd_test]#
===================================================
[root@localhost auditd_test]# ./run.sh &
[1] 10227
[1]+ Done ./run.sh
[root@localhost auditd_test]# vi test.txt
[root@localhost auditd_test]# tail -f /var/log/messages
Oct 15 19:43:57 localhost httpdwatchdog: start httpd service
Oct 15 19:45:24 localhost bash[10013]: root:#011 tail -f /var/log/messages
Oct 15 19:45:24 localhost bash[10015]: root:#011 tail -f /var/log/messages
Oct 15 19:45:26 localhost bash[10038]: root:#011 pwd
Oct 15 19:45:26 localhost bash[10041]: root:#011 pwd
Oct 15 19:46:23 localhost bash[10226]: root:#011 ll
Oct 15 19:46:23 localhost bash[10229]: root:#011 ll
Oct 15 19:46:33 localhost bash[10253]: root:#011 ./run.sh &
Oct 15 19:46:44 localhost bash[10278]: root:#011 ./run.sh &
Oct 15 19:46:48 localhost bash[10302]: root:#011 vi test.txt
^C
[root@localhost auditd_test]# cat run.sh
#!/bin/bash
echo "123456" >> test.txt
[root@localhost auditd_test]#
通过 vi 指令修改test.txt、和run.sh的动作指令可以被正常抓取,但是并不能知道run.sh在修改test.txt,实际应用中修改文件的逻辑隐藏的很深,可以是脚本,可以是应用程序等。如何能够详细的抓取对文件的动作即引入linux自带的审计功能 audit。
2 查看运行状态
[root@localhost auditd_test]# ps -ef | grep audit
root 1509 2 0 19:28 ? 00:00:00 [kauditd]
root 7077 1 0 19:29 ? 00:00:00 auditd
[root@localhost auditd_test]#
======================================================================
[root@localhost auditd_test]# service auditd status
auditd (pid 7077) 正在运行...
[root@localhost auditd_test]# auditctl -s
AUDIT_STATUS: enabled=1 flag=1 pid=7077 rate_limit=0 backlog_limit=320 lost=0 backlog=0
[root@localhost auditd_test]#
3 auditd的相关的工具
- auditctl : 即时控制审计守护进程的行为的工具,比如如添加规则等等。
- /etc/audit/audit.rules : 记录审计规则的文件。
- aureport : 查看和生成审计报告的工具。
- ausearch : 查找审计事件的工具
- auditspd : 转发事件通知给其他应用程序,而不是写入到审计日志文件中。
- autrace : 一个用于跟踪进程的命令。
- /etc/audit/auditd.conf : auditd工具的配置文件。
4 监控规则添加 auditctl
[root@localhost auditd_test]# auditctl
usage: auditctl [options]
-a <l,a> Append rule to end of <l>ist with <a>ction
-A <l,a> Add rule at beginning of <l>ist with <a>ction
-b <backlog> Set max number of outstanding audit buffers
allowed Default=64
-d <l,a> Delete rule from <l>ist with <a>ction
l=task,entry,exit,user,watch,exclude
a=never,possible,always
-D Delete all rules and watches
-e [0..2] Set enabled flag
-f [0..2] Set failure flag
0=silent 1=printk 2=panic
-F f=v Build rule: field name, operator(=,!=,<,>,<=,
>=,&,&=) value
-h Help
-i Ignore errors when reading rules from file
-k <key> Set filter key on audit rule
-l List rules
-m text Send a user-space message
-p [r|w|x|a] Set permissions filter on watch
r=read, w=write, x=execute, a=attribute
-q <mount,subtree> make subtree part of mount point's dir watches
-r <rate> Set limit in messages/sec (0=none)
-R <file> read rules from file
-s Report status
-S syscall Build rule: syscall name or number
-t Trim directory watches
-v Version
-w <path> Insert watch at <path>
-W <path> Remove watch at <path>
[root@localhost auditd_test]#
4.1 监控文件和目录的更改
[root@localhost auditd_test]# auditctl -w /etc/passwd -p rwxa
-w path : 指定要监控的路径,上面的命令指定了监控的文件路径 /etc/passwd
-p: 指定触发审计的文件/目录的访问权限
rwxa :指定的触发条件,r 读取权限,w 写入权限,x 执行权限,a 属性(attr)
4.2 规则永久生效 /etc/audit/audit.rules
[root@localhost auditd_test]# cat /etc/audit/audit.rules
# This file contains the auditctl rules that are loaded
# whenever the audit daemon is started via the initscripts.
# The rules are simply the parameters that would be passed
# to auditctl.
# First rule - delete all
-D
# Increase the buffers to survive stress events.
# Make this bigger for busy systems
-b 320
# Feel free to add below this line. See auditctl man page
-w /sbc/wq/auditd_test/test.txt -p wx
[root@localhost auditd_test]#
//需要重启服务,系统复位后规则不会丢失
[root@localhost auditd_test]# service auditd restart
停止 auditd: [确定]
正在启动 auditd: [确定]
[root@localhost auditd_test]#
4.3 查看已配置的规则
[root@localhost auditd_test]# auditctl -l
LIST_RULES: exit,always watch=/sbc/wq/auditd_test/test.txt perm=wx
LIST_RULES: exit,always watch=/etc/passwd perm=rwxa
[root@localhost auditd_test]#
5 查看审计日志 ausearch
5.1 审计文件
[root@localhost auditd_test]# ./run.sh &
[1] 13682
[1]+ Done ./run.sh
[root@localhost auditd_test]# ausearch -f test.txt
time->Thu Oct 15 20:03:39 2020
type=PATH msg=audit(1602792219.861:336): item=0 name="test.txt" inode=523325 dev=08:02 mode=0100644 ouid=0 ogid=0 rdev=00:00
type=CWD msg=audit(1602792219.861:336): cwd="/sbc/wq/auditd_test"
type=SYSCALL msg=audit(1602792219.861:336): arch=c000003e syscall=2 success=yes exit=3 a0=179e110 a1=441 a2=1b6 a3=76 items=1 ppid=5148 pid=13682 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts12 ses=4294967295 comm="run.sh" key=(null)
[root@localhost auditd_test]#
----------------------------------------------------------------------------------------
time : 审计时间。
name : 审计对象
cwd : 当前路径
syscall : 相关的系统调用
auid : 审计用户ID
uid 和 gid : 访问文件的用户ID和用户组ID
comm : 用户访问文件的命令
exe : 上面命令的可执行文件路径
5.2 审计目录
修改监控文件添加一个用户,看看auditd如何记录文件 /etc/passwd的改动的
[root@localhost auditd_test]# date
2020年 10月 15日 星期四 20:10:11 UTC
[root@localhost auditd_test]# useradd laowang &
[1] 15128
[root@localhost auditd_test]# 没有找到“mail”组。以 0600 权限模式创建用户的信箱文件。
[1]+ Done useradd laowang
[root@localhost auditd_test]# ausearch -f /etc/passwd
........
----
time->Thu Oct 15 20:10:30 2020
type=PATH msg=audit(1602792630.196:549): item=0 name="/etc/passwd" inode=808545 dev=08:02 mode=0100644 ouid=0 ogid=0 rdev=00:00
type=CWD msg=audit(1602792630.196:549): cwd="/sbc/wq/auditd_test"
type=SYSCALL msg=audit(1602792630.196:549): arch=c000003e syscall=2 success=yes exit=4 a0=7fb70734369a a1=80000 a2=1b6 a3=0 items=1 ppid=5148 pid=15128 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts12 ses=4294967295 comm="useradd" key=(null)
[root@localhost auditd_test]#
[root@localhost auditd_test]# mkdir test_dir
[root@localhost auditd_test]# ausearch -f /sbc/wq/auditd_test/test_dir
<no matches>
[root@localhost auditd_test]# auditctl -w /sbc/wq/auditd_test/test_dir
[root@localhost auditd_test]# ausearch -f /sbc/wq/auditd_test/test_dir
<no matches>
[root@localhost auditd_test]# chmod 777 test_dir
[root@localhost auditd_test]# ausearch -f /sbc/wq/auditd_test/test_dir
<no matches>
[root@localhost auditd_test]# ausearch -f test_dir
----
...
time->Thu Oct 15 20:17:55 2020
type=PATH msg=audit(1602793075.500:727): item=0 name="test_dir" inode=530417 dev=08:02 mode=040755 ouid=0 ogid=0 rdev=00:00
type=CWD msg=audit(1602793075.500:727): cwd="/sbc/wq/auditd_test"
type=SYSCALL msg=audit(1602793075.500:727): arch=c000003e syscall=268 success=yes exit=0 a0=ffffffffffffff9c a1=eb4100 a2=1ff a3=0 items=1 ppid=5148 pid=16637 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts12 ses=4294967295 comm="chmod" key=(null)
[root@localhost auditd_test]#
6 查看审计报告 aureport
aureport 是使用系统审计日志生成简要报告的工具。我们已经配置auditd去跟踪/etc/passwd文件。auditd参数设置后一段时间后,audit.log 文件就创建出来了。生成审计报告,我们可以使用aureport工具。
6.1 审计活动概述
[root@localhost auditd_test]# aureport
Summary Report
======================
Range of time in logs: 2020年09月22日 08:51:01.255 - 2020年10月15日 20:23:01.759
Selected time for report: 2020年09月22日 08:51:01 - 2020年10月15日 20:23:01.759
Number of changes in configuration: 46
Number of changes to accounts, groups, or roles: 40
Number of logins: 0
Number of failed logins: 0
Number of authentications: 155
Number of failed authentications: 0
Number of users: 2
Number of terminals: 17
Number of host names: 1
Number of executables: 7
Number of files: 0
Number of AVC's: 0
Number of MAC events: 0
Number of failed syscalls: 0
Number of anomaly events: 262
Number of responses to anomaly events: 0
Number of crypto events: 0
Number of keys: 0
Number of process IDs: 18629
Number of events: 166692
6.2 授权失败
[root@localhost auditd_test]# aureport -au
Authentication Report
============================================
# date time acct host term exe success event
============================================
1. 2020年09月27日 14:06:57 root ? pts/4 /bin/su yes 349057
2. 2020年09月27日 14:06:57 root ? pts/4 /bin/su yes 349063
....
155. 2020年10月15日 19:26:32 root ? console /bin/su yes 516
6.3 账户与修改相关事件
[root@localhost auditd_test]# aureport -m
Account Modifications Report
=================================================
# date time auid addr term exe acct success event
=================================================
1. 2020年09月27日 15:57:20 -1 ? ? /usr/bin/passwd 9_FTP yes 202736
2. 2020年09月27日 15:57:20 -1 ? ? /usr/bin/passwd ? yes 202737
3. 2020年09月27日 15:57:20 -1 ? ? /usr/bin/passwd 2_FTP yes 202738
....
40. 2020年10月15日 20:10:30 -1 ? pts/12 /usr/sbin/useradd ? yes 555
[root@localhost auditd_test]#