WEBVTT

00:01.130 --> 00:02.690
Thank you for showing up, everybody.

00:02.690 --> 00:03.910
I appreciate this.

00:03.910 --> 00:11.090
I know that I'm on opposite of Pony Awards, so if you're here for Pony Awards, you're in the right place.

00:11.130 --> 00:15.990
So this is Reverse Engineering MicroPython Frozen Modules.

00:16.010 --> 00:17.910
My name is Wesley McGrew.

00:17.970 --> 00:21.210
I'm Senior Cyber Fellow with Martin Federal.

00:21.470 --> 00:28.670
And this talk is going to be a lot of fun for those of you who like to hack on badge life stuff.

00:28.670 --> 00:35.210
A lot of badge life badges use this Python interpreter.

00:35.850 --> 00:41.530
And we're able to pick out some things that perhaps you wouldn't have otherwise.

00:43.930 --> 00:50.230
So the agenda for the talk, we're going to be, we're going to introduce what MicroPython is.

00:50.230 --> 00:55.410
For those of you not familiar, I'm pretty sure most of you know about Python in general.

00:55.490 --> 01:00.470
We're going to look at MicroPython firmware on the Raspberry Pi Pico.

01:00.630 --> 01:08.810
So it's available for a lot of different microcontrollers, but Pico is probably the easiest for you to set up experimentation in your lab with.

01:09.090 --> 01:25.310
We're going to build a Hello World module for it, and we're going to compile it down into bytecode, and then we're going to freeze it into the MicroPython firmware itself so that it's harder for somebody to extract.

01:25.310 --> 01:28.550
We're going to talk about that freezing process and why it's there.

01:28.890 --> 01:35.170
We're going to have a hands-on example of reconstructing that module from a compiled firmware.

01:35.170 --> 01:44.110
We're going to do it with symbols and without symbols, so show you how to do it in an environment where somebody's trying to actively keep you from doing it.

01:44.430 --> 01:51.350
We're going to do some analysis of that extracted module and come to some conclusions about what this means for reverse engineering it.

01:52.570 --> 02:02.810
So MicroPython is an implementation of Python 3 for microcontrollers, and it's very interesting in that it's not a straight port of CPython.

02:02.810 --> 02:16.550
CPython is the Python you're familiar with on your desktop system where it's an interpreter running bytecode based off of the Python code that you write.

02:16.790 --> 02:22.070
And MicroPython completely just re-implements it from scratch.

02:23.190 --> 02:28.170
There's probably reasons for doing that instead of starting from CPython.

02:28.370 --> 02:34.790
Microcontrollers have limited resources and so things are optimized for that kind of environment.

02:35.330 --> 02:38.770
There's a subset of Python standard libraries in there.

02:38.770 --> 02:49.230
There's not libraries for some of the standard library stuff that doesn't make sense on a microcontroller, but there's also some additional MicroPython only libraries.

02:49.230 --> 02:57.950
They're just for working with microcontroller hardware, primarily like GPIO, like turn pin one on, pull it low, that sort of thing.

02:58.250 --> 03:08.390
There is a REPL, so a command line you're used to with Python where you can just start typing Python code and have it execute and sort of play around with the Python environment.

03:08.390 --> 03:15.070
You can do that with MicroPython if you have a serial terminal on your microcontroller like you do with a Pico.

03:16.070 --> 03:29.730
The use cases for it, you don't... I don't see it in anything that you can just like go to Lowe's and buy for home automation, but you do see it in scientific application, industrial applications.

03:29.730 --> 03:32.770
You see it in badges.

03:32.770 --> 03:37.910
And so like the last year's snacky badge at least used MicroPython.

03:37.910 --> 03:52.190
And specifically the frozen modules are used by a lot of folks to have code cooked into the firmware itself that's not in a module file on the file system.

03:52.410 --> 03:53.830
It's really nice.

03:53.830 --> 03:57.710
So what I'm presenting here is not a mistake made with MicroPython.

03:57.710 --> 03:59.690
It's just a reverse engineering talk.

03:59.690 --> 04:03.710
There's no vulnerability here as MicroPython is concerned.

04:03.710 --> 04:04.930
I like it.

04:04.930 --> 04:10.310
It's really easy to create a prototype of an embedded device with MicroPython.

04:11.190 --> 04:13.350
And so you have three choices for your code.

04:13.350 --> 04:20.270
You can have plain text that's on a small file system that's emulated in part of your flash memory.

04:20.470 --> 04:24.710
You can... and you can read and write to that file system through the REPL.

04:25.050 --> 04:32.010
You can have pre-compiled bytecode modules, and we'll talk about the structure of those.

04:32.010 --> 04:39.850
But then you can also take those pre-compiled modules and cook them into the... into the MicroPython firmware at build time.

04:39.850 --> 04:46.210
And that removes a lot of the metadata that makes it easy to find those modules and disassemble them, right?

04:46.390 --> 04:49.450
There's tools for working with the other portions of this.

04:49.450 --> 04:52.970
There's no tools for going back from a frozen module.

04:53.050 --> 05:04.830
And so in badge life, a lot of CTFs that use badges will hold secrets and keys and code that they don't want you to see as part of the CTF inside those frozen modules.

05:05.910 --> 05:08.050
The feature is there for efficiency.

05:08.050 --> 05:20.830
So the feature is there so that you're not wasting additional memory loading dot py or loading the dot mpy file into memory and doing things when it's already in memory because it's on the flash.

05:20.930 --> 05:23.150
So it saves memory.

05:23.150 --> 05:24.150
It's convenient.

05:24.150 --> 05:25.590
You don't have to do any additional loads.

05:25.590 --> 05:26.910
It's already there.

05:27.390 --> 05:35.950
But it's being abused, misused for obfuscating and hiding code and secrets inside of these binaries.

05:36.090 --> 05:41.470
And we've got some quotes here from random forums and stuff.

05:41.470 --> 05:43.170
You probably can't read them.

05:43.330 --> 05:44.290
They're small.

05:44.290 --> 05:45.830
I just grabbed a bunch of them and threw them up.

05:45.830 --> 05:55.250
But the gist of all these quotes is people are asking, well, if I've got a password for a system, how do I keep that away from my users?

05:55.250 --> 05:56.290
Or I've got a key.

05:56.290 --> 05:59.630
I've got a secret that I don't want them to read my code.

05:59.810 --> 06:03.270
Their concerns are making this obfuscated.

06:03.290 --> 06:07.690
And people were responding to them on these forums of like, you know, it's not perfect.

06:07.690 --> 06:09.270
And everybody acknowledges that this isn't perfect.

06:09.270 --> 06:18.090
And it's not perfect, but you can make it harder to get at that stuff by cooking it into a frozen module because nobody has the tools to disassemble it.

06:18.090 --> 06:20.530
And so let's make something to disassemble it.

06:22.050 --> 06:26.470
It's a layer of protection for some folks, but it's really just an obfuscation.

06:27.210 --> 06:31.610
The first thing that you have to do to get this thing working is to get a firmware image.

06:31.710 --> 06:33.590
So that's out of scope for this talk.

06:33.590 --> 06:37.450
And it's tricky depending on the module, depending on your microcontroller.

06:37.450 --> 06:39.810
You might not be able to get a flash image easy.

06:39.810 --> 06:42.990
That's a whole talk on, you know, glitching and things like that.

06:42.990 --> 06:54.130
But since we're using Pico as the, we can, we can just use the standard Pico tools for this demonstration.

06:54.310 --> 07:04.530
If you're following at home with a Pico, you use the Pico tool info and it'll tell you about the, about the firmware of a connected Pico.

07:04.530 --> 07:07.690
But it actually makes it a little too easy as you can see from the output here.

07:07.690 --> 07:11.010
From the output, you have a list of frozen modules here.

07:11.110 --> 07:13.230
And you're like, well, that's, that seems dumb.

07:13.230 --> 07:14.970
Seems like a really short talk.

07:15.210 --> 07:24.970
And what that's from is from Pico's, Pico has binary information, quote unquote, binary information in the firmware image.

07:24.970 --> 07:27.990
And it can be read with Pico tool.

07:27.990 --> 07:31.210
It can be encoded, it's encoded by MicroPython.

07:31.270 --> 07:41.010
Firmware can specify in MicroPython that it's going to include additional fields in that Pico tool or in that binary information.

07:41.250 --> 07:45.930
And MicroPython does this with simply a list of the frozen modules.

07:46.310 --> 07:48.750
And so, again, that seems real easy.

07:48.750 --> 07:57.430
You can look at how to parse that from a tool that comes with MicroPython that we're going to be using a lot here, NPY tool.

07:57.430 --> 08:02.230
This is used for freezing modules, for inspecting NPY files.

08:03.090 --> 08:09.050
There's some stuff in there for how it encodes that, that information.

08:09.690 --> 08:16.490
And so since this is too easy as a starting point, I mean, it's too easy because you can strip this out.

08:16.490 --> 08:20.430
It's not necessary for the operation of the firmware, right?

08:20.430 --> 08:29.310
So other platforms, other micro controls may not even have this sort of field, this sort of binary information in the firmware.

08:29.870 --> 08:40.490
It's not used by MicroPython itself, so we can evade this analysis by commenting out the code in the freezing tool that generates that list, right?

08:40.490 --> 08:42.450
And so to make it harder on ourselves, right?

08:42.450 --> 08:48.470
Because we imagine the next step after we start ripping this stuff out, people are going to start obfuscating this part.

08:48.950 --> 08:52.410
So we can rip this out and not have those frozen modules listed.

08:53.970 --> 08:59.690
If you can get to a serial console REPL, you can also do help modules and list them out.

08:59.690 --> 09:02.670
That is probably something that needs to be in there.

09:03.170 --> 09:07.930
I don't know if you could patch out an evasion from, or a countermeasure against that.

09:08.010 --> 09:14.830
But that's, but it's not always the case that you're going to be able to easily get the REPL on a microcontroller.

09:16.610 --> 09:28.130
So for our example, we're going to have the minimum viable Python module here, a hello world that we're going to demonstrate in plain text.

09:28.130 --> 09:32.650
We're going to demonstrate it as a compiled byte code file on the file system.

09:33.030 --> 09:39.990
And then finally as a frozen module compiled into MicroPython and look at what that looks like in the binary.

09:40.790 --> 09:42.290
Plain text is simple.

09:42.290 --> 09:48.030
You simply use the MP remote tool to upload it into the file system on the microcontroller.

09:48.030 --> 09:53.510
There's a small, very small file system that it stores those things in.

09:53.530 --> 09:57.570
You can list files on it and you import and go.

09:58.690 --> 10:13.870
You can use the MPY cross compiler to compile your hello world dot PY into an MPY into PY file and you wind up with the hex dump or you wind up with a file that has the following hex representation.

10:13.870 --> 10:21.990
And so you can kind of see in there, you see our strings, you can see some byte code in there and so you're like okay, this is a thing.

10:22.150 --> 10:34.110
Um, this immediate, doesn't immediately look to you to be very useful, but the MPY tool can work with this format very well and you can upload it into the file system and, and use it.

10:34.110 --> 10:40.190
And so if you find one on a file system, use MPY tool to uh, to tinker around with it.

10:41.250 --> 10:48.750
As a frozen module, you can take that MPY and rebuild the microcontroller or firmware from scratch.

10:48.750 --> 10:53.790
You can rebuild MicroPython from scratch including that MPY that you created.

10:53.790 --> 11:02.890
It brings it in, it kind of uh, it brings it in as a set of C structures and we'll talk about that in a little bit, but basically it's hard to go back.

11:02.890 --> 11:06.190
After you've frozen it into the firmware, it's harder to pull it back.

11:06.190 --> 11:08.510
It's not just a file on the file system anymore.

11:08.510 --> 11:11.830
But you can use it as a Python module just fine.

11:16.200 --> 11:19.180
So what can we learn from an MPY file?

11:19.180 --> 11:28.880
Because our goal eventually here is going to be to take that uh, frozen module, extract it from the firmware, recreate the MPY so that we can parse it.

11:28.880 --> 11:34.960
The MPY tool creates um, uh, uh, the frozen module from the MPY.

11:34.960 --> 11:43.560
Uh, and so the MPYtool.py code has a lot of uh, uh, algorithms and data structures in it for generating this.

11:43.560 --> 11:48.140
And so it does not generate the, binary that's going to be in the firmware directly.

11:48.140 --> 11:54.920
It generates a set of C code that's going to get statically linked into the MicroPython firmware.

11:55.320 --> 12:01.620
So um, and it's less coded and it's just like a whole lot of data structure, a whole lot of structs, right?

12:01.620 --> 12:11.740
And those structs are going to get compiled in and they're going to get arranged by the compiler into memory in various places that are not immediately obvious to you from just reading the code.

12:12.000 --> 12:17.560
Uh, the same tool can be used to dump info about an MPY file with a disassemble flag.

12:17.560 --> 12:19.760
So it does have a built in disassembler.

12:19.880 --> 12:25.560
Uh, this is the only disassembler you get with MicroPython and it's only pulling from MPY files.

12:25.560 --> 12:30.680
So if we get code out of the binary, we're going to need to disassemble it with this tool.

12:30.840 --> 12:38.100
And by the way, the bytecode for MicroPython is not the same bytecode as CPython.

12:38.440 --> 12:40.860
It's definitely not the same opcodes.

12:40.860 --> 12:49.680
It's many of the same, uh, it's many of the same opcodes with different, different values and uh, not all of the same opcodes, right?

12:49.680 --> 12:52.000
So it's a from scratch implementation.

12:53.820 --> 13:03.160
If we dump or disassemble an MPY file using the MPY tool, we get three main things out of it.

13:03.160 --> 13:09.840
We get a string table, a module, a string table, an object table, and a set of code.

13:10.740 --> 13:18.480
What you're looking at here is the string table and I'm going to talk to you about this, this, um, intern string format that they have here.

13:19.960 --> 13:22.900
For the code, there's two pieces of code.

13:22.900 --> 13:31.080
If you remember, we had a module that defined, we defined a module and then we defined a go function in it or a hello world function in it.

13:31.400 --> 13:36.400
And, um, so you're going to have two functions that have bytecode.

13:36.400 --> 13:44.620
The first one is going to be a constructor and the next one is going to be your go function that actually repeats hello world every, uh, five seconds.

13:44.620 --> 13:49.060
And you can see the, the bytecode in these and it's a stack-based language.

13:49.060 --> 14:01.260
Basically you're, you're, you're constantly loading things out of different data structures, putting them on the stack, calling other functions, pushing and popping and looping around and doing things like that.

14:01.260 --> 14:10.800
And so again, if you read up on how a Python bytecode works, it would make sense to you, but, uh, it's not exactly the same.

14:10.800 --> 14:17.000
So you can't use like the body of Python disassembly tools on this or debugging tools for this.

14:18.600 --> 14:29.300
There's a really nice colored output hex dump that you can get within PY tool that will sort of tell you what the different parts of that hex dump mean and what, what, what they are, what they have, what they're about.

14:29.300 --> 14:32.160
That'll help us when we're trying to recreate one of these.

14:33.840 --> 14:44.560
So if we can rebuild an MPY of a frozen module out of information we extract from firmware, we've got something we you can use to, to, to get some work done with it.

14:44.640 --> 14:49.780
Uh, it'll take some RE work, but it's not because it's not there in a contiguous binary blob.

14:49.780 --> 14:51.800
It's structures we've got to walk, right?

14:51.800 --> 14:54.560
We've got to find them in memory and walk them.

14:54.840 --> 14:57.960
Uh, we have to understand string interning.

14:57.960 --> 15:00.720
We need to look at the byte, byte code briefly.

15:00.720 --> 15:03.640
It's just not going to be a byte code class or anything.

15:03.880 --> 15:12.360
Um, but also another tool that we can use is we can build a version of MicroPython that's the Unix port, quote unquote Unix port.

15:12.360 --> 15:30.320
You can build it for, for Windows, Linux or whatever, but it's basically a desktop version of MicroPython, uh, that you can, that you can build and load MPY files into, run them, analyze them and do things to where you're not just restricted to working on the microcontroller itself when you're doing your reverse engineering.

15:31.300 --> 15:36.200
So first off, uh, how we're storing our string constants.

15:36.260 --> 15:39.060
We're doing what's called string interning.

15:39.060 --> 15:51.400
And so this is something that you do on very limited memory environments where you want to avoid having the same constant string in memory more than once, right?

15:51.400 --> 16:03.280
You don't want to have, uh, main or, or something like, or some other common string 50 different times in your memory if your memory is in on the order of megabytes or kilobytes or something like that, right?

16:03.480 --> 16:12.340
And so MicroPython, MicroPython calls these Q strings for unique, for unique strings.

16:12.900 --> 16:22.740
Um, and basically Q string values, the way you refer to them is, is into an index, into a linked list of pools of these strings, right?

16:22.740 --> 16:42.540
And so if you're wanting to refer to hello cruel world, or you're wanting to refer to, uh, the, the, uh, the open tag module closed tag, uh, string, you're never going to refer to it as a pointer or you'll get a pointer to it, but you're going to refer to it as,

16:42.540 --> 16:47.460
and pass it around as an index into these linked, linked pools.

16:48.160 --> 16:56.520
And so there's a link, there's a set of static ones that are in the makeQStringData.py that's used in the build process of MicroPython.

16:56.520 --> 17:01.240
Um, and those are constant across all your builds of MicroPython.

17:01.240 --> 17:09.780
And then past that in the next pool, it's going to be stuff that's, varies between each build depending on which frozen modules you include.

17:10.760 --> 17:22.360
So if you use NPY tool to create frozen code generated from your NPY file, uh, it's going to create a strings, a string data structure.

17:22.360 --> 17:36.800
And so first you've got an enum that has, uh, basically, you know, that's going to compile down to one, two, three, four, basically a indexing hello cruel world, go hello world.py, sleep time, all of those sorts of things.

17:36.900 --> 17:43.540
Then you're going to have hashes for them, like a simple one byte hash, and then a link for each of them.

17:43.540 --> 17:45.620
Those are going to be placed into pools.

17:45.620 --> 17:55.380
And so on the right hand side there, you see the frozen constant pool, uh, that, uh, has, uh, has those five strings represented in there.

17:55.480 --> 18:01.740
There's also going to be a link to the previous pool and also a previous pool size.

18:01.820 --> 18:06.960
So your QString indexes are global.

18:06.960 --> 18:08.260
They're globally unique.

18:08.260 --> 18:14.140
And so you have to look at that previous pool size to know where the indexes start for this pool.

18:14.140 --> 18:15.860
All the pools are linked together.

18:15.860 --> 18:21.540
The first constant pool starts at zero and it goes to however many constants there are.

18:21.540 --> 18:28.380
And then the frozen pool, if it's next, starts at that number plus one, plus two, plus three, plus four, plus five.

18:32.120 --> 18:38.480
So, um, and so this is another, this is the, the QString table data, uh, here.

18:38.480 --> 18:42.820
So now you've got those, uh, these defined constants for these.

18:42.820 --> 18:45.840
And so this is how you refer to the QStrings that you have.

18:45.840 --> 18:48.640
These are all your indexes defined as constants.

18:51.650 --> 18:56.210
So the question is, how do we find the compiled module in a compiled firmware?

18:56.210 --> 18:59.170
Now, if you're lucky, they built it with symbols, right?

18:59.170 --> 19:01.090
But we're not going to assume we're lucky here.

19:01.090 --> 19:03.570
We're going to assume they stripped out that binary information.

19:03.570 --> 19:06.630
We're going to assume that they stripped out the, uh, the symbols.

19:06.730 --> 19:30.870
And so for me, whatever, whenever I see somebody in compiled code that I'm reverse engineering, and they're using, uh, an open source module, like if in malware, if they're using OpenSSL or they're using, um, uh, something similar, I'll try to build a copy of that SSL library myself of almost as close of a version as I can get,

19:30.870 --> 19:40.590
and as close to the same options as I can figure for theirs, so that I can then use that as sort of a Rosetta Stone for their module that doesn't have symbols, right?

19:40.610 --> 19:43.950
Because there's data structures that I want to walk through there.

19:44.150 --> 20:01.970
And so the, one of the best things that you can do is build a debug build of MicroPython that has a frozen module in it, not necessarily, you don't have their frozen module, but it has a frozen module in it, and use that to get your bearings in the binary,

20:01.970 --> 20:02.670
right?

20:02.870 --> 20:06.410
Uh, have it load at the same address and, that sort of thing.

20:06.410 --> 20:14.410
And then you can have two copies of Ghidra up, up side by side, and use one to figure out where the data structures are in the other.

20:16.070 --> 20:20.010
And so in the debug build, it's called mpFrozenNames.

20:20.010 --> 20:24.090
The mpFrozenNames is a list of the frozen modules on the system.

20:26.130 --> 20:37.090
In your, um, in your, uh, uh, uh, debug build, you also have the frozen modules themselves, mpFrozenNpyContent.

20:37.090 --> 20:42.310
So you've got, um, there's a lot of sort of built-in frozen modules.

20:42.470 --> 20:50.730
Uh, you'll see, um, like if you have an Adafruit CircuitPython, uh, device, CircuitPython is based on MicroPython.

20:50.910 --> 20:58.790
So, uh, CircuitPython devices will have other frozen modules in them, and you may not necessarily have the source code to them, and they'll be in this list.

20:58.790 --> 21:13.550
Uh, uh, I think Lego makes a, uh, a device that has a microcontroller in it that has MicroPython or CircuitPython on it, and they also have some of their own closed source libraries that are, that are in this as well.

21:13.590 --> 21:15.490
And so this is easy mode, right ?

21:15.490 --> 21:18.210
So we found the list of names of modules.

21:18.210 --> 21:29.950
We found pointers to the modules themselves, but we don't have the names of frozen names and frozen MPY content in our stripped binary.

21:29.950 --> 21:37.890
So we have a stripped binary that we copied from a microcontroller, or we got from an over-the-air update or something like that.

21:37.910 --> 21:41.470
We've got to find them in a non-debug build, right?

21:41.650 --> 21:59.750
And so the way we can find them, if we, if we can ever get to the same things, then we can start using those C data structures from the C files that these things, that from the source code of MicroPython, the source code of other frozen modules, we can start using those to sort of walk it and figure out what's different.

21:59.770 --> 22:02.470
But we've got to find those data structures first.

22:03.150 --> 22:13.310
The easiest way to find these two data structures in a non-debug build is to, is to, uh, one of the easiest ways for this is to just eyeball it.

22:13.310 --> 22:26.930
You can search for .py and just find sort of the list of constants that have .py, especially if you know the other frozen modules and the other frozen modules are consistent with the standard MicroPython ones.

22:26.930 --> 22:33.770
You can compare that list and then whatever's additional on there are other additional frozen MicroPython modules.

22:34.150 --> 22:47.710
Another, a little bit more, uh, resilient way of finding that that doesn't depend on you just sort of eyeballing a binary, which I don't mind doing all day, but y'all might, uh, is to find it as a code reference.

22:47.710 --> 23:05.870
And so there's a function that uses that list called mpfindFrozenModule and chances are it's going to build in roughly the same location in your debug build as in your, uh, right here.

23:05.870 --> 23:14.470
So you have this move, load, uh, this move, load, store combination here using this, uh, address.

23:14.470 --> 23:18.090
That address is the address to the mpFrozenNames.

23:20.250 --> 23:26.890
To find the frozen module content, there's all, there, you can't really eyeball it because it's all binary content.

23:26.990 --> 23:32.010
Uh, but there's, uh, indirect reference in, in that same mod, in that same function.

23:32.010 --> 23:45.630
So if you find those frozen names, eyeballing them, you can do a cross reference in Ghidra to walk back to where mpfindFrozenModule is and say, okay, that matches up with the debug build.

23:45.630 --> 23:49.770
Now, later on in that function, there's a bit of code that looks like this.

23:49.770 --> 23:52.570
This is a reference to the frozen MPY content.

23:52.830 --> 23:57.110
And so now we have pointers to our mod, to the, to the base data structures of our modules.

23:57.110 --> 24:03.670
So from here, it should be fairly simple work, just sort of working through those structures and pulling it out.

24:03.670 --> 24:12.570
And what we want to do is we want to recreate the hex of that original MPY file so that the rest of the work can be done with MPY tool.

24:15.380 --> 24:20.040
The header for the MPY file is, is, uh, going to be the same.

24:20.040 --> 24:27.640
Usually you can just take that from any of them and it's not super important as long as you get something that it passes the checks and it goes.

24:27.640 --> 24:33.380
It starts with magic letter M, it goes with a major micropython version.

24:34.700 --> 24:44.080
You want to have, you can have, so there's a flag in there for, or, or, and you can have it per function, whether it's native or non-native bytecode.

24:44.080 --> 25:00.860
So you can have, you can have an MPY module where the actual code of a function is native, say ARM or MIPS or whatever, or, uh, uh, byte, like not bytecode, machine code, right?

25:01.100 --> 25:11.080
Now for my money, that would be a lot easier because I can just, if I get a pointer to that, like Gaedra will, will decom, disassemble and decompile that for me.

25:11.080 --> 25:12.860
And I'm, and I'm off to the races.

25:12.860 --> 25:14.740
I don't have to interpret bytecode.

25:14.760 --> 25:20.540
So we're going with what I think is the harder example is if it's actual Python bytecode there.

25:22.740 --> 25:35.340
And then, um, there's a data type that comes up frequently in this called a variably in, a variably encoded unsigned integer, a VUINT.

25:35.380 --> 25:43.240
And this main, I don't know, maybe this is more common in microcontrollers than, than I know, but, uh, but I had to take a close look at this.

25:43.240 --> 25:54.920
And basically this is a variable length integer that for each byte, the lower seven bits is, uh, is part of the integer.

25:54.920 --> 25:58.760
And if there are more bits, then the eighth bit is set.

25:58.760 --> 26:05.520
And so if the eighth bit is set, then, you know, okay, I got to read another seven bits from the next byte and so on and so forth.

26:05.520 --> 26:07.680
And it can be arbitrarily long, right?

26:07.680 --> 26:14.040
And, and have a represent an arbitrarily large number, however much makes sense on your microcontroller.

26:14.160 --> 26:19.540
And so this is a data type that, that MicroPython uses in a lot of its data structures.

26:19.600 --> 26:23.480
And it can be a little bit of a mind bender to, to work with.

26:26.200 --> 26:34.100
So in the hello world dot mpy, we have a number of the, the intern strings that we want to have in there.

26:34.200 --> 26:43.580
Uh, and we'll see in there, we only, we only store the actual string data for strings that aren't already in the constant pool.

26:43.580 --> 26:48.040
So if something's already built into MicroPython, we don't redefine that string.

26:48.040 --> 26:49.480
We just make a reference to it.

26:49.480 --> 26:59.260
And so when we're creating our mpy file, we have to be mindful of which, which strings are new, essentially, which strings are what pool they come from.

27:02.850 --> 27:12.050
So frozen module key strings get mixed together in, um, in a, in a, in a mpy key string frozen constant pool, right?

27:12.050 --> 27:22.730
And so that's one of a few different pools in there that are all in a linked list where across the linked list, there's unique indexes for each string, right?

27:22.970 --> 27:43.570
So, um, so to find, to find that pool, the easiest thing to do is to go through, if you can find the main function, which you'd have if you're new to reverse engineering, maybe the tough part, but if you can kind of trace through the very beginnings of execution of that binary,

27:43.570 --> 27:48.670
if you can find main, there's mpy init and then key string init.

27:48.670 --> 27:54.990
And in the first four instructions in there, there is a reference to the key string frozen constant pool.

27:55.670 --> 27:59.230
Uh , you can search for a string that you know is part of that module.

27:59.230 --> 28:07.450
Like if you've just run strings on the binary, you'll have seen some of it and you can just do cross, cross references back from that if you really want to.

28:09.490 --> 28:14.570
So, so if we're going to parse these pools, we got to look at the data type for it.

28:14.570 --> 28:20.510
So a pool has a previous pointer to the previous pool in this linked list of pools.

28:20.730 --> 28:24.310
Uh, you can move backwards in the pools, but not forwards.

28:24.310 --> 28:34.710
And so, uh, that's why that first, uh, those first instructions in that key string init refer to the frozen constant pool because that's going to be the last one probably.

28:35.170 --> 28:41.230
Uh, there's a total previous length that you use to figure out where the indexes start in this pool.

28:41.390 --> 28:50.150
Uh, there's a few other fields that don't make a whole lot of difference, but at the end you have a list of hash values for the strings.

28:50.250 --> 28:54.930
Uh, you have a list of lengths for the strings, and then you have a list of the data, right?

28:54.930 --> 29:09.090
And so the string data is just going to be every string catenated together solidly with nothing in between because that lengths field in the previous, uh, the previous pointer or that, that list will tell you the lengths of each of them.

29:09.090 --> 29:16.030
And so you have to parse both of those arrays together to figure out, uh, where, what each string is.

29:18.600 --> 29:26.560
So with, uh, the previous pool is, the previous pool to the frozen pool is the npq string constant pool.

29:26.560 --> 29:35.160
The total previous length, uh, in our example is 3c2, so that's where the indexes start for the frozen modules.

29:35.160 --> 29:50.040
So any q string index, uh, greater than, so any q string greater than 962 is going to be in our frozen pool, which means that when we recreate our mpy, we're going to have to include that data into our mpy.

29:50.120 --> 29:54.460
Uh, anything less than that, we can just include the number and not re-encode the string.

29:58.250 --> 30:09.430
And so there's our list of indexes, and so there's a constant string, q string table data, uh, for hello world that you can locate through there, and that's the indexes.

30:09.430 --> 30:19.390
And so that first one, uh, uh, that first one, since it's greater than 962, we're going to have to encode that string hello world.py into our mpy file.

30:19.390 --> 30:29.070
But 7, which is just like the open tag module close tag, is already in the micropython binary, so we just include 7 and we don't re-encode it.

30:30.090 --> 30:32.850
And so this is how it looks when we re-encode it.

30:32.950 --> 30:37.670
Uh, this is, so we've already written the header, which is those first handful of bytes.

30:37.670 --> 30:40.330
The highlighted portion is the string table.

30:40.510 --> 30:49.050
Uh, there's a vunt where basically it defines whether or not it's a static entry or one that's defined in this mpy.

30:49.050 --> 30:53.430
So are we providing an index or are we providing a new string?

30:54.870 --> 30:56.590
And then the string follows.

30:56.590 --> 31:04.970
And that's why you see in that dump, you see, uh, the string data for the new strings, but not the built-in strings.

31:09.990 --> 31:14.930
So the next thing that you have to do is to, uh, build the object table.

31:14.930 --> 31:24.470
Now this is a more complex data structure, and especially as you get further from hello world, this is going to get more and more complicated.

31:24.470 --> 31:32.950
The more they're going to, the more they're using, uh, Python objects, the, the, the harder this part's going to become.

31:33.050 --> 31:38.370
But you can, like, follow through and, uh, and recreate these as well.

31:38.370 --> 31:47.830
Copy, really what you can do is you can copy these almost straight out of memory and put them into your binary and it'll just work without you necessarily having to understand them at first.

31:47.890 --> 32:00.210
Then once you get this thing fully recreated, you can load it into that Unix MicroPython interpreter and, uh, and start probing those objects from the REPL, which is much easier.

32:04.410 --> 32:06.910
And then finally, the byte code.

32:06.930 --> 32:11.770
So, oh, um, there's a format that for this code that we have to generate.

32:11.770 --> 32:23.570
If you look in mpytool.py's code, there's a function called read raw code that sort of hints to you as to how we need to build this byte code so that it can read it.

32:23.630 --> 32:42.090
Um, so the pointer to the code is the third field of that module, and so you see the frozen module, hello world there, and you see the QString table, the object table, and then you see the pointer to the first element of code, and that's for the prototype function for this object,

32:42.090 --> 32:43.170
right?

32:43.270 --> 32:51.390
And so each, each function, each, sorry, each piece of byte code can have children.

32:51.390 --> 33:01.150
So the prototype for this module is going to have a child of the go function that's inside of this module.

33:01.470 --> 33:12.090
And so it's going to contiguous, or continuously rather, or parse these elements of code, see whether they have children or not, and recursively follow down.

33:12.090 --> 33:20.670
And once you've parsed everything that doesn't have children, then you can walk back up that recursion and you've parsed all the byte code out.

33:21.270 --> 33:34.850
And so on the left there, you see the byte code for, um, for the, for the constructor, and on the right you see the byte code for the go function, which prints hello world in a loop, basically.

33:35.270 --> 33:38.250
And so one is the child of the other, essentially.

33:43.710 --> 33:50.230
So reconstructing this, uh, there are VUints for the kind, the length, and whether or not the code has children.

33:50.230 --> 33:56.670
So when we're putting this into our MPY binary, it needs to know how to reconstruct this into memory in a useful way for the interpreter.

33:56.790 --> 34:00.010
And so it's using those VUints to, uh, to do this.

34:00.010 --> 34:03.830
And so there's a kind, whether it's byte code or native code.

34:03.830 --> 34:08.770
Again, these things can be compiled down to where it's native ARM code for a function.

34:09.010 --> 34:19.390
And like I said, if that's the case, if you see something encoded like that, just, just drop your cursor and Ghidra or Idapro on that, hit disassemble, and you're off to the races, right?

34:19.390 --> 34:22.910
You can, you can read what it's doing a little bit easier.

34:22.910 --> 34:28.450
But if, if it's not, it's just, if it's byte code, Ghidra doesn't have a decoder for this, unless you want to write one.

34:28.450 --> 34:32.690
I didn't want to, uh, uh, uh, Karo back there probably will.

34:32.950 --> 34:47.690
Oh , uh, uh, and, uh, but, uh, but if you don't want to write a module to, to, to interpret this byte code, uh, you're, you're going to be left to pulling it out and dropping it in this file so the MPY tool can do this for us.

34:48.110 --> 34:53.730
Uh, the third, the third bit, is there a child for this block or not, right?

34:53.730 --> 34:57.590
And so that makes it recursively go through and read more code.

34:58.990 --> 35:16.050
So the one big problem with this, and, uh, I wanted to write a tool that would just sort of walk through a binary and do this, uh, but it's actually tricky to do because the length of the code is not referenced anywhere, right?

35:16.050 --> 35:24.830
The length of the code is defined by the fact that the code never exists, never steps outside of the bounds of its memory locations.

35:25.150 --> 35:35.550
So there's no length, it just starts execution there, and it'll just keep going as long as there's code, and as long as it doesn't return, and as long as it's looping around, it'll just sort of loop in that space.

35:35.550 --> 35:42.110
So there's no defined length for it, which, I don't know, maybe you could figure out a problem there, maybe you could exploit that in some way.

35:42.250 --> 35:48.610
But there's no length, and so it's hard in an automated fashion without parsing the byte code to know the length of it.

35:48.610 --> 35:50.810
And so you may have to eyeball that part of it.

35:50.810 --> 35:54.750
Or parse the byte code yourself, kind of trial and error.

35:55.110 --> 35:59.470
Each block of code is filed recursively by its own children.

36:00.570 --> 36:15.330
And so here you see in the first screenshot, we're encoding those integers and then plopping in the byte code for the constructor for the module.

36:15.730 --> 36:27.710
And then next, it has a child of the go function, and we go and parse that out, we put the vunts for it in there, and then we put the byte code in for the go function.

36:28.410 --> 36:41.690
Once you think you've created, and at this point, like if you take these slides and you go back, you'll see that this is bit for bit identical to the original mpy file for the hello world.mpy.

36:42.670 --> 36:45.630
We can use my Unix port to examine this.

36:45.810 --> 36:58.670
So we can load up the Unix port of MicroPython, import that mpy file, start looking at the objects, start looking at the modules, do the dict function to see what functions are available in there.

36:58.670 --> 37:00.310
We can call them.

37:01.470 --> 37:08.910
There is a tracing tool in, actually this is not, the tracing tool is in the next slide.

37:09.010 --> 37:23.570
This is another example that you can just sort of eyeball on your own of a, of a, this, this code right here will make a little light show on all the GPIO pins of a, of a, of a Pico.

37:23.570 --> 37:29.130
Basically just starts strobing all the, the, the GPIO in a pattern.

37:29.130 --> 37:34.730
If you have a dev board that has LEDs on all the pins, it'll do a little neat thing for you.

37:34.730 --> 37:48.730
That's the code that, this is the, this is the code that does this, but since it's doing GPIO, it won't work on the Unix MicroPython because it's doing like the pin function and stuff like that.

37:48.730 --> 37:53.110
You don't have GPIO on the Unix version of Micro, of MicroPython.

37:53.170 --> 38:02.610
So what you need to do is like if, if the code you pull out uses a function that's not there, you're going to have to implement some sort of stand, stand in there.

38:02.610 --> 38:06.630
And so then you can run it in the Unix port as long as you have those functions available.

38:08.050 --> 38:14.770
You can build MicroPython with SysSetTrace where you get a trace of execution.

38:14.770 --> 38:16.590
It's not quite what you'd expect.

38:16.590 --> 38:18.130
It's not a very detailed trace.

38:18.130 --> 38:25.770
It basically tells you which portion of the bytecode is executing in a very, in a very non-granular fashion.

38:25.770 --> 38:36.610
It just basically tells you which function is executing when, but you have to build, you have to define this when you're building your MicroPython binary or your MicroPython Unix port.

38:38.790 --> 38:43.290
The, the bytecode itself, this is the Hello World bytecode.

38:43.290 --> 38:47.850
Basically you're loading a global object of print, the print function.

38:47.850 --> 38:51.030
You're loading the Hello Cruel World string.

38:51.030 --> 38:53.090
You're calling the function.

38:53.150 --> 38:55.850
You pop the string off the top of the stack.

38:55.850 --> 38:58.270
You load the global time module.

38:58.270 --> 39:00.330
You load the method sleep from it.

39:00.330 --> 39:05.410
You load a constant integer of five because you want to wait for five seconds.

39:05.410 --> 39:07.990
You sleep for five seconds by calling the method.

39:08.170 --> 39:12.170
You pop the top off of that and then you jump back 17 bytes.

39:12.430 --> 39:14.630
And then you loop around and you do it again.

39:14.630 --> 39:18.630
And so that's the very basics of the language here.

39:20.730 --> 39:32.630
If you want to learn this bytecode, if you really want to take the time to step through bytecode of this and not just sort of interactively do it, you'll want to read through the MPY tool opcode class.

39:33.030 --> 39:39.150
And also, and in class in terms of like the class definition, not like a class that you sit down and learn.

39:39.490 --> 39:47.370
There's a vm.c in MicroPython that has the vm interpreter for this bytecode in there that you can read as well.

39:47.490 --> 39:51.950
This is not compatible with CPython, but familiarity with that would help.

39:51.950 --> 39:56.870
If you read through some Python bytecode tutorials, you'll understand what this is going.

40:00.330 --> 40:04.390
Conclusions on this, I'd really rather be reversing compiled C than this.

40:04.590 --> 40:22.030
Using MicroPython and creating bytecode out of your functions and stuff is probably almost a better obfuscation of your code than compiling a C firmware for your microcontroller.

40:22.030 --> 40:25.750
It's just annoying to try to read through this yourself.

40:25.750 --> 40:34.470
If you're more used to reading ARM assembly, if you're more used to reading decompiled C, it's doable though.

40:34.810 --> 40:41.050
And if you're really wanting to mess around with a device that does this, it's doable.

40:42.490 --> 40:48.830
These frozen modules are a great way to include project-specific stuff.

40:48.870 --> 41:01.410
Really the last stage of your development and deployment of a MicroPython firmware should be to put as many of your modules into this form as possible because it's just going to save memory and execution time.

41:01.430 --> 41:05.390
This is not a way to hide or obfuscate code or secrets though.

41:06.050 --> 41:12.390
Don't use it for that, especially now that this talk is out and this room of people know how to do it, right?

41:12.550 --> 41:19.710
So don't use this to go screw around with other people's CTFs in ways that ruin the fun for other people.

41:19.730 --> 41:24.650
But if you're like me and you're fun, you make your own fun, right?

41:24.650 --> 41:26.790
You don't really follow the rules on the CTF.

41:26.790 --> 41:29.430
You rather just play around with it and not even score any points.

41:29.430 --> 41:31.370
This is fun for that.

41:32.830 --> 41:36.670
It's only as well protected as access to your firmware.

41:38.290 --> 41:46.230
And so if you can dump from Flash, if you can dump from the REPL, if you can grab over the air updates for things, you can get at this stuff.

41:46.230 --> 41:48.050
So it's not a layer of protection.

41:48.490 --> 41:57.010
And you could probably automate this extraction process, but it's a bit tricky to figure out the lengths of some of those data structures too.

41:57.830 --> 42:22.510
So one thing that you should do if you are interested in this sort of stuff, if you like the idea of learning how to use Ghidra to step through all these data structures, if you want to learn how to sort of navigate around code in Ghidra, there's a great book from No Starch Press called The Ghidra Book,

42:22.510 --> 42:27.750
appropriately named, by Karen Antz and Chris Eagle.

42:27.750 --> 42:30.350
Karen Antz is in our audience here right now.

42:30.590 --> 42:35.850
She's been giving me the eye to mention this the whole time.

42:36.130 --> 42:40.830
She's actually going to be at the No Starch booth this afternoon doing a book signing.

42:40.830 --> 42:43.130
I want to say 3.30?

42:43.250 --> 42:44.410
3.30.

42:44.410 --> 42:47.930
She's going to be at the booth over there at No Starch doing a book signing.

42:47.930 --> 42:50.230
Go buy that book and get her autograph.

42:50.770 --> 42:55.730
And she knows everything about MicroPython, frozen modules as well.

42:55.730 --> 42:58.290
Divert any questions about this, talk to her.

42:58.590 --> 42:59.970
So, no.

43:00.470 --> 43:02.210
Ask me the things.

43:02.210 --> 43:05.850
And so with that, I'm going to close this off.

43:06.190 --> 43:12.150
I appreciate y'all coming to this instead of Pawnee Awards, however poor of a choice that might have been.

43:12.930 --> 43:16.090
And I look forward to interacting with y'all.

43:16.310 --> 43:17.090
Thank you.

