Labs · Cyber Security
Lab: Watch Your Own Login and File Events with auditd: Write Watch Rules for Key Files and Search Them with ausearch
Course: Cyber Security · Chapter 36: More Essential Kali Tools
Chapter 36 covers more essential Kali tools; this lab sets up the defender's side: lightweight monitoring that records who changed important files.
Chala mitrano! In Lab 31 the attacker created a new user. Would we have noticed? Today we switch on the Linux audit system, a built-in CCTV for important files. Every change to the user list, sudo rules or SSH settings gets recorded with who, when and which command. Halka, phukat, ani khup upayogi!
चला मित्रांनो! Lab 31 मध्ये attacker ने नवीन user बनवला. आपल्याला कळलं असतं का? आज आपण Linux audit system चालू करणार, महत्त्वाच्या files साठी built-in CCTV. User list, sudo rules किंवा SSH settings मधला प्रत्येक बदल कोणी, कधी आणि कोणत्या command ने हे सगळं record होणार. हलकं, फुकट, आणि खूप उपयोगी!
चलो दोस्तों! Lab 31 में attacker ने नया user बनाया। क्या हमें पता चलता? आज हम Linux audit system चालू करेंगे, ज़रूरी files के लिए built-in CCTV। User list, sudo rules या SSH settings में हर बदलाव किसने, कब और किस command से, सब record होगा। हल्का, मुफ़्त, और बहुत काम का!
Suppose we are…
Suppose we are a security engineer at Mphasis, and a client wants alerts when anyone changes user accounts or SSH settings on their Linux servers. A full SIEM such as Wazuh is the long-term plan, but it needs a dedicated server with lots of RAM. Today we set up the building block every SIEM agent relies on: auditd, the Linux audit system, on our own VM.
Goal of this lab
By the end you will have:
- auditd running with permanent watch rules on
/etc/passwd,/etc/group,/etc/sudoers,/etc/sudoers.d/and/etc/ssh/sshd_config. - Test events you created yourself, found with
ausearchby key, user and command. - A short report from
aureportof logins and key-based events.
What you need (all free)
- Your own Ubuntu Server VM with sudo. About 35 minutes, 1 GB RAM is enough.
Safety and ethics
Monitor only systems you own or manage, and tell other users of a shared machine that activity is audited. Audit logs can contain usernames and commands, so protect them like any other personal data.
Steps
-
Install and start the audit system:
sudo apt update && sudo apt install -y auditd sudo systemctl enable --now auditd sudo auditctl -sWhat you should see:
enabled 1in the status output. -
Write permanent watch rules.
-wis the path,-p wameans write and attribute changes,-kis a key (a tag to search by):sudo tee /etc/audit/rules.d/lab.rules > /dev/null <<'RULES' -w /etc/passwd -p wa -k identity -w /etc/group -p wa -k identity -w /etc/sudoers -p wa -k sudoers -w /etc/sudoers.d/ -p wa -k sudoers -w /etc/ssh/sshd_config -p wa -k sshd_config RULES sudo augenrules --load sudo auditctl -lWhat you should see: your five rules listed, for example
-w /etc/passwd -p wa -k identity. -
Create safe test events, like the attacker in Lab 31 but on purpose:
sudo adduser --gecos "" --disabled-password audittest sudo usermod -aG sudo audittest sudo touch /etc/ssh/sshd_config -
Search by key:
sudo ausearch -k identity -i | tail -n 20What you should see: records with
type=SYSCALL,comm=useraddorcomm=usermod,exe=/usr/sbin/useradd,auid=youruser(the real person behind sudo) and a readable time. -
Find the SSH config change:
sudo ausearch -k sshd_config -i | grep -E "type=SYSCALL" | tail -n 3What you should see:
comm=touchwith your username asauid. -
Look at logins and a summary by key:
sudo aureport -au -i | tail -n 10 sudo aureport -k -i --summaryWhat you should see: your sudo and SSH authentications with success or failure, and a count per key (
identity,sshd_config). -
Write one "alert rule" in plain words that a SIEM would use, for example: "Alert HIGH when key
identityshowsuseraddorusermodadding tosudo, outside a change window." - Clean up the test user:
sudo deluser --remove-home audittest. Then search again: the deletion is recorded too.
Ravindra Bagale's Tip
Look at auid, the audit user ID. Even when someone runs sudo or switches to root, auid still shows the person who logged in first. That is why auditd is great for answering "who really did this?". And keep rules few and focused, otherwise logs become huge. Kami pan mahatvache rules!
Ravindra Bagale's Tip – मराठी
auid बघा, म्हणजे audit user ID. कोणी sudo चालवलं किंवा root वर गेलं तरी auid मध्ये सगळ्यात आधी login केलेली व्यक्तीच दिसते. म्हणूनच "हे खरंच कोणी केलं?" याचं उत्तर देण्यासाठी auditd उत्तम आहे. आणि rules कमी आणि focused ठेवा, नाहीतर logs प्रचंड होतात. कमी पण महत्त्वाचे rules!
Ravindra Bagale's Tip – हिंदी
auid देखो, यानी audit user ID। कोई sudo चलाए या root बन जाए, तब भी auid में सबसे पहले login करने वाला इंसान ही दिखता है। इसीलिए "ये असल में किसने किया?" का जवाब देने के लिए auditd बढ़िया है। और rules कम और focused रखो, वरना logs बहुत बड़े हो जाते हैं। कम पर ज़रूरी rules!
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Adding rules only with auditctl -w |
They disappear after reboot | Put them in /etc/audit/rules.d/*.rules and run augenrules --load |
Watching whole folders like /var |
Logs fill the disk | Watch a few important files |
Searching without -i |
Numeric user IDs and raw times | Use ausearch -i for readable output |
Looking only at uid |
Shows root for every sudo command | Read auid for the real person |
Forgetting -k keys |
Hard to find events later | Give every rule a clear key |
Self-check checklist
0 of 5 done
Try-at-home challenge
Add a rule that records every command run as root by a logged-in human user, then find the commands you ran in the last 10 minutes.
Check your answer
Add to /etc/audit/rules.d/lab.rules: -a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=-1 -k root_cmds, then sudo augenrules --load. Run a few sudo commands, and search with sudo ausearch -k root_cmds -i --start recent (recent = last 10 minutes). This rule is noisier, so use it on lab or critical servers only.
Samjla ka? Watch the few files that matter, tag them, search by key, read auid. Aata pudhe jaauya: Chapter 37 reduces what your machines share on the network.