Binary Symlink Privilege Escalation
Binary symlink privilege escalation is a technique where an attacker abuses symbolic links (symlinks) to trick a privileged program into executing malicious code or performing unintended actions with elevated privileges. This type of attack often targets misconfigured or insecurely coded scripts or binaries that execute with higher privileges, such as root.
How It Works
Understanding Symlinks:
- A symlink is a file that points to another file or directory.
- For example:
1ln -s /path/to/target /path/to/symlink
Privilege Escalation via Symlinks:
- An attacker creates a symlink pointing to a malicious binary or file.
- A privileged program inadvertently interacts with the symlink, allowing the attacker to manipulate the file or execute code with elevated privileges.
Scenarios for Exploiting Symlinks
Writable Directories in Privileged Context:
- If a privileged program creates or interacts with files in a directory writable by unprivileged users, an attacker can replace those files with symlinks pointing to sensitive locations like
/etc/passwd.
- If a privileged program creates or interacts with files in a directory writable by unprivileged users, an attacker can replace those files with symlinks pointing to sensitive locations like
Temporary File Vulnerabilities:
- Programs that create temporary files without securely checking their existence are susceptible to symlink attacks.
- Example:
- A script creates a temporary file
/tmp/somefilewithout checking if it already exists. - The attacker creates a symlink
/tmp/somefilepointing to/etc/shadow. - When the privileged program writes to
/tmp/somefile, it overwrites/etc/shadow.
- A script creates a temporary file
Replacing Binaries:
- If a privileged service or cron job executes a script from a writable directory, an attacker can replace it with a symlink pointing to a malicious script.
Practical Example
1. Symlink Attack to Modify /etc/passwd
Assume a root script copies a file to
/etc/passwdwithout checking its destination:1cp /tmp/userfile /etc/passwdExploitation:
1 2ln -s /etc/passwd /tmp/userfile echo "malicious_entry:x:0:0:root:/root:/bin/bash" >> /tmp/userfileWhen the script runs, the symlink redirects
/tmp/userfileto/etc/passwd, adding the malicious entry with root privileges.
2. Temporary File Exploitation
A root-owned script writes logs to
/tmp/log.txt:1echo "System Log" > /tmp/log.txtExploitation:
1ln -s /etc/shadow /tmp/log.txtThe script overwrites
/etc/shadow, potentially corrupting it or injecting malicious content.
Mitigations
Secure Temporary File Creation:
Use secure functions like
mktempto create unique temporary files.1 2temp_file=$(mktemp) echo "Secure Data" > "$temp_file"
Validate Symlinks:
Use the
O_NOFOLLOWflag when opening files to avoid following symlinks:1open("/tmp/file", O_WRONLY | O_CREAT | O_NOFOLLOW);
Restrict Permissions:
Ensure sensitive directories and files are not writable by unprivileged users.
For example,
/tmpshould have thestickybit set:1chmod +t /tmp
Avoid Privileged Actions on User-Controlled Files:
- Avoid executing privileged operations on files in user-writable locations.
Monitor and Audit:
Regularly monitor for suspicious symlinks in writable directories using tools like
find:1find /tmp -type l -ls
