SMTP Injection in Gsuite
G Suite's email routing settings allowed newline characters in the custom subject field, enabling SMTP header injection. An attacker could spoof emails from arbitrary @google.com addresses.
Published on ehpus.com
Gsuite is an immensely powerful tool for account administration. It allows the administrator to control just about anything regarding the user accounts in his organization, from determining how one can login and which apps he can access, to which contacts are allowed and what Email headers should be applied.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
G Suite (now Google Workspace) is Google's account administration platform. An administrator can control almost everything about the user accounts in their organisation: how users log in, which apps they can access, which contacts are allowed, and which email headers should be applied to mail.
The last of these is what drew my attention. Modern email headers are effectively a hack layered on top of SMTP, a very old protocol that is still used by practically everyone connected to the internet. That layering leaves plenty of room for mistakes when software tries to manipulate headers. The short version: Google is not immune to errors, and G Suite's email configuration was vulnerable in a way that let an attacker spoof email messages from Google's servers.
SMTP in brief
SMTP is a simple, text based protocol that does not enforce authentication. If you can open a socket to an SMTP server, you can instruct it to send an email to any address, and, more importantly, from any address. That is why you cannot natively trust the origin of an email you receive.
There are very few native SMTP instructions:
- MAIL FROM: who sends the email. Natively this can be anything, including
[redacted]. - RCPT TO: who the email is sent to.
- DATA: the contents of the email.
That is it. There is no cc, no bcc and no subject in the protocol itself. Those headers came later and were hacked into the system: you place extra headers inside the DATA content, at the beginning, as long as both sender and receiver know how to parse them. The convention, also added to the RFC, is that each header goes on a new line and the header name and value are separated by :.
A quick example:
SMTP FROM:
[redacted]
SMTP TO:
[redacted]
DATA:
bcc: [redacted]
Send me all your money!!
.
Why identity checks matter
Email would not be very useful if these issues went unaddressed, so over time various mechanisms were developed on top of SMTP to give more assurance of identity and content. The main tool used today to verify that the sender is who they claim to be is DNS domain validation. If I own google.com, I can set a DNS record that instructs all SMTP servers in the world to accept google.com email only from me, with everything else going to spam. If you can spoof email from Google's own servers, no other mechanism can verify the sender's identity.
Setup
The feature lives in the G Suite admin console. After logging in to admin.google.com, the path is Apps -> G Suite -> Settings for Gmail -> Advanced settings -> Routing. There you can add a routing setting for inbound or outbound traffic. One of the optional settings is configuring a custom header to be added to all emails.
The screenshot shows the routing setting with the custom header option, where the header name is constrained.
Finding it
Following the concepts above, custom headers are presumably added to the SMTP DATA content, so if I could add any header I wanted, I could manipulate the email content. As the image shows, that is not the case: custom headers in G Suite have a leading X-, so the header name is not fully under my control.
But by convention every header is written on a new line. What if I could inject a newline as part of the header name? The next line would then already be a new header, under my control. That failed: Google made sure you cannot include newline characters in the header name.
Then I noticed something else. Just beneath the custom header option there is an option to prepend a custom subject to each mail. And there is no subject in SMTP: it is just another header in the DATA section.
Exploitation
Step 1: inject newlines into the subject
I launched my proxy and added newline characters (\r\n) into the subject setting.
The screenshot shows the subject setting with the newline characters inserted in the proxy request.
No errors were returned. I sent a test email to the account, opened Gmail, and saw this:
The newline characters were rendered server side and the subject header was split into several lines. Since each header is represented on a new line, the rest of the payload, everything after the newline characters, was pushed to the next header, which in this case was the email body. That is a successful SMTP injection.
Step 2: spoof the From header
I changed the subject setting again, this time to include a spoofed From header.
That worked too: Gmail presented the email as if it actually came from [redacted]. I tweaked the payload a bit and the final result can be seen here:
I was now able to spoof emails from arbitrary @google.com addresses.
Root cause
The custom subject value ended up in the SMTP DATA section, and newline characters in it were not rejected. Because headers are delimited by newlines, an injected \r\n ends the subject header and starts a new one, so the attacker controls arbitrary headers, including From. Newlines were blocked in the custom header name, but the custom subject setting accepted them.
Impact
Configuring a routing setting with a custom subject containing injected headers caused mail sent through Google's servers to carry a spoofed From address such as [redacted]. Because the mail originates from Google's own servers, DNS domain validation cannot distinguish it from genuine Google mail. The report demonstrates spoofing from arbitrary @google.com addresses; it does not describe other recipients or conditions.
Fix
Google accepted the issue and issued a reward of 3133.7 USD. The report does not describe the code change that was made.
Timeline
- 01/05/2020: issue reported to Google with the initial payload
- 01/13/2020: bug accepted by Google
- 01/15/2020: further details sent, including a working POC for
[redacted] - 02/11/2020: reward (3133.7 USD) issued
Variants to test
- Other header fields in mail configuration UIs: try
\r\nin any field that ends up as an SMTP header (subject, display name, reply-to, custom headers). A positive result is a delivered message whose headers are split across lines or that shows an injected header in the raw source. - Header name fields with different validation: test whether newline filtering is applied consistently to names and values. A positive result is a header name that is accepted with an embedded newline.
- Encoded newline forms: try
%0d%0a, bare\n, and\ralone where the UI or API accepts them. A positive result is the same header split in the received message. - API endpoints behind the UI: submit the same values directly to the configuration API, bypassing client side validation. A positive result is a stored setting that produces injected headers on subsequent mail.
What happened, in plain words
Google runs a service called G Suite (today it is called Google Workspace). It is the tool that an organisation's IT administrator uses to manage everyone's email accounts and apps. Among many other things, the administrator can tell Google to add extra information to every email that leaves or enters the organisation. One of those options lets the administrator put a custom subject line on every message.
I found that this subject setting did not check its input properly. By putting special invisible line-break characters into the subject, I could make the email system treat the text after the line break as a brand new email header. That let me add a fake From header, so a message sent through Google's own servers appeared to come from [redacted]. Google accepted the bug and paid a reward of 3133.7 USD.
This write-up walks through the whole thing from zero. You only need to be able to use a browser. Every technical word is explained the first time it appears, and there is a glossary at the end.
Background: what G Suite is and what an administrator can do
G Suite is Google's account administration platform. Think of it as the control panel for a company's Google accounts. An administrator can control almost everything about the user accounts in their organisation:
- how users log in,
- which apps they can access,
- which contacts are allowed,
- and which email headers should be applied to mail.
The last item is what drew my attention. To understand why, you need to know a little about how email actually works underneath the friendly Gmail screen.
SMTP in brief
SMTP stands for Simple Mail Transfer Protocol. It is the language that mail servers use to talk to each other when one server hands an email to another. It is a very old protocol, and it is still used by practically everyone connected to the internet.
Two properties of SMTP matter here:
- It is text based. The two servers literally exchange lines of readable text.
- It does not enforce authentication. The sending server does not have to prove who it is.
A socket is just an open network connection between two programs. If you can open a socket to an SMTP server, you can instruct it to send an email to any address, and, more importantly, from any address. That is why you cannot natively trust the origin of an email you receive. The server will happily believe whatever sender address you type.
There are very few native SMTP instructions. Here they are:
- MAIL FROM: who sends the email. Natively this can be anything, including
[redacted]. - RCPT TO: who the email is sent to.
- DATA: the contents of the email.
That is it. Notice what is missing: there is no cc, no bcc and no subject in the protocol itself. Those things came later. They were effectively hacked into the system: you place extra headers inside the DATA content, at the beginning, as long as both the sending and receiving side know how to parse them.
A header is a labelled line of metadata at the top of an email, like Subject: Hello or From: [redacted]. The convention, which was also written into the RFC (the document that defines internet standards), is that each header goes on a new line, and the header name and value are separated by a colon :.
A quick example of what that looks like on the wire:
SMTP FROM:
[redacted]
SMTP TO:
[redacted]
DATA:
bcc: [redacted]
Send me all your money!!
.
Line by line:
SMTP FROM:followed by[redacted]is the sender address the server is told to use. Nothing checks that it is true.SMTP TO:followed by[redacted]is the recipient.DATA:announces that everything after it is the message itself.bcc: [redacted]is an extra header placed at the start of the message body. Because it is on its own line and uses thename: valueconvention, the receiving mail software reads it as a real header, in this case a blind carbon copy to the attacker.- The blank line separates the headers from the actual message text.
Send me all your money!!is the body.- A line containing only
.tells the server the message is finished.
The important lesson: anything that ends up inside DATA and contains a line break can start a new header. That is the seed of the whole bug.
Why identity checks matter
Email would not be very useful if anyone could pretend to be anyone, so over time various mechanisms were developed on top of SMTP to give more assurance of identity and content. The main tool used today to verify that the sender is who they claim to be is DNS domain validation.
DNS is the internet's address book: it turns names like google.com into the servers that handle them. A DNS record is an entry in that address book. If I own google.com, I can set a DNS record that instructs all SMTP servers in the world to accept google.com email only from me, with everything else going to spam.
That is the key point for this story. If you can spoof email from Google's own servers, no other mechanism can verify the sender's identity, because the mail really is coming from Google's servers. The address book check passes, and the message looks genuine.
Setup: where the setting lives
The feature lives in the G Suite admin console. After logging in to admin.google.com, the path is:
Apps -> G Suite -> Settings for Gmail -> Advanced settings -> Routing
There you can add a routing setting for inbound or outbound traffic. A routing setting is a rule that tells Google how to handle mail on its way in or out. One of the optional settings is configuring a custom header to be added to all emails.
The screenshot shows the routing setting with the custom header option, where the header name is constrained.
Finding it: the first attempt and why it failed
Following the concepts above, custom headers are presumably added to the SMTP DATA content. So if I could add any header I wanted, I could manipulate the email content. As the image shows, that is not the case: custom headers in G Suite have a leading X-, so the header name is not fully under my control. Custom headers in G Suite have a leading X-.
But by convention every header is written on a new line. What if I could inject a newline as part of the header name? The next line would then already be a new header, under my control. That failed: Google made sure you cannot include newline characters in the header name.
Then I noticed something else. Just beneath the custom header option there is an option to prepend a custom subject to each mail. Prepend means to add at the front. And there is no subject in SMTP: it is just another header in the DATA section. So the subject field is another place where my text ends up as a header, and maybe it was not protected the same way.
Exploitation
Step 1: inject newlines into the subject
I launched my proxy and added newline characters (\r\n) into the subject setting.
A proxy is a tool that sits between your browser and the website, letting you see and edit every request before it is sent. \r\n is the standard way to write a line break in network protocols: \r is carriage return and \n is line feed. Together they mean "end this line and start a new one".
The screenshot shows the subject setting with the newline characters inserted in the proxy request.
No errors were returned. I sent a test email to the account, opened Gmail, and saw this:
The newline characters were rendered server side and the subject header was split into several lines. Since each header is represented on a new line, the rest of the payload, everything after the newline characters, was pushed to the next header, which in this case was the email body. That is a successful SMTP injection: untrusted text placed into a protocol message breaks out of its intended field and becomes protocol instructions.
Step 2: spoof the From header
I changed the subject setting again, this time to include a spoofed From header.
That worked too: Gmail presented the email as if it actually came from [redacted]. I tweaked the payload a bit and the final result can be seen here:
I was now able to spoof emails from arbitrary @google.com addresses.
Root cause
The custom subject value ended up in the SMTP DATA section, and newline characters in it were not rejected. Because headers are delimited by newlines, an injected \r\n ends the subject header and starts a new one, so the attacker controls arbitrary headers, including From. Newlines were blocked in the custom header name, but the custom subject setting accepted them.
Impact
Configuring a routing setting with a custom subject containing injected headers caused mail sent through Google's servers to carry a spoofed From address such as [redacted]. Because the mail originates from Google's own servers, DNS domain validation cannot distinguish it from genuine Google mail. The report demonstrates spoofing from arbitrary @google.com addresses; it does not describe other recipients or conditions.
Fix
Google accepted the issue and issued a reward of 3133.7 USD. The report does not describe the code change that was made.
Timeline
- 01/05/2020: issue reported to Google with the initial payload
- 01/13/2020: bug accepted by Google
- 01/15/2020: further details sent, including a working POC for
[redacted] - 02/11/2020: reward (3133.7 USD) issued
A POC, or proof of concept, is a small demonstration that proves a bug is real.
Variants to test
If you want to look for similar bugs yourself, these are the directions worth trying. Each one is a variation on the same idea: get a line break into something that becomes an email header.
- Other header fields in mail configuration UIs: try
\r\nin any field that ends up as an SMTP header (subject, display name, reply-to, custom headers). A positive result is a delivered message whose headers are split across lines or that shows an injected header in the raw source. The raw source is the full text of the email including all headers, which most mail clients can show you. - Header name fields with different validation: test whether newline filtering is applied consistently to names and values. A positive result is a header name that is accepted with an embedded newline.
- Encoded newline forms: try
%0d%0a, bare\n, and\ralone where the UI or API accepts them.%0d%0ais the URL-encoded form of\r\n. A positive result is the same header split in the received message. - API endpoints behind the UI: submit the same values directly to the configuration API, bypassing client side validation. An API is the programming interface a website uses behind the scenes; the browser page is just a front end that talks to it. Client side validation is checking done in your browser, which you can skip by talking to the API directly. A positive result is a stored setting that produces injected headers on subsequent mail.
Glossary
- API: the programming interface a website or app uses behind the scenes to exchange data with its servers.
- Client side validation: input checks performed in your own browser, which can be bypassed by sending requests directly.
- DNS: the internet's address book, mapping names like
google.comto the servers that handle them. - DNS domain validation: using DNS records to declare which servers are allowed to send mail for a domain, so receivers can reject forgeries.
- DATA: the SMTP instruction that introduces the contents of an email.
- G Suite: Google's account administration platform for organisations, now called Google Workspace.
- Header: a labelled line of metadata at the top of an email, written as
name: value. - MAIL FROM: the SMTP instruction that states who sends the email.
- Newline (
\r\n): the carriage return plus line feed characters that mark the end of a line in network protocols. - POC: proof of concept, a small demonstration that proves a bug is real.
- Prepend: to add something at the front.
- Proxy: a tool that sits between your browser and a website so you can inspect and edit requests.
- Raw source: the full text of an email, including all headers, as opposed to the formatted view.
- RCPT TO: the SMTP instruction that states who the email is sent to.
- RFC: a document that defines an internet standard.
- Routing setting: a rule in G Suite that tells Google how to handle mail on its way in or out.
- SMTP: Simple Mail Transfer Protocol, the text based language mail servers use to exchange email.
- SMTP injection: placing untrusted text into an SMTP message so that it breaks out of its field and becomes protocol instructions, such as extra headers.
- Socket: an open network connection between two programs.
- Spoof: to make a message appear to come from someone other than the real sender.
- X- prefix: a naming convention for email headers that are not part of the official standard.