00:00:00.000,00:00:05.005 >>uh welcome to track 1, it’s a 4:30 talk. Uh, this is Nicolas it is his 2nd time speaking, so 00:00:08.141,00:00:13.146 there will be no shots involved sorry. Um, anyway, give him a warm welcome [applause] >>thank 00:00:18.352,00:00:23.357 you [applause] Alright so, the title of this talk is Poking the S in SD Cards. Uh when i started 00:00:27.094,00:00:32.933 this, i was wondering why SD stands for Secure Digital so that might be something secure, 00:00:32.933,00:00:39.206 right? But exactly how it works, because it’s just a simple memory card, you put in, you 00:00:39.206,00:00:44.544 place your files and that’s it. [off mic audience statement] Oh. ah, ya sorry, oh way better! 00:00:44.544,00:00:48.482 Sorry [laughs] ok SD stands for secure digital. Was asking why is Secure for? Uh, the is not a 00:00:48.482,00:00:52.819 physical attack so everything is about the protocol that is using by a, that is used by the SD 00:00:52.819,00:00:56.890 cards. So the first step is, before hands, what is an SD card - everybody saw an SD card of 00:00:56.890,00:00:58.892 course. It’s basically a microcontroller which interface the SD interface with a flash 00:00:58.892,00:01:00.827 memory. Uh if you want more information, you can see there was a great talk by bunnie and 00:01:00.827,00:01:05.832 xobs uh that was uh at the uh CCC uh where he decapped uh the sd cards he showed exactly how 00:01:23.951,00:01:28.956 it works from the inside/ but. We are not doing exactly this, we are going to play with the uh 00:01:32.492,00:01:36.763 communication protocols. And basically those cards do support 3 different communication 00:01:36.763,00:01:41.768 protocols. The SPI, the classic SPI communication. The SD or USD-I Bus protocol which uses 00:01:44.938,00:01:50.310 more data lines so the transfer speeds are way better. And there is a newer one, maybe you didn’t 00:01:50.310,00:01:55.315 see it, uh, you don’t see that often, it’s the UHS-II which has way more uh connectors. A second 00:01:59.052,00:02:03.991 row of connectors. And it uses different channel lines to uh to transfer even faster uh data So 00:02:06.493,00:02:11.498 uh to get access to the specifications, and to know exactly how it works from the 00:02:15.168,00:02:20.173 inside. Uh i you can go to the SD cards dot org website they uh offer some general specs in 00:02:24.211,00:02:29.216 simplified form. The document is only 262 pages but it’s quite easy to read / write. And it 00:02:31.718,00:02:37.724 presents the general description and how to interact with SD cards. But umm, at the really 00:02:37.724,00:02:43.430 lower level. For instance, this is the initialization sequence, I won’t go into the details, 00:02:43.430,00:02:47.467 don’t worry, it’s just that there’s a lot of different comments, and depending on the 00:02:47.467,00:02:52.773 type of cards, its generations you need to send comments maybe the card will not respond then 00:02:52.773,00:02:57.177 you have to do another thing then maybe it will respond that is a new valued command so that 00:02:57.177,00:03:03.617 means it’s a 3rd kind of card so, ya its a little bit messy, but once you read and uh, poke 00:03:03.617,00:03:10.624 with it, you, you cannot get some replies back to from the card, so this is good. So umm, i 00:03:10.624,00:03:16.396 won’t go into the details of all the PDF, just so you know that the protocol is basically a 00:03:16.396,00:03:21.401 query and response uh protocol, so as the master so the device your computer sorry, you send a 00:03:25.605,00:03:31.812 comment then the card will reply back, back with more information. Uh for instance uh 00:03:31.812,00:03:36.817 there is the CMD0 this the first command you need to send to the cards it will basically 00:03:39.553,00:03:44.925 re-initialize the it’s status, and you need to reply with the ok I am here, so you know your 00:03:44.925,00:03:49.930 card is available. Uhh, there’s the communication protocol, its using, uhh, 6 bits for a 00:03:53.300,00:03:58.305 command, then you need to send up to 32 bits of arguments. There the CRC, and some bits. 00:04:02.142,00:04:08.482 The good thing is, that in the SPI mode uh the CRC you don’t need the CRC in SPI mode, so you 00:04:08.482,00:04:13.453 can just put all zeros and that’s fine. As I said the protocol is a bit of a mess 00:04:13.453,00:04:19.259 because depending on the command you have 7 different response formats. So sometimes it’s only 00:04:19.259,00:04:25.599 8 bits that you receive back, sometimes its 32 bits. Sometimes uh it’s just sends an 00:04:25.599,00:04:30.604 acknowledge then it sends a packet back so every command has to uh has to have its own parcer 00:04:32.873,00:04:37.878 to pass the reply. To communicate with the uh, to communicate with the memory of 00:04:42.315,00:04:47.320 the files within the card, 4 bytes is not that much, so there’s a block transfer mode, 00:04:50.524,00:04:55.529 where basically you send first uh uh block start command, which is just 0xFE and then you run, 00:04:58.832,00:05:03.770 you send all your data, then uh, you need to send the CRC, and then the card will process the 00:05:08.375,00:05:13.380 information. There’s no length checking by itself by default is 512 bytes, but you have command, 00:05:16.650,00:05:21.655 which is the command 16 that allows you to change the data size. So, now that I know 00:05:25.625,00:05:30.630 exactly, well not exactly, now that I know how it works, uhh, as I looked for the security 00:05:32.799,00:05:37.804 features of the card. So the first one that shows up is the SDMI uhh, specifications, which 00:05:42.108,00:05:47.113 is uh, unfortunately not, uh, the purpose of this talk. Because it’s only all the 00:05:49.382,00:05:55.622 documentation, is only available to SD members, and there’s no way to get this file, except if 00:05:55.622,00:06:01.194 you try to google it, because apparently it has been indexed and its, it’s going to be done 00:06:01.194,00:06:06.199 or did directly. Um, but there’s also another way, another uh possibility to protect your data 00:06:08.335,00:06:14.407 from the card. And its available to 2 commands, which allows you to set a password on, on the 00:06:14.407,00:06:19.813 card so, when you enter the, when you set the password, the next time you place the card in 00:06:19.813,00:06:24.217 it, you cannot read anything from it unless you provides the password again, then the card 00:06:24.217,00:06:29.222 will be unlocked, and with this, once it’s unlocked its acting exactly like a normal SD card. 00:06:32.292,00:06:38.131 Also, those commands, those specific commands, uh for the uh, password protection are 00:06:38.131,00:06:44.537 mandatory to, to get the SD labels so, basically, nearly every card, every SD card on the 00:06:44.537,00:06:49.542 market, supports those commands, but its not used by any operating system I know. So what 00:06:53.613,00:07:00.153 I did, basically uh, just using my laptop to send command was not really easy to do, so uh, I 00:07:00.153,00:07:05.158 used, uh, a hydroburst device which is kind of a bus pirate clone. And I did uh uh small 00:07:07.294,00:07:12.299 python script to drive the SPI bus and to be able to send the raw commands to the device. So 00:07:15.201,00:07:21.408 basically here is the setup. I have the interface there. I communicate with it in USB. and 00:07:21.408,00:07:26.513 I send command and then there’s the SPI communication going through this and you have the SD 00:07:26.513,00:07:31.518 card adapter right here. So how does it work? You have the same 42 command which is uh its 00:07:36.289,00:07:41.294 command name is lock/unlock. And its uh it tells you to set a password up to 16 bytes long. I 00:07:44.964,00:07:48.968 say bytes and not characters, because your not limited to printable characters you can set 00:07:48.968,00:07:53.973 any byte you like, so the keyspace is uh 128 bits so, kind of a lot, and it’s difficult or 00:07:58.511,00:08:03.450 nearly impossible to brute force it it would take way much time So the command takes uh 00:08:06.019,00:08:11.024 different parameters uh basically there are some bits that you can set to the card and 00:08:15.195,00:08:20.333 they will uh trigger different functions so basically you have the set and reset password you 00:08:20.333,00:08:25.805 have the clear password so if you forgot your password you can use the clear bites to make the 00:08:25.805,00:08:31.945 card reset so it will wipe the flash so the card is unlocked again. You have the lock and 00:08:31.945,00:08:36.950 unlock commands so at first I used it uh to try to lock the card and uh and to unlock it you 00:08:39.953,00:08:46.259 actually have to set the bits to zero. That’s kind of strange. You can also erase the password 00:08:46.259,00:08:51.264 once the card is unlocked and uh the COP is um is a new mode that I will talk about later. So, how 00:08:56.703,00:09:02.642 to unlock exactly an SD in practice. You have to send this same CMD42 command. Then uh, the 00:09:02.642,00:09:07.647 data block with the lock bit and set. The password bytes uh a CRC which is a uh used less but you 00:09:11.718,00:09:18.491 have to send it, and then the card, the what will the card do? It will um. Uh. set, uh its uh 00:09:18.491,00:09:20.493 data outline to the ground while its processing the password and once its done the line will be 00:09:20.493,00:09:25.498 up again and you can read the status of the card. Funny thing is that, if the card is uh, 00:09:34.641,00:09:39.646 sending you a command that its busy trying to uh analyze your password. What you can do is 00:09:41.748,00:09:46.753 something quite fun. I placed it here, you can see um, this is uh, logical analyzer output. So 00:09:51.458,00:09:57.197 you can see here it’s the command I send to the card, here, sorry is the password 00:09:57.197,00:10:01.835 block. And here is the response from the card, so you can see that the line is put down to 00:10:01.835,00:10:08.441 zero, for sometime while the card is uh processing my password. So I did out a 00:10:08.441,00:10:13.446 password of 123456, and if I zoom in, this part here, if I zoom in you can see that when I 00:10:16.716,00:10:21.721 put the password length of 5 characters, it will take approximately 355 microseconds 00:10:23.890,00:10:28.895 to process, and if I out 6 characters, it take a bit more time to process. So basically, 00:10:32.398,00:10:37.403 what happens is that the SD card the password checking functions is uh vulnerable to a uh timing 00:10:39.405,00:10:45.912 attack. And you are able to verify to verify that the password has the correct lengths 00:10:45.912,00:10:52.018 based on the time it takes to uh reply, so you try to send a password with the lengths 1, its 00:10:52.018,00:10:55.855 takes some time, 2 its takes some time, 3 maybe it takes more time so you know that the 00:10:55.855,00:11:01.895 password is of lengths 3, and then it will try to check every character one after the other, 00:11:01.895,00:11:06.900 so you can basically brute force bytes per bytes and recover the password from the SD card. Sorry 00:11:12.972,00:11:19.913 So having all those schematics, and taking all those uhh measurements by hand is not 00:11:19.913,00:11:24.918 really easy to do. So, since this information here, the the fact that the data line is at 00:11:28.688,00:11:33.693 zero, umm, what I can do is basically use my SPI interface and try to read as much bits as 00:11:36.462,00:11:41.467 I can, and since all those bits will all be a zero, I can count them and i will kind of know how 00:11:43.903,00:11:48.908 many time it took to uhh, to check the passwords, and once I detect a 1, again, so once its 00:11:51.411,00:11:56.416 finished, I know that the length, the uh, computation is over and I can do something In 00:11:59.986,00:12:06.292 practice. What does happen, I did this small script here, and if I set my famous password 00:12:06.292,00:12:11.297 123456, and I run my script, you can see that for password lengths of zero, it takes, it 00:12:13.766,00:12:18.771 read 122 zeros, before the card was uhh, available again. For 1, it read 124 zeros, and at length 00:12:24.544,00:12:29.549 6 you see it’s only 1 which was a bit longer so you know that the correct length is 6. I have 00:12:34.487,00:12:39.492 a demo, which unfortunately, is not live Alright, so what I do here is I initialize the card, I 00:12:50.670,00:12:56.843 can get it status, so for now its unlocked. I will resend the lock command with the password i 00:12:56.843,00:13:01.781 will reset, so I put my famous password 123456. And we check the status of the card, it’s in 00:13:05.351,00:13:12.258 locked mode now, so im not able to read data from it, I will receive and invalid command. And 00:13:12.258,00:13:17.263 there’s no data from the card. Oh, what did I do, ya. So now I can run my brute force tool, so 00:13:19.499,00:13:24.837 it will track all the different all the different uhh password lengths to find the correct 00:13:24.837,00:13:31.077 value. Its 6, that’s ok, and then it will try every bytes combination to try to find my 00:13:31.077,00:13:36.082 password. So it takes a bit of time And then it brute force the last characters, and once my 00:13:41.688,00:13:46.693 card is unlocked, and I try to read the data, I can get data from the card [applause] So, 00:13:59.005,00:14:03.643 once I have my card which was working, I was quite happy of course, but I was thinking so 00:14:03.643,00:14:09.148 maybe there are more cards that have this same problem, so i bought uhh, different SD cards, 00:14:09.148,00:14:14.587 I went to the store, I bought some cards from different vendors, different sizes. Uhh I 00:14:14.587,00:14:19.692 also colleagues , and friends to lend me their cards, to just have a look at it, and guess 00:14:19.692,00:14:25.531 what? The only card I locked, permanently I was, I still don’t know how to unlock it. Basically 00:14:25.531,00:14:30.536 it’s broken, is uhh, is a colleagues of mine, not one of my cards unfortunately. So I did 00:14:33.506,00:14:39.445 lock all those cards with the 123456 passwords and I tried my brute force again, and it 00:14:39.445,00:14:45.885 sometimes worked, sometimes it didn’t, and some time there was some funny results. So for 00:14:45.885,00:14:50.890 instance, I get some SOny SD cards, and it has a protection on the brute force which uh 00:14:53.292,00:14:58.297 makes it uh refuse the CMD42 command after the 3rd failed attempt. That’s quite good, the 00:15:00.600,00:15:06.639 only thing is that you uh just need to send one uh command xero, so you just reset the card 00:15:06.639,00:15:13.346 by the interface, then you have 3 more tries, so it just a bit, uh, it’s just a bit, uh, longer 00:15:13.346,00:15:18.351 but not that much The Sony uSD micro SD cards also uhh have something different where 00:15:22.588,00:15:28.628 basically I was not able to read the zeros, so I know that there was something but I was not able 00:15:28.628,00:15:35.568 to read it, because it was really really uh fast, so I tried with the logicalizer, so 00:15:35.568,00:15:42.175 it was not fast anymore, so not fast enough, so I went to uh, a colleague of mine and uh 00:15:42.175,00:15:47.180 borrowed uh a huge oscilloscope and im able to sample way faster and then I am able to see the 00:15:51.517,00:15:56.522 really small difference between lengths 5 and lengths 6. 5-6-5-6 , don’t see it? That’s normal 00:15:59.992,00:16:04.997 but [laughing] ya, you can still do it, it’s just uh, a bit more complicated. Uh there is 00:16:09.135,00:16:14.140 something strange ehh on Kingston SD cards, where I was able to find, to find uh the 00:16:17.476,00:16:22.481 password lengths but then I couldn’t find, I couldn’t brute force uhh every uhh byte. So I 00:16:25.918,00:16:32.792 did some tests again, and basically what I did was uh, I set up the oscilloscope again, 00:16:32.792,00:16:37.797 put 5 zeros, then 6 zeros, so I can find the length that’s fine, then I set the first bytes to be 00:16:40.166,00:16:45.171 correct, second one, third one, still nothing, and at the 4th one, you see a time difference. 00:16:51.544,00:16:56.649 So what happens is that uh this card checks the password by groups of 4 bytes. So I would 00:16:56.649,00:17:01.587 assume the microcontroller inside is a 32 bits one, so it’s uh a bit different so the attack 00:17:03.856,00:17:08.861 is still doable but you have to brute force by groups of 32 bits. So in my results, uh I 00:17:12.365,00:17:18.738 have all those cards uh please be careful about what’s uh written here, because this is 00:17:18.738,00:17:23.743 the name that is written on the card, but inside of the card it might be another manufacturer 00:17:26.279,00:17:32.785 that’s created the chip, you know? So, uh, the name is not really correlated to the 00:17:32.785,00:17:37.189 manufacturer of the cards, so this card might not be that useful, this one is more, you 00:17:37.189,00:17:42.194 can see those cards. Uh and this information, the manufacturer ID is uh sent by the card, and 00:17:47.934,00:17:54.273 unfortunately there is no public source of information about which ID corresponds to which 00:17:54.273,00:17:56.275 manufacturer. You can find some of them on the internet but for some of those, like this one, I 00:17:56.275,00:17:58.411 just don’t know who made it. Also all the cards were bought this year so we are in 2019 but 00:17:58.411,00:18:01.180 the production dates on the cards is around 2010 or 2011. Something about this. So, again 00:18:01.180,00:18:03.182 this is a value taken from the cards, i’m not sure it was uh, it’s the real production dates, 00:18:03.182,00:18:08.187 it just the data that on the inside of the card. Nearly all of those were vulnerable the 00:18:28.474,00:18:33.479 only one that i was not able to uhh use were uhh the San Disk models, and I was not able to 00:18:36.983,00:18:43.189 use the samsung cards because when I tried to use the CMD42 command the card response was 00:18:43.189,00:18:48.194 with an invalid command. So I don’t know how they got the uh the logo, but its working. Uhh, 00:18:53.532,00:18:59.105 so to conclude, I would say it’s pretty useless vulnerability because no operating system 00:18:59.105,00:19:04.043 supports this password locking function. Hopefully because it was not that useful. It also 00:19:06.045,00:19:11.584 affects a lot of manufacturers, uh, and good thing to do is ya, to read the specs, there is a 00:19:11.584,00:19:13.586 lot of information a lot of uhh thing, and things you don’t expect from a card, right? Also, 00:19:13.586,00:19:15.588 as a future work, as I said, there is the COP function basically it’s a you can set the 00:19:15.588,00:19:20.593 passwords to protect the card to uh, to password protect the password clear mechanism. I 00:19:33.005,00:19:37.710 don’t know what is that, but this is a new feature. I was not able to get the cards that works 00:19:37.710,00:19:42.715 with that has COP enabled. So, maybe I will try to find them, ya, maybe there is still another 00:19:45.284,00:19:50.289 vulnerability in those. So, a few takeaways. SD cards have a password protection as we said. 00:19:54.593,00:19:59.598 Um, most of the cards are vulnerable to a timinings attacks, uhh, and that’s pretty 00:20:01.801,00:20:06.806 much it! [applause] Thank you Oh, by the way, if you want to have a live demo, uhh, I will 00:20:13.279,00:20:18.484 be, after the talk I will go to the hardware hacking village so we can do the q and A there and 00:20:18.484,00:20:23.489 having a live demo. Thank you. [applause]