/etc/passwd and /etc/shadow Privilege Escalation
Misconfigured permissions on /etc/passwd and /etc/shadow represent a critical failure. If /etc/passwd is writable, inject a new root user. If /etc/shadow is readable, crack password hashes offline. If /etc/shadow is writable, overwrite the root password directly. All three paths lead to root without any exploit or kernel vulnerability.
Prerequisites Check
Check permissions, ACLs, and immutable flags — permissions on disk can be overridden by ACLs or chattr:
-rw-r--r-- 1 root root 2981 Jan 1 12:00 /etc/passwd -rw-r----- 1 root shadow 1843 Jan 1 12:00 /etc/shadow
Confirm the system actually uses local file authentication — some environments use LDAP or SSSD and ignore local files entirely:
passwd: files sssd shadow: files sss
If the output shows files first, local file changes take effect. If it only shows sssd or ldap, local injection will not authenticate.
Use getent as the source of truth — it queries through NSS the same way the system does:
root:x:0:0:root:/root:/bin/bash
Writable /etc/passwd — User Injection
When /etc/passwd is writable, add a new user with UID 0. The password hash goes directly in the second field — no shadow file needed.
Generate a password hash:
root:$y$j9T$salt$hash:19000:0:99999:7:::
Append the new root user:
$6$salt$hash...
Verify NSS sees it:
Authenticate:
newroot:x:0:0:root:/root:/bin/bash
Passwordless shortcut — if you want no password at all:
This works only if PAM is not configured to reject empty passwords. On most lab systems it works.
Remove root's password from shadow — if /etc/shadow is also writable, delete root's password field entirely:
Check if this actually works — some PAM configurations block null passwords:
nullok or nullok_secure present means empty passwords are accepted.
Verify and clean up after exploitation:
root:$y$j9T$salt$hash
Readable /etc/shadow — Offline Cracking
If /etc/shadow is readable, extract and crack hashes offline.
Extract the root hash:
y
Identify the hash format by the prefix:
Analyzing '$y$j9T$...' [+] Yescrypt
Prefix | Format | Hashcat Mode | Cracker |
|---|---|---|---|
| yescrypt | N/A | John only |
| SHA-512 crypt |
| Hashcat or John |
| SHA-256 crypt |
| Hashcat or John |
| MD5 crypt |
| Hashcat or John |
| bcrypt |
| Hashcat or John |
Modern Debian/Ubuntu uses yescrypt ($y$) which is not supported by hashcat. Use John:
Unshadow for John — if you want to crack all users at once, combine passwd and shadow:
Transfer the hash file to your attack box for GPU cracking if the password does not crack quickly on CPU.
Writable /etc/shadow — Direct Overwrite
If /etc/shadow is writable, replace the root hash with a known one. First check whether root is locked:
If the second field starts with ! or *, the account is locked. You can unlock it by replacing the field with a valid hash.
Generate a replacement hash:
$y$j9T$salt$hash
Use sed to replace root's hash field safely:
$6$salt$hash...
Verify the change:
Authenticate:
root: /etc/passwd and /etc/shadow agree No errors detected.
References
-
passwd(5) Manualman7.org/linux/man-pages/man5/passwd.5.html (opens in new tab)
passwd file format and field meanings
-
shadow(5) Manualman7.org/linux/man-pages/man5/shadow.5.html (opens in new tab)
shadow fields, lock characters, and semantics
-
getent(1) Manualman7.org/linux/man-pages/man1/getent.1.html (opens in new tab)
NSS-aware lookups for passwd/shadow sources
-
Hashcat Example Hasheshashcat.net/wiki/doku.php?id=example_hashes (opens in new tab)
Reference for hash mode selection and format examples
Was this helpful?
Your feedback helps improve this page.