start	end	text
0	600	All right, cool.
600	6080	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.
6600	6980	All right.
6980	9680	So we're going to run through bare metal reverse engineering.
10200	19420	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.
19480	23020	I will say, like, again, I'm going to run through this very quickly.
23140	27660	We will make these slides available somehow, probably just post them on Solacek.io.
29060	33680	But I'm also down in the biohacking village, so feel free to swing by the CTF at any point.
33840	35620	I'd be happy to talk about this in more detail.
35620	41980	And we also have an actual hands-on demo that you could do as part of the CTF for the biohacking village this year.
42840	43300	All right.
43300	45760	So me, really quickly, cofounder of Solacek.
45760	47500	My background is electroengineering.
47900	51800	So I was an embedded software developer for a number of years.
51840	54780	That's where I really learned how to do a lot of the firmware dev.
54960	58660	Firmware dev made me effective at firmware reverse engineering.
58660	60280	So that's where a lot of this is coming from.
60520	63440	And you can find me and follow me on Solideo Gloria.
64760	66460	There's my handle there.
67220	67460	All right.
67460	68660	Let's talk about bare metal.
68660	75420	So bare metal, if you're not familiar, is very different than other operating systems you might be used to.
75420	81480	I did a nice flow diagram so you can figure out where I think you stand if you're a bare metal developer.
82180	84180	That's obviously my unbiased opinion.
84340	88340	But obviously different than embedded Linux and Windows and all that kind of fun stuff.
88980	89460	All right.
89460	91560	So just a few things about bare metal.
91900	94620	There's really not the same level of privilege separation.
94620	102900	There is a wide spectrum of bare metal and RTOSs and different, you know, solutions that you can use.
102900	107260	But in general, most of them don't have the same level of privilege separation.
107720	109880	They're also running lightweight scheduling.
110080	113380	It's much different than the kernels that you'd see on Linux, for example.
113940	116740	And there's sometimes minimal abstraction.
116740	120120	We'll talk more about kind of the different layers in a second here.
120120	132300	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.
132300	136000	So knowing how to do that and deal with that is really the goal of this talk.
136000	140200	And the last thing as we'll see later as well, direct register access is common.
140200	141740	We'll learn more about that in a second.
142520	142860	All right.
142860	145920	First, I want to talk about firmware just a little bit.
146240	150640	Because I think knowing firmware is obviously essential to start reversing firmware.
151020	158020	So I guess just to define what firmware is, you know, it's the low level software runs on the hardware.
158020	161520	I think people have different call firmware different things every once in a while.
161520	166820	But I think anything that's interacting with hardware at a fundamental level I would consider firmware.
167960	168480	All right.
168480	169800	So why learn firmware?
169820	171660	I've already kind of alluded to it a little bit.
171660	174500	But it teaches you a lot about the hardware as we'll see.
175160	179000	Teaches you about memory structures and teaches you different coding patterns.
179000	182600	Those are all critical to understanding obviously how that works.
182600	185260	And then just some other fun side notes.
185440	190220	I do think that if you can do firmware, you're the best programmer.
193120	194820	That's how I've defined that.
194960	197160	It also pays fairly well.
197160	198960	It's actually kind of an untapped market.
199200	205220	So understanding how to do firmware reverse engineering for whatever reason is just not as common as like Intel x86, for example.
205300	207000	And it's just plain fun.
207000	208860	And I mean that with all my heart.
209640	210380	All right.
211240	215300	So again, going through some of the operating systems, I had the flow diagram earlier.
215300	218360	But bare metal, just think low level chips.
218740	227980	And kind of the next level, honestly from a reverse engineering perspective, you can really say that even RTOSs are kind of bare metal.
227980	229220	And I can explain that later.
229220	240360	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.
240660	243160	QNX, free RTOS, things like that.
243200	248860	And then there's also this whole world of embedded Linux, embedded Windows IoT, things like that as well.
248860	253480	Here we're going to really focus on kind of the RTOS and ARM 32 bit microcontrollers.
255040	255480	All right.
255480	256620	So this is important as well.
256620	262140	I wanted to find kind of the different layers of firmware that you often see.
262540	266800	The board support package, this is typically we'll look at some of this code.
267020	272880	But this is typically, you know, setting a register to enable like a GPIO pin or something like that.
272880	276520	It's the specific low level fundamental calls to the actual hardware.
277200	281260	On top of that, you'll often see hardware abstraction layers.
281320	288820	So, you know, if you're working on a particular part like an STM32, they have HAL abstraction layers which make it much easier.
288860	290840	And I've got some examples that we'll see on that.
291260	294220	And then an RTOS is kind of above that.
294220	296480	In some cases this can change.
296500	305260	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.
306420	306920	All right.
306920	309160	So let's dig into ARM assembly a little bit.
309160	313840	And I apologize, I'm going to go through this way faster than it probably should be gone through.
313920	317660	But again, feel free to stop me and I'll jump into this a little bit more.
317960	319860	So different CPU architectures, right?
319860	322200	I think everyone's probably familiar with most of these.
322500	327620	I think most of the reverse engineering I've seen has been really focused on Intel X86.
328800	332000	And then I haven't seen a ton of RISC-V and MIPS.
332000	333200	I'm sure it's out there.
333200	338540	But ARM is the area that I'm most familiar with and going to be the emphasis of this talk.
338600	342220	But some quick differences between Intel X86 and ARM.
342800	345060	One is a complex instruction set.
345060	347340	The other one is a reduced instruction set.
347720	349480	They've got dedicated registers .
349480	352720	They've got more simple decoding.
352720	359200	And then some other things that happen just because it's more stripped down on ARM that you'll see.
359200	367760	So understanding the intricacies of ARM assembly is really important when you start reverse engineering firmware on ARM microcontrollers.
368200	380040	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.
380680	381260	All right.
381260	384180	Another part of ARM is thumb.
385280	388660	It's I would say one of the most confusing things about ARM.
389380	393360	So thumb is well, I guess ARM is 32 bit all the time.
393360	395760	Thumb is kind of 32 bit, kind of 16 bit.
395760	399580	And then it also swaps back and forth from ARM and thumb on a lot of microprocessors.
400000	411860	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.
412200	416120	So understanding the intricacies of ARM and thumb are also extremely important.
417960	418840	All right.
418840	425140	So going through some of the registers, again, for ARM, we've got some general purpose registers, R0 through R12.
425200	427840	We'll see in some of these examples how those are actually utilized.
428260	432240	And then the specific registers that are important are R13, 14, and 15.
432240	435160	That would be the stack pointer, the link register, and the program counter.
435560	436620	Various data types.
436620	441840	You've got the words that are 32 bit, obviously ARM 32, and you've also got half words and bytes.
442600	452560	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.
452920	457580	But you can see, you know, you load word, load half word, load sign byte, for example.
457700	460600	Those are all contained within the actual instruction itself.
461920	462140	All right.
462140	465360	So diving into this, I know this probably looks scary.
465360	466980	It's really not that bad.
467880	470760	But there are a few different things that I want to talk about here.
471240	475120	Loading and storing, just this concept of loading.
475120	485520	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.
485520	490100	So that first example is loading the address from R0 into R2.
490240	492520	And then store basically does the opposite.
492520	496160	So it's taking R2 and storing it into the address of R1 there.
496160	499120	And then we'll talk about relative addressing later on.
499120	507820	But branching, the way the branching works here is that you can branch and you can modify the different registers and flags.
508160	511120	And then you can set it to branch to a specific label.
511120	522740	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.
523260	526480	But there are a few different branching functions.
526480	530960	These are all just basically if statements, switch cases, that's what they get compiled into.
532100	542540	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.
542840	548400	This is, you know, compiler optimization reasons behind why this is the case.
548520	553940	So you'll see, you know, the best example there is probably the third one where you're loading into R2.
554220	566920	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.
567560	569220	And the last one, stack frames.
569240	575520	So, you know, I won't get into this too much, but stack frames build and contain like a function, for example.
575900	578040	So, again, using the
581120	595260	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 .
595260	598000	So that's really important to see those distinctions.
599280	599720	All right.
599720	604260	I won't spend too much time on this, but there are a ton of really common instructions that you'll see.
604620	608540	Move, arithmetic, bit shifting, things like that.
609900	610440	All right.
610440	611800	So I'll spend a little bit of time on this.
611800	616340	Hopefully everyone can see this is like the most important part that seems to be blocked.
616600	628680	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.
628680	637680	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.
638260	639420	This is using Jeff.
639420	643520	I would highly recommend using Jeff, which is built on top of GDB.
643660	646880	It's wonderful for reverse engineering and understanding more about how to do this.
646880	656120	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.
656120	666280	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.
667080	667580	All right.
667580	670140	Now let's go back to firmware development for a second.
670140	677440	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?
677440	685400	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.
686320	693140	So in order to do firmware development, what we need to do is initialize the different peripherals, right?
693140	696480	This is often, you know, how you start interacting with hardware.
696480	703480	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.
703480	708660	So what we have to do is initialize the peripheral clock, and this is just on an STM32.
708660	710760	Some of these might have some nuanced differences.
711200	719000	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?
719000	722080	So it's just checking a pin value.
722140	725260	And it sounds super easy, but let's get through how to do that.
725260	731020	So the STM32... I wish I'd known about this podium, because I would have shifted all my slides the opposite direction.
731540	736240	The STM32 has different memory addressing for different peripherals.
736240	740260	So imagine just a giant block of memory that's only peripherals.
740260	750920	So in this case, I think it's like 40 million hex, and all the peripherals under that are in different offsets as well.
750920	753440	So you can kind of see... hopefully everyone can read that.
753600	756100	And this is just a data sheet that you'll get online.
756220	760460	You can see the boundary address, and you can see the offset of all these different peripheral registers.
760540	772140	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.
772340	775100	And you're going to set a specific value.
775280	781580	If anyone can see that, you're basically just going to set one specific bit in this register.
782420	789540	And then this specific bit, this GPIOC because we're setting PC2 in this case, that has to be set.
791780	792780	All right.
792780	796240	And then the next thing after you do that, you have to initialize the GPIO.
796380	799700	So to initialize the GPIO, again, it's got that peripheral offset.
799700	805360	We're going to set the specific mode, which is setting two bits in a... in the register.
805680	808560	And then pull up, pull down is the exact same way.
808920	816200	You'll just see, you know, how these have to be set in the register in order to enable that function.
816560	827520	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.
828060	837220	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.
837220	843400	So you can see some, you know, bit wise logic or equaling and then... and equaling with the complement, et cetera.
843400	847660	So that'll be... that'll become important later on when we get more into the reverse engineering.
849400	851340	So the input register, same thing.
851580	854660	You're just checking specific registers.
854700	857760	And this is, again, the hard way to do things.
858340	858580	All right.
858580	859800	So let's talk about the easy way.
859800	865140	That was hopefully... I emphasize how... how tedious that would be.
865140	866920	The easy way would be HAL drivers.
866920	869600	So in this case, we're going to set up a UART.
869840	873360	And you can see how much easier it is to do that when you actually use the HAL.
874640	875940	So it's just like this, right?
875940	879180	You just set up an instance and a structure that's prebuilt for you.
879180	880420	This is built by ST.
880480	885540	And you can just literally say in that structure what these specific values are.
885540	888060	And you can run through setting that.
888700	890560	And then using the HAL is very similar.
891140	901080	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?
901080	907520	So if you think about what HAL has to do, you have to abstract away a ton of functions.
907520	912560	You have to make sure... when you lose the discrete calls, you inherently...
915120	919720	when you lose the discrete calls, you inherently have more complicated code that you have to reverse engineer at the end of it.
919720	925580	So understanding what that looks like in reverse engineering really helps us get to the solution quicker.
926020	926420	All right.
926420	929240	So really quickly, let's talk through what's not going to work.
929420	935100	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.
935340	944400	File is effectively worthless on raw binary that you pull from a... from a firmware via like JTAG or SWD.
944600	946020	Binwalk is kind of helpful.
946020	947860	You can see if it's encrypted or not.
949400	952880	The AES key find, sometimes you'll find keys, but not really.
953320	954880	So let's go to Ghidra.
954880	958360	So to do Ghidra, I'm just going to run through these very quickly.
959160	960540	Just import a file.
960720	963240	In this case you set the little cortex Indian.
964360	966580	In this case we're also going to add a flash mirror block.
966580	968820	I'll explain that afterwards if anyone is interested.
969580	971600	I don't like to analyze immediately.
971600	975660	What I like to do is actually do the memory map configuration first.
976540	981520	So I'll set up the flash map, the mirror, the RAM, et cetera, all those things you can get from a data sheet.
982140	983520	And then I'll analyze the binary.
983520	986920	Ghidra has this ARM aggressive instruction finder, which is extremely helpful.
988200	991100	And then if anyone's used Ghidra before, it's pretty straightforward.
991340	994760	Well, straightforward if you are a reverse engineer, I guess.
995300	1000720	But you've got the disassembly, you've got the listing, and it'll try to recover the source code as much as possible.
1000760	1008100	You can rename functions, you can retype functions, and you can search for strings, and you can start to look for those string references.
1009200	1019360	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.
1020040	1029140	So you can just manage your scripts and you can add certain tool sets to Ghidra to help with the reverse engineering.
1029200	1034120	One of the ones I love to do with that is the... it's actually called SVD loader by Level Down Security.
1034300	1036840	Effectively, what this does is takes that entire data sheet.
1036840	1042400	So that data sheet is often represented in an XML file or a SIMSIS SVD file.
1042860	1051840	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.
1051880	1055040	And then what this tool does is put it into the memory map.
1055040	1058300	So after you run that tool, you can select an SVD file.
1058300	1061200	These can be found online for most microcontrollers.
1061200	1062720	And then you get this updated memory map.
1062720	1068520	So what that does now is we've got all those peripherals that were just general addresses with offsets.
1068660	1073020	Now those are actually peripherals that will be dereferenced in the code effectively.
1073020	1080780	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.
1082140	1083680	From there, you can actually type cast.
1083680	1093080	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.
1093580	1107500	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.
1108320	1114040	And if you do this enough, you can get to literally the exact same, exact same code.
1115720	1121540	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.
1122260	1122540	All right.
1122540	1124580	I also wrote another tool called Type Loader.
1124580	1128980	What Type Loader does is it parses the actual HAL files.
1129440	1134600	So like STM32 HAL, it'll parse that and it'll create all the data types for those HAL objects.
1134820	1137500	And then from there, you can actually start to dereference those types.
1137500	1148240	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.
1148240	1153080	I'm going to type cast it to that UART handle from HAL because it has that similar looking signature.
1153180	1156860	And now I've got the exact same source code on the HAL layer and on the BSP layer.
1157220	1159820	Last one is bsim.
1159900	1163160	This is, I think, by the NSA still is part of Ghidra.
1163260	1179580	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.
1179580	1182540	So basically, what you have to do is just enable bsim.
1182540	1184820	You can create an H2 bsim database.
1185100	1186700	And then this is the key part here.
1186700	1192720	You can build a binary dedicated and then reverse engineer that binary and then load that into the database.
1192720	1196280	So now, imagine if we built, we know it's an STM32.
1196280	1197280	We know it's using HAL.
1197280	1199420	We know it's using a free RTOS, for example.
1199460	1201760	I'm going to build an entire project that's just a sample.
1201760	1203820	I don't care what the code does.
1203820	1205560	It doesn't have to have like any application code.
1205560	1218900	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.
1219740	1224520	From that, you can add the reverse programs of the H2 bsim database.
1224720	1226020	And then you can start searching functions.
1226020	1231180	So when you search a function, you can set the thresholds for the similarity and confidence.
1231260	1233380	And then you can start to apply the signature data type.
1233380	1245420	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.
1245840	1249880	You can quickly determine that this is an RTOS call.
1250260	1252900	I probably don't care about reverse engineering the RTOS.
1252900	1253940	You might in some cases.
1253940	1262540	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.
1262540	1267180	And what this looks like, you can quickly see this OS thread new.
1267400	1272020	If I didn't know what that was, I might be reverse engineering that function for days potentially.
1272080	1275200	Quickly with bsim, I can just say, hey, this is starting a thread.
1275200	1275940	I don't care.
1275940	1283060	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.
1283060	1302560	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.
1302560	1305080	So I did it.
1305140	1306460	Any questions?
1307060	1308000	Thank you.
1313300	1315540	I've got like two minutes for questions.
1315660	1316240	One minute?
1316240	1317380	Or does everyone have to leave?
1318360	1318960	All right.
1318960	1320120	I guess everyone has to leave.
1320540	1324460	Y'all feel free to stop by the biohacking village and I'd be happy to talk to anybody if y'all have questions.
