CAPEC-27 - Leveraging Race Conditions via Symbolic Links

This attack leverages the use of symbolic links (Symlinks) in order to write to sensitive files. An attacker can create a Symlink link to a target file not otherwise accessible to her. When the privileged program tries to create a temporary file with the same name as the Symlink link, it will actually write to the target file pointed to by the attackers' Symlink link. If the attacker can insert malicious content in the temporary file she will be writing to the sensitive file by using the Symlink. The race occurs because the system checks if the temporary file exists, then creates the file. The attacker would typically create the Symlink during the interval between the check and the creation of the temporary file.

Severity

Likelihood

Confidentiality

Integrity

Availability

  • Attack Methods 3
  • Injection
  • Time and State
  • Modification of Resources
  • Purposes 1
  • Exploitation
  • Sec Principles 1
  • Least Privilege
  • Scopes 3
  • Modify application data
  • Integrity
  • Gain privileges / assume identity
  • Authorization
  • Access_Control
  • Confidentiality
  • DoS: resource consumption (other)
  • Availability

Medium level: This attack is sophisticated because the attacker has to overcome a few challenges such as creating symlinks on the target host during a precise timing, inserting malicious data in the temporary file and have knowledge about the temporary files created (file name and function which creates them).

The attacker is able to create Symlink links on the target host.

Tainted data from the attacker is used and copied to temporary files.

The target host does insecure temporary file creation.

The attacker will certainly look for file system locations where he can write and create Symlink links.

The attacker may also observe the system and locate the temporary files created during a call to a certain function.

Step 1 - Verify that target host's platform supports symbolic links.

This attack pattern is only applicable on platforms that support symbolic links..

Tecnique ID: 1 - Environment(s) env-Embedded env-Local

Research target platform to determine whether it supports symbolic links.

Tecnique ID: 2 - Environment(s) env-Embedded env-Local

Create a symbolic link and ensure that it works as expected on the given platform.

Outcome ID: 1

Type: Success

Target platform supports symbolic links (e.g. Linux, UNIX, etc.)

Outcome ID: 2

Type: Failure

Target platform does not support symbolic links (e.g. MS Windows)


Step 2 - Examine application's file I/O behavior

Analyze the application's file I/O behavior to determine where it stores files, as well as the operations it performs to read/write files..

Tecnique ID: 1 - Environment(s) env-Local

Use kernel tracing utility such as ktrace to monitor application behavior

Tecnique ID: 2 - Environment(s) env-Local

Use debugging utility such as File Monitor to monitor the application's filesystem I/O calls

Tecnique ID: 3 - Environment(s) env-Embedded env-Local

Watch temporary directories to see when temporary files are created, modified and deleted.

Tecnique ID: 4 - Environment(s) env-Embedded env-Local

Analyze source code for open-source systems like Linux, Apache, etc.

Indicator ID: 1 - Environment(s) env-Embedded env-Local

Type: Positive

Attacker can watch files being created, modified and/or deleted by application.

Indicator ID: 2 - Environment(s) env-Embedded env-Local

Type: Inconclusive

Application does not seem to perform any filesystem I/O operations.


Outcome ID: 1

Type: Success

Attacker identifies at least one reproducible file I/O operation performed by the application.

Outcome ID: 2

Type: Failure

Attacker cannot identify any file I/O operations being performed by the application.



Step 1 - Verify ability to write to filesystem

The attacker verifies ability to write to the target host's file system..

Tecnique ID: 1 - Environment(s) env-Embedded env-Local

Create a file that does not exist in the target directory (e.g. "touch temp.txt" in UNIX-like systems)

Tecnique ID: 2 - Environment(s) env-Embedded env-Local

On platforms that differentiate between file creation and file modification, if the target file that the application writes to already exists, attempt to modify it.

Tecnique ID: 3 - Environment(s) env-Embedded env-Local

Verify permissions on target directory

Indicator ID: 1 - Environment(s) env-Embedded env-Local

Type: Positive

Target directory is a globally writable temp directory (e.g. /tmp in many UNIX-like systems)

Indicator ID: 2 - Environment(s) env-Embedded env-Local

Type: Positive

Target directory is writable by the attackers' effective user ID.


Security Control ID: 1

Type: Preventative

Store temporary files in a directory with limited permissions where malicious users cannot tamper with them.


Outcome ID: 1

Type: Success

Attacker can create and modify files in the target directory.

Outcome ID: 2

Type: Failure

Attacker cannot create or modify files in the target directory.



Step 1 - Replace file with a symlink to a sensitive system file.

Between the time that the application checks to see if a file exists (or if the user has access to it) and the time the application actually opens the file, the attacker replaces the file with a symlink to a sensitive system file..

Tecnique ID: 1 - Environment(s) env-Embedded env-Local

Create an infinite loop containing commands such as "rm -f tempfile.dat; ln -s /etc/shadow tempfile.dat". Wait for an instance where the following steps occur in the given order: (1) Application ensures that tempfile.dat exists and that the user has access to it, (2) "rm -f tempfile.dat; ln -s /etc/shadow tempfile.dat", and (3) Application opens tempfile.dat for writing, and inadvertently opens /etc/shadow for writing instead.

Tecnique ID: 2 - Environment(s) env-Embedded env-Local

Use other techniques with debugging tools to replace the file between the time the application checks the file and the time the application opens it.

Security Control ID: 1

Type: Preventative

Use file handles to check existence of files, to check permissions and to open them. Do not use filename except to obtain a handle initially.

Security Control ID: 2

Type: Preventative

Drop application's permissions to the current user's permissions before performing any file I/O operations (e.g. using Process.as_uid() in Ruby).

Security Control ID: 3

Type: Corrective

Run application with minimal permissions. In particular, avoid running applications as root on UNIX-like systems and as Administrator on Windows systems.


Outcome ID: 1

Type: Success

Sensitive file tampered with successfully.

Outcome ID: 2

Type: Failure

Sensitive file could not be tampered with.



Use safe libraries when creating temporary files. For instance the standard library function mkstemp can be used to safely create temporary files. For shell scripts, the system utility mktemp does the same thing.

Access to the directories should be restricted as to prevent attackers from manipulating the files. Denying access to a file can prevent an attacker from replacing that file with a link to a sensitive file.

Follow the principle of least privilege when assigning access rights to files.

Ensure good compartmentalization in the system to provide protected areas that can be trusted.