Prerequisites
- Target system, dependencies and environment configured.
Usage
Purpose
sudo is how Linux grants controlled elevation — but a loose sudoers rule quietly hands a user full root, because many innocuous-looking commands can spawn a shell once you can run them as root. (root) NOPASSWD: /usr/bin/vim isn't "let them edit files as root"; it's "give them a root shell". This skill covers writing and reviewing sudo rules so elevation stays scoped to what was intended.
When to use it
Configuring sudo access, reviewing an existing sudoers for over-grants, or during privilege-escalation enumeration (sudo -l is the first thing to check on a foothold). It's a frequent, high-impact escalation path and a frequent hardening win.
Procedure
- List what each user can run as root. On a host you're assessing,
sudo -l shows the current user's rights; for a full review, read /etc/sudoers and /etc/sudoers.d/*:sudo -l # what can I run elevated?
sudo visudo -c # syntax-check sudoers
- Flag the effectively-root grants first.
ALL=(ALL) ALL (or NOPASSWD: ALL) is full root — fine for a genuine admin, a finding for a service or regular account. The subtler danger is a specific command that's actually a shell escape.
- Check each allowed command against GTFOBins for sudo abuse. A huge range of commands, when runnable as root via sudo, break out to a root shell — editors (
vim, nano, less), interpreters (python, perl), and tools (find, awk, tar, git, man). If a sudo rule allows one of these, it's a root shell, not a scoped privilege:# e.g. (root) NOPASSWD: /usr/bin/find
# -> sudo find . -exec /bin/sh \; -quit (root shell)
- Watch for wildcards and argument abuse. A rule with
* (e.g. /bin/cp *) often lets the user do far more than intended (overwrite /etc/shadow, copy arbitrary files). Wildcards in command arguments are a classic over-grant.
- Check
NOPASSWD usage — it removes the re-authentication barrier, so a hijacked session or a shell escape needs no password. Minimise it; require a password for elevation where feasible.
- Look for editable targets — if a sudo rule runs a script the user can modify, or a binary in a user-writable path, that's a direct root path regardless of the command itself.
Cheatsheet
review
sudo -l # per-user elevated rights
read /etc/sudoers + /etc/sudoers.d/*
visudo -c # validate syntax (always edit with visudo)
red flags
ALL=(ALL) ALL / NOPASSWD: ALL -> full root (ok for admins, not services)
a command on GTFOBins (sudo) -> it's a ROOT SHELL, not a scoped privilege
vim nano less find awk python perl tar git man sed ... (huge list)
wildcards in args /bin/cp * -> often far more than intended
NOPASSWD -> no re-auth barrier (shell escape = free root)
sudo runs a user-editable script / binary in writable path -> direct root
principle: sudo grants the ABILITY of the command, including any shell it can spawn.
Reading the review
- A specific-command rule that's actually a GTFOBins shell escape = the user has root, just indirectly; the most common sudo over-grant and easy to miss because the rule "looks scoped". Remove or replace it.
NOPASSWD: ALL on a non-admin or service account = full passwordless root; critical. Service accounts especially should not have this.
- Wildcards in command arguments = the rule likely permits far more than intended (arbitrary file overwrite/copy). Tighten to exact arguments or a wrapper.
- A sudo-runnable script the user can edit = they edit it to do anything as root; the command doesn't even need a built-in escape.
- Password-required, tightly-scoped rules to non-escaping commands = the good state; elevation is real and bounded.
The fix
- Grant the minimum, and verify the command can't spawn a shell. Before allowing a command via sudo, check GTFOBins — if it's abusable, don't grant it directly; wrap it in a purpose-built helper that does only the intended action.
- Avoid
ALL and wildcards in command specs; specify exact commands and, where possible, exact arguments.
- Require passwords for elevation — minimise
NOPASSWD, so a shell escape or hijacked session still needs authentication.
- Never sudo-allow a user-editable script or a binary in a writable path — lock down ownership and permissions of anything sudo runs.
- Use
visudo to edit (it validates syntax and prevents lockout), and keep rules in /etc/sudoers.d/ with clear ownership.
- Audit
sudo -l for key accounts as part of hardening, and log sudo usage for detection.
Pitfalls
- Thinking a specific-command rule is safe.
sudo vim (or find, awk, less, python…) is a root shell. The command's capabilities, including shells it spawns, are what you grant — not just its nominal function.
- Wildcards.
/bin/cp * and similar quietly permit far more than intended. Avoid * in command specs.
- Overusing NOPASSWD. It removes the last barrier before root; a shell escape then needs no password. Require re-auth where you can.
- Sudo running editable content. A script or binary the user can modify (or in a writable dir) turns any sudo rule into full root.
- Editing sudoers without visudo. A syntax error can lock everyone out of sudo; visudo validates first.
References
- GTFOBins (gtfobins.github.io) — sudo abuse reference
- sudoers(5) manual
- MITRE ATT&CK — T1548.003 (Sudo and Sudo Caching)
- The linux-privilege-escalation skill (sudo -l is its first check)
Inputs
- Relevant source code, logs, network traces, or system specifications.
Outputs
- Analysis findings, security audit report, or generated code artifacts.
1---2name: sudo-hardening3description: Use when reviewing or writing sudoers configuration — granting elevated access without handing out shell escapes or effectively-root privileges by accident.4---5678## Prerequisites9- Target system, dependencies and environment configured.1011## Usage12### Purpose1314`sudo` is how Linux grants controlled elevation — but a loose sudoers rule quietly hands a user full root, because many innocuous-looking commands can spawn a shell once you can run them as root. `(root) NOPASSWD: /usr/bin/vim` isn't "let them edit files as root"; it's "give them a root shell". This skill covers writing and reviewing sudo rules so elevation stays scoped to what was intended.1516### When to use it1718Configuring sudo access, reviewing an existing sudoers for over-grants, or during privilege-escalation enumeration (`sudo -l` is the first thing to check on a foothold). It's a frequent, high-impact escalation path and a frequent hardening win.1920### Procedure21221. **List what each user can run as root.** On a host you're assessing, `sudo -l` shows the current user's rights; for a full review, read `/etc/sudoers` and `/etc/sudoers.d/*`:23 ```24 sudo -l # what can I run elevated?25 sudo visudo -c # syntax-check sudoers26 ```272. **Flag the effectively-root grants first.** `ALL=(ALL) ALL` (or `NOPASSWD: ALL`) is full root — fine for a genuine admin, a finding for a service or regular account. The subtler danger is a *specific* command that's actually a shell escape.283. **Check each allowed command against GTFOBins for sudo abuse.** A huge range of commands, when runnable as root via sudo, break out to a root shell — editors (`vim`, `nano`, `less`), interpreters (`python`, `perl`), and tools (`find`, `awk`, `tar`, `git`, `man`). If a sudo rule allows one of these, it's a root shell, not a scoped privilege:29 ```30 # e.g. (root) NOPASSWD: /usr/bin/find31 # -> sudo find . -exec /bin/sh \; -quit (root shell)32 ```334. **Watch for wildcards and argument abuse.** A rule with `*` (e.g. `/bin/cp *`) often lets the user do far more than intended (overwrite `/etc/shadow`, copy arbitrary files). Wildcards in command arguments are a classic over-grant.345. **Check `NOPASSWD` usage** — it removes the re-authentication barrier, so a hijacked session or a shell escape needs no password. Minimise it; require a password for elevation where feasible.356. **Look for editable targets** — if a sudo rule runs a script the user can modify, or a binary in a user-writable path, that's a direct root path regardless of the command itself.3637### Cheatsheet3839```40review41 sudo -l # per-user elevated rights42 read /etc/sudoers + /etc/sudoers.d/*43 visudo -c # validate syntax (always edit with visudo)4445red flags46 ALL=(ALL) ALL / NOPASSWD: ALL -> full root (ok for admins, not services)47 a command on GTFOBins (sudo) -> it's a ROOT SHELL, not a scoped privilege48 vim nano less find awk python perl tar git man sed ... (huge list)49 wildcards in args /bin/cp * -> often far more than intended50 NOPASSWD -> no re-auth barrier (shell escape = free root)51 sudo runs a user-editable script / binary in writable path -> direct root5253principle: sudo grants the ABILITY of the command, including any shell it can spawn.54```5556### Reading the review5758- **A specific-command rule that's actually a GTFOBins shell escape** = the user has root, just indirectly; the most common sudo over-grant and easy to miss because the rule "looks scoped". Remove or replace it.59- **`NOPASSWD: ALL` on a non-admin or service account** = full passwordless root; critical. Service accounts especially should not have this.60- **Wildcards in command arguments** = the rule likely permits far more than intended (arbitrary file overwrite/copy). Tighten to exact arguments or a wrapper.61- **A sudo-runnable script the user can edit** = they edit it to do anything as root; the command doesn't even need a built-in escape.62- **Password-required, tightly-scoped rules to non-escaping commands** = the good state; elevation is real and bounded.6364### The fix6566- **Grant the minimum, and verify the command can't spawn a shell.** Before allowing a command via sudo, check GTFOBins — if it's abusable, don't grant it directly; wrap it in a purpose-built helper that does only the intended action.67- **Avoid `ALL` and wildcards** in command specs; specify exact commands and, where possible, exact arguments.68- **Require passwords for elevation** — minimise `NOPASSWD`, so a shell escape or hijacked session still needs authentication.69- **Never sudo-allow a user-editable script or a binary in a writable path** — lock down ownership and permissions of anything sudo runs.70- **Use `visudo`** to edit (it validates syntax and prevents lockout), and keep rules in `/etc/sudoers.d/` with clear ownership.71- **Audit `sudo -l` for key accounts** as part of hardening, and log sudo usage for detection.7273### Pitfalls7475- **Thinking a specific-command rule is safe.** `sudo vim` (or find, awk, less, python…) is a root shell. The command's *capabilities*, including shells it spawns, are what you grant — not just its nominal function.76- **Wildcards.** `/bin/cp *` and similar quietly permit far more than intended. Avoid `*` in command specs.77- **Overusing NOPASSWD.** It removes the last barrier before root; a shell escape then needs no password. Require re-auth where you can.78- **Sudo running editable content.** A script or binary the user can modify (or in a writable dir) turns any sudo rule into full root.79- **Editing sudoers without visudo.** A syntax error can lock everyone out of sudo; visudo validates first.8081### References8283- GTFOBins (gtfobins.github.io) — sudo abuse reference84- sudoers(5) manual85- MITRE ATT&CK — T1548.003 (Sudo and Sudo Caching)86- The linux-privilege-escalation skill (sudo -l is its first check)8788## Inputs89- Relevant source code, logs, network traces, or system specifications.9091## Outputs92- Analysis findings, security audit report, or generated code artifacts.