Link Following
CWE-59: Improper Link Resolution Before File Access ('Link Following')
CWE-59 refers to a security vulnerability that occurs when an application resolves symbolic or hard links to files before properly validating the link’s target. This can allow attackers to control which file or directory the application accesses, potentially leading to unauthorized access to sensitive files, privilege escalation, or other types of attacks.
How It Works:
This vulnerability arises when a program opens a file path (which may be controlled by the user) and fails to check whether the path refers to a symbolic link. Instead, the program blindly follows the link and accesses the file it points to, without any validation. If an attacker can control or manipulate the symbolic link’s target, they can trick the program into accessing sensitive files or data, such as system files that should be protected.
Vulnerable Scenario (Local and Remote)
1. Local Attack ( which is likely ) :
Vulnerable Application: A local file viewer or editor that opens any file without validating whether it is a symlink.
Attack Steps:
The attacker creates a symbolic link (
symlink.txt
) that points to/etc/passwd.
The attacker uploads the symbolic link to the server or places it within a directory accessible by the vulnerable application.
The application opens the symbolic link as if it were a regular file, and it ends up reading
/etc/passwd
.
The attacker gains unauthorized access to sensitive information, such as user credentials stored in
/etc/passwd
.
2. Remote Attack (via HTTP Request):
Vulnerable Web Application: A file upload service where users can upload files for sharing, without checking for symbolic links.
Attack Steps:
The attacker uploads a symbolic link (e.g.,
user_file.txt
) through the web interface. This symlink points to/etc/passwd
on the server.The web application stores the file in the server's file storage system without validating whether it's a symlink.
The attacker, knowing the file's location (e.g.,
/uploads/user_file.txt
), requests the file via an HTTP request:The application, unaware that
user_file.txt
is a symlink, follows it and opens/etc/passwd
instead of the intended file.
The attacker can access sensitive system information through the web interface, such as user credentials stored in
/etc/passwd:
Vulnerable Application Example
Let's take a look at an example in Python where the application reads a file without checking if it's a symbolic link
Lab Setup:
Create a vulnerable directory for file uploads and the sensitive file:
Create a sensitive file (e.g.,
/etc/passwd
or any other important file on the system):Create a symbolic link from the uploaded file path to the sensitive file:
Run the vulnerable application:
Exploit the vulnerability:
The attacker can upload a symbolic link to
/uploads/user_file.txt
, which points to/etc/passwd
. When the application reads the file, it will follow the symlink and access the sensitive file.
Impact and Exploitation
As seen in the example, the vulnerable application opens the file /uploads/user_file.txt
without checking whether it’s a symbolic link. Since /uploads/user_file.txt
is a symlink to /etc/passwd
, the application reads and prints the contents of /etc/passwd
, exposing sensitive system information. This results in information leakage, where data that should be protected is inadvertently exposed.
Mitigation Strategy
To mitigate this vulnerability, it is essential to verify the target of symbolic links before following them. In Python, this can be done using the os.path.islink()
function to detect symbolic links before accessing them.
Here’s a secure version of the code:
n this version, the program first checks whether the file is a symbolic link using os.path.islink()
. If the file is a symbolic link, the program aborts the operation and prevents accessing the target file, thus preventing the exploitation of the vulnerability.
Resources
Last updated