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.
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.
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
- Create a calculated question with one variable such as {a}
- Set the variable's value range to the course ID you want to delete
- Save the question, then edit it again
- Replace the answer formula with an expression that builds the function name and ends with the variable placeholder
- Save again and ignore any syntax error shown
- 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.