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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM