Debian 12 bookworm
Sponsored Link

Auditd : Add Audit Rules2023/07/13

 
It's possible to add your own Audit rules like follows.
[1] For example, Configure Audit rule that records writing and attributes change for [/etc/hosts].
# display current rules (no rules by default like follows)

root@dlp:~#
auditctl -l

No rules
# -p [r|w|x|a] : specify target action for Audit
# r=read, w=write, x=execute, a=attributes
# -k [words] : set keys for searching logs

root@dlp:~#
auditctl -w /etc/hosts -p wa -k hosts_change

root@dlp:~#
auditctl -l

-w /etc/hosts -p wa -k hosts_change
[2] When some actions are set and it is detected by new Audit rules, Audit logs are recorded like follows.
root@dlp:~#
ausearch -k hosts_change | aureport -f -i


===============================================
# date time file syscall success exe auid event
===============================================
1. 07/12/2023 21:18:16 /etc/hosts~ rename yes /usr/bin/vim.basic root 301
2. 07/12/2023 21:18:16 /etc/hosts openat yes /usr/bin/vim.basic root 302
3. 07/12/2023 21:18:16 (null) fchmod yes /usr/bin/vim.basic root 303
4. 07/12/2023 21:18:16 /etc/hosts setxattr yes /usr/bin/vim.basic root 304
5. 07/12/2023 21:18:27 /etc/hosts~ rename yes /usr/bin/vim.basic root 305
6. 07/12/2023 21:18:27 /etc/hosts openat yes /usr/bin/vim.basic root 306
7. 07/12/2023 21:18:27 (null) fchmod yes /usr/bin/vim.basic root 307
8. 07/12/2023 21:18:27 /etc/hosts setxattr yes /usr/bin/vim.basic root 308
9. 07/12/2023 21:19:19 /etc/hosts~ rename yes /usr/bin/vim.basic debian 339
10. 07/12/2023 21:19:19 /etc/hosts openat yes /usr/bin/vim.basic debian 340
11. 07/12/2023 21:19:19 (null) fchmod yes /usr/bin/vim.basic debian 341
12. 07/12/2023 21:19:19 /etc/hosts setxattr yes /usr/bin/vim.basic debian 342
[3] Rules added by [auditctl] command are not kept after restarting System, so it needs to add them in a file under [/etc/audit/rules.d] if you'd like to keep persistently. It's OK to add rules to any file name you like under [/etc/audit/rules.d], but extension should be [.rules].
# output current rules to [additional.rules]

root@dlp:~#
auditctl -l >> /etc/audit/rules.d/additional.rules

[4] If you set a directory for Audit Target, all files are targeted recursively under the directory.
# set Audit rule (reading) to [/home/testdir/]

root@dlp:~#
auditctl -w /home/testdir/ -p r -k testdir_audit

root@dlp:~#
auditctl -l

-w /etc/hosts -p wa -k hosts_change
-w /home/testdir -p r -k testdir_audit
# logs are recorded like follows

root@dlp:~#
ausearch -k testdir_audit | aureport -f -i


File Report
===============================================
# date time file syscall success exe auid event
===============================================
1. 07/12/2023 21:21:32 /home/testdir sendto yes /usr/sbin/auditctl root 52
2. 07/12/2023 21:22:40 /home/testdir/testfile.txt openat yes /usr/bin/vim.basic debian 83
3. 07/12/2023 21:22:40 /home/testdir/testfile.txt readlink no /usr/bin/vim.basic debian 84
4. 07/12/2023 21:22:40 /home/testdir/.testfile.txt.swp openat yes /usr/bin/vim.basic debian 85
5. 07/12/2023 21:22:40 /home/testdir/.testfile.txt.swx openat yes /usr/bin/vim.basic debian 86
6. 07/12/2023 21:22:40 /home/testdir/.testfile.txt.swp openat yes /usr/bin/vim.basic debian 87
7. 07/12/2023 21:22:40 /home/testdir/testfile.txt openat yes /usr/bin/vim.basic debian 88
8. 07/12/2023 21:22:44 /home/testdir/testfile.txt getxattr no /usr/bin/vim.basic debian 89
9. 07/12/2023 21:23:26 /home/testdir/test.txt openat yes /usr/bin/vim.basic bookworm 125
10. 07/12/2023 21:23:26 /home/testdir/test.txt openat yes /usr/bin/vim.basic bookworm 126
.....
.....
[5] For example, Set Audit rule that monitors files removed by users who has over UID 1000.
root@dlp:~#
auditctl -a always,exit -F arch=b64 -S unlink,unlinkat -F 'auid>=1000' -F 'auid!=-1' -F key=delete_audit

root@dlp:~#
auditctl -l

-w /etc/hosts -p wa -k hosts_change
-w /home/testdir -p r -k testdir_audit
-a always,exit -F arch=b64 -S unlink,unlinkat -F auid>=1000 -F auid!=-1 -F key=delete_audit
# logs are recorded like follows

root@dlp:~#
ausearch -k delete_audit | aureport -f -i


File Report
===============================================
# date time file syscall success exe auid event
===============================================
1. 07/12/2023 21:26:39 /run/user/1000/systemd/ unlink no /usr/lib/systemd/systemd debian 210
2. 07/12/2023 21:26:39 /run/user/1000/systemd/ unlink no /usr/lib/systemd/systemd debian 211
3. 07/12/2023 21:26:52 /home/testdir/test.txt unlinkat no /usr/bin/rm debian 233
4. 07/12/2023 21:27:12 /home/testdir/test.txt unlinkat yes /usr/bin/rm debian 239
5. 07/12/2023 21:27:32 /run/user/1000/systemd/units/invocation:dbus.socket unlink yes /usr/lib/systemd/systemd debian 269
6. 07/12/2023 21:27:32 init.scope unlinkat no /usr/lib/systemd/systemd debian 270
Matched Content