00:00:01.401-->00:00:07.541 [Applause] >> Yeah. Hello everybody, uh blessed that you are here. Um my name is Eric um 00:00:07.541-->00:00:12.546 my day job is uh pen testing, code auditing and um yeah want to do uh, want to talk about uh 00:00:14.748-->00:00:21.521 the stuff I did to uh, in my spare time for like the last year. Um the issue I’m 00:00:21.521-->00:00:26.526 presenting are fixed. I have um, I have um talked of endless about them so um now all, there 00:00:30.530-->00:00:36.003 is still some crooks about this um talk more about this later. Um, all the issues are in open 00:00:36.003-->00:00:40.941 source projects so if you are using them, help them finding such issues um read the code. 00:00:40.941-->00:00:45.946 This is not about blaming them about a righted code, it’s like, hey help them. Um, I have um 00:00:49.182-->00:00:55.055 some stuff of proprietary um Smartcard drivers as well but I’m not really interested in 00:00:55.055-->00:01:01.628 debugging in my spare time so um, yeah that’s why I don’t have bugs in them. I know they are 00:01:01.628-->00:01:08.101 there but I didn’t’ debug them. Um, what am I talking about. I’m talking about taking computers 00:01:08.101-->00:01:12.706 with a smartcard. Smartcards are used in a lot of different areas. You have your phones, you 00:01:12.706-->00:01:17.711 have um, ATM machines, um you have cars, you have healthcare environments. I’m most 00:01:20.314-->00:01:24.584 interested in the, the Linux logins or when you’re using a smartcard that’s the same 00:01:24.584-->00:01:29.589 vector, um this is a good idea not to. That’s the question I was asking myself. Um, why would 00:01:32.693-->00:01:38.665 uh you do this? Um, the smartcards are used for authentication process. This um, 00:01:38.665-->00:01:43.670 at least on Linux runs as root. So if you have a back error, it’s game over. Um and you have 00:01:45.839-->00:01:52.179 some uh interesting, just interesting effect that the programmers while writing for 00:01:52.179-->00:01:56.917 the smartcards, always think of the smartcard as this trustworthy highly security 00:01:56.917-->00:02:01.855 device, so they trust it. And the smart might not always be good idea. Um, to have a first 00:02:04.057-->00:02:10.497 uh look at smartcards sec, you have the user which wants login. Um the users using a smartcard 00:02:10.497-->00:02:17.371 for which there is a smartcard driver. And you have a reader which also requires a sm- uh 00:02:17.371-->00:02:23.977 which requires a kind of driver. On the lower layer you have the, the physical smartcard which is 00:02:23.977-->00:02:29.916 highly secure, tamper-proof device. Um contains a processor, some memory and people were 00:02:29.916-->00:02:36.523 always focusing on the smartcard when um uh taking smartcard security and try to glitch it, 00:02:36.523-->00:02:41.528 try to decap it, to read all the keys. Um I went the other way and looked at the softer side. 00:02:43.630-->00:02:48.635 If you are talking to smartcards you need to um some protocols. Uh the, the lowest level is some 00:02:51.505-->00:02:56.510 other APDU’s which you’re sending to the smartcards. It’s uh very simple protocol as you 00:02:58.578-->00:03:03.517 can see. You have a few bytes. Um which say, which um define, the, the kind of um packet you 00:03:07.421-->00:03:12.492 are sending. You have some parameters P1 and P2, then then there’s an angst spot and then 00:03:12.492-->00:03:17.631 you can append some additional data. If you look at the, the bottom there’s a small example. 00:03:17.631-->00:03:22.502 This is basically a command which sends a pin to the smartcard, to unlock the 00:03:22.502-->00:03:27.874 smartcard, in this case, 1, 2, 3, 4. Um so as you can see it’s very simple and the response is 00:03:27.874-->00:03:32.312 very simple as well. We have your payload and then two separate slides which say okay 00:03:32.312-->00:03:38.919 this was successful or we have an error. Or please um, request more data because it’s more than 00:03:38.919-->00:03:45.425 I can fit in a response. On the top of that you have the, the layer which uh controls the 00:03:45.425-->00:03:50.430 reader. Uh the interface in this case is also quite simple you give it a buffer length and it’s 00:03:52.732-->00:03:58.171 the data that’s sent to the cut and to give it a buffer and length and that’s the data you 00:03:58.171-->00:04:04.144 get back from the card. So it’s quite easy. There are some additional API calls to um, 00:04:04.144-->00:04:09.149 control further settings but this is the one, which is relevant for me. Um the API is 00:04:11.218-->00:04:16.223 uh similar Linux and on windows so you can easily mod your drivers and um this is um, this 00:04:19.693-->00:04:24.698 yeah, this is uh more stand-standardized somewhere. On top of that, the next such layer 00:04:29.603-->00:04:35.642 is PKCS11. Uh which is also a platform independent which you can use um for example by your 00:04:35.642-->00:04:40.647 browser to access the certificates which are stored on the card. So um, um, yeah you 00:04:43.450-->00:04:49.222 have common, common interface um which is audible and everything. In this case you have an API 00:04:49.222-->00:04:55.328 call which allows you to find objects on the card. For example, certificates or others. 00:04:55.328-->00:05:01.434 Um, Windows by now has a different interface, um it has a smartcard Minidriver interface. 00:05:01.434-->00:05:08.241 Which basically is quite similar but different. Um, s, um the thing that is interesting um 00:05:08.241-->00:05:14.581 when loading those drivers, as soon as the smartcard was plugged in there was this 00:05:14.581-->00:05:20.287 attribute reset, which is basically a string of bytes which tells the reader or which 00:05:20.287-->00:05:25.292 test, the, the operating system which driver it should load for this card. So um, this ATR is uh 00:05:27.460-->00:05:32.465 specific to the card and usually you cannot change them. So um, that, that means um they are 00:05:37.237-->00:05:42.242 static. Um, this a quick recap of the, the stack. So um, and what we are taking is the APDU 00:05:48.114-->00:05:53.119 part which is a smartcard driver. Um, what does a lock in real smartcard look like? Um on 00:05:55.755-->00:06:00.694 the left you have the, the pen uh authentication module in the middle of the smartcard and on 00:06:03.863-->00:06:09.402 the right is the certification, certificate revocation uh server. Um, first uh the, the 00:06:09.402-->00:06:16.176 computer also smartcards for the certificates it has stored. Um then it picks uh, picks the one, 00:06:16.176-->00:06:23.083 um validated if its from the right s- CA, if the user makes any sense for the system, um and 00:06:23.083-->00:06:28.088 if uh, if it’s valid, m the CRL servers ask um if it’s revoked or um if, if the certificates 00:06:34.094-->00:06:39.099 can be used for authentication. Um the, the check is um, for example um by using lip kernel 00:06:42.936-->00:06:48.275 or something. So if you’re taking those lip kernel runnings right there, so if you have 00:06:48.275-->00:06:53.446 interesting backseat that’s a way to use them. Um uh the next step is to generate some random 00:06:53.446-->00:07:00.086 numbers and these will send to the card and the card will expect it to um, sign those 00:07:00.086-->00:07:06.126 random numbers to prove that it contains the key to the certificate. And after this is 00:07:06.126-->00:07:12.132 done the server, or the computer checks if the signatures correct and if it is, it knows okay this 00:07:12.132-->00:07:17.370 is the letter to the card for this user. And yeah that’s basically your login process. 00:07:17.370-->00:07:22.375 So, I already told you um the, the smartcards driver developers always trust the card. Um, it’s 00:07:25.712-->00:07:30.717 I’m not sure maybe some subconscious thing that if, you really don’t expect it to send 00:07:32.819-->00:07:37.824 something strange or, and that’s when we get in and what I’m trying abuse. Um, those are the 00:07:40.060-->00:07:45.065 projects I’ve found bugs in. Two of them are um Yubico uh projects. Um, the, the patching 00:07:49.803-->00:07:54.808 process with them worked quite fine. Um then there’s smartcard services which is um, 00:07:59.379-->00:08:03.350 [indiscernible] smartcard stack. Um, the, the when I contacted them I told them, hey I send a 00:08:03.350-->00:08:08.288 talk to DefCon and want to talk about it, um it’s coordinated patching and release and 00:08:08.288-->00:08:13.293 everything. Um, they did not answer and after 2 or 3 weeks, there was a reply hey we fixed 00:08:16.029-->00:08:22.469 that first thing and you’re good. So yeah, ask them for embargo but yeah was kind of 00:08:22.469-->00:08:28.141 strange. Um they didn’t even mention that it was like a security barrier for everything. 00:08:28.141-->00:08:34.948 It’s um, yeah. Um, then there’s OpenSC, um I worked really good with them, it’s um the, the 00:08:34.948-->00:08:40.620 classic Linux smartcard driver sect. It’s um, contains drivers for a lot of different cards 00:08:40.620-->00:08:47.027 from a lot of different people. So um a lot of techs found fuzzing this was quite 00:08:47.027-->00:08:52.031 rewarding. Um there were tons of bugs, but um this was also a project which um was very good 00:08:54.234-->00:08:59.906 for [indiscernible] es- especially Frank Morgner, um did a good job of um getting the 00:08:59.906-->00:09:05.879 team there together, um trying to fix everything. Um, the only problem is um, I also 00:09:05.879-->00:09:12.685 coordinated the release for them on like for 4 days ago, but um, nothing happened. I’m not sure 00:09:12.685-->00:09:18.725 why, um they didn’t release anything. I haven’t heard from them since then. Um, so we 00:09:18.725-->00:09:23.730 decide to um, published to try the repository where all the fixes went in today. Um, so you 00:09:25.865-->00:09:32.672 can install that in case you’re affected um and don’t, yeah get to leave it there in limbo but 00:09:32.672-->00:09:38.478 uh, with the known vulner – with the known vulnerabilities in the software without having a patch. 00:09:38.478-->00:09:45.018 Um but I guess so there will be a released in the next few days to address those. Um, I did a 00:09:45.018-->00:09:51.958 lot of fuzzing on OpenSC but it was also like um huge cloud, I only fuzzed certain bugs. So 00:09:51.958-->00:09:57.831 there are certainly more bugs in there to be found. Um, so if you have spare time want to look at 00:09:57.831-->00:10:02.836 this stuff, go for it. Um, what kind of bugs, do uh, did I uh find. Um, for the Apple 00:10:05.705-->00:10:10.710 smartcardservices um, uh for one of the drivers, um we, we have this code, it uh, it’s a loop 00:10:14.681-->00:10:21.120 and it waits um while it loops, until the, um the card sends also server responses and um, if 00:10:21.120-->00:10:26.559 the results start with um it’s um, this stream it says, hey um you need to fetch more data, I 00:10:26.559-->00:10:33.500 have more data for you. And um, the loop until the cards says okay uh all the, or the card no 00:10:33.500-->00:10:38.104 longer says it has more data. And in each loop there retrieve the data from the card and 00:10:38.104-->00:10:45.612 append it to buffer. So if the card never stops saying you fetched everything the card 00:10:45.612-->00:10:50.149 fetch, uh copying everything into the buffer and on and on and on until you have your 00:10:50.149-->00:10:55.755 classic uh, the classic overflow. So, that’s the last thing you’d’ want to have, it’s 00:10:55.755-->00:11:02.028 uh classic memory corruption. Um, for OpenSC this is a back one of the helper tools, not in 00:11:02.028-->00:11:11.070 the main uh, main driver. Um, there’s a buffer of uh, 2 time 48 bytes and they have a 00:11:11.070-->00:11:19.712 variable bufsize, um and um, they request the file from the card and all selected file from 00:11:19.712-->00:11:25.585 the card and the selection process gives you structure which for example contains the 00:11:25.585-->00:11:31.391 size of the card, uh the size of the file. So, what they are doing they’re using uh, the size 00:11:31.391-->00:11:37.330 of the file as a parameter on how much data to request from the card into this fixed size 00:11:37.330-->00:11:44.404 buffer. So yeah, as soon as the files bigger than 2K you have your classic buffer overflow 00:11:44.404-->00:11:52.745 there. And I’m, will show, uh you should always bring like a calculator if you’re talking 00:11:52.745-->00:12:02.388 about buffer overflows. S-, Um, yeah the, what the tools does is um, you can use the tool to read 00:12:02.388-->00:12:08.861 for example. To search, search from the cart. Um there’s a minus R parameter which uh, 00:12:08.861-->00:12:13.600 triggers the code I showed you and yeah, using the buffer overflow you can spoil whatever 00:12:13.600-->00:12:31.751 you want in this case, the calculator. Um, how do I exploit this, um SSA? Um these RTFs 00:12:31.751-->00:12:38.291 which define which driver gets load for a calc is usually static, you can’t change them. 00:12:38.291-->00:12:43.896 Um I’m using a Basiccard which um is a smartcard you can fully control and fully program in a 00:12:43.896-->00:12:53.973 basic. Yeah, it’s 2018 but it’s basic. Um, and uh for this card you can change the ATR. Um, I 00:12:53.973-->00:13:01.314 will release um, um the examples for this later this year. Um other options for exploitation 00:13:01.314-->00:13:08.121 would be the SIMtrace project which is um meant to do man in the middle on a SIM card 00:13:08.121-->00:13:18.464 communication but you can also use it to um, emulate smartcard. And apparently there are also 00:13:18.464-->00:13:22.602 certain Javacards which allow you to change the ATR but so far, I was not able to get my 00:13:22.602-->00:13:27.807 hand on one of them, if somebody knows more about this, um please talk to me. I’m interested. I’m 00:13:27.807-->00:13:34.914 not writing basic anymore. Um, yeah for YUBICO um this is also, this is the part where um the 00:13:34.914-->00:13:40.586 data’s also copied from the card where the response are copied from the card into the buffer 00:13:40.586-->00:13:46.426 from the one cloning the uh API and um what they are doing, they are checking if the response um 00:13:46.426-->00:13:55.668 is uh, small enough to fit into the buffer and if it’s big they um, write, write an error and 00:13:55.668-->00:14:03.176 continue copying this stuff into the buffer which is too small. Um, yeah, I mean it happens, 00:14:03.176-->00:14:15.521 right? Um So, what does this look like? As I said I was um mostly interested in the login 00:14:15.521-->00:14:31.237 control process. So um, I abused this one to um, um to uh, yeah write and exploit, um exploit 00:14:31.237-->00:14:37.443 the log in. The, the user name is, um, not relevant, um as soon, as long as it triggers um 00:14:37.443-->00:14:44.183 request from the card, I left all the, d- debug stuff in so if you want to um, watch it again 00:14:44.183-->00:14:52.859 you can see what’s actually happening. Um, you can see there’s uh some check out um 00:14:52.859-->00:15:00.967 further down, um, th- th- return address is overwritten. Um, to execute the checkout and um, 00:15:00.967-->00:15:14.280 after while, yeah, you get your shell. And the nice thing is most of all pen stuff is running 00:15:14.280-->00:15:19.952 root, the root show so you don’t exploit looking for this user. [applause] So you don’t exploit 00:15:19.952-->00:15:33.900 just this user which you maybe try to login for but uh, yeah you get uh root access. And as 00:15:33.900-->00:15:39.772 far as you’ve seen also Windows is upfront with um, elevated privilege, so um, yeah, might be 00:15:39.772-->00:15:43.409 interesting to play around with, there as well but as I said uh I have found some bugs but I 00:15:43.409-->00:15:50.750 wasn’t interested in debugging them. Um, how do you approach this um, my approach was really 00:15:50.750-->00:15:57.957 code and do some fuzzing, defuzzing found way more bugs obviously. Um, most fuzzers 00:15:57.957-->00:16:03.429 currently are file-oriented um so if you want to fuzz something like a protocol you have to 00:16:03.429-->00:16:10.403 tweak it a little bit. Um, what I abused was that the API with the transfer call, tells you how 00:16:10.403-->00:16:18.377 much buffer or how big the buffer is and um, basically I, whenever it’s called by the 00:16:18.377-->00:16:27.053 transmit APIs called I copy everything into the buffer from a file. So, um, the um, in, in 00:16:27.053-->00:16:32.959 this case you don’t have poll handling like when fuzzing, for network sockets. So, um I always 00:16:32.959-->00:16:38.598 fill the whole buffer which is um, yeah lease out some corner cases but worked for me quite 00:16:38.598-->00:16:46.572 well and um, I was able um, yeah trigger a lot of cold calls by this. Um, this is um what my 00:16:46.572-->00:16:54.013 OpenSC fuzzing setup uh looked like. I used AFL, um, this AFL call to help a tool. Um, which 00:16:54.013-->00:17:01.387 uh contains some test code, I modified the test code so that just the gif mule or 00:17:01.387-->00:17:07.560 certificates and try to do a signature part is um, triggered. Um, this caused it to OpenSC 00:17:07.560-->00:17:16.502 which caused the cac driver and the cac driver then caused my fuzzing code. Oops. Accept. Um, 00:17:16.502-->00:17:31.083 and fuzzing code then reads everything from the file. Um, since um I did not only want to 00:17:31.083-->00:17:39.025 fuzz OpenSC I also wrote um, libraries which had to um, fuzz PC/SC for Linux and on Windows. 00:17:39.025-->00:17:49.135 Um, so in both cases you preload a library which intercepts the code, cause the driver uh, th- 00:17:49.135-->00:17:57.510 the- causes the reader driver and inserts some malicious data. Um, this wasn’t enough um, 00:17:57.510-->00:18:03.749 Travis, on Monday deported the Windows defender code uh, a while ago to Linux so we could 00:18:03.749-->00:18:10.690 load the Windows on Linux to fuzz it there. Um, which gives you nice pickups and sometimes 00:18:10.690-->00:18:20.099 also um, more um, more findings. Um, since you can control more stuff. I um, have beaten it 00:18:20.099-->00:18:28.708 enough to get the, the windows smartcard drivers running on, on uh Linux as well. So, um, the 00:18:28.708-->00:18:34.513 code was available, um you could, you can download it and play around on this if you have 00:18:34.513-->00:18:44.457 any questions give me mail or if, if you have pictures, yeah send them to me. Um, Yeah, most 00:18:44.457-->00:18:49.128 of the times, um memory corruptions are fun and interesting but um the logic 00:18:49.128-->00:18:53.599 backside there more interesting issues because they tend to stick around longer. They’re 00:18:53.599-->00:19:02.908 easier to exploit. Um, this is what I found in their, their pub library. Um, they’re then, login 00:19:02.908-->00:19:06.612 process is a little bit different from what I showed you earlier. The random numbers 00:19:06.612-->00:19:13.419 they, um, generate who have to cause sign um, they need to be very high entropy right? If you 00:19:13.419-->00:19:21.761 have a card from the same, um the same random nonce twice you can replay a text. So, where do 00:19:21.761-->00:19:27.700 you get good random numbers? Yeah from the smartcard right? So, if you get them from the 00:19:27.700-->00:19:36.142 smartcard and the smartcard can choose what the smartcard itself has to sign, you can um, can um 00:19:36.142-->00:19:43.082 do a replay attack again. If um a user is uh, inserting his card into a malicious computer it 00:19:43.082-->00:19:50.589 tries to longinthere, you um, can um, get uh, the, the, the random numbers signed by the 00:19:50.589-->00:19:57.329 user and then say, no you are rejected. You are not valid but then you have um, random numbers 00:19:57.329-->00:20:02.468 and you have the signature for it. And can modify your own smartcard, your malicious cards 00:20:02.468-->00:20:10.242 to replay exact login at another computer, a developed computer to login as said user. Um there 00:20:10.242-->00:20:15.448 are some roadblocks to all the exploitation stuff and uh, because you have a no 00:20:15.448-->00:20:21.220 interaction if you have a preprogrammed smartcard. Um, the channel back to the smartcard is 00:20:21.220-->00:20:29.428 quite limited so if you need to use information leaks, um that’s an issue. But uh, yeah in the 00:20:29.428-->00:20:37.369 end um, if you can control a bit of memory um that might be enough in login case. Um, as a 00:20:37.369-->00:20:42.108 take away, to think about your trust models. The, the smartcards are intended to make 00:20:42.108-->00:20:49.915 users more secure. But they also hugely increase your attack surface. So that’s something you 00:20:49.915-->00:00:00.000 should consider. Yeah, thanks everybody for listening. Um. [applause]