zl程序教程

您现在的位置是:首页 >  系统

当前栏目

系统操作审计查看

系统 操作 查看 审计
2023-06-13 09:13:29 时间

[TOC]

0x01 auditd 命令 - Linux审计守护进程

描述: auditd是Linux审计系统的用户空间组件, 该守护进程它负责将审计记录写入磁盘,我们可以使用 ausearch 或 aureport 实用程序查看日志。 与此同时,我们可以使用 auditctl 实用程序配置审计系统或加载规则,在 auditd 启动期间审计规则 /etc/audit/audit.rules,由 auditctl 读取并加载到内核中或者还有一个 augenrules 程序,它读取位于 /etc/audit/rules.d/ 中的规则并将它们编译成一个 audit.rules 文件。

在Redhat与CentOS已经预安装了auditd审计进程,而Ubuntu需要手动安装auditd审计进程: sudo apt install auditd

Ubuntu 系统中 auditd 审计守护进程自定义的配置选项位于/etc/audit/auditd.conf文件中, 其它配置文件说明如下:

/etc/audit/auditd.conf - 审核守护程序的配置文件
/etc/audit/audit.rules - 要在启动时加载的审核规则
/etc/audit/rules.d/ - 包含要通过augenrules编译成一个文件的单独规则集的目录。
/lib/systemd/system/auditd.service - systemd 关于 auditd.service 进程。

帮助文档: https://github.com/linux-audit/audit-documentation

语法参数

# 语法
auditd [-f] [-l] [-n] [-s disable|enable|nochange] [-c <config_dir>]

# 参数
-f 将审核守护进程留在前台进行调试。消息也会转到stderr,而不是审计日志文件中。
-l 允许审核守护进程跟踪配置文件的符号链接。
-n 运行inittab或systemd很有用(no fork)。
-s=ENABLE_STATE 指定何时启动auditd是否应更改内核启用标志的当前值
-c  指定守护进程配置目录 (default: /etc/audit/)

SIGNALS 信号: SIGHUP、SIGTERM、SIGUSR1、SIGUSR2

1.auditctl 命令 - 帮助控制内核审计系统的实用程序

描述: auditctl程序用于配置与审核相关的内核选项,查看配置状态和添加、删除任意审核规则。(>= 2.6内核 )

语法参数

# 规则选项:RULE OPTIONS
-a [list,action|action,list] : 使用操作将规则附加到列表末尾,多个列表以逗号进行分割。
  #  以下描述了规则的有效列表:
  task # Add a rule to the per task list. This rule list is used only at the time a task is created -- when fork() or clone() are called by  the parent task. When using this list, you should only use fields that are known at task creation time, such as the uid, gid, etc.
  exit # Add a rule to the syscall exit list. This list is used upon exit from a system call to determine if an audit event should be  cre‐ ated.
  user # Add  a rule to the user message filter list. This list is used by the kernel to filter event originating in user space before re‐ laying them to the audit daemon. It should be noted that the only fields that are valid  are:  uid,  auid,  gid,  pid,  subj_user, subj_role,  subj_type,  subj_sen, subj_clr, and msgtype. All other fields will be treated as non-matching. It should be understood that any event originating from user space from a process that has CAP_AUDIT_WRITE will be recorded into  the  audit  trail.  This  means  that  the  most likely use for this filter is with rules that have an action of never since nothing has to be done to allow events to be recorded.

  exclude # Add a rule to the event type exclusion filter list. This list is used to filter events that you do not want tosee.  For  example, if  you  do not want to see any avc messages, you would using this list to record that. Events can be excluded by process ID, user ID, group ID, login user ID, message type or subject context.  The action is ignored and uses its default of "never".
  filesystem # Add a rule that will be applied to a whole filesystem. The filesystem must be identified with a fstype field. Normally this filter is used to exclude any events for a whole filesystem such as tracefs or debugfs.

  # 以下描述了规则的有效操作:
  never  # 不会生成审计记录,这可用于抑制事件生成。通常您希望在列表顶部而不是底部进行抑制,这是因为事件在第一个匹配规则上触发
  always # 分配一个审计上下文,总是在系统调用进入时填写它,总是在系统调用退出时写出一条记录。
-A list,action : 用action向开始列表添加规则。
-b backlog : 如果所有的缓冲区都满了, 内核就会参照失败标志进行操作。
-d list,action : 使用操作从列表中删除规则,只有当规则与syscall名称和每个字段名和值完全匹配时,才会删除该规则。
-D :删除所有规则和手表,这也可以采用一个关键选项(-k)。
-e [0..2] : 设置启用标志, 0 临时禁用审计, 为 1 作为参数传递, 为 2 任何试图在此模式下更改配置的尝试都将被审计并拒绝。
-F [n=v | n!=v | n<v | n>v | n<=v | n>=v | n&v | n&=v] : 构建规则字段:名称、操作、值,以-F 开头在单个命令行上最多可以传递 64 个字段。 
-f [0..2] : 设置失败标志0=silent(),1=printk,2=panic(安全环境推荐)。该选项允许确定希望内核如何处理关键错误。
-i :从文件中读取规则时忽略错误。
-l :每行列出所有规则1,这也可以采用一个关键选项(-k)。
-k key : 在审计规则上设置过滤键, 筛选键是一个长度不超过31字节的任意文本字符串, 它可以唯一地标识规则产生的审计记录。
-m text : 向审计系统发送用户空间消息, 只能由root用户完成。
-P : 指定触发审计的文件/目录的访问权限。(rwxa: `r 读取权限,w 写入权限,x 执行权限,a 属性`)
-q mount-point,subtree : 如果有一个现有的目录监视并绑定或移动在监视子树中挂载另一个子树, 则需要告诉内核使被挂载的子树等同于被监视的目录。
-r rate : 在messages/sec中设置限制(0=none)。
-R file : 从文件中读取规则。规则必须是每行1,并且按照执行的顺序, 建议该规则文件必须由根用户拥有。
-S [Syscall name or number|all] : 使用任何系统调用名称或号码,也可以使用“全部”一词,如果给定的系统调用是由程序进行的则开始审计.
-t : 在挂载命令后修整子树。
-w path : 在路径中插入文件系统对象的监视,注意不支持目录及其通配符也不支持,会产生警告。 
-W path : 移除路径处文件系统对象的监视。       

# EXIT STATUS
0    if OK,
1    if nothing found, or argument errors or minor file acces/read errors,
10   invalid checkpoint data found in checkpoint file,
11   checkpoint processing error
12   checkpoint event not found in matching log file

使用案例:

# 0.查看audit运行状态
auditctl -s 
  # enabled 1   # 表示启用
  # failure 1
  # pid 3272123
  # rate_limit 0
  # backlog_limit 8192
  # lost 96243
  # backlog 0
  # backlog_wait_time 0
  # loginuid_immutable 0 unlocked

# 查看现有审计规则
auditctl -l
  # -a always,exit -S all -F pid=1005
  # -a always,exit -S all

# 1.要查看特定程序进行的所有系统调用,请执行以下操作:
auditctl -a always,exit -S all -F pid=1005

# 2.要查看特定用户打开的文件,请执行以下操作:
auditctl -a always,exit -S openat -F auid=510
auditctl -a always,exit -S openat -F success=0  # 要查看未成功的openat呼叫

# 3.文件访问审计方法,审计文件的更改(有两种表达方式):
auditctl -w /etc/passwd -p rwxa # 如审计用于保存系统用户名密码的passwd文件。
auditctl -w /etc/shadow -p wa
auditctl -a always,exit -F path=/etc/shadow -F perm=wa

# 4.要递归地查看目录中的更改(有两种表达方式):
auditctl -w /etc/ -p wa
auditctl -a always,exit -F dir=/etc/ -F perm=wa

# 5.要查看管理员是否正在访问其他用户的文件:
auditctl -a always,exit -F dir=/home/ -F uid=0 -C auid!=obj_uid

# 6.此外,在可行的情况下尝试使用文件系统审计会提高性能。例如,如果您想捕获所有上述失败的打开和截断,但只关心/etc中的文件,而不关心/usr或/sbin,那么可以使用以下规则:
auditctl -a always,exit -S openat -S truncate -F dir=/etc -F success=0

# 7.删除指定规则和全部规则
auditctl -d always,exit -S all -F pid=1005
auditctl -D

# 8.添加指定规则到指定目录下的文件(注意 : 需要重启auditd生效)
$ touch /etc/audit/rules.d/docker-audit.rules
$ cat > /etc/audit/rules.d/docker-audit.rules <<'EOF'
-w /usr/bin/docker -k docker
-w /usr/lib/systemd/system/docker.service -k docker 
-w /usr/lib/systemd/system/docker.socket -k docker
-w /usr/bin/docker-containerd -k docker
-w /usr/bin/docker-runc -k docker
-w /etc/docker -k docker
-w /etc/docker/daemon.json -k docker
EOF
systemctl restart auditd

2.ausearch 命令 - 审计事件查看工具

描述: 使用 ausearch 工具可以根据不同的搜索条件查询审计守护进程的事件日志, 其还可以从stdin获取输入,只要输入是原始日志数据,注意必须以root用户身份执行ausearch命令。该实用程序将显示所有组成一个事件的记录。这可能意味着,即使您搜索特定类型的记录,所产生的事件也可能包含SYSCALL记录。

语法参数:

# 语法:
ausearch [options]

# 参数:
-a, --event audit-event-id : 根据给定的事件ID搜索事件, 消息总是以`msg=audit(1116360555.39:2401771)`开头。
-c, --comm comm-name : 根据给定的 comm-name 搜索事件, comm 名是任务结构中可执行文件的名称。
-e, --exit exit-code-or-errno : 基于给定的系统调用退出码或errno搜索事件。
-f : 设定需要调出文件的审计内容。

-ua, --uid-all all-user-id : 搜索具有与给定user ID,匹配的user ID, 有效user ID,或登录user ID, (auid)的事件。
-ga, --gid-all all-group-id :搜索具有有效group ID 或 group ID 与给定group ID匹配的事件。
-ue, --uid-effective effective-user-id : 搜索具有给定有效user ID的事件。
-ge, --gid-effective effective-group-id : 搜索具有给定有效group ID 或 group Name的事件
-ui, --uid user-id : 搜索具有给定user ID的事件。
-gi, --gid group-id : 搜索具有给定有效group ID 或group Name的事件。
-ul, --loginuid login-id : 搜索具有给定登录user ID的事件。

-hn, --host host-name : 搜索具有给定主机名的事件, 可以是主机名、完全限定域名或数字网络地址。
-i, --interpret : 将数字实体解释为文本, 例如,uid被转换为帐户名。
-if, --input file-name : 使用给定的文件而不是日志。
--input-logs : 使用auditd.conf中的日志文件位置作为搜索输入。
--just-one : 在发出符合搜索条件的第一个事件后停止。
-k, --key key-string : 基于给定的key字符串搜索事件。
-l, --line-buffered : 刷新每一行的输出,当stdout连接到管道且默认的块缓冲策略不可取时最有用。
-m, --message message-type | comma-sep-message-type-list : 搜索与给定消息类型匹配的事件, 以逗号分隔的消息类型列表。
-n, --node node-name : 搜索源自节点名称字符串的事件。
-o, --object SE-Linux-context-string : 搜索带有与字符串匹配的`tcontext (object)`的事件。
-p, --pid process-id : 搜索与给定进程ID匹配的事件。
-pp, --ppid parent-process-id : 搜索与给定父进程ID匹配的事件。
-r, --raw : 输出完全没有格式化,这对于提取仍然可以由审计工具解释的记录非常有用。
-sc, --syscall syscall-name-or-value : 搜索与给定系统调用匹配的事件。
-se, --context SE-Linux-context-string : 搜索scontext/subject或tcontext/object匹配字符串的事件。
--session Login-Session-ID : 搜索与给定的登录会话ID匹配的事件。
-su, --subject SE-Linux-context-string : 搜索事件与scontext(subject)匹配的字符串。
-sv, --success success-value : 搜索与给定success值匹配的事件, 值可以为 yes 和 no.
-te, --end [end-date] [end-time] : 搜索时间戳等于或先于给定结束时间的事件。
-ts, --start [start-date] [start-time] : 搜索时间戳等于或超过给定结束时间的事件。
-tm, --terminal terminal : 	搜索与给定的终端值匹配的事件。一些守护进程(如cron和atd)对终端使用守护进程名称。
-w, --word : 基于字符串的匹配必须匹配整个单词, 这类匹配包括文件名、主机名、终端和SE Linux上下文。
-x, --executable executable : 搜索与给定可执行名称匹配的事件。

示例说明:

# 1.搜索id为0的审计操作记录
ausearch -ui 0
  # time->Fri Mar  4 16:01:26 2022
  # type=PROCTITLE msg=audit(1646380886.973:2764027): proctitle="/usr/libexec/fwupd/fwupd"
  # type=SYSCALL msg=audit(1646380886.973:2764027): arch=c000003e syscall=7 success=yes exit=0 a0=563114845000 a1=2 a2=f9c a3=563114850a28 items=0 ppid=1 pid=3303582 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="gmain" exe="/usr/libexec/fwupd/fwupd" key=(null)

# 2.终端tty1搜索审计记录
ausearch -tm tty1
ausearch -tm pts/0
  # time->Fri Mar  4 16:02:25 2022
  # type=USER_LOGIN msg=audit(1646380945.722:2821996): pid=3303764 uid=0 auid=1000 ses=4390 msg='op=login id=1000 exe="/usr/sbin/sshd" hostname=10.20.172.103 addr=10.20.172.103 terminal=/dev/pts/0 res=success'

# 3.查询更改或访问 /etc/passwd文件的审计内容
$ sudo ausearch -f /etc/passwd
# 审计时间
time->Fri Mar  4 14:34:23 2022
type=PROCTITLE msg=audit(1646375663.900:2559277): proctitle="-bash"
# name 审计对象
type=PATH msg=audit(1646375663.900:2559277): item=0 name="/etc/passwd" inode=4198577 dev=fd:00 mode=0100755 ouid=0 ogid=0 rdev=00:00 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
# cwd 当前路径
type=CWD msg=audit(1646375663.900:2559277): cwd="/home/weiyigeek"
# syscall 相关的系统调用, auid 审计用户ID, uid 和 gid 访问文件的用户ID和用户组ID,comm 用户访问文件的命令, exe 上面命令的可执行文件路径。
type=SYSCALL msg=audit(1646375663.900:2559277): arch=c000003e syscall=257 success=yes exit=3 a0=ffffff9c a1=7f8df0a56189 a2=80000 a3=0 items=1 ppid=3302926 pid=3303167 auid=1000 uid=1000 gid=1000 euid=0 suid=0 fsuid=0 egid=1000 sgid=1000 fsgid=1000 tty=pts0 ses=4373 comm="sudo" exe="/usr/bin/sudo" key=(null)

# 4.搜索查看指定Key的审计信息
ausearch -k docker

3.aureport 命令 - 查看和生成审计报告的工具

描述: 使用 aureport 命令可以生成审计信息的报表,必须以root用户执行。如果执行aureport命令时没有使用任何选项,那么会显示汇总报表。

使用案例:

# 1.显示日志时间的范围报表
$ aureport -t
  # Log Time Range Report
  # =====================
  # /var/log/audit/audit.log.4: 03/04/2022 16:01:29.385 - 03/04/2022 16:01:37.617
  # /var/log/audit/audit.log.3: 03/04/2022 16:01:37.657 - 03/04/2022 16:02:02.449
  # /var/log/audit/audit.log.2: 03/04/2022 16:02:10.517 - 03/04/2022 16:02:16.574
  # /var/log/audit/audit.log.1: 03/04/2022 16:02:14.974 - 03/04/2022 16:01:32.497
  # /var/log/audit/audit.log: 03/04/2022 16:02:24.058 - 03/04/2022 16:45:01.729
  # root@master-201:~# more /var/log/audit/audit.log.1

4.auditspd 命令 - 转发事件通知给其他应用程序,而不是写入到审计日志文件中

5.autrace 命令 - 一个用于跟踪进程的命令