WEBVTT

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:31.360
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.

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:54.440
The core of the talk will focus on parser discrepancies, including Unicode overflows, encoded word, and Unicode, all with real-world case studies.

00:54.620 --> 01:02.340
Then I'll share my methodology and tools for automating exploitation, followed by advice on defense.

01:02.380 --> 01:06.440
I'll conclude with the key takeaways, leaving five minutes for questions.

01:06.440 --> 01:08.920
There's also a bonus material for DEF CON too.

01:10.740 --> 01:21.980
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.

01:21.980 --> 01:30.180
For example, Slack will automatically 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:42.820
This makes email verification a critical security boundary.

01:42.820 --> 01:44.620
What could possibly go wrong?

01:47.040 --> 01:56.960
Everyone knows that URL parser discrepancies are critical as they can lead to SSRF path traversal and other security issues.

01:57.080 --> 02:04.660
Using an email domain for access control makes email address parser discrepancies critical too.

02:05.060 --> 02:15.120
Predicting which domain an email should be routed to should be simple, 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:41.320
Validation is often performed using regular expressions, copy and pasted from Stack Overflow, all trying to follow the RFC.

02:41.740 --> 02:47.020
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:05.020
The RFC allows what is called the quoted local part.

03:05.020 --> 03:12.020
This enables you to use characters in the local part that are not normally allowed by using quotes.

03:12.060 --> 03:18.240
In the example shown, I use a quoted local part to use the at symbol as part of the address.

03:18.540 --> 03:26.020
The same RFC allows you to use a quoted per, which means one character preceded by a backslash.

03:26.020 --> 03:33.100
This enables you to use a double quote and a backslash as part of the address provided it is escaped.

03:33.600 --> 03:41.940
Characters enclosed in parentheses are treated as comments and can be placed anywhere within the address except the local part.

03:42.180 --> 03:46.100
They are all ignored when the actual email is delivered.

03:48.280 --> 03:50.940
So here are two email addresses.

03:51.300 --> 03:59.160
You might be wondering which one is valid, as this is a typical question asked in an email presentation.

03:59.220 --> 04:02.980
However, it's the wrong question for this talk.

04:03.960 --> 04:08.700
The question you should be asking is which domain do they go to?

04:08.700 --> 04:14.680
You might expect both emails to go to example.com, but that's not what happens.

04:14.680 --> 04:17.680
They both go to psres.net.

04:17.740 --> 04:23.080
Remember how I said predicting the email destination is extremely difficult?

04:23.080 --> 04:25.720
This is just the tip of the iceberg.

04:25.920 --> 04:30.860
The first example works on Postfix and the second example works on Sendmail.

04:30.860 --> 04:33.840
If you are confused, you should be.

04:33.840 --> 04:36.700
Next, let's explain why this happens.

04:38.800 --> 04:46.080
A source route is a sequence of servers which an email is sent before reaching its final destination.

04:46.320 --> 04:55.100
Servers in the chain are separated by commas, with the final destination marked with a colon before the recipient's full address.

04:56.420 --> 05:00.380
Another form of source routing is called the percent hack.

05:00.380 --> 05:04.500
Despite its name, the actual character is determined by the mailer.

05:04.500 --> 05:16.320
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.

05:16.320 --> 05:19.980
This process can be repeated several times.

05:22.250 --> 05:29.150
Before email addresses existed, and even the internet, people exchanged messages using UUCP.

05:29.870 --> 05:33.410
UUCP stands for Unix to Unix Copy Protocol.

05:33.410 --> 05:42.790
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.

05:42.790 --> 05:55.350
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.

05:56.930 --> 06:10.770
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.

06:11.070 --> 06:19.070
Using a backslash and escape in the at symbol has the same effect on sendmail, which treats the email as a UUCP address.

06:19.430 --> 06:20.790
Pretty damn crazy, right?

06:20.790 --> 06:24.190
And I've tested it, and all those special characters do go to the mailbox.

06:26.450 --> 06:38.450
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.

06:39.570 --> 06:48.630
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.

06:48.630 --> 06:55.450
But actually, what happened was, I checked the syslog on the box that I was testing, and I noticed a difference.

06:56.370 --> 07:00.910
I was getting host unknown error in the delivery status notification.

07:01.050 --> 07:05.550
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:12.210
I didn't actually notice that at the time.

07:12.210 --> 07:18.310
So I started to remove characters, resend the email, and I still got the same error message.

07:18.310 --> 07:24.030
I narrowed it down to the exclamation mark and thus discovered this UUCP behavior.

07:24.310 --> 07:25.610
This was bonkers.

07:25.610 --> 07:31.130
By sheer luck, the characters I pasted into my email address ended with a backslash.

07:31.130 --> 07:33.270
I think I did this subconsciously.

07:33.270 --> 07:44.310
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.

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:56.670
These findings gave me great confidence that there were a ton of bugs out there, so I began to look for more.

07:58.790 --> 08:01.310
And this is a bonus slide for DEF CON.

08:01.570 --> 08:06.250
In the RSC, there are optional SMTP parameters.

08:06.330 --> 08:16.170
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.

08:16.250 --> 08:23.830
I found this parsing bug on a real target, but the application in question didn't use the email domain for access control.

08:24.050 --> 08:31.610
This attack works by escaping the backslash to break out of the quoted local part, confusing the email validation.

08:31.650 --> 08:42.130
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.

08:42.170 --> 08:53.650
And you will notice that this works on a real target and is a valid SMTP conversation.

08:53.650 --> 08:58.350
So when it does the receipt to command, it's a valid syntax for that.

08:59.970 --> 09:07.170
As a further bonus, here is some surprising email parsing behavior that I uncovered works on Postfix.

09:07.170 --> 09:17.650
I couldn't use these for access control bypasses, but nevertheless, they are interesting and challenges your assumptions of how email addresses are parsed.

09:17.650 --> 09:24.250
This first one uses a UUCP address and is sent regardless of the quotes.

09:24.250 --> 09:25.350
It's pretty damn crazy.

09:26.990 --> 09:29.470
This second one uses source roots.

09:29.470 --> 09:37.410
So if you use a square bracket syntax, Postfix will still treat it as a source root and send to that email.

09:40.300 --> 09:45.960
So one of the main problems I had to solve with this research was generating blocked characters.

09:45.960 --> 09:59.300
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.

09:59.300 --> 10:06.640
It's pretty much normalization, but I'm calling Unicode overflows because it's a different technique.

10:07.920 --> 10:15.400
So the PHP char function is a good example of how Unicode overflows can occur.

10:15.460 --> 10:19.480
The function generates a character from an integer code point.

10:19.480 --> 10:23.380
Here's an illustration of how the algorithm is used by this function.

10:23.380 --> 10:30.980
It loops while the code point is less than zero, continually adding 256 until it's non-negative, because why not?

10:30.980 --> 10:32.800
It's PHP after all.

10:33.300 --> 10:40.040
Then it uses the mod operation to ensure the byte value is always within a range of zero to 255.

10:40.040 --> 10:47.220
So if you introduce a Unicode character is larger than 255, it'd be truncated into this range.

10:47.260 --> 10:50.920
Note that Unicode overflows are just specific to PHP.

10:52.780 --> 10:57.520
So generating a Unicode overflow in JavaScript is quite simple.

10:57.580 --> 11:07.830
Using the from code point method, first you pass the number that will cause the overflow, such as 0x100, which is 256 in decimal.

11:08.360 --> 11:16.800
Next, you provide the second hex number to specify the actual character that you want, like 0x40, which is the art symbol.

11:17.080 --> 11:31.260
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.

11:32.960 --> 11:41.860
So I hunted for real-world Unicode overflows, and I found a target that altered characters within the mailer's SMTP conversation.

11:41.860 --> 11:54.900
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.

11:54.900 --> 12:06.980
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.

12:07.000 --> 12:20.500
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.

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:37.570
This next section is about encoded word.

12:37.730 --> 12:44.790
I discovered this when trying to find ways of encoding the art symbol without Unicode overflows.

12:44.990 --> 12:54.030
Encoded word is specified in RFC 2047, and it allows the inclusion of Unicode characters using encoded data.

12:56.290 --> 13:00.070
And here is how encoded word works.

13:00.190 --> 13:06.050
So first we've got the equals and question mark that indicate the start of encoded word.

13:07.050 --> 13:13.250
The following is the character encoding, in this case UTF-8.

13:21.060 --> 13:27.620
Inside the... so the question marks act as a separator between the charset and the encoding type.

13:28.100 --> 13:33.120
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:17.620
When probing, you should use lowercase characters in case the site rejects or transforms to lowercase.

14:17.620 --> 14:24.200
So I was initially using x as a charset, but to reduce the size of the probe.

14:24.200 --> 14:29.040
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:41.540
If successful in each case, you'll send an email to the ones displayed.

14:41.600 --> 14:45.640
In a real probe, you would use a collaborator payload or a similar tool.

14:45.660 --> 14:49.640
I've highlighted the encoded and decoded text in red.

14:49.640 --> 14:53.200
So for example, equals 61 is a lowercase a.

14:55.850 --> 14:59.490
So we've covered how encoded word works.

14:59.490 --> 15:02.890
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:15.870
At the time of testing, the version was 2.8.1.

15:15.870 --> 15:24.230
This library is used by many popular applications, including GitHub, Zendesk, GitLab, and Bugcrowd.

15:26.390 --> 15:31.690
So for my first case study, I'm going to show you how I exploited GitLab Enterprise.

15:31.770 --> 15:34.050
I love this vector because it's so elegant.

15:34.330 --> 15:39.690
So first I used a charset of x because this was not important on the enterprise product.

15:41.010 --> 15:45.450
Then I use an encoded at symbol, so equals 40 is encoded at.

15:45.650 --> 15:49.010
And then I used the underscore character.

15:49.010 --> 15:54.190
So interestingly, encoded word supports equals 20 as a space, but also underscore.

15:54.390 --> 16:01.050
What happens then is the email is split in half and the email is sent to that address.

16:01.050 --> 16:06.660
So GitLab thinks the address is example.com, but it actually goes to psres.net.

16:08.990 --> 16:12.630
So what was the impact on GitLab Enterprise?

16:12.810 --> 16:16.210
Well, I could verify email addresses that I didn't control.

16:16.210 --> 16:24.230
This means I could gain access to GitLab Enterprise servers that use domain-based registration restrictions.

16:27.540 --> 16:35.440
To understand the next set of slides, let's have a quick recap on how the email gets sent at the SMTP level.

16:35.540 --> 16:43.400
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.

16:43.400 --> 16:46.200
The important part is the receipt2 command.

16:46.280 --> 16:49.320
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:03.800
GitLab is also an IDP, and I exploited their email verification.

17:04.160 --> 17:08.360
So I used x for the slide because it wouldn't all fit on.

17:08.360 --> 17:13.640
No one needs to be able to see it, but you'd have to use this tri-set specified in red here.

17:14.780 --> 17:26.740
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.

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:26.160
Then we use a Q encoded double quote, so equals 22 is a double quote, 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:36.380
So this has the same effect on space.

18:36.380 --> 18:39.880
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:10.070
Absolutely crazy.

19:10.390 --> 19:12.230
Yeah, pretty nuts.

19:13.850 --> 19:27.170
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.

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:08.670
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.

20:11.520 --> 20:15.800
By spoofing domains on GitHub, I could verify domains I don't control.

20:15.800 --> 20:23.060
This means I could bypass domain-based access controls 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:49.290
Since any domain can be spoofed and verified, this vulnerability affected Cloudflare Zero Trust instance using email domain validation.

20:52.410 --> 21:00.960
So as I was testing multiple Ruby libraries, I discovered that you could use Base64 encoded email addresses.

21:00.960 --> 21:01.880
Because why not, right?

21:01.880 --> 21:02.980
We all need this, don't we?

21:02.980 --> 21:05.400
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:30.040
Don't worry, it gets worse.

21:30.040 --> 21:31.720
It gets more disgusting.

21:32.340 --> 21:34.740
So who remembers this?

21:34.740 --> 21:37.840
A script tag encoded in UTF-7.

21:37.840 --> 21:40.360
This XSS vector used to work on IE.

21:40.360 --> 21:41.980
Anybody remember it?

21:42.220 --> 21:43.340
Yeah?

21:43.560 --> 21:44.360
Surely?

21:44.360 --> 21:45.020
No.

21:45.020 --> 21:46.460
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:54.560
Of course they do.

21:54.560 --> 21:59.820
Using the Ruby library mail, here is a UTF-7 encoded address.

22:00.040 --> 22:03.700
So this is a UTF-7 foo bar.

22:03.700 --> 22:05.400
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:20.230
I know what you're thinking.

22:20.230 --> 22:22.270
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 --> 24:01.990
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.

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:29.630
I tried exploiting this in WordPress and other applications, but the requirements of needing angle brackets around the email made exploitation difficult.

24:29.710 --> 24:35.150
Still, I bet there are vulnerable systems 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:21.980
The algorithm converts the sequence of Unicode characters into a representation that utilizes only alphanumeric characters and hyphens.

25:22.350 --> 25:30.330
For example, the domain muchen, I hope I pronounced that right, is encoded with the following PuniCode sequence, as you can see there.

25:30.330 --> 25:42.810
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.

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:14.440
So after reading about PuniCode on Wikipedia, 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:25.250
I first started to add zeros to the beginning of the PuniCode address 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:43.430
So after searching GitHub, I found an interesting target using the library, Joomla.

26:43.430 --> 26:46.990
This was great because if I got XSS, then I would have RCE.

26:47.390 --> 26:54.070
Doing a source code analysis, I noticed they were escaping the email before it was PuniCode decoded.

26:54.070 --> 27:01.310
This means if I could produce some malformed PuniCode, 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 --> 28:01.110
This limitation arose from the specific workings of the PuniCode algorithm PHP 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:18.710
Because although I was able to generate a single quoted attribute, 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:44.330
And I worked out that you could use a single PuniCode subdomain and you could generate any opening tag.

28:44.330 --> 28:51.370
Eventually, after a lot of testing, 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:12.310
The rest of the pre-existing Joomla HTML code would add the space and the closing angle bracket for me.

29:12.310 --> 29:18.530
So it injects this style tag 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:49.550
This is to ensure the style injection occurs first 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:36.670
The attacker then uses import chaining and a custom node server, a custom CSS exfiltrator to quickly exfiltrate the CSERF token from the admin.

30:36.670 --> 30:44.570
And when the admin visits the user's page, 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:10.400
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.

31:12.480 --> 31:15.880
Now, James said I should do a live demo.

31:15.980 --> 31:19.260
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:33.070
On the right is the attacker.

31:33.150 --> 31:39.210
We've also got the exfiltrator running and this is where the token will be extracted.

31:39.930 --> 31:43.030
So the attacker visits the page.

31:43.110 --> 31:46.710
I've pre-filled the data because you don't want to see me typing everything.

31:47.530 --> 31:51.810
And the email address contains a malform punicode.

31:52.430 --> 31:57.410
So I get an error and that error is just because I haven't configured mail on the server.

31:58.270 --> 32:06.690
And then you register a second account and this is with the actual payload in that exfiltrates the token.

32:09.710 --> 32:12.030
So the two accounts are registered.

32:12.630 --> 32:17.470
Now let's log in as the admin and pray to the demo gods.

32:22.510 --> 32:25.630
Right, thank

32:29.020 --> 32:30.060
you very much.

32:30.060 --> 32:32.960
But we still need to send the message to the admin, right?

32:33.220 --> 32:35.040
Hey admin, how's your day going?

32:35.040 --> 32:36.860
Yeah, going good, living the dream.

32:36.860 --> 32:38.300
Let's click on this.

32:41.650 --> 32:43.850
So I'm showing you a real attack.

32:43.850 --> 32:53.070
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.

32:56.210 --> 32:58.650
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:17.780
Probe, observe, encode, exploit.

33:17.840 --> 33:20.340
First probe for encoded word.

33:20.340 --> 33:24.500
Observe that it will be decoded to confirm that it's supported.

33:24.520 --> 33:28.240
Then encode the various characters and observe how they're decoded.

33:28.240 --> 33:31.460
And then follow with an exploit that abuses these characters.

33:31.620 --> 33:34.820
So this methodology is spelt out here.

33:34.820 --> 33:37.300
So these are the steps you can take to exploit.

33:37.300 --> 33:41.400
And this is some of the steps I use to exploit these targets.

33:43.760 --> 33:48.180
Hackverter is a free open source tool that I wrote for the burp suite.

33:48.180 --> 33:56.700
I've crafted some hackverter tags to help you generate unicode overflows and encoded word attacks easily within burp.

33:56.720 --> 34:01.560
Turbo intruder is another free burp extension written by James Kettle.

34:01.560 --> 34:05.180
I've created a turbo intruder script to exploit the mailers.

34:05.180 --> 34:10.040
So you remember when github supported null for example, this was found with turbo intruder.

34:10.360 --> 34:15.520
I've also created a punicode fuzzer to help with malformed punicode.

34:15.520 --> 34:25.300
The fuzzer works by giving some input, some punicode address, and then the placeholders are substituted with random numbers, characters, or white space.

34:25.300 --> 34:29.340
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:37.080
For your convenience.

34:38.560 --> 34:44.940
To defend against email splitting attacks, make sure you filter out or disable encoded word.

34:44.980 --> 34:51.120
Always verify email addresses before using them, even when received from an SSO provider.

34:51.280 --> 34:59.640
After validating the email address, do not use the domain as a sole means of authorization because it can lead to bad stuff.

35:01.140 --> 35:05.980
A few blog posts were really inspirational when conducting this research.

35:05.980 --> 35:11.280
I recommend you read each one because they are really useful and contain really useful information.

35:11.280 --> 35:17.840
The import chaining technique to use to exfiltrate the token is from Petvilla and Donut, awesome researchers.

35:19.720 --> 35:29.480
So the three takeaways that I want to leave you with are valid email addresses that can trigger major parser discrepancies.

35:29.480 --> 35:40.640
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.

35:41.220 --> 35:47.920
We've created a web security academy CTF so you can try and do an email splitting attack.

35:47.920 --> 35:54.580
I've also created a docker file with the vulnerable Joomla installation for your convenience.

35:54.580 --> 36:02.980
You can get all the materials and download all the materials for this presentation on the GitHub repository.

36:02.980 --> 36:05.300
Thank you very much DEF CON.

