Mercurial can be tricked into granting authorized users access to the Python debugger
A customised hg-ssh wrapper that failed to validate the repository argument allowed an authorised user to pass a debugger flag, causing the Mercurial binary to open a Python debugger shell. This gave the attacker arbitrary Python code execution on the server.
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 3 months
Summary by the program
Mercurial 4.1.3 (2017-4-18)
hg serve --stdiocould be tricked into granting authorized users access to the Python debugger. Thanks to Jonathan Claudius of Mozilla for reporting this issue. This issue is only a security issue for repositories served using --stdio, which includes ssh but not http.
The report
I reported this bug privately to Mercurial and they produced an out of band release to fix the bug here:
https://www.mercurial-scm.org/wiki/WhatsNew#Mercurial_4.1.3_.282017-4-18.29
I produced a very detailed proof of concept with a Metasploit exploit module, which can be seen publicly here:
https://github.com/rapid7/metasploit-framework/pull/8263
The TLDR is that many services which host Mercurial servers often write their own hg-ssh wrapper or heavily customize the hg-ssh wrapper. If the customized wrapped does not explicitly validate user input to the repo attribute, an attacker can supply a string of "--debugger", which causes the internal hg binary to drop to a Pdb shell, which allows arbitrary Python code execution.
I'm submitting to this program because I believe source code management software like git and mercurial is considered critical infrastructure for the Internet.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the customised hg-ssh wrapper did not validate the user-supplied repository argument, so an option-like value was passed through to the hg binary and enabled its debugger.
Impact
An authorised user of a Mercurial server exposed over SSH could run arbitrary Python code on the host; the issue does not affect repositories served over HTTP.
What the attacker needed
Authorised access to a Mercurial server; server uses a customised hg-ssh wrapper; wrapper does not validate the repo argument
The fix
Mercurial 4.1.3 was released out of band to fix the bug.
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 report has not been written yet. The Original tab links to the source.
Mercurial is a version control system, like Git. Servers often wrap its SSH access in a small script that checks which repository a user is allowed to reach. If that script passes the user's repository name straight to Mercurial without checking it, the user can sneak in an extra option that starts a debugging tool, and from there run their own code on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.