Linux Privilege Escalation (THM)¶
Privilege escalation is a process, not a single exploit. There are no universal techniques that work everywhere. The success of privilege escalation depends on multiple system-specific factors such as:
-
Kernel version
-
Installed applications
-
User permissions
-
Misconfigurations
-
Enabled services
In real-world penetration testing, gaining initial access rarely provides administrative privileges. Privilege escalation allows an attacker to elevate their access and fully compromise the system.
What is Privilege Escalation?¶
Privilege escalation is the act of exploiting a vulnerability, design flaw, or misconfiguration to gain higher-level permissions than originally granted.
Types¶
-
Vertical escalation: User → root/admin
-
Horizontal escalation: User → another user with more privileges
Why Privilege Escalation is Important¶
Achieving higher privileges allows an attacker to:
-
Reset or steal passwords
-
Access protected data
-
Modify system configurations
-
Maintain persistence
-
Add or modify users
-
Execute unrestricted administrative commands
Enumeration (Most Important Phase)¶
Enumeration is required after initial access, not just before exploitation. Always assume more attack paths exist.
System Information Enumeration¶
hostname¶
Identifies the system name. Can reveal the system role.
Example:
uname -a¶
Displays kernel version and architecture.
Used to search for kernel exploits.
/proc/version¶
Shows kernel details and compiler information.
/etc/issue¶
Identifies OS distribution.
Process Enumeration¶
ps¶
View running processes.
Look for:
-
Root-owned processes
-
Custom scripts
-
Services running as root
Environment Variables¶
env¶
Displays environment variables.
Check:
-
PATH hijacking opportunities
-
Available interpreters (python, perl, gcc)
Sudo Permissions¶
sudo -l¶
Lists allowed sudo commands.
If a command is allowed, check it on GTFOBins.
File and User Enumeration¶
ls -la¶
Always use -la to find hidden files.
id¶
Shows user and group information.
/etc/passwd¶
Lists users on the system.
Extract usernames:
Find real users:
history¶
May contain passwords or sensitive commands.
Network Enumeration¶
Interfaces¶
Used to identify pivoting opportunities.
netstat¶
Check open ports and connections.
Look for:
-
Internal services
-
Root-owned listeners
-
Unexpected network activity
File Discovery with find¶
Common Finds¶
find / -name flag.txt 2>/dev/null
find / -type f -perm 0777 2>/dev/null
find / -perm -u=s -type f 2>/dev/null
Writable Directories¶
Development Tools¶
Automated Enumeration Tools¶
Used to save time (never rely on only one):
-
LinPEAS
-
LinEnum
-
Linux Exploit Suggester
-
Linux Smart Enumeration
-
Linux Priv Checker
Privilege Escalation Techniques¶
1. Kernel Exploitation (Kernel-Level Privilege Escalation)¶
What is the Kernel?¶
The Linux kernel is the core of the operating system. It controls:
-
Memory
-
CPU scheduling
-
Hardware access
-
Process permissions
The kernel always runs as root.
If you compromise the kernel, you become root.
Why Kernel Exploits Work¶
Kernel exploits take advantage of:
-
Outdated kernels
-
Vulnerable system calls
-
Race conditions
-
Memory corruption bugs
Since user-space applications rely on the kernel, a flaw here allows direct privilege escalation.
When Kernel Exploits Are Viable¶
Kernel exploits are typically used when:
-
No sudo access exists
-
No SUID binaries are exploitable
-
System is unpatched or legacy
-
CTF or lab environment
They are last-resort techniques in real pentests.
Enumeration for Kernel Exploits¶
Key things to note:
-
Kernel version
-
Architecture (x86_64, i686)
-
Distribution
-
Compiler version
Finding Exploits¶
Search using:
-
Google
-
CVE Details
-
exploit-db
-
searchsploit
-
Linux Exploit Suggester (LES)
Example:
Execution Flow¶
-
Transfer exploit to target
-
Compile if needed
-
Run exploit
-
Kernel bug triggers
-
Privilege escalates to root
Risks¶
-
System crash
-
Kernel panic
-
Data corruption
Never use blindly in production pentests.
2. Sudo-Based Privilege Escalation¶
What is sudo?¶
sudo allows a user to run specific commands as root.
Admins often allow limited sudo access, but mistakes happen.
Enumeration¶
This shows:
-
Allowed commands
-
Whether a password is required
-
Environment variables preserved
Why sudo Escalation Works¶
Many binaries can:
-
Execute shell commands
-
Spawn interactive shells
-
Read/write sensitive files
-
Load external libraries
Even if the binary itself is safe, its features may not be.
GTFOBins¶
GTFOBins documents known abuse techniques for binaries.
Examples:
-
vim
-
less
-
find
-
tar
-
awk
-
python
-
nmap
Example: sudo vim¶
Inside vim:
You now have a root shell.
LD_PRELOAD Exploitation (Advanced sudo)¶
What is LD_PRELOAD?¶
LD_PRELOAD forces a program to load a shared library before anything else.
If run as root → code executes as root.
Conditions Required¶
-
sudo allows the binary
-
env_keep+=LD_PRELOADenabled -
Binary does not drop privileges
Why It Works¶
The dynamic linker loads attacker-controlled code before main() executes.
Result¶
-
Root shell
-
Full control
This is a high-impact misconfiguration.
3. SUID (Set User ID) Exploitation¶
What is SUID?¶
SUID allows a binary to execute with the owner’s privileges, not the user’s.
Most SUID binaries are owned by root.
Why SUID Is Dangerous¶
If a SUID binary:
-
Allows file editing
-
Executes commands
-
Loads plugins
-
Spawns shells
It can be abused to escalate privileges.
Enumeration¶
Typical Exploitable SUID Binaries¶
-
nano
-
vim
-
find
-
cp
-
less
-
bash (rare)
-
custom scripts
Example: SUID nano¶
If nano is SUID-root:
You can:
-
Read password hashes
-
Modify system files
-
Add root users
Adding a Root User (Classic Technique)¶
-
Generate password hash
-
Edit
/etc/passwd -
Add user with UID 0
-
Switch user
This avoids password cracking entirely.
Why This Works¶
Linux trusts file permissions.
If you can write system authentication files as root → full compromise.
4. Linux Capabilities Exploitation¶
What are Capabilities?¶
Capabilities split root privileges into fine-grained permissions.
Examples:
-
cap_net_bind_service
-
cap_setuid
-
cap_sys_admin
Why Capabilities Exist¶
To avoid giving full root access for:
-
Network tools
-
Debuggers
-
Monitoring software
Enumeration¶
Why This Is Dangerous¶
Some binaries with capabilities can:
-
Change UID
-
Spawn shells
-
Execute commands
Even without SUID.
Example: vim with cap_setuid¶
The shell inherits elevated privileges.
Why This Works¶
Capabilities are applied at runtime.
If the binary allows command execution, it inherits the capability.
5. Cron Job Exploitation¶
What is Cron?¶
Cron schedules tasks to run automatically.
Cron jobs often run as:
-
root
-
service accounts
Why Cron Jobs Are Dangerous¶
If:
-
Script is writable
-
Path is not absolute
-
Script is deleted but cron remains
You can inject malicious code.
Enumeration¶
Writable Script Exploit¶
If root runs:
And you can edit backup.sh, insert:
PATH-Based Cron Exploit¶
If cron runs:
Without absolute path:
-
Create
antivirus.shin writable PATH -
Cron executes your script as root
Why This Works¶
Cron blindly trusts paths and scripts.
Poor maintenance creates privilege escalation.
6. PATH Hijacking¶
What is PATH?¶
PATH tells Linux where to search for executables.
Why PATH Hijacking Works¶
If:
-
A privileged binary executes a command without absolute path
-
You control a directory in PATH
You can replace the command.
Example¶
SUID binary executes:
Linux searches PATH for ls.
Exploitation Steps¶
-
Add writable directory to PATH
-
Create fake binary
-
Execute vulnerable program
-
Fake binary runs as root
Why This Is Realistic¶
Many custom scripts:
-
Forget absolute paths
-
Trust environment variables
7. NFS (Network File System) Exploitation¶
What is NFS?¶
NFS allows remote file sharing.
Config stored in:
no_root_squash Explained¶
By default:
- Root becomes
nfsnobody
With no_root_squash:
- Root remains root
Why This Is Dangerous¶
If share is writable:
-
You can create SUID binaries remotely
-
Execute them locally as root
Exploitation Flow¶
-
Mount NFS share
-
Create SUID root shell
-
Execute on target
-
Root access gained
Why This Works¶
NFS trusts remote root.
This violates local security boundaries.
Final Mental Model (Very Important)¶
Privilege escalation succeeds because of:
-
Trust assumptions
-
Misconfigurations
-
Convenience over security
-
Poor maintenance
Linux does exactly what it’s told to do.
Recommended Practice Order¶
-
Enumeration
-
Sudo
-
SUID
-
Capabilities
-
Cron
-
PATH
-
NFS
-
Kernel exploits
Personal Linux Privilege Escalation Checklist¶
0. Pre-Flight (Stabilize Access)¶
-
Upgrade shell
-
Confirm user context
-
Avoid noisy actions
1. System & Kernel Enumeration¶
Goal: Identify kernel-level escalation possibilities
✔ Note:
-
Kernel version
-
OS distribution
-
Architecture
2. User & Group Enumeration¶
Goal: Understand permission boundaries
Check other users:
3. Sudo Privileges (Highest Priority)¶
Goal: Abuse misconfigured sudo rules
If ANY binary is allowed:
-
Check GTFOBins
-
Test for shell escapes
-
Test file read/write abuse
-
Check
env_keep
High-value checks:
-
vim
-
less
-
find
-
tar
-
awk
-
python
-
nmap
4. Environment Variables¶
Goal: Detect PATH and LD_PRELOAD abuse
Questions:
-
Is PATH writable?
-
Can PATH be modified?
-
Is LD_PRELOAD preserved by sudo?
5. SUID Binary Enumeration¶
Goal: Find root-owned executables with unsafe behavior
Check each binary on GTFOBins:
-
Shell escape?
-
File edit?
-
Command execution?
High-risk binaries:
-
nano
-
vim
-
find
-
cp
-
bash
-
python
6. Linux Capabilities¶
Goal: Identify fine-grained privilege leaks
Focus on:
-
cap_setuid
-
cap_setgid
-
cap_sys_admin
-
cap_dac_read_search
If binary allows shell execution → exploit
7. Cron Jobs¶
Goal: Hijack scheduled root execution
Check:
-
Writable scripts
-
Missing scripts
-
Relative paths
-
Wildcards
If writable:
-
Inject reverse shell
-
Wait for execution
8. Writable Files & Directories¶
Goal: Detect unsafe permissions
Focus on:
-
/etc
-
/opt
-
/usr/local
-
custom app folders
9. PATH Hijacking¶
Goal: Replace executed binaries
If writable directory exists:
Check for:
-
SUID binaries
-
Root scripts
-
Cron jobs using relative paths
10. Network & Services¶
Goal: Identify pivoting and misconfigurations
Look for:
-
Root-owned listeners
-
Local-only services
-
Backup services
-
Admin interfaces
11. NFS & Shared Resources¶
Goal: Abuse no_root_squash
Check for:
-
no_root_squash
-
Writable shares
12. Credentials & Secrets¶
Goal: Steal or reuse credentials
Check:
-
.bash_history
-
config files
-
scripts
-
backup files
13. Kernel Exploits (LAST RESORT)¶
Goal: Exploit unpatched kernels
Only attempt if:
-
No misconfigurations found
-
System allows instability
-
You understand the exploit
14. Exploit Chaining Review¶
Before exploiting, ask:
-
Can I chain two small findings?
-
Is there a cleaner path?
-
Can I avoid kernel exploits?
15. Post-Escalation Validation¶
Confirm root:
Validate access:
-
/etc/shadow
-
/root
-
system configs
16. Cleanup (Real Pentests Only)¶
-
Remove payloads
-
Restore modified files
-
Document everything
Mental Rules (Never Break These)¶
-
Enumerate before exploiting
-
Prefer misconfigurations
-
Avoid kernel exploits unless required
-
One clean root shell is enough
-
Document every step
How to Use This Checklist¶
-
Print it
-
Put it in your notes
-
Convert to markdown
-
Follow top-to-bottom every time
-
Never skip steps