Welcome to splitting the email atom, exploiting parsers to bypass access controls. Email addresses might seem mundane, but the RFCs governing them are downright bonkers. I'll show you why predicting an email destination is highly complex, and I'll take you on a journey of ancient protocols, exotic encodings, access control bypasses, and how I gained remote code execution from an RFC compliant email address. First, I'll explain why email address parser discrepancies matter. Next, I'll cover the shaky foundations that emails are built on. The core of the talk will focus on parser discrepancies, including Unicode overflows, encoded word, and Unicode, all with real-world case studies. Then I'll share my methodology and tools for automating exploitation, followed by advice on defense. I'll conclude with the key takeaways, leaving five minutes for questions. There's also a bonus material for DEF CON too. So this research all started when we realized many websites use the domain part of the email address to infer the user's organization and apply access control. For example, Slack will automatically give you access to your company's Slack channel based upon your work email address. And Cloudflare Zero Trust can be configured to use the email domain to protect your internal network. This makes email verification a critical security boundary. What could possibly go wrong? Everyone knows that URL parser discrepancies are critical as they can lead to SSRF path traversal and other security issues. Using an email domain for access control makes email address parser discrepancies critical too. Predicting which domain an email should be routed to should be simple, but it's actually ludicrously difficult even for valid RFC compliant addresses. So we've established why email address parser discrepancies matter. Now let's explore why predicting the email destination is so challenging. Email addresses are built on a shaky foundation of RFCs designed many years ago. Validation is often performed using regular expressions, copy and pasted from Stack Overflow, all trying to follow the RFC. However, one mistake in the RegEx and everything can come crashing down. I'm not going to bore you with RFCs, but I will highlight the important ones for this talk. For instance, the characters before the at symbol in an email address are called the local part. The RFC allows what is called the quoted local part. This enables you to use characters in the local part that are not normally allowed by using quotes. In the example shown, I use a quoted local part to use the at symbol as part of the address. The same RFC allows you to use a quoted per, which means one character preceded by a backslash. This enables you to use a double quote and a backslash as part of the address provided it is escaped. Characters enclosed in parentheses are treated as comments and can be placed anywhere within the address except the local part. They are all ignored when the actual email is delivered. So here are two email addresses. You might be wondering which one is valid, as this is a typical question asked in an email presentation. However, it's the wrong question for this talk. The question you should be asking is which domain do they go to? You might expect both emails to go to example.com, but that's not what happens. They both go to psres.net. Remember how I said predicting the email destination is extremely difficult? This is just the tip of the iceberg. The first example works on Postfix and the second example works on Sendmail. If you are confused, you should be. Next, let's explain why this happens. A source route is a sequence of servers which an email is sent before reaching its final destination. Servers in the chain are separated by commas, with the final destination marked with a colon before the recipient's full address. Another form of source routing is called the percent hack. Despite its name, the actual character is determined by the mailer. In this process, the email is initially sent to example.com, after which the percent symbol is converted into an at symbol, and the email is sent to foo at psres.net. This process can be repeated several times. Before email addresses existed, and even the internet, people exchanged messages using UUCP. UUCP stands for Unix to Unix Copy Protocol. It's an early protocol that separates the local user and multiple hosts using an exclamation mark, creating what is known as the bang path. Unlike the standard format for internet email addresses today, the bang path lists the hosts from left to right, with the destination user mailbox on the right, as you can see in the example. So going back to the original examples with all the special characters removed, what the parentheses actually do is remove the at symbol and domain, and without the domain part, Postfix treats that as a percent source root. Using a backslash and escape in the at symbol has the same effect on sendmail, which treats the email as a UUCP address. Pretty damn crazy, right? And I've tested it, and all those special characters do go to the mailbox. I wish I could tell you that I uncovered this behaviour by analysing thousands of lines of code and using a debugger and stepping through the code flow in sendmail and Postfix, but that's not actually how it went down. I noticed an unnamed target was allowing all sorts of special characters, so I just took all the characters, pasted it into my email and expected it to fail. But actually, what happened was, I checked the syslog on the box that I was testing, and I noticed a difference. I was getting host unknown error in the delivery status notification. This was significant because it would suggest a different host was being used. If you look at the error, you'll notice the exclamation mark is missing. I didn't actually notice that at the time. So I started to remove characters, resend the email, and I still got the same error message. I narrowed it down to the exclamation mark and thus discovered this UUCP behavior. This was bonkers. By sheer luck, the characters I pasted into my email address ended with a backslash. I think I did this subconsciously. And then, when the exclamation mark was then, sorry, and then escape the out symbol, and then the exclamation mark was treating the address as a UUCP address. I then wrote a fuzzer to find the other behavior with source routes with Postfix. These findings gave me great confidence that there were a ton of bugs out there, so I began to look for more. And this is a bonus slide for DEF CON. In the RSC, there are optional SMTP parameters. I analyzed the Postfix code with a Poetswinger colleague, and I discovered that these parameters could be used to remove the domain part of the email address. I found this parsing bug on a real target, but the application in question didn't use the email domain for access control. This attack works by escaping the backslash to break out of the quoted local part, confusing the email validation. Then, you can use the greater than to end the receipt to command, and then you stick all the rest of the address within this SMTP optional parameter. And you will notice that this works on a real target and is a valid SMTP conversation. So when it does the receipt to command, it's a valid syntax for that. As a further bonus, here is some surprising email parsing behavior that I uncovered works on Postfix. I couldn't use these for access control bypasses, but nevertheless, they are interesting and challenges your assumptions of how email addresses are parsed. This first one uses a UUCP address and is sent regardless of the quotes. It's pretty damn crazy. This second one uses source roots. So if you use a square bracket syntax, Postfix will still treat it as a source root and send to that email. So one of the main problems I had to solve with this research was generating blocked characters. Since many web applications will block multiple art symbols, so this following section focuses on Unicode overflows, which enable the creation of ASCII characters from higher Unicode characters. It's pretty much normalization, but I'm calling Unicode overflows because it's a different technique. So the PHP char function is a good example of how Unicode overflows can occur. The function generates a character from an integer code point. Here's an illustration of how the algorithm is used by this function. It loops while the code point is less than zero, continually adding 256 until it's non-negative, because why not? It's PHP after all. Then it uses the mod operation to ensure the byte value is always within a range of zero to 255. So if you introduce a Unicode character is larger than 255, it'd be truncated into this range. Note that Unicode overflows are just specific to PHP. So generating a Unicode overflow in JavaScript is quite simple. Using the from code point method, first you pass the number that will cause the overflow, such as 0x100, which is 256 in decimal. Next, you provide the second hex number to specify the actual character that you want, like 0x40, which is the art symbol. The from code point method will generate a Unicode character, and when this character is transformed to fit within 255, a system that doesn't support higher Unicode characters will produce the ASCII character specified by the second number. So I hunted for real-world Unicode overflows, and I found a target that altered characters within the mailer's SMTP conversation. This application blocks the backslash within the quoted local part, but I bypassed the validation using Unicode overflows that enabled me to escape characters and break out of the quoted local part. Despite this, when using an encoded art symbol, the email wasn't sent, suggesting there is a double validation going on, once in the application and once before sending. So although I didn't manage to exploit this particular target, the key takeaway is, you can use a Unicode overflow to bypass validation and actually smuggle characters within the SMTP conversation. So Unicode overflows were great, but I needed more methods to generate blocked characters. So I took a deep dive into the glorious RFC looking for gold, and I found it. This next section is about encoded word. I discovered this when trying to find ways of encoding the art symbol without Unicode overflows. Encoded word is specified in RFC 2047, and it allows the inclusion of Unicode characters using encoded data. And here is how encoded word works. So first we've got the equals and question mark that indicate the start of encoded word. The following is the character encoding, in this case UTF-8. Inside the... so the question marks act as a separator between the charset and the encoding type. Then it's the type of encoding, and then you've got your encoded data as hex. So it's prefixed with equals, so equal 41 is an uppercase A, so that's where the data occurs. And the question mark equals indicate the end of the encoded word. And the actual email that gets sent is that. So all the meta characters are removed, and that email is sent to there. To probe for encoded word, you should make two requests with the two charsets. The reason for this is that many different email parsing libraries support different charsets. When probing, you should use lowercase characters in case the site rejects or transforms to lowercase. So I was initially using x as a charset, but to reduce the size of the probe. However, on some systems, they will reject unknown charsets and it would fail. So it's better to use these two probes and then follow up with a charset of x to see if it works. If successful in each case, you'll send an email to the ones displayed. In a real probe, you would use a collaborator payload or a similar tool. I've highlighted the encoded and decoded text in red. So for example, equals 61 is a lowercase a. So we've covered how encoded word works. So let's dive into some real world case studies. The following case studies all use the Ruby mail library, which has over 508 million downloads. At the time of testing, the version was 2.8.1. This library is used by many popular applications, including GitHub, Zendesk, GitLab, and Bugcrowd. So for my first case study, I'm going to show you how I exploited GitLab Enterprise. I love this vector because it's so elegant. So first I used a charset of x because this was not important on the enterprise product. Then I use an encoded at symbol, so equals 40 is encoded at. And then I used the underscore character. So interestingly, encoded word supports equals 20 as a space, but also underscore. What happens then is the email is split in half and the email is sent to that address. So GitLab thinks the address is example.com, but it actually goes to psres.net. So what was the impact on GitLab Enterprise? Well, I could verify email addresses that I didn't control. This means I could gain access to GitLab Enterprise servers that use domain-based registration restrictions. To understand the next set of slides, let's have a quick recap on how the email gets sent at the SMTP level. So there's a conversation between the mailer and the SMTP client, and there's a handshake that occurs, but that's not important for this part of the talk. The important part is the receipt2 command. You'll notice it's enclosed with angle brackets. We're going to abuse this in the next few slides to change the email destination. GitLab is also an IDP, and I exploited their email verification. So I used x for the slide because it wouldn't all fit on. No one needs to be able to see it, but you'd have to use this tri-set specified in red here. Then I used an encoded at symbol again, but this time I used the greater than symbol, so this is an encoded greater than symbol, and then an encoded space. And then GitLab thinks the domain is example.com, but it actually goes to psres.net. So you can spoof any domain you like and verify any domain you like on GitLab. So, yeah, using this bug, you could bypass domain-based access controls, websites that support GitLab as an IDP. This screenshot shows that I've got a Microsoft verified email address and GitLab email address. It's a bit small, but you can see it's verified. So this one is more complex, and this works on Zendesk and uses the quoted local part with encoded quotes. First, we start the quoted local part. Then we use a Q encoded double quote, so equals 22 is a double quote, followed by an encoded at symbol this time. Next, we have to use the greater than and a null character for Zendesk. So this has the same effect on space. The null character prevents the rest of the address from being used. Due to the quirks of the mailer and the validation that Zendesk used, I had to use an encoded less than character and a double quote. And my theory for this is that Zendesk were doing email parsing multiple times. So adding all this would then decode and then start to remove parts of the email address. And then we finish the quoted local part, and the actual email that gets sent is there. Absolutely crazy. Yeah, pretty nuts. So the impact of this attack was attackers could verify email addresses from domains they don't control, which means they could access Zendesk email domain protected support centers. GitHub were also vulnerable to the greater than and null attack. So first I created an encoded at symbol, then I used an encoded greater than symbol to end the receipt to command. And GitHub required a null character, so adding this null character would then ignore the rest of the address. And that would go to that email again. So using this technique, I was able to verify domains I didn't control on GitHub, and I had verified emails from Mozilla.com, GitHub.com, and Microsoft.com. By spoofing domains on GitHub, I could verify domains I don't control. This means I could bypass domain-based access controls on websites that used GitHub as an identity provider. For example, I could penetrate internal networks protected by Cloudflare Zero Trust. If it was configured to use GitHub as an IDP and email domain validation. So the following screenshot shows Zero Trust trusting domains from GitHub.com. Since any domain can be spoofed and verified, this vulnerability affected Cloudflare Zero Trust instance using email domain validation. So as I was testing multiple Ruby libraries, I discovered that you could use Base64 encoded email addresses. Because why not, right? We all need this, don't we? We need Base64 encoded addresses. In this example, it uses the B as the encoding type, so that indicates Base64. And this is a Base64 foo bar. So I'm using foo bar as an example, but any character can be encoded. And that results into an email of foobar at psres.net. Don't worry, it gets worse. It gets more disgusting. So who remembers this? A script tag encoded in UTF-7. This XSS vector used to work on IE. Anybody remember it? Yeah? Surely? No. Let's see. Surely email parsing libraries won't allow you to use a UTF-7 charset? Of course they do. Using the Ruby library mail, here is a UTF-7 encoded address. So this is a UTF-7 foo bar. You'll notice the ampersand. That's intentional because that's the character that they support for UTF-7. So the charset will be decoded and the email will be sent to foobar at psres.net. I know what you're thinking. Or maybe it's just me. But yes, you can use UTF-7 and Base64 in an email address. In this example, there's a Base64 email address with a UTF-7 charset. First, the email parser will decode Base64. Then the email parser will decode the UTF-7 charset. Finally, the decoded address will be sent there. Absolutely crazy. And I'll emphasize the point that any character can be encoded. I'm just using foobar to make it simple to follow. Then I thought, what about UTF-7 and queue encoding? So here I'm going to modify the A character and replace it with equals 41. This will then get decoded and then it will be decoded and form a UTF-7 encoded foobar. So it's going through two layers of encoding. At this point, you might start having a few doubts about following the RFC. Especially when I tell you that this works in the domain part 2. And when I tested the Ruby mail library, I know what you're thinking. It's... sorry, I've lost track. I know what you're thinking. No website is going to support UTF-7, right? No website is going to support that. But actually, there was a small source code web hosting site called GitHub. They supported UTF-7. I'm so gutted that I didn't exploit them with UTF-7 because they'd already fixed it by the time I found this. And then, yeah, it'll go to that email address. So foobar.psrez.net. So then I went hunting for encoded word outside of Ruby. And I found that phpMailer supports encoded word within the name of the recipient. I tried exploiting this in WordPress and other applications, but the requirements of needing angle brackets around the email made exploitation difficult. Still, I bet there are vulnerable systems that decode the actual name of the recipient. So you may find this in bug bounty sites. So we've explored how you can manipulate email parsing to sidestep access controls. But shall we take it a bit further? What if we could use an email address and weaponize it to gain remote code execution? In this section, we'll cover PuniCode attacks and how I exploited Joomla. So PuniCode is a way to represent Unicode characters in the current DNS system. PuniCode always starts with xn and is followed by two hyphens. The algorithm converts the sequence of Unicode characters into a representation that utilizes only alphanumeric characters and hyphens. For example, the domain muchen, I hope I pronounced that right, is encoded with the following PuniCode sequence, as you can see there. Due to the very nature of how PuniCode works, it makes it difficult to test because changing one character can affect the entire output and the character position due to how the algorithm works. So what we want to do is generate some malicious characters that when it's decoded... Sorry, what we want to do is generate malicious characters when it's decoded. But doing so is a big challenge. In these examples, you can see the Unicode character changes when one byte is modified. This makes it difficult to generate a malicious payload. So after reading about PuniCode on Wikipedia, I followed a link to an online PuniCode converter and I began to experiment. The converter used the IDN PHP library. I first started to add zeros to the beginning of the PuniCode address to convert it into ASCII characters. So I initially succeeded generating a comma, which was so surprising I doubted my eyes. Encouraged by this, I started to experiment further and managed to produce an art symbol. So after searching GitHub, I found an interesting target using the library, Joomla. This was great because if I got XSS, then I would have RCE. Doing a source code analysis, I noticed they were escaping the email before it was PuniCode decoded. This means if I could produce some malformed PuniCode, that when decoded produces HTML, I could get XSS. But it wouldn't be that easy. Sound simple? Just give the decoder some malformed PuniCode and generate some HTML. But remember how the algorithm works. Any change in the character can result in a completely different output. I thought about this for a while and I thought that this was a good job for a fuzzer. So I wrote a fuzzer to generate malformed PuniCode and the results were interesting. I found many ways to generate different characters. So you can see what I was trying to do here. I'm trying to generate an SVG type. So I started generating millions of different character combinations. I managed to construct partial XSS vectors, but I encountered several issues. I could only generate two ASCII characters by using more than one PuniCode subdomain. This limitation arose from the specific workings of the PuniCode algorithm PHP and the quirks of this buggy PHP IDN library. As you can see in the examples, I was really close. But these problems made exploiting Joomla very difficult. I concluded that XSS was not feasible. Because although I was able to generate a single quoted attribute, it required an underscore character. Joomla, however, does not permit underscore characters in the domain part of the email address. So you can see those mad PuniCode addresses are trying to inject some HTML. So was that the end of the story? Not quite. I thought about this for a while. And I worked out that you could use a single PuniCode subdomain and you could generate any opening tag. Eventually, after a lot of testing, I concluded the only exploitable vector was an opening style tag. Here I use malform PuniCode to construct it. It looks like a test string, right? But this is actually how it works. Style 1, 2, 3. And 3, 2, 1, sorry. And that would generate a style tag. The rest of the pre-existing Joomla HTML code would add the space and the closing angle bracket for me. So it injects this style tag and then the rest of the HTML adds those characters for me. So what I'd need is two accounts. The first registers with a style tag with malform PuniCode as demonstrated. The second account has the actual CSS payload inside the name of the user. That imports the malicious style sheet. The first account starts with A and the second account starts with X. This is to ensure the style injection occurs first and then the payload with the import occurs second. Notice I'm using curly braces. This is to treat all the HTML that occurs before the import as an invalid CSS selector. And this still works because this is CSS, right? It'll just work. So the first step is to register two accounts for the attacker. The first with the malicious PuniCode and the second with the evil CSS. So here we're using the malform PuniCode in the email and the second account in the name. The attacker then uses import chaining and a custom node server, a custom CSS exfiltrator to quickly exfiltrate the CSERF token from the admin. And when the admin visits the user's page, this CSS will get executed and the CSERF token is leaked to the attacker. The screenshot shows this process happening. Using the exfiltrated token, the attacker then uses the CSS exfiltrator and gives the admin a URL. The admin visits that site and a CSERF attack is then performed on the admin using the exfiltrated token and then the admin template is then modified and then you get RCE. Now, James said I should do a live demo. I wasn't as keen, but let's give it a shot. Right, so on the left is the victim, so it's the admin on the left. On the right is the attacker. We've also got the exfiltrator running and this is where the token will be extracted. So the attacker visits the page. I've pre-filled the data because you don't want to see me typing everything. And the email address contains a malform punicode. So I get an error and that error is just because I haven't configured mail on the server. And then you register a second account and this is with the actual payload in that exfiltrates the token. So the two accounts are registered. Now let's log in as the admin and pray to the demo gods. Right, thank you very much. But we still need to send the message to the admin, right? Hey admin, how's your day going? Yeah, going good, living the dream. Let's click on this. So I'm showing you a real attack. You wouldn't show the payload in there, but we've backdoored Joomla, and then the attacker goes to pond, and we've got ICE, we're casting etc password. Thank you, thank you. So whilst conducting this research, I found it useful to follow the following methodology. Probe, observe, encode, exploit. First probe for encoded word. Observe that it will be decoded to confirm that it's supported. Then encode the various characters and observe how they're decoded. And then follow with an exploit that abuses these characters. So this methodology is spelt out here. So these are the steps you can take to exploit. And this is some of the steps I use to exploit these targets. Hackverter is a free open source tool that I wrote for the burp suite. I've crafted some hackverter tags to help you generate unicode overflows and encoded word attacks easily within burp. Turbo intruder is another free burp extension written by James Kettle. I've created a turbo intruder script to exploit the mailers. So you remember when github supported null for example, this was found with turbo intruder. I've also created a punicode fuzzer to help with malformed punicode. The fuzzer works by giving some input, some punicode address, and then the placeholders are substituted with random numbers, characters, or white space. It was really effective in finding what characters could be generated. We've also updated burp intruder word list with a list of these attacks too. For your convenience. To defend against email splitting attacks, make sure you filter out or disable encoded word. Always verify email addresses before using them, even when received from an SSO provider. After validating the email address, do not use the domain as a sole means of authorization because it can lead to bad stuff. A few blog posts were really inspirational when conducting this research. I recommend you read each one because they are really useful and contain really useful information. The import chaining technique to use to exfiltrate the token is from Petvilla and Donut, awesome researchers. So the three takeaways that I want to leave you with are valid email addresses that can trigger major parser discrepancies. Even email addresses that end in at example.com might go elsewhere and as a result it's never safe to use email domains for access control enforcement. We've created a web security academy CTF so you can try and do an email splitting attack. I've also created a docker file with the vulnerable Joomla installation for your convenience. You can get all the materials and download all the materials for this presentation on the GitHub repository. Thank you very much DEF CON.