1 00:00:00,000 --> 00:00:05,740 Dennis here. He just gave a track, a talk at a main track and this is a continuation of his 2 00:00:05,740 --> 00:00:09,800 talk where he's going to do a quick review and then he's going to get into some of the stuff 3 00:00:09,800 --> 00:00:18,660 with the Xiaomi firmware and the ARM Cortex-M. I said that in a weird order. So sorry for my 4 00:00:18,660 --> 00:00:26,380 Yoda. Alright and with that, are you ready? Awesome. Here's Dennis. So. 5 00:00:30,620 --> 00:00:36,880 Yeah. Thank you. So who of you have been actually in the Flamingo and saw the EverTalk? Oh. 6 00:00:36,960 --> 00:00:43,680 Okay. So I'm sorry the first ten slides might be a little bit, you know, like coming, might be 7 00:00:43,680 --> 00:00:52,300 a little bit similar to the main track but yeah, I mean I tried to get quickly over that. So the 8 00:00:53,100 --> 00:00:59,040 outline for the talk would be like the following. So I give you a short motivation, give you 9 00:00:59,040 --> 00:00:59,920 an introduction to the Xiaomi. 10 00:01:00,000 --> 00:01:00,020 Okay. So the first thing I'm going to do is I'm going to give you a little bit of information 11 00:01:00,020 --> 00:01:04,760 about the Xiaomi cloud and then we go to the devices and now the important part comes to step 12 00:01:04,760 --> 00:01:10,900 by step binary patching. Some information about me, I'm a researcher at a Northeastern University 13 00:01:10,900 --> 00:01:16,620 in Boston and I'm working with Professor Givar Nobir and I'm also a grad student at the TU Darmstadt 14 00:01:16,620 --> 00:01:22,140 in Germany. I'm working there with the Simulab. The main stuff what I do at the moment is I do 15 00:01:22,140 --> 00:01:27,200 reverse engineering of interesting devices, interesting devices in terms of devices which I can 16 00:01:27,200 --> 00:01:29,980 use in my home, for example, or use on me. So I'm going to give you a little bit of information 17 00:01:30,000 --> 00:01:35,580 about what I do. So IoT at the moment is very interesting for me, smart locks and as a lock 18 00:01:35,580 --> 00:01:43,120 picker, of course, also physical locks. Okay. Let's start with your motivations. So why do we reverse 19 00:01:43,120 --> 00:01:49,020 IoT? And this is like more or less depending on like the so-called attacker model. Some people do 20 00:01:49,020 --> 00:01:54,740 that to find and exploit vulnerabilities to hack other people. But the more interesting stuff for 21 00:01:54,740 --> 00:01:59,980 me is I want to make, give you the possibility to describe what I do. So I'm going to give you a 22 00:02:11,220 --> 00:02:21,440 little bit more details. Let's get into it. So if you have one. This is a little bit more 23 00:02:21,440 --> 00:02:26,220 advanced of what you need. This really takes care only of your... the 24 00:02:26,220 --> 00:02:29,980 infrastructure, you need to have an application that is already installed. All this stuff. Do you 25 00:02:29,980 --> 00:02:32,920 in Europe more or less useless. But like two weeks later they 26 00:02:32,920 --> 00:02:35,040 created like a European cloud especially for the European 27 00:02:35,040 --> 00:02:38,820 users so now it's okay again. But the thing is like it shows 28 00:02:38,820 --> 00:02:45,200 us that IT devices are strongly relying on the vendor. Another 29 00:02:45,200 --> 00:02:47,900 thing is we want to get more functionality. For example, 30 00:02:47,960 --> 00:02:52,140 adding new features or localization. Devices like the 31 00:02:52,140 --> 00:02:55,180 smart home gateway speak Chinese. I don't. So it would be 32 00:02:55,180 --> 00:02:57,560 better if it would speak English or German or whatever. 33 00:02:57,560 --> 00:03:01,900 Another thing is also geo-blocking. Some of the 34 00:03:01,900 --> 00:03:05,720 devices are only working in specific countries. So if you 35 00:03:05,720 --> 00:03:09,280 try to connect specific IP cameras like in U.S. they won't 36 00:03:09,280 --> 00:03:12,740 work because they rely on Chinese servers which block U.S. 37 00:03:12,740 --> 00:03:17,420 IP addresses. So to avoid that you need to do reverse 38 00:03:17,420 --> 00:03:21,440 engineering there. So how this whole thing started. In May 39 00:03:21,440 --> 00:03:24,760 2017 I started with Daniel Wigema. He is sitting in the 40 00:03:24,760 --> 00:03:27,540 front line here. Front row here. And with the vacant 41 00:03:27,560 --> 00:03:30,960 cleaning robots and the Xiaomi Mi Band which is a smart watch 42 00:03:30,960 --> 00:03:34,160 like this which is also from Xiaomi. And we continue to work 43 00:03:34,160 --> 00:03:38,180 on the smart home gateway, the light bulbs, mostly all the 44 00:03:38,180 --> 00:03:42,520 stuff that you see in the front of me. But even more so over the 45 00:03:42,520 --> 00:03:48,360 time a lot of devices were coming around. Okay. So about 46 00:03:48,360 --> 00:03:52,900 the Xiaomi cloud. Xiaomi claims actually to have the biggest 47 00:03:52,900 --> 00:03:55,980 IOT ecosystem worldwide. So way bigger than Google or Samsung. 48 00:03:56,280 --> 00:03:57,540 They have over 8,000 devices. They have over 8,000 devices. 49 00:03:57,560 --> 00:03:59,460 They have over 85 million devices and 800 different 50 00:03:59,460 --> 00:04:04,100 models. The most interesting model that I saw so far is smart 51 00:04:04,100 --> 00:04:07,500 toilet seat actually which has Wi-Fi and Bluetooth and can 52 00:04:07,500 --> 00:04:11,080 recognize the person who is sitting on it and setting up the 53 00:04:11,080 --> 00:04:17,480 temperature and making some lightening stuff. If you buy a 54 00:04:17,480 --> 00:04:21,440 product which is working with the Xiaomi cloud then it's, the 55 00:04:21,440 --> 00:04:24,020 chances are very high that it's actually not produced by Xiaomi 56 00:04:24,020 --> 00:04:27,540 but by a third party vendor. So what the cloud is actually 57 00:04:27,560 --> 00:04:32,240 doing is it connects different vendors in one ecosystem and all 58 00:04:32,240 --> 00:04:36,060 the devices are using the same protocol. This cloud system also 59 00:04:36,060 --> 00:04:40,680 supports different technologies and for us as engineers the most 60 00:04:40,680 --> 00:04:42,360 important thing is the implementation is different 61 00:04:42,360 --> 00:04:44,760 from manufacturer to manufacturer. So if you see a 62 00:04:44,760 --> 00:04:47,660 device which is like super secure and they have all the stuff 63 00:04:47,660 --> 00:04:50,460 enabled like secure boot and so on, doesn't mean that it has to 64 00:04:50,460 --> 00:04:52,460 be the same case for all the other devices which are on the 65 00:04:52,460 --> 00:04:56,460 same ecosystem. Also the software quality is very, very different. 66 00:04:57,560 --> 00:05:01,460 It's very stupid things. The best thing that I saw is like some 67 00:05:01,460 --> 00:05:05,280 file which was copied from Stack Overflow or something with some 68 00:05:05,280 --> 00:05:11,220 example for a batch script. So you see everything there. So this 69 00:05:11,220 --> 00:05:16,200 is some overview about the cloud. So what we see here is the 70 00:05:16,200 --> 00:05:19,660 main three technologies which we use, Wi-Fi, Bluetooth LE, and 71 00:05:19,660 --> 00:05:23,420 ZigBee. And the central component if you have this kind 72 00:05:23,420 --> 00:05:26,940 of devices, it's your smart phone actually. So on your smart phone 73 00:05:26,940 --> 00:05:27,540 you usually have your smart phone and then you have the 74 00:05:27,560 --> 00:05:31,540 Mi Home app installed. And the Mi Home app is making sure that 75 00:05:31,540 --> 00:05:34,960 the devices get the initial setup. So you enter your Wi-Fi 76 00:05:34,960 --> 00:05:37,680 credentials in your smart phone. Then your smart phone is talking 77 00:05:37,680 --> 00:05:43,100 to the IOT devices. And as soon as this is done, they create 78 00:05:43,100 --> 00:05:46,300 their own connection to the Internet. So after that they 79 00:05:46,300 --> 00:05:50,040 connect directly to the Internet without like the smart phone. So 80 00:05:50,040 --> 00:05:52,340 you can walk around with your smart phone but the devices have 81 00:05:52,340 --> 00:05:56,580 still a connection. So in the next step I take a look at the 82 00:05:56,580 --> 00:06:02,660 cloud protocol for the device to cloud communication. To connect 83 00:06:02,660 --> 00:06:05,540 to the cloud you need two credentials. The one credential 84 00:06:05,540 --> 00:06:10,180 is the device ID which is unique pair device. For the Wi-Fi 85 00:06:10,180 --> 00:06:13,780 device it's like an eight digit number. For the other devices 86 00:06:27,120 --> 00:06:28,640 so device-to-cloud communication, 87 00:06:28,940 --> 00:06:30,600 and this key is also static. 88 00:06:30,920 --> 00:06:32,180 For most of these devices, 89 00:06:32,220 --> 00:06:34,100 it's burned into the one-time programmable memory, 90 00:06:34,280 --> 00:06:35,460 so you cannot change it yourself. 91 00:06:35,700 --> 00:06:36,840 Every time you boot up this device, 92 00:06:36,960 --> 00:06:40,700 it gets loaded from the one-time programmable memory, 93 00:06:41,940 --> 00:06:44,980 and if you do updates, if you do provisioning and so on, 94 00:06:45,020 --> 00:06:45,880 it always stays the same. 95 00:06:46,960 --> 00:06:48,400 For the token, it's a little bit different. 96 00:06:48,520 --> 00:06:50,660 This is used for the app-to-device communication. 97 00:06:51,060 --> 00:06:53,000 For example, if you want to control 98 00:06:53,000 --> 00:06:55,040 the vacuum cleaning robot in your local network, 99 00:06:55,880 --> 00:06:57,260 it's a little bit time-critical, 100 00:06:57,400 --> 00:06:59,060 so you don't want to wait one second 101 00:06:59,060 --> 00:07:01,280 for the command to go over the cloud, 102 00:07:01,340 --> 00:07:03,180 so you can speak also directly with the device. 103 00:07:03,460 --> 00:07:06,340 And for that, we use also a key which is dynamic. 104 00:07:06,840 --> 00:07:09,540 That means every time you're connected to a new Wi-Fi, 105 00:07:09,720 --> 00:07:11,900 this key gets regenerated, so it's a different key, 106 00:07:12,020 --> 00:07:14,640 like every time you create, for example, new Wi-Fi 107 00:07:14,640 --> 00:07:16,780 or if you provision it newly. 108 00:07:18,680 --> 00:07:22,580 The payload itself, this protocol can speak TCP and UDP, 109 00:07:23,000 --> 00:07:24,100 depending on what you block. 110 00:07:24,100 --> 00:07:28,180 So if you block TCP, then it just will happily talk over UDP, 111 00:07:29,040 --> 00:07:30,520 and this is how it looks like. 112 00:07:32,460 --> 00:07:34,960 The payload is encrypted, and the encryption key 113 00:07:34,960 --> 00:07:37,360 is more or less depending if you're speaking with the app 114 00:07:37,360 --> 00:07:38,520 or if you're speaking with the cloud. 115 00:07:39,360 --> 00:07:41,980 The important thing here is if you have an unprovisioned device, 116 00:07:42,600 --> 00:07:44,960 which you take just fresh off the packet 117 00:07:44,960 --> 00:07:47,220 or you have done a factory reset, 118 00:07:47,680 --> 00:07:50,200 then the device tells you the token, 119 00:07:50,380 --> 00:07:52,060 which is like the second secret key, 120 00:07:52,620 --> 00:07:53,740 in the checksum field. 121 00:07:54,100 --> 00:07:56,940 So this is required that your smartphone 122 00:07:56,940 --> 00:07:58,340 is able to provision the device. 123 00:08:00,200 --> 00:08:02,200 As you see here, they do some integrity thing. 124 00:08:02,380 --> 00:08:06,420 This is so the checksum is also protected with the key 125 00:08:06,420 --> 00:08:09,040 and all the other stuff is encrypted with AES. 126 00:08:10,420 --> 00:08:14,280 One interesting thing here is they have this Epoch timer. 127 00:08:14,500 --> 00:08:15,620 Maybe you see it here. 128 00:08:16,680 --> 00:08:19,340 This thing here, which is a Unix timestamp, 129 00:08:19,460 --> 00:08:21,080 and this is also used for time synchronization 130 00:08:21,080 --> 00:08:22,680 because usually if you boot up a device, 131 00:08:22,820 --> 00:08:23,640 then it doesn't have time, 132 00:08:24,100 --> 00:08:26,200 so over this thing, they can also synchronize the time. 133 00:08:27,440 --> 00:08:30,500 The protocol itself is JSON formatted messages, 134 00:08:30,740 --> 00:08:34,020 and every packet is more or less identifiable with a packet ID, 135 00:08:34,220 --> 00:08:36,540 and you have two different structures, more or less, 136 00:08:36,560 --> 00:08:37,280 methods and parameters. 137 00:08:37,600 --> 00:08:39,280 This could be like commands or results. 138 00:08:39,680 --> 00:08:42,360 For example, if the cloud asks you something, 139 00:08:42,580 --> 00:08:46,740 then you send a result to the cloud or vice versa. 140 00:08:47,220 --> 00:08:51,020 The thing here is every command and response 141 00:08:51,020 --> 00:08:51,940 is confirmed by the receiver. 142 00:08:52,200 --> 00:08:53,060 So you... 143 00:08:54,100 --> 00:08:55,980 You always make sure that the command has actually reached. 144 00:08:56,820 --> 00:08:59,100 I have here one example for such a command. 145 00:08:59,300 --> 00:09:02,180 This is the actual login, more or less, 146 00:09:02,180 --> 00:09:03,180 of the device to the cloud. 147 00:09:03,440 --> 00:09:06,980 So what happens here is, if you see here, 148 00:09:07,560 --> 00:09:11,660 it tells the cloud which kind of Wi-Fi you have, 149 00:09:11,880 --> 00:09:13,760 which is the SSID of your Wi-Fi access point, 150 00:09:14,020 --> 00:09:17,800 what's the local IP address, and so on. 151 00:09:17,940 --> 00:09:19,540 And for example, also the version number. 152 00:09:21,020 --> 00:09:23,600 So let's take a look at how these devices do. 153 00:09:23,600 --> 00:09:25,960 They're updates, and there are three different methods 154 00:09:25,960 --> 00:09:27,080 how they can do firmware updates. 155 00:09:27,780 --> 00:09:31,000 There's one thing where they can do app updates, 156 00:09:31,060 --> 00:09:32,600 which is the main software, more or less. 157 00:09:33,740 --> 00:09:35,220 So the cloud tells the device, 158 00:09:35,380 --> 00:09:37,300 hey, here's the firmware update under this URL, 159 00:09:37,500 --> 00:09:39,960 and the ND5 checks on, but you should expect it like this. 160 00:09:40,860 --> 00:09:44,200 The second kind of updates are MCU or Wi-Fi updates. 161 00:09:44,360 --> 00:09:45,940 In case of, like, some devices, 162 00:09:45,980 --> 00:09:47,600 it's, for example, the firmware for the ZigBee part. 163 00:09:48,840 --> 00:09:50,460 Here it's also telling, like, 164 00:09:50,500 --> 00:09:52,500 here's the URL where you get the firmware update, 165 00:09:53,600 --> 00:09:55,200 and they have no integrity check. 166 00:09:55,260 --> 00:09:56,800 So basically, they don't tell the device 167 00:09:57,740 --> 00:10:00,420 what kind of ND5 has to be expected. 168 00:10:00,720 --> 00:10:02,000 So if you change anything there, 169 00:10:02,180 --> 00:10:04,120 like, for example, you modify the firmware, 170 00:10:04,760 --> 00:10:06,700 why it gets downloaded to the device, 171 00:10:06,920 --> 00:10:09,600 they have no way to figure out that something was going wrong. 172 00:10:10,580 --> 00:10:12,400 And there's also sub-device updates. 173 00:10:12,720 --> 00:10:15,500 For example, if you have this smoke detector, 174 00:10:15,860 --> 00:10:18,020 it gets updated over ZigBee over the gateway. 175 00:10:19,380 --> 00:10:21,180 Then it's more or less the same thing. 176 00:10:21,900 --> 00:10:23,460 Instead, they have an additional check, 177 00:10:23,460 --> 00:10:24,720 which is the CSE-32. 178 00:10:25,320 --> 00:10:27,040 They give you also MD5, 179 00:10:27,100 --> 00:10:28,280 but MD5 is actually never checked. 180 00:10:28,540 --> 00:10:32,840 So the protection for this kind of firmware is CSE-32, 181 00:10:33,040 --> 00:10:35,160 which is, who thinks it's safe? 182 00:10:36,120 --> 00:10:36,400 Okay. 183 00:10:37,740 --> 00:10:41,040 So one example for a communication relation here 184 00:10:41,040 --> 00:10:45,020 is between the light bulb and the cloud. 185 00:10:45,240 --> 00:10:47,840 So this is just to illustrate, again, 186 00:10:48,480 --> 00:10:51,380 the light bulb itself connects directly to the cloud 187 00:10:51,380 --> 00:10:52,700 and have their own... 188 00:10:52,700 --> 00:10:53,960 all the stuff encrypted, 189 00:10:54,240 --> 00:10:56,020 but it can also connect over your smartphone. 190 00:10:56,840 --> 00:10:58,400 And the keys are different, of course, 191 00:10:58,460 --> 00:10:59,100 in terms of... 192 00:10:59,600 --> 00:11:00,820 it depends on the communication. 193 00:11:02,300 --> 00:11:02,780 Okay. 194 00:11:02,960 --> 00:11:07,380 So how we can disconnect these things 195 00:11:07,380 --> 00:11:09,640 from the big, bad Xiaomi cloud. 196 00:11:09,860 --> 00:11:13,660 And for this, we developed last year 197 00:11:13,660 --> 00:11:16,520 a software which is called DustCloud, 198 00:11:17,360 --> 00:11:19,180 which acts more or less as a proxy. 199 00:11:19,360 --> 00:11:21,860 And this is the way how it works. 200 00:11:21,860 --> 00:11:26,000 So basically, it is in the middle of the communication 201 00:11:26,000 --> 00:11:27,540 between the device and the cloud 202 00:11:28,380 --> 00:11:29,960 and can act as a... 203 00:11:29,960 --> 00:11:31,960 has different modes in this case. 204 00:11:32,980 --> 00:11:34,400 The exact modes are the following. 205 00:11:34,700 --> 00:11:36,600 You can use the DustCloud as a proxy. 206 00:11:36,780 --> 00:11:40,300 So what happens here is it takes the commands 207 00:11:40,300 --> 00:11:41,880 which are coming from the device to the cloud 208 00:11:41,880 --> 00:11:43,320 and forwards them to the cloud. 209 00:11:43,580 --> 00:11:45,580 Or you can just use it as an endpoint 210 00:11:45,580 --> 00:11:47,600 so the commands are not forwarded to the cloud. 211 00:11:48,900 --> 00:11:51,660 The device thinks that it's a legit... 212 00:11:51,660 --> 00:11:54,120 Xiaomi cloud, so it's a perfect emulation of that. 213 00:11:54,280 --> 00:11:56,140 You can read the traffic in plain text 214 00:11:56,140 --> 00:11:57,420 so you know what's going on, 215 00:11:57,480 --> 00:11:58,520 which data is sent to the cloud. 216 00:11:58,720 --> 00:12:01,440 You might even send your own commands to the device. 217 00:12:01,640 --> 00:12:03,780 For example, if you want to do, like, 218 00:12:03,880 --> 00:12:05,540 firmware updates, switch on the light, switch off the light. 219 00:12:06,240 --> 00:12:08,000 And the things what you can do also 220 00:12:08,000 --> 00:12:10,600 is, like, you can change and suppress messages 221 00:12:10,600 --> 00:12:13,060 which are coming from the cloud or going to the cloud. 222 00:12:13,760 --> 00:12:15,600 One interesting question here is, for example, 223 00:12:15,600 --> 00:12:16,420 if you do firmware updates 224 00:12:16,420 --> 00:12:18,520 and the cloud wants to push firmware updates 225 00:12:18,520 --> 00:12:20,820 onto your device, you can just block it 226 00:12:20,820 --> 00:12:21,440 or it can just... 227 00:12:21,660 --> 00:12:25,400 change the URL or the MD5 checksum for your firmware. 228 00:12:26,180 --> 00:12:28,620 To make the desk cloud work, you need a few things. 229 00:12:28,840 --> 00:12:31,220 And the first thing what you need is the device ID. 230 00:12:31,520 --> 00:12:32,680 Then you need the cloud key 231 00:12:32,680 --> 00:12:34,680 so to be able to decrypt the traffic 232 00:12:34,680 --> 00:12:35,840 or to create the traffic. 233 00:12:36,120 --> 00:12:38,220 And then you need to do DNS redirection 234 00:12:38,220 --> 00:12:39,520 so that the whole traffic is, like, 235 00:12:39,520 --> 00:12:40,740 coming actually to your desk cloud. 236 00:12:41,900 --> 00:12:42,300 Okay. 237 00:12:42,600 --> 00:12:44,420 Let's take a quick look at the products. 238 00:12:44,920 --> 00:12:47,100 So I'm sorry for the people who were in the other talk. 239 00:12:47,200 --> 00:12:47,920 It's, like, the same slide. 240 00:12:49,720 --> 00:12:51,420 If you use the... 241 00:12:51,660 --> 00:12:53,560 Mail in China server on your smartphone, 242 00:12:53,840 --> 00:12:55,180 which most of the people do 243 00:12:55,180 --> 00:12:58,420 because this server supports most of the devices, 244 00:12:59,100 --> 00:13:00,660 then your smartphone supports 245 00:13:00,660 --> 00:13:03,180 260 different models of devices. 246 00:13:03,560 --> 00:13:06,020 And these devices could be Wi-Fi, ZigBee, Bluetooth LE, 247 00:13:06,200 --> 00:13:08,400 or, like, even combinations of that. 248 00:13:08,820 --> 00:13:10,960 And so these 260 are more or less 249 00:13:10,960 --> 00:13:12,360 depending on your server location. 250 00:13:12,740 --> 00:13:15,160 If you're coming from Taiwan and using the Taiwanese server, 251 00:13:15,720 --> 00:13:17,420 then it's, like, less. 252 00:13:17,540 --> 00:13:19,320 It's, like, I think, like, less than 100. 253 00:13:20,000 --> 00:13:21,240 If you're in U.S., 254 00:13:21,240 --> 00:13:22,980 it's, I think, like, 20, 25. 255 00:13:23,880 --> 00:13:26,080 The thing here is the models are not always compatible. 256 00:13:26,340 --> 00:13:27,180 So I have a rice cooker, 257 00:13:27,340 --> 00:13:29,020 which I bought in Taiwan, like, two weeks ago. 258 00:13:29,920 --> 00:13:32,520 And I cannot connect it directly to the Chinese cloud. 259 00:13:32,620 --> 00:13:34,660 So I have to modify a few things 260 00:13:34,660 --> 00:13:37,180 because they make sure that you stay in the regions. 261 00:13:39,320 --> 00:13:41,400 Like I said, I mean, not all the products 262 00:13:41,400 --> 00:13:43,100 which are actually sold for the Xiaomi cloud 263 00:13:43,100 --> 00:13:43,920 are actually from Xiaomi. 264 00:13:44,420 --> 00:13:47,900 So this is, like, I mean, this is not very official. 265 00:13:47,940 --> 00:13:50,720 So the thing is I just found it out by configuration files. 266 00:13:51,240 --> 00:13:53,480 So that most of the products are actually by the company Lumi, 267 00:13:53,620 --> 00:13:55,800 which are producing all kinds of different sensors, 268 00:13:55,880 --> 00:13:59,220 like the smoke detector, motion detectors, and so on, 269 00:13:59,720 --> 00:14:00,540 and also the gateways. 270 00:14:00,820 --> 00:14:03,300 Xiaomi itself has only 11%, 271 00:14:03,300 --> 00:14:05,420 and most of the time they are, like, the Wi-Fi routers. 272 00:14:06,260 --> 00:14:07,840 Then we have also Yeelink, 273 00:14:07,920 --> 00:14:10,460 which is making, like, all the lightning stuff. 274 00:14:11,280 --> 00:14:12,920 One important information here is 275 00:14:14,220 --> 00:14:18,700 in this cloud, the smartphones are not connected. 276 00:14:18,860 --> 00:14:20,500 So basically, if you have a smartphone from Xiaomi, 277 00:14:21,800 --> 00:14:23,400 it's not connecting to this cloud. 278 00:14:23,480 --> 00:14:24,560 It's, like, a completely different system. 279 00:14:24,940 --> 00:14:27,240 So we're connecting here only the stuff, 280 00:14:27,340 --> 00:14:30,460 everything which is, like, smart home in your, like, fridge and so on. 281 00:14:30,760 --> 00:14:31,660 It's not the smartphones. 282 00:14:33,380 --> 00:14:33,860 Okay. 283 00:14:34,160 --> 00:14:36,920 So now you might ask, okay, about how many devices 284 00:14:36,920 --> 00:14:38,840 I can give you actually any information. 285 00:14:39,100 --> 00:14:41,720 And from the 360 models I have, actually, 42. 286 00:14:43,180 --> 00:14:45,720 And my inventory is more or less 99 devices 287 00:14:45,720 --> 00:14:50,120 which I bought myself for, you know, using or reverse engineering. 288 00:14:51,700 --> 00:14:52,100 Okay. 289 00:14:53,260 --> 00:14:56,200 Let's talk about the architectures which I use usually 290 00:14:56,200 --> 00:14:57,480 in this kind of IoT devices. 291 00:14:57,840 --> 00:15:00,560 We have the Cortex-A CPUs, 292 00:15:00,600 --> 00:15:02,420 which are more or less the same stuff 293 00:15:02,420 --> 00:15:04,540 which you have in your smartphone or a Raspberry Pi, for example. 294 00:15:05,180 --> 00:15:08,020 Then you have the Cortex-Ms, which are embedded devices. 295 00:15:08,720 --> 00:15:10,040 And they have, like, two flavors. 296 00:15:10,200 --> 00:15:11,860 The one which have, like, Wi-Fi. 297 00:15:11,960 --> 00:15:13,760 So if you have a device which has only Wi-Fi, 298 00:15:13,860 --> 00:15:15,520 then it's very likely that it's a Marvel chip. 299 00:15:15,900 --> 00:15:17,900 If you have devices which can do both things, 300 00:15:17,960 --> 00:15:19,280 like Wi-Fi and Bluetooth LE, 301 00:15:19,460 --> 00:15:20,760 then it's very likely that you have, 302 00:15:21,240 --> 00:15:22,340 like, a media tech chip. 303 00:15:22,920 --> 00:15:25,020 There's also a thing called MIPS, 304 00:15:25,160 --> 00:15:26,960 which are mostly used in IP cameras 305 00:15:26,960 --> 00:15:29,260 or in some routers and extensor, 306 00:15:29,480 --> 00:15:34,040 which some of you might know as ESP8266 or ESP32. 307 00:15:35,400 --> 00:15:37,960 And the focus of this talk will be more or less this. 308 00:15:38,200 --> 00:15:41,520 If you want to know more about the Cortex-A CPUs, 309 00:15:41,520 --> 00:15:44,860 then you need to watch the recording of my other talk. 310 00:15:46,000 --> 00:15:47,160 There's one good news. 311 00:15:47,320 --> 00:15:48,440 I mean, we have a lot of vendors, 312 00:15:48,640 --> 00:15:50,960 but there's one good news, and vendors are always lazy. 313 00:15:51,240 --> 00:15:55,120 So after looking at a lot of firmwares, 314 00:15:55,280 --> 00:15:57,980 what I assume what the development process is looking like, 315 00:15:58,120 --> 00:16:01,080 they're just taking the SDK or toolchain from the chip vendor, 316 00:16:01,260 --> 00:16:02,320 in this case, for example, Marvel. 317 00:16:02,760 --> 00:16:05,500 They add some SDK, which is coming from Xiaomi, 318 00:16:05,980 --> 00:16:08,200 and they're just looking for an example which fits the best. 319 00:16:08,360 --> 00:16:10,400 So for example, okay, we want to switch on the light 320 00:16:10,400 --> 00:16:11,680 and or switch off the light, 321 00:16:11,740 --> 00:16:12,860 so we need to control GPIO. 322 00:16:13,360 --> 00:16:15,260 So in this case, somewhere there's an example, 323 00:16:15,320 --> 00:16:17,180 like, okay, how to switch on GPIO. 324 00:16:17,600 --> 00:16:18,760 So we're taking this example, 325 00:16:18,820 --> 00:16:20,620 change a little bit of source code, compile it. 326 00:16:20,680 --> 00:16:20,960 It runs. 327 00:16:21,800 --> 00:16:22,480 Publish the firmware. 328 00:16:23,120 --> 00:16:25,020 So the good thing about that is 329 00:16:25,020 --> 00:16:26,540 all the firmwares are more or less similar. 330 00:16:27,400 --> 00:16:29,500 The memory layout, the functions, the strings, 331 00:16:29,680 --> 00:16:30,880 if you know one of them, 332 00:16:30,980 --> 00:16:32,360 then it's very likely that you know, 333 00:16:32,880 --> 00:16:34,780 that you get a better impression about the others 334 00:16:34,780 --> 00:16:35,800 as soon as you see the others. 335 00:16:37,260 --> 00:16:41,220 So here's one point which I always love to talk about, 336 00:16:41,340 --> 00:16:42,900 why I hate ESP8266. 337 00:16:43,860 --> 00:16:47,140 Every time I buy a device and I see the following, 338 00:16:47,460 --> 00:16:50,400 I open the device and I see it's an ESP controller, 339 00:16:50,400 --> 00:16:52,260 I'm always very, very sad. 340 00:16:52,760 --> 00:16:53,900 And the reason is the following. 341 00:16:55,260 --> 00:16:58,080 This extensor architecture is like a very weird architecture. 342 00:16:58,500 --> 00:16:59,720 It's like difficult to reverse engineer, 343 00:16:59,960 --> 00:17:00,960 not because it's secure, 344 00:17:01,160 --> 00:17:03,900 but the thing is it's very hard to find a decompiler for that. 345 00:17:04,040 --> 00:17:06,680 I don't know if actually any decompiler is existing. 346 00:17:07,780 --> 00:17:09,800 The disassembler support is also very limited. 347 00:17:10,000 --> 00:17:12,620 So there's a community plugin, I think, for IDA, 348 00:17:12,700 --> 00:17:14,340 but that's all. 349 00:17:14,720 --> 00:17:17,380 And most of the time you can't really use JTAG 350 00:17:18,080 --> 00:17:20,300 because the GPIOs are reused for others. 351 00:17:20,300 --> 00:17:24,300 And I think the JTAG support is actually not existing, more or less. 352 00:17:25,100 --> 00:17:27,460 The good news is it's very easy to replace the firmware. 353 00:17:27,800 --> 00:17:28,800 And there are two reasons. 354 00:17:30,120 --> 00:17:32,360 You have, most of the time, you have some connections. 355 00:17:32,560 --> 00:17:33,940 In this case, you can connect to the serial 356 00:17:33,940 --> 00:17:37,700 or you have the pins which you need to ground 357 00:17:37,700 --> 00:17:39,860 to make it more or less load the firmware. 358 00:17:40,780 --> 00:17:45,340 So the people here who work with ESP8266 probably know that. 359 00:17:45,860 --> 00:17:48,520 And the other thing is, speaking about updates, 360 00:17:48,820 --> 00:17:50,080 you can push the update. 361 00:17:50,300 --> 00:17:50,720 You can push the update over UART 362 00:17:50,720 --> 00:17:53,460 or you can just create your own OTA update. 363 00:17:53,740 --> 00:17:56,300 And the reason here is we don't use SSL 364 00:17:57,240 --> 00:18:00,240 and they transmit their unencrypted firmware over HTTP. 365 00:18:00,800 --> 00:18:02,240 And they don't check for MD5. 366 00:18:02,560 --> 00:18:03,360 So, well. 367 00:18:05,440 --> 00:18:05,920 Yeah. 368 00:18:07,260 --> 00:18:09,160 But reverse engineering of that stuff 369 00:18:09,160 --> 00:18:11,120 is not making a lot of fun. 370 00:18:11,220 --> 00:18:13,180 So this is like a desk lamp that they have from E-Link 371 00:18:13,180 --> 00:18:14,580 and you have to sort the stuff. 372 00:18:14,880 --> 00:18:17,020 And yeah, not so nice. 373 00:18:17,340 --> 00:18:18,160 There's one thing. 374 00:18:18,780 --> 00:18:20,200 If someone of you knows, 375 00:18:20,300 --> 00:18:22,580 how to reverse engineer ESP8266 firmware, 376 00:18:22,700 --> 00:18:24,120 and can show me and it works for me, 377 00:18:24,220 --> 00:18:26,240 you can win this very nice light bulb. 378 00:18:26,480 --> 00:18:27,180 Smart light bulb. 379 00:18:27,360 --> 00:18:28,640 But it's not an ESP, I promise. 380 00:18:29,980 --> 00:18:32,240 So if anyone knows that, 381 00:18:32,440 --> 00:18:33,460 then come to me 382 00:18:33,460 --> 00:18:36,140 and you can get this very nice light bulb. 383 00:18:38,100 --> 00:18:38,540 Okay. 384 00:18:38,940 --> 00:18:40,120 Let's start with our devices. 385 00:18:41,420 --> 00:18:44,280 So here we're talking about the smart home gateway, 386 00:18:44,520 --> 00:18:45,900 the light bulbs and all the LED strips. 387 00:18:46,420 --> 00:18:48,060 I put it in one group because 388 00:18:48,060 --> 00:18:49,620 they have the same processor. 389 00:18:50,300 --> 00:18:50,780 They have the same chip. 390 00:18:50,880 --> 00:18:51,560 They have the same layout. 391 00:18:51,700 --> 00:18:53,160 So it's quite easy to work on them. 392 00:18:54,940 --> 00:18:56,240 So to remind you, 393 00:18:56,340 --> 00:18:57,580 these are the devices. 394 00:18:57,800 --> 00:19:00,340 For example, they have the Wi-Fi connection directly. 395 00:19:00,820 --> 00:19:03,160 And if you take a look at the hardware, 396 00:19:03,320 --> 00:19:03,940 we see the following. 397 00:19:04,180 --> 00:19:05,420 So we have a Marvel CPU, 398 00:19:06,540 --> 00:19:09,160 which is a Cortex M4F with 200 megahertz. 399 00:19:09,280 --> 00:19:10,600 We have a little bit of RAM, 400 00:19:10,760 --> 00:19:12,060 like 512 kilobytes of RAM. 401 00:19:13,220 --> 00:19:15,000 And depending on the device which we have, 402 00:19:15,100 --> 00:19:16,040 the light bulbs, for example, 403 00:19:16,040 --> 00:19:17,920 have 4 megabytes of SPI flash 404 00:19:17,920 --> 00:19:19,340 and the gateway has 16. 405 00:19:19,960 --> 00:19:22,420 All of them have Wi-Fi core. 406 00:19:23,620 --> 00:19:25,160 And for all of them, 407 00:19:25,220 --> 00:19:26,340 the device ID and the key 408 00:19:26,340 --> 00:19:27,740 is stored in the one-time programmer memory. 409 00:19:28,020 --> 00:19:30,060 So if you dump the flash memory, 410 00:19:30,360 --> 00:19:31,800 the SPI flash, for example, 411 00:19:31,860 --> 00:19:32,720 you don't get the key 412 00:19:32,720 --> 00:19:34,260 because it's on the chip. 413 00:19:35,440 --> 00:19:36,740 Especially for the gateways, 414 00:19:36,880 --> 00:19:39,700 it has an NXP ZigBee chip. 415 00:19:39,900 --> 00:19:41,740 So this is like a special, 416 00:19:41,840 --> 00:19:44,180 like a different controller in the device 417 00:19:44,180 --> 00:19:46,020 which takes care of all the ZigBee chips. 418 00:19:47,080 --> 00:19:47,440 OK. 419 00:19:48,580 --> 00:19:49,780 So this is like also an overview 420 00:19:49,780 --> 00:19:51,140 about the chip itself. 421 00:19:51,380 --> 00:19:51,940 So what you see, 422 00:19:51,980 --> 00:19:53,100 so an interesting thing here 423 00:19:53,100 --> 00:19:55,200 is more or less that this Marvel CPU 424 00:19:55,200 --> 00:19:57,160 has two ARM cores, more or less. 425 00:19:57,180 --> 00:19:59,220 So you see one is the Cortex M4F, 426 00:19:59,340 --> 00:20:00,800 which has also this communication processor 427 00:20:00,800 --> 00:20:02,360 which takes care of all, 428 00:20:02,520 --> 00:20:03,920 like for example, Wi-Fi and so on. 429 00:20:05,600 --> 00:20:06,880 For the gateway, the question is, 430 00:20:06,940 --> 00:20:08,200 OK, what kind of ZigBee device 431 00:20:08,200 --> 00:20:09,080 you can connect to that. 432 00:20:09,300 --> 00:20:12,040 And from the Xiaomi cloud, 433 00:20:16,040 --> 00:20:17,820 there's power plugs, motion detection sensors, 434 00:20:17,920 --> 00:20:18,160 buttons. 435 00:20:18,800 --> 00:20:21,000 One of my favorites is also the smoke detector, 436 00:20:21,680 --> 00:20:23,580 which is always a thing like, 437 00:20:23,660 --> 00:20:25,220 hey, let's put security related, 438 00:20:25,280 --> 00:20:27,280 safety related devices into the cloud. 439 00:20:28,140 --> 00:20:29,200 What can possibly go wrong? 440 00:20:29,500 --> 00:20:31,720 Or like some smart door locks. 441 00:20:33,260 --> 00:20:33,720 OK. 442 00:20:33,840 --> 00:20:35,360 So let's get a little bit more technical. 443 00:20:35,620 --> 00:20:38,140 So if you're taking a look 444 00:20:38,140 --> 00:20:39,980 at the partition table of the gateway, 445 00:20:40,100 --> 00:20:40,540 for example, 446 00:20:40,640 --> 00:20:43,600 you will see that they have the typical thing 447 00:20:43,600 --> 00:20:45,820 what nearly all of the IoT providers do. 448 00:20:46,300 --> 00:20:47,720 They have multiple copies 449 00:20:47,720 --> 00:20:48,520 of the operation system. 450 00:20:48,700 --> 00:20:49,480 So basically here, 451 00:20:50,400 --> 00:20:51,840 the application firmware, 452 00:20:52,000 --> 00:20:53,400 this app, FW, 453 00:20:53,660 --> 00:20:54,960 is existing twice. 454 00:20:55,040 --> 00:20:56,940 The Wi-Fi firmware is also existing twice. 455 00:20:57,380 --> 00:20:58,920 And you have a boot sector 456 00:20:58,920 --> 00:21:01,040 and some partition which holds 457 00:21:01,040 --> 00:21:02,540 all the config variables. 458 00:21:04,240 --> 00:21:04,800 Right. 459 00:21:06,520 --> 00:21:07,260 Oh, yeah. 460 00:21:07,340 --> 00:21:08,840 So the important information here 461 00:21:08,840 --> 00:21:10,840 is also that if you have a firmware update, 462 00:21:11,080 --> 00:21:13,100 it's actually the image of one partition. 463 00:21:13,340 --> 00:21:15,020 So if you do a firmware update, 464 00:21:15,220 --> 00:21:17,160 then it just overwrites complete partitions. 465 00:21:17,300 --> 00:21:19,180 It doesn't replace single files 466 00:21:19,180 --> 00:21:20,340 because there's no files available, 467 00:21:20,560 --> 00:21:22,620 but it just completely replaces the partition. 468 00:21:24,520 --> 00:21:26,080 So, how we can get the keys? 469 00:21:26,800 --> 00:21:28,280 It depends a little bit on the device. 470 00:21:28,980 --> 00:21:30,280 So for the smart home gateway, 471 00:21:30,520 --> 00:21:32,060 which I have actually open here, 472 00:21:32,180 --> 00:21:33,400 so if you want to take a look later, 473 00:21:35,860 --> 00:21:37,620 it has a lot of test points 474 00:21:37,620 --> 00:21:38,820 and it has a lot 475 00:21:38,840 --> 00:21:40,560 of SWD enabled by default. 476 00:21:40,900 --> 00:21:42,360 If you ever have this kind of device, 477 00:21:42,520 --> 00:21:44,040 then here is like the layout of the pins. 478 00:21:45,200 --> 00:21:45,940 We have SW, 479 00:21:46,240 --> 00:21:48,000 we have our serial port there, 480 00:21:48,160 --> 00:21:50,420 so we can get some information. 481 00:21:50,840 --> 00:21:52,060 And the information we can get 482 00:21:52,060 --> 00:21:53,620 is actually as soon as the device is running, 483 00:21:53,760 --> 00:21:55,140 we can connect to our SWD 484 00:21:55,140 --> 00:21:56,520 and just dump the whole memory 485 00:21:56,520 --> 00:21:57,880 and get the key from there. 486 00:21:58,580 --> 00:22:01,920 If you connect over serial, 487 00:22:02,180 --> 00:22:04,520 then you can get access to a CLI. 488 00:22:04,600 --> 00:22:06,740 And I mean, this is like a long list of commands, 489 00:22:06,920 --> 00:22:08,440 but the most important are this one. 490 00:22:08,840 --> 00:22:09,480 So you can, 491 00:22:09,980 --> 00:22:11,240 if you connect it over serial, 492 00:22:11,420 --> 00:22:12,680 you can update the firmware directly 493 00:22:12,680 --> 00:22:15,900 without any need to manipulate 494 00:22:15,900 --> 00:22:17,500 like manual models or whatever. 495 00:22:17,660 --> 00:22:20,560 So you can just send your own server there. 496 00:22:21,640 --> 00:22:22,440 So while 497 00:22:23,100 --> 00:22:24,260 it's very easy to 498 00:22:24,840 --> 00:22:25,900 get access to the gateway, 499 00:22:26,020 --> 00:22:26,820 it's a little bit more difficult 500 00:22:26,820 --> 00:22:29,580 if you have, for example, a light bulb. 501 00:22:29,980 --> 00:22:31,160 And then the question is, 502 00:22:31,260 --> 00:22:33,700 hey, can we get access to this thing 503 00:22:34,420 --> 00:22:35,500 without a hardware attack? 504 00:22:35,600 --> 00:22:37,140 And a hardware attack, what I mean with that is, 505 00:22:38,840 --> 00:22:40,380 so the light bulb that you see here in the picture 506 00:22:40,380 --> 00:22:41,620 is actually, at the end of the day, 507 00:22:41,660 --> 00:22:44,140 if you want to extract the chip from there, 508 00:22:44,260 --> 00:22:45,720 you have to saw it open 509 00:22:45,720 --> 00:22:48,180 because it's like, you know, 510 00:22:48,200 --> 00:22:50,460 a little bit complex to open it. 511 00:22:50,780 --> 00:22:52,500 So this is usually the case 512 00:22:52,500 --> 00:22:54,020 which you don't want to use after that, 513 00:22:54,120 --> 00:22:56,180 after you get access to this device. 514 00:22:56,420 --> 00:22:58,020 So what we want to do is, 515 00:22:58,120 --> 00:22:59,940 we want to get some access on the device 516 00:22:59,940 --> 00:23:01,320 without sawing the device open. 517 00:23:02,480 --> 00:23:04,220 And the good news here is, again, 518 00:23:04,400 --> 00:23:05,680 the firmwares are not signed, 519 00:23:05,680 --> 00:23:07,100 so what we can just do is, 520 00:23:07,160 --> 00:23:09,100 hey, just create a modified firmware 521 00:23:09,100 --> 00:23:10,360 which gives us the key automatically. 522 00:23:12,440 --> 00:23:12,920 Okay. 523 00:23:13,400 --> 00:23:14,860 The bonus of that would be, 524 00:23:14,900 --> 00:23:16,040 we don't need a hardware access, 525 00:23:16,340 --> 00:23:17,740 but there's a big problem. 526 00:23:17,880 --> 00:23:20,280 And the problem is, in a difference, 527 00:23:20,460 --> 00:23:21,720 for example, to the vacuum cleaner 528 00:23:21,720 --> 00:23:23,680 which runs a complete Ubuntu operation system, 529 00:23:24,100 --> 00:23:26,180 these light bulbs have a bare metal OS. 530 00:23:26,480 --> 00:23:27,980 So basically you have no, 531 00:23:28,340 --> 00:23:30,240 so you have just one binary which is running 532 00:23:30,240 --> 00:23:32,700 and no file system where you can change 533 00:23:32,700 --> 00:23:33,560 individual binaries. 534 00:23:37,860 --> 00:23:38,260 Okay. 535 00:23:40,760 --> 00:23:42,660 So let's talk about binary patching. 536 00:23:43,560 --> 00:23:44,960 So if you have a binary, 537 00:23:45,100 --> 00:23:46,760 what happens quite often is 538 00:23:46,760 --> 00:23:47,620 that we have some, 539 00:23:47,660 --> 00:23:48,860 in our original code, 540 00:23:49,000 --> 00:23:50,480 we have some function which is running 541 00:23:50,480 --> 00:23:51,300 and at some point, 542 00:23:51,340 --> 00:23:52,000 you have some, for example, 543 00:23:52,040 --> 00:23:53,340 branch links which are jumping 544 00:23:53,340 --> 00:23:55,320 in some other code. 545 00:23:56,660 --> 00:23:58,900 Our goal, if you want to modify this thing, 546 00:23:59,000 --> 00:24:01,120 is that we have some patch code 547 00:24:01,560 --> 00:24:05,200 and we somehow manipulate this branch link 548 00:24:05,200 --> 00:24:05,660 that it includes in the binary. 549 00:24:05,680 --> 00:24:07,020 So instead of going to the original code, 550 00:24:07,120 --> 00:24:08,100 it goes into the patch code. 551 00:24:09,200 --> 00:24:11,080 To not break the functionality after that, 552 00:24:11,520 --> 00:24:12,980 we want to go back 553 00:24:13,520 --> 00:24:15,240 at the end, so we want to call 554 00:24:15,240 --> 00:24:16,540 the original function after that again. 555 00:24:17,960 --> 00:24:19,120 So what does it mean? 556 00:24:19,260 --> 00:24:21,620 So we need to modify the program flow, 557 00:24:21,760 --> 00:24:23,040 we need to add existing code, 558 00:24:23,590 --> 00:24:24,140 additional code, 559 00:24:24,620 --> 00:24:26,500 but the trick here is also we need to use 560 00:24:27,060 --> 00:24:27,820 existing functions, 561 00:24:28,740 --> 00:24:30,180 because all the things which are not 562 00:24:30,180 --> 00:24:31,040 present in the firmware, 563 00:24:31,040 --> 00:24:32,680 we need to bring somehow into the firmware. 564 00:24:34,160 --> 00:24:34,600 Okay, 565 00:24:34,700 --> 00:24:36,700 so why is binary patching 566 00:24:36,700 --> 00:24:37,540 so difficult? 567 00:24:38,960 --> 00:24:40,240 Especially for ARM, 568 00:24:40,340 --> 00:24:42,720 as soon as you override branch instructions, 569 00:24:43,040 --> 00:24:44,600 the new address is more or less dependent 570 00:24:44,600 --> 00:24:45,440 on the program counter. 571 00:24:45,640 --> 00:24:47,560 So you cannot work with fixed 572 00:24:48,740 --> 00:24:50,720 addresses in the branch links. 573 00:24:51,640 --> 00:24:52,540 Most of the time, 574 00:24:52,560 --> 00:24:53,740 if you want to modify binaries, 575 00:24:53,740 --> 00:24:55,680 you need to write new code in assembly, 576 00:24:56,480 --> 00:24:57,760 which some people can do, 577 00:24:57,900 --> 00:25:00,440 but I think most of the time it's a little bit risky. 578 00:25:01,160 --> 00:25:02,240 Especially if you have a light bulb, 579 00:25:02,340 --> 00:25:04,600 they have only one shot to put your firmware on it. 580 00:25:04,640 --> 00:25:06,460 If it crashes, then the light bulb is done. 581 00:25:07,580 --> 00:25:08,720 Also what you need to do is 582 00:25:08,720 --> 00:25:10,040 to model the address space, 583 00:25:10,780 --> 00:25:12,040 so you need to have some idea 584 00:25:12,040 --> 00:25:13,780 what the RAM looks like, 585 00:25:13,900 --> 00:25:15,960 if you want to access the ROM and so on. 586 00:25:16,060 --> 00:25:17,380 And you need to make sure that you have 587 00:25:17,380 --> 00:25:18,880 specific free space for patches later. 588 00:25:20,580 --> 00:25:22,360 And you need to call existing functions, 589 00:25:23,720 --> 00:25:24,080 and 590 00:25:24,740 --> 00:25:25,920 now the problem is, 591 00:25:26,020 --> 00:25:27,080 because we have a lot of devices, 592 00:25:27,260 --> 00:25:29,680 you need to handle different firmware versions and different devices. 593 00:25:31,040 --> 00:25:31,680 So luckily for us, 594 00:25:31,700 --> 00:25:34,340 there's a great, great tool for that, 595 00:25:34,440 --> 00:25:35,640 which is called Nexmon. 596 00:25:36,600 --> 00:25:38,660 And this takes care 597 00:25:38,660 --> 00:25:40,240 of most of these problems. 598 00:25:41,100 --> 00:25:42,040 So what's Nexmon? 599 00:25:42,440 --> 00:25:44,720 Nexmon was initially developed 600 00:25:44,720 --> 00:25:46,500 by Daniel Wegemer and Matthias Schulz 601 00:25:47,000 --> 00:25:48,840 at the Zemo Labs and at the TU Darmstadt. 602 00:25:49,060 --> 00:25:50,540 Daniel is also here in the first row, 603 00:25:50,580 --> 00:25:51,640 so if you have any questions for him, 604 00:25:51,640 --> 00:25:53,000 he is happy to answer that. 605 00:25:53,200 --> 00:25:56,400 It's a C-based firmware binary patching framework, 606 00:25:56,580 --> 00:25:58,540 and it supports Cortex-A 607 00:25:58,540 --> 00:25:59,860 and Cortex-M binaries. 608 00:26:01,040 --> 00:26:02,600 So you can modify Cortex-M firmware, 609 00:26:02,760 --> 00:26:05,040 but you can also modify Cortex-A binaries 610 00:26:05,840 --> 00:26:07,160 in your Linux system, for example. 611 00:26:07,820 --> 00:26:08,300 And 612 00:26:09,000 --> 00:26:11,140 the main use case of this framework 613 00:26:11,140 --> 00:26:12,960 was to modify the full Mac 614 00:26:12,960 --> 00:26:14,980 Wi-Fi firmware in, for example, 615 00:26:14,980 --> 00:26:16,940 smartphones or like on a Raspberry Pi. 616 00:26:17,120 --> 00:26:18,600 So what you can do with that is 617 00:26:18,600 --> 00:26:20,960 you can modify the ARM firmware 618 00:26:20,960 --> 00:26:21,560 in your 619 00:26:24,200 --> 00:26:25,320 smartphone's Wi-Fi chip. 620 00:26:26,240 --> 00:26:26,720 So 621 00:26:27,380 --> 00:26:28,220 for example, 622 00:26:28,300 --> 00:26:30,580 the idea of that is to enable 623 00:26:31,260 --> 00:26:34,700 so you can see Wi-Fi traffic, 624 00:26:34,860 --> 00:26:36,720 which is not supposed for you, 625 00:26:36,780 --> 00:26:37,580 or you can do other stuff. 626 00:26:39,060 --> 00:26:40,300 For our use case, 627 00:26:40,520 --> 00:26:42,340 we want to use it to modify 628 00:26:42,340 --> 00:26:43,380 IoT firmware. 629 00:26:45,240 --> 00:26:46,300 Nexmon itself is 630 00:26:46,920 --> 00:26:48,060 quite black magic, 631 00:26:48,140 --> 00:26:49,080 so it contains a lot of 632 00:26:49,080 --> 00:26:52,280 Mac files, scripts, and GCC plug-in. 633 00:26:52,460 --> 00:26:54,860 So this is more or less the map 634 00:26:54,860 --> 00:26:56,580 what it does for the Wi-Fi firmware, 635 00:26:56,780 --> 00:26:58,440 but for us, 636 00:26:59,600 --> 00:27:01,140 we don't need so much. 637 00:27:01,260 --> 00:27:01,940 So for us, 638 00:27:01,960 --> 00:27:03,660 the simplified version is absolutely okay. 639 00:27:04,300 --> 00:27:05,980 The interesting thing here is that 640 00:27:07,360 --> 00:27:08,180 what does 641 00:27:09,280 --> 00:27:10,580 Nexmon does for us 642 00:27:10,580 --> 00:27:12,240 is taking our patch 643 00:27:12,920 --> 00:27:14,440 and it makes sure that it 644 00:27:14,440 --> 00:27:15,700 gets linked to the correct functions 645 00:27:15,700 --> 00:27:17,340 which are already existing in the code. 646 00:27:17,480 --> 00:27:19,020 So basically it takes care of all the linking 647 00:27:19,020 --> 00:27:21,980 and so on, so we don't need to do it manually. 648 00:27:23,160 --> 00:27:24,460 The requirements for that 649 00:27:24,460 --> 00:27:25,040 are the following. 650 00:27:25,300 --> 00:27:26,740 So we need at some point, we need to have the 651 00:27:29,620 --> 00:27:30,420 memory layout, 652 00:27:30,640 --> 00:27:32,040 so where the functions are 653 00:27:32,040 --> 00:27:34,340 or how the binary is loaded, for example, 654 00:27:34,340 --> 00:27:35,580 or how the memory is looking like. 655 00:27:36,240 --> 00:27:38,160 If you want to write patch code, 656 00:27:38,340 --> 00:27:40,360 then we need to have free space 657 00:27:40,360 --> 00:27:42,180 on the flash for the patch code. 658 00:27:42,380 --> 00:27:43,760 If you don't have enough free space, 659 00:27:43,960 --> 00:27:44,460 for example, 660 00:27:45,320 --> 00:27:47,560 if the flash chip is already full, 661 00:27:47,740 --> 00:27:49,360 then you need to make some free space 662 00:27:49,360 --> 00:27:51,580 in terms of delete functions which are maybe not used 663 00:27:51,580 --> 00:27:52,460 or hopefully not used. 664 00:27:53,840 --> 00:27:55,940 And if you want to reuse functions, 665 00:27:56,680 --> 00:27:58,180 you need to know the function names 666 00:27:58,920 --> 00:27:59,960 and the signatures 667 00:27:59,960 --> 00:28:01,340 and you need to know where they are. 668 00:28:02,560 --> 00:28:04,680 Okay, like I said, this is a step-by-step approach. 669 00:28:04,820 --> 00:28:05,780 Let's start with step one 670 00:28:05,780 --> 00:28:06,800 and retrieving the firmware. 671 00:28:07,620 --> 00:28:09,920 So the whole time I was talking about the smart home gateway 672 00:28:09,920 --> 00:28:11,660 but it's more or less the same for the laptops 673 00:28:11,660 --> 00:28:14,440 and for all the other products 674 00:28:14,440 --> 00:28:15,420 which are based on this chip. 675 00:28:16,580 --> 00:28:17,980 Okay, so how do we get the firmware? 676 00:28:18,800 --> 00:28:19,500 There's one possibility. 677 00:28:19,880 --> 00:28:22,220 If you open the device, you can just dump the SPI flash, 678 00:28:22,440 --> 00:28:24,300 for example, JTAG SWD 679 00:28:25,140 --> 00:28:26,260 or desolder the flash. 680 00:28:27,480 --> 00:28:28,360 A helpful tool 681 00:28:28,440 --> 00:28:30,580 if you want to use JTAG or SWD, 682 00:28:30,680 --> 00:28:31,740 you can use 683 00:28:32,740 --> 00:28:34,500 Raspberry Pi. It's absolutely sufficient 684 00:28:34,500 --> 00:28:35,880 for that and with OpenOCD. 685 00:28:36,360 --> 00:28:38,400 The good thing is that Marvel gives you all the tools 686 00:28:38,400 --> 00:28:40,640 ready to connect to this chip 687 00:28:40,640 --> 00:28:42,520 over SWD or JTAG. 688 00:28:42,740 --> 00:28:43,880 If you desolder the chip, 689 00:28:44,020 --> 00:28:45,400 you can read it out with FlashRAM. 690 00:28:46,060 --> 00:28:48,340 The other possibility is that you intercept the traffic 691 00:28:48,340 --> 00:28:49,020 by the firmware update. 692 00:28:49,980 --> 00:28:51,540 But here the idea is the following. 693 00:28:51,760 --> 00:28:54,440 It's very advised that you actually block the updates 694 00:28:54,440 --> 00:28:55,240 that you get the 695 00:28:55,900 --> 00:28:57,680 that you intercept the firmware update 696 00:28:58,440 --> 00:28:59,180 and block it for the device. 697 00:29:00,700 --> 00:29:02,020 Because you don't want the device 698 00:29:02,020 --> 00:29:03,440 to actually update the firmware. 699 00:29:04,440 --> 00:29:06,440 And here Xiaomi gets a little bit sneaky 700 00:29:06,440 --> 00:29:08,080 so if DNS is failing, 701 00:29:08,240 --> 00:29:09,940 if they try the first time to update 702 00:29:09,940 --> 00:29:12,300 the firmware with a DNS address 703 00:29:12,300 --> 00:29:13,840 and it fails, 704 00:29:13,860 --> 00:29:14,940 then they send the command again 705 00:29:14,940 --> 00:29:16,440 this time with the IP address. 706 00:29:16,760 --> 00:29:19,220 So if you're clever enough to redirect the host names 707 00:29:19,220 --> 00:29:21,260 in your router, then in the second time 708 00:29:21,260 --> 00:29:22,160 this device will be updated. 709 00:29:24,140 --> 00:29:25,160 For some devices, 710 00:29:25,320 --> 00:29:27,640 not for this lightning stuff, but for our devices, 711 00:29:28,440 --> 00:29:29,700 SSL, most of the time 712 00:29:30,440 --> 00:29:32,260 you can just use fake certificates 713 00:29:32,260 --> 00:29:34,120 or what I experience 714 00:29:34,120 --> 00:29:35,520 if you block SSL, at some point 715 00:29:35,520 --> 00:29:37,120 they fall back to HTTP 716 00:29:37,120 --> 00:29:38,420 and they do the update over HTTP. 717 00:29:39,540 --> 00:29:41,780 The goal of this whole thing is to retrieve 718 00:29:41,780 --> 00:29:43,300 the special URL for the firmware update 719 00:29:43,300 --> 00:29:44,360 and the reason is the following. 720 00:29:46,100 --> 00:29:47,480 If you want to download the firmware, 721 00:29:47,620 --> 00:29:49,800 the file names are not easy guessable 722 00:29:49,800 --> 00:29:51,980 because the file name 723 00:29:51,980 --> 00:29:52,920 of the firmware update 724 00:29:53,640 --> 00:29:55,940 has the MD5 checksum of the firmware update. 725 00:29:56,100 --> 00:29:57,640 As you usually don't know it, 726 00:29:58,440 --> 00:29:58,740 you can download it. 727 00:29:58,920 --> 00:30:00,480 The other thing is where CDN 728 00:30:00,480 --> 00:30:01,280 is using authentication, 729 00:30:01,920 --> 00:30:04,720 so basically you get some kind of token 730 00:30:04,720 --> 00:30:05,640 to download the firmware, 731 00:30:06,360 --> 00:30:08,660 which is I think valid for two months or so, 732 00:30:08,820 --> 00:30:10,680 but still you need to have this token 733 00:30:10,680 --> 00:30:12,140 to be able to download the firmware. 734 00:30:12,340 --> 00:30:14,540 If I just give you the URL with the file name 735 00:30:14,540 --> 00:30:16,080 you can download it, you get the access denied. 736 00:30:16,480 --> 00:30:17,980 So you need this URL. 737 00:30:19,800 --> 00:30:21,340 Let's assume that you have it already. 738 00:30:21,540 --> 00:30:23,420 So you intercepted it for example with Wireshark 739 00:30:23,420 --> 00:30:24,820 or you have the firmware image, 740 00:30:24,960 --> 00:30:27,180 the next step what we need to do is we need to pass the firmware. 741 00:30:29,100 --> 00:30:30,820 Firmwares have the bad attitude 742 00:30:30,820 --> 00:30:32,720 that they have proprietary format 743 00:30:32,720 --> 00:30:34,400 and it's like difficult to load 744 00:30:34,400 --> 00:30:35,420 in IDA Pro for example 745 00:30:36,020 --> 00:30:38,040 and you need to know where the segments are, 746 00:30:38,160 --> 00:30:39,620 what the segments are, how big they are 747 00:30:39,620 --> 00:30:41,320 and where the entry point is. 748 00:30:42,300 --> 00:30:44,380 So our goal here is to convert the firmware 749 00:30:44,380 --> 00:30:46,440 to some kind of ELF format or ELF file 750 00:30:46,440 --> 00:30:48,180 because the ELF file has some description 751 00:30:48,180 --> 00:30:50,500 and it tells you where the segments for example are. 752 00:30:50,860 --> 00:30:52,460 So we need to understand how it works. 753 00:30:52,760 --> 00:30:54,220 And the idea here is like 754 00:30:54,220 --> 00:30:56,000 the typical thing that you do all the time 755 00:30:56,000 --> 00:30:57,720 so you get some kind of SDK from the vendor 756 00:30:58,440 --> 00:31:00,440 you can download the Amazon SDK 757 00:31:01,420 --> 00:31:03,200 and you just compile a sample firmware 758 00:31:03,200 --> 00:31:04,200 and you just look 759 00:31:04,200 --> 00:31:06,360 okay so at some point you have the ELF file 760 00:31:06,360 --> 00:31:08,300 and then you look okay what's the output 761 00:31:08,300 --> 00:31:09,760 and then you try to figure out 762 00:31:09,760 --> 00:31:10,480 what's going on there. 763 00:31:12,760 --> 00:31:14,420 And for Marvel it's the case 764 00:31:14,420 --> 00:31:16,060 that it creates at some point the ELF file 765 00:31:16,060 --> 00:31:18,000 and they're using then later in the script 766 00:31:18,000 --> 00:31:18,840 a tool which is called 767 00:31:20,440 --> 00:31:21,320 EFX firmware 768 00:31:21,320 --> 00:31:23,700 which converts the firmware to a binary format. 769 00:31:25,280 --> 00:31:26,420 So we did back then 770 00:31:26,420 --> 00:31:28,020 reverse engineering for you 771 00:31:28,440 --> 00:31:30,700 so this is how the header looks like 772 00:31:30,700 --> 00:31:31,520 from the format 773 00:31:31,520 --> 00:31:34,140 and we have a tool which can go 774 00:31:34,140 --> 00:31:35,640 from this binary format into 775 00:31:36,540 --> 00:31:37,480 the ELF format 776 00:31:37,480 --> 00:31:39,500 and from ELF back to binary. 777 00:31:40,440 --> 00:31:41,480 There's one fun thing 778 00:31:42,080 --> 00:31:43,100 which I never tested 779 00:31:43,100 --> 00:31:44,520 but if someone of you has the 780 00:31:45,600 --> 00:31:47,360 smart Barbie doll 781 00:31:47,360 --> 00:31:49,180 it has the same chip actually. 782 00:31:49,440 --> 00:31:51,760 So this is for the people of you 783 00:31:51,760 --> 00:31:52,500 who don't know that 784 00:31:52,500 --> 00:31:53,280 it's not from Xiaomi 785 00:31:53,280 --> 00:31:55,100 it's from the Barbie company. 786 00:31:59,280 --> 00:32:00,380 It's actually a doll where 787 00:32:00,380 --> 00:32:02,040 the kids can tell your Barbie 788 00:32:02,040 --> 00:32:04,000 some stories and then as parents 789 00:32:04,000 --> 00:32:05,300 you can listen to the stories 790 00:32:06,020 --> 00:32:06,900 on your smartphone. 791 00:32:07,080 --> 00:32:09,080 So if your kid is telling your Barbie 792 00:32:09,080 --> 00:32:11,540 a secret then you as a parent 793 00:32:11,540 --> 00:32:12,040 will know that. 794 00:32:12,420 --> 00:32:15,780 In case of MediaTek 795 00:32:15,780 --> 00:32:17,440 the format is looking quite similar 796 00:32:18,660 --> 00:32:20,340 again we have some kind of segments 797 00:32:20,340 --> 00:32:22,100 this time we use some kind of 798 00:32:22,100 --> 00:32:24,500 checksum while the Marvel firmware 799 00:32:24,500 --> 00:32:26,200 has no checksum in there 800 00:32:26,200 --> 00:32:28,040 basically if you change specifically 801 00:32:28,720 --> 00:32:31,260 if you change the URL in the binary 802 00:32:31,260 --> 00:32:32,560 then you can upload the binary 803 00:32:32,560 --> 00:32:33,340 and it should be fine. 804 00:32:34,940 --> 00:32:36,980 So let's take a look at 805 00:32:36,980 --> 00:32:38,020 the workflow for Marvel. 806 00:32:38,280 --> 00:32:39,940 So let's assume we have intercepted 807 00:32:39,940 --> 00:32:40,800 the firmware or we downloaded 808 00:32:40,800 --> 00:32:41,420 the firmware ourselves 809 00:32:41,420 --> 00:32:46,520 how we pass the firmware. 810 00:32:47,200 --> 00:32:48,640 So as soon as we run our 811 00:32:48,640 --> 00:32:49,980 parser tool what it does is 812 00:32:49,980 --> 00:32:51,580 it figures out where the sections are 813 00:32:51,580 --> 00:32:54,540 and creates for us the sections 814 00:32:54,540 --> 00:32:55,900 as individual files 815 00:32:56,440 --> 00:32:57,480 and two kinds of scripts. 816 00:32:58,440 --> 00:33:00,140 And one script is the description 817 00:33:00,140 --> 00:33:02,340 of the sections for the LD command 818 00:33:02,340 --> 00:33:03,800 which is later important for 819 00:33:04,380 --> 00:33:05,860 manufacturers to rebuild the firmware 820 00:33:06,280 --> 00:33:08,240 and the second script which you get 821 00:33:08,240 --> 00:33:10,240 out of our tool is that it gets 822 00:33:10,240 --> 00:33:11,740 commands for the object copy 823 00:33:11,740 --> 00:33:12,880 and also the LD command. 824 00:33:13,160 --> 00:33:16,340 So if you run the 825 00:33:16,340 --> 00:33:18,000 builder command then at the end of the day 826 00:33:18,000 --> 00:33:19,480 you have an L file which is correctly 827 00:33:19,480 --> 00:33:22,220 which is built up correctly. 828 00:33:22,680 --> 00:33:23,960 As an example here 829 00:33:25,060 --> 00:33:26,380 this is the command 830 00:33:26,380 --> 00:33:27,840 which I run for the firmware 831 00:33:28,780 --> 00:33:30,440 which I downloaded like a few days ago 832 00:33:31,160 --> 00:33:31,960 and I 833 00:33:32,960 --> 00:33:34,340 actually yeah it's a quite new firmware 834 00:33:34,340 --> 00:33:35,100 you see the build date 835 00:33:35,100 --> 00:33:36,780 this is the date where we 836 00:33:37,720 --> 00:33:38,800 build actually the firmware 837 00:33:41,300 --> 00:33:42,380 and you see it has 838 00:33:42,380 --> 00:33:42,900 three sections 839 00:33:43,460 --> 00:33:44,640 and when we run 840 00:33:45,340 --> 00:33:48,120 this is how the thing looks like 841 00:33:48,120 --> 00:33:49,980 and as soon as I run this builder script 842 00:33:49,980 --> 00:33:50,680 which you see there 843 00:33:51,200 --> 00:33:53,220 at the end of the day we get an L file 844 00:33:53,220 --> 00:33:56,640 which is also correctly working. 845 00:33:56,860 --> 00:33:58,420 In theory you could take this L file 846 00:33:58,440 --> 00:33:59,860 and run it on a Linux 847 00:33:59,860 --> 00:34:01,380 but I think it shouldn't do anything 848 00:34:01,380 --> 00:34:02,680 at least yeah. 849 00:34:03,720 --> 00:34:05,240 So having the L file now 850 00:34:05,240 --> 00:34:06,200 what can we do next? 851 00:34:06,580 --> 00:34:08,760 Well we can load it in a disassembler 852 00:34:08,760 --> 00:34:11,180 so I use IDA because I got at some point 853 00:34:11,180 --> 00:34:11,700 an IDA license 854 00:34:11,700 --> 00:34:15,060 I think most of you will use 855 00:34:15,060 --> 00:34:16,680 a different disassembler 856 00:34:16,680 --> 00:34:20,180 but you can just load the file into IDA 857 00:34:20,180 --> 00:34:21,700 and you can figure out 858 00:34:21,700 --> 00:34:23,860 where the interesting areas are. 859 00:34:24,040 --> 00:34:25,660 What I figured out at some point 860 00:34:25,660 --> 00:34:27,880 is that you have some spaces 861 00:34:28,440 --> 00:34:29,980 in the firmware where 862 00:34:29,980 --> 00:34:32,140 at some point the data from 863 00:34:32,140 --> 00:34:33,380 the one term programmer memory 864 00:34:33,380 --> 00:34:35,660 is copied into this space 865 00:34:35,660 --> 00:34:37,120 so basically what you see here 866 00:34:37,120 --> 00:34:39,420 are the three main interesting fields 867 00:34:39,420 --> 00:34:41,920 tag MAC is the place where 868 00:34:41,920 --> 00:34:43,520 the MAC address is stored later 869 00:34:44,280 --> 00:34:46,480 tag DID is the address 870 00:34:46,480 --> 00:34:47,100 where the 871 00:34:49,440 --> 00:34:50,480 device ID is stored 872 00:34:50,480 --> 00:34:51,080 and the key 873 00:34:51,940 --> 00:34:53,800 below there is also the model number 874 00:34:53,800 --> 00:34:54,680 so theoretically you could 875 00:34:55,460 --> 00:34:57,480 get the model number but the model number is known 876 00:34:58,440 --> 00:34:59,540 and there is also the area 877 00:34:59,540 --> 00:35:01,060 for the server 878 00:35:01,840 --> 00:35:04,140 for example if you have a Taiwanese device 879 00:35:04,140 --> 00:35:05,520 then it's like TW there 880 00:35:05,520 --> 00:35:06,940 or if you have a Chinese device it's empty 881 00:35:08,240 --> 00:35:10,320 right so now we need to prepare 882 00:35:11,280 --> 00:35:12,060 all the data 883 00:35:12,060 --> 00:35:12,780 for next month 884 00:35:14,400 --> 00:35:16,300 so I showed you 885 00:35:16,300 --> 00:35:17,620 the partition layout in the beginning 886 00:35:17,620 --> 00:35:20,180 so what we figured out is that 887 00:35:20,180 --> 00:35:22,280 the partition has a size of 614 KB 888 00:35:22,280 --> 00:35:24,160 and the size of the original firmware 889 00:35:24,160 --> 00:35:25,780 is only 569 890 00:35:25,780 --> 00:35:27,680 so basically we have a lot of space for our patches 891 00:35:28,440 --> 00:35:29,720 so the trick is 892 00:35:29,720 --> 00:35:31,440 as we need space for our patches 893 00:35:31,440 --> 00:35:34,180 we just append 1280 894 00:35:35,100 --> 00:35:35,640 nullbots 895 00:35:35,640 --> 00:35:37,720 which is that baton hex 896 00:35:38,440 --> 00:35:39,320 so we end 897 00:35:39,320 --> 00:35:41,320 with a new section file 898 00:35:42,700 --> 00:35:44,340 we have chosen section 899 00:35:44,340 --> 00:35:45,860 file 2 because it contains all the code 900 00:35:46,820 --> 00:35:48,400 where we have the original section 901 00:35:48,400 --> 00:35:50,200 and we have 902 00:35:50,200 --> 00:35:50,820 the patch code 903 00:35:50,820 --> 00:35:52,500 so this is looking like this 904 00:35:52,500 --> 00:35:54,560 so now we have some space 905 00:35:55,460 --> 00:35:56,000 to 906 00:35:58,440 --> 00:35:58,960 put our patches in 907 00:35:58,960 --> 00:36:01,540 if you do it in Linux it's quite easy 908 00:36:01,540 --> 00:36:04,160 you can just use DD to do that 909 00:36:04,160 --> 00:36:05,600 so at the end of the day we have 910 00:36:05,600 --> 00:36:06,900 some space for our patches 911 00:36:07,360 --> 00:36:08,500 now the next step is 912 00:36:08,500 --> 00:36:10,620 we need to figure out what are the function names 913 00:36:11,720 --> 00:36:13,620 and for example for every function 914 00:36:13,620 --> 00:36:15,780 that you want to use later in the source code 915 00:36:15,780 --> 00:36:17,060 in your own patch 916 00:36:17,060 --> 00:36:19,260 you need to figure out if it's existing already 917 00:36:19,260 --> 00:36:20,780 in the firmware or not 918 00:36:20,780 --> 00:36:22,040 or you have to introduce it later 919 00:36:22,040 --> 00:36:23,460 so how we do that 920 00:36:24,840 --> 00:36:26,300 again we take the SDK 921 00:36:26,300 --> 00:36:27,720 we compile an example project 922 00:36:28,120 --> 00:36:29,960 which more or less contains all the stuff 923 00:36:29,960 --> 00:36:30,980 that we need later 924 00:36:30,980 --> 00:36:33,700 and because it's compiled with debug symbols 925 00:36:33,700 --> 00:36:35,860 what we can do is we can load the binary 926 00:36:35,860 --> 00:36:36,460 into IDA 927 00:36:36,460 --> 00:36:39,520 and make a diff between 928 00:36:40,900 --> 00:36:41,940 our unknown 929 00:36:42,680 --> 00:36:43,940 executable and our 930 00:36:43,940 --> 00:36:44,920 example executable 931 00:36:45,600 --> 00:36:47,260 for IDA there is a free plugin 932 00:36:47,260 --> 00:36:48,200 which is called Bindiff 933 00:36:48,200 --> 00:36:48,940 which you can download 934 00:36:50,080 --> 00:36:52,100 for me only version 4.2 works 935 00:36:52,100 --> 00:36:54,600 so 4.3 is somehow completely bugged 936 00:36:54,600 --> 00:36:55,920 and doesn't work for me 937 00:36:56,440 --> 00:36:58,640 so how does it look like 938 00:36:58,640 --> 00:37:00,700 so you see here on the left side 939 00:37:00,700 --> 00:37:01,400 the name primary 940 00:37:01,400 --> 00:37:02,700 these are all the unknown functions 941 00:37:02,700 --> 00:37:04,980 which are in the binary 942 00:37:04,980 --> 00:37:06,500 and on the right side 943 00:37:06,500 --> 00:37:08,080 the name secondary 944 00:37:08,080 --> 00:37:09,080 these are actually the functions 945 00:37:09,080 --> 00:37:10,620 which I use for my example project 946 00:37:10,620 --> 00:37:11,500 which I compiled myself 947 00:37:11,500 --> 00:37:14,040 so basically I can align all the functions 948 00:37:14,940 --> 00:37:16,140 all the unknown functions 949 00:37:17,480 --> 00:37:21,000 into my unknown firmware 950 00:37:21,000 --> 00:37:22,600 and I know more like 951 00:37:24,600 --> 00:37:25,240 and what's going on 952 00:37:25,240 --> 00:37:25,720 after that 953 00:37:26,620 --> 00:37:28,760 so for example for this gateway 954 00:37:28,760 --> 00:37:30,800 I have chosen a few functions 955 00:37:30,800 --> 00:37:32,720 which are interesting for me 956 00:37:32,720 --> 00:37:35,260 for example there is a http c get command 957 00:37:35,260 --> 00:37:37,020 which is doing a get query 958 00:37:37,020 --> 00:37:38,020 to a web server 959 00:37:38,020 --> 00:37:40,720 I found out what the address is for that 960 00:37:40,720 --> 00:37:42,640 there is a bin2hex command 961 00:37:42,640 --> 00:37:44,740 as nprintf is also quite useful 962 00:37:45,400 --> 00:37:46,920 vmprintf is a console output 963 00:37:46,920 --> 00:37:48,180 so if you connect it over serial 964 00:37:48,180 --> 00:37:51,680 then this command is used to output 965 00:37:54,600 --> 00:37:55,380 and for the xiaomi cloud specific 966 00:37:55,380 --> 00:37:56,540 there is a command called 967 00:37:56,540 --> 00:37:59,180 or a function called otutimerinfo 968 00:37:59,180 --> 00:38:01,460 and this is actually the 969 00:38:01,460 --> 00:38:04,080 login command to the cloud 970 00:38:04,680 --> 00:38:06,120 if the device is 971 00:38:07,260 --> 00:38:08,560 connected over udp 972 00:38:09,380 --> 00:38:10,640 so like I said 973 00:38:10,640 --> 00:38:11,540 this device can communicate 974 00:38:11,540 --> 00:38:13,680 to the cloud over tcp and udp 975 00:38:13,680 --> 00:38:16,000 so if you patch the udp function 976 00:38:16,000 --> 00:38:17,120 and something goes wrong 977 00:38:17,120 --> 00:38:19,240 we have still the tcp function 978 00:38:19,240 --> 00:38:21,760 so we can maybe unbreak the device 979 00:38:21,760 --> 00:38:23,080 if we did something completely wrong 980 00:38:24,600 --> 00:38:25,620 what we use here 981 00:38:25,620 --> 00:38:28,220 in this otutimerinfo function 982 00:38:28,220 --> 00:38:29,100 there is at some point 983 00:38:29,100 --> 00:38:30,260 a snprintf 984 00:38:30,260 --> 00:38:32,320 and this is like the address of the snprintf 985 00:38:32,320 --> 00:38:33,800 this is also useful for later 986 00:38:35,920 --> 00:38:37,080 so after that 987 00:38:37,080 --> 00:38:38,500 we have the binary 988 00:38:38,500 --> 00:38:41,780 we know how the memory layout looks like 989 00:38:41,780 --> 00:38:43,360 because the header file 990 00:38:43,360 --> 00:38:44,760 of the firmware told us 991 00:38:44,760 --> 00:38:46,120 more or less where the segments are loaded 992 00:38:46,120 --> 00:38:47,140 at which memory address 993 00:38:47,140 --> 00:38:49,380 we created more free space 994 00:38:50,320 --> 00:38:52,060 for the flash of the patch 995 00:38:54,600 --> 00:38:56,080 and we know now the functions 996 00:38:56,760 --> 00:38:58,220 function names and the signatures 997 00:38:58,700 --> 00:39:00,360 so we need to configure Nexmon 998 00:39:01,520 --> 00:39:02,400 so Nexmon 999 00:39:02,400 --> 00:39:03,740 like I said is consisting of 1000 00:39:04,380 --> 00:39:05,960 a lot of different scripts 1001 00:39:05,960 --> 00:39:07,460 and now I give you a step by step 1002 00:39:07,460 --> 00:39:09,860 what you need to do if you want to add a new device 1003 00:39:10,560 --> 00:39:11,580 the first thing is 1004 00:39:11,580 --> 00:39:13,540 there is a firmware version file 1005 00:39:13,540 --> 00:39:14,820 where you just need to create 1006 00:39:16,560 --> 00:39:18,460 define a number for the device 1007 00:39:18,460 --> 00:39:19,300 it could be any number 1008 00:39:19,300 --> 00:39:21,540 just some number which is internally used 1009 00:39:21,540 --> 00:39:24,500 by the gcc plugin of Nexmon 1010 00:39:25,040 --> 00:39:27,000 so just define like a new device 1011 00:39:27,000 --> 00:39:27,840 and maybe a new firmware 1012 00:39:28,560 --> 00:39:29,620 and the next step 1013 00:39:29,620 --> 00:39:32,640 we need to tell Nexmon 1014 00:39:32,640 --> 00:39:33,860 if you create a device 1015 00:39:33,860 --> 00:39:35,180 where it can find everything 1016 00:39:35,180 --> 00:39:36,620 so for example 1017 00:39:38,240 --> 00:39:40,280 where is our original code 1018 00:39:40,280 --> 00:39:41,840 this information we know 1019 00:39:41,840 --> 00:39:43,040 from a deposit tool 1020 00:39:43,040 --> 00:39:44,500 and also we need to tell 1021 00:39:44,500 --> 00:39:46,700 where is the space for the patch code 1022 00:39:46,700 --> 00:39:48,560 and for both things we need to tell 1023 00:39:48,560 --> 00:39:50,680 how much space is available 1024 00:39:52,260 --> 00:39:54,220 especially the patch size is very important 1025 00:39:54,600 --> 00:39:55,480 we don't want to go over 1026 00:39:56,340 --> 00:39:56,980 over this 1027 00:39:57,680 --> 00:39:59,060 I actually don't know what happens 1028 00:39:59,060 --> 00:40:00,820 if the patch is bigger than this 1029 00:40:00,820 --> 00:40:02,340 but usually it should never happen 1030 00:40:03,760 --> 00:40:05,280 right so the next thing is 1031 00:40:05,280 --> 00:40:06,600 now we want to write up 1032 00:40:06,600 --> 00:40:08,780 we want to define the existing functions 1033 00:40:08,780 --> 00:40:10,100 so like I said 1034 00:40:10,100 --> 00:40:12,440 for me the interesting function was 1035 00:40:13,360 --> 00:40:14,100 httpc get 1036 00:40:14,720 --> 00:40:16,660 so here you see the device 1037 00:40:17,460 --> 00:40:19,240 sorry the function name 1038 00:40:19,240 --> 00:40:20,240 and the signature 1039 00:40:20,240 --> 00:40:23,380 so these are the arguments that it has 1040 00:40:24,600 --> 00:40:25,720 and this is the important thing 1041 00:40:25,720 --> 00:40:27,940 you tell more or less the Nexmon framework 1042 00:40:27,940 --> 00:40:29,620 at which address it can find 1043 00:40:30,900 --> 00:40:31,780 this function 1044 00:40:31,780 --> 00:40:33,800 in the firmware 1045 00:40:34,480 --> 00:40:36,000 and the same stuff we do 1046 00:40:36,000 --> 00:40:37,260 for the bintx function 1047 00:40:37,260 --> 00:40:38,060 we do it for 1048 00:40:40,500 --> 00:40:41,380 for snprintf 1049 00:40:41,380 --> 00:40:44,340 and we do it also for this vmprintf 1050 00:40:44,340 --> 00:40:46,160 which is giving us the console output 1051 00:40:46,160 --> 00:40:48,300 so by this we are telling Nexmon 1052 00:40:48,300 --> 00:40:50,940 exactly where it can find 1053 00:40:50,940 --> 00:40:52,760 the stuff for this one particular device 1054 00:40:52,760 --> 00:40:54,580 and for this one particular framework 1055 00:40:54,600 --> 00:40:54,980 in the firmware version 1056 00:40:56,660 --> 00:40:57,620 okay so 1057 00:40:58,180 --> 00:40:59,360 let's write our patch 1058 00:40:59,360 --> 00:41:00,060 compile and rebuild 1059 00:41:00,700 --> 00:41:03,720 so this is one of my example patches 1060 00:41:03,720 --> 00:41:06,100 so what I do here 1061 00:41:06,100 --> 00:41:07,760 is actually I do http request 1062 00:41:07,760 --> 00:41:08,480 to my server 1063 00:41:10,080 --> 00:41:11,500 so we saw in IDA 1064 00:41:11,500 --> 00:41:13,140 where the interesting memory areas are 1065 00:41:13,140 --> 00:41:14,800 so here are the addresses of the 1066 00:41:14,800 --> 00:41:16,300 MAC address, device ID key 1067 00:41:16,300 --> 00:41:18,320 I created a buffer 1068 00:41:18,320 --> 00:41:20,540 and what I do here is I convert the key 1069 00:41:20,540 --> 00:41:22,640 into some hex format and I run just 1070 00:41:24,600 --> 00:41:25,180 on my server 1071 00:41:25,180 --> 00:41:27,480 so later on if I look at my server log 1072 00:41:27,480 --> 00:41:29,820 I can see the key hopefully in clear text 1073 00:41:30,540 --> 00:41:32,340 the important thing here is 1074 00:41:32,340 --> 00:41:35,100 that we need to tell 1075 00:41:35,100 --> 00:41:36,500 the Nexmon framework 1076 00:41:36,500 --> 00:41:37,600 where it has to 1077 00:41:38,520 --> 00:41:40,100 to patch this thing 1078 00:41:40,100 --> 00:41:41,780 so in this case 1079 00:41:41,780 --> 00:41:43,500 this address is more or less 1080 00:41:43,500 --> 00:41:46,700 the original branch instruction 1081 00:41:46,700 --> 00:41:48,160 the branch link 1082 00:41:48,160 --> 00:41:50,420 and the thing 1083 00:41:50,420 --> 00:41:51,860 because we don't want to break the functionality 1084 00:41:51,860 --> 00:41:53,860 of the device so here you see also 1085 00:41:54,820 --> 00:41:55,380 actually 1086 00:41:55,380 --> 00:41:57,640 in the original printf again 1087 00:41:57,640 --> 00:41:58,700 so basically we 1088 00:41:58,700 --> 00:42:01,800 every time if the SN printf is called 1089 00:42:01,800 --> 00:42:03,960 in this OTU info function 1090 00:42:04,400 --> 00:42:05,740 we jump into this patch 1091 00:42:06,560 --> 00:42:07,920 we extract the key 1092 00:42:07,920 --> 00:42:08,700 and then we jump 1093 00:42:08,700 --> 00:42:11,340 we go back to the original program flow 1094 00:42:11,340 --> 00:42:12,560 so the device is still functional 1095 00:42:12,560 --> 00:42:15,360 so now we have our patch 1096 00:42:15,360 --> 00:42:17,220 so if we do the rebuild thing 1097 00:42:17,220 --> 00:42:20,620 if you compile everything with Nexmon 1098 00:42:20,620 --> 00:42:21,600 we get the segment 1099 00:42:21,600 --> 00:42:22,960 sections again sorry 1100 00:42:24,600 --> 00:42:25,980 we can build the firmware again 1101 00:42:25,980 --> 00:42:28,180 and we can use the model tool 1102 00:42:28,180 --> 00:42:29,860 to build the original firmware 1103 00:42:29,860 --> 00:42:31,340 which is then patched 1104 00:42:32,520 --> 00:42:34,180 okay so now we need to 1105 00:42:34,180 --> 00:42:36,060 apply that and at this point 1106 00:42:36,060 --> 00:42:37,700 I mean I should say like before 1107 00:42:37,700 --> 00:42:39,940 but obviously your warranty is void after that 1108 00:42:40,940 --> 00:42:42,180 every time you open the device 1109 00:42:42,180 --> 00:42:43,660 actually it's a problematic thing 1110 00:42:45,360 --> 00:42:46,660 so how we do that 1111 00:42:46,660 --> 00:42:47,900 the first step 1112 00:42:47,900 --> 00:42:50,500 and this is the step which we can't avoid 1113 00:42:50,500 --> 00:42:51,440 at least for the devices 1114 00:42:52,120 --> 00:42:54,420 like the light bulbs we need to have 1115 00:42:54,600 --> 00:42:55,280 a new firmware update 1116 00:42:55,280 --> 00:42:56,900 it must be available 1117 00:42:56,900 --> 00:42:59,160 so if you're already sitting on the newest firmware update 1118 00:42:59,160 --> 00:43:00,060 it's not possible 1119 00:43:00,820 --> 00:43:01,580 so the 1120 00:43:01,580 --> 00:43:04,580 as soon as we get 1121 00:43:04,580 --> 00:43:06,300 like this OTU update command 1122 00:43:06,300 --> 00:43:07,260 from the cloud 1123 00:43:08,860 --> 00:43:09,980 then it's a good sign 1124 00:43:09,980 --> 00:43:11,820 because that means actually there's a new firmware update 1125 00:43:11,820 --> 00:43:13,600 so we can somehow manipulate that 1126 00:43:13,600 --> 00:43:15,760 and how this is done is actually 1127 00:43:15,760 --> 00:43:17,480 sorry this animation is broken 1128 00:43:18,880 --> 00:43:20,560 this would be the usual way 1129 00:43:20,560 --> 00:43:21,920 so we would get 1130 00:43:24,600 --> 00:43:25,500 to the cloud server 1131 00:43:25,500 --> 00:43:27,100 and we would get the firmware 1132 00:43:27,100 --> 00:43:28,660 but we don't want that 1133 00:43:28,660 --> 00:43:31,540 because we don't want to have the newest version on the device 1134 00:43:31,540 --> 00:43:33,120 instead what we're doing here 1135 00:43:33,120 --> 00:43:36,000 is we're using some kind of DNS switch 1136 00:43:36,480 --> 00:43:37,480 and we're switching this thing 1137 00:43:37,480 --> 00:43:40,280 to our IoT Village CDN 1138 00:43:40,280 --> 00:43:41,520 so our patched firmware 1139 00:43:41,520 --> 00:43:43,000 is downloaded to the device 1140 00:43:43,000 --> 00:43:44,660 and then the device is very happy about that 1141 00:43:44,660 --> 00:43:46,260 I tried to prepare a demo 1142 00:43:46,260 --> 00:43:48,460 but the thing is it's nearly impossible to have Wi-Fi here 1143 00:43:48,460 --> 00:43:52,000 so I didn't want to break the devices 1144 00:43:52,000 --> 00:43:53,420 but if you're interested you can take a look 1145 00:43:54,600 --> 00:43:54,880 on the devices 1146 00:43:54,880 --> 00:43:57,320 and we can try something out 1147 00:43:58,400 --> 00:44:00,540 there's also one interesting thing 1148 00:44:00,540 --> 00:44:02,080 if you get the SDK 1149 00:44:02,080 --> 00:44:04,100 they have also some very interesting 1150 00:44:04,100 --> 00:44:05,780 demo applications there 1151 00:44:05,780 --> 00:44:07,340 for example P2P demo 1152 00:44:07,340 --> 00:44:10,380 where the devices can make some 1153 00:44:10,380 --> 00:44:12,120 P2P network over Wi-Fi 1154 00:44:12,120 --> 00:44:14,900 there's a Wi-Fi frame inject demo 1155 00:44:14,900 --> 00:44:17,040 or like a Wi-Fi sniffer demo 1156 00:44:17,040 --> 00:44:18,660 so you can use this kind of 1157 00:44:18,660 --> 00:44:20,700 demos for your patches 1158 00:44:20,700 --> 00:44:21,880 if you're interested in 1159 00:44:21,880 --> 00:44:23,140 doing specific things 1160 00:44:24,800 --> 00:44:26,260 OK so for the summary 1161 00:44:26,260 --> 00:44:27,840 what we can do 1162 00:44:27,840 --> 00:44:28,920 we can modify the firmware 1163 00:44:28,920 --> 00:44:30,740 we can root the device 1164 00:44:31,260 --> 00:44:32,400 and we can do it remotely 1165 00:44:32,400 --> 00:44:35,100 and this is thanks to the missing integrity checks 1166 00:44:35,100 --> 00:44:36,700 so we don't check for the MD5 1167 00:44:36,700 --> 00:44:38,640 we don't have any signatures in the firmware 1168 00:44:39,080 --> 00:44:40,040 what we can do then 1169 00:44:40,040 --> 00:44:41,560 is we can read all the cloud communication 1170 00:44:41,560 --> 00:44:43,680 because as soon as we have the key 1171 00:44:43,680 --> 00:44:44,400 we can decrypt it 1172 00:44:44,400 --> 00:44:45,800 and we can run it for example 1173 00:44:45,800 --> 00:44:46,920 also with our own cloud 1174 00:44:48,440 --> 00:44:49,460 there's one thing 1175 00:44:49,460 --> 00:44:49,980 I told 1176 00:44:49,980 --> 00:44:50,580 everyone 1177 00:44:52,700 --> 00:44:53,880 at the end of my slides 1178 00:44:53,880 --> 00:44:55,020 and this is like the thing 1179 00:44:55,020 --> 00:44:57,140 never leave your devices unprovisioned 1180 00:44:57,140 --> 00:45:00,060 I know people who buy the light bulbs 1181 00:45:00,060 --> 00:45:01,660 and they put just the light bulbs 1182 00:45:02,180 --> 00:45:03,220 somewhere in their home 1183 00:45:03,220 --> 00:45:04,580 and just use them as normal light bulbs 1184 00:45:04,580 --> 00:45:06,880 it doesn't make sense for a Wi-Fi connected light bulb 1185 00:45:06,880 --> 00:45:08,020 but what happens is 1186 00:45:08,020 --> 00:45:09,140 this device is unprovisioned 1187 00:45:09,140 --> 00:45:11,000 so everyone who is in proximity 1188 00:45:11,000 --> 00:45:14,620 of this light bulb can provision that for you and install some malicious software on it 1189 00:45:16,920 --> 00:45:17,200 so if you have any 1190 00:45:17,200 --> 00:45:19,500 devices which you buy at Amazon 1191 00:45:19,500 --> 00:45:20,720 or eBay and so on 1192 00:45:20,720 --> 00:45:22,440 you have no idea what kind of firmware is installed 1193 00:45:22,440 --> 00:45:23,420 and the thing is 1194 00:45:23,420 --> 00:45:25,320 you can't actually check that 1195 00:45:25,320 --> 00:45:27,400 so they have no possibility like with a laptop 1196 00:45:27,400 --> 00:45:30,380 to figure out is the firmware malicious or not malicious 1197 00:45:30,380 --> 00:45:30,860 and so on 1198 00:45:30,860 --> 00:45:32,960 you can do anything with the firmware there 1199 00:45:34,160 --> 00:45:35,260 so for the conclusion 1200 00:45:35,260 --> 00:45:38,020 the best practices for IOT devices 1201 00:45:38,020 --> 00:45:39,420 or like in general for embedded devices 1202 00:45:39,420 --> 00:45:39,920 are not used 1203 00:45:39,920 --> 00:45:41,600 so we have no MD5 verification 1204 00:45:46,920 --> 00:45:47,880 we don't use HTTPS at all 1205 00:45:47,880 --> 00:45:51,000 the certificate verification is also broken 1206 00:45:51,440 --> 00:45:53,340 the hardware security features are missing 1207 00:45:53,340 --> 00:45:54,960 so some of the chips actually have features 1208 00:45:54,960 --> 00:45:55,720 like secure boot 1209 00:45:55,720 --> 00:45:58,920 or where you can do some verification 1210 00:45:59,440 --> 00:46:00,640 of the firmware in hardware 1211 00:46:00,640 --> 00:46:01,880 they are not using that 1212 00:46:02,360 --> 00:46:03,540 the good thing for us is 1213 00:46:03,540 --> 00:46:04,320 we can modify devices 1214 00:46:04,320 --> 00:46:05,440 but the bad thing is 1215 00:46:05,440 --> 00:46:07,540 everyone else who has some knowledge about that 1216 00:46:07,540 --> 00:46:08,640 can do that too 1217 00:46:09,360 --> 00:46:09,800 alright 1218 00:46:10,320 --> 00:46:11,920 so I want to thank a few people 1219 00:46:16,980 --> 00:46:18,120 also Professor Nabeer 1220 00:46:18,120 --> 00:46:19,740 from the Northeastern University 1221 00:46:19,740 --> 00:46:21,100 the Zemo Lab 1222 00:46:21,100 --> 00:46:22,420 with Professor Matthias Hoveck 1223 00:46:22,420 --> 00:46:24,820 and Andrew Sellers 1224 00:46:24,820 --> 00:46:26,960 and his team from the Boston University CyberLock Clinic 1225 00:46:26,960 --> 00:46:28,720 and there is a particular reason why he is there 1226 00:46:30,120 --> 00:46:31,320 because in US 1227 00:46:31,320 --> 00:46:33,720 to publish specific things is a little bit tricky 1228 00:46:33,720 --> 00:46:34,400 with the law 1229 00:46:34,400 --> 00:46:36,720 so basically they make sure that I'm not getting into trouble 1230 00:46:36,720 --> 00:46:38,260 so special thanks for them 1231 00:46:39,120 --> 00:46:40,960 and that's all for my presentations 1232 00:46:40,960 --> 00:46:42,800 and I'm happy to answer your questions 1233 00:46:42,800 --> 00:46:45,820 or if you need any contact data 1234 00:46:46,920 --> 00:46:48,320 I created especially for the DEF CON 1235 00:46:48,320 --> 00:46:48,900 a Twitter account 1236 00:46:48,900 --> 00:46:50,680 so you can send me Twitter messages 1237 00:46:50,680 --> 00:46:51,680 tweets whatever 1238 00:46:52,700 --> 00:46:54,140 or meet me in Boston 1239 00:46:54,140 --> 00:46:55,220 or invite me over telegram 1240 00:46:55,660 --> 00:46:56,880 ok thank you very much