We would like to describe how we discovered three RCE vulnerabilities, managed to escape the sandbox, and gained access to all of the company's servers.
As part of the pentest, we identified a service for managing virtual and physical servers called Foreman. It appeared to be interesting to us for further analysis and security testing of its usage.
Foreman is a tool for managing the lifecycle of physical and virtual servers. It enables system administrators to easily automate repetitive tasks, quickly deploy applications, and actively manage servers, whether local or cloud-based.
All investigations were conducted on the latest available version of Foreman as of now, version 3.4.1.
Using a standard administrator account, we began to explore the available functionality.
First, we focused on the "Provisioning Templates" functionality. It is used to configure the operating systems of connected servers.
After reviewing the documentation, we learned that ERB (Embedded Ruby) language is used for working with templates.
After obtaining the result of a simple mathematical operation, we thought we had found it, but it turned out that it was not the case. When we added the attacking payload, we encountered a SafeMode feature.
We consulted the documentation and conducted a more detailed study of the available methods at our disposal.
We were drawn to the "Bind" method.
It can be seen that "instance_vars" falls into eval on the 5th line. Based on the code, one can conclude that a request
may lead to injection into eval and execution of arbitrary code bypassing SafeMode.
And this became the First blood in getting to know the product. The SafeMode gem is a development of Foreman, as evidenced by their GitHub repository - https://github.com/theforeman/safemode. Based on the information available on public GitHub and Rubygems pages, we can assume this gem may be used in other projects where a sandbox is needed.
The Second blood came in the process of studying the Global Parameters functionality. Global Parameters simplifies the development and joint use of Puppet modules and classes. While working with global parameters, we noticed that they can be passed using various formats, including passing them through YAML.
Recalling various techniques/articles on deserializing data in YAML, we used a useful payload for "Universal RCE with Ruby YAML.load (versions > 2.7)"*.
The culmination of all the bugs found was the RCE in the Command Runner module.
As we can see, the execution of incoming commands occurs in the Foreman::CommandRunner, which is set in the Provisioning settings as "CoreOS Transpiler Command" and "Fedora CoreOS Transpiler Command".
The "value" block is of interest to us, where the location of "ct" and "fcct" is specified. Therefore, we can set an arbitrary program. For example, replace
After such changes, it is enough for us to go to the tab with Provisioning Templates and use the functionality of creating or editing a template. And as a template, you can set -
“<%= transpile_coreos_linux_config('id;whoami') %>”
Thus, our third RCE for Foreman has been revealed. Now, we don't even need to run away from the SafeMod sandbox, as we did in the very first RCE.
In conclusion, we’re extremely grateful to the Foreman team for their assistance in addressing the two CVEs, describing our findings:
Their prompt response, expertise, and professionalism have helped much.
Author: Dinko Dimitrov