Linux auditd主机系统安全审计服务配置技术方案

Auditd是什么

Auditd是Linux审计系统的用户空间组件。Auditd是Linux审计守护进程的缩写。在Linux中,daemon被称为后台运行服务,当它在后台运行时,应用程序服务的末尾附加了一个“d”。auditd的工作是作为后台服务收集审计日志文件并将其写入磁盘。
在这里插入图片描述
审计系统由两个主要部分组成:用户空间应用程序和实用程序,以及内核端系统调用处理。内核组件接收来自用户空间应用程序的系统调用,并通过以下过滤器之一对其进行过滤:user、task、fstype或exit。

一旦系统调用通过了exclude过滤器,它就会通过前面提到的过滤器之一发送事件消息。该过滤器根据审核规则配置将其发送到审核守护进程进行进一步处理。

用户空间审计守护进程从内核收集信息,并在日志文件中创建条目。用户可以通过用户空间的审计实用程序与审计守护程序、内核审计组件或审计日志文件交互:

  • audisp,Audit dispatcher守护进程与Audit守护进程进行交互,并将事件发送到其他应用程序进行进一步处理。这个守护进程的目的是提供一个插件机制,以便实时分析程序可以与审计事件交互。
  • auditctl,审计控制实用程序与内核审计组件交互,以管理规则并控制事件生成过程的许多设置和参数。
  • 其它工具,大多是对审计日志进行查询或生成统计报告用途。

为什么使用Auditd

这个Linux服务为用户提供了一个Linux环境的安全审计解决方案。auditd收集和保存的日志是用户在Linux环境中执行的不同活动,如果有任何用户想询问其他用户在公司或多用户环境中做了什么,该用户可以以简化和最小化的形式访问此类信息,也就是安全审计日志。此外,如果用户的系统出现异常活动,比如说他的系统被破坏了,那么用户可以追踪并查看其系统是如何被破坏的,这在许多情况下也有助于事件响应。

常见使用场景有:

  • 监视对文件的访问。文件或目录,无论是读取、修改、还是变更文件属性。
  • 监视系统调用。
  • 记录用户执行的命令。
  • 记录进入的哪些系统路径。
  • 记录安全事件。
  • 查询事件消息。
  • 生成事件统计报告。
  • 监视网络访问。

注意,审核规则不是越多越好,当audit需要收集大量的事件消息时,就会对系统性能产生一些负面的影响。

安装方法

debian-based Linux distributions:

sudo apt-get install auditd audispd-plugins
service auditd start

centos:

yum install audit
systemctl start auditd
systemctl enable auditd
  • 值得注意的一点是,centos7 systemctl工具无法对auditd服务执行stop,restart的管理命令,需要使用service工具来实现:
service auditd restart

用户可以使用ausearch和aureport实用程序搜索auditd保存的日志。审计规则位于/etc/audit/audit.rules中。使用auditctl可以读取,也可以修改这些规则。/etc/audit/auditd.conf文件中是auditd的服务参数配置。

auditd工具包

auditctl:配置审核规则

  • -w,为一个文件添加监视,这意味着audit将监视该文件,并将与该文件相关的用户活动记录到日志中。
  • -k,在审核规则上设置筛选关键词
  • -p [r|w|x|a],添加针对文件权限的监视,r=read, w=write, x=execute, a=attribute
  • -a <l,a>,向list的末尾添加一条规则并设定采取的行动,list与action的取值范围参见man auditctl
  • -S,设定并监视一个系统调用,执行ausyscall --dump可以查看auditd支持的所有SYSCALL选项
  • -F f=v,Build rule: field name, operator(=,!=,<,>,<=,>=,&,&=) value

aureport:生成审计报告

  • -k,根据审核规则或配置中指定的key生成报告。
  • -i,启用解释模式,如显示用户名而不是userid
  • -au,生成身份验证审计报告
  • -l,生成用户登录报告

ausearch:查询审计日志

  • -p,查询指定进程pid相关的审计日志
  • -m,按指定关键词查询事件日志,执行ausearch -m查看支持的关键词范围
  • -ua,按指定用户名查询相关的事件日志
  • -i,启用解释模式,如显示用户名而不是userid
  • -ts,-te,按指定的起止时间打印事件日志,如ausearch -ts 2022年03月25日 13:30:00 -te 2022年03月25日 14:00:00,可以执行date '+%x’查看当前系统支持的日期格式
  • -c commond,只显示和指定命令有关的事件,如只显示rm命令产生的事件:auserach -c rm

autrace:进程审计,参考下面的方法。

# auditctl -D
No rules
# autrace /bin/ls /tmp
Waiting to execute: /bin/ls
hsperfdata_bjxtb  systemd-private-5e979fd32cfa4f68a1caf461c8af9796-chronyd.service-ZWAp4d  zabbix
Cleaning up...
Trace complete. You can locate the records with 'ausearch -i -p 18037'
# ausearch -i -p 18037

使用aureport查看当天的统计数据:

# aureport --start today --event --summary -i

Event Summary Report
======================
total  type
======================
25505  SYSCALL
1003  USER_ACCT
1002  CRED_ACQ
995  LOGIN
976  USER_START
960  CRED_REFR
959  CRED_DISP
959  USER_END
199  CONFIG_CHANGE
14  USER_AUTH

设计audit审核规则

audit审核规则包括以下3类:

  • 控制类规则,针对audit服务自身服务控制参数的配置规则;
  • 文件系统类规则
  • 系统调用类规则

设置审核规则的方式:

  • 使用auditctl工具进行动态的设置
  • 在/etc/audit/audit.rules文件中做持久化地定义

动态写入一条监视规则,对密码文件的写入与属性变更做监视

auditctl -w /etc/passwd -p wa -k user-modify
  • -k参数,为该条规则设置了一个关键词标识,方便归类或过滤事件日志

做个测试,查看审计日志效果:

sudo useradd testuser
cat /var/log/audit/audit.log | grep user-modify
ausearch -i -k user-modify

监视rm命令被执行

auditctl -w /bin/rm -p x -k removefile  #-w指定所要监控的文件或命令
                                               #-p指定监控属性,如x执行、w修改
                                               #-k是设置一个关键词用于查询

监视系统时间的变更

auditctl -a exit,always -F arch=b64 -S clock_settime -k changetime
auditctl -a always,exit -F exe=/bin/id -F arch=b64 -S execve -k execution_bin_id
  • -a exit,always ,在指定的系统调用退出时触发一个事件并记录到审计日志中,是最常用的参数值组合
  • 通过监视指定的系统调用,达到监控执行/bin/id命令的效果

审记每个用户的文件访问

auditctl -a exit,always -F arch=x86_64 -S open -F auid=80
  • -F arch=x86_64 定义使用什么体系结构(uname -m)来监视正确的系统调用(一些系统调用在主结构之间是不明确的)。
  • -S open 选择“open”系统调用
  • -F auid=80 相关用户ID

一份审核规则的配置模板数据

# pwd
/usr/share/doc/audit-2.8.5/rules
# ls
10-base-config.rules  12-cont-fail.rules     21-no32bit.rules             30-nispom.rules       30-stig.rules         40-local.rules       43-module-load.rules  99-finalize.rules
10-no-audit.rules     12-ignore-error.rules  22-ignore-chrony.rules       30-ospp-v42.rules     31-privileged.rules   41-containers.rules  70-einval.rules       README-rules
11-loginuid.rules     20-dont-audit.rules    23-ignore-filesystems.rules  30-pci-dss-v31.rules  32-power-abuse.rules  42-injection.rules   71-networking.rules
  • 到上面路径下面寻找下这份配置模板,有很高的参考价值。

查看audit配置规则和服务状态

auditctl -s
auditctl -l

禁止输出到/var/log/message文件

审计日志的还会输出到/var/log/message文件中,如果需要禁止输出到/var/log/message文件,可以修改rsyslog.conf配置项并重启即可:

*.info;mail.none;authpriv.none;cron.none.local2.none /var/log/messages

在audit.log文件中保存的时间格式是自1970年以来的秒数,可以用下面方法转成容易理解的日期时间:

date --date=@1648198201
auditctl -l #查看定义的规则
auditctl -D #清空定义的规则

将审核规则写入到配置文件中,以持续生效:/etc/audit/rules.d/audit.rules

通过audit审计服务过滤掉特定消息

-a always,exclude -F msgtype=USER_START
-a always,exclude -F msgtype=CRED_REFR
-a always,exclude -F msgtype=CRED_ACQ
-a always,exclude -F msgtype=USER_ACCT
-a always,exclude -F msgtype=USER_END
-a always,exclude -F msgtype=CRED_DISP
  • 上面的6个被排除的事件类型均与用户登录行为有关,但crond在执行任务时会自动触发以上类型的事件消息,会产生大量的干扰信息。两害取其轻,只好做个过滤处理。
  • 用户登录系统时还会触发多个其它类型的记录,如USER_AUTH、USER_LOGIN

网上的两份参考资料

  • Redhat官网:audit log中记录中各属性字段的含义,消息的类型信息,可以从官网查询:https://access.redhat.com/articles/4409591
  • Github:https://github.com/Neo23x0/auditd/blob/master/audit.rules

一份自行整理的audit.rules配置参考资料

参考了很多配置模板后,整理出的一份配置文件,仅供参考。
/etc/audit/rules.d/audit.rules

## First rule - delete all
-D

## Increase the buffers to survive stress events.
## Make this bigger for busy systems
-b 8192

## Set failure mode to syslog
-f 1

-c

# log messages rate limit,100 messages per second at most.
-r 100

## Make the loginuid immutable. This prevents tampering with the auid.
--loginuid-immutable

# This prevents chrony from overwhelming the logs
-a never,exit -F arch=x86_64 -S adjtimex -F auid=unset -F uid=chrony -F subj_type=chronyd_t

## If you are on a 64 bit platform, everything _should_ be running
## in 64 bit mode. This rule will detect any use of the 32 bit syscalls
## because this might be a sign of someone exploiting a hole in the 32
## bit API.
-a always,exit -F arch=b32 -S all -F key=32bit-abi

## This rule suppresses the time-change event when chrony does time updates
-a never,exit -F arch=b64 -S adjtimex -F auid=unset -Fuid=chrony -F subj_type=chronyd_t

# This rule supresses events that originate on the below file systems.
# Typically you would use this in conjunction with rules to monitor
# kernel modules. The filesystem listed are known to cause hundreds of
# path records during kernel module load. As an aside, if you do see the
# tracefs or debugfs module load and this is a production system, you really
# should look into why its getting loaded and prevent it if possible.
-a never,filesystem -F fstype=tracefs
-a never,filesystem -F fstype=debugfs

## Things that could affect time
-w /etc/localtime -p wa -k time-change

## Things that could affect system locale
-a always,exit -F arch=b64 -S sethostname,setdomainname -F key=system-locale
-w /etc/issue -p wa -k system-locale
-w /etc/issue.net -p wa -k system-locale
-w /etc/hosts -p wa -k system-locale
-w /etc/hostname -p wa -k system-locale
-w /etc/sysconfig/network -p wa -k system-locale
-a always,exit -F dir=/etc/NetworkManager/ -F perm=wa -F key=system-locale

## Audit 1, 1(b) Successful and unsuccessful logons and logoffs.
## This is covered by patches to login, gdm, and openssh
## Might also want to watch these files if needing extra information
-w /var/log/tallylog -p wa -k logins
-w /var/run/faillock/ -p wa -k logins
-w /var/log/lastlog -p wa -k logins
-w /var/log/btmp -p wa -k logins
-w /var/run/utmp -p wa -k logins

## unsuccessful creation
-a always,exit -F arch=b64 -S mkdir,creat,link,symlink,mknod,mknodat,linkat,symlinkat -F exit=-EACCES -F key=creation
-a always,exit -F arch=b64 -S mkdir,link,symlink,mkdirat -F exit=-EPERM -F key=creation

## unsuccessful open
-a always,exit -F arch=b64 -S open,openat,open_by_handle_at -F exit=-EACCES -F key=open
-a always,exit -F arch=b64 -S open,openat,open_by_handle_at -F exit=-EPERM -F key=open

## unsuccessful close
-a always,exit -F arch=b64 -S close -F exit=-EIO -F key=close

## unsuccessful modifications
-a always,exit -F arch=b64 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EACCES -F key=mods
-a always,exit -F arch=b64 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EPERM -F key=mods

## unsuccessful deletion
-a always,exit -F arch=b64 -S rmdir,unlink,unlinkat -F exit=-EACCES -F key=delete
-a always,exit -F arch=b64 -S rmdir,unlink,unlinkat -F exit=-EPERM -F key=delete

## Unsuccessful ownership change
-a always,exit -F arch=b64 -S lchown,fchown,chown,fchownat -F exit=-EACCES -F auid>=1000 -F auid!=unset -F key=unsuccesful-perm-change
-a always,exit -F arch=b64 -S lchown,fchown,chown,fchownat -F exit=-EPERM -F auid>=1000 -F auid!=unset -F key=unsuccesful-perm-change

## Audit 1, 1(d) Changes in user authenticators.
## Covered by patches to libpam, passwd, and shadow-utils
## Might also want to watch these files for changes
-w /etc/group -p wa -k auth
-w /etc/passwd -p wa -k auth
-w /etc/gshadow -p wa -k auth
-w /etc/shadow -p wa -k auth
-w /etc/security/opasswd -p wa -k auth

## Group add delete modify. This is covered by pam. However, someone could
## open a file and directly create or modify a user, so we'll watch group and
## gshadow for writes
-a always,exit -F arch=b64 -S openat,open_by_handle_at -F a2&03 -F path=/etc/group -F auid>=1000 -F auid!=unset -F key=group-modify
-a always,exit -F arch=b64 -S open -F a1&03 -F path=/etc/group -F auid>=1000 -F auid!=unset -F key=group-modify
-a always,exit -F arch=b64 -S openat,open_by_handle_at -F a2&03 -F path=/etc/gshadow -F auid>=1000 -F auid!=unset -F key=group-modify
-a always,exit -F arch=b64 -S open -F a1&03 -F path=/etc/gshadow -F auid>=1000 -F auid!=unset -F key=group-modify

## Use of special rights for config changes. This would be use of setuid
## programs that relate to user accts. This is not all setuid apps because
## requirements are only for ones that affect system configuration.
-a always,exit -F path=/usr/bin/chfn -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/chage -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/gpasswd -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/newgrp -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/chsh -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/sudo -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/mount -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/su -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/umount -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/pkexec -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/bin/passwd -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/pam_timestamp_check -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/unix_chkpwd -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/usernetctl -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/mtr -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/lsof -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged
-a always,exit -F path=/usr/sbin/tcpdump -F perm=x -F auid>=1000 -F auid!=unset -F key=privileged

## Things that could affect MAC policy
-a always,exit -F dir=/etc/selinux/ -F perm=wa -F key=MAC-policy

## Cron configuration & scheduled jobs
-w /etc/cron.allow -p wa -k cron
-w /etc/cron.deny -p wa -k cron
-w /etc/cron.d/ -p wa -k cron
-w /etc/cron.daily/ -p wa -k cron
-w /etc/cron.hourly/ -p wa -k cron
-w /etc/cron.monthly/ -p wa -k cron
-w /etc/cron.weekly/ -p wa -k cron
-w /etc/crontab -p wa -k cron
-w /var/spool/cron/ -p wa -k cron

## Look for pam_tty_audit and add it to your login entry point's pam configs.
## If that is not found, use sudo which should be patched to record its
## commands to the audit system. Do not allow unrestricted root shells or
## sudo cannot record the action.
-w /etc/sudoers -p wa -k actions
-w /etc/sudoers.d/ -p wa -k actions

## The purpose of this rule is to detect when an admin may be abusing power
## by looking in user's home dir.
-a always,exit -F dir=/home -F uid=0 -F auid>=1000 -F auid!=unset -C auid!=obj_uid -F key=power-abuse

## These rules watch for code injection by the ptrace facility.
## This could indicate someone trying to do something bad or
## just debugging
-a always,exit -F arch=b64 -S ptrace -F key=tracing
-a always,exit -F arch=b64 -S ptrace -F a0=0x4 -F key=code-injection
-a always,exit -F arch=b64 -S ptrace -F a0=0x5 -F key=data-injection
-a always,exit -F arch=b64 -S ptrace -F a0=0x6 -F key=register-injection

## These rules watch for kernel module insertion. By monitoring
## the syscall, we do not need any watches on programs.
-a always,exit -F arch=b64 -S init_module,finit_module -F key=module-load
-a always,exit -F arch=b64 -S delete_module -F key=module-unload

## SSH configuration
-w /etc/ssh/sshd_config -p wa -k sshd

## root ssh key tampering
-w /root/.ssh -p wa -k rootkey

# Systemd
-w /bin/systemctl -p x -k systemd
-w /etc/systemd/ -p wa -k systemd

## Shell/profile configurations
-w /etc/profile.d/ -p wa -k shell_profiles
-w /etc/profile -p wa -k shell_profiles
-w /etc/shells -p wa -k shell_profiles
-w /etc/bashrc -p wa -k shell_profiles
-w /etc/csh.cshrc -p wa -k shell_profiles
-w /etc/csh.login -p wa -k shell_profiles
-w /etc/fish/ -p wa -k shell_profiles
-w /etc/zsh/ -p wa -k shell_profiles

## 排除一些干扰性过强的消息
-a always,exclude -F msgtype=USER_START
-a always,exclude -F msgtype=CRED_REFR
-a always,exclude -F msgtype=CRED_ACQ
-a always,exclude -F msgtype=USER_ACCT
-a always,exclude -F msgtype=USER_END
-a always,exclude -F msgtype=CRED_DISP
-a always,exclude -F msgtype=LOGIN
-a always,exclude -F msgtype=PROCTITLE
## Ignore current working directory records
-a always,exclude -F msgtype=CWD
## Cron jobs fill the logs with stuff we normally don't want (works with SELinux)
-a never,user -F subj_type=crond_t
-a never,exit -F subj_type=crond_t
## This is not very interesting and wastes a lot of space if the server is public facing
-a always,exclude -F msgtype=CRYPTO_KEY_USER

## FileBeat 
#-a never,exit -F arch=b64 -F path=/opt/filebeat -k filebeat

## This is to check if the system is making or recieving connections
## externally
#-a always,exit -F arch=b64 -S accept,connect -F key=external-access

## Make the configuration immutable - reboot is required to change audit rules
#-e 2

## 在audit.log文件中保存的时间格式是自1970年以来的秒数,可以用下面方法转成容易理解的日期时间,audit(1364481363.243:24287)
## date --date=@1364481363.243


  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值