1 00:00:00,000 --> 00:00:05,840 Welcome, everyone. My name is Michael Stepankin. In this session, we'll dive into Mutual TLS 2 00:00:05,840 --> 00:00:12,220 and authentication based on certificates. I'm excited to present some interesting attacks on 3 00:00:12,220 --> 00:00:18,840 MTLS, as well as demonstrate vulnerabilities in popular open source software. My purpose 4 00:00:18,840 --> 00:00:23,760 is to give you some fresh ideas on how to attack and secure this technology. 5 00:00:23,760 --> 00:00:32,740 So, what is Mutual TLS? In short, it's another form of client authentication. Most of you probably 6 00:00:32,740 --> 00:00:38,900 know that the browser checks a server certificate. The same idea can be applied to authenticate 7 00:00:38,900 --> 00:00:45,840 browsers or TLS clients in general. For example, a server can ask a browser to provide a valid 8 00:00:45,840 --> 00:00:51,120 certificate. It can be either used as an alternative to classic password authentication, 9 00:00:51,520 --> 00:00:53,640 or as an additional factor. 10 00:00:53,760 --> 00:01:02,020 This process is defined in the TLS RFC and based on asymmetric cryptography. During the handshake, 11 00:01:02,180 --> 00:01:09,660 the server can send a certificate request message. The client replies with a certificate message that 12 00:01:09,660 --> 00:01:15,880 contains a public key. This is followed by a certificate verify message encrypted by a private 13 00:01:15,880 --> 00:01:22,920 key, so that even if the message is intercepted, a person in the middle would not see any secrets 14 00:01:22,920 --> 00:01:23,740 in the place. 15 00:01:25,640 --> 00:01:33,040 So, what exactly is a certificate? Generally speaking, it's a blob of binary encoded data. 16 00:01:33,380 --> 00:01:40,080 The most important fields are the subject, the issuer, and the public key. The subject usually 17 00:01:40,080 --> 00:01:47,020 defines a username or hostname. Similar to JSON Web Tokens, the certificate is signed by the 18 00:01:47,020 --> 00:01:53,540 issuer's private key, so the data inside cannot be tampered with. The issue field points to a 19 00:01:53,540 --> 00:01:56,280 another certificate who signed this certificate. 20 00:01:58,220 --> 00:02:01,500 A sequence of certificates signing each other 21 00:02:01,500 --> 00:02:06,320 forms a certificate chain. In order to check the validity of the certificate, 22 00:02:06,640 --> 00:02:10,760 the server should build a trusted chain from the client certificate 23 00:02:11,300 --> 00:02:16,040 to a trusted root certificate. The client and intermediate certificates are 24 00:02:16,040 --> 00:02:20,740 normally sent by the clients whereas the server only needs to know 25 00:02:20,740 --> 00:02:22,680 about trusted root certificates. 26 00:02:24,420 --> 00:02:29,220 Let's see how to use MTLS in an example Java web application. 27 00:02:29,840 --> 00:02:34,560 In the bare minimum, the developers only need to enable it in the application 28 00:02:34,560 --> 00:02:38,480 settings and specify the location of all trusted 29 00:02:38,480 --> 00:02:42,300 root certificates. From the client perspective, such as 30 00:02:42,300 --> 00:02:46,520 cURL, you need to specify what certificate is sent to the server. 31 00:02:46,920 --> 00:02:50,460 The rest of the application code, such as request mapping, 32 00:02:50,460 --> 00:02:53,600 is exactly the same as for normal web 33 00:02:53,600 --> 00:02:54,000 application. 34 00:02:56,540 --> 00:03:01,320 As you can see, this approach brings some advantages and disadvantages compared to 35 00:03:01,320 --> 00:03:06,820 traditional authentication methods, such as password. First of all, it might be 36 00:03:06,820 --> 00:03:10,680 faster for your application as the authentication can only happen 37 00:03:10,680 --> 00:03:15,660 at the handshake level, so all subsequent HTTP requests will be 38 00:03:15,660 --> 00:03:19,680 authenticated, saving our CPU time. Also, 39 00:03:19,680 --> 00:03:25,320 similar to JSON Web Tokens, there is a storage benefit, as the server does not 40 00:03:25,320 --> 00:03:29,520 need to store all the client certificates, only need to access the 41 00:03:29,520 --> 00:03:35,500 root certificate. Speaking of the cons, there is no granular control. As soon as 42 00:03:35,500 --> 00:03:40,340 you enable MTLS in your application, all requests have to be authenticated, 43 00:03:40,480 --> 00:03:47,880 even to the public pages or JS or CSS resources. Also, any certificates signed 44 00:03:47,880 --> 00:03:49,660 by a root certificate can be authenticated. 45 00:03:49,680 --> 00:03:51,000 For example, the MUTL-TLS can be used for client authentication. 46 00:03:51,560 --> 00:03:55,880 Even if the certificate is issued for another purpose, 47 00:03:56,180 --> 00:03:59,500 it still can potentially be used for TLA authentication. 48 00:04:00,620 --> 00:04:04,060 Furthermore, the MUTL-TLS does not specify 49 00:04:05,420 --> 00:04:10,300 how this certificate should be issued. It's up to the application to define 50 00:04:10,300 --> 00:04:13,820 additional mechanism for certificate issuance. 51 00:04:14,380 --> 00:04:18,060 Finally, certificates are always expired, 52 00:04:18,060 --> 00:04:21,240 and it's just a hassle to rotate them frequently. 53 00:04:24,280 --> 00:04:26,460 Before we dive into the attack section, 54 00:04:27,390 --> 00:04:31,880 I'm going to briefly mention some of the previous well-known attacks on 55 00:04:31,880 --> 00:04:35,960 certificate validation. Obviously, the security of the 56 00:04:35,960 --> 00:04:38,560 authentication system depends on the strength 57 00:04:38,560 --> 00:04:43,600 of the signature. If we can somehow forge the content of the certificate, 58 00:04:43,920 --> 00:04:47,620 but keep the same signature, we can completely break the 59 00:04:47,620 --> 00:04:53,520 authentication process. Also, since X.509 format is quite complex, 60 00:04:54,220 --> 00:04:57,860 just parsing these data structures can lead to buffer 61 00:04:57,860 --> 00:05:04,200 and heap overflows. In addition, lack of basic constraint checking can 62 00:05:04,200 --> 00:05:06,720 also be used to bypass the authentication process. 63 00:05:09,100 --> 00:05:12,260 I've got to say that in my research, I mostly looked at 64 00:05:12,260 --> 00:05:16,360 how MTLS is implemented in applications written in Java. 65 00:05:17,620 --> 00:05:21,800 In Java Development Kit, most of these attacks are already mitigated on the 66 00:05:21,800 --> 00:05:27,180 language level, so I had to take a different approach. At the same time, 67 00:05:27,220 --> 00:05:31,160 the attacks I'm going to describe are also applicable to other languages. 68 00:05:35,260 --> 00:05:39,680 All right, let's move on to the attack section. In the first chapter, 69 00:05:40,260 --> 00:05:42,640 we'll cover improper certificate extraction. 70 00:05:45,680 --> 00:05:51,180 In real-life applications, developers often need to access the subject name from the 71 00:05:51,180 --> 00:05:56,840 certificate presented during the TLX handshake. For example, they may need it for authorization 72 00:05:56,840 --> 00:06:06,860 needs to understand what is the current username. In Java, you can use the SSLSession.getPairCertificates 73 00:06:06,860 --> 00:06:12,620 method. Interestingly, this method returns an array of certificates, not a single one. 74 00:06:12,620 --> 00:06:20,660 Why? Perhaps because TLS specification defines that clients may send a full chain of certificates, 75 00:06:21,100 --> 00:06:28,280 not only one. So, I decided to take a look at how different applications use this API. 76 00:06:28,720 --> 00:06:33,400 The most common approach I've seen is to take only the first certificate from the array 77 00:06:33,400 --> 00:06:41,800 and consider it as a client certificate. This is correct. As MTLS RFC explicitly says, 78 00:06:42,620 --> 00:06:48,700 the sender certificate must come first in the list. At the same time, I discovered some rare 79 00:06:48,700 --> 00:06:55,320 cases when applications disregard this rule and iterate over the array trying to find a certificate 80 00:06:55,320 --> 00:06:59,120 that matches some criteria. This can be dangerous, 81 00:06:59,440 --> 00:07:04,720 as the underlying TLS library in Java only verifies the first certificate from the list. 82 00:07:07,940 --> 00:07:12,020 One of these examples was of vulnerability I discovered in KeyClock. 83 00:07:12,620 --> 00:07:20,220 KeyClock is a popular authorization server that supports auth, SAML, and other authorization 84 00:07:20,220 --> 00:07:26,520 methods, as well as Mutual TLS. KeyClock iterates over all certificates in the array, 85 00:07:26,680 --> 00:07:32,640 searching for the one that matches the client ID form parameter. As soon as it found one certificate, 86 00:07:33,000 --> 00:07:40,880 it implicitly trusts it, assuming that its signature has already been checked during the TLS handshake. 87 00:07:41,600 --> 00:07:42,480 In reality, 88 00:07:42,940 --> 00:07:50,160 a client can send as many certificates as they want, so the server only verifies the first one. 89 00:07:55,360 --> 00:07:59,540 A potential attacker can exploit this behavior to authenticate 90 00:07:59,540 --> 00:08:04,140 under a different username. It is possible to send a list of certificates, 91 00:08:04,460 --> 00:08:11,060 where the first one contains one username and is properly chained to a root CA. The last certificate 92 00:08:12,620 --> 00:08:16,800 will be issued to the client and belongs to a different user. The client does not even need to 93 00:08:16,800 --> 00:08:22,620 provide a valid private key for it. There are a number of endpoints in KeyClock 94 00:08:22,620 --> 00:08:28,840 that support MTL authentication. For exploitation, we need one that does not require 95 00:08:28,840 --> 00:08:32,140 any additional factors, such as tokens or secrets. 96 00:08:33,020 --> 00:08:41,100 Client Management Register node is a good example, as it mutates users' data. In the first comment, 97 00:08:41,400 --> 00:08:48,640 it shows how we can normally use this API with MTLS. In the second request, we generate a new 98 00:08:48,640 --> 00:08:56,260 self-signed certificate using OpenSSL and add it to the chain. When we send the second curl request, 99 00:08:56,760 --> 00:09:04,300 KeyClock will perform this action on behalf of the user specified in certificate 2 instead of 100 00:09:04,860 --> 00:09:10,700 certificate 1. Therefore, we can mutate data on the server for any client, 101 00:09:10,700 --> 00:09:19,840 that supports MTLS. How to fix that? Easy. Just use the first certificate from the array. 102 00:09:20,160 --> 00:09:26,320 That's exactly how KeyClock patched this vulnerability. It's a good example of how 103 00:09:26,320 --> 00:09:32,740 developers provide methods and interfaces that can be misunderstood or used incorrectly. 104 00:09:35,740 --> 00:09:40,680 Another common scenario for MTLS deployments is when TLS connections are not 105 00:09:40,700 --> 00:09:46,960 available on the reverse proxy. In this case, the reverse proxy often checks the certificate 106 00:09:47,340 --> 00:09:51,160 and forwards it to a backend server as an additional header. 107 00:09:51,420 --> 00:09:56,000 On this slide, you can see a typical NGIN configuration to enable 108 00:09:56,000 --> 00:10:01,920 MTLS. I've seen a number of systems like that, and in most cases, backend servers 109 00:10:03,320 --> 00:10:10,100 behind NGINs do not perform additional validation, trusting the reverse proxy. This behavior is not 110 00:10:10,100 --> 00:10:15,720 exploitable straight away, but it's not ideal either. Why? Well, first of all, 111 00:10:15,740 --> 00:10:20,300 it means that any server in the local network can make requests with the header, 112 00:10:20,500 --> 00:10:26,120 so the network segment should be properly isolated from the traffic coming from outside. 113 00:10:27,280 --> 00:10:33,360 Additionally, if your backend or reverse proxy is affected by a request smuggling vulnerability, 114 00:10:34,120 --> 00:10:40,080 its exploitation becomes trivial. Over the past few years, we've seen a lot of requests and 115 00:10:40,100 --> 00:10:46,500 headers smuggling vulnerabilities, including the latest CVEs in Apache and Node.js. 116 00:10:47,260 --> 00:10:54,160 Be careful when implementing these scenarios and check the certificate signature on all servers. 117 00:10:57,580 --> 00:11:02,040 Moving on to the next chapter, follow the chain where it leads you. 118 00:11:05,120 --> 00:11:09,760 In large systems, servers may not store all root and intermediate 119 00:11:10,100 --> 00:11:16,940 certificates locally, but use an external storage instead. RFC defines the concept of a 120 00:11:16,940 --> 00:11:23,340 certificate store, an interface you can use to lazily access certificates during chain validation. 121 00:11:24,280 --> 00:11:31,640 These stores are implemented over different protocols, such as HTTP, LDAP, FTP, or SQL queries. 122 00:11:32,500 --> 00:11:39,200 The X.509 format defines some certificate extensions that can contain information where to 123 00:11:39,200 --> 00:11:44,980 find the issuer and CA certificates. For instance, the Authority Information Access 124 00:11:44,980 --> 00:11:52,740 extension contains a URL pointing to the issuer's certificate. If this extension is used for 125 00:11:52,740 --> 00:11:59,400 validation, there is a high chance that you can exploit it to perform an SSRF attack. 126 00:12:00,120 --> 00:12:08,920 Also, subject, issue, serial, and their alternative names can be used to construct SQL and LDAP queries. 127 00:12:09,200 --> 00:12:11,280 Leaving options for injections attack. 128 00:12:11,840 --> 00:12:16,600 When certificate stores are in use, you want to think about these values 129 00:12:17,280 --> 00:12:22,040 as insertion points, similar to those ones we have in Burp Suite Intruder. 130 00:12:23,320 --> 00:12:25,500 And what's really good for attackers, 131 00:12:26,340 --> 00:12:30,720 all of these values can be used in queries before the signature is checked. 132 00:12:35,020 --> 00:12:37,700 To demonstrate an example of this vulnerability, 133 00:12:37,700 --> 00:12:41,840 we'll use an LDAP search store from the Bouncy Castle library. 134 00:12:42,320 --> 00:12:47,840 Bouncy Castle is one of the most popular libraries for certificate validation in Java. 135 00:12:48,120 --> 00:12:53,680 Here is an example of how you can use this store to build and validate a certificate chain. 136 00:12:55,140 --> 00:13:01,560 Under the hood, Bouncy Castle uses a subject field from the certificate to build an LDAP query. 137 00:13:02,000 --> 00:13:07,680 The subject field is inserted in the filter and, as you can imagine, without any changes. 138 00:13:07,680 --> 00:13:15,000 So, if the subject contains any special characters, it can change the syntax of the LDAP query. 139 00:13:15,280 --> 00:13:19,740 In most cases, this can be exploited as a blind LDAP query injection. 140 00:13:20,420 --> 00:13:27,720 Therefore, it could be possible to use this vulnerability to extract other fields from the LDAP directory. 141 00:13:28,080 --> 00:13:33,880 The exploitation is based on many factors, including whether the application exposes any errors, 142 00:13:34,120 --> 00:13:37,660 and it also depends on the structure of the LDAP directory. 143 00:13:39,060 --> 00:13:45,760 Anyway, whenever you incorporate user-supplied data into a LDAP query, 144 00:13:46,220 --> 00:13:48,800 special characters should be properly filtered. 145 00:13:49,240 --> 00:13:53,800 That's exactly how this CVE has been patched in the Bouncy Castle code. 146 00:13:54,600 --> 00:13:56,900 Next, we'll cover revocation. 147 00:13:57,440 --> 00:14:01,320 This controversial topic sparked many debates over the years. 148 00:14:02,600 --> 00:14:07,640 Similar to JSON Web Tokens, the beauty of certificate chains is that they are not limited to a single domain. 149 00:14:07,640 --> 00:14:10,440 They can be trusted just based on their signature. 150 00:14:10,920 --> 00:14:15,860 But what happens if we need to revoke a certificate so it can no longer be used? 151 00:14:17,540 --> 00:14:23,740 The PKI specification addresses this problem by proposing a special store for revoked certificates, 152 00:14:24,060 --> 00:14:27,340 accessible via HTTP or LDAP protocols. 153 00:14:28,160 --> 00:14:33,200 Certificate verifiers can use two different methods, OACP or CRL. 154 00:14:34,200 --> 00:14:36,860 Many developers believe that revocation checking 155 00:14:36,860 --> 00:14:42,420 is absolutely necessary, whereas others urge to avoid it for performance reasons 156 00:14:42,690 --> 00:14:44,680 or use offline revocation lists. 157 00:14:45,180 --> 00:14:50,680 Generally speaking, the store location can be hard-coded into the application 158 00:14:51,010 --> 00:14:53,160 or taken from the certificate itself. 159 00:14:53,920 --> 00:14:56,720 There are two certificate extensions used for that, 160 00:14:57,020 --> 00:15:00,880 authority information access and CRL distribution points. 161 00:15:02,160 --> 00:15:04,680 Looking at it from the hacker's point of view, 162 00:15:04,680 --> 00:15:09,480 I think it's incredible that the location of the revocation server 163 00:15:09,920 --> 00:15:11,500 can be taken from the certificate. 164 00:15:12,420 --> 00:15:18,440 So if the application takes URLs relying on extensions to make a revocation check, 165 00:15:18,880 --> 00:15:21,120 it can be abused for SSRF attacks. 166 00:15:21,720 --> 00:15:26,240 Sadly for attackers, this normally happens after the signature checks, 167 00:15:26,420 --> 00:15:28,700 but in some cases it's still exploitable. 168 00:15:29,280 --> 00:15:33,780 Moreover, the LDAP protocol is also supported, at least in Java. 169 00:15:36,200 --> 00:15:39,400 Wait, did I hear LDAP in Java? 170 00:15:40,280 --> 00:15:40,640 Yes. 171 00:15:40,860 --> 00:15:46,920 The standard Java library can use JNDI to access revocation locations under some settings. 172 00:15:47,540 --> 00:15:48,900 You've probably heard that in Java, 173 00:15:48,980 --> 00:15:53,420 in-marshaling an LDAP lockup response can lead to a remote code execution. 174 00:15:54,240 --> 00:15:58,340 A few years back, this problem was reported by Moritz Bechler, 175 00:15:58,540 --> 00:16:03,340 and remote code execution via revocation has been patched in GDK. 176 00:16:03,340 --> 00:16:06,660 You can check out his blog post for more details. 177 00:16:08,600 --> 00:16:13,180 In my research, I decided to check if bouncy castle library is also affected. 178 00:16:13,660 --> 00:16:17,620 It turned out that the bouncy castle can use the CRLDP extension 179 00:16:18,100 --> 00:16:20,060 and make calls to an LDAP server. 180 00:16:20,440 --> 00:16:26,140 At the same time, bouncy castle only fetches a specific attribute from the LDAP response 181 00:16:26,140 --> 00:16:28,340 and does not support references. 182 00:16:29,060 --> 00:16:32,900 So remote code execution is sadly not possible there. 183 00:16:34,160 --> 00:16:36,380 HTTPSSRF is still a viable thing, though. 184 00:16:38,280 --> 00:16:44,540 I also had a quick look at open-source projects that support MTLS and perform revocation checking. 185 00:16:44,840 --> 00:16:47,160 One of these projects was Apirio CAS. 186 00:16:47,480 --> 00:16:52,140 It's another popular authentication server that is highly configurable. 187 00:16:53,140 --> 00:16:56,980 Administrators of Apirio CAS can enable the revocation check 188 00:16:56,980 --> 00:17:02,920 using the external LDAP server by specifying its address and password in the settings. 189 00:17:03,960 --> 00:17:09,600 If these settings are applied, Apirio CAS performs the revocation check for the certificate, 190 00:17:09,920 --> 00:17:13,840 fetching the address from the certificate's CRLDP extension. 191 00:17:14,660 --> 00:17:19,000 I was afraid that this could also lead to remote code execution, 192 00:17:19,460 --> 00:17:24,640 but it turned out that Apirio CAS uses a custom library for LDAP connection, 193 00:17:24,920 --> 00:17:30,640 which does not support external code bases or object factories needed for RCE. 194 00:17:33,980 --> 00:17:38,520 When I tested this in Apirio CAS, I noticed one interesting behavior. 195 00:17:38,900 --> 00:17:43,920 The server prefers LDAP URLs located inside the certificate, 196 00:17:44,260 --> 00:17:47,180 instead the one that is configured in the settings. 197 00:17:47,680 --> 00:17:51,820 At the same time, Apirio CAS still sends the password from the settings. 198 00:17:52,240 --> 00:17:57,500 I quickly set up a testing environment and send a self-signed certificate in the header. 199 00:17:57,840 --> 00:18:03,320 This certificate has CRLDP extension with LDAP URL pointing to a netcat list. 200 00:18:03,340 --> 00:18:06,560 After sending this request to Apirio CAS, 201 00:18:06,780 --> 00:18:13,160 I managed to receive a request to my netcat listener with a username and password leaked. 202 00:18:14,860 --> 00:18:20,900 After reporting this vulnerability, the application developers issued a fix within just 203 00:18:20,900 --> 00:18:25,760 one day. They patched it by clearing the login and password from LDAP connection. 204 00:18:26,540 --> 00:18:33,120 Therefore, the password leak is no longer possible. But in general, I'd say that you 205 00:18:35,060 --> 00:18:37,160 should be careful with this. 206 00:18:37,160 --> 00:18:48,020 To summarize, when you develop an MTLS system or perform a security assessment, I suggest 207 00:18:49,820 --> 00:18:54,520 First, pay attention when extracting user names from the MTLS chain, 208 00:18:54,720 --> 00:18:58,440 as the server only verifies the first certificate in the chain. 209 00:18:58,700 --> 00:19:03,320 Second, use certificate stores with caution. It can lead to else. 210 00:19:03,340 --> 00:19:10,300 up and SQL injections. And last but not least, certificate revocation can lead to SSRF or even 211 00:19:10,300 --> 00:19:16,160 RCE in the worst case. So it's advised to do the revocation check only after all other checks. 212 00:19:17,820 --> 00:19:20,560 And that's it. Thank you for your attention. 213 00:19:20,920 --> 00:19:24,480 The full write-up will be available in our blog at GitHub. 214 00:19:24,480 --> 00:19:25,140 Bye.