First of all, allow me to introduce myself because you probably don't know me. I work as a security engineer for over eight years. Currently I'm working in Accenture Embedded System Security Team that's based in Prague, Czech Republic. That's also where I come from. 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. Let's have a look at what's ahead of us. We will start with the description of the CAN bus. 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. 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. 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. 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. So I brought an ECU from a vehicle and I'll be doing security assessment of that. This demo is quite packed. We will be using JTAG debugger, we will be analyzing firmware, we will be using oscilloscopes so there's a lot to see. 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. At the end I hope there will be still some time left for questions and answers. So without further ado, let's jump into the first slide and that's the description of the CAN bus. 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. To achieve this there are two wires used for this which are carrying a differential signal. Differential signal means that the data being transmitted over these two lines is basically identical, it's just the polarity is inverted. And each of these buses is terminated by 120 ohm resistor. Typical vehicle probably contains multiple of these buses which are usually connected by something that's called security gateway. If I'm to use analogy to IP networks that would be like a router that decides which message can pass where. Thanks to these properties like robustness, it's found its way into other industries as well. That's why the talk is described as embedded devices. 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. It's also important to mention that there are multiple versions of the CAN bus as it evolved over the time. It started with an 11 bit identifier CAN bus that carried just 8 bytes of data. These have been deemed insufficient so an extended CAN ID came which just increased the ID portion to 29 bits. But it kept the 8 bytes of the data. Then further CAN bus versions have been introduced such as CAN FD with 64 bytes of data. But the talk that I'm having today is only applicable to the standard CAN ID frame and extended CAN ID frame. We'll discuss briefly CAN FD and CAN XL but only at the end of this talk. This slide shows the message, the frame and its individual fields. In the green part there is the most important field that's CAN ID. Obviously you need that. Then in the yellow you can see field called DLC or data length code. This tells us how many bytes of data actually follow. That could be between 0 to 8. So there is a perfect valid message in CAN bus that has 0 bytes of data. The meaning could be carried just inside the ID. But you can have anywhere up to 8 bytes of data. 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. For the picture at the bottom, that's a capture from an oscilloscope. It shows you how it works on the physical layer. You can see the differential signaling where the red line is CAN high, the blue line is CAN low. As you can see the data is basically identical. It's just a mirror image between those two. We'll be playing a bit with the oscilloscope later on in the demo. But for now this is just for me to show you how it works on the physical layer. It's not really relevant to what I'm going to talk about. 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. To express 8 in binary, you need 4 bits because that's 1 followed by 3 zeros. And therefore, I got an idea like what would happen if I'm going to just send DLC field with all 4 ones. First disclaimer, you can never send more than 8 bytes of data in those CAN frames. Those packets would be just malformed, never really received, so you'll never send more than 8 bytes of data. However, what I found is that none of the hardware and most of the software actually cares about the DLC field value. 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. And then it's up to the firmware development team how they work with this value. On the slide, you can see a hypothetical memcpy situation. 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? Then the source of the operation as a second parameter would be CAN data, because that's what we want to copy. 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. And that's exactly where the problem comes. Now I'm going to do the funny thing of readjusting the microphone. I'm sorry if I drop it, because I want to sit for the demo. Sorry for that delay. We'll basically jump into our first demonstration. But before that, I'd like to introduce you to the hardware that I brought with me. It's in this yellow suitcase that you can see on the stage. And as you can see, it contains a lot of PCBs, wires, some displays, LEDs. 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. I had to take it apart. And they basically swiped the hell out of it with those anti-explosive detective papers. So it's kind of a miracle it still works, but it does. In this case, I have a red board here, the red one in the corner. That will be a victim for our first demo. It's an NXP board where I'll have source code running and everything. The guy in the corner here, the simple Arduino, that's the attacker. It has a little button here that you can't really see, but whenever I press it, a malicious message will be sent. I'm going to ignore the middle Arduino for now. And this is just a power source so that it actually works. So that's not really important. 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. In the setup function, I'll just set up CAN bus serial and the button. That's not really interesting. Then I set up the message. I'll start by setting a CAN ID 600A. You'll find out in the second demo why. But the most important thing, I'll set the DLC field value to F. Then I need to set some dummy payload. 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. 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. When I switch to the victim, so the red board code, which we will be debugging, there's a main function. We just have two variables. One is obviously called overwrite buffer because we're going to overwrite it. 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. Then there is not really interesting CAN bus setup, which I'll just skip. And in here, I'll be waiting for a message to arrive. 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. Frame DSR is how MCU Expresso chose to call the data field. I'm not to blame for that. And frame DLR is the DLC field value. That's how it's called in MCU Expresso. Again, in every framework, it will be called differently. 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. 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. 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. 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. 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. And in the DLR, we can see value 15, which is the DLC field value which we've set. I'm just going to step over this so that the memcpy finishes what it did. Now, inside the buffer, I should have this A, B, C, D string. But more importantly, let's have a look at what is in the overwrite buffer. I'm just going to scroll over it. 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. But the remaining values I don't really recognize, because there is nullbyte, then 137, 172, then repeated, then another nullbyte. The final nullbyte hasn't been overwritten, because we're overwriting just with seven bytes, so the final nullbyte is from the original buffer. But now it would be good to see whether we have some control over the data that we're overwriting with. To do that, we have to investigate a structure called mscanframe. Again, this is specific to MCUXpresso framework. In other frameworks, you'll find something completely different. 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. But right after that, there is the DLR value, which is the DLC. This confirms our suspicion that we are overwriting with this structure. But the remaining values, such as buffer priority, type, format, and some timestamps, those aren't really in control of the attacker. So we have very limited control of what we overwrite with. But nevertheless, we can actually use this for some other impact than just denial of service that's in the title of this talk. 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. Why this is important? And that's because when you program in C, there's quite a common practice of doing a null check. A null check is, for example, is authenticated if it is a variable. Then if it is zero, you're not authenticated, and anything else would mean you are. And this way you could theoretically bypass some of the controls. 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. With that, I'd like to transition into the second demonstration where I'll be using an ECU from a Tesla Model 3 vehicle. It's the security controller that sits between the seats where you tap your NFC card when you want to drive. It's responsible, for example, for unlocking the door and the decision whether you can drive or not. This is done together with the body control module. And if we do a typical assessment, we first unpack the ECU and see what the PCB has to offer. 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. 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. And this is exactly what I'd like to do here. 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. So let me just show you the unobfuscated view on the suitcase. You can see the ECU, obviously the big green thing with the Tesla logo. The Arduino in the middle serves only one purpose, and that is to simulate the rest of the car. So in fact, this suitcase is the smallest Tesla car that you have probably seen. I'll have this again, a little black button. Whenever I 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, I can't really work with this ECU and show you it live. I talked about the debugger, so I have just the JLink. I hope it's readable. In this suitcase, there are actually multiple layers. One of the layers contains a JLink debugger, and I can connect with it and just type connect. I have pre-selected the correct MCU, so SPC560B64. So I'll just leave everything default, because I like defaults. And the CPU is now halted and I'm connected to it. This is a good time to dump the firmware image. I can use savebin command for that. And as a destination, I'll choose file tmpfw.bin, for example. And now I have to specify the memory range, which I want to dump. So it will be from address 0 to address 17FFFF. 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. But this will save the binary from that section. You can find it in the memory map. There's actually no science around this. Now we have a firmware dumped, and I'm going to open IDA and analyze the binary. I made all fonts ridiculously large, so I hope you can read it. For me, it's quite painful to work like this, but it's for the audience's sake. I'm going to choose PowerPC, because as I said, I was reading the data sheet, so I know that it's a PowerPC. And from now on, I'll use my beloved defaults everywhere. And this should load me a binary in an IDA. 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? Is it data? Is it a mix of both? There are no entry points. What should I do? 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. And here I just have to select the brand, the line of the MCU, and the correct MCU. And this will start the auto-analysis. It will also create memory sections such as RAM, peripherals, and will mark the code flash as executable and trigger the auto-analysis. If I'm to do a real assessment, this is a starting point, not a complete analysis. 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. So I'm going to be fine with the analysis that it did, mainly because it already found 1700 or so functions. So we have the binary loaded and we can start working with it. Now I'd like to show you two different ways on how to approach firmware analysis of an MCU. The first approach is identifying standard functions. Standard functions are things like memcpy, malloc, free, all of those libc functions that developers use everywhere. It's good to find them, because then it's a bit easier to uncover the custom logic in the firmware. You can use AIDA Lumina if you have AIDA for that. 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. Let me just adjust the view of AIDA a little bit so that we have a more comfortable view. And I'm also going to need to set the compiler. Sorry for that delay. And this function is actually memcpy. I'm just going to walk you through very quickly on why it is a memcpy. 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. 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. So the third parameter, R5, is here first incremented, and then it's set into something called counter register. Again, I had to read about PowerPC, unfortunately. Then this counter register is decremented every time we execute this instruction. And if it reaches zero, we go here to end the function. But as long as it's not zero, we will be executing this code block in a loop. 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. 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. Then instruction after that takes that value from R7 and stores it in the first argument, which is R3, again indexed by R0. Then the index is just incremented and this repeats for however many times we specified. This is how you find a standard function. Sometimes it's a bit more complicated. I admit this was one of the very easiest examples. 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. 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. I'm lazy and I don't have time, so I'm not going to do that. Second approach that I'd like to show you is a combination of static and dynamic analysis. During the dynamic analysis I know what the ECU sends on a CAN bus. I know what it receives on the CAN bus. So it's actually very simple to just use search for immediate values and find the CAN ID I'm interested in. This isn't exactly working every time, but in this case it does. 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. And if I look around I actually see a lot of the values that I've seen on the CAN bus. This one, this one, this one. So this is good enough assumption for me to just rename this to CAN handler because it appears to be handling incoming CAN messages. And now I'll end the torture and switch to the decompiler because we need to speed up a bit. And we said that the first one is CAN DLC, so I'm just, uh, CAN ID, sorry. I'm going to rename it to that. 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. And now we basically went from a black box assessment where we took an ECU from a vehicle to a crappy source code review. Uh, and again, as you can see there are many CAN bus IDs being handled by this function. And in theory I should go through all of them and identify whether any of these are vulnerable to some issues. Again, I'm lazy, I don't have time so I'm not going to do that. Uh, instead I'm going to skip over and if you remember the attacking board sends CAN ID 600A. So I'm going to just look at the handler for CAN ID 600A. I'm going to rename it to that because I don't want to lose track. And this function gets CAN data and CAN DLC as parameters. So let's just open it. And do the renaming again. And now this mem copy here screams at us I'm vulnerable basically. Because as you can see this length specifier here is CAN DLC. The source is CAN data. And as a destination there is this weird memory address. Uh, weird memory addresses in MCU firmware are usually global variables. They're just static places in memory which are then used to access the variable values. If I look at it, I can find that there is between 1AF0 and 1AF7 I have preallocated 8 bytes for this variable. And right after it there is variable called 1AF8. And if I look at the cross references I can see that it is being used somewhere. So if I overflow this variable, I may adjust something in the firmware and cause some undesired behavior. I'm just going to synchronize the view because that's going to be useful if my right click would have worked. Sorry for that. And now when we saw the CAN handler, there were many, many, many functions that were handling CAN messages. I said I'm lazy so I'm not going to do that. 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. This plugin is called Wolfie. And to work with it, I wonder why my touch pad doesn't work. Thanks. You just have to add certain functions. I'm sorry I don't know how to enlarge this text so I'll just describe it. We first select some name for the rule that actually doesn't matter at all. 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. To find this vulnerability, there is one precondition. The memcpy function must not have static third parameter. If there is a static value like 2, 3, 8 in a third parameter, this vulnerability will not work. So I'll start by saying that the not param 2 dot is constant. I'm sorry for the syntax, it's just Python code that gets evaluated. 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. So I can just say function, if I'm be able to type, function call dot reachable from can handler. 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. As you can see, the very first one is the one we found manually in can handler 68, but there are five others. I'd like to specifically look at the one at 6F1BA, which is right here. 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. And it's right here. As you can see again, it gets canData, canDLC as parameters. So rename again. And now we can see that right in the middle of the function, there is a memcpy function. It uses as a source canData as a count of bytes. This time it's V4, but V4 is here, it's just canDLC, so it's just canDLC through a proxy, let's say. The destination in this time is variable V7, which is a local variable. It is an 8 byte array. So we can share again, we will be overflowing. But we will be overflowing into something called variable V8. 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. And then it's compared to it at the end of the function. This is a thing called stack canaries. And it is used to protect the values on the stack against buffer overflows such as this one. So in this case, we know that when we send this scan message, we would actually cause this stack canary to get triggered. But we should have a look at what's going to happen. And that's in this function, we will actually play a bit with peripherals. But more importantly, there is this endless while loop. Endless while loops are the best indicators for fault handlers in microcontrollers. Because while in endless while loop, the device cannot cause any harm by just doing weird things with its peripherals. It's just executing the same instruction over and over again. Therefore, we will cause a crash of this microcontroller. That's why denial of service. But unfortunately, it would be through an anti-climatic overwrite of a stack canary. 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. So to do that, I decided to torture myself further and show you the access in the debugger. Where I can set breakpoints. First one would be at before mem copy function. The second one will be right after it. And I'll check if the CPU is halted. It is. I'll let it go. Now, the ECU wouldn't actually react to any kind of messages because it's sleeping state. So I have to press this button. This will turn on one of the LEDs that were so loved at the airport. And once it turns off, I know that the car is properly simulated and I can start working with the ECU. The CPU still shouldn't be halted. But this would change after I send the attacker message. And as you can see, it did. Now we're right before the mem copy function. And we can investigate the registers. And from the beginning of the demo, we can actually know that the register R3 is the one that holds the destination location. So I can use mem 32 function command to dump that part of the memory. I'm also terrible sorry to anyone who knows how to work with Jlink and these kind of commands. I'm not really good with that. But here, these eight bytes are the legitimate V7 buffer. This is what should be overwritten with the candata. This is stack canary, which is interestingly set to all zeros. And this is the value that should be protected by the stack canary. And now comes the part where I'm not really good with Jlink. Because this is how I step over break points. As I said, I'm sorry to anyone who knows how to do that. 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. This is the stack canary, which has been overwritten with the can ID. So we can cheer up a bit because we are controlling quite a lot of bytes that we're overflowing. But we can't really because if we change the can ID, this overflow would never happen. So again, we can't really change what we overflow with in this case. Then this is probably can DLC based on the value here. Then there are some null bytes for some flags inside the can structure. And this D8 is from the original pointer. We haven't really overwritten that. Now having access to the debugger is nice. And I'm not really good with that. But sometimes you don't have that. And let me just check that the CPU is not halted. It is not. And in this suitcase, there is a third layer underneath the J link layer and that contains an oscilloscope. One of the oscilloscope probes is connected here to the CAN bus. The other oscilloscope probe is hidden under this to sense the NFC field. So if I now press this simulation button, I can go into the oscilloscope view and start tracking. The red line is the NFC field. The blue line is the CAN bus. So when the ECU comes up, you can see NFC field is up. And if I press the attacker button, you'll first see that... let me stop that first. You'll see that the CAN messages have stopped being sent. That's the reset of the ECU. And about a half a second after that, the NFC field got killed as well. This is how we initially found the vulnerability through fuzzing and using this side channel detection. 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. And there is a communication line between the main MCU and the NFC chip. And that's why there is a bit of a half second delay before that shuts down. I'll try to zoom into this, but I have never found a way how to increase text here. So you'll probably not be able to read that. But the message before this drop happens, the last message is the one we've sent. So ID 600A. And the voltage drop is actually caused by the GPIO pins being reset into their default state. After that, if I zoom out a bit, there is a break in CAN messages being sent. This is because the ECU is restarting. And then it gets into the initial state where it again waits for it to be restarted. This is just one of the side channel ways. And when I was here, I've been actually given this invitation to a high roller cone. And quite interestingly, it actually works as an NFC field detector. 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. If placed properly, this will light up the LED here whenever it sees the NFC field. And then by just pressing this button, you'll see that the NFC field will stop. I'm not going to the high roller cone, by the way. I have other plans. But anyway, thanks. I like this. The other side channel possibilities are detecting power consumption. That, however, requires soldering, which we have done here. But you can also do it on the CAN bus. There are usually messages that appear periodically. So you can detect the time difference in between the periodic messages. And the easiest way is when you find an ECU that actually sends a specific message on a reset. That's the easiest way how to detect a reset. There is a fuzzer published for that as well. It's actually not published yet because I haven't clicked the button to publish it. But it will be right after this call. Now I'd like to discuss who's affected by this issue. It could be basically anyone who just blindly accepts the value of this DLC field in some memory copy operations. 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. We reported this to all of them. So as of now, this should have been fixed. But there are other frameworks, some of them commercial, we haven't really looked at. So those might be still vulnerable. We looked also at the default Linux kernel drivers, which are not vulnerable. So those will never ever pass you anything higher than 8 in the DLC field if you're working with it. And now is a good time to look at other CAN bus versions. So CAN FD is not affected by the nature of it because the data payload was increased to 64 bytes. 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. CAN XL is likely not affected. I would like to wait with any judgments here because I haven't seen any real implementation of it anywhere. But based on the specification, it should not be. Finally, I would like to have some general takeaways from this. 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. 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. 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. With that, I'd like to thank you for your attention and ask if you have any questions. I'll try to stand and adjust this microphone again. The high roller invitation, although I'm not sure if it's invitation, I can give it to someone if they want. I have enough things that the tech NFC feels at home. There's a microphone over there so that everyone can hear. Have you tried to increase the F value to read more memory and see what happens? I'm sorry, I haven't heard. Could you come just here? This microphone seems to work a bit better. You have a stage time. So the question was whether I tried to increase the F value in the DLC. I can't because there are just four bits. And if I set all four bits to one, it is up to F. I'd love to have more than F, but I can't. Actually, in CanXL, the DLC field is 11, which can fit up to 2047. But then the specification says you can have up to 2048 bytes of data. And I don't know how is that going to work. But I'll wait for the implementation and then I'll hack crap out of it. I guess that's all. So thank you and have a nice day.