You can write auditd rules with an LLM in a minute, and most of what comes back will load. That is the problem. The Linux audit system rarely tells you a rule is wrong. A rule that watches the wrong path, reads the wrong syscall argument or sits behind a lock loads quietly, and you find out during an incident, when the event you needed is not in the log.
So we tested it. We gave a goal to a model-style first draft, loaded it on a real kernel, planted three events, and searched for them with ausearch. The drafts in this post were written for this guide to show the mistakes models commonly make with audit.rules. They are not a captured model transcript, and no model was called. The kernel's audit log is the evidence.
Authorized work only. Audit rules change what a system records about its users. Write and load them on machines you own or administer, and test on a throwaway VM first.
Why write auditd rules with an LLM
The rule syntax is small, and the audit.rules man page covers it in one sitting. There are three kinds of rules:
- Control rules configure the audit system:
-Ddeletes all rules,-bsets the kernel backlog,-fsets the failure mode, and-eenables auditing or, with-e 2, locks the configuration until reboot. - File rules (watches). The current form is a syscall rule such as
-a always,exit -F arch=b64 -F path=/etc/sudoers -F perm=wa -F key=sudoers.pathmatches one file,dirmatches a directory tree, andpermtakesr,w,xanda. The older-w path -p wa -k keyform still works, but the auditctl man page calls it deprecated and slower. - Syscall rules:
-a action,list -S syscall -F field=value -k key, where the action isalwaysorneverand the list is usuallyexit.
A model is good at turning "tell me when someone changes sudo config" into that shape. It is less good at the details that decide whether the rule fires: which architecture, which argument, which path, and in what order the file loads.
The lab
We could not load rules on the machine we write from: it runs in a container, and auditctl -s there returns Operation not permitted. So everything below ran in a throwaway Debian 13 VM (kernel 6.12.118, auditd 4.0.2, coreutils 9.7, sudo 1.9.16p2). We logged in over SSH as an ordinary admin user, which gives the session a real login UID (auid) of 1000.
The goal for the model: record changes to sudo configuration, any new setuid bit, and every root command run by a real logged-in user.
After loading each rule set, the same script planted three events and one piece of noise:
- P1: wrote a new file,
/etc/sudoers.d/99-planted, withsudo tee. - P2: copied
/bin/trueto/tmp, made it root-owned and ransudo chmod u+son it. - P3: ran
sudo /usr/bin/id. - Noise: ran
hostnameas root throughsystemd-run, which runs under systemd with no login UID.
Then it ran ausearch --input-logs -k <key> for each key and counted the SYSCALL records.
Draft 1: it locked itself before loading anything
A typical first answer:
-D
-e 2
-b 8192
-w /etc/sudoers -p wa -k sudoers
-a always,exit -S chmod,fchmod,fchmodat -F a1&04000 -k setuid
-a always,exit -S execve -F euid=0 -F auid>=1000 -k root_cmd
We loaded it with auditctl -R. The result:
enabled 2
...
exit=1
### auditctl -l
No rules
-e 2 makes the configuration immutable, and every line after it was refused. In our run, auditctl -R printed no error for the refused rule. The only signs were exit code 1, enabled 2 in the status, and an empty rule list. A later auditctl -w on the same boot did say it: The audit system is in immutable mode, no rule changes allowed. The only way out was a reboot.
The auditctl man page says the lock "is intended to be the last command in audit.rules". If you load rules through augenrules, its man page says the last -e directive is always moved to the end of the merged file, so this mistake mostly bites people who load a file by hand. Either way, keep -e 2 out of the file until the rules are tested.
Draft 1 again, without the lock
We rebooted the VM, removed -e 2 and loaded the rest. auditctl printed two warnings and accepted everything:
WARNING - 32/64 bit syscall mismatch in line 6, you should specify an arch
WARNING - 32/64 bit syscall mismatch in line 7, you should specify an arch
load_exit=0
Then the planted events:
| Key | SYSCALL records | What happened |
|---|---|---|
sudoers | 0 | P1 missed. The watch covers /etc/sudoers only, not the drop-in directory |
setuid | 2 | P2 matched, and so did a plain chmod 0440 on the sudoers file |
root_cmd | 38 | P3 matched, and so did the systemd-run noise with auid=unset |
Each row is a different kind of wrong.
The sudoers watch looked at the wrong path. Many systems put sudo rules in /etc/sudoers.d/, and a new file there is a common persistence step. A watch on /etc/sudoers never sees it. That was the planted event the draft was written to catch, and the log had nothing.
The setuid rule worked by accident. The audit record for P2 shows why (trimmed):
syscall=fchmodat a0=AT_FDCWD a1=0x5596e60bdc20 a2=suid,0755 comm=chmod
chmod from coreutils calls fchmodat, whose mode is the third argument, a2. The draft tested a1, which for fchmodat is a pointer to the path string. That pointer happened to have the 0x800 bit set in both calls, so the rule matched the setuid change and the harmless 0440 change alike. The auditctl man page warns about this: string arguments are passed as pointers, and "triggering on a pointer address value is not likely to work". On another run, with different addresses, the same rule could miss P2 entirely.
The root command rule let in processes with no login. The audit system stores an unset login UID as 4294967295, which is greater than 1000. The audit.rules man page says to pair auid>=1000 with auid!=unset for exactly this reason. Our systemd-run noise showed up as auid=unset uid=root comm=hostname.
The fix
-D
-b 8192
## sudo config: the file and the drop-in directory
-a always,exit -F arch=b64 -F path=/etc/sudoers -F perm=wa -k sudoers
-a always,exit -F arch=b32 -F path=/etc/sudoers -F perm=wa -k sudoers
-a always,exit -F arch=b64 -F dir=/etc/sudoers.d -F perm=wa -k sudoers
-a always,exit -F arch=b32 -F dir=/etc/sudoers.d -F perm=wa -k sudoers
## setuid bit: the mode is a1 for chmod/fchmod, a2 for fchmodat and fchmodat2
-a always,exit -F arch=b64 -S chmod,fchmod -F a1&04000 -k setuid
-a always,exit -F arch=b64 -S fchmodat,fchmodat2 -F a2&04000 -k setuid
-a always,exit -F arch=b32 -S chmod,fchmod -F a1&04000 -k setuid
-a always,exit -F arch=b32 -S fchmodat,fchmodat2 -F a2&04000 -k setuid
## root commands from real logins only
-a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=unset -k root_cmd
-a always,exit -F arch=b32 -S execve -F euid=0 -F auid>=1000 -F auid!=unset -k root_cmd
It loaded with no warnings, and the same planted events gave:
| Key | SYSCALL records | Result |
|---|---|---|
sudoers | 2 | P1 caught: the openat that created 99-planted, then its chmod 0440 |
setuid | 1 | Only P2, a2=suid,0755 on /tmp/lab-true |
root_cmd | 36 | P3 caught; no auid=unset records |
What changed:
archcomes first and is split per ABI. The man page explains that 32-bit and 64-bit syscall numbers differ, so each rule is written twice.auditctlrefuses-Sbeforearchwitharch must be before -S. We loaded theb32rules but did not run a 32-bit program, so that half is untested here.dir=watches the drop-in directory recursively.auditctl -lshows thatpath=anddir=rules withperm=waexpand into a long list of write and attribute syscalls, which is why one line coverstee,cpandchmod.- The mode is read from the right argument for each syscall.
fchmodat2is newer andauditctl4.0.2 accepted it. Ourchmodusedfchmodat, so thefchmodat2half did not fire in this lab. auid!=unsetremoves service and cron processes from the root command key.
root_cmd is still noisy. Most of its 36 records were sudo itself and unix_chkpwd, which PAM runs during each sudo. That is a tuning decision for your environment, and aureport shows where the volume comes from:
ausearch --input-logs -k root_cmd -ts 15:55:08 --raw | aureport -x --summary
Mistakes auditctl caught, and one it did not
We also loaded single lines that models tend to write. Each was run against the real kernel and deleted afterwards:
| Line | Result |
|---|---|
-S chmod -F mode&04000 | -F unknown field: mode (there is no mode field) |
-S socketcall with arch=b64 | Syscall name unknown: socketcall |
-a always,entry -S execve | Error sending add rule data request (Invalid argument) |
-S chmod -F arch=b64 | arch must be before -S |
-w '/etc/sudoers.d/*.conf' -p wa | Loaded, with Warning - wildcard notation is not supported |
The last one is the dangerous kind. It loads as a watch on a literal path named *.conf, which never exists. The man page says wildcards are not supported; the warning is easy to miss in a long load.
One more trap: ausearch from a script
Our first search ran from a non-interactive SSH session and returned <no matches> for every key, while grep found key="root_cmd" lines in /var/log/audit/audit.log. The ausearch man page says it can take raw log data from stdin, and its --input-logs option is "needed if you are using ausearch from a cron job". With --input-logs the same search found the events. If an LLM writes your checker script, check that it does this, or every test will look like a missed event.
How to write auditd rules with an LLM
Keep the model in a short loop, with the kernel as the checker:
- Paste the syntax. The rule forms from the audit.rules man page and the
-Ffield list from the auditctl man page. Tell the model to use only those fields. - State the events, not the rules. "Changes to /etc/sudoers and anything under /etc/sudoers.d. chmod calls that set the setuid bit. execve as root by users with a login UID of 1000 or more."
- Ask for the boring parts.
archbefore-S, one rule per ABI,auid!=unsetnext toauid>=1000, a key on every rule, and no-e 2until testing is done. - Load it on a test VM and read the output. Treat any warning as a failure. Check
auditctl -lagainst what you meant. - Plant the events and search for them. One planted action per key, then
ausearch --input-logs -k <key>. A key with zero records is a failed rule, not a quiet system. - Add
-e 2last, only on systems that need the lock, after the rules are final.
Before you send anything, strip hostnames, usernames and paths that identify a client or a person. The rule text and a few redacted audit records are enough. If you want a summary of how long prompts are kept, Zero Data Retention is an opt-in setting, off by default, and what RedRouter keeps explains the rest.
Where auditd fits
auditd records syscalls and file access through the kernel audit subsystem, and its rules decide what gets written to the log in the first place. Falco rules work differently: they evaluate runtime events against conditions and raise alerts, rather than configuring what the kernel audit log records. If you query endpoint state on demand, see osquery queries with an LLM. If your audit log ends up in a SIEM, Wazuh rules with an LLM covers the detection side.
Choosing a model to write auditd rules
Rule files are short, so the cost per draft is small and the speed of the load-and-fix loop matters more. GLM 5.3 Flash Red (glm-5.3-flash-red) is a good first choice for that loop: at the time of writing it lists $0.25 input, $0.13 cached input and $1.00 output per 1M tokens. red-cyber-1, listed at $0.28 / $0.13 / $1.10 and marked Beta, is the featured model for authorized penetration testing and red-team work, and fits when the prompt carries more of an engagement's context. The models docs list current rates; plans are on /pricing. RedRouter is built for this kind of authorized security work.
FAQ
Can I write auditd rules with an LLM?
Yes, as a first draft. In our lab the first draft locked the audit system before any rule loaded. Without the lock, it missed a planted file in /etc/sudoers.d and matched setuid changes on a pointer value. The fixed rules caught all three planted events. Load every rule on a test VM and search for an event you planted.
Why is my auditd rule not logging anything?
Check five things: that auditctl -s does not show enabled 2 from a lock loaded too early, that arch comes before -S, that the rule tests the right syscall and argument, that the watched path is where the change happens, and that your search uses ausearch --input-logs when it runs from a script or cron job.
Should I use -w or -F path and -F dir?
The auditctl man page calls -w deprecated and slower and says to convert watches to the syscall form. Use -F path= for one file and -F dir= for a directory tree, with -F arch= and -F perm=.
Where does -e 2 go in audit.rules?
Last. It makes the configuration immutable until reboot, so any rule after it is refused. augenrules moves the last -e to the end of the merged file, but auditctl -R loads lines in the order they appear.
Why add auid!=unset to an auid>=1000 rule?
An unset login UID is stored as 4294967295, which passes auid>=1000. Without auid!=unset, processes started by systemd, cron and other services match a rule meant for logged-in users.
Which model should I use for auditd rules?
Start with GLM 5.3 Flash Red, and move to red-cyber-1 when the prompt needs more context. The models docs list current rates.