Cyber Security News

Rails Apps File Write Vulnerability Let Attackers Execute Code Remotely

Researchers uncovered a critical security vulnerability in Rails applications that leverages the Bootsnap caching library. This exploit allows attackers to achieve remote code execution (RCE) by exploiting an arbitrary file write vulnerability. 

The issue is particularly concerning as Bootsnap has been a default component in Rails since version 5.2.

Understanding The Vulnerability

The vulnerability stems from a standard arbitrary file write flaw. Below is an example of vulnerable code:

According to the Conviso Research Team, in this scenario, users can control both the file path and content. This allows malicious actors to write files to arbitrary locations on the server, potentially leading to RCE.

Despite the exploit’s potential, certain restrictions make its execution non-trivial. Rails production environments often use Docker containers with limited writable directories such as /tmp, db, and log. 

Investigate Real-World Malicious Links & Phishing Attacks With Threat Intelligence Lookup - Try for Free

However, these limitations can be bypassed by targeting specific writable directories, such as tmp/cache/bootsnap.

Bootsnap Cache Exploitation

Bootsnap optimizes Rails applications by caching expensive computations. Its cache files are stored in tmp/cache/bootsnap and include compiled Ruby files. These files follow a specific structure consisting of a header (cache key) and the compiled content.

By overwriting a cache file with malicious Ruby code and triggering its execution during application startup, attackers can achieve RCE. An overview of the exploitation process is as follows:

  • Identify Target File: Select a file likely to be executed during application startup (e.g., set.rb from Ruby’s standard library).
  • Generate Malicious Cache: Create a cache key using Bootsnap’s hashing mechanism. Embed malicious Ruby code into the cache file.
  • Write Malicious Cache: Use the arbitrary file write vulnerability to overwrite the target cache file.
  • Restart Application: Trigger a server restart by writing to tmp/restart.txt, leveraging Puma’s restart functionality.
  • Execute Malicious Code: During the restart, the application loads the malicious cache file, executing the attacker’s payload.

Mitigation Strategies

  • Restrict Writable Directories: Limit writable paths in production environments.
  • Validate User Input: Sanitize filenames and paths to prevent directory traversal attacks.
  • Monitor Cache Files: Regularly inspect Bootsnap cache directories for unauthorized changes.
  • Update Dependencies: Ensure Bootsnap and Rails are updated to their latest versions with security patches.

This vulnerability highlights the risks associated with arbitrary file write flaws in web applications. By abusing Bootsnap’s caching mechanism, attackers can escalate privileges to achieve RCE. 

Developers should adopt stringent security practices, including input validation and dependency management, to mitigate such threats effectively.

Integrating Application Security into Your CI/CD Workflows Using Jenkins & Jira -> Free Webinar

Guru Baran

Gurubaran KS is a cybersecurity analyst, and Journalist with a strong focus on emerging threats and digital defense strategies. He is the Co-Founder and Editor-in-Chief of Cyber Security News, where he leads editorial coverage on global cybersecurity developments.

Recent Posts

Hackers Target AI Infrastructure With RCE, Prompt Injection and API Key Theft

Hackers are actively probing AI systems, turning exposed gateways and agent tools into routes for…

4 hours ago

Hackers Make Phishing Pages Change Their Code Every Time Someone Opens Them

Hackers are making some phishing pages harder to track by changing the code delivered to…

4 hours ago

Iran-Linked Hackers Reportedly Knock UK Power Plant Offline for Four Days

A cyber incident reportedly forced a British power plant to halt operations for about four…

5 hours ago

Russian Hackers Use New HOOKEDGE Malware to Spy on European Defense and Diplomatic Targets

Russian hackers have used a new backdoor called HOOKEDGE to target defense manufacturers, government bodies,…

6 hours ago

Ransomware Gang Claims AI Can Analyze 700GB of Stolen Data Every Hour

TITAN ransomware is pairing file encryption with an ambitious claim: artificial intelligence that can sort…

6 hours ago

Hackers Compromise Hundreds of WordPress Sites to Deploy Amatera Stealer via ClickFix

A fake student resume is being used to place a remote-access tool on researchers’ Windows…

7 hours ago