An old wooden punch-card time clock between two racks of time cards, with the article title about writing Linux auditd rules with an LLM
Cover based on a photo by Rodw, via Wikimedia Commons, Public domain

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: -D deletes all rules, -b sets the kernel backlog, -f sets the failure mode, and -e enables 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. path matches one file, dir matches a directory tree, and perm takes r, w, x and a. The older -w path -p wa -k key form 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 is always or never and the list is usually exit.

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:

  1. P1: wrote a new file, /etc/sudoers.d/99-planted, with sudo tee.
  2. P2: copied /bin/true to /tmp, made it root-owned and ran sudo chmod u+s on it.
  3. P3: ran sudo /usr/bin/id.
  4. Noise: ran hostname as root through systemd-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:

KeySYSCALL recordsWhat happened
sudoers0P1 missed. The watch covers /etc/sudoers only, not the drop-in directory
setuid2P2 matched, and so did a plain chmod 0440 on the sudoers file
root_cmd38P3 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.

Diagram of LLM-written auditd rules tested in a lab: a draft with -e 2 first loads zero rules, a draft with a single sudoers file watch misses a planted sudoers.d file and matches setuid on a pointer, and the fixed rules catch all three planted events
Locking first loaded nothing. Without the lock, the draft missed the planted sudoers.d file and matched setuid on the wrong argument. The fixed rules caught all three planted events. Image: RedRouter, Original

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:

KeySYSCALL recordsResult
sudoers2P1 caught: the openat that created 99-planted, then its chmod 0440
setuid1Only P2, a2=suid,0755 on /tmp/lab-true
root_cmd36P3 caught; no auid=unset records

What changed:

  • arch comes 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. auditctl refuses -S before arch with arch must be before -S. We loaded the b32 rules but did not run a 32-bit program, so that half is untested here.
  • dir= watches the drop-in directory recursively. auditctl -l shows that path= and dir= rules with perm=wa expand into a long list of write and attribute syscalls, which is why one line covers tee, cp and chmod.
  • The mode is read from the right argument for each syscall. fchmodat2 is newer and auditctl 4.0.2 accepted it. Our chmod used fchmodat, so the fchmodat2 half did not fire in this lab.
  • auid!=unset removes 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:

LineResult
-S chmod -F mode&04000-F unknown field: mode (there is no mode field)
-S socketcall with arch=b64Syscall name unknown: socketcall
-a always,entry -S execveError sending add rule data request (Invalid argument)
-S chmod -F arch=b64arch must be before -S
-w '/etc/sudoers.d/*.conf' -p waLoaded, 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:

  1. Paste the syntax. The rule forms from the audit.rules man page and the -F field list from the auditctl man page. Tell the model to use only those fields.
  2. 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."
  3. Ask for the boring parts. arch before -S, one rule per ABI, auid!=unset next to auid>=1000, a key on every rule, and no -e 2 until testing is done.
  4. Load it on a test VM and read the output. Treat any warning as a failure. Check auditctl -l against what you meant.
  5. 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.
  6. Add -e 2 last, 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.