[00:01.920 --> 00:06.700] First of all, allow me to introduce myself because you probably don't know me. I work as a [00:06.700 --> 00:12.500] security engineer for over eight years. Currently I'm working in Accenture Embedded System Security [00:12.500 --> 00:17.100] Team that's based in Prague, Czech Republic. That's also where I come from. And I'm a big [00:17.100 --> 00:22.000] open source enthusiast so the tools that I'll be showing you here today are already published on [00:22.000 --> 00:31.210] GitHub and could be used by anyone. Let's have a look at what's ahead of us. We will start with [00:31.210 --> 00:35.870] the description of the CAN bus. We're right next to the car hacking village so 90% people there [00:35.870 --> 00:41.670] actually know that better than I do, but I'll still try. We will use this knowledge to define [00:41.670 --> 00:47.590] the problem that I'd like to talk about and then I'll go into the demo section which will be quite [00:47.590 --> 00:53.410] long because I have two of the demos. First demo will be using a development board and access to [00:53.410 --> 00:59.010] the source code debugger and we will basically have a white box approach and we will see [00:59.730 --> 01:06.050] how this vulnerability works. Then we will continue in the second demonstration and I'd like [01:06.050 --> 01:11.830] to approach this one as a simulated security assessment here on stage of a real life ECU. [01:11.870 --> 01:16.790] So I brought an ECU from a vehicle and I'll be doing security assessment of that. This demo is [01:16.790 --> 01:22.850] quite packed. We will be using JTAG debugger, we will be analyzing firmware, we will be using [01:22.850 --> 01:29.850] oscilloscopes so there's a lot to see. And then we will just wrap up the talk with just a couple [01:29.850 --> 01:35.290] points about who could be affected by it and some general takeaways. At the end I hope there will [01:35.290 --> 01:41.750] be still some time left for questions and answers. So without further ado, let's jump into the first [01:41.750 --> 01:47.330] slide and that's the description of the CAN bus. So most of you probably know but CAN bus or [01:47.330 --> 01:52.470] controller area network bus is a standard that was originally aimed at the vehicles to allow [01:52.470 --> 01:57.390] multiple ECUs to communicate with each other reliably in the noisy environments such as the [01:57.390 --> 02:03.730] car. To achieve this there are two wires used for this which are carrying a differential signal. [02:03.730 --> 02:08.290] Differential signal means that the data being transmitted over these two lines is basically [02:08.290 --> 02:14.550] identical, it's just the polarity is inverted. And each of these buses is terminated by 120 ohm [02:14.550 --> 02:20.950] resistor. Typical vehicle probably contains multiple of these buses which are usually connected by [02:20.950 --> 02:26.110] something that's called security gateway. If I'm to use analogy to IP networks that would be [02:26.110 --> 02:31.850] like a router that decides which message can pass where. Thanks to these properties like [02:31.850 --> 02:36.530] robustness, it's found its way into other industries as well. That's why the talk is [02:37.110 --> 02:42.810] described as embedded devices. Because you can find CAN bus in heavy industry equipment, you can [02:42.810 --> 02:48.650] find CAN bus in medical devices and on a lot of other places too. It's also important to mention [02:48.650 --> 02:53.570] that there are multiple versions of the CAN bus as it evolved over the time. It started with an [02:53.570 --> 02:59.750] 11 bit identifier CAN bus that carried just 8 bytes of data. These have been deemed insufficient [02:59.750 --> 03:08.630] so an extended CAN ID came which just increased the ID portion to 29 bits. But it kept the 8 [03:08.630 --> 03:15.030] bytes of the data. Then further CAN bus versions have been introduced such as CAN FD with 64 [03:15.030 --> 03:21.610] bytes of data. But the talk that I'm having today is only applicable to the standard CAN ID [03:21.610 --> 03:27.890] frame and extended CAN ID frame. We'll discuss briefly CAN FD and CAN XL but only at the end [03:27.890 --> 03:37.110] of this talk. This slide shows the message, the frame and its individual fields. In the green [03:37.110 --> 03:42.190] part there is the most important field that's CAN ID. Obviously you need that. Then in the yellow [03:42.190 --> 03:48.790] you can see field called DLC or data length code. This tells us how many bytes of data actually [03:48.790 --> 03:55.570] follow. That could be between 0 to 8. So there is a perfect valid message in CAN bus that has [03:55.570 --> 04:00.830] 0 bytes of data. The meaning could be carried just inside the ID. But you can have anywhere [04:00.830 --> 04:07.430] up to 8 bytes of data. Then in the blue field you can see the CRC but that's not really relevant [04:07.430 --> 04:14.870] for this talk so I'm going to skip the remaining fields. For the picture at the bottom, that's a [04:14.870 --> 04:20.170] capture from an oscilloscope. It shows you how it works on the physical layer. You can see the [04:20.170 --> 04:25.770] differential signaling where the red line is CAN high, the blue line is CAN low. As you can see [04:25.770 --> 04:31.010] the data is basically identical. It's just a mirror image between those two. We'll be playing [04:31.010 --> 04:36.190] a bit with the oscilloscope later on in the demo. But for now this is just for me to show you how [04:36.190 --> 04:40.950] it works on the physical layer. It's not really relevant to what I'm going to talk about. [04:43.290 --> 04:49.810] If we dive a bit into the specification of the CAN bus and we look at the description of the [04:49.810 --> 04:57.510] data length code field, we will find that it has to have values between 0 and 8. To express 8 in [04:57.510 --> 05:04.710] binary, you need 4 bits because that's 1 followed by 3 zeros. And therefore, I got an idea like [05:04.710 --> 05:12.850] what would happen if I'm going to just send DLC field with all 4 ones. First disclaimer, you can [05:12.850 --> 05:20.130] never send more than 8 bytes of data in those CAN frames. Those packets would be just malformed, [05:20.130 --> 05:27.090] never really received, so you'll never send more than 8 bytes of data. However, what I found is [05:27.090 --> 05:33.330] that none of the hardware and most of the software actually cares about the DLC field value. So you [05:33.330 --> 05:38.610] can send whatever you want there and it's still valid and it will still be passed all the way to [05:38.610 --> 05:44.090] the developer. And then it's up to the firmware development team how they work with this value. [05:44.670 --> 05:50.970] On the slide, you can see a hypothetical memcpy situation. And because the developers that are [05:50.970 --> 05:55.250] working with CAN bus probably know that they can never receive more than 8 bytes, the destination [05:55.250 --> 06:00.550] buffer would be allocated to 8 bytes because why waste memory? Then the source of the operation [06:00.550 --> 06:06.230] as a second parameter would be CAN data, because that's what we want to copy. But since you're not [06:06.230 --> 06:11.950] always receiving 8 bytes, but sometimes you receive 4, 3 or any other value, you can use [06:11.950 --> 06:17.890] this convenient DLC field, which the frameworks you're using is giving you, to actually specify [06:17.890 --> 06:23.330] how many bytes should be copied. And that's exactly where the problem comes. Now I'm going to do the [06:23.330 --> 06:29.570] funny thing of readjusting the microphone. I'm sorry if I drop it, because I want to sit for the [06:29.570 --> 06:46.480] demo. Sorry for that delay. We'll basically jump into our first demonstration. But before that, I'd [06:46.480 --> 06:51.300] like to introduce you to the hardware that I brought with me. It's in this yellow suitcase [06:51.300 --> 06:58.020] that you can see on the stage. And as you can see, it contains a lot of PCBs, wires, some displays, [06:58.020 --> 07:04.060] LEDs. And I hope you like it at least as much as people at the airport security did, because I had [07:04.060 --> 07:10.720] to explain to like 3 people what it is. I had to take it apart. And they basically swiped the [07:10.720 --> 07:16.840] hell out of it with those anti-explosive detective papers. So it's kind of a miracle it still works, [07:16.840 --> 07:25.840] but it does. In this case, I have a red board here, the red one in the corner. That will be a [07:25.840 --> 07:30.540] victim for our first demo. It's an NXP board where I'll have source code running and everything. [07:31.100 --> 07:36.980] The guy in the corner here, the simple Arduino, that's the attacker. It has a little button here [07:36.980 --> 07:41.600] that you can't really see, but whenever I press it, a malicious message will be sent. I'm going [07:41.600 --> 07:47.520] to ignore the middle Arduino for now. And this is just a power source so that it actually works. [07:47.520 --> 07:53.920] So that's not really important. Now, if I'd like to show you actually how the source code looks [07:53.920 --> 08:00.060] like for these boards, I'll start with the attacker one, so the Arduino. In the setup function, [08:00.060 --> 08:05.320] I'll just set up CAN bus serial and the button. That's not really interesting. Then I set up the [08:05.320 --> 08:12.600] message. I'll start by setting a CAN ID 600A. You'll find out in the second demo why. But the [08:12.600 --> 08:20.300] most important thing, I'll set the DLC field value to F. Then I need to set some dummy payload. In [08:20.300 --> 08:25.260] this case, it would be A, B, C, D, E, F, G, H in capital ASCII so that we can actually see it and [08:25.260 --> 08:30.900] refer to it while we're debugging. And inside the loop, it just waits for the button press and [08:30.900 --> 08:38.260] whenever that happens, we're going to send the message with this function. When I switch to the [08:38.260 --> 08:43.360] victim, so the red board code, which we will be debugging, there's a main function. We just have [08:43.360 --> 08:49.840] two variables. One is obviously called overwrite buffer because we're going to overwrite it. And [08:49.840 --> 08:56.720] we start by setting it to value 1, 2, 3, 4, 5, 6, 7, again, so that we have something easy to refer [08:56.720 --> 09:04.400] to. Then there is not really interesting CAN bus setup, which I'll just skip. And in here, I'll be [09:04.400 --> 09:12.320] waiting for a message to arrive. And when it does, there is the main copy function, which is basically [09:12.320 --> 09:19.080] identical to the one from the previous slide, where we're copying into the buffer. Frame DSR [09:19.080 --> 09:26.860] is how MCU Expresso chose to call the data field. I'm not to blame for that. And frame DLR is the [09:26.860 --> 09:32.080] DLC field value. That's how it's called in MCU Expresso. Again, in every framework, it will be [09:32.080 --> 09:38.220] called differently. Since I have the board in the case, I can just hit debug, and this should run [09:38.220 --> 09:45.340] it, run the code, and we should stop on our first breakpoint. If I step over this, I can just [09:45.340 --> 09:51.280] investigate that the override buffer is now set to values 1, 2, 3, 4, 5, 6, 7, and ASCII terminated [09:51.280 --> 09:56.960] by a null byte, which is exactly as expected. And if I run the code, nothing really happens, [09:56.960 --> 10:01.180] because we're now in this endless loop and we're waiting for CAN messages to arrive. [10:01.460 --> 10:09.560] To do so, I need to press this almost invisible button, and hopefully you see a notification [10:09.560 --> 10:14.420] pop up here on my screen that tells me that the breakpoint was hit at this memcpy function. [10:15.340 --> 10:21.620] 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, [10:21.620 --> 10:27.100] which we've set in the Arduino. And in the DLR, we can see value 15, which is the DLC field [10:27.640 --> 10:33.000] value which we've set. I'm just going to step over this so that the memcpy finishes what it did. [10:33.120 --> 10:40.920] Now, inside the buffer, I should have this A, B, C, D string. But more importantly, [10:40.920 --> 10:48.000] let's have a look at what is in the overwrite buffer. I'm just going to scroll over it. [10:48.240 --> 10:53.980] The very first value, I hope it's readable a bit, is value 15, which is suspicious, [10:53.980 --> 10:59.300] because 15 is the DLC field we've sent. But the remaining values I don't really recognize, [10:59.300 --> 11:05.560] because there is nullbyte, then 137, 172, then repeated, then another nullbyte. The final [11:05.560 --> 11:09.260] nullbyte hasn't been overwritten, because we're overwriting just with seven bytes, [11:09.260 --> 11:14.320] so the final nullbyte is from the original buffer. But now it would be good to see whether [11:15.340 --> 11:21.300] we have some control over the data that we're overwriting with. To do that, we have to investigate [11:22.880 --> 11:28.000] a structure called mscanframe. Again, this is specific to MCUXpresso framework. In other [11:28.000 --> 11:33.300] frameworks, you'll find something completely different. And we'll see that the value of DSR, [11:33.300 --> 11:37.220] which is the data where we started the copy operation, is actually a member of a union, [11:37.220 --> 11:41.220] so that you can access the data as data words or data bytes. [11:41.400 --> 11:47.420] But right after that, there is the DLR value, which is the DLC. This confirms our suspicion [11:47.420 --> 11:54.920] that we are overwriting with this structure. But the remaining values, such as buffer priority, [11:54.920 --> 12:00.320] type, format, and some timestamps, those aren't really in control of the attacker. So we have [12:00.320 --> 12:06.600] very limited control of what we overwrite with. But nevertheless, we can actually use this for [12:06.600 --> 12:10.720] some other impact than just denial of service that's in the title of this talk. I can imagine [12:10.720 --> 12:17.260] situations where you overwrite local and global variables in a way that you change their values [12:17.260 --> 12:23.600] from a zero to something else. Why this is important? And that's because when you program [12:23.600 --> 12:28.720] in C, there's quite a common practice of doing a null check. A null check is, for example, [12:29.320 --> 12:34.060] is authenticated if it is a variable. Then if it is zero, you're not authenticated, [12:34.060 --> 12:39.340] and anything else would mean you are. And this way you could theoretically bypass some of the [12:39.340 --> 12:46.860] controls. Although in the real life examples, this always resulted just in the denial of service, [12:46.860 --> 12:51.480] usually for various reasons, but mainly by overwriting pointers to something that makes [12:51.480 --> 12:57.360] absolutely no sense, and then crashing the firmware. With that, I'd like to transition [12:57.360 --> 13:04.500] into the second demonstration where I'll be using an ECU from a Tesla Model 3 vehicle. [13:04.620 --> 13:08.940] It's the security controller that sits between the seats where you tap your NFC card when you [13:08.940 --> 13:15.380] want to drive. It's responsible, for example, for unlocking the door and the decision whether you [13:15.380 --> 13:20.620] can drive or not. This is done together with the body control module. And if we do a typical [13:20.620 --> 13:29.480] assessment, we first unpack the ECU and see what the PCB has to offer. In this case, we found that [13:29.480 --> 13:37.280] on the bottom of the PCB, there is an SPC560B64 microcontroller from STMicro, and literally right [13:37.280 --> 13:43.680] next to it are pads which are labeled STJTAG. The jackpot in this case was that this was actually [13:43.680 --> 13:48.780] working JTAG interface, so we were able to dump the firmware image and proceed with the assessment. [13:48.780 --> 13:53.280] And this is exactly what I'd like to do here. And we will start by dumping the image, then we will [13:53.280 --> 13:58.540] analyze the firmware, find the vulnerability, exploit it, and then play with the oscilloscope [13:58.540 --> 14:06.020] and have some fun. So let me just show you the unobfuscated view on the suitcase. You can see [14:06.020 --> 14:11.800] the ECU, obviously the big green thing with the Tesla logo. The Arduino in the middle serves only [14:11.800 --> 14:17.200] one purpose, and that is to simulate the rest of the car. So in fact, this suitcase is the smallest [14:17.200 --> 14:22.860] Tesla car that you have probably seen. I'll have this again, a little black button. Whenever I [14:22.860 --> 14:28.180] press it, I simulate the rest of the car so that the ECU works as if it is in. If I don't do that, [14:28.180 --> 14:35.040] I can't really work with this ECU and show you it live. I talked about the debugger, so I have [14:35.040 --> 14:41.500] just the JLink. I hope it's readable. In this suitcase, there are actually multiple layers. One [14:41.500 --> 14:49.200] of the layers contains a JLink debugger, and I can connect with it and just type connect. I have [14:49.200 --> 14:56.520] pre-selected the correct MCU, so SPC560B64. So I'll just leave everything default, because I like [14:56.520 --> 15:02.800] defaults. And the CPU is now halted and I'm connected to it. This is a good time to dump the [15:02.800 --> 15:10.560] firmware image. I can use savebin command for that. And as a destination, I'll choose file tmpfw.bin, [15:10.560 --> 15:15.600] for example. And now I have to specify the memory range, which I want to dump. So it will be from [15:15.600 --> 15:24.120] address 0 to address 17FFFF. This memory range, I've read it from the data sheet, but I consider [15:24.120 --> 15:29.740] it kind of boring to open a data sheet here, so I'm just going to cheat and skip on that. But this [15:29.740 --> 15:34.960] will save the binary from that section. You can find it in the memory map. There's actually no [15:34.960 --> 15:42.700] science around this. Now we have a firmware dumped, and I'm going to open IDA and analyze the [15:42.700 --> 15:49.220] binary. I made all fonts ridiculously large, so I hope you can read it. For me, it's quite painful [15:49.220 --> 15:57.600] to work like this, but it's for the audience's sake. I'm going to choose PowerPC, because as I [15:57.600 --> 16:04.440] said, I was reading the data sheet, so I know that it's a PowerPC. And from now on, I'll use my [16:04.440 --> 16:11.880] beloved defaults everywhere. And this should load me a binary in an IDA. But as you can see, [16:11.880 --> 16:17.320] nothing really happened, because we're working with a binary blob, and IDA doesn't really know, [16:17.320 --> 16:21.500] is it the code? Is it data? Is it a mix of both? There are no entry points. What should I do? [16:22.260 --> 16:28.080] Since this is a situation that we faced quite often, working with extracted firmware image [16:28.080 --> 16:33.740] from a microcontroller, we made a little plug-in for IDA Pro, which is called Firm Loader. And [16:33.740 --> 16:41.500] here I just have to select the brand, the line of the MCU, and the correct MCU. And this will [16:41.500 --> 16:46.800] start the auto-analysis. It will also create memory sections such as RAM, peripherals, [16:46.800 --> 16:53.060] and will mark the code flash as executable and trigger the auto-analysis. If I'm to do a real [16:53.060 --> 16:58.160] assessment, this is a starting point, not a complete analysis. I have to do a bit more manual [16:58.160 --> 17:05.280] work to make it ideal, but we don't have time and we have to cheat a bit. So I'm going to be fine [17:05.280 --> 17:13.280] with the analysis that it did, mainly because it already found 1700 or so functions. So we have [17:13.280 --> 17:18.020] the binary loaded and we can start working with it. Now I'd like to show you two different ways [17:18.020 --> 17:24.900] on how to approach firmware analysis of an MCU. The first approach is identifying standard [17:24.900 --> 17:32.240] functions. Standard functions are things like memcpy, malloc, free, all of those libc functions [17:32.240 --> 17:37.380] that developers use everywhere. It's good to find them, because then it's a bit easier to [17:37.380 --> 17:42.940] uncover the custom logic in the firmware. You can use AIDA Lumina if you have AIDA for that. [17:42.940 --> 17:47.400] It's a great tool, but I'm going to do it manually, not because I just want to scare you with the [17:47.400 --> 17:53.540] assembly, but I just actually want to show you that it's not that hard. Let me just adjust the [17:53.540 --> 17:59.680] view of AIDA a little bit so that we have a more comfortable view. And I'm also going to need to [17:59.680 --> 18:06.600] set the compiler. Sorry for that delay. And this function is actually memcpy. I'm just going to [18:06.600 --> 18:13.360] walk you through very quickly on why it is a memcpy. Again, I had to read about PowerPC to [18:13.360 --> 18:18.820] figure out that the first three arguments to a function are passed in registers R3, R4, and R5. [18:18.820 --> 18:25.360] Therefore, if I look at the memcpy parameters, I know that the first one is the destination, [18:25.360 --> 18:29.440] the second one is source, and the third one is how many bytes should be copied. [18:29.760 --> 18:34.880] So the third parameter, R5, is here first incremented, and then it's set into something [18:34.880 --> 18:40.520] called counter register. Again, I had to read about PowerPC, unfortunately. Then this counter [18:40.520 --> 18:47.020] register is decremented every time we execute this instruction. And if it reaches zero, [18:47.020 --> 18:53.060] we go here to end the function. But as long as it's not zero, we will be executing this code [18:53.060 --> 18:59.200] block in a loop. So we already know that whatever this function does, it will do it for how many [18:59.200 --> 19:03.800] times we tell it in a third argument, which is already a good definition of a memcpy. [19:04.320 --> 19:10.360] But inside the body of the loop, we can see that it takes the second argument, so register R4, [19:10.360 --> 19:18.120] indexed by R0, and stores that value into R7. Then instruction after that takes that value from [19:18.120 --> 19:25.060] R7 and stores it in the first argument, which is R3, again indexed by R0. Then the index is just [19:25.060 --> 19:30.500] incremented and this repeats for however many times we specified. This is how you find a standard [19:30.500 --> 19:36.780] function. Sometimes it's a bit more complicated. I admit this was one of the very easiest examples. [19:37.340 --> 19:41.180] I'm also going to set the type of the function because I would like to show you some automation [19:41.180 --> 19:48.640] later and for that this is necessary. Now, if I'm doing a security analysis of a firmware, I can hit [19:48.640 --> 19:54.480] X on the keyboard and I should now theoretically go through all of these memcpy cross-references [19:54.480 --> 20:00.240] and see if they are vulnerable or not. I'm lazy and I don't have time, so I'm not going to do that. [20:00.980 --> 20:05.920] Second approach that I'd like to show you is a combination of static and dynamic analysis. [20:06.780 --> 20:12.120] During the dynamic analysis I know what the ECU sends on a CAN bus. I know what it receives on [20:12.120 --> 20:17.200] the CAN bus. So it's actually very simple to just use search for immediate values [20:18.680 --> 20:26.000] and find the CAN ID I'm interested in. This isn't exactly working every time, but in this case it [20:26.000 --> 20:32.740] does. So the second hit is especially interesting because it's in compare instruction and it's [20:32.740 --> 20:38.520] comparing register R3, so first argument to a function, to the value that I know I saw on the [20:38.520 --> 20:43.240] CAN bus. And if I look around I actually see a lot of the values that I've seen on the CAN bus. [20:43.240 --> 20:49.200] This one, this one, this one. So this is good enough assumption for me to just rename this [20:49.200 --> 20:56.960] to CAN handler because it appears to be handling incoming CAN messages. And now I'll end the [20:56.960 --> 21:02.420] torture and switch to the decompiler because we need to speed up a bit. And we said that the first [21:02.420 --> 21:09.240] one is CAN DLC, so I'm just, uh, CAN ID, sorry. I'm going to rename it to that. With a bit of [21:09.240 --> 21:14.400] analysis that I'm not going to show you here, uh, we would figure out this is CAN data and we [21:14.400 --> 21:21.360] would actually figure out this is CAN DLC. And now we basically went from a black box assessment [21:21.360 --> 21:29.440] where we took an ECU from a vehicle to a crappy source code review. Uh, and again, as you can [21:29.440 --> 21:35.040] see there are many CAN bus IDs being handled by this function. And in theory I should go [21:35.040 --> 21:41.400] through all of them and identify whether any of these are vulnerable to some issues. Again, I'm [21:41.400 --> 21:46.880] lazy, I don't have time so I'm not going to do that. Uh, instead I'm going to skip over and if [21:46.880 --> 21:52.400] you remember the attacking board sends CAN ID 600A. So I'm going to just look at the handler [21:52.400 --> 21:59.160] for CAN ID 600A. I'm going to rename it to that because I don't want to lose track. And this [21:59.160 --> 22:06.120] function gets CAN data and CAN DLC as parameters. So let's just open it. And do the renaming [22:06.120 --> 22:17.200] again. And now this mem copy here screams at us I'm vulnerable basically. Because as you can see [22:17.200 --> 22:23.820] this length specifier here is CAN DLC. The source is CAN data. And as a destination there is this [22:23.820 --> 22:29.260] weird memory address. Uh, weird memory addresses in MCU firmware are usually global variables. [22:29.260 --> 22:34.760] They're just static places in memory which are then used to access the variable values. If I look [22:34.760 --> 22:41.640] at it, I can find that there is between 1AF0 and 1AF7 I have preallocated 8 bytes for this [22:41.640 --> 22:47.740] variable. And right after it there is variable called 1AF8. And if I look at the cross [22:47.740 --> 22:53.280] references I can see that it is being used somewhere. So if I overflow this variable, I may [22:53.280 --> 22:59.080] adjust something in the firmware and cause some undesired behavior. I'm just going to [22:59.080 --> 23:04.760] synchronize the view because that's going to be useful if my right click would have worked. [23:04.980 --> 23:15.650] Sorry for that. And now when we saw the CAN handler, there were many, many, many functions [23:15.650 --> 23:20.930] that were handling CAN messages. I said I'm lazy so I'm not going to do that. But because I'm [23:20.930 --> 23:27.410] lazy all the time, but I don't have time just right now, we created a plugin that automates a [23:27.790 --> 23:35.670] bit a search for cross references. This plugin is called Wolfie. And to work with it, I wonder [23:35.670 --> 23:43.690] why my touch pad doesn't work. Thanks. You just have to add certain functions. I'm sorry I [23:43.690 --> 23:48.990] don't know how to enlarge this text so I'll just describe it. We first select some name for [23:48.990 --> 23:55.110] the rule that actually doesn't matter at all. And then we have to use a filter to somehow [23:55.110 --> 23:59.990] filter out all the memcpy functions we're not interested in or to just filter those that we [23:59.990 --> 24:05.310] are interested in. To find this vulnerability, there is one precondition. The memcpy function [24:05.310 --> 24:11.130] must not have static third parameter. If there is a static value like 2, 3, 8 in a third [24:11.130 --> 24:18.650] parameter, this vulnerability will not work. So I'll start by saying that the not param 2 dot [24:19.170 --> 24:27.990] is constant. I'm sorry for the syntax, it's just Python code that gets evaluated. And if this [24:27.990 --> 24:32.490] condition is fulfilled, I can also say that I'm actually only interested in the functions that [24:32.490 --> 24:40.290] are called from the can handler. So I can just say function, if I'm be able to type, function [24:40.290 --> 24:52.590] call dot reachable from can handler. And this query will now take a look at all 100 plus [24:52.590 --> 24:57.470] cross references to a memcpy function and it will only filter out those that match the [24:57.470 --> 25:03.370] criteria I've specified. As you can see, the very first one is the one we found manually in [25:03.370 --> 25:11.210] can handler 68, but there are five others. I'd like to specifically look at the one at 6F1BA, [25:12.050 --> 25:16.430] which is right here. The main reason I want to look at it is if I look at the cross references [25:16.430 --> 25:24.410] of this function, we can see it's being called at the can handler 68 here. And it's right here. [25:24.410 --> 25:38.510] As you can see again, it gets canData, canDLC as parameters. So rename again. And now we can see [25:38.510 --> 25:44.030] that right in the middle of the function, there is a memcpy function. It uses as a source canData [25:44.680 --> 25:51.050] as a count of bytes. This time it's V4, but V4 is here, it's just canDLC, so it's just canDLC [25:51.050 --> 25:57.250] through a proxy, let's say. The destination in this time is variable V7, which is a local [25:57.250 --> 26:04.310] variable. It is an 8 byte array. So we can share again, we will be overflowing. But we will be [26:04.310 --> 26:09.950] overflowing into something called variable V8. And if you look at what it is, it actually is set [26:09.950 --> 26:15.250] to some constant value here from a global variable at the beginning of the function. And then it's [26:15.250 --> 26:22.290] compared to it at the end of the function. This is a thing called stack canaries. And it is used [26:22.290 --> 26:28.290] to protect the values on the stack against buffer overflows such as this one. So in this case, we [26:28.290 --> 26:33.970] know that when we send this scan message, we would actually cause this stack canary to get [26:33.970 --> 26:38.910] triggered. But we should have a look at what's going to happen. And that's in this function, we [26:38.910 --> 26:43.930] will actually play a bit with peripherals. But more importantly, there is this endless while [26:43.930 --> 26:50.370] loop. Endless while loops are the best indicators for fault handlers in microcontrollers. Because [26:50.370 --> 26:56.150] while in endless while loop, the device cannot cause any harm by just doing weird things with [26:56.150 --> 27:02.590] its peripherals. It's just executing the same instruction over and over again. Therefore, we [27:02.590 --> 27:08.210] will cause a crash of this microcontroller. That's why denial of service. But unfortunately, it [27:08.210 --> 27:14.710] would be through an anti-climatic overwrite of a stack canary. We have access to the debugger, so [27:14.710 --> 27:19.170] it would be quite nice to actually see this in action and see if we actually overwrite with the [27:19.170 --> 27:24.690] same values as we have seen in the previous demo. So to do that, I decided to torture myself [27:24.690 --> 27:33.970] further and show you the access in the debugger. Where I can set breakpoints. First one would be [27:33.970 --> 27:44.320] at before mem copy function. The second one will be right after it. And I'll check if the CPU is [27:44.320 --> 27:51.100] halted. It is. I'll let it go. Now, the ECU wouldn't actually react to any kind of messages [27:51.100 --> 27:56.660] because it's sleeping state. So I have to press this button. This will turn on one of the LEDs [27:56.660 --> 28:02.020] that were so loved at the airport. And once it turns off, I know that the car is properly [28:02.020 --> 28:11.200] simulated and I can start working with the ECU. The CPU still shouldn't be halted. But this would [28:11.200 --> 28:19.600] change after I send the attacker message. And as you can see, it did. Now we're right before the [28:19.600 --> 28:24.980] mem copy function. And we can investigate the registers. And from the beginning of the demo, we [28:24.980 --> 28:31.900] can actually know that the register R3 is the one that holds the destination location. So I can [28:31.900 --> 28:38.880] use mem 32 function command to dump that part of the memory. I'm also terrible sorry to anyone who [28:38.880 --> 28:46.520] knows how to work with Jlink and these kind of commands. I'm not really good with that. But here, [28:46.520 --> 28:52.440] these eight bytes are the legitimate V7 buffer. This is what should be overwritten with the [28:52.440 --> 28:59.560] candata. This is stack canary, which is interestingly set to all zeros. And this is the value [28:59.560 --> 29:06.540] that should be protected by the stack canary. And now comes the part where I'm not really good [29:06.540 --> 29:13.060] with Jlink. Because this is how I step over break points. As I said, I'm sorry to anyone who [29:13.060 --> 29:21.300] knows how to do that. And if I repeat the command now, we can see that these 41, 42, this is [29:21.300 --> 29:26.960] basically the A, B, C, D, E, F, G, H string. This is the stack canary, which has been [29:26.960 --> 29:33.760] overwritten with the can ID. So we can cheer up a bit because we are controlling quite a lot of [29:33.760 --> 29:39.620] bytes that we're overflowing. But we can't really because if we change the can ID, this overflow [29:39.620 --> 29:45.760] would never happen. So again, we can't really change what we overflow with in this case. Then [29:45.760 --> 29:50.480] this is probably can DLC based on the value here. Then there are some null bytes for some [29:50.480 --> 29:56.220] flags inside the can structure. And this D8 is from the original pointer. We haven't really [29:56.220 --> 30:03.740] overwritten that. Now having access to the debugger is nice. And I'm not really good with [30:03.760 --> 30:09.800] that. But sometimes you don't have that. And let me just check that the CPU is not halted. It is [30:09.800 --> 30:15.720] not. And in this suitcase, there is a third layer underneath the J link layer and that contains [30:15.720 --> 30:20.520] an oscilloscope. One of the oscilloscope probes is connected here to the CAN bus. The other [30:20.520 --> 30:28.800] oscilloscope probe is hidden under this to sense the NFC field. So if I now press this simulation [30:28.800 --> 30:35.780] button, I can go into the oscilloscope view and start tracking. The red line is the NFC field. [30:35.780 --> 30:41.560] The blue line is the CAN bus. So when the ECU comes up, you can see NFC field is up. And if I [30:41.560 --> 30:49.460] press the attacker button, you'll first see that... let me stop that first. You'll see that the CAN [30:49.460 --> 30:55.460] messages have stopped being sent. That's the reset of the ECU. And about a half a second [30:55.460 --> 30:59.940] after that, the NFC field got killed as well. This is how we initially found the vulnerability [30:59.940 --> 31:06.240] through fuzzing and using this side channel detection. The delay between the crash of the MCU [31:06.240 --> 31:11.120] and the NFC field disappearing is caused by the fact that the NFC controller is actually a [31:11.120 --> 31:16.880] separate chip. And there is a communication line between the main MCU and the NFC chip. And [31:16.880 --> 31:22.180] that's why there is a bit of a half second delay before that shuts down. I'll try to zoom into [31:22.180 --> 31:28.140] this, but I have never found a way how to increase text here. So you'll probably not be able to [31:28.140 --> 31:35.640] read that. But the message before this drop happens, the last message is the one we've sent. [31:35.640 --> 31:41.640] So ID 600A. And the voltage drop is actually caused by the GPIO pins being reset into their [31:42.000 --> 31:48.880] default state. After that, if I zoom out a bit, there is a break in CAN messages being sent. This [31:48.880 --> 31:53.320] is because the ECU is restarting. And then it gets into the initial state where it again [31:53.320 --> 32:01.520] waits for it to be restarted. This is just one of the side channel ways. And when I was here, I've [32:01.520 --> 32:07.800] been actually given this invitation to a high roller cone. And quite interestingly, it [32:07.800 --> 32:13.280] actually works as an NFC field detector. So if you don't want to use oscilloscopes, for [32:13.280 --> 32:21.240] whatever reason, you can actually use this invitation that you can get here at DEF CON. If [32:21.240 --> 32:30.460] placed properly, this will light up the LED here whenever it sees the NFC field. And then by [32:30.460 --> 32:36.560] just pressing this button, you'll see that the NFC field will stop. I'm not going to the high [32:36.560 --> 32:44.620] roller cone, by the way. I have other plans. But anyway, thanks. I like this. The other side [32:44.620 --> 32:50.660] channel possibilities are detecting power consumption. That, however, requires soldering, [32:50.660 --> 32:58.780] which we have done here. But you can also do it on the CAN bus. There are usually messages [32:58.780 --> 33:03.060] that appear periodically. So you can detect the time difference in between the periodic [33:03.060 --> 33:08.860] messages. And the easiest way is when you find an ECU that actually sends a specific [33:08.860 --> 33:15.340] message on a reset. That's the easiest way how to detect a reset. There is a fuzzer published [33:15.340 --> 33:19.120] for that as well. It's actually not published yet because I haven't clicked the button to [33:19.120 --> 33:24.800] publish it. But it will be right after this call. Now I'd like to discuss who's affected by this [33:24.800 --> 33:29.800] issue. It could be basically anyone who just blindly accepts the value of this DLC field in [33:29.800 --> 33:35.800] some memory copy operations. We also looked at the popular SDKs and RTOSs and found that [33:35.800 --> 33:42.580] Armabed OS, STM32Cube SDK and NXP MCU Express OSDK, they don't really care and they will send [33:42.580 --> 33:48.640] you any value in the DLC field that you send to it. We reported this to all of them. So as of [33:48.640 --> 33:54.000] now, this should have been fixed. But there are other frameworks, some of them commercial, we [33:54.000 --> 33:58.940] haven't really looked at. So those might be still vulnerable. We looked also at the default [33:58.940 --> 34:05.140] Linux kernel drivers, which are not vulnerable. So those will never ever pass you anything higher [34:05.140 --> 34:11.080] than 8 in the DLC field if you're working with it. And now is a good time to look at other [34:11.080 --> 34:17.580] CAN bus versions. So CAN FD is not affected by the nature of it because the data payload was [34:17.580 --> 34:23.820] increased to 64 bytes. But the DLC field stayed at 4 bits and there is this weird translation [34:23.820 --> 34:30.460] table where you translate into those 4 bits how many bytes follow. CAN XL is likely not [34:30.460 --> 34:35.960] affected. I would like to wait with any judgments here because I haven't seen any real [34:35.960 --> 34:43.340] implementation of it anywhere. But based on the specification, it should not be. Finally, I [34:43.340 --> 34:47.560] would like to have some general takeaways from this. So first of all, if you're a developer, [34:47.560 --> 34:52.460] regardless of the protocol you work with, you should consider the metadata of it as [34:52.460 --> 34:59.180] entrusted, not just the data it arrives over it. It is also good to know how the libraries [34:59.180 --> 35:03.220] you're actually working with are handling values such as length of incoming data for that [35:03.220 --> 35:07.960] matter. And if you're on the assessment side such as me, I would like to suggest that it's [35:07.960 --> 35:12.440] always worth looking one layer below the obvious application data into the protocol world [35:12.440 --> 35:17.660] because there may be some interesting vulnerabilities in there as well. With that, I'd [35:17.660 --> 35:22.240] like to thank you for your attention and ask if you have any questions. I'll try to stand and [35:22.240 --> 35:36.070] adjust this microphone again. The high roller invitation, although I'm not sure if it's [35:36.070 --> 35:41.630] invitation, I can give it to someone if they want. I have enough things that the tech NFC [35:41.630 --> 35:55.900] feels at home. There's a microphone over there so that everyone can hear. Have you tried to [35:55.900 --> 36:02.980] increase the F value to read more memory and see what happens? I'm sorry, I haven't heard. [36:03.440 --> 36:08.140] Could you come just here? This microphone seems to work a bit better. You have a stage [36:08.140 --> 36:18.440] time. So the question was whether I tried to increase the F value in the DLC. I can't [36:18.440 --> 36:24.920] because there are just four bits. And if I set all four bits to one, it is up to F. I'd love to [36:24.920 --> 36:31.320] have more than F, but I can't. Actually, in CanXL, the DLC field is 11, which can fit up to [36:31.920 --> 36:40.740] 2047. But then the specification says you can have up to 2048 bytes of data. And I don't know [36:40.740 --> 36:44.980] how is that going to work. But I'll wait for the implementation and then I'll hack crap out of [36:44.980 --> 36:56.740] it. I guess that's all. So thank you and have a nice day.