... Loading ...

SSD Secure Disclosure

Disclosing vulnerabilities responsibly since 2007
Dark Theme

SquirrelMail – Incoming e-Mails Stored XSS

Abstract
SquirrelMail allows to display HTML messages provided that non-safe fragments are redacted. An input sanitization vulnerability that can be exploited to perform stored cross-site scripting (XSS) attacks has been discovered.

A remote attacker can send a specially crafted e-mail containing malicious HTML and execute arbitrary JavaScript code in the context of the vulnerable webmail interface when the user displays the message. This basically grants the attacker the same privileges of the authenticated victim, in particular this enables to (among other things): send e-mail messages on the behalf of the victim, fetch conversations from folders, delete or otherwise manage messages, log the victim out of SquirrelMail, etc.

It is likely that even prior versions are affected since this does not appear to be a regression but merely an insufficient implementation.

Details
The HTML sanitizer uses a blacklist approach based on tag and attributes names to recognize potentially dangerous HTML code and decide how to fix it, for example, attributes starting with on are removed as they usually represent events. In particular, the <script> element is deleted and the href attribute can only assume certain schemes (e.g., not javascript:) otherwise it is replaced with a void image URL.

It is possible to bypass these checks by using the SVG counterpart of the <a> and <script> elements. This variant exposes the href attribute as part of the xlink namespace (for the latter it allows to specify the resource containing the script code) therefore it can be accessed with xlink:href which is ignored by SquirrelMail. Moreover, in this context <script> can be self-closing and the lack of closing tag is enough to deceive the sanitizer.

Two methods have been devised, to maximize the chances of success it may be advisable to employ both.

Credit
An independent security researcher, Andrea Cardaci, has reported this vulnerability to SSD Secure Disclosure program.

Affected versions
SquirrelMail version 1.4.23 (SM-1_4-STABLE @ r14746)
SquirrelMail version 1.5.2 (trunk @ r14747)

No user action required
This solution only works with Firefox and Edge [1] and requires no additional interaction from of the user:

Arbitrarily complex code can be deployed by using the Base64 format of the Data URL scheme.

User action required
This solution has been tested with all major browsers and requires the user to click on an anchor element:

Arbitrarily complex code can be deployed by evaluating a decoded Base64 string.

Additionally, to mimic the look and feel of a regular link, the following attributes of the text element can be used:

A note about Firefox
The HTML sanitizer adds the target=”_blank” attribute to links, in Firefox this means that even javascript: URLs are evaluated in a new tab. Luckily it is possible to obtain the original frame using the window.opener property and possibly close the new window afterwards.

To increase the chances of success the JavaScript payload should look like this:

Limitations
The HTML visualization of messages in SquirrelMail is not enabled by default, users of the stable version need to enable it globally[2] whereas in the development version it can also be toggled for single messages[3]. Nowadays HTML e-mails are sadly widespread so it is reasonable to assume that most users are willing to properly display them.

Proof of concept
This proof of concept (PoC) shows how it is possible to trick SquirrelMail in sending arbitrary e-mails on the behalf of a SquirrelMail user.

To prevent cross-site request forgery, SquirrelMail employs per-user security tokens, this is not a problem in this scenario since the valid token can be easily obtained from the current page using this JavaScript snippet:

The administrator may decide to enable a per-action token generation instead[4], in this case a token can be obtained with:

The following JavaScript payload takes into account the aforementioned considerations:

The payload[5]
can be Base64-encoded and the HTML message can be crafted as follows:

It is likewise possible to retrieve sensitive data by fetching the proper URL[6], for example:
* /src/right_main.php?showall=1&mailbox=INBOX to obtain the message list;
* /src/read_body.php?mailbox=INBOX&passed_id=<messageid> to obtain the message content.

A possible attack scenario would be to fetch all the message identifiers from the first URL, then use the second to fetch individual messages and finally use the above send function to exfiltrate this data [7]

Bonus scenario
Another interesting attack scenario takes advantage of the fact that browsers prompt to save the login credentials, the attacker could craft a fake and invisible login form to harvest them. This is particularly worrisome if SquirrelMail is used as a system mail interface where credentials are actual system credentials that might grant access to other services, e.g., SSH, FTP, etc.

Countermeasures
Users should refrain from displaying HTML e-mails in SquirrelMail. At the very least, authors should treat the xlink:href attribute the same way as href.

Notes
[1] Tested with Firefox version 57.0.1 and Edge version 41.16299.15.0. Apparently, this is a specification misinterpretation by Chrome and others.
[2] Options -> Display Preferences -> Show HTML Version by Default; this vulnerability can also be used to set this option once triggered the first time.
[3] Because the “View as HTML” plugin has been included in that version.
[4] By setting $do_not_use_single_token to TRUE in config/config_local.php.
[5] The destination account does not need to be on the same server, attacker@localhost is used just for the sake of the example.
[6] These URLs may differ across versions.
[7] Not implemented in this PoC for the sake of brevity.

Print Friendly, PDF & Email

Leave a Reply