Labs · Cyber Security
Lab: Audit sudo Rights and SUID Files on Your Own Linux Server and Remove What Is Not Needed
Course: Cyber Security · Chapter 26: Privilege Escalation
Chapter 26 explains how privilege escalation works; this lab removes the extra rights that make it possible.
Chala mitrano! Privilege escalation means a normal user becomes root. Most of the time it happens through extra rights someone forgot: a user still in the sudo group, a NOPASSWD rule, a program with the SUID bit. Today we audit those on our own server and clean them up. Kami adhikar, kami dhoka!
चला मित्रांनो! Privilege escalation म्हणजे साधा user root बनतो. बहुतेक वेळा हे कोणीतरी विसरलेल्या extra rights मधून होतं: sudo group मध्ये अजून असलेला user, NOPASSWD rule, SUID bit असलेला program. आज आपण आपल्या server वर ते audit करणार आणि साफ करणार. कमी अधिकार, कमी धोका!
चलो दोस्तों! Privilege escalation मतलब साधारण user root बन जाता है। ज़्यादातर ये किसी के भूले हुए extra rights से होता है: sudo group में अभी भी मौजूद user, NOPASSWD rule, SUID bit वाला program। आज हम अपने server पर इन्हें audit करेंगे और साफ़ करेंगे। कम अधिकार, कम ख़तरा!
Suppose we are…
Suppose we manage the Linux servers of a small Ola analytics team. Last month an intern got sudo "just for one day" and a helper script was given special rights. Nobody removed them. Our job: list everyone and everything that can act as root, compare it with what is really needed, and remove the rest.
Goal of this lab
By the end you will be able to:
- List members of the
sudogroup and every sudo rule, includingNOPASSWD. - List all SUID files (programs that run with their owner's rights, often root) and spot one that does not belong to any package.
- Remove the extra sudo right and the extra SUID bit, and prove it.
What you need (all free)
- Your own Ubuntu Server VM (Lab 17). On Amazon Linux the admin group is
wheelinstead ofsudo, and you userpm -qfinstead ofdpkg -S. - About 35 minutes.
Safety and ethics
Audit only servers you own or manage. Never remove your own admin rights: keep a second terminal logged in as admin while you make changes, so you cannot lock yourself out.
Part 1: set up the "forgotten" rights (practice only)
-
On your VM create a practice user and give them sudo, as if an intern had been added:
sudo adduser --gecos "" intern sudo usermod -aG sudo intern -
Plant a harmless SUID copy of the
idprogram, as if a helper had been given special rights:sudo cp /usr/bin/id /usr/local/bin/helper-id sudo chmod u+s /usr/local/bin/helper-id
Part 2: audit sudo
-
List who is in the admin group:
getent group sudoWhat you should see:
sudo:x:27:youruser,intern. Ask yourself: doesinternstill need this? -
Look for rules that skip the password and check the sudoers syntax:
sudo grep -rn "NOPASSWD" /etc/sudoers /etc/sudoers.d/ sudo visudo -cWhat you should see:
parsed OKfor each file. On cloud images you may find aNOPASSWDline for the default user (for example in90-cloud-init-users); note it and decide whether it is acceptable. -
See exactly what one user may run:
sudo -l -U intern
Part 3: audit SUID files
-
List every SUID file on the system:
sudo find / -xdev -perm -4000 -type f 2>/dev/nullWhat you should see: about 10–20 paths such as
/usr/bin/passwd,/usr/bin/sudo,/usr/bin/su,/usr/bin/mount, and also/usr/local/bin/helper-id. -
Check which package installed each file. Files from the operating system belong to a package; anything that does not is suspicious:
for f in $(sudo find / -xdev -perm -4000 -type f 2>/dev/null); do dpkg -S "$f" >/dev/null 2>&1 || echo "NOT FROM A PACKAGE: $f"; doneWhat you should see:
NOT FROM A PACKAGE: /usr/local/bin/helper-id.
Part 4: remove and verify
-
Remove the extra sudo right and the SUID file:
sudo gpasswd -d intern sudo sudo chmod u-s /usr/local/bin/helper-id sudo rm /usr/local/bin/helper-id -
Verify both:
getent group sudo sudo -l -U intern sudo find / -xdev -perm -4000 -type f -newer /etc/hostname 2>/dev/nullWhat you should see:
internis gone from the group,User intern is not allowed to run sudo, and the last command prints nothing new. -
Save the clean SUID list as your baseline, so next month you can compare:
sudo find / -xdev -perm -4000 -type f 2>/dev/null | sort > ~/suid-baseline.txt -
Clean up the practice user:
sudo deluser --remove-home intern.
Ravindra Bagale's Tip
A baseline turns a long audit into a quick diff. Next month run the same find, then diff ~/suid-baseline.txt new.txt. Anything new needs a reason. Same idea for getent group sudo: keep a list of who should be there. Simple ani powerful!
Ravindra Bagale's Tip – मराठी
Baseline मुळे मोठं audit एका छोट्या diff मध्ये बदलतं. पुढच्या महिन्यात तोच find चालवा, मग diff ~/suid-baseline.txt new.txt. नवीन काही दिसलं तर त्याला कारण हवं. getent group sudo साठी पण तेच: कोण असायला हवं याची list ठेवा. सोपं आणि powerful!
Ravindra Bagale's Tip – हिंदी
Baseline से लंबा audit एक छोटे diff में बदल जाता है। अगले महीने वही find चलाओ, फिर diff ~/suid-baseline.txt new.txt। कुछ नया दिखे तो उसका कारण चाहिए। getent group sudo के लिए भी यही: कौन होना चाहिए उसकी list रखो। आसान और powerful!
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Editing /etc/sudoers with nano |
One typo can break sudo for everyone | Use sudo visudo and check with visudo -c |
Removing your own user from sudo |
You lose admin access | Keep a second admin session open; remove only others |
Removing SUID from /usr/bin/passwd or sudo |
Users cannot change passwords or use sudo | Only remove SUID from files that are not from a package or not needed |
Searching without -xdev |
find wanders into /proc and mounted drives and is slow |
Use -xdev |
| No baseline | Next audit starts from zero | Save suid-baseline.txt |
Self-check checklist
0 of 5 done
Try-at-home challenge
Give intern permission to run only systemctl restart nginx as root, nothing else. How do you write and check that rule?
Check your answer
Run sudo visudo -f /etc/sudoers.d/intern-nginx and add one line: intern ALL=(root) /usr/bin/systemctl restart nginx. Check with sudo visudo -c and sudo -l -U intern, which should list only that command. This is least privilege: one exact command instead of full sudo.
Samjla ka? Know who can be root, remove what is not needed, keep a baseline. Aata pudhe jaauya: Chapter 27 is about people, not programs.