1 00:00:01,920 --> 00:00:05,740 First of all, allow me to introduce myself because you probably don't know me. 2 00:00:05,740 --> 00:00:08,740 I work as a security engineer for over eight years. 3 00:00:09,120 --> 00:00:14,100 Currently I'm working in Accenture Embedded System Security Team that's based in Prague, Czech Republic. 4 00:00:14,100 --> 00:00:15,820 That's also where I come from. 5 00:00:16,000 --> 00:00:23,920 And I'm a big open source enthusiast so the tools that I'll be showing you here today are already published on GitHub and could be used by anyone. 6 00:00:27,590 --> 00:00:30,050 Let's have a look at what's ahead of us. 7 00:00:30,050 --> 00:00:32,630 We will start with the description of the CAN bus. 8 00:00:32,630 --> 00:00:39,210 We're right next to the car hacking village so 90% people there actually know that better than I do, but I'll still try. 9 00:00:39,610 --> 00:00:49,930 We will use this knowledge to define the problem that I'd like to talk about and then I'll go into the demo section which will be quite long because I have two of the demos. 10 00:00:49,930 --> 00:01:02,130 First demo will be using a development board and access to the source code debugger and we will basically have a white box approach and we will see how this vulnerability works. 11 00:01:02,610 --> 00:01:11,830 Then we will continue in the second demonstration and I'd like to approach this one as a simulated security assessment here on stage of a real life ECU. 12 00:01:11,870 --> 00:01:16,070 So I brought an ECU from a vehicle and I'll be doing security assessment of that. 13 00:01:16,070 --> 00:01:17,690 This demo is quite packed. 14 00:01:17,690 --> 00:01:25,550 We will be using JTAG debugger, we will be analyzing firmware, we will be using oscilloscopes so there's a lot to see. 15 00:01:26,590 --> 00:01:33,890 And then we will just wrap up the talk with just a couple points about who could be affected by it and some general takeaways. 16 00:01:33,890 --> 00:01:37,870 At the end I hope there will be still some time left for questions and answers. 17 00:01:38,490 --> 00:01:44,230 So without further ado, let's jump into the first slide and that's the description of the CAN bus. 18 00:01:44,230 --> 00:01:58,270 So most of you probably know but CAN bus or controller area network bus is a standard that was originally aimed at the vehicles to allow multiple ECUs to communicate with each other reliably in the noisy environments such as the car. 19 00:01:58,470 --> 00:02:03,730 To achieve this there are two wires used for this which are carrying a differential signal. 20 00:02:03,730 --> 00:02:11,430 Differential signal means that the data being transmitted over these two lines is basically identical, it's just the polarity is inverted. 21 00:02:11,830 --> 00:02:15,570 And each of these buses is terminated by 120 ohm resistor. 22 00:02:16,310 --> 00:02:23,150 Typical vehicle probably contains multiple of these buses which are usually connected by something that's called security gateway. 23 00:02:23,650 --> 00:02:29,270 If I'm to use analogy to IP networks that would be like a router that decides which message can pass where. 24 00:02:30,210 --> 00:02:35,510 Thanks to these properties like robustness, it's found its way into other industries as well. 25 00:02:35,510 --> 00:02:39,410 That's why the talk is described as embedded devices. 26 00:02:39,590 --> 00:02:47,030 Because you can find CAN bus in heavy industry equipment, you can find CAN bus in medical devices and on a lot of other places too. 27 00:02:47,270 --> 00:02:52,830 It's also important to mention that there are multiple versions of the CAN bus as it evolved over the time. 28 00:02:52,890 --> 00:02:57,690 It started with an 11 bit identifier CAN bus that carried just 8 bytes of data. 29 00:02:57,890 --> 00:03:07,310 These have been deemed insufficient so an extended CAN ID came which just increased the ID portion to 29 bits. 30 00:03:07,470 --> 00:03:09,870 But it kept the 8 bytes of the data. 31 00:03:10,490 --> 00:03:16,230 Then further CAN bus versions have been introduced such as CAN FD with 64 bytes of data. 32 00:03:16,610 --> 00:03:23,850 But the talk that I'm having today is only applicable to the standard CAN ID frame and extended CAN ID frame. 33 00:03:23,850 --> 00:03:29,410 We'll discuss briefly CAN FD and CAN XL but only at the end of this talk. 34 00:03:30,850 --> 00:03:36,110 This slide shows the message, the frame and its individual fields. 35 00:03:36,390 --> 00:03:40,010 In the green part there is the most important field that's CAN ID. 36 00:03:40,010 --> 00:03:41,510 Obviously you need that. 37 00:03:41,510 --> 00:03:46,330 Then in the yellow you can see field called DLC or data length code. 38 00:03:46,470 --> 00:03:49,490 This tells us how many bytes of data actually follow. 39 00:03:49,490 --> 00:03:51,850 That could be between 0 to 8. 40 00:03:51,850 --> 00:03:56,830 So there is a perfect valid message in CAN bus that has 0 bytes of data. 41 00:03:56,830 --> 00:03:59,530 The meaning could be carried just inside the ID. 42 00:03:59,790 --> 00:04:02,830 But you can have anywhere up to 8 bytes of data. 43 00:04:03,730 --> 00:04:10,350 Then in the blue field you can see the CRC but that's not really relevant for this talk so I'm going to skip the remaining fields. 44 00:04:11,290 --> 00:04:16,170 For the picture at the bottom, that's a capture from an oscilloscope. 45 00:04:16,170 --> 00:04:19,190 It shows you how it works on the physical layer. 46 00:04:19,270 --> 00:04:24,990 You can see the differential signaling where the red line is CAN high, the blue line is CAN low. 47 00:04:25,070 --> 00:04:27,470 As you can see the data is basically identical. 48 00:04:27,470 --> 00:04:29,850 It's just a mirror image between those two. 49 00:04:30,070 --> 00:04:33,710 We'll be playing a bit with the oscilloscope later on in the demo. 50 00:04:33,950 --> 00:04:37,670 But for now this is just for me to show you how it works on the physical layer. 51 00:04:37,670 --> 00:04:40,950 It's not really relevant to what I'm going to talk about. 52 00:04:43,290 --> 00:04:55,890 If we dive a bit into the specification of the CAN bus and we look at the description of the data length code field, we will find that it has to have values between 0 and 8. 53 00:04:55,950 --> 00:05:01,630 To express 8 in binary, you need 4 bits because that's 1 followed by 3 zeros. 54 00:05:01,970 --> 00:05:10,850 And therefore, I got an idea like what would happen if I'm going to just send DLC field with all 4 ones. 55 00:05:11,450 --> 00:05:16,570 First disclaimer, you can never send more than 8 bytes of data in those CAN frames. 56 00:05:16,850 --> 00:05:25,070 Those packets would be just malformed, never really received, so you'll never send more than 8 bytes of data. 57 00:05:25,210 --> 00:05:32,950 However, what I found is that none of the hardware and most of the software actually cares about the DLC field value. 58 00:05:32,950 --> 00:05:39,590 So you can send whatever you want there and it's still valid and it will still be passed all the way to the developer. 59 00:05:39,870 --> 00:05:44,090 And then it's up to the firmware development team how they work with this value. 60 00:05:44,670 --> 00:05:49,210 On the slide, you can see a hypothetical memcpy situation. 61 00:05:49,470 --> 00:05:58,810 And because the developers that are working with CAN bus probably know that they can never receive more than 8 bytes, the destination buffer would be allocated to 8 bytes because why waste memory? 62 00:05:59,090 --> 00:06:04,550 Then the source of the operation as a second parameter would be CAN data, because that's what we want to copy. 63 00:06:04,850 --> 00:06:19,770 But since you're not always receiving 8 bytes, but sometimes you receive 4, 3 or any other value, you can use this convenient DLC field, which the frameworks you're using is giving you, to actually specify how many bytes should be copied. 64 00:06:20,070 --> 00:06:22,430 And that's exactly where the problem comes. 65 00:06:22,430 --> 00:06:25,750 Now I'm going to do the funny thing of readjusting the microphone. 66 00:06:26,230 --> 00:06:30,130 I'm sorry if I drop it, because I want to sit for the demo. 67 00:06:37,820 --> 00:06:39,160 Sorry for that delay. 68 00:06:40,320 --> 00:06:45,140 We'll basically jump into our first demonstration. 69 00:06:45,580 --> 00:06:49,000 But before that, I'd like to introduce you to the hardware that I brought with me. 70 00:06:49,800 --> 00:06:53,080 It's in this yellow suitcase that you can see on the stage. 71 00:06:53,240 --> 00:06:58,720 And as you can see, it contains a lot of PCBs, wires, some displays, LEDs. 72 00:06:59,360 --> 00:07:06,680 And I hope you like it at least as much as people at the airport security did, because I had to explain to like 3 people what it is. 73 00:07:06,680 --> 00:07:08,360 I had to take it apart. 74 00:07:08,960 --> 00:07:13,980 And they basically swiped the hell out of it with those anti-explosive detective papers. 75 00:07:14,380 --> 00:07:17,820 So it's kind of a miracle it still works, but it does. 76 00:07:18,500 --> 00:07:24,740 In this case, I have a red board here, the red one in the corner. 77 00:07:24,740 --> 00:07:27,020 That will be a victim for our first demo. 78 00:07:27,020 --> 00:07:30,540 It's an NXP board where I'll have source code running and everything. 79 00:07:31,100 --> 00:07:35,520 The guy in the corner here, the simple Arduino, that's the attacker. 80 00:07:35,520 --> 00:07:41,120 It has a little button here that you can't really see, but whenever I press it, a malicious message will be sent. 81 00:07:41,120 --> 00:07:43,960 I'm going to ignore the middle Arduino for now. 82 00:07:44,120 --> 00:07:47,520 And this is just a power source so that it actually works. 83 00:07:47,520 --> 00:07:49,120 So that's not really important. 84 00:07:49,540 --> 00:07:58,260 Now, if I'd like to show you actually how the source code looks like for these boards, I'll start with the attacker one, so the Arduino. 85 00:07:58,600 --> 00:08:02,360 In the setup function, I'll just set up CAN bus serial and the button. 86 00:08:02,360 --> 00:08:03,780 That's not really interesting. 87 00:08:04,480 --> 00:08:06,020 Then I set up the message. 88 00:08:06,100 --> 00:08:09,180 I'll start by setting a CAN ID 600A. 89 00:08:09,200 --> 00:08:11,760 You'll find out in the second demo why. 90 00:08:12,340 --> 00:08:16,520 But the most important thing, I'll set the DLC field value to F. 91 00:08:17,580 --> 00:08:20,220 Then I need to set some dummy payload. 92 00:08:20,220 --> 00:08:27,520 In this case, it would be A, B, C, D, E, F, G, H in capital ASCII so that we can actually see it and refer to it while we're debugging. 93 00:08:28,020 --> 00:08:36,080 And inside the loop, it just waits for the button press and whenever that happens, we're going to send the message with this function. 94 00:08:37,220 --> 00:08:42,940 When I switch to the victim, so the red board code, which we will be debugging, there's a main function. 95 00:08:42,940 --> 00:08:44,360 We just have two variables. 96 00:08:44,360 --> 00:08:48,540 One is obviously called overwrite buffer because we're going to overwrite it. 97 00:08:49,320 --> 00:08:57,100 And we start by setting it to value 1, 2, 3, 4, 5, 6, 7, again, so that we have something easy to refer to. 98 00:08:58,040 --> 00:09:01,820 Then there is not really interesting CAN bus setup, which I'll just skip. 99 00:09:02,060 --> 00:09:06,620 And in here, I'll be waiting for a message to arrive. 100 00:09:06,620 --> 00:09:17,920 And when it does, there is the main copy function, which is basically identical to the one from the previous slide, where we're copying into the buffer. 101 00:09:17,920 --> 00:09:23,540 Frame DSR is how MCU Expresso chose to call the data field. 102 00:09:23,880 --> 00:09:25,480 I'm not to blame for that. 103 00:09:25,480 --> 00:09:28,460 And frame DLR is the DLC field value. 104 00:09:28,540 --> 00:09:30,340 That's how it's called in MCU Expresso. 105 00:09:30,340 --> 00:09:33,100 Again, in every framework, it will be called differently. 106 00:09:33,860 --> 00:09:42,100 Since I have the board in the case, I can just hit debug, and this should run it, run the code, and we should stop on our first breakpoint. 107 00:09:42,280 --> 00:09:54,360 If I step over this, I can just investigate that the override buffer is now set to values 1, 2, 3, 4, 5, 6, 7, and ASCII terminated by a null byte, which is exactly as expected. 108 00:09:54,780 --> 00:10:01,180 And if I run the code, nothing really happens, because we're now in this endless loop and we're waiting for CAN messages to arrive. 109 00:10:01,460 --> 00:10:14,420 To do so, I need to press this almost invisible button, and hopefully you see a notification pop up here on my screen that tells me that the breakpoint was hit at this memcpy function. 110 00:10:15,340 --> 00:10:23,000 In the data that I'm using as a source for the memcpy, you can see the A, B, C, D, E, F, G, H, which we've set in the Arduino. 111 00:10:23,560 --> 00:10:28,920 And in the DLR, we can see value 15, which is the DLC field value which we've set. 112 00:10:29,220 --> 00:10:33,000 I'm just going to step over this so that the memcpy finishes what it did. 113 00:10:33,120 --> 00:10:38,980 Now, inside the buffer, I should have this A, B, C, D string. 114 00:10:39,480 --> 00:10:44,620 But more importantly, let's have a look at what is in the overwrite buffer. 115 00:10:44,620 --> 00:10:48,000 I'm just going to scroll over it. 116 00:10:48,240 --> 00:10:56,580 The very first value, I hope it's readable a bit, is value 15, which is suspicious, because 15 is the DLC field we've sent. 117 00:10:56,720 --> 00:11:04,660 But the remaining values I don't really recognize, because there is nullbyte, then 137, 172, then repeated, then another nullbyte. 118 00:11:04,900 --> 00:11:11,860 The final nullbyte hasn't been overwritten, because we're overwriting just with seven bytes, so the final nullbyte is from the original buffer. 119 00:11:12,680 --> 00:11:19,680 But now it would be good to see whether we have some control over the data that we're overwriting with. 120 00:11:19,760 --> 00:11:25,160 To do that, we have to investigate a structure called mscanframe. 121 00:11:25,320 --> 00:11:27,720 Again, this is specific to MCUXpresso framework. 122 00:11:27,720 --> 00:11:30,580 In other frameworks, you'll find something completely different. 123 00:11:31,080 --> 00:11:41,220 And we'll see that the value of DSR, which is the data where we started the copy operation, is actually a member of a union, so that you can access the data as data words or data bytes. 124 00:11:41,400 --> 00:11:46,040 But right after that, there is the DLR value, which is the DLC. 125 00:11:46,140 --> 00:11:51,080 This confirms our suspicion that we are overwriting with this structure. 126 00:11:51,120 --> 00:11:59,860 But the remaining values, such as buffer priority, type, format, and some timestamps, those aren't really in control of the attacker. 127 00:11:59,860 --> 00:12:03,100 So we have very limited control of what we overwrite with. 128 00:12:03,480 --> 00:12:10,120 But nevertheless, we can actually use this for some other impact than just denial of service that's in the title of this talk. 129 00:12:10,120 --> 00:12:19,520 I can imagine situations where you overwrite local and global variables in a way that you change their values from a zero to something else. 130 00:12:19,740 --> 00:12:21,180 Why this is important? 131 00:12:21,440 --> 00:12:27,000 And that's because when you program in C, there's quite a common practice of doing a null check. 132 00:12:27,000 --> 00:12:31,420 A null check is, for example, is authenticated if it is a variable. 133 00:12:31,460 --> 00:12:36,240 Then if it is zero, you're not authenticated, and anything else would mean you are. 134 00:12:36,340 --> 00:12:40,420 And this way you could theoretically bypass some of the controls. 135 00:12:40,800 --> 00:12:55,100 Although in the real life examples, this always resulted just in the denial of service, usually for various reasons, but mainly by overwriting pointers to something that makes absolutely no sense, and then crashing the firmware. 136 00:12:55,560 --> 00:13:04,500 With that, I'd like to transition into the second demonstration where I'll be using an ECU from a Tesla Model 3 vehicle. 137 00:13:04,620 --> 00:13:10,080 It's the security controller that sits between the seats where you tap your NFC card when you want to drive. 138 00:13:10,460 --> 00:13:16,460 It's responsible, for example, for unlocking the door and the decision whether you can drive or not. 139 00:13:16,460 --> 00:13:19,240 This is done together with the body control module. 140 00:13:19,760 --> 00:13:26,980 And if we do a typical assessment, we first unpack the ECU and see what the PCB has to offer. 141 00:13:27,240 --> 00:13:41,180 In this case, we found that on the bottom of the PCB, there is an SPC560B64 microcontroller from STMicro, and literally right next to it are pads which are labeled STJTAG. 142 00:13:41,420 --> 00:13:48,780 The jackpot in this case was that this was actually working JTAG interface, so we were able to dump the firmware image and proceed with the assessment. 143 00:13:48,780 --> 00:13:51,280 And this is exactly what I'd like to do here. 144 00:13:51,280 --> 00:13:59,840 And we will start by dumping the image, then we will analyze the firmware, find the vulnerability, exploit it, and then play with the oscilloscope and have some fun. 145 00:14:00,020 --> 00:14:04,580 So let me just show you the unobfuscated view on the suitcase. 146 00:14:05,140 --> 00:14:09,200 You can see the ECU, obviously the big green thing with the Tesla logo. 147 00:14:09,440 --> 00:14:14,960 The Arduino in the middle serves only one purpose, and that is to simulate the rest of the car. 148 00:14:14,960 --> 00:14:19,300 So in fact, this suitcase is the smallest Tesla car that you have probably seen. 149 00:14:20,200 --> 00:14:22,480 I'll have this again, a little black button. 150 00:14:22,480 --> 00:14:27,160 Whenever I press it, I simulate the rest of the car so that the ECU works as if it is in. 151 00:14:27,160 --> 00:14:31,520 If I don't do that, I can't really work with this ECU and show you it live. 152 00:14:32,780 --> 00:14:36,320 I talked about the debugger, so I have just the JLink. 153 00:14:36,880 --> 00:14:38,060 I hope it's readable. 154 00:14:39,120 --> 00:14:41,360 In this suitcase, there are actually multiple layers. 155 00:14:41,360 --> 00:14:48,340 One of the layers contains a JLink debugger, and I can connect with it and just type connect. 156 00:14:48,600 --> 00:14:53,380 I have pre-selected the correct MCU, so SPC560B64. 157 00:14:53,380 --> 00:14:57,700 So I'll just leave everything default, because I like defaults. 158 00:14:58,260 --> 00:15:01,500 And the CPU is now halted and I'm connected to it. 159 00:15:01,500 --> 00:15:03,700 This is a good time to dump the firmware image. 160 00:15:03,700 --> 00:15:06,620 I can use savebin command for that. 161 00:15:06,660 --> 00:15:11,480 And as a destination, I'll choose file tmpfw.bin, for example. 162 00:15:12,100 --> 00:15:15,040 And now I have to specify the memory range, which I want to dump. 163 00:15:15,040 --> 00:15:19,240 So it will be from address 0 to address 17FFFF. 164 00:15:20,060 --> 00:15:28,920 This memory range, I've read it from the data sheet, but I consider it kind of boring to open a data sheet here, so I'm just going to cheat and skip on that. 165 00:15:29,380 --> 00:15:31,820 But this will save the binary from that section. 166 00:15:32,000 --> 00:15:33,660 You can find it in the memory map. 167 00:15:33,660 --> 00:15:36,560 There's actually no science around this. 168 00:15:37,320 --> 00:15:43,420 Now we have a firmware dumped, and I'm going to open IDA and analyze the binary. 169 00:15:43,580 --> 00:15:47,680 I made all fonts ridiculously large, so I hope you can read it. 170 00:15:47,680 --> 00:15:53,960 For me, it's quite painful to work like this, but it's for the audience's sake. 171 00:15:55,240 --> 00:16:01,420 I'm going to choose PowerPC, because as I said, I was reading the data sheet, so I know that it's a PowerPC. 172 00:16:02,000 --> 00:16:06,380 And from now on, I'll use my beloved defaults everywhere. 173 00:16:08,520 --> 00:16:11,200 And this should load me a binary in an IDA. 174 00:16:11,200 --> 00:16:18,080 But as you can see, nothing really happened, because we're working with a binary blob, and IDA doesn't really know, is it the code? 175 00:16:18,080 --> 00:16:18,720 Is it data? 176 00:16:18,720 --> 00:16:19,600 Is it a mix of both? 177 00:16:19,600 --> 00:16:20,580 There are no entry points. 178 00:16:20,580 --> 00:16:21,500 What should I do? 179 00:16:22,260 --> 00:16:33,400 Since this is a situation that we faced quite often, working with extracted firmware image from a microcontroller, we made a little plug-in for IDA Pro, which is called Firm Loader. 180 00:16:33,540 --> 00:16:39,900 And here I just have to select the brand, the line of the MCU, and the correct MCU. 181 00:16:40,760 --> 00:16:42,880 And this will start the auto-analysis. 182 00:16:42,880 --> 00:16:51,400 It will also create memory sections such as RAM, peripherals, and will mark the code flash as executable and trigger the auto-analysis. 183 00:16:51,860 --> 00:16:56,960 If I'm to do a real assessment, this is a starting point, not a complete analysis. 184 00:16:56,960 --> 00:17:04,220 I have to do a bit more manual work to make it ideal, but we don't have time and we have to cheat a bit. 185 00:17:04,340 --> 00:17:11,720 So I'm going to be fine with the analysis that it did, mainly because it already found 1700 or so functions. 186 00:17:11,840 --> 00:17:16,000 So we have the binary loaded and we can start working with it. 187 00:17:16,120 --> 00:17:21,580 Now I'd like to show you two different ways on how to approach firmware analysis of an MCU. 188 00:17:22,000 --> 00:17:25,620 The first approach is identifying standard functions. 189 00:17:25,680 --> 00:17:34,540 Standard functions are things like memcpy, malloc, free, all of those libc functions that developers use everywhere. 190 00:17:34,680 --> 00:17:39,500 It's good to find them, because then it's a bit easier to uncover the custom logic in the firmware. 191 00:17:40,000 --> 00:17:42,940 You can use AIDA Lumina if you have AIDA for that. 192 00:17:42,940 --> 00:17:51,140 It's a great tool, but I'm going to do it manually, not because I just want to scare you with the assembly, but I just actually want to show you that it's not that hard. 193 00:17:51,760 --> 00:17:57,360 Let me just adjust the view of AIDA a little bit so that we have a more comfortable view. 194 00:17:58,340 --> 00:18:00,820 And I'm also going to need to set the compiler. 195 00:18:00,820 --> 00:18:02,180 Sorry for that delay. 196 00:18:03,280 --> 00:18:05,780 And this function is actually memcpy. 197 00:18:05,780 --> 00:18:10,120 I'm just going to walk you through very quickly on why it is a memcpy. 198 00:18:11,040 --> 00:18:18,820 Again, I had to read about PowerPC to figure out that the first three arguments to a function are passed in registers R3, R4, and R5. 199 00:18:18,820 --> 00:18:29,440 Therefore, if I look at the memcpy parameters, I know that the first one is the destination, the second one is source, and the third one is how many bytes should be copied. 200 00:18:29,760 --> 00:18:36,280 So the third parameter, R5, is here first incremented, and then it's set into something called counter register. 201 00:18:36,280 --> 00:18:38,920 Again, I had to read about PowerPC, unfortunately. 202 00:18:39,520 --> 00:18:45,120 Then this counter register is decremented every time we execute this instruction. 203 00:18:45,380 --> 00:18:49,020 And if it reaches zero, we go here to end the function. 204 00:18:49,020 --> 00:18:54,560 But as long as it's not zero, we will be executing this code block in a loop. 205 00:18:55,220 --> 00:19:03,800 So we already know that whatever this function does, it will do it for how many times we tell it in a third argument, which is already a good definition of a memcpy. 206 00:19:04,320 --> 00:19:14,640 But inside the body of the loop, we can see that it takes the second argument, so register R4, indexed by R0, and stores that value into R7. 207 00:19:15,160 --> 00:19:23,840 Then instruction after that takes that value from R7 and stores it in the first argument, which is R3, again indexed by R0. 208 00:19:23,940 --> 00:19:28,780 Then the index is just incremented and this repeats for however many times we specified. 209 00:19:28,960 --> 00:19:31,320 This is how you find a standard function. 210 00:19:31,320 --> 00:19:33,660 Sometimes it's a bit more complicated. 211 00:19:34,060 --> 00:19:36,780 I admit this was one of the very easiest examples. 212 00:19:37,340 --> 00:19:43,580 I'm also going to set the type of the function because I would like to show you some automation later and for that this is necessary. 213 00:19:44,180 --> 00:19:57,300 Now, if I'm doing a security analysis of a firmware, I can hit X on the keyboard and I should now theoretically go through all of these memcpy cross-references and see if they are vulnerable or not. 214 00:19:57,360 --> 00:20:00,240 I'm lazy and I don't have time, so I'm not going to do that. 215 00:20:00,980 --> 00:20:05,920 Second approach that I'd like to show you is a combination of static and dynamic analysis. 216 00:20:06,780 --> 00:20:11,040 During the dynamic analysis I know what the ECU sends on a CAN bus. 217 00:20:11,040 --> 00:20:13,200 I know what it receives on the CAN bus. 218 00:20:13,340 --> 00:20:21,660 So it's actually very simple to just use search for immediate values and find the CAN ID I'm interested in. 219 00:20:21,660 --> 00:20:26,860 This isn't exactly working every time, but in this case it does. 220 00:20:27,140 --> 00:20:39,320 So the second hit is especially interesting because it's in compare instruction and it's comparing register R3, so first argument to a function, to the value that I know I saw on the CAN bus. 221 00:20:39,320 --> 00:20:43,240 And if I look around I actually see a lot of the values that I've seen on the CAN bus. 222 00:20:43,240 --> 00:20:45,640 This one, this one, this one. 223 00:20:45,640 --> 00:20:55,540 So this is good enough assumption for me to just rename this to CAN handler because it appears to be handling incoming CAN messages. 224 00:20:56,040 --> 00:21:00,780 And now I'll end the torture and switch to the decompiler because we need to speed up a bit. 225 00:21:01,180 --> 00:21:05,700 And we said that the first one is CAN DLC, so I'm just, uh, CAN ID, sorry. 226 00:21:05,800 --> 00:21:07,760 I'm going to rename it to that. 227 00:21:08,420 --> 00:21:16,540 With a bit of analysis that I'm not going to show you here, uh, we would figure out this is CAN data and we would actually figure out this is CAN DLC. 228 00:21:18,120 --> 00:21:26,540 And now we basically went from a black box assessment where we took an ECU from a vehicle to a crappy source code review. 229 00:21:27,480 --> 00:21:33,220 Uh, and again, as you can see there are many CAN bus IDs being handled by this function. 230 00:21:33,500 --> 00:21:40,680 And in theory I should go through all of them and identify whether any of these are vulnerable to some issues. 231 00:21:40,880 --> 00:21:43,800 Again, I'm lazy, I don't have time so I'm not going to do that. 232 00:21:44,020 --> 00:21:50,660 Uh, instead I'm going to skip over and if you remember the attacking board sends CAN ID 600A. 233 00:21:50,660 --> 00:21:54,400 So I'm going to just look at the handler for CAN ID 600A. 234 00:21:54,400 --> 00:21:58,040 I'm going to rename it to that because I don't want to lose track. 235 00:21:58,400 --> 00:22:02,840 And this function gets CAN data and CAN DLC as parameters. 236 00:22:02,860 --> 00:22:04,460 So let's just open it. 237 00:22:05,200 --> 00:22:07,160 And do the renaming again. 238 00:22:10,220 --> 00:22:15,880 And now this mem copy here screams at us I'm vulnerable basically. 239 00:22:16,360 --> 00:22:20,140 Because as you can see this length specifier here is CAN DLC. 240 00:22:20,360 --> 00:22:22,080 The source is CAN data. 241 00:22:22,220 --> 00:22:25,240 And as a destination there is this weird memory address. 242 00:22:25,240 --> 00:22:29,260 Uh, weird memory addresses in MCU firmware are usually global variables. 243 00:22:29,260 --> 00:22:34,340 They're just static places in memory which are then used to access the variable values. 244 00:22:34,400 --> 00:22:42,540 If I look at it, I can find that there is between 1AF0 and 1AF7 I have preallocated 8 bytes for this variable. 245 00:22:42,840 --> 00:22:46,280 And right after it there is variable called 1AF8. 246 00:22:46,740 --> 00:22:50,460 And if I look at the cross references I can see that it is being used somewhere. 247 00:22:50,460 --> 00:22:58,060 So if I overflow this variable, I may adjust something in the firmware and cause some undesired behavior. 248 00:22:58,160 --> 00:23:04,760 I'm just going to synchronize the view because that's going to be useful if my right click would have worked. 249 00:23:04,980 --> 00:23:06,620 Sorry for that. 250 00:23:09,750 --> 00:23:17,550 And now when we saw the CAN handler, there were many, many, many functions that were handling CAN messages. 251 00:23:17,550 --> 00:23:20,010 I said I'm lazy so I'm not going to do that. 252 00:23:20,090 --> 00:23:29,550 But because I'm lazy all the time, but I don't have time just right now, we created a plugin that automates a bit a search for cross references. 253 00:23:30,090 --> 00:23:32,470 This plugin is called Wolfie. 254 00:23:32,730 --> 00:23:37,570 And to work with it, I wonder why my touch pad doesn't work. 255 00:23:37,570 --> 00:23:38,630 Thanks. 256 00:23:40,050 --> 00:23:42,890 You just have to add certain functions. 257 00:23:42,890 --> 00:23:46,890 I'm sorry I don't know how to enlarge this text so I'll just describe it. 258 00:23:46,990 --> 00:23:52,430 We first select some name for the rule that actually doesn't matter at all. 259 00:23:52,670 --> 00:24:01,310 And then we have to use a filter to somehow filter out all the memcpy functions we're not interested in or to just filter those that we are interested in. 260 00:24:01,310 --> 00:24:04,230 To find this vulnerability, there is one precondition. 261 00:24:04,230 --> 00:24:07,630 The memcpy function must not have static third parameter. 262 00:24:07,630 --> 00:24:13,570 If there is a static value like 2, 3, 8 in a third parameter, this vulnerability will not work. 263 00:24:13,570 --> 00:24:21,970 So I'll start by saying that the not param 2 dot is constant. 264 00:24:22,930 --> 00:24:26,090 I'm sorry for the syntax, it's just Python code that gets evaluated. 265 00:24:26,630 --> 00:24:34,310 And if this condition is fulfilled, I can also say that I'm actually only interested in the functions that are called from the can handler. 266 00:24:34,370 --> 00:24:46,810 So I can just say function, if I'm be able to type, function call dot reachable from can handler. 267 00:24:48,110 --> 00:24:59,230 And this query will now take a look at all 100 plus cross references to a memcpy function and it will only filter out those that match the criteria I've specified. 268 00:24:59,690 --> 00:25:06,710 As you can see, the very first one is the one we found manually in can handler 68, but there are five others. 269 00:25:06,710 --> 00:25:13,330 I'd like to specifically look at the one at 6F1BA, which is right here. 270 00:25:13,330 --> 00:25:22,470 The main reason I want to look at it is if I look at the cross references of this function, we can see it's being called at the can handler 68 here. 271 00:25:23,170 --> 00:25:24,410 And it's right here. 272 00:25:24,410 --> 00:25:28,350 As you can see again, it gets canData, canDLC as parameters. 273 00:25:29,030 --> 00:25:31,090 So rename again. 274 00:25:37,050 --> 00:25:41,830 And now we can see that right in the middle of the function, there is a memcpy function. 275 00:25:42,450 --> 00:25:46,070 It uses as a source canData as a count of bytes. 276 00:25:46,070 --> 00:25:53,290 This time it's V4, but V4 is here, it's just canDLC, so it's just canDLC through a proxy, let's say. 277 00:25:53,490 --> 00:25:57,990 The destination in this time is variable V7, which is a local variable. 278 00:25:57,990 --> 00:25:59,790 It is an 8 byte array. 279 00:26:00,050 --> 00:26:02,710 So we can share again, we will be overflowing. 280 00:26:03,410 --> 00:26:07,150 But we will be overflowing into something called variable V8. 281 00:26:07,250 --> 00:26:14,390 And if you look at what it is, it actually is set to some constant value here from a global variable at the beginning of the function. 282 00:26:14,390 --> 00:26:17,650 And then it's compared to it at the end of the function. 283 00:26:17,830 --> 00:26:21,290 This is a thing called stack canaries. 284 00:26:21,290 --> 00:26:27,370 And it is used to protect the values on the stack against buffer overflows such as this one. 285 00:26:27,490 --> 00:26:34,890 So in this case, we know that when we send this scan message, we would actually cause this stack canary to get triggered. 286 00:26:35,230 --> 00:26:37,370 But we should have a look at what's going to happen. 287 00:26:37,370 --> 00:26:41,250 And that's in this function, we will actually play a bit with peripherals. 288 00:26:41,250 --> 00:26:44,670 But more importantly, there is this endless while loop. 289 00:26:44,670 --> 00:26:49,990 Endless while loops are the best indicators for fault handlers in microcontrollers. 290 00:26:50,030 --> 00:26:57,310 Because while in endless while loop, the device cannot cause any harm by just doing weird things with its peripherals. 291 00:26:57,310 --> 00:27:00,510 It's just executing the same instruction over and over again. 292 00:27:01,650 --> 00:27:05,070 Therefore, we will cause a crash of this microcontroller. 293 00:27:05,070 --> 00:27:06,790 That's why denial of service. 294 00:27:07,170 --> 00:27:12,010 But unfortunately, it would be through an anti-climatic overwrite of a stack canary. 295 00:27:12,770 --> 00:27:22,010 We have access to the debugger, so it would be quite nice to actually see this in action and see if we actually overwrite with the same values as we have seen in the previous demo. 296 00:27:22,310 --> 00:27:27,630 So to do that, I decided to torture myself further and show you the access in the debugger. 297 00:27:28,850 --> 00:27:32,270 Where I can set breakpoints. 298 00:27:32,770 --> 00:27:36,310 First one would be at before mem copy function. 299 00:27:36,670 --> 00:27:39,450 The second one will be right after it. 300 00:27:42,780 --> 00:27:44,740 And I'll check if the CPU is halted. 301 00:27:44,740 --> 00:27:45,400 It is. 302 00:27:45,400 --> 00:27:46,500 I'll let it go. 303 00:27:47,320 --> 00:27:52,720 Now, the ECU wouldn't actually react to any kind of messages because it's sleeping state. 304 00:27:52,720 --> 00:27:54,640 So I have to press this button. 305 00:27:54,860 --> 00:27:58,860 This will turn on one of the LEDs that were so loved at the airport. 306 00:27:59,380 --> 00:28:06,440 And once it turns off, I know that the car is properly simulated and I can start working with the ECU. 307 00:28:07,080 --> 00:28:09,720 The CPU still shouldn't be halted. 308 00:28:10,360 --> 00:28:14,360 But this would change after I send the attacker message. 309 00:28:16,860 --> 00:28:17,980 And as you can see, it did. 310 00:28:17,980 --> 00:28:20,900 Now we're right before the mem copy function. 311 00:28:21,100 --> 00:28:23,240 And we can investigate the registers. 312 00:28:23,240 --> 00:28:31,460 And from the beginning of the demo, we can actually know that the register R3 is the one that holds the destination location. 313 00:28:31,600 --> 00:28:36,800 So I can use mem 32 function command to dump that part of the memory. 314 00:28:37,100 --> 00:28:42,360 I'm also terrible sorry to anyone who knows how to work with Jlink and these kind of commands. 315 00:28:42,360 --> 00:28:44,020 I'm not really good with that. 316 00:28:44,960 --> 00:28:50,080 But here, these eight bytes are the legitimate V7 buffer. 317 00:28:50,080 --> 00:28:53,340 This is what should be overwritten with the candata. 318 00:28:53,520 --> 00:28:57,580 This is stack canary, which is interestingly set to all zeros. 319 00:28:58,300 --> 00:29:02,240 And this is the value that should be protected by the stack canary. 320 00:29:03,540 --> 00:29:07,520 And now comes the part where I'm not really good with Jlink. 321 00:29:07,520 --> 00:29:10,300 Because this is how I step over break points. 322 00:29:11,540 --> 00:29:14,200 As I said, I'm sorry to anyone who knows how to do that. 323 00:29:14,620 --> 00:29:23,900 And if I repeat the command now, we can see that these 41, 42, this is basically the A, B, C, D, E, F, G, H string. 324 00:29:24,680 --> 00:29:28,900 This is the stack canary, which has been overwritten with the can ID. 325 00:29:29,380 --> 00:29:35,420 So we can cheer up a bit because we are controlling quite a lot of bytes that we're overflowing. 326 00:29:35,760 --> 00:29:40,960 But we can't really because if we change the can ID, this overflow would never happen. 327 00:29:41,440 --> 00:29:45,420 So again, we can't really change what we overflow with in this case. 328 00:29:45,420 --> 00:29:48,860 Then this is probably can DLC based on the value here. 329 00:29:48,860 --> 00:29:53,040 Then there are some null bytes for some flags inside the can structure. 330 00:29:53,140 --> 00:29:55,660 And this D8 is from the original pointer. 331 00:29:55,660 --> 00:29:57,480 We haven't really overwritten that. 332 00:29:58,880 --> 00:30:01,780 Now having access to the debugger is nice. 333 00:30:03,220 --> 00:30:04,340 And I'm not really good with that. 334 00:30:04,340 --> 00:30:06,220 But sometimes you don't have that. 335 00:30:07,200 --> 00:30:09,560 And let me just check that the CPU is not halted. 336 00:30:09,560 --> 00:30:10,580 It is not. 337 00:30:10,940 --> 00:30:16,700 And in this suitcase, there is a third layer underneath the J link layer and that contains an oscilloscope. 338 00:30:16,700 --> 00:30:20,140 One of the oscilloscope probes is connected here to the CAN bus. 339 00:30:20,180 --> 00:30:25,980 The other oscilloscope probe is hidden under this to sense the NFC field. 340 00:30:26,280 --> 00:30:33,940 So if I now press this simulation button, I can go into the oscilloscope view and start tracking. 341 00:30:33,940 --> 00:30:35,780 The red line is the NFC field. 342 00:30:35,780 --> 00:30:37,740 The blue line is the CAN bus. 343 00:30:37,960 --> 00:30:41,200 So when the ECU comes up, you can see NFC field is up. 344 00:30:41,200 --> 00:30:48,040 And if I press the attacker button, you'll first see that... let me stop that first. 345 00:30:48,040 --> 00:30:51,380 You'll see that the CAN messages have stopped being sent. 346 00:30:51,380 --> 00:30:53,840 That's the reset of the ECU. 347 00:30:54,240 --> 00:30:58,200 And about a half a second after that, the NFC field got killed as well. 348 00:30:58,200 --> 00:31:03,360 This is how we initially found the vulnerability through fuzzing and using this side channel detection. 349 00:31:03,720 --> 00:31:11,960 The delay between the crash of the MCU and the NFC field disappearing is caused by the fact that the NFC controller is actually a separate chip. 350 00:31:12,520 --> 00:31:16,520 And there is a communication line between the main MCU and the NFC chip. 351 00:31:16,620 --> 00:31:20,320 And that's why there is a bit of a half second delay before that shuts down. 352 00:31:20,900 --> 00:31:26,700 I'll try to zoom into this, but I have never found a way how to increase text here. 353 00:31:26,700 --> 00:31:29,160 So you'll probably not be able to read that. 354 00:31:29,640 --> 00:31:35,640 But the message before this drop happens, the last message is the one we've sent. 355 00:31:35,640 --> 00:31:37,200 So ID 600A. 356 00:31:37,500 --> 00:31:42,860 And the voltage drop is actually caused by the GPIO pins being reset into their default state. 357 00:31:43,220 --> 00:31:48,780 After that, if I zoom out a bit, there is a break in CAN messages being sent. 358 00:31:48,780 --> 00:31:50,880 This is because the ECU is restarting. 359 00:31:51,020 --> 00:31:55,420 And then it gets into the initial state where it again waits for it to be restarted. 360 00:31:55,700 --> 00:31:58,080 This is just one of the side channel ways. 361 00:31:58,360 --> 00:32:06,080 And when I was here, I've been actually given this invitation to a high roller cone. 362 00:32:06,740 --> 00:32:10,040 And quite interestingly, it actually works as an NFC field detector. 363 00:32:10,600 --> 00:32:20,140 So if you don't want to use oscilloscopes, for whatever reason, you can actually use this invitation that you can get here at DEF CON. 364 00:32:20,680 --> 00:32:29,060 If placed properly, this will light up the LED here whenever it sees the NFC field. 365 00:32:29,580 --> 00:32:34,700 And then by just pressing this button, you'll see that the NFC field will stop. 366 00:32:34,980 --> 00:32:38,360 I'm not going to the high roller cone, by the way. 367 00:32:38,360 --> 00:32:39,740 I have other plans. 368 00:32:40,280 --> 00:32:41,660 But anyway, thanks. 369 00:32:41,660 --> 00:32:42,680 I like this. 370 00:32:43,780 --> 00:32:48,480 The other side channel possibilities are detecting power consumption. 371 00:32:48,480 --> 00:32:52,520 That, however, requires soldering, which we have done here. 372 00:32:53,320 --> 00:32:56,940 But you can also do it on the CAN bus. 373 00:32:57,300 --> 00:33:00,100 There are usually messages that appear periodically. 374 00:33:00,100 --> 00:33:03,920 So you can detect the time difference in between the periodic messages. 375 00:33:04,420 --> 00:33:10,300 And the easiest way is when you find an ECU that actually sends a specific message on a reset. 376 00:33:10,300 --> 00:33:12,940 That's the easiest way how to detect a reset. 377 00:33:13,380 --> 00:33:16,340 There is a fuzzer published for that as well. 378 00:33:16,340 --> 00:33:19,700 It's actually not published yet because I haven't clicked the button to publish it. 379 00:33:19,700 --> 00:33:21,520 But it will be right after this call. 380 00:33:22,560 --> 00:33:25,600 Now I'd like to discuss who's affected by this issue. 381 00:33:25,620 --> 00:33:31,660 It could be basically anyone who just blindly accepts the value of this DLC field in some memory copy operations. 382 00:33:32,120 --> 00:33:45,720 We also looked at the popular SDKs and RTOSs and found that Armabed OS, STM32Cube SDK and NXP MCU Express OSDK, they don't really care and they will send you any value in the DLC field that you send to it. 383 00:33:46,020 --> 00:33:48,180 We reported this to all of them. 384 00:33:48,180 --> 00:33:51,020 So as of now, this should have been fixed. 385 00:33:51,200 --> 00:33:55,140 But there are other frameworks, some of them commercial, we haven't really looked at. 386 00:33:55,140 --> 00:33:56,980 So those might be still vulnerable. 387 00:33:57,660 --> 00:34:02,960 We looked also at the default Linux kernel drivers, which are not vulnerable. 388 00:34:02,960 --> 00:34:08,620 So those will never ever pass you anything higher than 8 in the DLC field if you're working with it. 389 00:34:09,520 --> 00:34:12,220 And now is a good time to look at other CAN bus versions. 390 00:34:12,220 --> 00:34:19,500 So CAN FD is not affected by the nature of it because the data payload was increased to 64 bytes. 391 00:34:19,620 --> 00:34:28,060 But the DLC field stayed at 4 bits and there is this weird translation table where you translate into those 4 bits how many bytes follow. 392 00:34:28,660 --> 00:34:31,400 CAN XL is likely not affected. 393 00:34:32,080 --> 00:34:37,800 I would like to wait with any judgments here because I haven't seen any real implementation of it anywhere. 394 00:34:37,940 --> 00:34:41,360 But based on the specification, it should not be. 395 00:34:42,280 --> 00:34:45,860 Finally, I would like to have some general takeaways from this. 396 00:34:45,860 --> 00:34:55,920 So first of all, if you're a developer, regardless of the protocol you work with, you should consider the metadata of it as entrusted, not just the data it arrives over it. 397 00:34:56,740 --> 00:35:03,920 It is also good to know how the libraries you're actually working with are handling values such as length of incoming data for that matter. 398 00:35:04,360 --> 00:35:16,320 And if you're on the assessment side such as me, I would like to suggest that it's always worth looking one layer below the obvious application data into the protocol world because there may be some interesting vulnerabilities in there as well. 399 00:35:16,800 --> 00:35:21,080 With that, I'd like to thank you for your attention and ask if you have any questions. 400 00:35:21,080 --> 00:35:24,020 I'll try to stand and adjust this microphone again. 401 00:35:33,390 --> 00:35:38,470 The high roller invitation, although I'm not sure if it's invitation, I can give it to someone if they want. 402 00:35:39,450 --> 00:35:42,790 I have enough things that the tech NFC feels at home. 403 00:35:49,260 --> 00:35:52,340 There's a microphone over there so that everyone can hear. 404 00:35:55,020 --> 00:36:00,820 Have you tried to increase the F value to read more memory and see what happens? 405 00:36:01,120 --> 00:36:02,980 I'm sorry, I haven't heard. 406 00:36:03,440 --> 00:36:04,540 Could you come just here? 407 00:36:04,540 --> 00:36:06,500 This microphone seems to work a bit better. 408 00:36:07,240 --> 00:36:08,840 You have a stage time. 409 00:36:13,120 --> 00:36:17,220 So the question was whether I tried to increase the F value in the DLC. 410 00:36:17,460 --> 00:36:20,160 I can't because there are just four bits. 411 00:36:20,260 --> 00:36:23,920 And if I set all four bits to one, it is up to F. 412 00:36:24,260 --> 00:36:27,200 I'd love to have more than F, but I can't. 413 00:36:27,200 --> 00:36:32,720 Actually, in CanXL, the DLC field is 11, which can fit up to 2047. 414 00:36:33,300 --> 00:36:38,700 But then the specification says you can have up to 2048 bytes of data. 415 00:36:39,500 --> 00:36:42,100 And I don't know how is that going to work. 416 00:36:42,260 --> 00:36:45,520 But I'll wait for the implementation and then I'll hack crap out of it. 417 00:36:53,140 --> 00:36:54,380 I guess that's all. 418 00:36:54,380 --> 00:36:56,740 So thank you and have a nice day.