Skip to content
RCE Write-up Intermediate

3 Symfony (RCE): A Peek Behind the Curtain

A Symfony application had its /_fragment endpoint enabled and used the default secret key, allowing an attacker to sign requests and execute arbitrary PHP code. The attacker achieved remote code execution on three subdomains, including a production application.

Omar ElSayed Follow

Published on medium.com

Hi, in this write up I will be sharing my 3 RCE Findings, and simply explain everything.

Omar ElSayed, on medium.com

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. 6 min read.

Context

Symfony is a free and open source PHP framework. One of its built-in features is the /_fragment endpoint, which lets clients supply custom PHP commands and returns the HTML output. To prevent misuse, Symfony signs /_fragment requests with a secret key: the signature is generated with the HMAC algorithm over a plain text value, which is the URL, together with the secret key. The secret key is known only to the server, and a request is rejected if the signature does not match the expected value.

The target in this write-up is a subdomain referred to as calculator-test.redacted.com, which served the default Symfony homepage with debug mode enabled. The same application also existed at calculator.redacted.com and calculator-dev.redacted.com.

From the original on medium.com

Setup

No accounts or special roles were needed. The researcher worked from a browser and a shell with PHP available, and used the following resources to learn how Symfony fragments work:

The attack relies on a Symfony application with the /_fragment page enabled and a secret key that is weak, default or leaked. The researcher notes that the default secret value might be in use, and that the secret can be leaked through phpinfo, among several other ways.

From the original on medium.com

Finding it

The researcher came across calculator-test.redacted.com during routine hunting and saw the default Symfony homepage with debug mode enabled.

From the original on medium.com

He first tested known misconfigurations that could expose sensitive data:

  1. Tried to access the Symfony Profiler (not enabled).
  2. Tried to get a phpinfo file (did not find it).
  3. Tried a directory brute force (did not help).
  4. Since debug mode was enabled, tried to force the app to return errors that might contain sensitive information (did not work).
  5. Tried to access the /_fragment page (enabled), then ran the ambionics SCRIPT; it returned some errors and was not able to exploit it (did not work).

None of these worked. Because this was his first time dealing with Symfony, he researched the framework and concluded the following:

  • Symfony is a free and open source PHP framework that makes web development faster and easier.
  • Symfony has a built-in feature, /_fragment, which allows clients to provide custom PHP commands and return the HTML output.
  • Symfony signs /_fragment requests with a secret key to prevent misuse.
  • A weak or leaked secret could lead to RCE.
  • Anyone with the /_fragment secret key can execute arbitrary PHP code on the server, leading to RCE.
  • The default secret value might be in use.
  • The secret value can be leaked through phpinfo, among several ways.

Exploitation

Step 1: Understand the signature behaviour

The researcher states that the /_fragment endpoint returns a 403 Forbidden status code if a request is made to it without a valid signature, but if a valid signature is provided, the endpoint should return a 404 Not Found status code. The signature is generated using the HMAC algorithm over a plain text value, which is the URL, and a secret key known only to the server; the request is rejected if the signature does not match the expected value.

From the original on medium.com
This screenshot shows a request to calculator-test.redacted.com/_fragment without a valid signature.

Because the ambionics script output was some weird errors, he decided to check it manually.

Step 2: Assume the default secret

With /_fragment enabled on calculator-test.redacted.com, he assumed the secret key was the default value:

Text
ThisTokenIsNotSoSecretChangeIt

He generated the signature with the following PHP code:

Bash
php -r "echo(urlencode(base64_encode(hash_hmac('sha256', 'https://calculator-test.redacted.com/_fragment', 'ThisTokenIsNotSoSecretChangeIt', 1))) . PHP_EOL);"

The code generates an HMAC signature for the given URL/endpoint, secret key and algorithm, then encodes the signature using base64 and URL encoding. The output is used to send requests to /_fragment.

From the original on medium.com

Step 3: Validate the hash

He validated the hash by sending a request to /_fragment and using the output as the value of the _hash parameter:

HTTP
https://calculator-test.redacted.com/_fragment?_hash=M76MW%2Fbf%2BBsOzND95apIHF%2BW2PD9ofEuBovlI600xpM%3D

He opened the URL with the base64 encoded hash. It responded with a 404 status code, which means the key is valid.

From the original on medium.com
We’re almost in.
My thoughts were crazy lmao

Step 4: Build the exploit URL

To exploit it, the _path parameter is used together with _controller, the system function and the command to execute. The same signing steps are then used to get a valid signature for the exploit URL:

Text
_path=_controller%3Dsystem%26command%3Did%26return_value%3Dnull

This sets the _controller value to the system function and then executes the id command. The signature is then computed for the full URL:

From the original on medium.com

Step 5: Execute the command

He opened the resulting URL in the browser:

The id command got executed.

`id` command got executed. From the original on medium.com

Step 6: Find more instances

He quickly created a script to find all subdomains that use Symfony and have the /_fragment page enabled. It found calculator.redacted.com and calculator-dev.redacted.com. Both shared the same secret value and were vulnerable to the same RCE, so using the same steps he gained 3 RCEs in total.

From the original on medium.com
calculator.redacted.com was the production application
/_fragment — After The Mitigation
I hope y’all enjoyed the writeup.

Root cause

The application left the /_fragment endpoint enabled and used the default Symfony secret value, ThisTokenIsNotSoSecretChangeIt. Because the signature is only an HMAC over the URL with that secret, anyone who knows or guesses the secret can produce valid signatures and pass arbitrary _path values to the endpoint. The _controller value is then resolved to the system function and the command parameter is executed, which gives remote code execution. The signing mechanism therefore did not stop the attack: it only checks that the request was signed with the secret, and the secret was the publicly known default.

Impact

An attacker who can reach a Symfony application with /_fragment enabled and a default or leaked secret can execute arbitrary PHP code on the server, leading to RCE. In this case the same secret was shared across calculator-test.redacted.com, calculator.redacted.com (the production application) and calculator-dev.redacted.com, so the same steps yielded three RCEs.

Fix

The report shows a screenshot labelled /_fragment — After The Mitigation, and the timeline records the issue as fixed on Dec 8, 2022, one day after a request for more info and three days after the report on Dec 6, 2022. A bounty was rewarded on Dec 9, 2022. The report does not describe the specific change that was made.

Variants to test

  • Check other Symfony applications for the default secret ThisTokenIsNotSoSecretChangeIt by signing a request to /_fragment and looking for a 404 instead of a 403.
  • Look for the secret in phpinfo output or other exposed debug pages, then sign a _path payload with it.
  • Enumerate subdomains of the same organisation and test whether they share the same secret, as happened here with the test, production and dev hosts.
  • Try the ambionics symfony-exploits script first; if it errors, fall back to manual HMAC generation as described above.

Timeline

Date Event
Dec 6, 2022 Reported
Dec 7, 2022 Need more info
Dec 8, 2022 Fixed
Dec 9, 2022 Bounty rewarded

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

A security researcher found three websites belonging to the same company that were all built with the same web framework, Symfony. Each of those sites had a special built-in page switched on, called /_fragment, and each of them was still using the framework's factory default password for that page. Because that default password is written in public documentation, anyone who knows it can ask the site to run commands on its own server. The researcher used it to run the id command, which is a harmless way of proving that code execution is possible. He did this on a test site, then found the same weakness on a development site and on the production site, so the same trick worked three times. The timeline records the issue as fixed the day after a request for more information, and a bounty paid the day after that.

From the original on medium.com

Who is writing this

The author introduces himself as Omar, known online as bxrowski0x, a bug hunter who reports vulnerabilities to companies. A bug bounty programme is a scheme where a company invites outsiders to look for security problems in its systems and pays them for valid reports. This article is his account of three findings of the same kind.

From the original on medium.com

How the vulnerability was found

He was browsing for targets as usual and came across a subdomain, which is a smaller site that sits under a company's main domain name, for example shop.example.com under example.com. He refers to it as:

Text
calculator-test.redacted.com

That host showed the default Symfony welcome page, and it had debug mode turned on. Debug mode is a development setting that makes an application show detailed error messages and internal information instead of a plain error page. It is meant for programmers, not for the public internet.

From the original on medium.com

When he saw it, he first tried a set of well-known misconfigurations, meaning settings that are left in a state that exposes too much. The five things he tried were:

  1. Access the Symfony Profiler. The profiler is a debugging tool bundled with Symfony that shows requests, database queries and configuration. It was not enabled.
  2. Look for a phpinfo file. phpinfo is a standard PHP page that prints the server's configuration, including environment variables and sometimes secrets. He did not find one.
  3. Run a directory brute force. This means asking the server for a long list of common file and folder names to see which ones exist. It did not help.
  4. Because debug mode was on, try to make the application throw errors that might reveal sensitive information. This did not work.
  5. Try to reach the /_fragment page. This one was enabled. He then ran a public exploitation script from a company called ambionics, which automates attacks on this Symfony feature. For some reason it returned errors and did not manage to exploit the target, so that did not work either.

None of the five attempts succeeded.

Since this was his first time working with Symfony, he read up on the framework. He names two resources he used: a page on book.hacktricks.xyz about testing Symfony web applications, and an article on www.ambionics.io about Symfony's secret and the fragment feature. Reading these gave him a working understanding of how fragments are signed.

What he learned about Symfony

  • Symfony is a free, open source framework written in PHP. A framework is a ready-made set of building blocks that programmers use to assemble a web application faster. PHP is a programming language commonly used to build websites.
  • Symfony has a built-in feature called /_fragment. It lets a client send a small piece of PHP code to the server and get back the HTML that the code produces. HTML is the language web pages are written in.
  • To stop people misusing /_fragment, Symfony requires each request to it to carry a signature made with a secret key. A signature here is a short string of characters computed from the request and the secret; it proves the request was made by someone who knows the secret.
  • If that secret is weak or has leaked, the protection is worthless and can lead to RCE, short for remote code execution: the ability to make the server run commands of your choosing.
  • Anyone who has the /_fragment secret can run arbitrary PHP code on the server, which is exactly RCE.
  • The secret might still be set to the value Symfony ships with by default.
  • The secret can leak through phpinfo, among several other ways.

RCE using /_fragment

From the original on medium.com

Before showing the attack, the author states two rules about how the endpoint behaves:

The /_fragment endpoint returns a 403 Forbidden status code if a request is made to it without a valid signature. But if a valid signature is provided, the endpoint should return a 404 Not Found status code.

A status code is the three-digit number a web server sends back with every response. 403 means "I understood you but I refuse". 404 means "I could not find that page". The odd part, and the useful part for an attacker, is that a correctly signed request gets 404 rather than a normal page: the signature check passes, and then the request fails for a different reason. So the status code alone tells you whether your signature was accepted.

The signature is generated using the HMAC algorithm and a plain text which is the URL and a secret key. The secret key is known only to the server. The request is rejected if the signature does not match the expected value.

HMAC is a standard way of combining a message and a secret key into a fixed-length fingerprint. Change either the message or the key and the fingerprint changes completely. It is like a wax seal on an envelope: only someone holding the stamp can make a matching seal, and any tampering with the letter breaks the match. Here the "letter" is the URL being requested and the "stamp" is the secret key.

This screenshot shows a request to calculator-test.redacted.com/_fragment without a valid signature. From the original on medium.com

The screenshot shows a request to calculator-test.redacted.com/_fragment sent without a valid signature, and the rejection that follows.

Because the ambionics script had produced strange errors, he decided to work through the check by hand.

Step 1: Assume the default secret

With /_fragment confirmed as enabled on calculator-test.redacted.com, he made a guess: the secret key was still the value Symfony uses out of the box. That value is the default Symfony ships with, so it is not secret at all once a site forgets to change it.:

Text
ThisTokenIsNotSoSecretChangeIt

He then computed a signature for the plain /_fragment URL with this PHP one-liner:

Bash
php -r "echo(urlencode(base64_encode(hash_hmac('sha256', 'https://calculator-test.redacted.com/_fragment', 'ThisTokenIsNotSoSecretChangeIt', 1))) . PHP_EOL);"

Line by line, in plain words:

  • php -r runs a piece of PHP code given directly on the command line, without saving it to a file. PHP is the language Symfony is written in, so it is the natural tool for reproducing what the server does.
  • hash_hmac('sha256', ...) computes the HMAC fingerprint. sha256 is the hashing algorithm used. The next argument is the message being signed, here the full URL https://calculator-test.redacted.com/_fragment. The argument after that is the secret key, ThisTokenIsNotSoSecretChangeIt. The final 1 asks for the raw binary form of the fingerprint rather than a hex string.
  • base64_encode(...) turns that raw binary into text made of ordinary characters, so it can travel safely in a URL. Base64 is a standard way of representing binary data as letters and digits.
  • urlencode(...) then escapes any characters that would be ambiguous inside a URL, such as +, / and =, replacing them with % codes.
  • echo(...) prints the result, and PHP_EOL adds a newline at the end so the output is tidy.

The output is the signature string he will paste into the request.

From the original on medium.com

Step 2: Check whether the signature is accepted

He sent a request to /_fragment with the computed value placed in a parameter called _hash:

HTTP
https://calculator-test.redacted.com/_fragment?_hash=M76MW%2Fbf%2BBsOzND95apIHF%2BW2PD9ofEuBovlI600xpM%3D

A parameter is a named value attached to the end of a URL after a question mark. Here the only parameter is _hash, and its value is the signature. The %2F and %3D sequences are the URL-encoded forms of / and =, produced by the urlencode step above.

He opened that URL in the browser. The server answered with a 404 status code. By the rule stated earlier, that means the signature was accepted and the secret key is valid. If the key had been wrong, the answer would have been 403.

From the original on medium.com
We’re almost in.
My thoughts were crazy lmao

Step 3: Build the exploit URL

Now that the key was confirmed, the next step was to make the endpoint do something useful. The _path parameter is the part of the request that tells Symfony which internal piece of code to run. He filled it with a small instruction that names the PHP function system as the controller, passes it the command id, and sets a return value of null:

Text
_path=_controller%3Dsystem%26command%3Did%26return_value%3Dnull

Decoded, the %3D characters are = and the %26 characters are &, so this reads as three settings joined together: _controller=system, command=id and return_value=null. In other words: use the system function, which runs a shell command and prints its output, and run the command id. The id command simply reports which user the server process is running as. It is the standard, harmless way to prove that command execution works, because it changes nothing on the machine.

Because the URL has now changed, the old signature no longer matches. HMAC fingerprints change completely when the message changes, so he had to sign the new, longer URL:

This is the same command as before with one difference: the message being signed is now the full exploit URL, including the _path parameter. Everything else, the algorithm, the secret and the encoding steps, is unchanged.

From the original on medium.com

Step 4: Run the command

He opened the resulting URL in his browser:

This URL carries both parts: the _path that says what to run, and the _hash that proves the request was signed with the secret. The server checked the signature, found it matched, and executed the id command.

`id` command got executed. From the original on medium.com

The screenshot accompanies the note that the id command got executed.

Step 5: Find more instances

Having one working attack, he wrote a small script to look for other subdomains that use Symfony and have /_fragment enabled. The script found two more:

Text
calculator.redacted.com
calculator-dev.redacted.com

Both of them used the same secret value as the test host, so the same steps worked against them without any change. That brought the total to three RCEs: one on the test host, one on the development host and one on production.

From the original on medium.com
calculator.redacted.com was the production application

The production application was calculator.redacted.com, the live site real users would reach.

/_fragment — After The Mitigation From the original on medium.com

The report includes a screenshot labelled /_fragment — After The Mitigation.

I hope y’all enjoyed the writeup. From the original on medium.com

Why it worked

The application left the /_fragment endpoint switched on and kept the default Symfony secret, ThisTokenIsNotSoSecretChangeIt. The signature is only an HMAC over the URL using that secret. Anyone who knows or guesses the secret can therefore produce valid signatures and pass any _path value they like. The _controller value is then resolved to the system function and the command parameter is executed, which is remote code execution.

The signing mechanism did not stop the attack. It only checks that a request was signed with the secret, and the secret was the publicly known default.

What the impact was

An attacker who can reach a Symfony application with /_fragment enabled and a default or leaked secret can run arbitrary PHP code on the server, which is RCE. Here the same secret was shared across the test, production and development hosts, so the same steps produced three RCEs.

How it was fixed

The report shows the screenshot labelled /_fragment — After The Mitigation, and the timeline records the issue as fixed on 8 December 2022, one day after a request for more information and three days after the report on 6 December 2022. A bounty was paid on 9 December 2022. The report does not describe the specific change that was made.

Other places to look

  • Check other Symfony applications for the default secret ThisTokenIsNotSoSecretChangeIt by signing a request to /_fragment and looking for a 404 instead of a 403.
  • Look for the secret in phpinfo output or other exposed debug pages, then sign a _path payload with it.
  • Enumerate subdomains of the same organisation and test whether they share the same secret, as happened here with the test, production and development hosts.
  • Try the ambionics symfony-exploits script first; if it errors, fall back to generating the HMAC by hand as described above.

Timeline

Date Event
6 Dec 2022 Reported
7 Dec 2022 Need more info
8 Dec 2022 Fixed
9 Dec 2022 Bounty rewarded

Glossary

  • API: a defined way for one piece of software to ask another to do something, like a service counter with a fixed list of requests it accepts.
  • Base64: a way of writing binary data using only ordinary letters, digits and a few symbols, so it can be sent inside text such as a URL.
  • Bug bounty programme: a scheme where a company invites outsiders to find security problems in its systems and pays them for valid reports.
  • Controller: in a web framework, the piece of code that handles a particular request. Here the _controller parameter names which function should handle the fragment request.
  • Debug mode: a development setting that makes an application show detailed errors and internal information instead of a plain error page.
  • Directory brute force: asking a server for a long list of common file and folder names to discover which ones exist.
  • Framework: a ready-made set of building blocks that programmers use to assemble an application faster.
  • HMAC: a standard way of combining a message and a secret key into a fingerprint, so that anyone holding the key can verify the message and anyone without it cannot forge one.
  • HTML: the language web pages are written in.
  • Misconfiguration: a setting left in a state that exposes more than it should, such as a default password or a debug page left on.
  • Parameter: a named value attached to the end of a URL after a question mark, used to pass information to the server.
  • PHP: a programming language commonly used to build websites; Symfony is written in it.
  • phpinfo: a standard PHP page that prints the server's configuration, including environment variables and sometimes secrets.
  • Production: the live version of an application that real users reach, as opposed to test or development copies.
  • RCE (remote code execution): the ability to make a server run commands of your choosing from a distance.
  • Secret key: a private string of characters used to sign or encrypt data; anyone who knows it can produce valid signatures.
  • Signature: a short string computed from a message and a secret key, used to prove the message came from someone who knows the key.
  • Status code: the three-digit number a web server sends back with every response, such as 403 for refused or 404 for not found.
  • Subdomain: a smaller site that sits under a company's main domain name, for example shop.example.com under example.com.
  • Symfony: a free, open source PHP framework for building web applications.
  • Symfony Profiler: a debugging tool bundled with Symfony that shows requests, database queries and configuration.
  • URL encoding: replacing characters that are ambiguous inside a URL, such as +, / and =, with % codes.
  • Vulnerability: a weakness in software that someone can use to make it behave in a way its owner did not intend.

More by Omar ElSayed

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.