Without further ado, I'm gonna let Sheila take, it is Sheila, yes. Man, it's been a rough morning. Uh, I'm gonna let Sheila take it away. Okay, I don't wanna break something. It's okay? Ah, amazing. Okay. Uh, hi everyone, thanks for being here despite being Sunday morning. I'm really happy to be here sharing this moment with all of you. And a little bit sleepy too. Well, uh, let me introduce myself briefly. My name is Sheila. I'm a security researcher because I love writing stuff. I come from Buenos Aires, Argentina, a city 10,000 kilometers far from here, near the end of the world. And also a developer in assembly for microcontrollers and microprocessors. C, C++, Python and Go. And as a speaker, this is my second time giving a talk at DEFCON. I shot the noob last year. And was also a speaker at Black Hat, Echo Party, Hacking the Box, and other security conferences. But let's go to the important thing. Uh, in the last month, there were many, many noobs about backdoors inside hardware boards. We all know that these kind of backdoors exist. And they could be inside smartphones, computers, routers, computers, computers, and routers, VMCs, and so on. So, backdoors, backdoors everywhere. Uh, and in the years having published many research about different ways of backtoring devices through their own hardware components. However, most of those research focuses on devices based on powerful microprocessors, like ARM, Intel, or AMD, instead of microcontrollers. So, backdoors are in fact, a kind of a microcontroller. So, let's see some technical differences between them. On one hand we have microprocessors, which are entirely CPU. All the components that a microprocessor needs to work, like memories, input and output uses, uh, are physically separated. They are also bigger than a microcontroller, and have greater processing capacity because of their size and separated components. Um, most microprocessors have a, uh, a built-in, uh, have a modified hardware memory organization and 32 of 64 bits of architecture. And in the other hand we have microcontrollers. They have inside a little CPU and all the necessary components to get it working. I mean inside a microcontroller we have the CPU, RAM, ROM, input and output buses as well as other peripherals. Um the fact that microcontrollers are putting it all together in a very tiny space makes them with a less processing capacity and slower than microprocessors. There are other technical differences too. Um micro microcontrollers have hardware memory organization and 16 bits of architecture. They also have a little stack. For example PIC 18 have a stack able to store up to 31 memory addresses and if this gets overflow the PIC will reset itself. That's some information there. So after seeing some technical differences between microprocessors and microcontrollers a question that could arise is why some would use a microcontroller instead of a powerful microprocessor? Well um usually microprocessors are implemented on multitasking devices that need to run an entirely operative system. On the other hand microcontrollers are used for doing a specific task. Usually the same work dealing with the same kind of inputs and outputs like automatizing a routine. It's like comparing a Raspberry Pi which has a ARM microprocessor to an Arduino which has an Atmel microcontroller. Both are useful devices but they are used for different purposes. Um microcontrollers have evolved a lot. Um microcontrollers are used for different purposes. Um microchips are a clear example. Too many years ago PIC 12 very simple pin out with basic input and output ports and a few peripherals. PIC 16 um they have more peripherals. It's more familiar for us maybe because someone has a lot programming microcontrollers using these devices. They have more peripherals including user communication and CCP modules. PIC 18 also called high performance microcontrollers are my favorite ones. They have a lot of peripherals supporting different communication protocols like USB, CAN, AS-ON as well as all common peripherals such as timers, CCP modules, digital converters among others. And finally we have the 32 bit microcontrollers. They are the most similar thing to a powerful microprocessor. Uh even they use a Cortex M TPU. Uh it's a built-in microcontroller, and on the back is a pop up but still being a microcontroller. So, after all, targeting microcontrollers is worth it for. Um, nowadays they are responsible for controlling a wide range of systems, like physical security systems, some car security, semaphores, elevators, sensors, components of industrial systems, uh, some home appliances, and even robots. This is an example, it's a car security that handles the fuel injection, and is fully controlled by a PKT microcontroller. So, there are interesting devices to target into. Um, all microcontrollers need to be programmed, otherwise they will do nothing. So, as I said, there is a little CPU inside them, which is able to execute every assembly instruction of a program loaded in the microcontroller's program. Memory. These are the steps for programming microcontrollers, and we can develop the firmware in assembly, or C. And after compiling and assembling it, we're gonna get the hex file, which is the firmware. To load such hex file into the microcontroller, we have to use the programmer's software and hardware, usually provided by the microcontroller's manufacturer. And, similar to the world of microprocessors, um, uh, microcontrollers, uh, uh, uh, uh, uh, microcontrollers, um, every microcontroller manufacturer, has their own assembly instruction set for the CPU of their devices. This is an example to turning on, uh, a LED in a peak microcontroller. I love programming them in assembly, but you can use, well, uh, assembly or C. For microchip devices, we can use the MPLABID, which is free, to develop and compile our firmware. By building the project, we're gonna get the hex file, which is the firmware, ready to be written in the microcontroller's program memory. Um, as I said, there is, it's necessary to use the programmer's software and, and hardware to, to load the firmware into the microcontroller. These are the microchip official tools. We can use the amp-lab ID or IP together with the picket free, uh, and load the firmware into the microcontroller. Uh, the interesting thing is that, as well as these tools can be used to read, sorry, to write the program memory of a microcontroller, they can be used to read the program memory. I mean, we can write a firmware, or we can dump the firmware. So, let's see how we can dump the program memories, more interesting for us. Uh, first of all, this is the memory organization, um, for microchip devices. We have the program memory, where is the firmware is located, and it's the memory that we will dump. But there are other two memories. The RAM memory, which contains the SFR, special function registers, and CPR, general proposed registers. And the ROM memory, where the program can store data that won't be lost after our reset. So, the ROM and program memory are non-volatile, while the RAM is, is RAM, is volatile. Uh, to make a memory dump, the first step is connecting the target microcontroller to the picket free. There are other tools for doing this process, but I prefer to use the official tool because it works very well, and it's cheap, it costs around 40 dollars. So, it's a very, very simple process, or very, very reliable. This is an example of connection. We have to match the pins of the target device, the target microcontroller, with the pins of the picket-free connector. For example, the VPP pin of the microcontroller must be connected to the VPP pin of the picket-free, and so on with the other pins. It's very easy. Then, we connected to picket-free to the, to our computer, and we can use the MPLAB ID to read the firmware. The first step is vendering create the an standalone project inside the MPLAB ID and specify what microcontroller has our target device. Fortunately it's very easy to get this information because uh the PIC model is printed on the microcontroller. And then we must set the programmer hardware, in this case the PIC3. And finally we can use this option in the MPLAB ID to read the firmware and dump it into a hex file. The MPLAB ID has a disassembler so we can uh load the hex file and go to target memory views, program memory and there we'll see the disassembly view where we can find all the assembly instructions of the firmware together with their respective opcodes to be executed by the CPU. And also the memory address is the address of the memory address memory address where each one is located. Let's compare. Um in one side we have the source code and in the other side the disassembly view. Um this program after the start has five assembly instructions and we can find them in a disassembly view. The only, it's very, it's almost equal, the only difference is the access word after some instructions. It means that uh data memory access is performed because ports D and trees D are special function registers located at the RAM. That's why the access word is present. Um by observing the, the opcodes we can match the assembly instruction in the, in the hex dump. They're gonna be better because of the little endian format like most microprocessors, microcontrollers use little endian to store bytes. So we can match the assembly instructions in the hex dump. So we find the same way with our memory. Okay, now that we learned how to damp a firmware, let's see how we can modify the hex file and reload the firmware with something injected. Um in order to alter the behavior of the target device. When injecting a payload into a binary process it's necessary to find a place where our payload get executed at least once. In this case we need the same. So, the necessity is to find a place where our payload is step is to find a place inside the firmware where we could inject a malicious code or payload. I will explain three different injection techniques. The first one is uh- is about the injecting at the entry point. I mean when the program starts. But where is the entry point? Well this is the standard structure of a program for microchip devices. The first four sections are self-planned and they are not important for us at this moment. So let's focus on the reset vector. This always present in every microchip device at the memory address 0000. And it's followed by a goto start which is a jump to the first assembly structure of the program. Basically the entry point. In the middle the interrupt vector is present at the address 8 or 18. But uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- uh- we'll go deeper on that later. Uh here we can observe a- a jump to the entry point in the source code as well as in the disassembly view. Um this little program does not use interruptions so the goto in the reset vector is making a very short jump. But in large programs uh- like this other one, the jump is going to be quite longer. So remember the reset vector e- we be always at the address 0000. It's the first line in the disassembly view. There we're gonna find a a goto to the entry point. Um in the first case the entry point is at the address 06 and in the second case is at 7F84. Those are the memory addresses where we should inject our payload to get it executed when the program starts. So the next question is what payload we should inject or how can we build a payload for this kind of devices? Well we have to use the specific assembly instruction set for our target device. This is an example to turn it on two different LEDs in a Pico microcontroller. But we need to get the opcodes of these instructions. An option to get them is by writing all the assembly instructions of our payload in an assembly file inside an stash file. So we need to get the opcodes of these other projects in the MPLAB ID and then compile it so we can see the disassembly view and get the opcodes. Those are gonna be the opcodes of our payload. But remember the little Indian formats. So that's gonna be our final payload with the bytes inverter. So we are ready to make the injection. In this example the entry point is at the address 28. So we have to locate this address in the hexam. Um we can look for the base memory address first. 20 in this case. And then count until 8 bytes. That is where the entry point is located and where we should inject the opcodes of our payload. But there is something that we should keep in mind is the checksum. It's at the end of every line and if we modify something we'll have to recalculate it for every affected line. First- first we're gonna inject our payload at the entry point. Um the original bytes in that part will be shipped to the right. But remember not to move the byte of the checksum because we're gonna recalculate it. This is the method we should do to recalculate the checksum. For example if we have this line we should make a sum of all the bytes that are in the hexam. So open up the bytes of the line and then make uh naught plusX1 and the last byte of the outcome gonna be our checksum. But there is always a lifesaver. Heh. And we can use this website by calculating the checksum practically. So remember for every affected line after payload injection we have to fix the checksum. If we don't we gonna get that at the moment as a mod block. Han So if we hit this right before the checkpoint the samples will be miserable over here. So if you're we can use the MPLAB ID or IP to load this modified firmware into the target device in the microcontroller's program memory of our target device. And what is the result? This is the target device with the original firmware and this is what happened after loading the modified firmware. Um, the first light is on because it's part of the original program but there are other two lights on because of our payload. So, the proof of concept worked. This is like pop a kite but hardware version. So, let's see a real case. Um, this is a dashboard for observing the behavior of a car's IQ. Um, the car's IQ is for uh, handle the fuel injection. So, we have four blue LED for the four petrol injectors and other four yellow LED or lights for the CNG or CNB injectors. In normal behavior, the IQ starts injecting petrol and then switches on CNG. In the firmware of this car, IQ TN2 point is at the 115.2a. I placed a little payload there to modify the right behavior and continue injecting petrol after switching. I mean, the IQ will be injecting both, petrol and CNG at the same time. This must not, must not be cool for the car. So, let's see a video. Uh, this is the car's IQ. Uh, we, we, uh, this is the car's IQ. Uh, we start seeing the, I will keep the sound because it's very, uh, good. So, we start seeing the normal behavior. The blue light turned on because the car started using petrol and then it switched to CNG. So the petrol injector stopped working and the car is using CNG. Now I'm loading the modified firmware into the, the microcontrollers of TECO. Uh, CNG, CNG, CNG. Uh, and I will repeat the process. The, the car starts using petrol, I speed up the car to reaching out the condition to automatically switch to CNG, but when it occur, the car's, is using both, the AQ is injecting both petrol and CNG at the same time. This is just an example, something bad for the car. Okay. Let's see how it works. So, let's see how it works. Let's talk about the second injection technique. Um, maybe we prefer to get our payload executed not when the program starts, but when a specific action occurs. It might be associated with an interruption. Um, in big programs, there will always be interruptions, because of the time the microcontroller comes on, to interruptions, to alert us. For example, digital converter transmits the audio, transmits the signal to the parallel of different communication protocols, as other . Execution flow occurs. Um, no matter what the microcontroller is doing, it will interrupt vector, okay, the address 8 for high priority interruptions, and 18 for low priority interruptions. Once there, a procedure known as is used to detect who triggers the interruption. It's what's the timer, the analog to digital converter, or who was. After detecting who was, the corresponding code routine is executed. The red fine instruction at the end program counter at the memory address immediately after the before the interruption occurs. There are some special function register that's aimed interruption handling. When a program is using interruptions, the bits G, I, E and P, I, E of the in code register will be set to one. In assemblies, looks like this. The bits, the bits S, F, instruction is used for set to one a bit of the register. So, when we done, we can look for this two instructions in the disassembly view, you in order to know if interruptions are enabled in our target device. For every peripheral that could trigger an interruption there are two bits inside a special register. The interruption enable bit and the interruption flag bit. As example we can quote the timer zero. It's interruption bits are located in the in-con register. When a program wants to use this timer the TNR0IE bit in the in-con register must be set to 1. When the timer triggers an interruption the TNR0IF will be automatically set to 1. While not, this flag will be 2 0. Uh due to in the latest microcontrollers there are too many peripherals the special registers pi 1, pi 2 and even pi 3 have interruption enable bit while pi 1, pi 2 and pi 3 have their respective interruption flags for different hardware peripherals. So as I said a procedure known as polling is used at the interrupt vector to detect who triggers an interruption. Uh this process is done using the VTFSC instruction for testing the value of the interruption flags. In this example we have four peripherals that could have trigger an interruption. The polling process will start testing the flag of one of them. If the flag is set to 1, the call below will be done jumping to a call routine that must be executed every time that this specific peripheral triggers an interruption. If not the polling will continue testing the other flags until finding the flag set to 1. This is how the polling process looks in the disassembly view. By introducing the TNR0IE bit by bit the educational process and the the polling we are able to know what peripherals are being used for our target device. Remember that the polling will be always located at the address 8 for high priority interruptions or 18 for low priority. If we do some in one of the interruption flags we observe that the bit file is being tested of the P1 register but what is the bit file? And in the data sheet of our target device we found that the bit file corresponds to the ERC interruption flag. This is used by communication peripherals when, when the microcontroller receives data from any communication protocol it will share an interruption that will set this specific flag to 1. In the polling process if this flag is to 1 the call will be executed jumping to a correct interruption. So, we can see that the call routine that must be executed in this case located at the address 48. Such call routine will do something with the data received by these peripherals. So, um, by inspecting the polling we not only know what peripherals are being used, but we can get different memory addresses where we could inject our payload. For example, if we want to do something when the microcontroller receives data from that communication protocol, we can inject that payload to the we should inject our payload at the ERC interruption routine. Um, in this case it's located at the address 48. Or if we want to do something when the timer 0 triggers an interruption, we should inject at the address 4E and so on. The idea is be able to modify the the original behavior of the target device when the microcontroller is using its different hardware peripherals. So let's see an example of factoring the user communication using this technique. The first step is locating where the ERC interruption routine begins. By inspecting the polling we got that in this case the memory address is 48. So we have to locate this memory address in the hex dump. So we can look for the base memory address 40 and then count until 8 bytes. And there is one example of this. So we have to locate this memory address and see where the ERC interruption routine begins and where we should inject our payload. But what payload? What- we could look up payload that makes our relaying of the received data to a transmission port that we are able to monitor starting. I mean the microcontroller will receive data from anywhere and then we trigger an interruption. At that moment our payload will be executed to get such information and relay it to us. This is the problem of the ERC payload. And first we cut the received data and we move it to the W register. Then the the transmission is enabled, the operation mode is set as a synchronous and the TX pin is set as an output. And finally we move the received data in W to the TX drive. Anything written in such register will be transmitted through the TX pin to a USB interface or wireless module. In my case I will use a um USB interface because it's easier to show you what happened, but it could be a wireless module. This instruction could vary a little depending on the target device. These are the opcodes of every assembly instruction and our final payload. So we are ready to make the injection. Um we have to inject the the payload of Factor when where they RC interruption routine begins. So in this case it's at the address 48. And we'll place the the payload in the same way that we did in previous examples. So let's see a demo. In the next video you will see a hoverboard receiving information from a smartphone. That's when the interruption occurs and the data is relayed to our computer. So let's see how it works. Okay um the Factor firmware has been loaded in the target device and we are listening to the USB interface. So we send a message to the hoverboard. That's when the interruption occurs. Um the information is relayed to our computer. So as I said I'm using a USB interface in this case, but it could be replaced by a wireless module for remote connection and it's gonna work in the same way. Nice. Okay um at the moment of injecting a payload at whatever place we are making a shifting of bytes that could affect the call and go to instruction of the original program. Because now they are jumping to memory addresses whose original bytes have been moved. In large program this is a real problem that we have to solve. For example in the graph we can see a call instruction jumping to the address 10 while after payload injection it should be jumping to the address 16. We have to fix this to avoid a flow corruption. So let's see how it works. These are the opcodes of Godot call and op instructions. In PK-10 microcontrollers the assembly instructions are 16 bits in length. So 8 bits are used for for the opcode and 8 bits for the memory address where it has to jump to. But 2 more bytes are reserved in case of needing jumping more than to to those 2s actually 255 positions. Uh this this we can see that the here we can see an example of Godot call. The first jump is to the opcode 6 and the second one to the opcode 467. Um in the disassembly view we gonna see the memory address. So we have to make uh we have to divide it by 2 to get the opcode and then be able to locate the jump in the hex down. To fix it we have to keep in mind the memory address where we have injected our payload and the payload length. And we have to fix only those jumps to memory addresses located after the one where we have injected. For example if we inject our payload at the address 48 and we have a call at 56. We have to recalculate the jump by making a sum of the original memory address plus the payload length. So in log in log programs we'll probably need to fix this. So we have to fix some jumps. In this case I have to fix 3 call instructions. Um make the injection, fix the jumps, and the injection might be tedious especially in large programs. Um that's why I'm working on a little tool for automating this process. The tool received as parameter the hex down, the payload, the offset where it has to be injected, and the name of the output file. So basically the tool makes the injection and fix all the necessary things. I hope to be able to publish this tool on my github in these days. So stay tuned. Oops. So let's talk about the last second last sorry the last injection technique. And I will explain how we can alter the micro controller's stack and take control of the program flow. In microchip devices, we have to use a set of four initial function registers to manipulate the stack. The first one contains the stack pointer and the the tussle, tush and tussle compose the top of the stack. In the graph we can see in an example, the stack pointer is pointing to the second entry of the stack. Uh which is value is 0.0.1.8.1.34. In practical implementation of these resistors, the second entry is the uh first we should increment the stack pointer and then write the task registers with the mem- with the memory address where we want to jump to. And finally we have to execute our return. This is how it looks in the in- in assembly. First we increment the stack pointer and then we write all the necessary values to the task registers. In this case to jump to the address 00 00672. And finally is the return. In the disassembly view we're gonna see something like this. When- when the return is executed the program will jump to the memory address 24 in this case. From this example we can get the opcode of these instructions. So regarding payload injection at this moment we have two alternatives. On one hand we could inject our payload anywhere inside the firmware and then write the task registers with the corresponding memory address. Or well, we can do a rockchain writing the task with memory addresses from part of the code that we want to execute. I mean create the payload with assembly instructions already written in the original program. This is an example of rock. Um at the left we have we see all the memory addresses of our ROP gadgets. That means the memory addresses from part of the code that we want to execute. And in- in the other side we see all the necessary opcodes to write the memory addresses in the stack. All- all those opcodes including the final read will compose our payload. Um microcontrollers use a LIFO stack too. So the first ROP gadget executed will be the one located at the address 28 in this case. It's the last one injected in the ROP chain. This is an example of ROP gadget. It starts at the address 40 and ends at 46 with a retl w. All gadgets must end at the time of retl w to continue executing the other gadgets in- in the right way. So let's see a demo start. Um in the next video you will see a live turning on for every ROP gadget being executed. It's a nice way that I found to yoke what happened. Okay, it's inverted but you cannot see it. So let's see how this works. After 8 gadgets being executed, of course they could be more or less but it's just an example. So finally let's talk about um memory protections. Um from a security point of view we cannot avoid that someone overwrites the whole program memory of our microcontroller. But we can protect it against memory dumps and with that avoid payload issues. So let's see how this works. Um at the beginning of a program, before they make code, it's necessary to set some configuration bits for microchip devices. Among them is the code protection bit. But by enabling only these ones, the memory dumps will work. So if you assemble your program with these specific code protections, um anyone else will be able to dump your program and disassemble it. So to preventing memory dumps, we have to use the boot protection, which is a CPB bit, and also the data protection is useful too. If we enable these two bits um program memory dumps won't work, and if someone tried to dump your firmware the hex file will contain only zeros. This is just an example of protection for microchip devices. Really I don't have to say that don't have, I didn't have time to, to get deep in, in protections but it's something that you can do. Uh, conclusions. Vectoring microcontroller is possible and there are a lot of interesting devices that are fully controlled by them. Um, I explained three different injection techniques. I wrote a white paper explaining each one of them in details. Um, and though I focus on microchip, I think that most concepts can be extended to other vendors. And finally, I want to thank Sol and Nico Weisman for their help while I was writing the white paper and also to the people of Dreamlab for all the support. And thanks to you.