1 00:00:00,000 --> 00:00:06,200 Welcome to splitting the email atom, exploiting parsers to bypass access controls. 2 00:00:06,560 --> 00:00:13,280 Email addresses might seem mundane, but the RFCs governing them are downright bonkers. 3 00:00:13,280 --> 00: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. 4 00:00:33,680 --> 00:00:38,280 First, I'll explain why email address parser discrepancies matter. 5 00:00:38,800 --> 00:00:43,000 Next, I'll cover the shaky foundations that emails are built on. 6 00:00:43,300 --> 00: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. 7 00:00:54,620 --> 00:01:02,340 Then I'll share my methodology and tools for automating exploitation, followed by advice on defense. 8 00:01:02,380 --> 00:01:06,440 I'll conclude with the key takeaways, leaving five minutes for questions. 9 00:01:06,440 --> 00:01:08,920 There's also a bonus material for DEF CON too. 10 00:01:10,740 --> 00: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. 11 00:01:21,980 --> 00:01:30,180 For example, Slack will automatically give you access to your company's Slack channel based upon your work email address. 12 00:01:30,200 --> 00:01:37,680 And Cloudflare Zero Trust can be configured to use the email domain to protect your internal network. 13 00:01:37,780 --> 00:01:42,820 This makes email verification a critical security boundary. 14 00:01:42,820 --> 00:01:44,620 What could possibly go wrong? 15 00:01:47,040 --> 00:01:56,960 Everyone knows that URL parser discrepancies are critical as they can lead to SSRF path traversal and other security issues. 16 00:01:57,080 --> 00:02:04,660 Using an email domain for access control makes email address parser discrepancies critical too. 17 00:02:05,060 --> 00: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. 18 00:02:17,160 --> 00:02:21,660 So we've established why email address parser discrepancies matter. 19 00:02:21,660 --> 00:02:26,120 Now let's explore why predicting the email destination is so challenging. 20 00:02:26,560 --> 00:02:32,820 Email addresses are built on a shaky foundation of RFCs designed many years ago. 21 00:02:32,840 --> 00:02:41,320 Validation is often performed using regular expressions, copy and pasted from Stack Overflow, all trying to follow the RFC. 22 00:02:41,740 --> 00:02:47,020 However, one mistake in the RegEx and everything can come crashing down. 23 00:02:49,220 --> 00:02:54,720 I'm not going to bore you with RFCs, but I will highlight the important ones for this talk. 24 00:02:54,720 --> 00:03:00,620 For instance, the characters before the at symbol in an email address are called the local part. 25 00:03:00,760 --> 00:03:05,020 The RFC allows what is called the quoted local part. 26 00:03:05,020 --> 00:03:12,020 This enables you to use characters in the local part that are not normally allowed by using quotes. 27 00:03:12,060 --> 00:03:18,240 In the example shown, I use a quoted local part to use the at symbol as part of the address. 28 00:03:18,540 --> 00:03:26,020 The same RFC allows you to use a quoted per, which means one character preceded by a backslash. 29 00:03:26,020 --> 00:03:33,100 This enables you to use a double quote and a backslash as part of the address provided it is escaped. 30 00:03:33,600 --> 00:03:41,940 Characters enclosed in parentheses are treated as comments and can be placed anywhere within the address except the local part. 31 00:03:42,180 --> 00:03:46,100 They are all ignored when the actual email is delivered. 32 00:03:48,280 --> 00:03:50,940 So here are two email addresses. 33 00:03:51,300 --> 00:03:59,160 You might be wondering which one is valid, as this is a typical question asked in an email presentation. 34 00:03:59,220 --> 00:04:02,980 However, it's the wrong question for this talk. 35 00:04:03,960 --> 00:04:08,700 The question you should be asking is which domain do they go to? 36 00:04:08,700 --> 00:04:14,680 You might expect both emails to go to example.com, but that's not what happens. 37 00:04:14,680 --> 00:04:17,680 They both go to psres.net. 38 00:04:17,740 --> 00:04:23,080 Remember how I said predicting the email destination is extremely difficult? 39 00:04:23,080 --> 00:04:25,720 This is just the tip of the iceberg. 40 00:04:25,920 --> 00:04:30,860 The first example works on Postfix and the second example works on Sendmail. 41 00:04:30,860 --> 00:04:33,840 If you are confused, you should be. 42 00:04:33,840 --> 00:04:36,700 Next, let's explain why this happens. 43 00:04:38,800 --> 00:04:46,080 A source route is a sequence of servers which an email is sent before reaching its final destination. 44 00:04:46,320 --> 00: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. 45 00:04:56,420 --> 00:05:00,380 Another form of source routing is called the percent hack. 46 00:05:00,380 --> 00:05:04,500 Despite its name, the actual character is determined by the mailer. 47 00:05:04,500 --> 00: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. 48 00:05:16,320 --> 00:05:19,980 This process can be repeated several times. 49 00:05:22,250 --> 00:05:29,150 Before email addresses existed, and even the internet, people exchanged messages using UUCP. 50 00:05:29,870 --> 00:05:33,410 UUCP stands for Unix to Unix Copy Protocol. 51 00:05:33,410 --> 00: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. 52 00:05:42,790 --> 00: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. 53 00:05:56,930 --> 00: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. 54 00:06:11,070 --> 00: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. 55 00:06:19,430 --> 00:06:20,790 Pretty damn crazy, right? 56 00:06:20,790 --> 00:06:24,190 And I've tested it, and all those special characters do go to the mailbox. 57 00:06:26,450 --> 00: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. 58 00:06:39,570 --> 00: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. 59 00:06:48,630 --> 00:06:55,450 But actually, what happened was, I checked the syslog on the box that I was testing, and I noticed a difference. 60 00:06:56,370 --> 00:07:00,910 I was getting host unknown error in the delivery status notification. 61 00:07:01,050 --> 00:07:05,550 This was significant because it would suggest a different host was being used. 62 00:07:06,230 --> 00:07:10,070 If you look at the error, you'll notice the exclamation mark is missing. 63 00:07:10,070 --> 00:07:12,210 I didn't actually notice that at the time. 64 00:07:12,210 --> 00:07:18,310 So I started to remove characters, resend the email, and I still got the same error message. 65 00:07:18,310 --> 00:07:24,030 I narrowed it down to the exclamation mark and thus discovered this UUCP behavior. 66 00:07:24,310 --> 00:07:25,610 This was bonkers. 67 00:07:25,610 --> 00:07:31,130 By sheer luck, the characters I pasted into my email address ended with a backslash. 68 00:07:31,130 --> 00:07:33,270 I think I did this subconsciously. 69 00:07:33,270 --> 00: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. 70 00:07:44,410 --> 00:07:49,910 I then wrote a fuzzer to find the other behavior with source routes with Postfix. 71 00:07:50,530 --> 00: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. 72 00:07:58,790 --> 00:08:01,310 And this is a bonus slide for DEF CON. 73 00:08:01,570 --> 00:08:06,250 In the RSC, there are optional SMTP parameters. 74 00:08:06,330 --> 00: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. 75 00:08:16,250 --> 00: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. 76 00:08:24,050 --> 00:08:31,610 This attack works by escaping the backslash to break out of the quoted local part, confusing the email validation. 77 00:08:31,650 --> 00: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. 78 00:08:42,170 --> 00:08:53,650 And you will notice that this works on a real target and is a valid SMTP conversation. 79 00:08:53,650 --> 00:08:58,350 So when it does the receipt to command, it's a valid syntax for that. 80 00:08:59,970 --> 00:09:07,170 As a further bonus, here is some surprising email parsing behavior that I uncovered works on Postfix. 81 00:09:07,170 --> 00: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. 82 00:09:17,650 --> 00:09:24,250 This first one uses a UUCP address and is sent regardless of the quotes. 83 00:09:24,250 --> 00:09:25,350 It's pretty damn crazy. 84 00:09:26,990 --> 00:09:29,470 This second one uses source roots. 85 00:09:29,470 --> 00: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. 86 00:09:40,300 --> 00:09:45,960 So one of the main problems I had to solve with this research was generating blocked characters. 87 00:09:45,960 --> 00: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. 88 00:09:59,300 --> 00:10:06,640 It's pretty much normalization, but I'm calling Unicode overflows because it's a different technique. 89 00:10:07,920 --> 00:10:15,400 So the PHP char function is a good example of how Unicode overflows can occur. 90 00:10:15,460 --> 00:10:19,480 The function generates a character from an integer code point. 91 00:10:19,480 --> 00:10:23,380 Here's an illustration of how the algorithm is used by this function. 92 00:10:23,380 --> 00:10:30,980 It loops while the code point is less than zero, continually adding 256 until it's non-negative, because why not? 93 00:10:30,980 --> 00:10:32,800 It's PHP after all. 94 00:10:33,300 --> 00:10:40,040 Then it uses the mod operation to ensure the byte value is always within a range of zero to 255. 95 00:10:40,040 --> 00:10:47,220 So if you introduce a Unicode character is larger than 255, it'd be truncated into this range. 96 00:10:47,260 --> 00:10:50,920 Note that Unicode overflows are just specific to PHP. 97 00:10:52,780 --> 00:10:57,520 So generating a Unicode overflow in JavaScript is quite simple. 98 00:10:57,580 --> 00: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. 99 00:11:08,360 --> 00: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. 100 00:11:17,080 --> 00: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. 101 00:11:32,960 --> 00: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. 102 00:11:41,860 --> 00: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. 103 00:11:54,900 --> 00: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. 104 00:12:07,000 --> 00: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. 105 00:12:23,150 --> 00:12:28,630 So Unicode overflows were great, but I needed more methods to generate blocked characters. 106 00:12:28,630 --> 00:12:33,810 So I took a deep dive into the glorious RFC looking for gold, and I found it. 107 00:12:33,930 --> 00:12:37,570 This next section is about encoded word. 108 00:12:37,730 --> 00:12:44,790 I discovered this when trying to find ways of encoding the art symbol without Unicode overflows. 109 00:12:44,990 --> 00:12:54,030 Encoded word is specified in RFC 2047, and it allows the inclusion of Unicode characters using encoded data. 110 00:12:56,290 --> 00:13:00,070 And here is how encoded word works. 111 00:13:00,190 --> 00:13:06,050 So first we've got the equals and question mark that indicate the start of encoded word. 112 00:13:07,050 --> 00:13:13,250 The following is the character encoding, in this case UTF-8. 113 00:13:21,060 --> 00:13:27,620 Inside the... so the question marks act as a separator between the charset and the encoding type. 114 00:13:28,100 --> 00:13:33,120 Then it's the type of encoding, and then you've got your encoded data as hex. 115 00:13:33,120 --> 00:13:40,380 So it's prefixed with equals, so equal 41 is an uppercase A, so that's where the data occurs. 116 00:13:42,300 --> 00:13:47,060 And the question mark equals indicate the end of the encoded word. 117 00:13:47,680 --> 00:13:51,140 And the actual email that gets sent is that. 118 00:13:51,780 --> 00:13:56,600 So all the meta characters are removed, and that email is sent to there. 119 00:13:58,600 --> 00:14:04,280 To probe for encoded word, you should make two requests with the two charsets. 120 00:14:04,360 --> 00:14:10,440 The reason for this is that many different email parsing libraries support different charsets. 121 00:14:10,440 --> 00:14:17,620 When probing, you should use lowercase characters in case the site rejects or transforms to lowercase. 122 00:14:17,620 --> 00:14:24,200 So I was initially using x as a charset, but to reduce the size of the probe. 123 00:14:24,200 --> 00:14:29,040 However, on some systems, they will reject unknown charsets and it would fail. 124 00:14:29,040 --> 00: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. 125 00:14:35,740 --> 00:14:41,540 If successful in each case, you'll send an email to the ones displayed. 126 00:14:41,600 --> 00:14:45,640 In a real probe, you would use a collaborator payload or a similar tool. 127 00:14:45,660 --> 00:14:49,640 I've highlighted the encoded and decoded text in red. 128 00:14:49,640 --> 00:14:53,200 So for example, equals 61 is a lowercase a. 129 00:14:55,850 --> 00:14:59,490 So we've covered how encoded word works. 130 00:14:59,490 --> 00:15:02,890 So let's dive into some real world case studies. 131 00:15:03,190 --> 00:15:11,490 The following case studies all use the Ruby mail library, which has over 508 million downloads. 132 00:15:11,590 --> 00:15:15,870 At the time of testing, the version was 2.8.1. 133 00:15:15,870 --> 00:15:24,230 This library is used by many popular applications, including GitHub, Zendesk, GitLab, and Bugcrowd. 134 00:15:26,390 --> 00:15:31,690 So for my first case study, I'm going to show you how I exploited GitLab Enterprise. 135 00:15:31,770 --> 00:15:34,050 I love this vector because it's so elegant. 136 00:15:34,330 --> 00:15:39,690 So first I used a charset of x because this was not important on the enterprise product. 137 00:15:41,010 --> 00:15:45,450 Then I use an encoded at symbol, so equals 40 is encoded at. 138 00:15:45,650 --> 00:15:49,010 And then I used the underscore character. 139 00:15:49,010 --> 00:15:54,190 So interestingly, encoded word supports equals 20 as a space, but also underscore. 140 00:15:54,390 --> 00:16:01,050 What happens then is the email is split in half and the email is sent to that address. 141 00:16:01,050 --> 00:16:06,660 So GitLab thinks the address is example.com, but it actually goes to psres.net. 142 00:16:08,990 --> 00:16:12,630 So what was the impact on GitLab Enterprise? 143 00:16:12,810 --> 00:16:16,210 Well, I could verify email addresses that I didn't control. 144 00:16:16,210 --> 00:16:24,230 This means I could gain access to GitLab Enterprise servers that use domain-based registration restrictions. 145 00:16:27,540 --> 00: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. 146 00:16:35,540 --> 00: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. 147 00:16:43,400 --> 00:16:46,200 The important part is the receipt2 command. 148 00:16:46,280 --> 00:16:49,320 You'll notice it's enclosed with angle brackets. 149 00:16:49,660 --> 00:16:54,360 We're going to abuse this in the next few slides to change the email destination. 150 00:16:58,640 --> 00:17:03,800 GitLab is also an IDP, and I exploited their email verification. 151 00:17:04,160 --> 00:17:08,360 So I used x for the slide because it wouldn't all fit on. 152 00:17:08,360 --> 00: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. 153 00:17:14,780 --> 00: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. 154 00:17:27,220 --> 00:17:33,200 And then GitLab thinks the domain is example.com, but it actually goes to psres.net. 155 00:17:33,200 --> 00:17:37,820 So you can spoof any domain you like and verify any domain you like on GitLab. 156 00:17:39,580 --> 00:17:49,500 So, yeah, using this bug, you could bypass domain-based access controls, websites that support GitLab as an IDP. 157 00:17:49,500 --> 00:17:56,040 This screenshot shows that I've got a Microsoft verified email address and GitLab email address. 158 00:17:56,580 --> 00:17:58,980 It's a bit small, but you can see it's verified. 159 00:18:00,200 --> 00:18:09,480 So this one is more complex, and this works on Zendesk and uses the quoted local part with encoded quotes. 160 00:18:10,360 --> 00:18:13,920 First, we start the quoted local part. 161 00:18:15,480 --> 00: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. 162 00:18:27,440 --> 00:18:34,040 Next, we have to use the greater than and a null character for Zendesk. 163 00:18:34,040 --> 00:18:36,380 So this has the same effect on space. 164 00:18:36,380 --> 00:18:39,880 The null character prevents the rest of the address from being used. 165 00:18:42,570 --> 00: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. 166 00:18:52,070 --> 00:18:57,190 And my theory for this is that Zendesk were doing email parsing multiple times. 167 00:18:57,190 --> 00:19:03,050 So adding all this would then decode and then start to remove parts of the email address. 168 00:19:03,590 --> 00:19:08,450 And then we finish the quoted local part, and the actual email that gets sent is there. 169 00:19:08,450 --> 00:19:10,070 Absolutely crazy. 170 00:19:10,390 --> 00:19:12,230 Yeah, pretty nuts. 171 00:19:13,850 --> 00: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. 172 00:19:30,290 --> 00:19:34,170 GitHub were also vulnerable to the greater than and null attack. 173 00:19:34,170 --> 00: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. 174 00:19:43,730 --> 00:19:51,370 And GitHub required a null character, so adding this null character would then ignore the rest of the address. 175 00:19:51,630 --> 00:19:53,770 And that would go to that email again. 176 00:19:56,790 --> 00: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. 177 00:20:11,520 --> 00:20:15,800 By spoofing domains on GitHub, I could verify domains I don't control. 178 00:20:15,800 --> 00:20:23,060 This means I could bypass domain-based access controls on websites that used GitHub as an identity provider. 179 00:20:23,280 --> 00:20:28,400 For example, I could penetrate internal networks protected by Cloudflare Zero Trust. 180 00:20:28,400 --> 00:20:34,450 If it was configured to use GitHub as an IDP and email domain validation. 181 00:20:34,720 --> 00:20:40,420 So the following screenshot shows Zero Trust trusting domains from GitHub.com. 182 00:20:40,420 --> 00:20:49,290 Since any domain can be spoofed and verified, this vulnerability affected Cloudflare Zero Trust instance using email domain validation. 183 00:20:52,410 --> 00:21:00,960 So as I was testing multiple Ruby libraries, I discovered that you could use Base64 encoded email addresses. 184 00:21:00,960 --> 00:21:01,880 Because why not, right? 185 00:21:01,880 --> 00:21:02,980 We all need this, don't we? 186 00:21:02,980 --> 00:21:05,400 We need Base64 encoded addresses. 187 00:21:05,640 --> 00:21:13,640 In this example, it uses the B as the encoding type, so that indicates Base64. 188 00:21:14,420 --> 00:21:17,620 And this is a Base64 foo bar. 189 00:21:17,620 --> 00:21:23,280 So I'm using foo bar as an example, but any character can be encoded. 190 00:21:23,280 --> 00:21:28,700 And that results into an email of foobar at psres.net. 191 00:21:28,700 --> 00:21:30,040 Don't worry, it gets worse. 192 00:21:30,040 --> 00:21:31,720 It gets more disgusting. 193 00:21:32,340 --> 00:21:34,740 So who remembers this? 194 00:21:34,740 --> 00:21:37,840 A script tag encoded in UTF-7. 195 00:21:37,840 --> 00:21:40,360 This XSS vector used to work on IE. 196 00:21:40,360 --> 00:21:41,980 Anybody remember it? 197 00:21:42,220 --> 00:21:43,340 Yeah? 198 00:21:43,560 --> 00:21:44,360 Surely? 199 00:21:44,360 --> 00:21:45,020 No. 200 00:21:45,020 --> 00:21:46,460 Let's see. 201 00:21:46,740 --> 00:21:52,440 Surely email parsing libraries won't allow you to use a UTF-7 charset? 202 00:21:52,850 --> 00:21:54,560 Of course they do. 203 00:21:54,560 --> 00:21:59,820 Using the Ruby library mail, here is a UTF-7 encoded address. 204 00:22:00,040 --> 00:22:03,700 So this is a UTF-7 foo bar. 205 00:22:03,700 --> 00:22:05,400 You'll notice the ampersand. 206 00:22:05,400 --> 00:22:09,730 That's intentional because that's the character that they support for UTF-7. 207 00:22:10,460 --> 00:22:17,610 So the charset will be decoded and the email will be sent to foobar at psres.net. 208 00:22:18,340 --> 00:22:20,230 I know what you're thinking. 209 00:22:20,230 --> 00:22:22,270 Or maybe it's just me. 210 00:22:22,270 --> 00:22:28,130 But yes, you can use UTF-7 and Base64 in an email address. 211 00:22:28,660 --> 00:22:35,220 In this example, there's a Base64 email address with a UTF-7 charset. 212 00:22:36,030 --> 00:22:39,820 First, the email parser will decode Base64. 213 00:22:40,190 --> 00:22:45,170 Then the email parser will decode the UTF-7 charset. 214 00:22:45,170 --> 00:22:48,110 Finally, the decoded address will be sent there. 215 00:22:48,110 --> 00:22:49,250 Absolutely crazy. 216 00:22:49,250 --> 00:22:53,910 And I'll emphasize the point that any character can be encoded. 217 00:22:53,910 --> 00:22:56,750 I'm just using foobar to make it simple to follow. 218 00:22:58,590 --> 00:23:02,970 Then I thought, what about UTF-7 and queue encoding? 219 00:23:02,970 --> 00:23:08,650 So here I'm going to modify the A character and replace it with equals 41. 220 00:23:08,650 --> 00:23:17,330 This will then get decoded and then it will be decoded and form a UTF-7 encoded foobar. 221 00:23:17,330 --> 00:23:19,810 So it's going through two layers of encoding. 222 00:23:19,810 --> 00:23:24,310 At this point, you might start having a few doubts about following the RFC. 223 00:23:24,990 --> 00:23:29,210 Especially when I tell you that this works in the domain part 2. 224 00:23:29,210 --> 00:23:35,710 And when I tested the Ruby mail library, I know what you're thinking. 225 00:23:37,190 --> 00:23:39,570 It's... sorry, I've lost track. 226 00:23:40,070 --> 00:23:41,570 I know what you're thinking. 227 00:23:41,570 --> 00:23:44,830 No website is going to support UTF-7, right? 228 00:23:44,830 --> 00:23:46,950 No website is going to support that. 229 00:23:47,070 --> 00:23:52,890 But actually, there was a small source code web hosting site called GitHub. 230 00:23:52,890 --> 00:23:55,390 They supported UTF-7. 231 00:23:55,490 --> 00: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. 232 00:24:01,990 --> 00:24:06,150 And then, yeah, it'll go to that email address. 233 00:24:06,150 --> 00:24:07,930 So foobar.psrez.net. 234 00:24:09,990 --> 00:24:14,170 So then I went hunting for encoded word outside of Ruby. 235 00:24:14,170 --> 00:24:20,130 And I found that phpMailer supports encoded word within the name of the recipient. 236 00:24:20,430 --> 00: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. 237 00:24:29,710 --> 00:24:35,150 Still, I bet there are vulnerable systems that decode the actual name of the recipient. 238 00:24:35,650 --> 00:24:39,190 So you may find this in bug bounty sites. 239 00:24:41,990 --> 00:24:48,150 So we've explored how you can manipulate email parsing to sidestep access controls. 240 00:24:48,150 --> 00:24:49,810 But shall we take it a bit further? 241 00:24:49,810 --> 00:24:55,730 What if we could use an email address and weaponize it to gain remote code execution? 242 00:24:56,230 --> 00:25:01,230 In this section, we'll cover PuniCode attacks and how I exploited Joomla. 243 00:25:02,790 --> 00:25:08,770 So PuniCode is a way to represent Unicode characters in the current DNS system. 244 00:25:08,770 --> 00:25:13,470 PuniCode always starts with xn and is followed by two hyphens. 245 00:25:13,470 --> 00:25:21,980 The algorithm converts the sequence of Unicode characters into a representation that utilizes only alphanumeric characters and hyphens. 246 00:25:22,350 --> 00: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. 247 00:25:30,330 --> 00: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. 248 00:25:42,810 --> 00:25:48,030 So what we want to do is generate some malicious characters that when it's decoded... 249 00:25:49,110 --> 00:25:54,390 Sorry, what we want to do is generate malicious characters when it's decoded. 250 00:25:54,390 --> 00:25:56,510 But doing so is a big challenge. 251 00:25:56,510 --> 00:26:01,730 In these examples, you can see the Unicode character changes when one byte is modified. 252 00:26:01,730 --> 00:26:04,700 This makes it difficult to generate a malicious payload. 253 00:26:06,130 --> 00:26:14,440 So after reading about PuniCode on Wikipedia, I followed a link to an online PuniCode converter and I began to experiment. 254 00:26:14,950 --> 00:26:18,530 The converter used the IDN PHP library. 255 00:26:18,530 --> 00:26:25,250 I first started to add zeros to the beginning of the PuniCode address to convert it into ASCII characters. 256 00:26:25,250 --> 00:26:31,070 So I initially succeeded generating a comma, which was so surprising I doubted my eyes. 257 00:26:31,110 --> 00:26:35,890 Encouraged by this, I started to experiment further and managed to produce an art symbol. 258 00:26:37,670 --> 00:26:43,430 So after searching GitHub, I found an interesting target using the library, Joomla. 259 00:26:43,430 --> 00:26:46,990 This was great because if I got XSS, then I would have RCE. 260 00:26:47,390 --> 00:26:54,070 Doing a source code analysis, I noticed they were escaping the email before it was PuniCode decoded. 261 00:26:54,070 --> 00:27:01,310 This means if I could produce some malformed PuniCode, that when decoded produces HTML, I could get XSS. 262 00:27:01,310 --> 00:27:02,730 But it wouldn't be that easy. 263 00:27:04,070 --> 00:27:05,270 Sound simple? 264 00:27:05,270 --> 00:27:09,710 Just give the decoder some malformed PuniCode and generate some HTML. 265 00:27:09,750 --> 00:27:12,010 But remember how the algorithm works. 266 00:27:12,010 --> 00:27:16,150 Any change in the character can result in a completely different output. 267 00:27:16,190 --> 00:27:21,350 I thought about this for a while and I thought that this was a good job for a fuzzer. 268 00:27:21,350 --> 00:27:27,230 So I wrote a fuzzer to generate malformed PuniCode and the results were interesting. 269 00:27:27,570 --> 00:27:30,450 I found many ways to generate different characters. 270 00:27:30,450 --> 00:27:32,410 So you can see what I was trying to do here. 271 00:27:32,410 --> 00:27:34,650 I'm trying to generate an SVG type. 272 00:27:36,210 --> 00:27:40,450 So I started generating millions of different character combinations. 273 00:27:40,450 --> 00:27:45,310 I managed to construct partial XSS vectors, but I encountered several issues. 274 00:27:45,310 --> 00:27:50,510 I could only generate two ASCII characters by using more than one PuniCode subdomain. 275 00:27:50,510 --> 00:28:01,110 This limitation arose from the specific workings of the PuniCode algorithm PHP and the quirks of this buggy PHP IDN library. 276 00:28:01,110 --> 00:28:04,230 As you can see in the examples, I was really close. 277 00:28:04,230 --> 00:28:07,850 But these problems made exploiting Joomla very difficult. 278 00:28:07,850 --> 00:28:10,880 I concluded that XSS was not feasible. 279 00:28:11,820 --> 00:28:18,710 Because although I was able to generate a single quoted attribute, it required an underscore character. 280 00:28:18,710 --> 00:28:25,510 Joomla, however, does not permit underscore characters in the domain part of the email address. 281 00:28:25,750 --> 00:28:31,030 So you can see those mad PuniCode addresses are trying to inject some HTML. 282 00:28:32,250 --> 00:28:34,470 So was that the end of the story? 283 00:28:34,470 --> 00:28:35,610 Not quite. 284 00:28:36,110 --> 00:28:38,090 I thought about this for a while. 285 00:28:38,090 --> 00:28:44,330 And I worked out that you could use a single PuniCode subdomain and you could generate any opening tag. 286 00:28:44,330 --> 00:28:51,370 Eventually, after a lot of testing, I concluded the only exploitable vector was an opening style tag. 287 00:28:51,510 --> 00:28:54,670 Here I use malform PuniCode to construct it. 288 00:28:54,710 --> 00:28:56,370 It looks like a test string, right? 289 00:28:56,370 --> 00:28:58,750 But this is actually how it works. 290 00:28:58,750 --> 00:29:00,390 Style 1, 2, 3. 291 00:29:00,390 --> 00:29:01,750 And 3, 2, 1, sorry. 292 00:29:01,750 --> 00:29:03,650 And that would generate a style tag. 293 00:29:05,050 --> 00:29:12,310 The rest of the pre-existing Joomla HTML code would add the space and the closing angle bracket for me. 294 00:29:12,310 --> 00:29:18,530 So it injects this style tag and then the rest of the HTML adds those characters for me. 295 00:29:19,650 --> 00:29:22,050 So what I'd need is two accounts. 296 00:29:22,050 --> 00:29:27,710 The first registers with a style tag with malform PuniCode as demonstrated. 297 00:29:27,710 --> 00:29:34,650 The second account has the actual CSS payload inside the name of the user. 298 00:29:34,670 --> 00:29:37,470 That imports the malicious style sheet. 299 00:29:37,470 --> 00:29:41,990 The first account starts with A and the second account starts with X. 300 00:29:41,990 --> 00:29:49,550 This is to ensure the style injection occurs first and then the payload with the import occurs second. 301 00:29:49,870 --> 00:29:52,170 Notice I'm using curly braces. 302 00:29:52,170 --> 00:29:58,650 This is to treat all the HTML that occurs before the import as an invalid CSS selector. 303 00:29:58,650 --> 00:30:02,170 And this still works because this is CSS, right? 304 00:30:02,170 --> 00:30:03,450 It'll just work. 305 00:30:05,930 --> 00:30:10,430 So the first step is to register two accounts for the attacker. 306 00:30:10,430 --> 00:30:15,950 The first with the malicious PuniCode and the second with the evil CSS. 307 00:30:16,050 --> 00:30:22,630 So here we're using the malform PuniCode in the email and the second account in the name. 308 00:30:25,270 --> 00: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. 309 00:30:36,670 --> 00: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. 310 00:30:45,650 --> 00:30:48,470 The screenshot shows this process happening. 311 00:30:51,180 --> 00:30:59,140 Using the exfiltrated token, the attacker then uses the CSS exfiltrator and gives the admin a URL. 312 00:30:59,140 --> 00: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. 313 00:31:12,480 --> 00:31:15,880 Now, James said I should do a live demo. 314 00:31:15,980 --> 00:31:19,260 I wasn't as keen, but let's give it a shot. 315 00:31:25,290 --> 00:31:31,070 Right, so on the left is the victim, so it's the admin on the left. 316 00:31:31,070 --> 00:31:33,070 On the right is the attacker. 317 00:31:33,150 --> 00:31:39,210 We've also got the exfiltrator running and this is where the token will be extracted. 318 00:31:39,930 --> 00:31:43,030 So the attacker visits the page. 319 00:31:43,110 --> 00:31:46,710 I've pre-filled the data because you don't want to see me typing everything. 320 00:31:47,530 --> 00:31:51,810 And the email address contains a malform punicode. 321 00:31:52,430 --> 00:31:57,410 So I get an error and that error is just because I haven't configured mail on the server. 322 00:31:58,270 --> 00:32:06,690 And then you register a second account and this is with the actual payload in that exfiltrates the token. 323 00:32:09,710 --> 00:32:12,030 So the two accounts are registered. 324 00:32:12,630 --> 00:32:17,470 Now let's log in as the admin and pray to the demo gods. 325 00:32:22,510 --> 00:32:25,630 Right, thank 326 00:32:29,020 --> 00:32:30,060 you very much. 327 00:32:30,060 --> 00:32:32,960 But we still need to send the message to the admin, right? 328 00:32:33,220 --> 00:32:35,040 Hey admin, how's your day going? 329 00:32:35,040 --> 00:32:36,860 Yeah, going good, living the dream. 330 00:32:36,860 --> 00:32:38,300 Let's click on this. 331 00:32:41,650 --> 00:32:43,850 So I'm showing you a real attack. 332 00:32:43,850 --> 00: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. 333 00:32:56,210 --> 00:32:58,650 Thank you, thank you. 334 00:33:09,040 --> 00:33:14,540 So whilst conducting this research, I found it useful to follow the following methodology. 335 00:33:14,540 --> 00:33:17,780 Probe, observe, encode, exploit. 336 00:33:17,840 --> 00:33:20,340 First probe for encoded word. 337 00:33:20,340 --> 00:33:24,500 Observe that it will be decoded to confirm that it's supported. 338 00:33:24,520 --> 00:33:28,240 Then encode the various characters and observe how they're decoded. 339 00:33:28,240 --> 00:33:31,460 And then follow with an exploit that abuses these characters. 340 00:33:31,620 --> 00:33:34,820 So this methodology is spelt out here. 341 00:33:34,820 --> 00:33:37,300 So these are the steps you can take to exploit. 342 00:33:37,300 --> 00:33:41,400 And this is some of the steps I use to exploit these targets. 343 00:33:43,760 --> 00:33:48,180 Hackverter is a free open source tool that I wrote for the burp suite. 344 00:33:48,180 --> 00:33:56,700 I've crafted some hackverter tags to help you generate unicode overflows and encoded word attacks easily within burp. 345 00:33:56,720 --> 00:34:01,560 Turbo intruder is another free burp extension written by James Kettle. 346 00:34:01,560 --> 00:34:05,180 I've created a turbo intruder script to exploit the mailers. 347 00:34:05,180 --> 00:34:10,040 So you remember when github supported null for example, this was found with turbo intruder. 348 00:34:10,360 --> 00:34:15,520 I've also created a punicode fuzzer to help with malformed punicode. 349 00:34:15,520 --> 00: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. 350 00:34:25,300 --> 00:34:29,340 It was really effective in finding what characters could be generated. 351 00:34:29,340 --> 00:34:34,900 We've also updated burp intruder word list with a list of these attacks too. 352 00:34:35,380 --> 00:34:37,080 For your convenience. 353 00:34:38,560 --> 00:34:44,940 To defend against email splitting attacks, make sure you filter out or disable encoded word. 354 00:34:44,980 --> 00:34:51,120 Always verify email addresses before using them, even when received from an SSO provider. 355 00:34:51,280 --> 00: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. 356 00:35:01,140 --> 00:35:05,980 A few blog posts were really inspirational when conducting this research. 357 00:35:05,980 --> 00:35:11,280 I recommend you read each one because they are really useful and contain really useful information. 358 00:35:11,280 --> 00:35:17,840 The import chaining technique to use to exfiltrate the token is from Petvilla and Donut, awesome researchers. 359 00:35:19,720 --> 00:35:29,480 So the three takeaways that I want to leave you with are valid email addresses that can trigger major parser discrepancies. 360 00:35:29,480 --> 00: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. 361 00:35:41,220 --> 00:35:47,920 We've created a web security academy CTF so you can try and do an email splitting attack. 362 00:35:47,920 --> 00:35:54,580 I've also created a docker file with the vulnerable Joomla installation for your convenience. 363 00:35:54,580 --> 00:36:02,980 You can get all the materials and download all the materials for this presentation on the GitHub repository. 364 00:36:02,980 --> 00:36:05,300 Thank you very much DEF CON.