00:00:00.033,00:00:05.939 >>Without further ado, I’m gonna let Sheila take- it is Sheila? Yes. Man it’s been a rough 00:00:05.939,00:00:10.944 morning. I’m gonna let Sheila take it away. [audience claps] >> Hi, it’s alright? Can you 00:00:19.486,00:00:24.491 hear? >>No! [from audience] >>Hi. Okay I don’t want to wreck something. It’s okay? Okay? Uh 00:00:31.365,00:00:36.737 hi everyone thanks for being here despite being Sunday morning umm I’m really happy to 00:00:36.737,00:00:41.742 be here sharing this moment with all of you- a little bit sleepy too. But uhh let me introduce 00:00:44.444,00:00:50.550 myself briefly my name is Sheila. I’m a offensive security researcher because I love 00:00:50.550,00:00:55.555 wrecking stuff. I come from Buenos Aires, Argentina a city 10,000 kilometers far from here, 00:00:58.458,00:01:03.430 near the end of the world. [laughs] And also a developer and assembly for 00:01:03.430,00:01:09.436 microcontrollers and microprocessors C, C plus plus, Python, and Go. And as a speaker 00:01:09.436,00:01:15.742 this is my second time giving a talk at DefCon. I shut the [inaudible] last year, and was 00:01:15.742,00:01:20.948 also a speaker at Black Hat, Ekoparty, Hacking the box and other security conferences. But 00:01:20.948,00:01:27.721 let’s go to the important thing. Uhh in the last month there were many many news about backdoors 00:01:27.721,00:01:32.726 inside hardware wars. We all know that this kind of backdoors exist and they could be inside 00:01:34.861,00:01:39.866 smartphones, computers, routers, BMT’s and so on. So backdoors. Backdoors everywhere! Uhh and 00:01:47.808,00:01:53.347 the year having published many research about different ways of factoring devices through their 00:01:53.347,00:01:58.352 own hardware components. However, most of those resource focuses on the devices based on 00:02:00.988,00:02:05.993 powerful microprocessors like RM, Intel or AMD instead of microcontrollers. So let’s see 00:02:08.395,00:02:13.400 some technical differences between them. On one hand we have microprocessors which are 00:02:15.435,00:02:21.975 entirely CPU. All the components that the microprocessor needs to work, like memories, input and 00:02:21.975,00:02:26.980 output uses um are physically separated. They are also bigger than a microcontroller and have 00:02:30.117,00:02:36.657 greater processing capacity because of their size and separated components. Umm most 00:02:36.657,00:02:41.662 microprocessor have a Modified-Harvard memory organization with 32 or 64 bits 00:02:43.897,00:02:48.368 of architecture. And in the other hand we have microcontrollers. They have 00:02:48.368,00:02:54.574 inside a little CPU and all the necessary components to get it working. I mean inside a 00:02:54.574,00:02:59.579 microcontroller we have the CPU, RAM, ROM, input and output uses, as well as other peripherals. 00:03:01.648,00:03:07.788 And the fact that microcontrollers are putting it all together in a very tiny 00:03:07.788,00:03:13.593 space makes them with a less processing capacity and are slower than microprocessors. 00:03:13.593,00:03:19.433 There are other technical differences too, um micro- microcontrollers have 00:03:19.433,00:03:23.670 harvard-memory organization and 16 bits of architecture. They also have a little stack. For 00:03:23.670,00:03:28.675 example, big a team, having a stack able to store up to 31 memory addresses, and it is kept 00:03:33.413,00:03:38.418 overflow, the big will reset itself. Just some information. So after seeing some technical 00:03:42.189,00:03:46.259 differences between microprocessors and microcontrollers, a question 00:03:46.259,00:03:51.865 that could arise is, what sound would use a microcontroller instead of a powerful 00:03:51.865,00:03:56.870 microprocessor? Well usually microprocessors are implemented on multi docking devices that 00:04:00.006,00:04:05.479 need to run an entirely operative system. On the other hand microcontrollers are used 00:04:05.479,00:04:11.284 for doing a specific task. Usually the same work dealing with the same kind of inputs and 00:04:11.284,00:04:17.557 output like automatizing our routine. It’s a lot like comparing our raspberry pie 00:04:17.557,00:04:22.562 which has a ARM microprocessor to an Arduino which has an atmel microcontroller. Both are useful 00:04:25.098,00:04:30.103 devices but they are used for different purposes. Uh microcontrollers have evolved a 00:04:32.372,00:04:37.377 lot, and microchip will be our example. Too many years ago, pic 12, very simple pin out with 00:04:41.348,00:04:46.353 basic input and output parts, and a few peripherals. Pic 16 umm they have more peripherals, 00:04:51.291,00:04:55.862 it’s more friendlier for us maybe, some of us have to learn to program in microcontrollers 00:04:55.862,00:05:00.800 using these devices. They have more peripherals including user communication and CCP modules. 00:05:03.136,00:05:09.576 Pic 18, also called high performance microcontrollers, are my favorite ones, they have 00:05:09.576,00:05:14.848 a lot of peripherals supporting different communication protocols like, USB, GAM, as 00:05:14.848,00:05:19.853 some. As well all common peripherals such as timers, CCB modules and [inaudible] 00:05:21.988,00:05:27.194 converters among others. And finally we have the 72 bit microcontrollers, they are the 00:05:27.194,00:05:32.199 most similar thing to a powerful microprocessor, uh even they use a cortex MTBU, but we still need 00:05:34.301,00:05:39.306 a microcontroller. So after all, targeting microcontrollers worth it for? Um, nowadays they are 00:05:43.710,00:05:48.782 responsible for controlling a widespread of systems like physical security system, somes 00:05:48.782,00:05:54.454 car’s ECU, semaphores, elevators, sensors, components of industrial systems uhh some 00:05:54.454,00:05:59.459 home appliances, and even robots. This an example, it’s a car’s ECU that handle the fuel 00:06:01.962,00:06:08.368 injection and is fully controlled by a Pic 18 microcontroller. So, very 00:06:08.368,00:06:13.373 interesting devices to target into. [speaker coughs] Eh, all microcontrollers need to be 00:06:16.743,00:06:23.116 programmed, otherwise they will do nothing. So as I said, there is a little CPU inside them, 00:06:23.116,00:06:28.288 which is able to ex- to execute every assembly structure of a program loaded in the 00:06:28.288,00:06:34.294 microcontroller’s program memory. These are the steps for programming microcontrollers: um 00:06:34.294,00:06:39.299 we can level up the firmware in assembly or C, after compiling in assembly we’re going to get 00:06:41.434,00:06:47.374 the hex file, which is the firmware to load such hex file into the microcontroller, we 00:06:47.374,00:06:52.445 have to use the programmer’s software and hardware, usually provided by microcontroller’s 00:06:52.445,00:06:57.450 manufacturer. And similar to the world of microprocessors, umm every microcontroller 00:06:59.786,00:07:04.724 manufacturer has their own assembly instruction set for the CPU of the devices. This an 00:07:07.294,00:07:12.299 example to turning on a a LED in a pic microcontroller. I love programming them in assembly, 00:07:14.501,00:07:19.506 but you can use both uh assembly or C. For microchip devices we can use the MPLAB IDE, which is 00:07:22.909,00:07:29.582 free to double up and compile firmware. By building the project, we are going to get the 00:07:29.582,00:07:33.820 hex file, which is the firmware ready to be written in the microcontroller’s firmware 00:07:33.820,00:07:38.825 memory. Um as I said, it is necessary to use the programmers software and hardware to to load 00:07:44.331,00:07:49.336 the firmware into the microcontroller. These are the microchip official tools- we can 00:07:49.336,00:07:54.341 use the MPLAB ID or IT or together with the PIC free and together load the firmware into 00:07:57.711,00:08:03.583 the microcontroller. Uh the interesting is that, as well as these tools can be used to read- 00:08:03.583,00:08:08.154 sorry to write the program memory from microcontroller, they can be used to read the 00:08:08.154,00:08:13.159 program memory. I mean we can write the firmware or we can dump the firmware. So, let’s see 00:08:17.364,00:08:23.870 how we can dump the program memories- more interesting for us. Uh first of all, this is the 00:08:23.870,00:08:28.875 memory organization um for microchip devices. We have the program memory where is the 00:08:31.678,00:08:37.384 firmware is located and it’s the memory that we will dump, but there are other two memories: 00:08:37.384,00:08:43.923 the Ram memory, which contains the SFRs special function registers and GPR, general 00:08:43.923,00:08:49.996 purpose registers, and the ROM memory where the program can store data that won’t be lost 00:08:49.996,00:08:55.001 after I reset. So, the ROM and program memory are non-volatile, while the RAM is, is RAM is 00:09:00.306,00:09:05.845 volatile. Uh to make a memory dump, the first step is connecting the target 00:09:05.845,00:09:12.118 microcontroller to the Pic 3. There are other tools for doing this process, but I prefer to 00:09:12.118,00:09:18.291 use the official tool because it works very well, umm it’s cheap, it costs the RAM for 2 dollars. 00:09:18.291,00:09:23.296 So, this is an example of connection. We had to match the beams of the target device- the 00:09:25.832,00:09:30.837 target microcontroller, with the beams of the Pic 3 connector. For example, the VPP pin of the 00:09:33.273,00:09:39.479 microcontroller must be connected to the VPP pin of the pic 3 and so on with the other 00:09:39.479,00:09:44.484 pins. It’s, it’s very easy. Then, we connect the Pic 3 to our computer and then we can use 00:09:47.220,00:09:52.926 the MPLB IDE to write the firmware. The first step is creating a stand alone project 00:09:52.926,00:09:59.899 inside the MPLAB IDE, and specify what microcontroller has our target device. Fortunately 00:09:59.899,00:10:04.370 it is very easy to get this information because uh the pic model is printed on the 00:10:04.370,00:10:09.375 microcontroller. And then we must set the parameter hardware in this case the Pic 3, and 00:10:12.846,00:10:17.851 finally we can use this option in the MPLAB IDE to read the firmware and dump in into our 00:10:21.154,00:10:26.159 hex file. The MPLAB IDE has a dis-assembler, so we can uh load the hex file and go to the 00:10:31.030,00:10:36.035 target memory views, program memory, and there we will see the disassembly view, all the 00:10:38.571,00:10:44.143 assembly instruction of the firmware, together with their respective out codes to be 00:10:44.143,00:10:49.816 executed by the CPU. And also the memory address is the memory address where each one is 00:10:49.816,00:10:54.821 located. Let’s compare, and on one side we have the source code and in the other side the 00:10:57.123,00:11:02.061 disassembly view. Um this program after the start has file assembly instructions, and we 00:11:04.864,00:11:10.503 can find them in the disassembly view. The only- it’s, it’s very- it’s almost equal, the only 00:11:10.503,00:11:15.508 difference is the access word that is after some instructions. It means that uh data memory 00:11:17.577,00:11:22.582 access is performed because ports d and tres-D are special function registers located in 00:11:24.617,00:11:29.622 data ram- that’s why the access word is present. Umm the observing the out codes, we can 00:11:33.459,00:11:38.464 match the assembly instruction in the, in the hex dump. They’re gonna be better because of the 00:11:41.200,00:11:45.438 [inaudible] formats like most microprocessors, microcontrollers use [inaudible] 00:11:45.438,00:11:50.443 to be restore bytes in memory. Okay and now that we learned how to dump a firmware, let’s see 00:11:53.346,00:11:59.485 how we can modify the hex file and reload the firmware with something infective uh in order 00:11:59.485,00:12:05.458 to alter the original behavior of the target device. When instructing a payload into a 00:12:05.458,00:12:11.431 binary process, it’s necessary to find a place where our payload gets executed at least 00:12:11.431,00:12:17.804 once. In this case we need the same, so the next step is to find a place inside the firmware 00:12:17.804,00:12:22.809 where we can inject a malicious code or a payload. I will explain three different 00:12:25.078,00:12:30.083 instruction techniques: The first one is about injecting at the entry point. I mean when the 00:12:33.052,00:12:38.057 program starts. But where is the enter point? Well, this is the standard instructor of a program 00:12:41.494,00:12:48.001 for microchip devices. The first four sections are selif plane, and they are not important for 00:12:48.001,00:12:54.607 us at this moment, so let’s focus on the recent vector. This always present in every 00:12:54.607,00:12:59.612 microchip device of the memory address 0000 and is followed by a go to start, which is a jump 00:13:02.148,00:13:08.054 to the first assembly instruction of the program, basically the entry point. In 00:13:08.054,00:13:13.059 the middle the interrupt vector is present, at the address 8 or 18, but we’ll go deeper on that 00:13:15.294,00:13:20.299 later. And here we can observe a a jump through the entry point in the source code as well as 00:13:22.568,00:13:27.573 the disassembly view. Um this little program does not use interruptions, so the goto in 00:13:30.209,00:13:35.214 the receipt vector is making a very short jump, but in large programs, uh like this other 00:13:37.817,00:13:42.822 one, the jump gonna be quite longer. So remember there is a vector e- will be always at the 00:13:44.957,00:13:51.497 address 0000, it’s the first line in the disassembly view. There we gonna find a a go to to 00:13:51.497,00:13:58.037 the entry point. Ummm in the first case, the entry point is of the address 06, and in the 00:13:58.037,00:14:02.975 second case is at 7F84. Those are the memory addresses where we should inject our payload to 00:14:05.611,00:14:10.616 get it executed when the program starts. So the next question is, what payload we should inject or 00:14:13.252,00:14:19.826 how can we build a payload for these kind of devices? Well we have to use the specific 00:14:19.826,00:14:24.831 assembly instruction set for our target device. This an example to turning on two different LEDS 00:14:27.066,00:14:32.672 on a pic microcontroller. That’s- we need to get the out code of these instructions. And 00:14:32.672,00:14:38.311 I’ve tried to get them is by writing all the assembly instructions of our payload in 00:14:38.311,00:14:45.017 an assembly file inside and start up project in the MPLAB IDE and then compile it. So we 00:14:45.017,00:14:51.491 can see the disassembly view and get the out codes. Those gonna be the out codes of our payload, 00:14:51.491,00:14:56.496 but remember the little ending formats, so that’s gonna be our final payload. With the bytes in 00:15:01.667,00:15:07.673 barter. So we need to make an injection, in this example the entry point is at the address 00:15:07.673,00:15:12.678 28, so we have to locate this address in the hex sum, and we can look for the base memory 00:15:15.648,00:15:20.653 address first, 20 in this case, and then count until 8 bytes. This is where the entry point is 00:15:24.924,00:15:30.696 located and where we should inject the out codes as our payload. But there is something 00:15:30.696,00:15:37.170 we should keep in mind, is the check sum. It’s at the end of every line, and if we modify 00:15:37.170,00:15:43.976 something we must recalculate it for every affected line. But first we gonna inject our 00:15:43.976,00:15:48.981 payload at the entry point and the original byte in that part will be shift to the right, but 00:15:52.585,00:15:59.425 remember not to move the byte of check sum because we’re gonna recalculate it. This is the math 00:15:59.425,00:16:04.363 that we should do to recalculate the check sum- for example, if we have this line, we should 00:16:07.066,00:16:12.071 make a sum of all the byte of the line, and then make uh not plus 1, and the last byte of the 00:16:16.309,00:16:22.615 out code gonna be our check sum. But there is always a lifesaver [laughs] we can use this website 00:16:22.615,00:16:28.921 to calculate the check sum quickly. So remember for every affected line after payload 00:16:28.921,00:16:35.595 injection we have to fix the check sum- if we don’t we gonna get an error at the moment of 00:16:35.595,00:16:40.600 loading this modified firmware into the target device. Okay so um we already, we can use the 00:16:45.137,00:16:52.111 MBLAB IDE or IP to load this modified firmware into the target device- in the, in the 00:16:52.111,00:16:57.116 microcontrollers program memory of our target device. And what is our result? This is the 00:16:59.552,00:17:05.658 target device with the original firmware, and this is what happened after loading the 00:17:05.658,00:17:12.465 modified firmware. And the first light is on because it’s part of the original program, but there 00:17:12.465,00:17:17.470 other two lights on because of our payload. So the proof of concept worked, this is like pop 00:17:20.606,00:17:25.611 a calc but hardware version. So let’s see a real case umm this is a dashboard for observing the 00:17:27.880,00:17:32.885 behavior for a car’s ECU. Um the car’s ECU is um for handling the fuel injections, so we have four 00:17:37.189,00:17:42.194 blue led for the four petrol injectors, and other four yellow led or light for the CNG or CMB 00:17:46.198,00:17:51.203 injectors. In normal behaviors, the ECU starts injecting petrol and then switches on CNG. in the 00:17:54.874,00:17:59.879 fuel of this car ECU, the entry point is at the address 15, 2, 8. I’d place a little payload 00:18:02.848,00:18:08.955 there to modify the right behavior and continue injecting petrol after switching. I mean 00:18:08.955,00:18:15.127 the ECU will be injecting both petrol and CNG at the same time, does match not, not match the 00:18:15.127,00:18:20.132 code for the car. So let’s see a video. Uhh this is a car’s ECU uh we we start seeing the [loud 00:18:32.411,00:18:37.416 clicking]I will kill the sound, cause it’s very not good. So. We start seeing the normal 00:18:45.458,00:18:50.896 behavior, the blue lights are on because the car starts using petrol and then it switch to 00:18:50.896,00:18:57.303 CNG, so the petrol injectors stopped working and the car just using CNG. Now we are loading 00:18:57.303,00:19:02.241 the modified firmware into the, the microcontrollers of the ECU and it will repeat the process. 00:19:05.444,00:19:11.217 The car starts using petrol, I speed up the car to reach now the condition to automatically 00:19:11.217,00:19:16.222 switch to CNG. But when it took your- the car is, is using both, the ECU is injecting both petrol 00:19:19.759,00:19:24.764 and CNG at the same time. This is just an example, something bad for the car. Okay, let’s 00:19:31.871,00:19:36.876 talk about the second injection technique. Uh maybe we’d prefer to get our payload executed not 00:19:39.045,00:19:44.583 when the program start, but when a specific action occurs. It might be associated with an 00:19:44.583,00:19:49.588 interruption. Uh in big problems we always interruption because the microcontroller can’t 00:19:54.093,00:19:59.098 perform interruptions to alert us. For example converter transmit of different 00:20:05.037,00:20:10.042 communication protocols [speaker coughs] execution flow when matter what is doing it will 00:20:18.017,00:20:23.022 enter a vector located at the address A or high priority interruptions and 18 for low 00:20:29.695,00:20:34.700 priority interruptions. Once there, a procedure known as is used to detect who triggered the 00:20:37.436,00:20:43.275 interruption. It’s [inaudible] the analog to ditch the converter or who it was. After 00:20:43.275,00:20:49.582 detecting who was, the corresponding code routine is executed. The red file 00:20:49.582,00:20:54.587 instruction at the end program at the memory address immediately after they execute 00:20:57.189,00:21:03.562 before the interruption occurred. There are some special function registers that’s aimed 00:21:03.562,00:21:08.567 interruption handed. When a program is using interruptions, the bits GIE and PEIE after 00:21:11.370,00:21:16.375 intcon register, will be set to one. In assembly it looks like this: the B, the BSF instruction 00:21:19.678,00:21:24.683 is used for sets to one bit of our register. So when we dump a firmware we can look for these 00:21:29.121,00:21:35.427 two instructions in the disassembly view. In order to know with interrupts are enabled 00:21:35.427,00:21:40.432 in our target device. For every peripheral that could trigger an interruption, there are two bits 00:21:43.736,00:21:50.576 inside a special register. The interruption enabled bit, and the interruption flagged bit. As 00:21:50.576,00:21:55.581 example, we can quote the timer zero, these interruption bits are located in the intcon 00:21:55.581,00:22:00.519 register. When a product wants to use this timer, the TMR0IE which in the intcon register 00:22:04.590,00:22:09.595 must be set to one. When the timer triggers an interruption, the TMR0IF will be automatically 00:22:12.531,00:22:19.405 set to one. When not, these flags will be to zero. Uhh due to in the latest 00:22:19.405,00:22:24.710 microcontrollers, there are too many peripherals the special registers pie one, pie two, and 00:22:24.710,00:22:31.250 even pie three have interruption enabled bits. While pier one, pier two and pier three have 00:22:31.250,00:22:36.255 their respective interruption flags for different hardware peripherals. So as I said a 00:22:39.425,00:22:44.363 procedure known as polling is used to interrupt vector to detect who triggers an 00:22:44.363,00:22:51.303 interruption. Uhh this process is done using the BTFSC instruction for testing the 00:22:51.303,00:22:57.843 value of the interruption flags. In this example we have four peripherals that could help 00:22:57.843,00:23:02.348 trigger an interruption. The polling process will start testing the flag of one of them. 00:23:02.348,00:23:09.188 If the flag is set to one, the code below will be done. Jumping to a core routine that must be 00:23:09.188,00:23:15.894 executed every time there is a specific peripheral trigger sign interruption. If not, the 00:23:15.894,00:23:20.899 polling will continue testing other flags and find the flags set to one. This is how the 00:23:23.369,00:23:28.741 polling process looks in the disassembly view. By inspecting the polling we are able to know 00:23:28.741,00:23:35.381 what peripherals are being used for our target device. Remember that the polling will be always 00:23:35.381,00:23:40.386 located at the address 8 for high priority interruptions, or 18 for low priority. [coughs] If 00:23:43.055,00:23:48.060 we do some in one of the interruption flags, we’ll serve that the bit 5 is being tested 00:23:52.064,00:23:57.069 of the pier one register. But what is the bit 5? Umm in the, the machine that is our target 00:23:59.538,00:24:04.476 device, we found that the bit 5 corresponds to the error C interruption flag. This is used 00:24:06.912,00:24:12.051 by the communication peripherals, when, when the microcontroller receives data 00:24:12.051,00:24:17.690 from any communication protocol it will cheer an interruption that will set this specific flag 00:24:17.690,00:24:22.695 to one. In the polling process if this flag is to one, the call will be executed, jump into a 00:24:26.865,00:24:33.005 core routine that must be executed, in this case located at the address 48. Such core 00:24:33.005,00:24:38.010 routine will do something with the data received by this peripheral. So uh by inspecting 00:24:41.046,00:24:46.385 the polling, we not only know what peripherals are being used, but we can get different memory 00:24:46.385,00:24:51.390 addresses where we could inject our payload. For example, if we want to do something when the 00:24:53.425,00:24:58.464 microcontroller receives data from that communication protocol, we should inject our 00:24:58.464,00:25:03.402 payload, at the error’s interruption routine. Umm in this case it’s located at the 00:25:05.971,00:25:12.277 address 48. Or if we want to do something when the timer zero triggers an interruption, we 00:25:12.277,00:25:18.851 should inject at the address for e, and so on. The idea is being able to modify the, the original 00:25:18.851,00:25:24.890 behavior of the target device when the microcontroller is using it’s different hardware 00:25:24.890,00:25:30.396 peripherals. So let’s see an example of factoring the user communication using this 00:25:30.396,00:25:36.735 technique. The first step is locating where the error’s interruption routine begins. By 00:25:36.735,00:25:43.308 inspecting the polling we go to, in this case the memory address is 48. So we have to locate this 00:25:43.308,00:25:50.115 memory address in the hex dump, so we can look for the base memory address 14 and then count 00:25:50.115,00:25:55.788 until 8 byte. And there’s where the error’s interruption routine begins and where we should 00:25:55.788,00:26:00.726 inject our payload. But what payload? What- we cook a payload that makes or relying of the 00:26:03.662,00:26:09.868 received data to a transmission port that we are able to monitor strongly. I mean, the 00:26:09.868,00:26:16.408 microcontroller will receive data from anywhere, and if we trigger an interruption, at that 00:26:16.408,00:26:20.979 moment, our payload will be executed to catch such information and relay it to us. 00:26:20.979,00:26:25.984 This is the payload. And first we got the received data and we move it to the W register. Then 00:26:31.690,00:26:37.930 the the transmission is enabled, the operation mode is set as asynchronous and the TX pin is 00:26:37.930,00:26:44.903 set as an output. And finally we move the received data in W to the TXREG. Anything written in 00:26:44.903,00:26:50.876 such register will be transmitted through the TX pin to a USB interface or wireless 00:26:50.876,00:26:57.249 module. In my case I will use uh a um USB interface because it‘s easier to show you what 00:26:57.249,00:27:02.788 happened, but it could be a wireless module. This instruction vary a little 00:27:02.788,00:27:07.793 depending on the target device. These are the out codes of every assembly instruction and our 00:27:10.295,00:27:15.300 final payload. [coughs] So we are ready to make the injection. Um we have to inject the payload 00:27:19.571,00:27:25.377 factor when- where the error’s interruption routine begins, so in this case it’s at the address 00:27:25.377,00:27:30.382 48 and we’ll place the the payload in the same way that we did in previous examples. So 00:27:32.651,00:27:37.723 let’s see a demo. In the next video you will see hardware receiving information from a 00:27:37.723,00:27:42.861 smartphone, that’s when the interruption occurs and the data is relayed to our computer. 00:27:42.861,00:27:47.866 Mmmhmm Ok umm the vector firmware has to be loaded in the target device and we are 00:28:07.286,00:28:12.291 listening to the USB interface. So we send a message to the hardware bar. That’s when the 00:28:21.366,00:28:27.139 interruption occurs and the information is relayed to our computer. So as I said, I’m 00:28:27.139,00:28:32.678 using a usb interface in this case but it could be replaced by a wireless module for 00:28:32.678,00:28:37.683 multi-connection and it’s gonna work in the same way. Oh. Nice. Okay umm, at the moment of 00:28:43.989,00:28:48.994 injecting a payload at wherever place, we are making a shifting byte that could affect the call 00:28:53.966,00:28:59.671 and go to instruction of the original program because now they are jumping to memory 00:28:59.671,00:29:05.611 addresses who’s original bytes had been moved. In large program, this is a real problem 00:29:05.611,00:29:11.216 that we had to solve. For example on the left we can see call instruction jumping to the 00:29:11.216,00:29:16.221 address 10 while after payload injection it should be jumping to the address 16. We have to 00:29:18.290,00:29:24.863 fix this to avoid overflow corruption. These are the out codes, so GOTO code and NOP 00:29:24.863,00:29:31.269 instructions. In Pic 18 microcontrollers their assembly instructions are 16 bits in 00:29:31.269,00:29:38.176 length. So 8 bits are used for the out code and 8 bits for the memory address where you have to 00:29:38.176,00:29:45.083 jump to. But two more vital reserve in case of needing jumping more than two two 00:29:45.083,00:29:50.088 thousand- uhh sorry, two hundred fifty five position. Uh this this here we can see an example 00:29:52.324,00:29:57.329 with GOTO code the first jump is to the offset six and the second one to the offset 467. Um in the 00:30:02.100,00:30:08.540 disassembly view we’re gonna see the memory address, so we have to make uh, we have to divide it 00:30:08.540,00:30:13.545 by two, to get the offset, and then be able to locate the jump in the hex sum. To fix it, we 00:30:16.615,00:30:21.620 have to keep in mind the memory address where we have injected our payload, and the payload 00:30:21.620,00:30:28.393 then. Umm we have to fix only those jumps to memory addresses located after the one where we 00:30:28.393,00:30:35.267 have injected. For example, if we inject our payload at the address 48, and we have a call 00:30:35.267,00:30:40.272 at 56, we have to recalculate the jump, by making a sum of the original memory address plus the 00:30:45.243,00:30:50.248 payload land. So in log in log programs we’ll probably need to fix some jumps. In this case I 00:30:53.351,00:30:58.356 have to fix three call instructions. And make the injection fix the jumps and 00:31:01.259,00:31:06.264 check sum BT views, especially in large programs and that’s why I’m working on a little tool for 00:31:10.836,00:31:17.309 automating this process. The tool receives this parameter the hex sum, the payload the offset 00:31:17.309,00:31:22.814 where it has to be injected and the name of the output file. And basically the tool makes the 00:31:22.814,00:31:27.819 wanna fix all the necessary things. I hope to be able to publish this tool on my github 00:31:31.523,00:31:36.528 in this page. So it stayed too. Oops. So let’s talk about the last second- last, sorry, the 00:31:39.364,00:31:46.238 last injection technique. Uh I will explain how we can alter the microntroller’s stack, and 00:31:46.238,00:31:51.243 take control of the program flow. In microchip device there are 4 TL function registers to 00:31:53.979,00:31:58.984 manipulate this stack. The first one contains the stack pointer, and the TOSU, TOSH and TOSL 00:32:02.521,00:32:07.526 compose the top of the stack. In the graph we can see an example, the stack pointer is pointing to 00:32:09.528,00:32:14.533 the second entry of the stack. Uh which is why you is 0001834. Impractical implantation of 00:32:19.905,00:32:26.077 these registers, uh first we should increment this stack pointer and then write the TOSH 00:32:26.077,00:32:33.018 registers with the memory address where we want to jump to. And finally we have to 00:32:33.018,00:32:38.023 execute our return. This is how it looks in the in assembly. First we increment the stack 00:32:42.294,00:32:48.366 pointer, and then we write all the necessary values to the TOSS registers. In this case we jump 00:32:48.366,00:32:53.371 to the address 00-06rnt2, and finally the return. In the disassembly view we’re gonna see 00:32:57.042,00:33:03.615 something like this. When, when the return is executed, the program will jump to the memory 00:33:03.615,00:33:08.620 address 24 in this case. From this example we can get the out code of these instructions, so 00:33:11.623,00:33:18.363 regarding payload instruction at this moment we have 2 alternatives. And what have we 00:33:18.363,00:33:23.735 could inject our payload anywhere inside the firmware and then write the TOSH registers 00:33:23.735,00:33:28.740 with the corresponding memory address. Or well, we can do a ROP chain right in the TOSS with 00:33:31.042,00:33:36.047 memory addresses part of the call that we want to execute. I mean create a payload with 00:33:38.450,00:33:43.455 assembly instructions already written in the original program. [coughs] This is an example of 00:33:45.891,00:33:50.896 ROP. Um at the left we see all the memory addresses of our ROP gadgets, that means the memory 00:33:54.499,00:34:00.772 addresses from part of the call that we want to execute, and in, in the other side we see all the 00:34:00.772,00:34:05.777 necessary out codes to write the memory addresses in the stack. All, all those out codes 00:34:08.113,00:34:14.619 including the final RET, will compose our payload. Uh microcontrollers use a lethal 00:34:14.619,00:34:21.359 stack too, so the first ROP gadget executed will be the one located at the address 28 in 00:34:21.359,00:34:26.364 this case. It’s the last one injected in the ROP chain. This an example of ROP gadget. It 00:34:28.900,00:34:33.905 starts at the address 40 and ends at 46 with our RETLW. All gadgets must end, or RETLW to 00:34:38.009,00:34:43.014 continue executing the other gadgets in the right way. So let’s see a demo start, um in 00:34:46.251,00:34:52.924 the next video you will see a light turning on for every ROP gadget being executed. It’s a 00:34:52.924,00:34:57.929 nice way that I found to view what happened. Hmm hmm hmm Okay. it’s mounted but you can observe 00:35:11.176,00:35:16.181 8 gadgets being executed, of course there could be more or less, but it’s just an example. 00:35:29.928,00:35:34.933 So finally, let’s talk about uh memory protections and from a security point of view, we 00:35:37.602,00:35:42.707 cannot avoid if someone overwrite the whole program memory of our microcontroller. 00:35:42.707,00:35:47.712 But we can protect it against memory dumps and with that avoid payload injections. And at the 00:35:51.082,00:35:55.787 beginning of the whole program before the main code, it’s necessary to set some 00:35:55.787,00:36:00.725 configuration bits for microchip devices. Among them is the code protection bit, but by enabling 00:36:03.361,00:36:10.368 only these ones, the memory dumps will work, so if you assemble your program with this 00:36:10.368,00:36:15.373 specific protections, umm as anyone else will be able to dump your program and disassemble it. 00:36:19.477,00:36:24.482 So to preventing memory dumps, we have to use the boot protection, which is the CPB bit 00:36:28.186,00:36:33.191 and also the data protection is useful too. If we enable these two bits, umm program memory 00:36:35.560,00:36:40.565 dumps won’t work, and if someone tried to dump your firmware, the hex file while contain only 00:36:42.867,00:36:49.240 zeros. This is an example of protection for microchip devices. Really I don’t have- I 00:36:49.240,00:36:54.245 didn’t have time to, to get VPN in protections, but it’s something that you can do. Uh 00:36:57.382,00:37:03.054 conclusions. Vectoring microcontroller is possible, and there are a lot of interesting 00:37:03.054,00:37:08.793 devices that are fully controlled by them. Umm I explained three different 00:37:08.793,00:37:15.033 injection techniques I wrote a white paper explaining each one of them in details. Um and 00:37:15.033,00:37:20.038 though I focus on microchip I think that most concepts can be extended to other vendors. And 00:37:23.374,00:37:29.080 finally, I want to thank to Saul and Nico Wiseman for their help while I was writing the white 00:37:29.080,00:37:34.085 paper, and also to the people of Dream Lab for all the support. And thanks to you! [applause]