linux audit审计(2)--audit启动

参考:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec-defining_audit_rules_and_controls

1、启动audit内核模块    

    有些系统audit的内核模块时默认关闭的。可以查看/proc/cmdline,看audit=0,如果为0,则默认不启动audit。通过设置/boot/grub2/grub.cfg文件,使audit=1,或者去掉audit=0,然后重启系统,使audit内核模块启动。再次查看/proc/cmdline已修改为audit=1。

    如果audit的内核模块启动了,用auditctl -s查询enabled为1,但是用户空间的auditd守护进程没有运行,审计日志无人接管,就会被写到/var/log/messages中。

2、启动auditd守护进程

    我们习惯使用systemctl start xxx来启动一个服务,但是auditd手册中,明确指出使用service命令是唯一一个正确开启auditd守护进程的方式。使用systemctl只是enable,status这两个动作可以。

    正常一个服务,我们使用service xx start时都会显示Redirecting to /bin/systemctl start xxx.service。我们可以通过which service来查看这个命令的源文件,/usr/sbin/service。这里对于不同的情况,有不同的处理。1)/etc/init.d目录下有这个服务;2)/usr/libexec/initscripts/legacy-actions目录下有这个服务;3)除了以上两种情况,其他情况下会定向到systemctl cmd xxx.service。auditd属于第二种情况,/usr/libexec/initscripts/legacy-actions目录有auditd目录,如下:

linux-xdYUnA:/usr/libexec/initscripts/legacy-actions/auditd # ls
condrestart  restart  resume  rotate  stop

1)设置开机启动/不启动

systemctl enable auditd
systemctl disable auditd

2)stop

service auditd stop

3) reload

service auditd reload

重新加载auditd的配置文件/etc/audit/auditd.conf。使用systemctl reload auditd也是生效的。

4)rotate

 service auditd rotate

在/var/log/audit/目录下rotate。

5)resume

service auditd resume

当日志记录停止时,用于恢复日志记录。

6)condrestart

service auditd condrestart

当服务正在运行时,重启服务。

3、auditd的服务文件如下:

[Unit]
Description=Security Auditing Service
DefaultDependencies=no

## If auditd.conf has tcp_listen_port enabled, copy this file to
## /etc/systemd/system/auditd.service and add network-online.target
## to the next line so it waits for the network to start before launching.

## 如果在auditd.conf中已经配置了tcp_listen_port,那么需要在After后面增加network-online.target,保证先启动网络,后再启动auditd
After=local-fs.target systemd-tmpfiles-setup.service
Conflicts=shutdown.target
Before=sysinit.target shutdown.target
RefuseManualStop=yes
ConditionKernelCommandLine=!audit=0
Documentation=man:auditd(8) https://github.com/linux-audit/audit-documentation

[Service]
Type=forking
PIDFile=/var/run/auditd.pid
ExecStart=/sbin/auditd

## To not use augenrules, copy this file to /etc/systemd/system/auditd.service
## and comment/delete the next line and uncomment the auditctl line.
## NOTE: augenrules expect any rules to be added to /etc/audit/rules.d/

## ungenrules这个工具是把/etc/audit/rules.d/目录下的规则文件,按照顺序加载进来,写入到/etc/audit/audit.rules文件中。如果不使用这个工具,下面这一行需要注释或删掉。auditctl那一行取消注释。
ExecStartPost=-/sbin/augenrules --load
#ExecStartPost
=-/sbin/auditctl -R /etc/audit/audit.rules ExecReload=/bin/kill -HUP $MAINPID
# By default we don
't clear the rules on exit. To enable this, uncomment # the next line after copying the file to /etc/systemd/system/auditd.service

## 这个是stop后执行的动作。stop.rulse主要是 -e 0 disable 和-D删除所有规则
## 如果不删除已经配置好的规则的话,在stop后,用auditctl -l查询可以看到有很多规则,并且enabled为1,这样的话,当auditd服务关闭了,但规则还在,那就会有大量的日志记录在messages中。stop.rules中通过-e 0使audit不工作,日志也不会记录在messages中 #ExecStopPost
=/sbin/auditctl -R /etc/audit/audit-stop.rules [Install] WantedBy=multi-user.target

 4、audit的使能

通过auditctl -s查询可以看到audit的状态,这个是查询audit内核模块的状态。

linux-xdYUnA:/etc/audit # auditctl -s
enabled 0
failure 1
pid 0
rate_limit 0
backlog_limit 8192
lost 219
backlog 0

其中“enabled”这个选项很重要的,如果为0,则audit就不工作了,看不到任何审计日志,哪怕是系统日志messages。所有的规则都不会生效,连没有设定规则时,su提权这样的操作也不会记录。无论是使用auditctl临设配置规则,还是用/etc/audit/audit.rules文件永久配置规则,所有规则设定都要在enabled为1时才可以生效。

当enabled为2时,则表示规则锁定,所有设置都不起作用,只有当系统重启后,enabled不为2了才可以。

服务是服务,enable是enable,这两者并没有关系,服务启动或停止是针对auditd这个守护进程。auditctl -s查询的enabled的状态为audit内核模块的状态。内核模块主要用来获取审计信息,用户态的守护进程主要用来收集信息和记录日志。enabled为1,服务停止,日志会记录在messages中;enable为0,服务启动,没有任何审计信息,不会记录日志。也就是auditd服务是否启动,决定日志记录在哪里,启动了,日志记录在配置文件指定的路径,没有启动,记录在messages日志中。enable决定内核是否开始审计,为0,不启动,为1,启动。

可以通过auditctl -e改变状态,0 不使能,1 使能,2 锁定配置。如果在/boot/grub2/grub.cfg里配置的audit=1,当系统重启后,enable默认为1,如果没有配置audit,则enable为0。

当然了,如果audit的内核模块不加载(即在/boot/grub2/grub.cfg中配置为audit=0),与内核模块交互的auditctl也就无法工作了,状态当然也无法查询。

linux-xdYUnA:~ # auditctl -s
Error - audit support not in kernel
Cannot open netlink audit socket
原文地址:https://www.cnblogs.com/xingmuxin/p/8675294.html