[00:00.000 --> 00:06.200] Welcome to splitting the email atom, exploiting parsers to bypass access controls. [00:06.560 --> 00:13.280] Email addresses might seem mundane, but the RFCs governing them are downright bonkers. [00:13.280 --> 00:18.740] I'll show you why predicting an email destination is highly complex, [00:18.740 --> 00:25.300] and I'll take you on a journey of ancient protocols, exotic encodings, access control bypasses, [00:25.300 --> 00:31.360] and how I gained remote code execution from an RFC compliant email address. [00:33.680 --> 00:38.280] First, I'll explain why email address parser discrepancies matter. [00:38.800 --> 00:43.000] Next, I'll cover the shaky foundations that emails are built on. [00:43.300 --> 00:49.340] The core of the talk will focus on parser discrepancies, including Unicode overflows, [00:49.340 --> 00:54.440] encoded word, and Unicode, all with real-world case studies. [00:54.620 --> 00:59.540] Then I'll share my methodology and tools for automating exploitation, [00:59.900 --> 01:04.520] followed by advice on defense. I'll conclude with the key takeaways, [01:04.520 --> 01:08.920] leaving five minutes for questions. There's also a bonus material for DEF CON too. [01:10.740 --> 01:17.040] So this research all started when we realized many websites use the domain part of the email address [01:17.040 --> 01:24.360] to infer the user's organization and apply access control. For example, Slack will automatically [01:24.360 --> 01:30.180] give you access to your company's Slack channel based upon your work email address. [01:30.200 --> 01:37.680] And Cloudflare Zero Trust can be configured to use the email domain to protect your internal network. [01:37.780 --> 01:44.620] This makes email verification a critical security boundary. What could possibly go wrong? [01:47.040 --> 01:53.620] Everyone knows that URL parser discrepancies are critical as they can lead to SSRF path [01:53.620 --> 02:00.380] traversal and other security issues. Using an email domain for access control [02:00.380 --> 02:04.660] makes email address parser discrepancies critical too. [02:05.060 --> 02:09.240] Predicting which domain an email should be routed to should be simple, [02:09.240 --> 02:15.120] but it's actually ludicrously difficult even for valid RFC compliant addresses. [02:17.160 --> 02:21.660] So we've established why email address parser discrepancies matter. [02:21.660 --> 02:26.120] Now let's explore why predicting the email destination is so challenging. [02:26.560 --> 02:32.820] Email addresses are built on a shaky foundation of RFCs designed many years ago. [02:32.840 --> 02:38.860] Validation is often performed using regular expressions, copy and pasted from Stack Overflow, [02:38.860 --> 02:47.020] all trying to follow the RFC. However, one mistake in the RegEx and everything can come crashing down. [02:49.220 --> 02:54.720] I'm not going to bore you with RFCs, but I will highlight the important ones for this talk. [02:54.720 --> 03:00.620] For instance, the characters before the at symbol in an email address are called the local part. [03:00.760 --> 03:08.120] The RFC allows what is called the quoted local part. This enables you to use characters in the [03:08.120 --> 03:14.940] local part that are not normally allowed by using quotes. In the example shown, I use a quoted local [03:14.940 --> 03:22.280] part to use the at symbol as part of the address. The same RFC allows you to use a quoted per, [03:22.280 --> 03:28.040] which means one character preceded by a backslash. This enables you to use a double [03:28.040 --> 03:34.660] quote and a backslash as part of the address provided it is escaped. Characters enclosed [03:34.660 --> 03:40.380] in parentheses are treated as comments and can be placed anywhere within the address except the [03:40.380 --> 03:46.100] local part. They are all ignored when the actual email is delivered. [03:48.280 --> 03:55.700] So here are two email addresses. You might be wondering which one is valid, as this is a [03:55.700 --> 04:04.640] typical question asked in an email presentation. However, it's the wrong question for this talk. [04:04.640 --> 04:08.700] The question you should be asking is which domain do they go to? [04:08.700 --> 04:15.820] You might expect both emails to go to example.com, but that's not what happens. They both go to [04:15.820 --> 04:23.080] psres.net. Remember how I said predicting the email destination is extremely difficult? [04:23.080 --> 04:29.500] This is just the tip of the iceberg. The first example works on Postfix and the second example [04:29.500 --> 04:36.700] works on Sendmail. If you are confused, you should be. Next, let's explain why this happens. [04:38.800 --> 04:44.800] A source route is a sequence of servers which an email is sent before reaching its final [04:44.800 --> 04:50.920] destination. Servers in the chain are separated by commas, with the final destination marked [04:50.920 --> 04:58.940] with a colon before the recipient's full address. Another form of source routing is called the [04:58.940 --> 05:04.980] percent hack. Despite its name, the actual character is determined by the mailer. In this [05:04.980 --> 05:11.520] process, the email is initially sent to example.com, after which the percent symbol is converted into [05:11.520 --> 05:18.720] an at symbol, and the email is sent to foo at psres.net. This process can be repeated several [05:18.720 --> 05:29.150] times. Before email addresses existed, and even the internet, people exchanged messages using UUCP. [05:29.870 --> 05:35.790] UUCP stands for Unix to Unix Copy Protocol. It's an early protocol that separates the [05:35.790 --> 05:42.790] local user and multiple hosts using an exclamation mark, creating what is known as the bang path. [05:42.790 --> 05:48.930] Unlike the standard format for internet email addresses today, the bang path lists the hosts [05:48.930 --> 05:55.350] from left to right, with the destination user mailbox on the right, as you can see in the example. [05:56.930 --> 06:02.090] So going back to the original examples with all the special characters removed, [06:02.090 --> 06:06.330] what the parentheses actually do is remove the at symbol and domain, [06:06.330 --> 06:10.770] and without the domain part, Postfix treats that as a percent source root. [06:11.070 --> 06:16.590] Using a backslash and escape in the at symbol has the same effect on sendmail, which treats the [06:16.590 --> 06:22.830] email as a UUCP address. Pretty damn crazy, right? And I've tested it, and all those special characters [06:22.830 --> 06:30.130] do go to the mailbox. I wish I could tell you that I uncovered this behaviour by analysing [06:30.130 --> 06:34.710] thousands of lines of code and using a debugger and stepping through the code flow in sendmail [06:34.710 --> 06:41.950] and Postfix, but that's not actually how it went down. I noticed an unnamed target was allowing [06:41.950 --> 06:47.090] all sorts of special characters, so I just took all the characters, pasted it into my email and [06:47.090 --> 06:52.890] expected it to fail. But actually, what happened was, I checked the syslog on the box that I was [06:52.890 --> 06:59.310] testing, and I noticed a difference. I was getting host unknown error in the delivery [06:59.310 --> 07:05.550] status notification. This was significant because it would suggest a different host was being used. [07:06.230 --> 07:10.070] If you look at the error, you'll notice the exclamation mark is missing. [07:10.070 --> 07:14.750] I didn't actually notice that at the time. So I started to remove characters, [07:14.750 --> 07:19.930] resend the email, and I still got the same error message. I narrowed it down to the exclamation [07:19.930 --> 07:27.670] mark and thus discovered this UUCP behavior. This was bonkers. By sheer luck, the characters I [07:27.670 --> 07:33.270] pasted into my email address ended with a backslash. I think I did this subconsciously. [07:33.270 --> 07:39.850] And then, when the exclamation mark was then, sorry, and then escape the out symbol, [07:39.850 --> 07:44.310] and then the exclamation mark was treating the address as a UUCP address. [07:44.410 --> 07:49.910] I then wrote a fuzzer to find the other behavior with source routes with Postfix. [07:50.530 --> 07:54.530] These findings gave me great confidence that there were a ton of bugs out there, [07:54.530 --> 08:01.310] so I began to look for more. And this is a bonus slide for DEF CON. [08:01.570 --> 08:09.210] In the RSC, there are optional SMTP parameters. I analyzed the Postfix code with a Poetswinger [08:09.210 --> 08:15.050] colleague, and I discovered that these parameters could be used to remove the domain part of the [08:15.050 --> 08:20.730] email address. I found this parsing bug on a real target, but the application in question [08:20.730 --> 08:27.510] didn't use the email domain for access control. This attack works by escaping the backslash to [08:27.510 --> 08:33.010] break out of the quoted local part, confusing the email validation. Then, you can use the [08:33.010 --> 08:37.770] greater than to end the receipt to command, and then you stick all the rest of the address [08:37.770 --> 08:46.530] within this SMTP optional parameter. And you will notice that this works on a real target [08:46.530 --> 08:57.470] and is a valid SMTP conversation. So when it does the receipt to command, it's a valid syntax for [08:57.470 --> 09:06.410] that. As a further bonus, here is some surprising email parsing behavior that I uncovered works on [09:06.410 --> 09:12.310] Postfix. I couldn't use these for access control bypasses, but nevertheless, they are interesting [09:12.310 --> 09:20.350] and challenges your assumptions of how email addresses are parsed. This first one uses a UUCP [09:21.030 --> 09:25.350] address and is sent regardless of the quotes. It's pretty damn crazy. [09:26.990 --> 09:33.570] This second one uses source roots. So if you use a square bracket syntax, Postfix will still treat [09:33.570 --> 09:42.840] it as a source root and send to that email. So one of the main problems I had to solve with this [09:42.840 --> 09:49.140] research was generating blocked characters. Since many web applications will block multiple art [09:49.140 --> 09:56.300] symbols, so this following section focuses on Unicode overflows, which enable the creation of [09:56.300 --> 10:02.200] ASCII characters from higher Unicode characters. It's pretty much normalization, but I'm calling [10:02.200 --> 10:11.340] Unicode overflows because it's a different technique. So the PHP char function is a good [10:11.340 --> 10:18.200] example of how Unicode overflows can occur. The function generates a character from an integer [10:18.200 --> 10:24.420] code point. Here's an illustration of how the algorithm is used by this function. It loops while [10:24.420 --> 10:30.580] the code point is less than zero, continually adding 256 until it's non-negative, because why [10:30.580 --> 10:37.480] not? It's PHP after all. Then it uses the mod operation to ensure the byte value is always [10:37.480 --> 10:45.040] within a range of zero to 255. So if you introduce a Unicode character is larger than 255, it'd be [10:45.040 --> 10:53.960] truncated into this range. Note that Unicode overflows are just specific to PHP. So generating [10:54.380 --> 11:00.060] a Unicode overflow in JavaScript is quite simple. Using the from code point method, [11:00.060 --> 11:07.830] first you pass the number that will cause the overflow, such as 0x100, which is 256 in decimal. [11:08.360 --> 11:13.420] Next, you provide the second hex number to specify the actual character that you want, [11:13.420 --> 11:20.840] like 0x40, which is the art symbol. The from code point method will generate a Unicode character, [11:20.840 --> 11:25.860] and when this character is transformed to fit within 255, a system that doesn't support [11:25.860 --> 11:31.260] higher Unicode characters will produce the ASCII character specified by the second number. [11:32.960 --> 11:39.040] So I hunted for real-world Unicode overflows, and I found a target that altered characters [11:39.040 --> 11:45.400] within the mailer's SMTP conversation. This application blocks the backslash within the [11:45.400 --> 11:51.680] quoted local part, but I bypassed the validation using Unicode overflows that enabled me to escape [11:51.680 --> 11:58.760] characters and break out of the quoted local part. Despite this, when using an encoded art symbol, [11:59.160 --> 12:04.940] the email wasn't sent, suggesting there is a double validation going on, once in the application [12:04.940 --> 12:10.840] and once before sending. So although I didn't manage to exploit this particular target, [12:10.840 --> 12:16.080] the key takeaway is, you can use a Unicode overflow to bypass validation [12:16.080 --> 12:20.500] and actually smuggle characters within the SMTP conversation. [12:23.150 --> 12:28.630] So Unicode overflows were great, but I needed more methods to generate blocked characters. [12:28.630 --> 12:33.810] So I took a deep dive into the glorious RFC looking for gold, and I found it. [12:33.930 --> 12:40.230] This next section is about encoded word. I discovered this when trying to find ways of [12:40.230 --> 12:49.150] encoding the art symbol without Unicode overflows. Encoded word is specified in RFC 2047, [12:49.150 --> 12:54.030] and it allows the inclusion of Unicode characters using encoded data. [12:56.290 --> 13:03.270] And here is how encoded word works. So first we've got the equals and question mark that [13:03.270 --> 13:13.250] indicate the start of encoded word. The following is the character encoding, in this case UTF-8. [13:21.060 --> 13:26.560] Inside the... so the question marks act as a separator between the charset and the encoding [13:26.560 --> 13:33.120] type. Then it's the type of encoding, and then you've got your encoded data as hex. [13:33.120 --> 13:40.380] So it's prefixed with equals, so equal 41 is an uppercase A, so that's where the data occurs. [13:42.300 --> 13:47.060] And the question mark equals indicate the end of the encoded word. [13:47.680 --> 13:51.140] And the actual email that gets sent is that. [13:51.780 --> 13:56.600] So all the meta characters are removed, and that email is sent to there. [13:58.600 --> 14:04.280] To probe for encoded word, you should make two requests with the two charsets. [14:04.360 --> 14:10.440] The reason for this is that many different email parsing libraries support different charsets. [14:10.440 --> 14:15.580] When probing, you should use lowercase characters in case the site rejects or [14:15.580 --> 14:23.580] transforms to lowercase. So I was initially using x as a charset, but to reduce the size of the [14:23.580 --> 14:29.040] probe. However, on some systems, they will reject unknown charsets and it would fail. [14:29.040 --> 14:35.740] So it's better to use these two probes and then follow up with a charset of x to see if it works. [14:35.740 --> 14:42.680] If successful in each case, you'll send an email to the ones displayed. In a real probe, [14:42.680 --> 14:47.680] you would use a collaborator payload or a similar tool. I've highlighted the encoded [14:47.680 --> 14:53.200] and decoded text in red. So for example, equals 61 is a lowercase a. [14:55.850 --> 15:02.890] So we've covered how encoded word works. So let's dive into some real world case studies. [15:03.190 --> 15:11.490] The following case studies all use the Ruby mail library, which has over 508 million downloads. [15:11.590 --> 15:19.690] At the time of testing, the version was 2.8.1. This library is used by many popular applications, [15:19.690 --> 15:24.230] including GitHub, Zendesk, GitLab, and Bugcrowd. [15:26.390 --> 15:32.230] So for my first case study, I'm going to show you how I exploited GitLab Enterprise. I love [15:32.230 --> 15:38.030] this vector because it's so elegant. So first I used a charset of x because this was not important [15:38.030 --> 15:46.270] on the enterprise product. Then I use an encoded at symbol, so equals 40 is encoded at. And then [15:46.270 --> 15:52.410] I used the underscore character. So interestingly, encoded word supports equals 20 as a space, [15:52.410 --> 15:59.670] but also underscore. What happens then is the email is split in half and the email is sent [15:59.670 --> 16:06.660] to that address. So GitLab thinks the address is example.com, but it actually goes to psres.net. [16:08.990 --> 16:15.170] So what was the impact on GitLab Enterprise? Well, I could verify email addresses that I [16:15.170 --> 16:21.770] didn't control. This means I could gain access to GitLab Enterprise servers that use domain-based [16:21.770 --> 16:32.740] registration restrictions. To understand the next set of slides, let's have a quick recap on how the [16:32.740 --> 16:38.260] email gets sent at the SMTP level. So there's a conversation between the mailer and the SMTP [16:38.260 --> 16:43.400] client, and there's a handshake that occurs, but that's not important for this part of the talk. [16:43.400 --> 16:49.320] The important part is the receipt2 command. You'll notice it's enclosed with angle brackets. [16:49.660 --> 16:54.360] We're going to abuse this in the next few slides to change the email destination. [16:58.640 --> 17:06.780] GitLab is also an IDP, and I exploited their email verification. So I used x for the slide [17:06.780 --> 17:11.140] because it wouldn't all fit on. No one needs to be able to see it, but you'd have to use this [17:11.140 --> 17:19.700] tri-set specified in red here. Then I used an encoded at symbol again, but this time I used [17:19.700 --> 17:26.740] the greater than symbol, so this is an encoded greater than symbol, and then an encoded space. [17:27.220 --> 17:33.200] And then GitLab thinks the domain is example.com, but it actually goes to psres.net. [17:33.200 --> 17:37.820] So you can spoof any domain you like and verify any domain you like on GitLab. [17:39.580 --> 17:49.500] So, yeah, using this bug, you could bypass domain-based access controls, websites that support GitLab as an IDP. [17:49.500 --> 17:56.040] This screenshot shows that I've got a Microsoft verified email address and GitLab email address. [17:56.580 --> 17:58.980] It's a bit small, but you can see it's verified. [18:00.200 --> 18:09.480] So this one is more complex, and this works on Zendesk and uses the quoted local part with encoded quotes. [18:10.360 --> 18:13.920] First, we start the quoted local part. [18:15.480 --> 18:22.020] Then we use a Q encoded double quote, so equals 22 is a double quote, [18:22.800 --> 18:26.160] followed by an encoded at symbol this time. [18:27.440 --> 18:34.040] Next, we have to use the greater than and a null character for Zendesk. [18:34.040 --> 18:39.880] So this has the same effect on space. The null character prevents the rest of the address from being used. [18:42.570 --> 18:52.050] 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. [18:52.070 --> 18:57.190] And my theory for this is that Zendesk were doing email parsing multiple times. [18:57.190 --> 19:03.050] So adding all this would then decode and then start to remove parts of the email address. [19:03.590 --> 19:08.450] And then we finish the quoted local part, and the actual email that gets sent is there. [19:08.450 --> 19:12.230] Absolutely crazy. Yeah, pretty nuts. [19:13.850 --> 19:22.130] So the impact of this attack was attackers could verify email addresses from domains they don't control, [19:22.130 --> 19:27.170] which means they could access Zendesk email domain protected support centers. [19:30.290 --> 19:34.170] GitHub were also vulnerable to the greater than and null attack. [19:34.170 --> 19:42.970] So first I created an encoded at symbol, then I used an encoded greater than symbol to end the receipt to command. [19:43.730 --> 19:51.370] And GitHub required a null character, so adding this null character would then ignore the rest of the address. [19:51.630 --> 19:53.770] And that would go to that email again. [19:56.790 --> 20:00.350] So using this technique, I was able to verify domains [20:00.350 --> 20:08.670] I didn't control on GitHub, and I had verified emails from Mozilla.com, GitHub.com, and Microsoft.com. [20:11.520 --> 20:15.800] By spoofing domains on GitHub, I could verify domains I don't control. [20:15.800 --> 20:18.500] This means I could bypass domain-based access controls [20:18.500 --> 20:23.060] on websites that used GitHub as an identity provider. [20:23.280 --> 20:28.400] For example, I could penetrate internal networks protected by Cloudflare Zero Trust. [20:28.400 --> 20:34.450] If it was configured to use GitHub as an IDP and email domain validation. [20:34.720 --> 20:40.420] So the following screenshot shows Zero Trust trusting domains from GitHub.com. [20:40.420 --> 20:43.520] Since any domain can be spoofed and verified, [20:43.520 --> 20:49.290] this vulnerability affected Cloudflare Zero Trust instance using email domain validation. [20:52.410 --> 20:55.660] So as I was testing multiple Ruby libraries, [20:55.660 --> 21:00.960] I discovered that you could use Base64 encoded email addresses. [21:00.960 --> 21:05.400] Because why not, right? We all need this, don't we? We need Base64 encoded addresses. [21:05.640 --> 21:13.640] In this example, it uses the B as the encoding type, so that indicates Base64. [21:14.420 --> 21:17.620] And this is a Base64 foo bar. [21:17.620 --> 21:23.280] So I'm using foo bar as an example, but any character can be encoded. [21:23.280 --> 21:28.700] And that results into an email of foobar at psres.net. [21:28.700 --> 21:31.720] Don't worry, it gets worse. It gets more disgusting. [21:32.340 --> 21:37.840] So who remembers this? A script tag encoded in UTF-7. [21:37.840 --> 21:41.980] This XSS vector used to work on IE. Anybody remember it? [21:42.220 --> 21:46.460] Yeah? Surely? No. Let's see. [21:46.740 --> 21:52.440] Surely email parsing libraries won't allow you to use a UTF-7 charset? [21:52.850 --> 21:59.820] Of course they do. Using the Ruby library mail, here is a UTF-7 encoded address. [22:00.040 --> 22:05.400] So this is a UTF-7 foo bar. You'll notice the ampersand. [22:05.400 --> 22:09.730] That's intentional because that's the character that they support for UTF-7. [22:10.460 --> 22:17.610] So the charset will be decoded and the email will be sent to foobar at psres.net. [22:18.340 --> 22:22.270] I know what you're thinking. Or maybe it's just me. [22:22.270 --> 22:28.130] But yes, you can use UTF-7 and Base64 in an email address. [22:28.660 --> 22:35.220] In this example, there's a Base64 email address with a UTF-7 charset. [22:36.030 --> 22:39.820] First, the email parser will decode Base64. [22:40.190 --> 22:45.170] Then the email parser will decode the UTF-7 charset. [22:45.170 --> 22:48.110] Finally, the decoded address will be sent there. [22:48.110 --> 22:49.250] Absolutely crazy. [22:49.250 --> 22:53.910] And I'll emphasize the point that any character can be encoded. [22:53.910 --> 22:56.750] I'm just using foobar to make it simple to follow. [22:58.590 --> 23:02.970] Then I thought, what about UTF-7 and queue encoding? [23:02.970 --> 23:08.650] So here I'm going to modify the A character and replace it with equals 41. [23:08.650 --> 23:17.330] This will then get decoded and then it will be decoded and form a UTF-7 encoded foobar. [23:17.330 --> 23:19.810] So it's going through two layers of encoding. [23:19.810 --> 23:24.310] At this point, you might start having a few doubts about following the RFC. [23:24.990 --> 23:29.210] Especially when I tell you that this works in the domain part 2. [23:29.210 --> 23:35.710] And when I tested the Ruby mail library, I know what you're thinking. [23:37.190 --> 23:39.570] It's... sorry, I've lost track. [23:40.070 --> 23:41.570] I know what you're thinking. [23:41.570 --> 23:44.830] No website is going to support UTF-7, right? [23:44.830 --> 23:46.950] No website is going to support that. [23:47.070 --> 23:52.890] But actually, there was a small source code web hosting site called GitHub. [23:52.890 --> 23:55.390] They supported UTF-7. [23:55.490 --> 23:58.350] I'm so gutted that I didn't exploit them with UTF-7 [23:58.350 --> 24:01.990] because they'd already fixed it by the time I found this. [24:01.990 --> 24:06.150] And then, yeah, it'll go to that email address. [24:06.150 --> 24:07.930] So foobar.psrez.net. [24:09.990 --> 24:14.170] So then I went hunting for encoded word outside of Ruby. [24:14.170 --> 24:20.130] And I found that phpMailer supports encoded word within the name of the recipient. [24:20.430 --> 24:24.470] I tried exploiting this in WordPress and other applications, [24:24.470 --> 24:29.630] but the requirements of needing angle brackets around the email made exploitation difficult. [24:29.710 --> 24:31.970] Still, I bet there are vulnerable systems [24:31.970 --> 24:35.150] that decode the actual name of the recipient. [24:35.650 --> 24:39.190] So you may find this in bug bounty sites. [24:41.990 --> 24:48.150] So we've explored how you can manipulate email parsing to sidestep access controls. [24:48.150 --> 24:49.810] But shall we take it a bit further? [24:49.810 --> 24:55.730] What if we could use an email address and weaponize it to gain remote code execution? [24:56.230 --> 25:01.230] In this section, we'll cover PuniCode attacks and how I exploited Joomla. [25:02.790 --> 25:08.770] So PuniCode is a way to represent Unicode characters in the current DNS system. [25:08.770 --> 25:13.470] PuniCode always starts with xn and is followed by two hyphens. [25:13.470 --> 25:16.850] The algorithm converts the sequence of Unicode characters [25:16.850 --> 25:21.980] into a representation that utilizes only alphanumeric characters and hyphens. [25:22.350 --> 25:25.810] For example, the domain muchen, I hope I pronounced that right, [25:25.810 --> 25:30.330] is encoded with the following PuniCode sequence, as you can see there. [25:30.330 --> 25:34.950] Due to the very nature of how PuniCode works, it makes it difficult to test [25:34.950 --> 25:38.910] because changing one character can affect the entire output [25:38.910 --> 25:42.810] and the character position due to how the algorithm works. [25:42.810 --> 25:48.030] So what we want to do is generate some malicious characters that when it's decoded... [25:49.110 --> 25:54.390] Sorry, what we want to do is generate malicious characters when it's decoded. [25:54.390 --> 25:56.510] But doing so is a big challenge. [25:56.510 --> 26:01.730] In these examples, you can see the Unicode character changes when one byte is modified. [26:01.730 --> 26:04.700] This makes it difficult to generate a malicious payload. [26:06.130 --> 26:10.130] So after reading about PuniCode on Wikipedia, [26:10.130 --> 26:14.440] I followed a link to an online PuniCode converter and I began to experiment. [26:14.950 --> 26:18.530] The converter used the IDN PHP library. [26:18.530 --> 26:22.590] I first started to add zeros to the beginning of the PuniCode address [26:22.590 --> 26:25.250] to convert it into ASCII characters. [26:25.250 --> 26:31.070] So I initially succeeded generating a comma, which was so surprising I doubted my eyes. [26:31.110 --> 26:35.890] Encouraged by this, I started to experiment further and managed to produce an art symbol. [26:37.670 --> 26:42.650] So after searching GitHub, I found an interesting target using the library, [26:42.650 --> 26:43.430] Joomla. [26:43.430 --> 26:46.990] This was great because if I got XSS, then I would have RCE. [26:47.390 --> 26:51.650] Doing a source code analysis, I noticed they were escaping the email [26:51.650 --> 26:54.070] before it was PuniCode decoded. [26:54.070 --> 26:57.270] This means if I could produce some malformed PuniCode, [26:57.270 --> 27:01.310] that when decoded produces HTML, I could get XSS. [27:01.310 --> 27:02.730] But it wouldn't be that easy. [27:04.070 --> 27:05.270] Sound simple? [27:05.270 --> 27:09.710] Just give the decoder some malformed PuniCode and generate some HTML. [27:09.750 --> 27:12.010] But remember how the algorithm works. [27:12.010 --> 27:16.150] Any change in the character can result in a completely different output. [27:16.190 --> 27:21.350] I thought about this for a while and I thought that this was a good job for a fuzzer. [27:21.350 --> 27:27.230] So I wrote a fuzzer to generate malformed PuniCode and the results were interesting. [27:27.570 --> 27:30.450] I found many ways to generate different characters. [27:30.450 --> 27:32.410] So you can see what I was trying to do here. [27:32.410 --> 27:34.650] I'm trying to generate an SVG type. [27:36.210 --> 27:40.450] So I started generating millions of different character combinations. [27:40.450 --> 27:45.310] I managed to construct partial XSS vectors, but I encountered several issues. [27:45.310 --> 27:50.510] I could only generate two ASCII characters by using more than one PuniCode subdomain. [27:50.510 --> 27:57.410] This limitation arose from the specific workings of the PuniCode algorithm PHP [27:57.410 --> 28:01.110] and the quirks of this buggy PHP IDN library. [28:01.110 --> 28:04.230] As you can see in the examples, I was really close. [28:04.230 --> 28:07.850] But these problems made exploiting Joomla very difficult. [28:07.850 --> 28:10.880] I concluded that XSS was not feasible. [28:11.820 --> 28:16.430] Because although I was able to generate a single quoted attribute, [28:16.430 --> 28:18.710] it required an underscore character. [28:18.710 --> 28:25.510] Joomla, however, does not permit underscore characters in the domain part of the email address. [28:25.750 --> 28:31.030] So you can see those mad PuniCode addresses are trying to inject some HTML. [28:32.250 --> 28:34.470] So was that the end of the story? [28:34.470 --> 28:35.610] Not quite. [28:36.110 --> 28:38.090] I thought about this for a while. [28:38.090 --> 28:41.490] And I worked out that you could use a single PuniCode subdomain [28:41.490 --> 28:44.330] and you could generate any opening tag. [28:44.330 --> 28:46.810] Eventually, after a lot of testing, [28:46.810 --> 28:51.370] I concluded the only exploitable vector was an opening style tag. [28:51.510 --> 28:54.670] Here I use malform PuniCode to construct it. [28:54.710 --> 28:56.370] It looks like a test string, right? [28:56.370 --> 28:58.750] But this is actually how it works. [28:58.750 --> 29:00.390] Style 1, 2, 3. [29:00.390 --> 29:01.750] And 3, 2, 1, sorry. [29:01.750 --> 29:03.650] And that would generate a style tag. [29:05.050 --> 29:09.710] The rest of the pre-existing Joomla HTML code would add the space [29:09.710 --> 29:12.310] and the closing angle bracket for me. [29:12.310 --> 29:14.190] So it injects this style tag [29:14.190 --> 29:18.530] and then the rest of the HTML adds those characters for me. [29:19.650 --> 29:22.050] So what I'd need is two accounts. [29:22.050 --> 29:27.710] The first registers with a style tag with malform PuniCode as demonstrated. [29:27.710 --> 29:34.650] The second account has the actual CSS payload inside the name of the user. [29:34.670 --> 29:37.470] That imports the malicious style sheet. [29:37.470 --> 29:41.990] The first account starts with A and the second account starts with X. [29:41.990 --> 29:45.250] This is to ensure the style injection occurs first [29:45.250 --> 29:49.550] and then the payload with the import occurs second. [29:49.870 --> 29:52.170] Notice I'm using curly braces. [29:52.170 --> 29:58.650] This is to treat all the HTML that occurs before the import as an invalid CSS selector. [29:58.650 --> 30:02.170] And this still works because this is CSS, right? [30:02.170 --> 30:03.450] It'll just work. [30:05.930 --> 30:10.430] So the first step is to register two accounts for the attacker. [30:10.430 --> 30:15.950] The first with the malicious PuniCode and the second with the evil CSS. [30:16.050 --> 30:22.630] So here we're using the malform PuniCode in the email and the second account in the name. [30:25.270 --> 30:30.570] The attacker then uses import chaining and a custom node server, [30:30.870 --> 30:36.670] a custom CSS exfiltrator to quickly exfiltrate the CSERF token from the admin. [30:36.670 --> 30:39.510] And when the admin visits the user's page, [30:39.510 --> 30:44.570] this CSS will get executed and the CSERF token is leaked to the attacker. [30:45.650 --> 30:48.470] The screenshot shows this process happening. [30:51.180 --> 30:59.140] Using the exfiltrated token, the attacker then uses the CSS exfiltrator and gives the admin a URL. [30:59.140 --> 31:04.180] The admin visits that site and a CSERF attack is then performed on the admin [31:04.180 --> 31:10.400] using the exfiltrated token and then the admin template is then modified and then you get RCE. [31:12.480 --> 31:19.260] Now, James said I should do a live demo. I wasn't as keen, but let's give it a shot. [31:25.290 --> 31:31.070] Right, so on the left is the victim, so it's the admin on the left. [31:31.070 --> 31:35.730] On the right is the attacker. We've also got the exfiltrator running [31:36.490 --> 31:39.210] and this is where the token will be extracted. [31:39.930 --> 31:45.810] So the attacker visits the page. I've pre-filled the data because you don't want to see me typing [31:45.810 --> 31:53.870] everything. And the email address contains a malform punicode. So I get an error and that [31:53.870 --> 32:00.730] error is just because I haven't configured mail on the server. And then you register a [32:00.730 --> 32:06.690] second account and this is with the actual payload in that exfiltrates the token. [32:09.710 --> 32:17.470] So the two accounts are registered. Now let's log in as the admin and pray to the demo gods. [32:22.510 --> 32:32.960] Right, thank you very much. But we still need to send the message to the admin, right? [32:33.220 --> 32:38.300] Hey admin, how's your day going? Yeah, going good, living the dream. Let's click on this. [32:41.650 --> 32:45.550] So I'm showing you a real attack. You wouldn't show the payload in there, [32:45.550 --> 32:49.430] but we've backdoored Joomla, and then the attacker goes to pond, [32:49.430 --> 32:58.650] and we've got ICE, we're casting etc password. Thank you, thank you. [33:09.040 --> 33:14.540] So whilst conducting this research, I found it useful to follow the following methodology. [33:14.540 --> 33:21.920] Probe, observe, encode, exploit. First probe for encoded word. Observe that it will be decoded [33:21.920 --> 33:27.440] to confirm that it's supported. Then encode the various characters and observe how they're [33:27.440 --> 33:32.440] decoded. And then follow with an exploit that abuses these characters. So this methodology [33:33.280 --> 33:38.740] is spelt out here. So these are the steps you can take to exploit. And this is some of the [33:38.740 --> 33:46.480] steps I use to exploit these targets. Hackverter is a free open source tool that I wrote for [33:46.480 --> 33:52.960] the burp suite. I've crafted some hackverter tags to help you generate unicode overflows [33:52.960 --> 33:59.920] and encoded word attacks easily within burp. Turbo intruder is another free burp extension [33:59.920 --> 34:05.380] written by James Kettle. I've created a turbo intruder script to exploit the mailers. So you [34:05.380 --> 34:11.300] remember when github supported null for example, this was found with turbo intruder. I've also [34:11.300 --> 34:18.600] created a punicode fuzzer to help with malformed punicode. The fuzzer works by giving some input, [34:18.600 --> 34:24.000] some punicode address, and then the placeholders are substituted with random numbers, characters, [34:24.000 --> 34:29.340] or white space. It was really effective in finding what characters could be generated. [34:29.340 --> 34:34.900] We've also updated burp intruder word list with a list of these attacks too. [34:35.380 --> 34:42.920] For your convenience. To defend against email splitting attacks, make sure you filter out or [34:42.920 --> 34:49.100] disable encoded word. Always verify email addresses before using them, even when received [34:49.100 --> 34:56.660] from an SSO provider. After validating the email address, do not use the domain as a sole means of [34:56.660 --> 35:04.260] authorization because it can lead to bad stuff. A few blog posts were really inspirational when [35:04.260 --> 35:09.300] conducting this research. I recommend you read each one because they are really useful and [35:09.300 --> 35:14.400] contain really useful information. The import chaining technique to use to exfiltrate the [35:14.400 --> 35:22.000] token is from Petvilla and Donut, awesome researchers. So the three takeaways that I [35:22.000 --> 35:29.480] want to leave you with are valid email addresses that can trigger major parser discrepancies. [35:29.480 --> 35:36.000] Even email addresses that end in at example.com might go elsewhere and as a result it's never [35:36.000 --> 35:43.600] safe to use email domains for access control enforcement. We've created a web security academy [35:43.600 --> 35:49.960] CTF so you can try and do an email splitting attack. I've also created a docker file with [35:49.960 --> 35:56.420] the vulnerable Joomla installation for your convenience. You can get all the materials and [35:56.420 --> 36:04.120] download all the materials for this presentation on the GitHub repository. Thank you very much [36:04.120 --> 36:05.300] DEF CON.