SSD Advisory – Horde Groupware Webmail Multiple Remote Code Execution Vulnerabilities

Credit to Author: Maor Schwartz| Date: Sun, 09 Apr 2017 10:28:46 +0000

Want to get paid for a vulnerability similar to this one?
Contact us at: sxsxdx@xbxexyxoxnxdxsxexcxuxrxixtxy.xcom

Vulnerabilities Summary
The following advisory describes two (2) vulnerabilities found in Horde Groupware Webmail.

Horde Groupware Webmail Edition is a free, enterprise ready, browser based communication suite. Users can read, send and organize email messages and manage and share calendars, contacts, tasks, notes, files, and bookmarks with the standards compliant components from the Horde Project. Horde Groupware Webmail Edition bundles the separately available applications IMP, Ingo, Kronolith, Turba, Nag, Mnemo, Gollem, and Trean.

It can be extended with any of the released Horde applications or the applications that are still in development, like a bookmark manager or a file manager.

Affected versions: Horde 5, 4 and 3

The vulnerabilities found in Horde Groupware Webmail are:

  • Authentication Remote Code Execution
  • Unauthentication Remote Code Execution

Credit
An independent security researcher has reported this vulnerability to Beyond Security’s SecuriTeam Secure Disclosure program.

Vendor response
Horde has released a patch to address the vulnerabilities.

For more information: https://lists.horde.org/archives/horde/Week-of-Mon-20170403/056767.html

Vulnerabilities Details

Authentication Remote Code Execution
Horde Webmail contains a vulnerability that allows a remote attacker to execute arbitrary code with the privileges of the user who runs the web server.

For successful attack GnuPG feature should be enabled on the target server (path to gpg binary should be defined in $conf[gnupg][path] setting).

Vulnerable code: encryptMessage() function of GPG feature.

Path: /Horde/Crypt/Pgp/Backend/Binary.php:

$params[‘recips’] will be added to $cmdline array and passed to _callGpg():

Path: /Horde/Crypt/Pgp/Backend/Binary.php:

We can see that our recipients (addresses) will be in command line that is going to be executed. encryptMessage() function can be reached by various API, requests. For example it will be called when user try to send encrypted message.

Our request for encryption and sending our message will be processed by buildAndSendMessage() method:
Path: /imp/lib/Compose.php

In line 741 it tries to create recipient list: Horde parsers values of ‘to’, ‘cc’, ‘bcc’ headers and creates list of Rfc822 addresses. In general there are restrictions for characters in addresses but if we will use the next format:

somemailbox will be parsed by _rfc822ParseQuotedString() method:

Path: /Horde/Mail/Rfc822.php:

There are only a few limitations:

  • we cannot use “
  • n will be deleted
  • we cannot use at the end of our mailbox

After creation of recipient list buildAndSendMessage() will call _createMimeMessage():

Path: /imp/lib/Compose.php

Here we can see validation (1695-1696 lines) that:

  • Current user has enabled “use_pgp” feature in his preferences (it is not a problem as an attacker can edit his own preferences)
  • $conf[‘gnupg’][‘path’] is not empty. This value can be edited only by admin. So if we don’t have value here our server is not vulnerable. But if admin wants to allow users to use GPG feature he/she needs to define value for this config.

Also we can see that in lines 1737-1739 to our recipient list will be added address “from” as well.

Path: /imp/lib/Crypt/Pgp.php

Before encryptMimePart() call Horde uses _encryptParameters()

Path: /imp/lib/Crypt/Pgp.php

Horde will add to each address its Public Key. There a few source of Public Keys:

  • AddressBook (we will use this source)
  • Servers with Public Keys

Note that Horde should be able to find Public Key for our “From” address as well.
We can generate pair of PGP keys (https is required) or we can use the same trick with AddressBook (we can create some contact, add any valid Public PGP key, and add this address to default identity) encryptMimePart() will call encrypt() method

Path: /Horde/Crypt/Pgp.php

It will call encryptMessage()

Path: /Horde/Crypt/Pgp.php

In conclusions:
If Horde server has enabled “GnuPG feature” any unprivileged user is able to execute arbitrary code.

  1. Enable GPG feature for attacker account (“Enable PGP functionality?” checkbox on “PGP Configure PGP encryption support.” section in Prefferences->Mail page )
  2. Create some contact in the attacker AddressBook, add any valid Public PGP key, and add this address to default identity
  3. Create another contact in the attacker AddressBook, add any valid Public PGP key, and change email address to some$(desired command to execute) contact@somedomain.com
  4. Create a new message to some$(desired command to execute) contact@somedomain.com
  5. Choose Encryption:PGP Encrypt Message option
  6. Click Send button

And desired command will be executed on the Horde server.

Proof of Concept – Authenticated Code Execution

For Proof of Concept we can use preconfigured image of Horde server from Bitnami (Bitnami – “Easy to use cloud images, containers, and VMs that work on any platform”):

https://downloads.bitnami.com/files/stacks/horde/5.2.17-0/bitnami-horde-5.2.17-0-linux-ubuntu-14.04-x86_64.ova

Step 1 – Login as admin (by default user:bitnami) and go to Administration -> Configuration and choose Horde (horde). Open GnuPG tab, enter /usr/bin/gpg into $conf[gnupg][path] setting and click “Generate Horde Configuration“:

Now we have enabled GPG feature on our server and we can login as regular user and try to execute desired commands. But Bitnami image does not have installed and configured Mail server so we need to use external one or install it on local machine.

We will use gmail account (to be able to login to it from Horde I had to change Gmail account setting Allow less secure apps: ON).

To use external Mail server we need to change the next setting: “Administrator Panel” -> “Configuration” -> “Horde” -> “Authentication”

Step 2 – Configure Horde web-mail authentication ($conf[auth][driver]) to “Let a Horde application handle authentication” and click “Generate Horde Configuration”:

Step 3 – logout and login with your gmail account. Currently we are login as regular user so we can try to execute desired commands:

    1. Go to Preferences -> Mail and click on PGP link. Check Enable PGP functionality? checkbox and click “Save”:

      1. Create “from” contact in our AddressBook: “Address Book -> New Contact -> in Address Book of …
        • Personal tab – Last Name: mymailboxwithPGPkey
        • Communication tab – Email: mymailboxwihPGP@any.com
        • Other tab – PGP Public Key: any valid Public PGP key.

      For example:

          • Click “Add” button:

          1. Go to Preferences -> Global Preferences and click on Personal Information link. Put mymailboxwihPGP@any.com into field The default e-mail address to use with this identity and Click “Save”:

            1. Create our “to” contact in our AddressBook: “Address Book -> New Contact -> in Address Book of …
              • Personal tab – Last Name: contact_for_attack
              • Communication tab – Email: hereinj@any.com
              • Other tab – PGP Public Key: any valid Public PGP key (it can be the same as in the previous step)
              • And click “Add” button:

              1. Inject our command: Click on Edit. Go to Communication Tab, put cursor in Email field and chose “Inspect Element (Q)” from context menu:

                  • Delete “email” from the type argument and close Inspector:

                    • Edit the address as we want – for example hereinj$(touch /tmp/hereisvuln)@any.com and click “Save”:

                    1. Create a new message ( Mail -> New Message) with our contact as recipient:

                        • Choose PGP Encrypt Message in Encryption option:

                          • Enter any subject and any content. Click “Send”

                            • We will get “PGP Error:…”

                              • It is ok – let’s check our server:

                              • We have a new file “hereisvuln” so our command was executed.

                            Unauthentication Remote Code Execution
                            Horde Webmail contains a vulnerability that allows a remote attacker to execute arbitrary code with the privileges of the user who runs the web server.

                            Vulnerable code: decryptSignature() function of GPG feature.

                            Path: /Horde/Crypt/Pgp/Backend/Binary.php:

                            $params[‘charset’] will be added to $cmdline array and passed to _callGpg():

                            Our $params[‘charset’] will be in command line that is going to be executed.

                            decryptSignature() is called from decrypt() method:

                            Path – /Horde/Crypt/Pgp.php:

                            decrypt() with needed parameters is used in verifySignature():

                            Path – /imp/lib/Crypt/Pgp.php

                            verifySignature() is called from _outputPGPSigned():

                            Path – /imp/lib/Mime/Viewer/Pgp.php

                            And it is used in _renderInline():

                            Path – /imp/lib/Mime/Viewer/Pgp.php

                            Let’s go back to _outputPGPSigned() method. We can see a few requirements before the needed call:

                            • $conf[‘gnupg’][‘path’] should be not empty. This value can be edited only by admin(if he/she wants to allow users to use GPG feature he/she needs to define value for this config).
                            • Current user has enabled “use_pgp” feature in his preferences
                            • Current user has enabled “pgp_verify” feature in his preferences
                            • Current user has enabled “pgp_verify” feature in his preferences

                            Also we see that our charset value is taken from $sig_part -> getMetadata(Horde_Crypt_Pgp_Parse::SIG_CHARSET)

                            Our value will be stored during parsing of PGP parts:

                            Path – /Horde/Crypt/Pgp/Parse.php

                            It is called from _parsePGP():

                            Path – /imp/lib/Mime/Viewer/Plain.php

                            Our charset value is taken from CHARSET attribute of Content-Type header of parent MIMEpart.

                            _parsePGP() is used in _getEmbeddedMimeParts() method and from Horde Webmail ver 5.2.0 it looks like:

                            Path – /imp/lib/Mime/Viewer/Plain.php

                            We can see an additional requirement – our function will be called only if ‘pgp_inline‘ config parameter is “true”. It is defined in:

                            Path – /imp/config/mime_drivers.php

                            Default value is false, so the major part of Horde servers is not vulnerable and our attack is relevant only if an admin manually has changed this line to ‘pgp_inline‘ => true.

                            But in older versions (before 5.2.0) the code of _getEmbeddedMimeParts() is a bit different:

                            Path – /imp/lib/Mime/Viewer/Plain.php

                            So instead of requirement to have config parameter we have requirement of ‘pgp_scan_body‘ Preference of current user. And it is more likely to find a victim with needed preferences. We saw where our injected command is executed and from where and when it is taken

                            During rendering of massage we:

                            1. Will parse PGP values:
                              • #0 IMP_Mime_Viewer_Plain->_parsePGP() called at [/imp/lib/Mime/Viewer/Plain.php:225]
                              • #1 IMP_Mime_Viewer_Plain->_getEmbeddedMimeParts() called at [/Horde/Mime/Viewer/Base.php:298]
                              • #2 Horde_Mime_Viewer_Base->getEmbeddedMimeParts() called at [/imp/lib/Contents.php:1114]
                              • #3 IMP_Contents->_buildMessage() called at [/imp/lib/Contents.php:1186]
                              • #4 IMP_Contents->getContentTypeMap() called at [/imp/lib/Contents.php:1423]
                              • #5 IMP_Contents->getInlineOutput() called at [/imp/lib/Ajax/Application/ShowMessage.php:296]
                            2. Will use them in:
                              • #0 IMP_Mime_Viewer_Plain->_parsePGP() called at [/imp/lib/Mime/Viewer/Plain.php:225]
                              • #0 IMP_Mime_Viewer_Pgp->_renderInline() called at [/Horde/Mime/Viewer/Base.php:156]
                              • #1 Horde_Mime_Viewer_Base->render() called at [/Horde/Mime/Viewer/Base.php:207]
                              • #2 Horde_Mime_Viewer_Base->_renderInline() called at [/Horde/Mime/Viewer/Base.php:156]
                              • #3 Horde_Mime_Viewer_Base->render() called at [/imp/lib/Contents.php:654]
                              • #4 IMP_Contents->renderMIMEPart() called at [/imp/lib/Contents.php:1462]
                              • #5 IMP_Contents->getInlineOutput() called at [/imp/lib/Ajax/Application/ShowMessage.php:296]]

                            In conclusions:

                            If Horde server has vulnerable configuration:

                            1. Enabled “GnuPG feature” (there is path to gpg binary in $conf[gnupg][path] setting)
                            2. Only for ver 5.2.0 and newer: ‘pgp_inline’ => true, in /imp/config/mime_drivers.php

                            And the victim has checked the next checkbox in his/her preferences ( “PGP Configure PGP encryption support.” in Prefferences->Mail) :

                            1. “Enable PGP functionality”
                            2. “Should PGP signed messages be automatically verified when viewed?” if it is not checked our command will be executed when the victim clicks on the link “Click HERE to verify the message.”
                            3. For versions before 5.2.0: “Should the body of plaintext message be scanned for PGP data”

                            An attacker can create email with PGP data, put desired command into CHARSET attribute of ContentType header, and this command will be executed on Horde server when the victim opens this email.

                            Proof of Concept – Remote Code Execution

                            For Proof of Concept we can use preconfigured image of Horde server from Bitnami (Bitnami – “Easy to use cloud images, containers, and VMs that work on any platform”):

                            https://downloads.bitnami.com/files/stacks/horde/5.2.17-0/bitnami-horde-5.2.17-0-linux-ubuntu-14.04-x86_64.ova

                            Step 1 – Login as admin (by default user:bitnami) and go to Administration -> Configuration and choose Horde (horde). Open GnuPG tab, enter /usr/bin/gpg into $conf[gnupg][path] setting and click “Generate Horde Configuration“:

                            Now we have enabled GPG feature on our server and we can login as regular user and try to execute desired commands. But Bitnami image does not have installed and configured Mail server so we need to use external one or install it on local machine.

                            We will use gmail account (to be able to login to it from Horde I had to change Gmail account setting Allow less secure apps: ON).

                            To use external Mail server we need to change the next setting: “Administrator Panel” -> “Configuration” -> “Horde” -> “Authentication

                            Configure the application authentication ($conf[auth][driver]) – change this option to “Let a Horde application handle authentication” and click “Generate Horde Configuration”.

                            If we have Horde Webmail ver 5.2.0 or newer we need to edit /imp/config/mime_drivers.php file. Login to the console of bitnami image (default bitnami:bitnami) and run the next command:

                            Change the line: “‘pgp_inline’ => false” to “‘pgp_inline’ => true” and save the changes.

                            Step 2 – Logout and login with your gmail account.

                            Step 3 – Go to Preferences -> Mail and click on PGP link:

                            1. Check Enable PGP functionality checkbox and click “Save”
                            2. Check Should PGP signed messages be automatically verified when viewed checkbox
                            3. For versions before 5.2.0 check “Should the body of plain-text message be scanned for PGP data” checkbox Click “Save

                            For version before 5.2.0:

                            Step 4 – Go to the Mail, take any mail folder (for example Drafts), and chose “Import” item from context menu and import attack_whoami.eml file (in the end of this blog).

                            Click on the imported email:

                            Our Horde serve is launched under daemon user

                            Step 5 – We can do the same with attack_touch.eml (in the end of this blog) file (import it and click on the new mail) and check /tmp folder:

                            attack_touch.eml

                            attack_whoami.eml

                             

                            https://blogs.securiteam.com/index.php/feed