Uh, welcome to track one, it's a 4.30 talk. Uh, this is Nicholas, it is his tech- second time speaking, so there should be no shots involved. Sorry. Um, anyway, give him a warm welcome. Thank you. Alright, so the title of this talk is Poking the S in SD Cards. Uh, when I started this, I was wondering why SD stands for Secure Digital. So, there might be something secure, right? But exactly how it works, because it's just a simple memory card, you put it in, you place your files, and that's it. Oh, ah, yeah, sorry, oh, yeah, way better, sorry. Okay, so SD stands for Secure Digital. I was asking why is, uh, Secure 4? Uh, this is not a physical attack, so everything is only about the protocol that is, uh, that is using by, uh, that is used by the, uh, the SD cards. So, the first step is, beforehand, what is an SD card? Everybody saw an SD card, of course. Um, it's basically a microcontroller, which interface the SD interface with a flash memory. Uh, if you want more information, you can see there was a, there's a great talk by Bernie and Klobs, uh, that was, uh, at the CCC, uh, where he decapped, uh, the SD cards, he showed exactly how it works from the inside. But, we are not doing exactly this. We are gonna play with, uh, uh, the communication protocols, and basically those cards do support, uh, three different communication protocols. The SPI, classic SPI communication. The SD, uh, UHS-1 was protocol, which uses more data lines, so the transfer speeds are way better. And there's a newer one, maybe you didn't see it, uh, you don't see that often, it's the UHS-II which has way more uh connectors, a second row of connectors and it uses differential lines to uh to transfer even uh faster uh data. So uh to get access to the specifications and to know exactly how it works from the inside uh I can you can go to the sdcards.org website, they pro- they uh offer uh some general specs in simplified form. The document is only 262 pages so it's quite easy to read, right? And it presents the general description on how to interact with SD cards but uh uh at a really low level. For instance this is the initialization sequence, I won't go into the details, don't worry. It's just that there's a lot of different commands and depends on what you're doing. So uh depending on the type of cards, uh his generation, you need to send commands, maybe the card will not respond, then you have to do another thing, then maybe it will respond that is an invalid command so that means it's a third kind of card. So yeah it's a bit messy but once you read and uh poke with it, you you you cannot get some replies back to uh from the card. So this is good. So uh I won't go into the details of all the pdf, just so you know that the protocol is basically a query and response uh protocol. So as the master or so the uh the device your computer sorry, you send a command then the card will respa- will reply back back with more information. Uh for instance uh there's the cmd0 this is the first command you need to send to the card. It will basically uh reinitialize the its status and it will reply with a okay I'm here so you know that your card is uh this and that so I'm gonna do this. available. Uh there's a communication protocol it's using uh 6 bits uh for a command then you need to send up to 32 bits of arguments. There's a CRC and some bits. Good thing is that in the SPI mode uh the CRC you you don't need the CRC in SPI mode so you can just put all zeros and that's fine. As I said the protocol is a bit a mess because uh depending on the command you have 7 different response formats. So sometimes it's only 8 bits that you receive by a back, sometimes it's 32 bits, sometimes uh it just sends an acknowledge then it sends a packet back so every command has to uh has to has it's own parser parser to to pass the the reply. To communicate with the uh to communicate with the the the memory or the fi- the files within the card. You need to have a password. You need to have a password. You need to have a password. You need to have a password. You need to have a password. You need to have a password. You need to have a password. You need to have a password. You need to have a password. Uh so 4 bytes is not that much. So there's also a block transfer mode where basically you send first uh block start command which is just a 0xFE and then you run uh you send all your data. Then uh you need to send the CRC and then the card will will process uh the information. There's no links checking by itself, by default it's 512 by bytes, uh uh you have short road but you have commands which is the command 16 that allows you to change the data size. So now that I know exactly, well not exactly but now that I know how it works uh as I looked for all the security features of the card. So the first uh one that shows up is the SDMI uh uh specifications which is uh unfortunately not the the the purpose of this talk because it's only uh all the documentation is only available to SD members and there's no way to uh get this file except if you try to google it because apparently it has been indexed and it's it can be downloaded directly. Uh but there's also another uh way another uh possibility to protect your data from the card. Uh so the uh the uh the uh the uh the uh the uh the uh the uh the uh the uh the the uh card. And it's available to 2 commands, which are used to set uh the password on the card. So when you enter when you set the password the next time you place the card in it you cannot read anything from it unless you provide the password again. Then the card would be unlocked. And with this uh once it's unlocked it's acting exactly like a Nomar SD card. Also those commands uh those specific commands uh for the uh password protection are mandatory to to get the SD label. So basically nearly every card uh every SD card on the market supports the those commands. But it's not used by any operating system I know so. So what I did basically uh just using my laptop to send command was not really uh easy to do. So I used uh a di- an hydro bus device which is kind of a bus pirate clone. And I did uh a small python script to drive the the SPI bus and to be able to send a row commands to the device. So basically here's the setup. I have the interface there. I communicate with it in USB. And I send command and then there's the SPI communication going through this and you have the SD card adapter right here. So how does it work? You have the CMD 42 command which is uh it's common name is lock unlock. Uh it's uh it's uh it's uh it's uh it's uh it's uh it's Primetime 40 ready. And it's uh it allows you to set a password up to 16 bytes long. I say bytes are not characters, because you're not limited to printable characters. You can set any byte you like. So the key space is uh of uh uh 128 bits so kind a lot uh kind of a lot and it's difficult uh or nearly impossible to brute force. It would take way much time. So the command takes uh different parameters uh but umm it's very fast so uh there's the uh basically there are some bits that you can uh set to the card and they will uh trigger different functions. So basically you have the set and reset passwords, you have the clear password so if you forgot your password you can use the clear bit to make the card reset so it will wipe the flash and then the card is uh unlocked again. You have the lock and unlock command so at first uh I used it uh to try to lock the card and uh to unlock it you actually have to set the bit to zero. That's kind of strange. You can also erase the password once the card is unlocked and uh the COP is uh is a new mode that I will talk about later. So how to unlock exactly an SD card in practice you have to send this cmd42 command then uh you have to uh the data block with the lock bit and set the password bytes uh CRC which is uh uh useless but you have to send it and then the card the the what will the card do it will uh uh set uh it's uh data out line to the ground while it's processing the password and once it's done the line will be up again and you can read the status of the card. Funny thing is that uh you can if the card is uh sending you a command that it's busy trying to uh analyze your password what you can do is something quite fun. I placed it here you can see uh this is uh logic analyzer output. So you can see here it's the command I sent to the card. This is the here here sorry is the password block and here is the response from the card. So you can see that the line is put down to zero. For sometime while the card is uh processing my password. So I did put a password of one two three four five six and if I zoom in this part here if I zoom in you can see that when I put a password length of five characters it will take approximately 55 microseconds to process and if I put 6 characters it takes a bit more time to process. So basically you can see that this card is processing basically, what happens is that the SD card, the password checking functions is uh vulnerable to a timing attack and we are able to verify that uh the password has the correct length based on the time it takes to reply so you try to send a password with uh the length one, it takes some time, two, it takes some time, three, maybe it takes more time so you know that the password is of length three and then it will try to check every character one after the other so you can basically brute force a byte per byte and recover the password from the SD card. Sorry. So having all those schematics and taking all those uh measurements by hand is not really easy to do so since this information here the the fact that the data the data line is at zero um the data line is at zero so um the uh uh the data line is at zero so what I can do is basically use my SPI interface and try to read as much bits as I can and since all those bits will be at zero I can count them and I will kind of know how many time it took to uh to uh generate to check the password and once the once I detect a one again so once it's finished I know that the length is uh the the computation is over and I can do something. In this case I did uh this small script here and if I set the uh my famous password one two three four five six and I run my script you can see that for password length of zero it took uh it read uh 122 zeros before the card was uh available again. For one it read 124 zeros and at length six you see it's the only one which was a bit longer so you know that's the correct length is six. I have a demo which unfortunately is not live. Alright, so what I do here is I initialize the card. I can get it status so for now it's unlocked. I will send the lock command with the password I will set so I put my famous password one two three four five six. Now it show's uh a I will check the status of the card. It's in locked mode now so I'm not able to read data from it. I will receive an invalid command and there's no data from the card. What did I do? Yeah. So now I can run my brute force tool. So it will track all the le- the different uh password lengths to find the correct value. It says that's okay. And then it will try every byte combination to try to find my password. So it takes a bit of time. And then it brute force the last characters and once my card is unlocked and I try to read the data, I can get data from the card. So now I can read the data. So now I can read the data. So now I can read the data. So once I have my card which was working, I was quite happy of course, but I was thinking so maybe there are more cards that have this- the same problem. So I bought uh different SD cards. I went to- to the store. I bought some cards from different vendors, different sizes. Uh I also asked colleagues and friends to- to lend me their cards just to- to have a look of it. And guess what? The only card I- I locked permanently, I was- I'm still don't know how to unlock it. Basically it's broken. Is uh uh colleagues of- of my friends. So I'm going to show you a little bit of mine. Not my- not one of my cards unfortunately. So I did lock all those cards with the 1, 2, 3, 4, 5, 6 passwords and I tried my brute force again. And it sometime worked. It sometime didn't. And sometime there was some funny results. So for instance I get some uh Sony SD cards. And it has let's say a protection on the brute force which uh makes it uh refuse the CMD 42 commands after the third uh failed attempt. So I did lock all those cards with the CMD 42 commands after the third attempt. That's quite good. The only thing is that uh you uh just need to send one uh common 0 so you just reset the card by the interface and then you have three more tries. So it's just a bit uh it's just a bit uh longer but not that much. The Sony usd micro micro SD cards also uh have something different where basically I was not able able to uh read uh the zeros. So I know that there was something but I was not able to read it because it was really really uh uh fast. So I tried with the logic analyzer it was still not fast anymore so not fast enough so I went to a colleague of mine and uh brought uh borrowed a huge oscilloscope and I'm able to sample way faster and then I'm able to see this really small difference between length 5 and length 6. 5, 6, 5, 6. Don't see it that's normal but yeah you can still do it it's just uh bit more complicated. Uh there's also uh something strange in on Kingston SD cards where I was able to find to to find the the the password length but then I couldn't find any uh I couldn't brute force the every uh uh password length. Uh I couldn't uh bite. So I did some tests again and basically what I did was uh I set up the oscilloscope again put uh five zeros then six zeros so I can find the length that's fine. Then I set the first by the first bytes to be correct. Second one, third one, still nothing. And at the fourth one you'll see a time difference. So what happens is that uh the uh the uh the uh the 蛋 uh 29th and the account obec uh came that order four bytes. Uh second one 2.2. And I have 2 bits of 4 bytes. So there are saying that hyung's password is nice even though all left the card even though all the chat is onTT dieserירing that's all. Only thing about this card where you have uh correlates by groups of 4 bytes. So it? would I assumed that the microcontroller on inside is a 32 bits one so it's a bit different so the attack is still doable but you alsoge that the groups of 32 bits. So in my results I have all of those cards please be careful about now or youn put everything there. Please be careful about the user who makes side of the card there's a not it might be another manufacturer that's created the chip you know. So uh the name is not uh correlated to the manufacturer of the card so this uh colon might not be that useful this one is more is more you can see those cards uh and this information the manufacturer ID is uh sent is sent by the card and unfortunately there's no uh public source of information about uh which ID corresponds to which uh manufacturer. So you you can find some of them uh on the internet but for some of those like this one I just don't know who made it. Also all those cards were bought this year so we are in 2019 but the production date on the card is around 2010 or 2011 something about this. So again this is a value taken from the card I'm not sure it was uh it's the real production date but I'm not sure it's the actual production date it's just the data that is in the inside the of the the card. Nearly all of those were vulnerable the only one that uh I was not able to uh use is the the uh the SanDisk models and I was not able to use the the Samsung cards because when I tried to use the CMD42 command the card responds with an invalid command. So I don't know how they got the the logo but it's it's working. Uh so to conclude uh I would say it's a pretty useless vulnerability because no operating system supports uh this password locking function. Hopefully because uh it was not that useful. It also affects a lot of manufacturers uh and good thing to do is yeah read the specs there are a lot of info-informations, a lot of uh things and things you don't expect from a card right. Also as the future work uh as I said there's a the COP uh function basically it's a you can set a password to protect uh the card uh to uh to password protect the password clear mechanism. I don't know why it's that but there's a this is a new feature I was not able to get a card that works with uh that has COP enabled so maybe I will try to find and yeah maybe there's also another vulnerability on those. So a few takeaways uh SD cards uh just have a a a password protection as we said uh most of the cards uh are vulnerable to a timing attacks. Uh and that's pretty much it. Thank you. Oh uh by the way if you want to have a live demo uh I will be uh after the talk I will be go to the hardware hacking village so we can do the Q&A there and having a live demo. Thank you.