yeah hello everybody nice that you're here my name is Eric my day job is pentesting code auditing and I want to do want to talk about the stuff I did in my spare time for like the last year the issues I am presenting are fixed I have I have told the vendors about them so nowadays there are some quirks about this more about this later all the issues are an open-source project so if you are using them help them finding such issues read the code this is not about blaming them that they write the code this is more like hey help them I have some stuff for proprietary smart card drivers as well but I'm not really interested in debugging in my spare time so yeah that's why I don't have bugs in them I know they are there but I didn't debug them what I'm talking about I'm talking about attacking computers with smart card smart cards are used in a lot of different areas you have your phones you have ATM machines you have cars you have healthcare environments I'm mostly interested in the Linux so when you're using a smart card as a second factor this is a good idea or not that's basically the question I was asking myself why would you do this the smart cards are used for the authentication process this at least on Linux runs as root so if you have a bug there it's game over and you have this interesting this interesting effect that the programmers who are writing software for smart cards always think of the smart card as this trustworthy highly secure device so it they trust it and this might not always be a good idea to have a first look at the smart card stack you have to user which wants to log in the user is using a smart card for which there is a smart card driver and you have a reader which also requires a smart which requires a kind of driver on the lower layer you have to the physical smart card which is This was highly secure, tamper-proof device. Contains a processor, some memory, and people were always focusing on the smart card when attacking smart card security, and tried to glitch it, tried to decap it, to read out the keys. I went the other way and looked at the software side. If you are talking to a smart card, you need some protocols. The lowest level is the APDUs which you are sending to smart cards. It's a very simple protocol, as you can see. You have few bytes, which say, or which define the kind of packet you are sending. You have some parameters, P1 and P2. Then there's a length byte, and then you can append some additional data. If you look at the bottom, there's a small example. This is basically a command which sends a pin to the smart card to unlock the smart card. In this case, one, two, three, four. So as you can see, it's very simple. The response is simple as well. You have your payload, and then two status bytes, which say, okay, this was successful, or we have an error, or please request more data because there's more than I can fit in a response. On the top of that, you have the layer which controls the reader. The interface in this case is also quite simple. You give it a buffer, and then you have the interface. You give it a buffer length, and that's the data that's sent to the card, and you give it a buffer and the length, and that's the data you get back from the card. So it's quite easy. There are some additional API calls to control further settings, but this is the one which is relevant for me. The API is similar on Linux and on Windows, so you can easily port your drivers. And this is, yeah, this is also standardized somewhere. On top of that, the next instruction layer is PKCS11, which is also platform independent, which you can use, for example, via your browser to access the certificates which are stored on the card. So, yeah, you have a common interface which is portable and everything. In this case, you have an API call which allows you to find objects on the card. For example, certificates or others. Windows, by now, has a different interface. It has the SmartCard mini driver interface, which basically is quite similar, but different. The thing that is interesting, when loading those drivers, as soon as the SmartCard is plugged in, there's this answer to reset, which is basically a string of bytes, which tells the reader, or which tells the operating system, which driver it should load for this card. So, these RTLs are specific to the card, and usually you cannot change them. So, that means that they are static. Yeah, just a quick recap of the stack. So, and what we are attacking is the APU part, which is the SmartCard driver. So, what does the login with the SmartCard look like? On the left, you have the PAM, the Plugable Authentication Module. In the middle is the SmartCard, and on the right is the Certificate Vocation Server. First, the computer asks the SmartCards for all the certificates it has stored. Then it picks one, validates it, if it's from the right CA, if the user makes any sense for the system, and if the, if it's valid. The CRS server is asked if it's revoked, or if the certificate can be used for authentication. The check is, for example, done by using Libcurl or something. So, if you're attacking this, there's Libcurl running this route right there. So, if you have interesting bugs there, that's a way to use them. The next step, is to generate some random numbers, and these are sent to the card. And the card is expected to sign those random numbers, to prove that it contains the key to the certificate. And after this is done, the server or the computer checks if the signature's correct, and if it is, it knows, okay, this is a legitimate card for this user. And yeah, that's basically your login process. So, I already told you, the smart card driver developers always trust the card. It's, I'm not sure, maybe some subconscious thing that they usually don't expect to send something strange. And that's why I'm hooking in and what I'm trying to use. Those are the projects I found bugs in. Two of them are Yubico projects. The patching process with them worked quite fine. Then there are smart card services, which is the old Apple smart card stack. When I contacted them, I told them, hey, I sent a talk to DEF CON and want to talk about it. Let's coordinate the patching and release and everything. They did not answer. And after, I'm sure, three weeks, there was a reply, hey, we fixed everything and you're good. So, yeah, I asked for an embargo. Yeah. Yeah, it was kind of strange. They didn't even mention that it's like a security bug fix or everything. It's, yeah. Then there's OpenSC. I worked really good with them. It's the classic Linux smart card driver stack. It contains drivers for a lot of different cards from a lot of different people. So, a lot of attack surface fuzzing. This was quite rewarding. There were tons of bugs, but this was also a project, which was very good to work with, especially Frank Morgan did a good job of getting the team there together and trying to fix everything. The only problem is I also coordinated the release with them like four days ago, but nothing happened. I'm not sure why. They didn't release anything. I haven't heard from them since then. So, we decided to publish. We published a private repository where all the fixes went in today. So, you can install that in case you're affected and don't need to linger there in limbo with the known vulnerabilities in the software without having a patch. But I guess there will be a release in the next few days to address those. I did a lot of fuzzing on OpenSC, but it was not like a huge cloud. I only... I only passed certain parts, so there are certainly more bugs in there to be found. So, if you have spare time and want to look at this stuff, go for it. What kind of bugs did I find? For the Apple SmartCard services, for one of the drivers, we have this code. It is a loop, and it waits or it loops until the card sends those responses. And if the response is not good, the results start with 6-3. It says, hey, you need to fetch more data. I have more data for you. And they loop until the card says, okay, or until the card no longer says that it has more data. And in each loop, they retrieve the data from the card and append it to a buffer. So, if the card never stops saying you fetched everything, they continue copying everything into the buffer and on and on and on until you have your classic, in this case, heap over. So, that's nothing you want to have. It's your classic memory corruption. For OpenSC, this is a bug in one of the helper tools, not in the main driver. There's a buffer of 2048 bytes, and they have a variable buff size. And they request a file from the card or selected a file from the card. And the selection process gives you a structure, which, for example, contains the size of the card, the size of the file. So, what they are doing, they are using the size of the file as a parameter on how much data to request from the card into this fixed-size buffer. So, yeah, as soon as the file is bigger than 2K, you have your classic buffer overflow there. And I was told you should always bring, like, a calculator if you're talking about buffer overflows. Yeah, what the tool does is you can use it to read, for example, the search from the card. That's the minus R parameter, which triggers the code I showed you. And, yeah, using the buffer overflow, you can spawn whatever you want, in this case, the calculator. How do I exploit this? As I said, these RTLs, which define which driver gets loaded for a card, are usually static. You can't change them. I'm using a basic card, which is a smart card you can fully control and fully program in BASIC. Yeah, it's 2018, but it's BASIC. And for these cards, you can use a basic card, which is a smart card, and you can change the ATR. I will release an example exploit for this later this year. Other options for exploitation would be the SimTrace project, which is meant to do men in the middle on SIM card communication, but you can also use it to emulate a smart card. And apparently, there are also certain Java cards, which allow you to change these RTL bytes, but so far, I was not able to get my hands on them. And one of them, if somebody here knows more about this, please talk to me. I'm interested in not writing BASIC anymore. Yeah, for Yubico, this is also, this is in the part where data is also copied from the card, where the responses are copied from the card into the buffer of the one calling the API. And what they are doing, they are checking if the response is small enough to fit in the buffer, and if it's too big, they write an error and continue copying the stuff into the buffer, which is too small. Yeah, I mean, it happens, right? So what does this look like? As I said, I was mostly interested in the login process, so I've used this one. To, yeah, write an exploit, exploiting login. The username is not relevant as soon, as long as it triggers a request from the card. I left all the debug stuff in, so if you want to watch it again, you can see what's actually happening. You can see there's some check, and you can see the return address, and the shell code. Further down, the return address is overwritten to execute the shell code. And after a while, yeah, you get your shell. And the nice thing is since the whole PEM stuff is running its route, it's a route shell, so you don't exploit the login for this user. So, you don't explore just this user, which you maybe try to log on for, but, yeah, you get to root access. And as far as the scene, also on Windows, the stuff runs with the elevated privilege, so, yeah, it might be interesting to play around with there as well, but as I said, I found some bugs, but I wasn't interested in debugging them. How do you approach this? My approach was reading code and do some fuzzing. The fuzzing found way more bugs, obviously. Most fuzzers currently are file-oriented, so if you want to fuzz something like a protocol, you have to tweak it a little bit. What I abused was that the API with the transmit call tells you how much the buffer or how big the buffer is, and basically, whenever it's called, when the transmit is called, API is called, I copy everything into the buffer from a file, so, yeah, in this case, you don't have full handling, like when fuzzing network sockets, so I always fill the whole buffer, which is, yeah, at least out some corner cases, but worked for me quite well, and I was able to, yeah, trigger a lot of code pass by this. This is what my OpenSC fuzzing set up. It looked like I used AFL, this AFL called the helper tool, which contains some test code. I modified the test code so that just the give me all certificates and try to do a signature part is triggered. This calls into OpenSC, which calls the card driver, and the card driver then calls my fuzzing code. And the fuzzing code then reads everything from the file. Since I did not only want to fuzz OpenSC, I also wrote libraries which had to fuzz the PCSC stuff on Linux and on Windows, so in both cases, you preload a library which intercepts the calls to the driver. The calls to this reader driver and inserts malicious data. This wasn't enough. Tavis or Mandy ported the Windows Defender code a while ago to Linux, so you could load the Windows DLL on Linux to fuzz it there, which gives you nice speedups and sometimes also more findings, since you can control more stuff. So, yeah. I have beaten it enough to get the Windows smart card drivers running on Linux as well. So, yeah. The code is available. You can download it, play around with this. If you have any questions, give me mail, or if you have patches, yeah, send them to me. Yeah. Most of the times, memory corruptions are fun and interesting. But the logic bugs are the more interesting issues, because they tend to stick around longer. They're easier to exploit. This is what I found in the PAM library. The lock-in process is a little bit different from what I showed you earlier. The random numbers they generate to have the card signed, they need to be very high entropy, right? If you have a card signed the same random, known twice, you can do replay attacks. So where do you get good random numbers? Yeah, from the smart card, right? So if you get them from the smart card, and the smart card can choose what the smart card itself has to sign, you can do a replay attack again. If a user is inserting his card into a malicious computer, it tries to do the same thing. login there. You can get the random number signed by the user and then say, no, you're rejected, you're not valid. But then you have a random number and you have the signature for it and can modify your own smart card, your malicious card to replay exactly this login at another computer, at a valid computer to log in as that user. There are some roadblocks to all the exploitation stuff and because you have no interaction, if you have a pre-programmed smart card, the channel back to the smart card is quite limited. So if you need to abuse information leaks, that's an issue. But in the end, if you can control a bit of memory, that might be enough in a login case. As a takeaway, think about your trust models. The smart cards are intended to make you more secure, but they also hugely increase your attack surface. So that's something you should consider. Yeah. Thanks, everybody, for listening.