iac.htora.dev · security templates

Home/Templates/Record what investigations ask first

Record what investigations ask first

auditd rules on Linux for account changes, sudo rights, SSH keys, persistence, kernel modules, and commands people ran as root.

Ansible

Why bother

The first questions on a Linux host are usually these. Who changed an account or gave themselves sudo? Which SSH key was added? What was set to start on its own, and what did someone run as root? The default logs answer few of them. auditd records each one with the account that signed in, even after sudo, when the rules ask for it.

How you know it worked

Change a watched file, such as adding a file to /etc/sudoers.d. ausearch -k privilege shows it with the account that did it.

How it works

1Someone changes a watched file or runs a command as rootafter signing in as themselves2The kernel records itwith the signed-in account, which sudo does not change3auditd writes ittagged with a key you search by4syslog carries it off the hostthrough the host log template (Standard)
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Ansible. auditd rules in /etc/audit/rules.d, loaded with augenrules

What you need first

  • An inventory, and an account you can become root with.
  • For forwarding (Standard), the host log shipping template or another syslog forwarder on each host.

What it creates

  • /etc/audit/rules.d/50-iac.rules: watches on account, sudo, SSH, and persistence files, kernel module loads, and root commands by people
  • 32-bit coverage on x86_64, a larger kernel backlog, and PAM and audit configuration watches (Standard)
  • The auditd syslog plugin switched on, so records reach the host log forwarder (Standard)
  • An optional lock, -e 2, that stops anyone changing the rules until reboot (Standard)

The code

Quick startReadyone file, 76 lines
One playbook. Watches on accounts, sudo, SSH, and persistence files, kernel module loads, and 64-bit root commands by people.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini playbook.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini playbook.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini playbook.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download playbook.yml
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-audit-rules/linux/t0
playbook.yml
Open playbook.yml as plain text
StandardReadysix files, 184 lines
A role. Adds 32-bit coverage on x86_64, extra watches as data, a larger backlog, syslog forwarding, an optional lock, and a read-back of the loaded rules.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all six files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-audit-rules/linux/t1
roles/audit_rules/defaults/main.yml
Open main.yml as plain text
roles/audit_rules/handlers/main.yml
Open main.yml as plain text
roles/audit_rules/meta/main.yml
Open main.yml as plain text
roles/audit_rules/tasks/main.yml
Open main.yml as plain text
roles/audit_rules/templates/50-iac.rules.j2
Open 50-iac.rules.j2 as plain text
site.yml
Open site.yml as plain text
HardenedReadythree files, 159 lines
Standard's roles, unchanged, with your settings in settings.yml. Rules refuse settings that remove a default watch, malform a watch, stop logging root commands or sending to syslog, or shrink the backlog. A daily check-mode run reports drift, and ansible-lint runs on the production profile.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all three files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-audit-rules/linux/t2
guard.yml
Open guard.yml as plain text
settings.yml
Open settings.yml as plain text
site.yml
Open site.yml as plain text

How to check it worked

sudo touch /etc/sudoers.d/iac-test, then ausearch -k privilege -ts recent. The record shows the file and the account that signed in. Remove the file afterwards.

What catches people

auditd refuses a plain systemctl restart. The templates load rules with augenrules --load, and Standard restarts the daemon with the service command, which auditd allows.

With the lock on, a later rule change fails the run until the host reboots. Settle the rules first, then turn the lock on.

Commands by every user are far louder than commands run as root. Standard has that switch off by default; turn it on where you can absorb the volume.

How to undo it

Delete /etc/audit/rules.d/50-iac.rules and run augenrules --load. With the lock on, the old rules stay until reboot.

What it costs

None beyond the disk and forwarding volume. Root commands by people are a small share; every user's commands can be large.

Good to know

  • Every rule carries a key. ausearch -k <key> finds its records, and the same key is in each record forwarded to your SIEM.
  • auid is the account that signed in. It survives sudo, so records show the person behind a root command.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.