Skip to content
RCE Write-up Advanced

Back To School - Exploiting A Remote Code Execution Vulnerability In Moodle

Moodle's calculated-question answer formulas were passed to PHP eval() after a weak sanitiser that only allowed a small set of maths functions and operators. The authors bypassed it by building function names as strings from NAN values and XOR, then used variable substitution to supply the call parentheses, achieving remote code execution.

RedTeam Pentesting Follow

Published on blog.redteam-pentesting.de

Surprisingly often, implementations include functionality where user input is passed to dangerous functions like PHP’s eval() - despite clear warnings. Often, devs are somewhat aware of this danger and attempt to sanitize the input, but this approach is rarely as robust as assumed.

RedTeam Pentesting GmbH, on blog.redteam-pentesting.de

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.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the sanitiser validated the formula text before variable placeholders were substituted, and it allowed enough operators and maths functions to construct arbitrary strings that were then called as variable functions.

Impact

An attacker with a trainer role could call PHP functions with one numeric argument, disclosing information such as phpinfo output or deleting courses, though arbitrary code execution was not demonstrated by this method.

What the attacker needed

A trainer account able to create quizzes; ability to preview the question

The fix

The vulnerability was fixed in Moodle 4.4.2, 4.3.6, 4.2.9 and 4.1.12 by correcting the formula handling.

How it was found

  1. Create a calculated question with one variable such as {a}
  2. Set the variable's value range to the course ID you want to delete
  3. Save the question, then edit it again
  4. Replace the answer formula with an expression that builds the function name and ends with the variable placeholder
  5. Save again and ignore any syntax error shown
  6. Preview the question to trigger the function call

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Moodle lets teachers write maths formulas for quiz questions, and the server runs those formulas as PHP code. The check that was meant to keep the formulas harmless could be tricked, so a teacher could make the server run almost any PHP function. That is dangerous because the server then does whatever the attacker's formula says.

What is RCE?

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

More by RedTeam Pentesting

View all

5 more disclosed reports, best first.

More from Moodle

View all

The newest disclosed reports for this program.

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.