Course contentsShow
← Security
Lesson 2146 of 3,05242. Penetration Testing and Red TeamingFree lesson

Writable Files and Directory Permissions

Identifying world-writable files, scripts called by root, and exploiting weak file permissions.

Writable Files and Directory Permissions

You'll learn how attackers exploit world-writable files and directories to escalate privileges, especially when trusted processes or root-owned scripts interact with them.

The Core Vulnerability

Linux file permissions control who can read, write, and execute files. When a file or directory is "world-writable" (permissions like 777 or directories with 777), any user can modify it. This becomes dangerous when:

  • A script executed by root references a world-writable file
  • A privileged process reads configuration from a writable location
  • Libraries or binaries in writable directories get loaded by SUID/root programs

Think of it like this: Imagine a king (root) who always consults a particular scroll (config file) before making decisions. If that scroll is sitting on a public table where anyone can edit it, a peasant could rewrite the king's instructions and gain power.

Identifying Vulnerable Files

Attackers hunt for world-writable files that privileged processes use:

# Find world-writable files
find / -perm -002 -type f 2>/dev/null

# Find world-writable directories
find / -perm -002 -type d 2>/dev/null

Then they correlate these with scripts or binaries run by root (check cron jobs, startup scripts, or systemd services).

Exploitation Example

If a root-owned cron job executes /usr/local/bin/backup.sh and that file is world-writable, you can inject commands:

echo "cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash" >> /usr/local/bin/backup.sh

When root runs the script, it creates a root-owned SUID shell for you.

Directory Permission Dangers

World-writable directories in $PATH or loaded by dynamic linkers are especially critical—attackers can place malicious binaries or libraries there to be executed by privileged processes.

Key Takeaway: World-writable files and directories become privilege escalation vectors when trusted processes interact with them—always verify permissions on files referenced by privileged services, scripts, and environment variables.