1 00:00:00,000 --> 00:00:00,600 All right, cool. 2 00:00:00,600 --> 00:00:06,080 So I've got a ton of slides to get through and everyone thinks it's not going to be possible, so we're going to jump straight into it. 3 00:00:06,600 --> 00:00:06,980 All right. 4 00:00:06,980 --> 00:00:09,680 So we're going to run through bare metal reverse engineering. 5 00:00:10,200 --> 00:00:19,420 We're going to talk through some of the arm assembly, bare metal reverse engineering, how to do some firmware dev, and then just some tools that I've used along the way. 6 00:00:19,480 --> 00:00:23,020 I will say, like, again, I'm going to run through this very quickly. 7 00:00:23,140 --> 00:00:27,660 We will make these slides available somehow, probably just post them on Solacek.io. 8 00:00:29,060 --> 00:00:33,680 But I'm also down in the biohacking village, so feel free to swing by the CTF at any point. 9 00:00:33,840 --> 00:00:35,620 I'd be happy to talk about this in more detail. 10 00:00:35,620 --> 00:00:41,980 And we also have an actual hands-on demo that you could do as part of the CTF for the biohacking village this year. 11 00:00:42,840 --> 00:00:43,300 All right. 12 00:00:43,300 --> 00:00:45,760 So me, really quickly, cofounder of Solacek. 13 00:00:45,760 --> 00:00:47,500 My background is electroengineering. 14 00:00:47,900 --> 00:00:51,800 So I was an embedded software developer for a number of years. 15 00:00:51,840 --> 00:00:54,780 That's where I really learned how to do a lot of the firmware dev. 16 00:00:54,960 --> 00:00:58,660 Firmware dev made me effective at firmware reverse engineering. 17 00:00:58,660 --> 00:01:00,280 So that's where a lot of this is coming from. 18 00:01:00,520 --> 00:01:03,440 And you can find me and follow me on Solideo Gloria. 19 00:01:04,760 --> 00:01:06,460 There's my handle there. 20 00:01:07,220 --> 00:01:07,460 All right. 21 00:01:07,460 --> 00:01:08,660 Let's talk about bare metal. 22 00:01:08,660 --> 00:01:15,420 So bare metal, if you're not familiar, is very different than other operating systems you might be used to. 23 00:01:15,420 --> 00:01:21,480 I did a nice flow diagram so you can figure out where I think you stand if you're a bare metal developer. 24 00:01:22,180 --> 00:01:24,180 That's obviously my unbiased opinion. 25 00:01:24,340 --> 00:01:28,340 But obviously different than embedded Linux and Windows and all that kind of fun stuff. 26 00:01:28,980 --> 00:01:29,460 All right. 27 00:01:29,460 --> 00:01:31,560 So just a few things about bare metal. 28 00:01:31,900 --> 00:01:34,620 There's really not the same level of privilege separation. 29 00:01:34,620 --> 00:01:42,900 There is a wide spectrum of bare metal and RTOSs and different, you know, solutions that you can use. 30 00:01:42,900 --> 00:01:47,260 But in general, most of them don't have the same level of privilege separation. 31 00:01:47,720 --> 00:01:49,880 They're also running lightweight scheduling. 32 00:01:50,080 --> 00:01:53,380 It's much different than the kernels that you'd see on Linux, for example. 33 00:01:53,940 --> 00:01:56,740 And there's sometimes minimal abstraction. 34 00:01:56,740 --> 00:02:00,120 We'll talk more about kind of the different layers in a second here. 35 00:02:00,120 --> 00:02:12,300 But the another example is that you typically when you exfill firmware from a board, you're exfilling the raw binary which in some cases is stripped and doesn't have any symbols or anything like that. 36 00:02:12,300 --> 00:02:16,000 So knowing how to do that and deal with that is really the goal of this talk. 37 00:02:16,000 --> 00:02:20,200 And the last thing as we'll see later as well, direct register access is common. 38 00:02:20,200 --> 00:02:21,740 We'll learn more about that in a second. 39 00:02:22,520 --> 00:02:22,860 All right. 40 00:02:22,860 --> 00:02:25,920 First, I want to talk about firmware just a little bit. 41 00:02:26,240 --> 00:02:30,640 Because I think knowing firmware is obviously essential to start reversing firmware. 42 00:02:31,020 --> 00:02:38,020 So I guess just to define what firmware is, you know, it's the low level software runs on the hardware. 43 00:02:38,020 --> 00:02:41,520 I think people have different call firmware different things every once in a while. 44 00:02:41,520 --> 00:02:46,820 But I think anything that's interacting with hardware at a fundamental level I would consider firmware. 45 00:02:47,960 --> 00:02:48,480 All right. 46 00:02:48,480 --> 00:02:49,800 So why learn firmware? 47 00:02:49,820 --> 00:02:51,660 I've already kind of alluded to it a little bit. 48 00:02:51,660 --> 00:02:54,500 But it teaches you a lot about the hardware as we'll see. 49 00:02:55,160 --> 00:02:59,000 Teaches you about memory structures and teaches you different coding patterns. 50 00:02:59,000 --> 00:03:02,600 Those are all critical to understanding obviously how that works. 51 00:03:02,600 --> 00:03:05,260 And then just some other fun side notes. 52 00:03:05,440 --> 00:03:10,220 I do think that if you can do firmware, you're the best programmer. 53 00:03:13,120 --> 00:03:14,820 That's how I've defined that. 54 00:03:14,960 --> 00:03:17,160 It also pays fairly well. 55 00:03:17,160 --> 00:03:18,960 It's actually kind of an untapped market. 56 00:03:19,200 --> 00:03:25,220 So understanding how to do firmware reverse engineering for whatever reason is just not as common as like Intel x86, for example. 57 00:03:25,300 --> 00:03:27,000 And it's just plain fun. 58 00:03:27,000 --> 00:03:28,860 And I mean that with all my heart. 59 00:03:29,640 --> 00:03:30,380 All right. 60 00:03:31,240 --> 00:03:35,300 So again, going through some of the operating systems, I had the flow diagram earlier. 61 00:03:35,300 --> 00:03:38,360 But bare metal, just think low level chips. 62 00:03:38,740 --> 00:03:47,980 And kind of the next level, honestly from a reverse engineering perspective, you can really say that even RTOSs are kind of bare metal. 63 00:03:47,980 --> 00:03:49,220 And I can explain that later. 64 00:03:49,220 --> 00:04:00,360 But tons of RTOSs that are out there that have different levels of scheduling and different complexity and some are much more lower level and closer to the hardware. 65 00:04:00,660 --> 00:04:03,160 QNX, free RTOS, things like that. 66 00:04:03,200 --> 00:04:08,860 And then there's also this whole world of embedded Linux, embedded Windows IoT, things like that as well. 67 00:04:08,860 --> 00:04:13,480 Here we're going to really focus on kind of the RTOS and ARM 32 bit microcontrollers. 68 00:04:15,040 --> 00:04:15,480 All right. 69 00:04:15,480 --> 00:04:16,620 So this is important as well. 70 00:04:16,620 --> 00:04:22,140 I wanted to find kind of the different layers of firmware that you often see. 71 00:04:22,540 --> 00:04:26,800 The board support package, this is typically we'll look at some of this code. 72 00:04:27,020 --> 00:04:32,880 But this is typically, you know, setting a register to enable like a GPIO pin or something like that. 73 00:04:32,880 --> 00:04:36,520 It's the specific low level fundamental calls to the actual hardware. 74 00:04:37,200 --> 00:04:41,260 On top of that, you'll often see hardware abstraction layers. 75 00:04:41,320 --> 00:04:48,820 So, you know, if you're working on a particular part like an STM32, they have HAL abstraction layers which make it much easier. 76 00:04:48,860 --> 00:04:50,840 And I've got some examples that we'll see on that. 77 00:04:51,260 --> 00:04:54,220 And then an RTOS is kind of above that. 78 00:04:54,220 --> 00:04:56,480 In some cases this can change. 79 00:04:56,500 --> 00:05:05,260 But an RTOS is really managing all the scheduling and you can set up threads and queues and semaphores and mailboxes and all that kind of fun stuff as well. 80 00:05:06,420 --> 00:05:06,920 All right. 81 00:05:06,920 --> 00:05:09,160 So let's dig into ARM assembly a little bit. 82 00:05:09,160 --> 00:05:13,840 And I apologize, I'm going to go through this way faster than it probably should be gone through. 83 00:05:13,920 --> 00:05:17,660 But again, feel free to stop me and I'll jump into this a little bit more. 84 00:05:17,960 --> 00:05:19,860 So different CPU architectures, right? 85 00:05:19,860 --> 00:05:22,200 I think everyone's probably familiar with most of these. 86 00:05:22,500 --> 00:05:27,620 I think most of the reverse engineering I've seen has been really focused on Intel X86. 87 00:05:28,800 --> 00:05:32,000 And then I haven't seen a ton of RISC-V and MIPS. 88 00:05:32,000 --> 00:05:33,200 I'm sure it's out there. 89 00:05:33,200 --> 00:05:38,540 But ARM is the area that I'm most familiar with and going to be the emphasis of this talk. 90 00:05:38,600 --> 00:05:42,220 But some quick differences between Intel X86 and ARM. 91 00:05:42,800 --> 00:05:45,060 One is a complex instruction set. 92 00:05:45,060 --> 00:05:47,340 The other one is a reduced instruction set. 93 00:05:47,720 --> 00:05:49,480 They've got dedicated registers . 94 00:05:49,480 --> 00:05:52,720 They've got more simple decoding. 95 00:05:52,720 --> 00:05:59,200 And then some other things that happen just because it's more stripped down on ARM that you'll see. 96 00:05:59,200 --> 00:06:07,760 So understanding the intricacies of ARM assembly is really important when you start reverse engineering firmware on ARM microcontrollers. 97 00:06:08,200 --> 00:06:20,040 So, you know, decompiling firmware, looking through, you know, what ARM turns into from a compiler and what crazy things compilers decide to do sometimes is something I would definitely encourage folks to do. 98 00:06:20,680 --> 00:06:21,260 All right. 99 00:06:21,260 --> 00:06:24,180 Another part of ARM is thumb. 100 00:06:25,280 --> 00:06:28,660 It's I would say one of the most confusing things about ARM. 101 00:06:29,380 --> 00:06:33,360 So thumb is well, I guess ARM is 32 bit all the time. 102 00:06:33,360 --> 00:06:35,760 Thumb is kind of 32 bit, kind of 16 bit. 103 00:06:35,760 --> 00:06:39,580 And then it also swaps back and forth from ARM and thumb on a lot of microprocessors. 104 00:06:40,000 --> 00:06:51,860 And it's very difficult to reverse engineer and understand because you've got different bite offsets and the last bit of the instruction is how it's switching. 105 00:06:52,200 --> 00:06:56,120 So understanding the intricacies of ARM and thumb are also extremely important. 106 00:06:57,960 --> 00:06:58,840 All right. 107 00:06:58,840 --> 00:07:05,140 So going through some of the registers, again, for ARM, we've got some general purpose registers, R0 through R12. 108 00:07:05,200 --> 00:07:07,840 We'll see in some of these examples how those are actually utilized. 109 00:07:08,260 --> 00:07:12,240 And then the specific registers that are important are R13, 14, and 15. 110 00:07:12,240 --> 00:07:15,160 That would be the stack pointer, the link register, and the program counter. 111 00:07:15,560 --> 00:07:16,620 Various data types. 112 00:07:16,620 --> 00:07:21,840 You've got the words that are 32 bit, obviously ARM 32, and you've also got half words and bytes. 113 00:07:22,600 --> 00:07:32,560 When you're actually looking at the disassembly, you can see when these are actually getting called in the actual instruction itself, which I'll get into more in a second. 114 00:07:32,920 --> 00:07:37,580 But you can see, you know, you load word, load half word, load sign byte, for example. 115 00:07:37,700 --> 00:07:40,600 Those are all contained within the actual instruction itself. 116 00:07:41,920 --> 00:07:42,140 All right. 117 00:07:42,140 --> 00:07:45,360 So diving into this, I know this probably looks scary. 118 00:07:45,360 --> 00:07:46,980 It's really not that bad. 119 00:07:47,880 --> 00:07:50,760 But there are a few different things that I want to talk about here. 120 00:07:51,240 --> 00:07:55,120 Loading and storing, just this concept of loading. 121 00:07:55,120 --> 00:08:05,520 So if you think, you know, in a load message, you can kind of see it here, or load instruction, you're actually taking the argument and pulling it into the ‑‑ sorry, the second argument and putting it into the first argument. 122 00:08:05,520 --> 00:08:10,100 So that first example is loading the address from R0 into R2. 123 00:08:10,240 --> 00:08:12,520 And then store basically does the opposite. 124 00:08:12,520 --> 00:08:16,160 So it's taking R2 and storing it into the address of R1 there. 125 00:08:16,160 --> 00:08:19,120 And then we'll talk about relative addressing later on. 126 00:08:19,120 --> 00:08:27,820 But branching, the way the branching works here is that you can branch and you can modify the different registers and flags. 127 00:08:28,160 --> 00:08:31,120 And then you can set it to branch to a specific label. 128 00:08:31,120 --> 00:08:42,740 And you can see there are a few different branch calls, branch equals, branch not equals, branch with link, branch with the BX there is actually switching to thumb, I believe. 129 00:08:43,260 --> 00:08:46,480 But there are a few different branching functions. 130 00:08:46,480 --> 00:08:50,960 These are all just basically if statements, switch cases, that's what they get compiled into. 131 00:08:52,100 --> 00:09:02,540 Relative addressing, this is super common, and we'll see this as well, where when you actually load something, sometimes it might not be calling a specific address. 132 00:09:02,840 --> 00:09:08,400 This is, you know, compiler optimization reasons behind why this is the case. 133 00:09:08,520 --> 00:09:13,940 So you'll see, you know, the best example there is probably the third one where you're loading into R2. 134 00:09:14,220 --> 00:09:26,920 The address of R0 at offset of four, which means that you're going and looking at a specific memory address and you're shifting down actually in the instruction itself instead of just calling the direct address. 135 00:09:27,560 --> 00:09:29,220 And the last one, stack frames. 136 00:09:29,240 --> 00:09:35,520 So, you know, I won't get into this too much, but stack frames build and contain like a function, for example. 137 00:09:35,900 --> 00:09:38,040 So, again, using the 138 00:09:41,120 --> 00:09:55,260 code that I showed earlier or using the unique registers that I saw earlier, you can see how it handles the link register and the stack pointer at the beginning, the prolog and the epilog of a function . 139 00:09:55,260 --> 00:09:58,000 So that's really important to see those distinctions. 140 00:09:59,280 --> 00:09:59,720 All right. 141 00:09:59,720 --> 00:10:04,260 I won't spend too much time on this, but there are a ton of really common instructions that you'll see. 142 00:10:04,620 --> 00:10:08,540 Move, arithmetic, bit shifting, things like that. 143 00:10:09,900 --> 00:10:10,440 All right. 144 00:10:10,440 --> 00:10:11,800 So I'll spend a little bit of time on this. 145 00:10:11,800 --> 00:10:16,340 Hopefully everyone can see this is like the most important part that seems to be blocked. 146 00:10:16,600 --> 00:10:28,680 But so if you imagine, you know, just a quick, you know, easy function that checks the value of A to B and does some returns, you can see how that actually builds out. 147 00:10:28,680 --> 00:10:37,680 So you can see the prolog, the epilog, you can see some of the loading and storing, you can see the relative addressing, you can see all the branching, again, moving to that check function. 148 00:10:38,260 --> 00:10:39,420 This is using Jeff. 149 00:10:39,420 --> 00:10:43,520 I would highly recommend using Jeff, which is built on top of GDB. 150 00:10:43,660 --> 00:10:46,880 It's wonderful for reverse engineering and understanding more about how to do this. 151 00:10:46,880 --> 00:10:56,120 And again, this basic function running on my computer and then stepping through every piece of it is a wonderful way to actually learn how ARM assembly works. 152 00:10:56,120 --> 00:11:06,280 And you can see these, you know, in addition to Jeff, you can see the stack register or the stack kind of changing, the registers changing, and you can really track through what the actual code is doing. 153 00:11:07,080 --> 00:11:07,580 All right. 154 00:11:07,580 --> 00:11:10,140 Now let's go back to firmware development for a second. 155 00:11:10,140 --> 00:11:17,440 I want to quickly run through how to do firmware development, the hard way and the easy way, and then we're going to start talking about how to disassemble everything, right? 156 00:11:17,440 --> 00:11:25,400 Because hopefully we're going to build out some fundamental knowledge of what's actually happening, and then you can see, you know, how we get back to the source code, which is our ultimate goal. 157 00:11:26,320 --> 00:11:33,140 So in order to do firmware development, what we need to do is initialize the different peripherals, right? 158 00:11:33,140 --> 00:11:36,480 This is often, you know, how you start interacting with hardware. 159 00:11:36,480 --> 00:11:43,480 In this case, I'm going to show what it takes to actually initialize a single GPIO the hard way, and then I'll explain it the easy way. 160 00:11:43,480 --> 00:11:48,660 So what we have to do is initialize the peripheral clock, and this is just on an STM32. 161 00:11:48,660 --> 00:11:50,760 Some of these might have some nuanced differences. 162 00:11:51,200 --> 00:11:59,000 You'll initialize the peripheral clock, and you'll set the GPIO as an input, and then in this case, I'm just setting the GPIO as a pull-up resistor, right? 163 00:11:59,000 --> 00:12:02,080 So it's just checking a pin value. 164 00:12:02,140 --> 00:12:05,260 And it sounds super easy, but let's get through how to do that. 165 00:12:05,260 --> 00:12:11,020 So the STM32... I wish I'd known about this podium, because I would have shifted all my slides the opposite direction. 166 00:12:11,540 --> 00:12:16,240 The STM32 has different memory addressing for different peripherals. 167 00:12:16,240 --> 00:12:20,260 So imagine just a giant block of memory that's only peripherals. 168 00:12:20,260 --> 00:12:30,920 So in this case, I think it's like 40 million hex, and all the peripherals under that are in different offsets as well. 169 00:12:30,920 --> 00:12:33,440 So you can kind of see... hopefully everyone can read that. 170 00:12:33,600 --> 00:12:36,100 And this is just a data sheet that you'll get online. 171 00:12:36,220 --> 00:12:40,460 You can see the boundary address, and you can see the offset of all these different peripheral registers. 172 00:12:40,540 --> 00:12:52,140 So in this case, to set the peripheral clock, we're going to set the RCC peripheral boundary address, which would be at the 40,000... or 40,023,800 or whatever. 173 00:12:52,340 --> 00:12:55,100 And you're going to set a specific value. 174 00:12:55,280 --> 00:13:01,580 If anyone can see that, you're basically just going to set one specific bit in this register. 175 00:13:02,420 --> 00:13:09,540 And then this specific bit, this GPIOC because we're setting PC2 in this case, that has to be set. 176 00:13:11,780 --> 00:13:12,780 All right. 177 00:13:12,780 --> 00:13:16,240 And then the next thing after you do that, you have to initialize the GPIO. 178 00:13:16,380 --> 00:13:19,700 So to initialize the GPIO, again, it's got that peripheral offset. 179 00:13:19,700 --> 00:13:25,360 We're going to set the specific mode, which is setting two bits in a... in the register. 180 00:13:25,680 --> 00:13:28,560 And then pull up, pull down is the exact same way. 181 00:13:28,920 --> 00:13:36,200 You'll just see, you know, how these have to be set in the register in order to enable that function. 182 00:13:36,560 --> 00:13:47,520 So one thing on that, you can easily just set it, that register equal to something, but you run into a ton of issues where if you set that register, you could potentially modify the configuration of the other peripherals. 183 00:13:48,060 --> 00:13:57,220 So this is just some of the logic that I use in general to... to make sure that... and force that... that function into the correct state. 184 00:13:57,220 --> 00:14:03,400 So you can see some, you know, bit wise logic or equaling and then... and equaling with the complement, et cetera. 185 00:14:03,400 --> 00:14:07,660 So that'll be... that'll become important later on when we get more into the reverse engineering. 186 00:14:09,400 --> 00:14:11,340 So the input register, same thing. 187 00:14:11,580 --> 00:14:14,660 You're just checking specific registers. 188 00:14:14,700 --> 00:14:17,760 And this is, again, the hard way to do things. 189 00:14:18,340 --> 00:14:18,580 All right. 190 00:14:18,580 --> 00:14:19,800 So let's talk about the easy way. 191 00:14:19,800 --> 00:14:25,140 That was hopefully... I emphasize how... how tedious that would be. 192 00:14:25,140 --> 00:14:26,920 The easy way would be HAL drivers. 193 00:14:26,920 --> 00:14:29,600 So in this case, we're going to set up a UART. 194 00:14:29,840 --> 00:14:33,360 And you can see how much easier it is to do that when you actually use the HAL. 195 00:14:34,640 --> 00:14:35,940 So it's just like this, right? 196 00:14:35,940 --> 00:14:39,180 You just set up an instance and a structure that's prebuilt for you. 197 00:14:39,180 --> 00:14:40,420 This is built by ST. 198 00:14:40,480 --> 00:14:45,540 And you can just literally say in that structure what these specific values are. 199 00:14:45,540 --> 00:14:48,060 And you can run through setting that. 200 00:14:48,700 --> 00:14:50,560 And then using the HAL is very similar. 201 00:14:51,140 --> 00:15:01,080 So now hopefully you understand the... with the HAL and with the BSP, it's very different in terms of how that ends up being reverse engineering, right? 202 00:15:01,080 --> 00:15:07,520 So if you think about what HAL has to do, you have to abstract away a ton of functions. 203 00:15:07,520 --> 00:15:12,560 You have to make sure... when you lose the discrete calls, you inherently... 204 00:15:15,120 --> 00:15:19,720 when you lose the discrete calls, you inherently have more complicated code that you have to reverse engineer at the end of it. 205 00:15:19,720 --> 00:15:25,580 So understanding what that looks like in reverse engineering really helps us get to the solution quicker. 206 00:15:26,020 --> 00:15:26,420 All right. 207 00:15:26,420 --> 00:15:29,240 So really quickly, let's talk through what's not going to work. 208 00:15:29,420 --> 00:15:35,100 Strings might give you some stuff, but it's really not going to give you everything and you don't know how those strings are being used. 209 00:15:35,340 --> 00:15:44,400 File is effectively worthless on raw binary that you pull from a... from a firmware via like JTAG or SWD. 210 00:15:44,600 --> 00:15:46,020 Binwalk is kind of helpful. 211 00:15:46,020 --> 00:15:47,860 You can see if it's encrypted or not. 212 00:15:49,400 --> 00:15:52,880 The AES key find, sometimes you'll find keys, but not really. 213 00:15:53,320 --> 00:15:54,880 So let's go to Ghidra. 214 00:15:54,880 --> 00:15:58,360 So to do Ghidra, I'm just going to run through these very quickly. 215 00:15:59,160 --> 00:16:00,540 Just import a file. 216 00:16:00,720 --> 00:16:03,240 In this case you set the little cortex Indian. 217 00:16:04,360 --> 00:16:06,580 In this case we're also going to add a flash mirror block. 218 00:16:06,580 --> 00:16:08,820 I'll explain that afterwards if anyone is interested. 219 00:16:09,580 --> 00:16:11,600 I don't like to analyze immediately. 220 00:16:11,600 --> 00:16:15,660 What I like to do is actually do the memory map configuration first. 221 00:16:16,540 --> 00:16:21,520 So I'll set up the flash map, the mirror, the RAM, et cetera, all those things you can get from a data sheet. 222 00:16:22,140 --> 00:16:23,520 And then I'll analyze the binary. 223 00:16:23,520 --> 00:16:26,920 Ghidra has this ARM aggressive instruction finder, which is extremely helpful. 224 00:16:28,200 --> 00:16:31,100 And then if anyone's used Ghidra before, it's pretty straightforward. 225 00:16:31,340 --> 00:16:34,760 Well, straightforward if you are a reverse engineer, I guess. 226 00:16:35,300 --> 00:16:40,720 But you've got the disassembly, you've got the listing, and it'll try to recover the source code as much as possible. 227 00:16:40,760 --> 00:16:48,100 You can rename functions, you can retype functions, and you can search for strings, and you can start to look for those string references. 228 00:16:49,200 --> 00:16:59,360 In addition to that, and why I like using Ghidra, I'm sure I'll argue with someone about IDA and Binary Ninja later, you can do your own scripts, which are really nice, and it's just Python-based. 229 00:17:00,040 --> 00:17:09,140 So you can just manage your scripts and you can add certain tool sets to Ghidra to help with the reverse engineering. 230 00:17:09,200 --> 00:17:14,120 One of the ones I love to do with that is the... it's actually called SVD loader by Level Down Security. 231 00:17:14,300 --> 00:17:16,840 Effectively, what this does is takes that entire data sheet. 232 00:17:16,840 --> 00:17:22,400 So that data sheet is often represented in an XML file or a SIMSIS SVD file. 233 00:17:22,860 --> 00:17:31,840 It's effectively just an XML that talks about all the register mapping that I was talking about before, which obviously makes it where you can automate that and parse that data. 234 00:17:31,880 --> 00:17:35,040 And then what this tool does is put it into the memory map. 235 00:17:35,040 --> 00:17:38,300 So after you run that tool, you can select an SVD file. 236 00:17:38,300 --> 00:17:41,200 These can be found online for most microcontrollers. 237 00:17:41,200 --> 00:17:42,720 And then you get this updated memory map. 238 00:17:42,720 --> 00:17:48,520 So what that does now is we've got all those peripherals that were just general addresses with offsets. 239 00:17:48,660 --> 00:17:53,020 Now those are actually peripherals that will be dereferenced in the code effectively. 240 00:17:53,020 --> 00:18:00,780 And you can actually look at where those functions are getting called and find the cross reference and then you can go back to that peripheral, which is extremely helpful when you're reverse engineering firmware. 241 00:18:02,140 --> 00:18:03,680 From there, you can actually type cast. 242 00:18:03,680 --> 00:18:13,080 So if you all remember, you know, what this looked like before, where it's, you know, nonsense, this, you know, pointer to a pointer to this cast three times. 243 00:18:13,580 --> 00:18:27,500 When you actually know what it is and you can kind of detect that this is RCC, if you cast it to the right object or type here, you'll actually see, you know, we're starting to get to exactly what the source code looks like, which is, it's pretty impressive. 244 00:18:28,320 --> 00:18:34,040 And if you do this enough, you can get to literally the exact same, exact same code. 245 00:18:35,720 --> 00:18:41,540 Again, I wrote this code, so it's pretty easy to reverse engineer your own code, but I think this can be applied across the board. 246 00:18:42,260 --> 00:18:42,540 All right. 247 00:18:42,540 --> 00:18:44,580 I also wrote another tool called Type Loader. 248 00:18:44,580 --> 00:18:48,980 What Type Loader does is it parses the actual HAL files. 249 00:18:49,440 --> 00:18:54,600 So like STM32 HAL, it'll parse that and it'll create all the data types for those HAL objects. 250 00:18:54,820 --> 00:18:57,500 And then from there, you can actually start to dereference those types. 251 00:18:57,500 --> 00:19:08,240 So now, instead of just doing it at the low level of the BSP where you're changing the actual peripheral calls, now you step one step up and you can say this code looks like it's actually that UART handle. 252 00:19:08,240 --> 00:19:13,080 I'm going to type cast it to that UART handle from HAL because it has that similar looking signature. 253 00:19:13,180 --> 00:19:16,860 And now I've got the exact same source code on the HAL layer and on the BSP layer. 254 00:19:17,220 --> 00:19:19,820 Last one is bsim. 255 00:19:19,900 --> 00:19:23,160 This is, I think, by the NSA still is part of Ghidra. 256 00:19:23,260 --> 00:19:39,580 What bsim does is it allows you to do binary similarity, I think is what it stands for maybe, where you can basically precompile a project, and then you can analyze that project against a known function data type. 257 00:19:39,580 --> 00:19:42,540 So basically, what you have to do is just enable bsim. 258 00:19:42,540 --> 00:19:44,820 You can create an H2 bsim database. 259 00:19:45,100 --> 00:19:46,700 And then this is the key part here. 260 00:19:46,700 --> 00:19:52,720 You can build a binary dedicated and then reverse engineer that binary and then load that into the database. 261 00:19:52,720 --> 00:19:56,280 So now, imagine if we built, we know it's an STM32. 262 00:19:56,280 --> 00:19:57,280 We know it's using HAL. 263 00:19:57,280 --> 00:19:59,420 We know it's using a free RTOS, for example. 264 00:19:59,460 --> 00:20:01,760 I'm going to build an entire project that's just a sample. 265 00:20:01,760 --> 00:20:03,820 I don't care what the code does. 266 00:20:03,820 --> 00:20:05,560 It doesn't have to have like any application code. 267 00:20:05,560 --> 00:20:18,900 But we built all of those function signatures out and we can then start to use that binary or that database to analyze our functions later on that are also using STM32 and HAL, presumably. 268 00:20:19,740 --> 00:20:24,520 From that, you can add the reverse programs of the H2 bsim database. 269 00:20:24,720 --> 00:20:26,020 And then you can start searching functions. 270 00:20:26,020 --> 00:20:31,180 So when you search a function, you can set the thresholds for the similarity and confidence. 271 00:20:31,260 --> 00:20:33,380 And then you can start to apply the signature data type. 272 00:20:33,380 --> 00:20:45,420 So if you see some random function that does all kinds of crazy stuff, you can see context switching and it's going in and out of these different modes and yielding like in an RTOS or like what that will do. 273 00:20:45,840 --> 00:20:49,880 You can quickly determine that this is an RTOS call. 274 00:20:50,260 --> 00:20:52,900 I probably don't care about reverse engineering the RTOS. 275 00:20:52,900 --> 00:20:53,940 You might in some cases. 276 00:20:53,940 --> 00:21:02,540 But for me, if you're reverse engineering and you're trying to get to the application on embedded firmware, this is a quick way to start weeding out stuff that doesn't really matter like an RTOS. 277 00:21:02,540 --> 00:21:07,180 And what this looks like, you can quickly see this OS thread new. 278 00:21:07,400 --> 00:21:12,020 If I didn't know what that was, I might be reverse engineering that function for days potentially. 279 00:21:12,080 --> 00:21:15,200 Quickly with bsim, I can just say, hey, this is starting a thread. 280 00:21:15,200 --> 00:21:15,940 I don't care. 281 00:21:15,940 --> 00:21:23,060 Or it can help me figure out from there, I want to look at where that actually function is getting called and then I'll start to look into the actual application. 282 00:21:23,060 --> 00:21:42,560 So hopefully, I've gone through extremely fast the method for kind of how firmware works a little bit and ARM assembly and then also some tools to really just work all the way up from the hardware low level BSP to the HAL to the RTOS so we can start reverse engineering the application which is really most important for us. 283 00:21:42,560 --> 00:21:45,080 So I did it. 284 00:21:45,140 --> 00:21:46,460 Any questions? 285 00:21:47,060 --> 00:21:48,000 Thank you. 286 00:21:53,300 --> 00:21:55,540 I've got like two minutes for questions. 287 00:21:55,660 --> 00:21:56,240 One minute? 288 00:21:56,240 --> 00:21:57,380 Or does everyone have to leave? 289 00:21:58,360 --> 00:21:58,960 All right. 290 00:21:58,960 --> 00:22:00,120 I guess everyone has to leave. 291 00:22:00,540 --> 00:22:04,460 Y'all feel free to stop by the biohacking village and I'd be happy to talk to anybody if y'all have questions.