Privilege
Sudoers Backdoors: Linux Persistence via sudo Rules
How attackers abuse /etc/sudoers and sudoers.d with NOPASSWD, !authenticate and env_keep LD_PRELOAD to keep root on Linux, and how to audit sudo rules.
- MITRE ATT&CK
- T1548.003
- Tactics
- Persistence, Privilege escalation, Defense evasion
- Runs
- Privilege
- Set up by
- root
- Locations
- /etc/sudoers
- /etc/sudoers.d/*
- /etc/sudo.conf
How it works
sudo's default policy plugin reads /etc/sudoers. Debian, Ubuntu, RHEL and Fedora end that file with an include of /etc/sudoers.d, written #includedir or, since sudo 1.9.1, @includedir; despite the #, the first form is not a comment. Files in that folder are read in lexical order, and names that end in ~ or contain a . are skipped. #include and @include can pull in any other file. sudo refuses a sudoers file that is world-writable or not owned by root. visudo edits with locking and a syntax check.
A rule reads who host = (runas) TAGS: commands:
%sudo ALL=(ALL:ALL) ALL # Debian, Ubuntu default
%wheel ALL=(ALL) ALL # RHEL, Fedora default
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app
When several rules match, the last one wins. By default sudo asks for the invoking user's own password and caches it for a few minutes. Defaults lines change behaviour globally, per user (Defaults:alice), per run-as user (Defaults>root), per host or per command. env_reset, on by default, starts commands with a minimal environment; env_keep lists the variables let through.
Cloud images add /etc/sudoers.d/90-cloud-init-users, which gives the image's default user NOPASSWD:ALL. It is legitimate, and it is the template intruders copy.
Rules can also come from LDAP or SSSD (the sudoers: line in /etc/nsswitch.conf), and /etc/sudo.conf chooses which plugins sudo loads.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/sudoers | Main policy | root (through visudo) |
/etc/sudoers.d/* | Drop-ins in lexical order; names with a . or a trailing ~ are ignored | root, packages, cloud-init |
Any path named by #include or @include | Extra policy files | root |
/etc/sudo.conf | Which plugins sudo loads, and from where | root |
/usr/libexec/sudo/ or /usr/lib/sudo/ | sudoers.so and other plugins | packages |
/etc/nsswitch.conf | Whether rules also come from LDAP or SSSD | root |
How attackers use it
Passwordless root for an account they control
# /etc/sudoers.d/95-sysupdate
svc_backup ALL=(ALL) NOPASSWD: ALL
A drop-in named like a package or cloud-init file, or one more line in 90-cloud-init-users, blends in. It pairs with a planted or revived local account.
A harmless-looking command that spawns a shell
www-data ALL=(root) NOPASSWD: /usr/bin/find
sudo find /tmp -maxdepth 0 -exec /bin/sh \; -quit
Editors, pagers, interpreters, tar, git, env, systemctl and many more give a root shell the same way; the GTFOBins project catalogues them.
Turn authentication off without NOPASSWD
Defaults:deploy !authenticate
No NOPASSWD appears anywhere, so a grep for it finds nothing, yet every rule for deploy runs without a password.
Keep LD_PRELOAD
Defaults env_keep += "LD_PRELOAD"
Any allowed command then loads the user's library as root:
sudo LD_PRELOAD=/tmp/x.so /usr/bin/systemctl status app
Hide the rule
Cmnd_Alias HEALTHCHECK = /usr/bin/python3
monitor ALL=(root) NOPASSWD: HEALTHCHECK
#include /var/lib/misc/.policy
Defaults !syslog
An alias hides the program behind a harmless name, an include keeps the real rule outside /etc/sudoers.d, and !syslog stops sudo logging its commands to syslog.
Replace the plugin
Pointing /etc/sudo.conf at another policy plugin, or patching sudoers.so or the sudo binary, changes sudo's behaviour without a single sudoers line. Package verification catches the changed files.
How to check a host
On a live host
sudo visudo -c # parses everything, names every file read
sudo ls -la --time-style=full-iso /etc/sudoers.d/
sudo grep -rnE 'NOPASSWD|authenticate|env_keep|env_reset|SETENV|include|syslog|_Alias' /etc/sudoers /etc/sudoers.d/
sudo -l -U deploy # effective rules for one account, LDAP/SSSD included
grep -E '^sudoers' /etc/nsswitch.conf; cat /etc/sudo.conf 2>/dev/null
dpkg -V sudo # rpm -V sudo on RHEL and Fedora
grep -E 'sudo:.*COMMAND=' /var/log/auth.log # /var/log/secure on RHEL; or journalctl _COMM=sudo
visudo -c prints one line per file it parsed, which exposes #include targets outside /etc/sudoers.d. On Debian and Ubuntu /etc/sudoers is a conffile of the sudo package, so dpkg -V sudo shows ??5?????? c /etc/sudoers when it was edited, and ??5?????? against the binary or plugins if they were replaced.
Offline or from a collection
- Read
/etc/sudoers, every file insudoers.dand every#includetarget. Note which files sudo would load: names with a.or a trailing~are skipped, and the owner and mode in the image tell you whether sudo would refuse a file. - On Debian and Ubuntu, compare
md5sum etc/sudoerswith the sudo package'sConffiles:entry invar/lib/dpkg/status. On RPM systems, runrpm --root <mount> -V sudo. - Map file times against sudo usage in
auth.logorsecure(each run logs user, terminal, working directory and command), and against sudo I/O logs iflog_outputwas enabled.
Linux Persistence parses these files from a collection.
What Linux Persistence flags
One entry per rule, named who → runas with its tags, and one per Defaults line that weakens sudo.
- Passwordless sudo to anything (high):
NOPASSWDand a command list ending inALL. - Passwordless sudo (medium):
NOPASSWDon specific commands. - Full sudo (low):
ALL, with a password. - Can run command as root (medium): a rule that is not
ALLallows a program that can spawn a shell: editors, pagers,find,awk,sed, interpreters, shells,env,tar,zip,git,cp,mv,tee,dd,chmod,chown,docker,rsync,ssh,openssl,systemctl,journalctl,crontab, package managers,pip,make,gcc,tcpdumpand a few more. The title says root whatever the run-as user. - sudo asks no password (!authenticate) (high): a
Defaultsline with!authenticate. - sudo keeps LD_PRELOAD (high):
env_keepwithLD_PRELOAD,LD_LIBRARY_PATH,PYTHONPATHorPERL5LIB. - Package layer: when
/etc/sudoersis the packaged, unchanged file, its flags are shown as info, so the distribution's%sudo ALLis no alarm. An edited one adds Differs from package package (low) and keeps its flags at full severity. A drop-in no package installed adds Not installed by any package (low). Changed date marks files modified within 7 days of the collection, and program also runs from mechanisms links an allowed script that another mechanism also starts.
Without the dpkg database (RPM hosts), the default rules keep their severity and show as Full sudo. The cloud-init drop-in shows as Passwordless sudo to anything: check that the account is the image's default user.
Limits: aliases are not expanded, so a rule allowing a Cmnd_Alias is judged on the alias name and a shell-capable program behind it is missed. #include lines are skipped and targets outside sudoers.d are not read. A file in sudoers.d that sudo ignores (a . in its name or a trailing ~) is still listed, with Ignored by sudo (file name) and its other flags shown as info: the rules never apply, but someone wrote them. The SETENV tag, !env_reset, !syslog, sudo.conf and LDAP or SSSD rules are not checked. Rules for %sudo or %wheel depend on group members, which the local accounts checks list.
Cleaning up
- Preserve:
cp -athe sudoers files, hash them, save thevisudo -candsudo -l -Uoutput, and export the sudo log lines for the accounts involved: they show what was run as root. - Keep a root shell open. Move rogue drop-ins out of
/etc/sudoers.dinto your evidence folder rather than editing them in place, fix/etc/sudoerswithvisudo, and runvisudo -cagain. - If the binary, the plugins or
sudo.conffail verification, reinstall sudo (apt install --reinstall sudo,dnf reinstall sudo); modified configuration files are not overwritten by that. - Check the other ways back to root:
sudo,wheelanddockermembership, SUID binaries and file capabilities, polkit rules in/etc/polkit-1/rules.d, PAM changes tosuandsudo, a system-wide LD_PRELOAD, and the account's SSH keys. - Rotate: a passwordless or
!authenticaterule means the account was root. Treat what root can read as exposed: shadow hashes, SSH host and user keys, service credentials.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: