Auditd : Add Audit Rules2026/06/11 |
|
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 # perm : [r|w|x|a] : specify target action for Audit # ⇒ r=read, w=write, x=execute, a=attributes # key : [words] : set keys for searching logs root@dlp:~# auditctl -a always,exit -F arch=b64 -F path=/etc/hosts -F perm=wa -F key=hosts_change root@dlp:~# auditctl -l -a always,exit -F arch=b64 -S open,bind,truncate,ftruncate,rename,mkdir,rmdir,creat,link,unlink,symlink,chmod,fchmod,chown,fchown,lchown,mknod,acct,swapon,quotactl,setxattr,lsetxattr,fsetxattr,removexattr,lremovexattr,fremovexattr,openat,mkdirat,mknodat,fchownat,unlinkat,renameat,linkat,symlinkat,fchmodat,fallocate,renameat2,openat2 -F path=/etc/hosts -F perm=wa -F key=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 File Report =============================================== # date time file syscall success exe auid event =============================================== 1. 06/11/26 01:05:17 /etc/hosts~ rename yes /usr/bin/vim.basic root 500 2. 06/11/26 01:05:17 /etc/hosts openat yes /usr/bin/vim.basic root 501 3. 06/11/26 01:05:17 (null) fchmod yes /usr/bin/vim.basic root 502 4. 06/11/26 01:05:17 /etc/hosts setxattr yes /usr/bin/vim.basic root 503 5. 06/11/26 01:05:41 /etc/hosts openat yes /usr/bin/bash root 504 6. 06/11/26 01:06:14 /etc/hosts openat yes /usr/bin/nano root 505 7. 06/11/26 01:06:44 /etc/hosts openat yes /usr/lib/cargo/bin/coreutils/tee root 506 |
| [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 -a always,exit -F arch=b64 -F dir=/home/testdir -F perm=r -F key=testdir_audit root@dlp:~# auditctl -l -a always,exit -F arch=b64 -S open,bind,truncate,ftruncate,rename,mkdir,rmdir,creat,link,unlink,symlink,chmod,fchmod,chown,fchown,lchown,mknod,acct,swapon,quotactl,setxattr,lsetxattr,fsetxattr,removexattr,lremovexattr,fremovexattr,openat,mkdirat,mknodat,fchownat,unlinkat,renameat,linkat,symlinkat,fchmodat,fallocate,renameat2,openat2 -F path=/etc/hosts -F perm=wa -F key=hosts_change -a always,exit -F arch=b64 -S open,readlink,quotactl,getxattr,lgetxattr,fgetxattr,listxattr,llistxattr,flistxattr,openat,readlinkat,openat2 -F dir=/home/testdir -F perm=r -F key=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. 06/11/26 01:08:41 /home/testdir sendto yes /usr/sbin/auditctl root 279 2. 06/11/26 01:09:38 /home/testdir/ openat yes /usr/bin/gnumv root 280 3. 06/11/26 01:09:48 /home/testdir/test.txt openat yes /usr/bin/vim.basic root 281 4. 06/11/26 01:09:48 /home/testdir/test.txt readlink no /usr/bin/vim.basic root 282 5. 06/11/26 01:09:48 /home/testdir/.test.txt.swp openat yes /usr/bin/vim.basic root 283 6. 06/11/26 01:09:48 /home/testdir/.test.txt.swx openat yes /usr/bin/vim.basic root 284 7. 06/11/26 01:09:48 /home/testdir/.test.txt.swp openat yes /usr/bin/vim.basic root 285 8. 06/11/26 01:09:48 /home/testdir/test.txt openat yes /usr/bin/vim.basic root 286 9. 06/11/26 01:09:52 /home/testdir/test.txt getxattr no /usr/bin/vim.basic root 287 10. 06/11/26 01:09:52 /home/testdir/test.txt~ listxattr yes /usr/bin/vim.basic root 288 11. 06/11/26 01:10:37 /home/testdir openat yes /usr/lib/cargo/bin/coreutils/ls root 289 12. 06/11/26 01:10:37 /home/testdir listxattr yes /usr/lib/cargo/bin/coreutils/ls root 290 13. 06/11/26 01:10:37 /home/testdir/ruleset.nft listxattr yes /usr/lib/cargo/bin/coreutils/ls root 291 14. 06/11/26 01:10:37 /home/testdir/test.txt listxattr yes /usr/lib/cargo/bin/coreutils/ls root 292 15. 06/11/26 01:10:37 /home/testdir/testfile.txt listxattr yes /usr/lib/cargo/bin/coreutils/ls root 293 ..... ..... |
| [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 -a always,exit -F arch=b64 -S open,bind,truncate,ftruncate,rename,mkdir,rmdir,creat,link,unlink,symlink,chmod,fchmod,chown,fchown,lchown,mknod,acct,swapon,quotactl,setxattr,lsetxattr,fsetxattr,removexattr,lremovexattr,fremovexattr,openat,mkdirat,mknodat,fchownat,unlinkat,renameat,linkat,symlinkat,fchmodat,fallocate,renameat2,openat2 -F path=/etc/hosts -F perm=wa -F key=hosts_change -a always,exit -F arch=b64 -S open,readlink,quotactl,getxattr,lgetxattr,fgetxattr,listxattr,llistxattr,flistxattr,openat,readlinkat,openat2 -F dir=/home/testdir -F perm=r -F key=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. 06/11/26 01:15:38 /run/user/1000/systemd/ unlink no /usr/lib/systemd/systemd ubuntu 316 2. 06/11/26 01:15:38 /run/user/1000/systemd/ unlink no /usr/lib/systemd/systemd ubuntu 317 3. 06/11/26 01:15:38 /run/user/1000/systemd/ unlink no /usr/lib/systemd/systemd ubuntu 318 4. 06/11/26 01:15:42 test.txt unlinkat yes /usr/bin/gnurm ubuntu 325 5. 06/11/26 01:15:44 testfile.txt unlinkat yes /usr/bin/gnurm ubuntu 326 6. 06/11/26 01:15:47 services.txt unlinkat yes /usr/bin/gnurm ubuntu 327 |
| Sponsored Link |
|
|