[00:01.130 --> 00:06.870] Thank you for showing up, everybody. I appreciate this. I know that I'm on opposite of Pony Awards, [00:06.870 --> 00:13.210] so if you're here for Pony Awards, you're in the right place. So this is Reverse Engineering [00:13.970 --> 00:20.270] MicroPython Frozen Modules. My name is Wesley McGrew. I'm Senior Cyber Fellow with Martin [00:20.270 --> 00:27.690] Federal. And this talk is going to be a lot of fun for those of you who like to hack on [00:27.690 --> 00:36.810] badge life stuff. A lot of badge life badges use this Python interpreter. And we're able [00:36.810 --> 00:45.890] to pick out some things that perhaps you wouldn't have otherwise. So the agenda for the talk, [00:45.890 --> 00:50.890] we're going to be, we're going to introduce what MicroPython is. For those of you not [00:50.890 --> 00:56.310] familiar, I'm pretty sure most of you know about Python in general. We're going to look [00:56.310 --> 01:03.070] at MicroPython firmware on the Raspberry Pi Pico. So it's available for a lot of different [01:03.070 --> 01:08.210] microcontrollers, but Pico is probably the easiest for you to set up experimentation in your lab [01:08.210 --> 01:17.370] with. We're going to build a Hello World module for it, and we're going to compile it down into [01:17.370 --> 01:22.530] bytecode, and then we're going to freeze it into the MicroPython firmware itself so that it's [01:22.530 --> 01:27.650] harder for somebody to extract. We're going to talk about that freezing process and why it's [01:27.650 --> 01:34.190] there. We're going to have a hands-on example of reconstructing that module from a compiled [01:34.190 --> 01:40.610] firmware. We're going to do it with symbols and without symbols, so show you how to do it in an [01:40.610 --> 01:45.210] environment where somebody's trying to actively keep you from doing it. We're going to do some [01:45.210 --> 01:49.990] analysis of that extracted module and come to some conclusions about what this means for reverse [01:49.990 --> 01:58.470] engineering it. So MicroPython is an implementation of Python 3 for microcontrollers, and it's very [01:58.470 --> 02:05.170] interesting in that it's not a straight port of CPython. CPython is the Python you're familiar [02:05.170 --> 02:14.390] with on your desktop system where it's an interpreter running bytecode based off of the [02:14.390 --> 02:22.070] Python code that you write. 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:41.170] There's a subset of Python standard libraries in there. There's not libraries for some of the [02:41.170 --> 02:45.110] standard library stuff that doesn't make sense on a microcontroller, but there's also some [02:45.110 --> 02:52.010] additional MicroPython only libraries. They're just for working with microcontroller hardware, [02:52.010 --> 03:00.690] primarily like GPIO, like turn pin one on, pull it low, that sort of thing. There is a REPL, so a [03:00.690 --> 03:05.590] command line you're used to with Python where you can just start typing Python code and have it [03:05.590 --> 03:10.010] execute and sort of play around with the Python environment. You can do that with MicroPython [03:10.010 --> 03:15.070] if you have a serial terminal on your microcontroller like you do with a Pico. [03:16.070 --> 03:22.370] The use cases for it, you don't... I don't see it in anything that you can just like go to Lowe's [03:22.370 --> 03:29.730] and buy for home automation, but you do see it in scientific application, industrial applications. [03:29.730 --> 03:37.910] You see it in badges. And so like the last year's snacky badge at least used MicroPython. [03:37.910 --> 03:46.250] And specifically the frozen modules are used by a lot of folks to have code cooked into the [03:46.250 --> 03:54.450] firmware itself that's not in a module file on the file system. It's really nice. So what I'm [03:54.450 --> 03:59.250] presenting here is not a mistake made with MicroPython. It's just a reverse engineering [03:59.250 --> 04:06.250] talk. There's no vulnerability here as MicroPython is concerned. I like it. It's really easy to [04:06.250 --> 04:12.410] create a prototype of an embedded device with MicroPython. And so you have three choices for [04:12.410 --> 04:19.010] your code. You can have plain text that's on a small file system that's emulated in part of your [04:19.010 --> 04:24.710] flash memory. 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:37.710] But then you can also take those pre-compiled modules and cook them into the... into the [04:37.710 --> 04:42.330] MicroPython firmware at build time. And that removes a lot of the metadata that makes it [04:42.330 --> 04:48.430] easy to find those modules and disassemble them, right? There's tools for working with the other [04:48.430 --> 04:55.030] portions of this. There's no tools for going back from a frozen module. And so in badge life, [04:55.350 --> 05:01.070] a lot of CTFs that use badges will hold secrets and keys and code that they don't want you to [05:01.070 --> 05:08.050] see as part of the CTF inside those frozen modules. The feature is there for efficiency. [05:08.050 --> 05:13.590] So the feature is there so that you're not wasting additional memory loading dot py or [05:13.590 --> 05:19.550] loading the dot mpy file into memory and doing things when it's already in memory because it's [05:19.550 --> 05:25.690] on the flash. So it saves memory. It's convenient. You don't have to do any additional loads. It's [05:25.690 --> 05:34.610] already there. But it's being abused, misused for obfuscating and hiding code and secrets inside [05:34.610 --> 05:41.790] of these binaries. And we've got some quotes here from random forums and stuff. You probably [05:41.790 --> 05:46.310] can't read them. They're small. I just grabbed a bunch of them and threw them up. But the gist [05:46.310 --> 05:53.510] of all these quotes is people are asking, well, if I've got a password for a system, how do I [05:53.510 --> 05:58.270] keep that away from my users? Or I've got a key. I've got a secret that I don't want them to [05:58.270 --> 06:05.130] read my code. Their concerns are making this obfuscated. And people were responding to them [06:05.130 --> 06:08.810] on these forums of like, you know, it's not perfect. And everybody acknowledges that this [06:08.810 --> 06:13.390] isn't perfect. And it's not perfect, but you can make it harder to get at that stuff by cooking [06:13.390 --> 06:18.830] it into a frozen module because nobody has the tools to disassemble it. And so let's make [06:18.830 --> 06:25.210] something to disassemble it. It's a layer of protection for some folks, but it's really just [06:25.210 --> 06:30.610] an obfuscation. The first thing that you have to do to get this thing working is to get a [06:30.610 --> 06:36.310] firmware image. So that's out of scope for this talk. And it's tricky depending on the module, [06:36.310 --> 06:40.350] depending on your microcontroller. You might not be able to get a flash image easy. That's a [06:40.350 --> 06:48.570] whole talk on, you know, glitching and things like that. But since we're using Pico as the, [06:48.570 --> 06:55.090] we can, we can just use the standard Pico tools for this demonstration. If you're following at [06:55.090 --> 07:03.090] home with a Pico, you use the Pico tool info and it'll tell you about the, about the firmware of [07:03.170 --> 07:07.410] a connected Pico. But it actually makes it a little too easy as you can see from the output [07:07.410 --> 07:12.230] here. From the output, you have a list of frozen modules here. And you're like, well, that's, [07:12.230 --> 07:18.230] that seems dumb. Seems like a really short talk. And what that's from is from Pico's, Pico [07:18.570 --> 07:25.770] has binary information, quote unquote, binary information in the firmware image. And it can [07:25.770 --> 07:33.250] be read with Pico tool. It can be encoded, it's encoded by MicroPython. Firmware can specify in [07:33.250 --> 07:39.690] MicroPython that it's going to include additional fields in that Pico tool or in that binary [07:39.690 --> 07:47.050] information. And MicroPython does this with simply a list of the frozen modules. And so, [07:47.050 --> 07:52.210] again, that seems real easy. You can look at how to parse that from a tool that comes with [07:52.210 --> 07:58.830] MicroPython that we're going to be using a lot here, NPY tool. This is used for freezing [07:58.830 --> 08:07.890] modules, for inspecting NPY files. There's some stuff in there for how it encodes that, that [08:07.890 --> 08:14.350] information. And so since this is too easy as a starting point, I mean, it's too easy because [08:14.350 --> 08:21.150] you can strip this out. It's not necessary for the operation of the firmware, right? So other [08:21.150 --> 08:27.530] platforms, other micro controls may not even have this sort of field, this sort of binary [08:27.530 --> 08:34.590] information in the firmware. It's not used by MicroPython itself, so we can evade this analysis [08:34.590 --> 08:41.030] by commenting out the code in the freezing tool that generates that list, right? And so to make [08:41.030 --> 08:45.490] it harder on ourselves, right? Because we imagine the next step after we start ripping this [08:45.490 --> 08:50.690] stuff out, people are going to start obfuscating this part. So we can rip this out and not have [08:50.690 --> 08:57.930] those frozen modules listed. If you can get to a serial console REPL, you can also do help [08:57.930 --> 09:03.670] modules and list them out. That is probably something that needs to be in there. I don't know [09:03.670 --> 09:09.210] if you could patch out an evasion from, or a countermeasure against that. But that's, but it's [09:09.210 --> 09:14.830] not always the case that you're going to be able to easily get the REPL on a microcontroller. [09:16.610 --> 09:22.970] So for our example, we're going to have the minimum viable Python module here, a hello world [09:22.970 --> 09:30.310] that we're going to demonstrate in plain text. We're going to demonstrate it as a compiled byte [09:30.310 --> 09:37.470] code file on the file system. And then finally as a frozen module compiled into MicroPython and [09:37.470 --> 09:44.370] look at what that looks like in the binary. Plain text is simple. You simply use the MP remote [09:44.370 --> 09:50.550] tool to upload it into the file system on the microcontroller. There's a small, very small [09:50.550 --> 09:57.570] file system that it stores those things in. You can list files on it and you import and go. [09:58.690 --> 10:07.990] You can use the MPY cross compiler to compile your hello world dot PY into an MPY into PY file [10:07.990 --> 10:12.610] and you wind up with the hex dump or you wind up with a file that has the following hex [10:12.610 --> 10:18.230] representation. And so you can kind of see in there, you see our strings, you can see some [10:18.230 --> 10:23.670] byte code in there and so you're like okay, this is a thing. Um, this immediate, doesn't [10:23.670 --> 10:29.970] immediately look to you to be very useful, but the MPY tool can work with this format very well [10:29.970 --> 10:35.550] and you can upload it into the file system and, and use it. And so if you find one on a file [10:35.550 --> 10:44.430] system, use MPY tool to uh, to tinker around with it. As a frozen module, you can take that MPY [10:44.430 --> 10:50.910] and rebuild the microcontroller or firmware from scratch. You can rebuild MicroPython from scratch [10:50.910 --> 10:58.390] including that MPY that you created. It brings it in, it kind of uh, it brings it in as a set of C [10:58.390 --> 11:03.150] structures and we'll talk about that in a little bit, but basically it's hard to go back. After [11:03.150 --> 11:07.410] you've frozen it into the firmware, it's harder to pull it back. It's not just a file on the file [11:07.410 --> 11:18.100] system anymore. But you can use it as a Python module just fine. So what can we learn from an [11:18.100 --> 11:24.360] MPY file? Because our goal eventually here is going to be to take that uh, frozen module, [11:24.360 --> 11:30.700] extract it from the firmware, recreate the MPY so that we can parse it. The MPY tool creates [11:30.700 --> 11:39.660] um, uh, uh, the frozen module from the MPY. Uh, and so the MPYtool.py code has a lot of uh, [11:39.660 --> 11:46.140] uh, algorithms and data structures in it for generating this. And so it does not generate the, [11:46.140 --> 11:51.660] binary that's going to be in the firmware directly. It generates a set of C code that's [11:51.660 --> 11:57.940] going to get statically linked into the MicroPython firmware. So um, and it's less coded and it's just [11:57.940 --> 12:02.860] like a whole lot of data structure, a whole lot of structs, right? And those structs are going to [12:02.860 --> 12:07.900] get compiled in and they're going to get arranged by the compiler into memory in various places that [12:07.900 --> 12:13.940] are not immediately obvious to you from just reading the code. Uh, the same tool can be used [12:13.940 --> 12:20.540] to dump info about an MPY file with a disassemble flag. So it does have a built in disassembler. Uh, [12:20.540 --> 12:25.560] this is the only disassembler you get with MicroPython and it's only pulling from MPY files. [12:25.560 --> 12:31.700] So if we get code out of the binary, we're going to need to disassemble it with this tool. And by [12:31.700 --> 12:39.860] the way, the bytecode for MicroPython is not the same bytecode as CPython. It's definitely not the [12:39.860 --> 12:46.000] same opcodes. It's many of the same, uh, it's many of the same opcodes with different, different values [12:46.000 --> 12:52.000] and uh, not all of the same opcodes, right? So it's a from scratch implementation. [12:53.820 --> 13:01.960] If we dump or disassemble an MPY file using the MPY tool, we get three main [13:01.960 --> 13:09.840] things out of it. We get a string table, a module, a string table, an object table, and a set of code. [13:10.740 --> 13:14.140] What you're looking at here is the string table and I'm going to talk to you about this, [13:14.140 --> 13:18.480] this, um, intern string format that they have here. [13:19.960 --> 13:25.420] For the code, there's two pieces of code. If you remember, we had a module that defined, [13:25.420 --> 13:31.080] we defined a module and then we defined a go function in it or a hello world function in it. [13:31.400 --> 13:37.340] And, um, so you're going to have two functions that have bytecode. The first one is going to [13:37.340 --> 13:42.400] be a constructor and the next one is going to be your go function that actually repeats hello [13:42.400 --> 13:48.420] world every, uh, five seconds. And you can see the, the bytecode in these and it's a stack-based [13:48.420 --> 13:53.760] language. Basically you're, you're, you're constantly loading things out of different [13:53.760 --> 13:58.700] data structures, putting them on the stack, calling other functions, pushing and popping [13:58.700 --> 14:05.340] and looping around and doing things like that. And so again, if you read up on how a Python bytecode [14:05.340 --> 14:12.000] works, it would make sense to you, but, uh, it's not exactly the same. So you can't use like the [14:12.000 --> 14:19.600] body of Python disassembly tools on this or debugging tools for this. There's a really nice [14:19.600 --> 14:24.800] colored output hex dump that you can get within PY tool that will sort of tell you what the [14:24.800 --> 14:28.740] different parts of that hex dump mean and what, what, what they are, what they have, what they're [14:28.740 --> 14:37.160] about. That'll help us when we're trying to recreate one of these. So if we can rebuild an [14:37.160 --> 14:41.540] MPY of a frozen module out of information we extract from firmware, we've got something we [14:41.540 --> 14:46.680] you can use to, to, to get some work done with it. Uh, it'll take some RE work, but it's not [14:46.680 --> 14:51.800] because it's not there in a contiguous binary blob. It's structures we've got to walk, right? [14:51.800 --> 14:58.300] We've got to find them in memory and walk them. Uh, we have to understand string interning. We [14:58.300 --> 15:02.880] need to look at the byte, byte code briefly. It's just not going to be a byte code class or [15:02.880 --> 15:09.420] anything. Um, but also another tool that we can use is we can build a version of MicroPython [15:09.420 --> 15:14.760] that's the Unix port, quote unquote Unix port. You can build it for, for Windows, Linux or [15:14.760 --> 15:20.940] whatever, but it's basically a desktop version of MicroPython, uh, that you can, that you can [15:20.940 --> 15:25.940] build and load MPY files into, run them, analyze them and do things to where you're not just [15:25.940 --> 15:30.320] restricted to working on the microcontroller itself when you're doing your reverse engineering. [15:31.300 --> 15:38.140] So first off, uh, how we're storing our string constants. We're doing what's called string [15:38.140 --> 15:45.300] interning. And so this is something that you do on very limited memory environments where you [15:45.300 --> 15:52.180] want to avoid having the same constant string in memory more than once, right? You don't want to [15:52.180 --> 15:57.960] have, uh, main or, or something like, or some other common string 50 different times in your [15:57.960 --> 16:02.940] memory if your memory is in on the order of megabytes or kilobytes or something like that, [16:02.940 --> 16:12.340] right? And so MicroPython, MicroPython calls these Q strings for unique, for unique strings. [16:12.900 --> 16:19.540] Um, and basically Q string values, the way you refer to them is, is into an index, into a linked [16:19.540 --> 16:27.580] list of pools of these strings, right? And so if you're wanting to refer to hello cruel world, [16:27.580 --> 16:36.080] or you're wanting to refer to, uh, the, the, uh, the open tag module closed tag, uh, string, [16:36.080 --> 16:41.080] you're never going to refer to it as a pointer or you'll get a pointer to it, but you're going [16:41.080 --> 16:47.460] to refer to it as, and pass it around as an index into these linked, linked pools. [16:48.160 --> 16:53.900] And so there's a link, there's a set of static ones that are in the makeQStringData.py that's [16:53.900 --> 17:00.300] used in the build process of MicroPython. Um, and those are constant across all your builds of [17:00.300 --> 17:06.540] MicroPython. And then past that in the next pool, it's going to be stuff that's, varies between each [17:06.540 --> 17:15.080] build depending on which frozen modules you include. So if you use NPY tool to create frozen [17:15.080 --> 17:22.360] code generated from your NPY file, uh, it's going to create a strings, a string data structure. [17:22.360 --> 17:27.100] And so first you've got an enum that has, uh, basically, you know, that's going to compile [17:27.100 --> 17:33.840] down to one, two, three, four, basically a indexing hello cruel world, go hello world.py, [17:33.840 --> 17:38.700] sleep time, all of those sorts of things. Then you're going to have hashes for them, [17:38.700 --> 17:44.620] like a simple one byte hash, and then a link for each of them. Those are going to be placed [17:44.620 --> 17:51.940] into pools. And so on the right hand side there, you see the frozen constant pool, uh, that, uh, [17:51.940 --> 17:57.420] has, uh, has those five strings represented in there. There's also going to be a link to the [17:57.420 --> 18:07.620] previous pool and also a previous pool size. So your QString indexes are global. They're globally [18:07.620 --> 18:13.420] unique. And so you have to look at that previous pool size to know where the indexes start for [18:13.420 --> 18:19.360] this pool. All the pools are linked together. The first constant pool starts at zero and it goes to [18:19.360 --> 18:25.140] however many constants there are. And then the frozen pool, if it's next, starts at that number [18:25.140 --> 18:34.940] plus one, plus two, plus three, plus four, plus five. So, um, and so this is another, this is [18:34.940 --> 18:42.260] the, the QString table data, uh, here. So now you've got those, uh, these defined constants [18:42.260 --> 18:47.160] for these. And so this is how you refer to the QStrings that you have. These are all your indexes [18:47.160 --> 18:56.210] defined as constants. So the question is, how do we find the compiled module in a compiled firmware? [18:56.210 --> 19:00.810] Now, if you're lucky, they built it with symbols, right? But we're not going to assume we're lucky [19:00.810 --> 19:04.270] here. We're going to assume they stripped out that binary information. We're going to assume that [19:04.270 --> 19:10.650] they stripped out the, uh, the symbols. And so for me, whatever, whenever I see somebody [19:11.210 --> 19:17.510] in compiled code that I'm reverse engineering, and they're using, uh, an open source module, [19:17.510 --> 19:22.950] like if in malware, if they're using OpenSSL or they're using, um, uh, something similar, [19:22.950 --> 19:30.310] I'll try to build a copy of that SSL library myself of almost as close of a version as I can [19:30.310 --> 19:35.790] get, and as close to the same options as I can figure for theirs, so that I can then use that [19:35.790 --> 19:41.690] as sort of a Rosetta Stone for their module that doesn't have symbols, right? Because there's data [19:41.690 --> 19:47.390] structures that I want to walk through there. And so the, one of the best things that you can do [19:49.290 --> 19:55.410] is build a debug build of MicroPython that has a frozen module in it, not necessarily, [19:55.410 --> 19:58.910] you don't have their frozen module, but it has a frozen module in it, [19:58.910 --> 20:05.770] and use that to get your bearings in the binary, right? Uh, have it load at the same address and, [20:05.770 --> 20:10.190] that sort of thing. And then you can have two copies of Ghidra up, up side by side, [20:10.190 --> 20:14.410] and use one to figure out where the data structures are in the other. [20:16.070 --> 20:21.650] And so in the debug build, it's called mpFrozenNames. The mpFrozenNames is a [20:21.650 --> 20:31.590] list of the frozen modules on the system. In your, um, in your, uh, uh, uh, debug build, [20:31.590 --> 20:39.490] you also have the frozen modules themselves, mpFrozenNpyContent. So you've got, um, there's [20:39.630 --> 20:45.470] a lot of sort of built-in frozen modules. Uh, you'll see, um, like if you have an Adafruit [20:46.090 --> 20:52.450] CircuitPython, uh, device, CircuitPython is based on MicroPython. So, uh, CircuitPython [20:52.450 --> 20:57.090] devices will have other frozen modules in them, and you may not necessarily have the source code [20:57.090 --> 21:03.910] to them, and they'll be in this list. Uh, uh, I think Lego makes a, uh, a device that has a [21:03.910 --> 21:09.430] microcontroller in it that has MicroPython or CircuitPython on it, and they also have some [21:09.430 --> 21:15.430] of their own closed source libraries that are, that are in this as well. And so this is easy [21:15.430 --> 21:20.590] mode, right? So we found the list of names of modules. We found pointers to the modules [21:20.590 --> 21:29.230] themselves, but we don't have the names of frozen names and frozen MPY content in our stripped [21:29.230 --> 21:35.170] binary. So we have a stripped binary that we copied from a microcontroller, or we got from [21:35.170 --> 21:40.730] an over-the-air update or something like that. We've got to find them in a non-debug build, [21:40.730 --> 21:45.790] right? And so the way we can find them, if we, if we can ever get to the same things, [21:45.790 --> 21:51.590] then we can start using those C data structures from the C files that these things, that from [21:51.590 --> 21:57.150] the source code of MicroPython, the source code of other frozen modules, we can start using those [21:57.150 --> 22:02.470] to sort of walk it and figure out what's different. But we've got to find those data structures first. [22:03.150 --> 22:10.610] The easiest way to find these two data structures in a non-debug build is to, is to, uh, one of the [22:10.610 --> 22:17.570] easiest ways for this is to just eyeball it. You can search for .py and just find sort of the list [22:17.570 --> 22:23.230] of constants that have .py, especially if you know the other frozen modules and the other frozen [22:23.230 --> 22:28.630] modules are consistent with the standard MicroPython ones. You can compare that list and [22:28.630 --> 22:34.930] then whatever's additional on there are other additional frozen MicroPython modules. Another, [22:35.190 --> 22:40.350] a little bit more, uh, resilient way of finding that that doesn't depend on you just sort of [22:40.350 --> 22:47.190] eyeballing a binary, which I don't mind doing all day, but y'all might, uh, is to find it as a code [22:47.190 --> 22:55.090] reference. And so there's a function that uses that list called mpfindFrozenModule and chances [22:55.090 --> 23:04.910] are it's going to build in roughly the same location in your debug build as in your, uh, [23:04.910 --> 23:13.350] right here. So you have this move, load, uh, this move, load, store combination here using this, [23:13.350 --> 23:18.090] uh, address. That address is the address to the mpFrozenNames. [23:20.250 --> 23:25.130] To find the frozen module content, there's all, there, you can't really eyeball it because it's [23:25.130 --> 23:31.630] all binary content. Uh, but there's, uh, indirect reference in, in that same mod, in that same [23:31.630 --> 23:37.350] function. So if you find those frozen names, eyeballing them, you can do a cross reference [23:37.350 --> 23:44.810] in Ghidra to walk back to where mpfindFrozenModule is and say, okay, that matches up with the debug [23:44.810 --> 23:50.530] build. Now, later on in that function, there's a bit of code that looks like this. This is a [23:50.530 --> 23:55.830] reference to the frozen MPY content. And so now we have pointers to our mod, to the, to the base [23:55.830 --> 24:01.230] data structures of our modules. So from here, it should be fairly simple work, just sort of [24:01.230 --> 24:05.790] working through those structures and pulling it out. And what we want to do is we want to [24:05.790 --> 24:12.570] 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:21.640] The header for the MPY file is, is, uh, going to be the same. Usually you can just take that [24:21.640 --> 24:26.180] from any of them and it's not super important as long as you get something that it passes the [24:26.180 --> 24:33.380] checks and it goes. It starts with magic letter M, it goes with a major micropython version. [24:34.700 --> 24:40.160] You want to have, you can have, so there's a flag in there for, or, or, and you can have it [24:40.160 --> 24:48.640] per function, whether it's native or non-native bytecode. So you can have, you can have an MPY [24:48.640 --> 24:57.580] module where the actual code of a function is native, say ARM or MIPS or whatever, or, uh, uh, [24:57.580 --> 25:04.580] byte, like not bytecode, machine code, right? Now for my money, that would be a lot easier because [25:04.580 --> 25:10.340] I can just, if I get a pointer to that, like Gaedra will, will decom, disassemble and decompile [25:10.340 --> 25:15.820] that for me. And I'm, and I'm off to the races. I don't have to interpret bytecode. So we're going [25:15.820 --> 25:20.540] with what I think is the harder example is if it's actual Python bytecode there. [25:22.740 --> 25:30.940] And then, um, there's a data type that comes up frequently in this called a variably in, [25:31.360 --> 25:37.740] a variably encoded unsigned integer, a VUINT. And this main, I don't know, maybe this is more [25:37.740 --> 25:43.240] common in microcontrollers than, than I know, but, uh, but I had to take a close look at this. [25:43.240 --> 25:52.940] And basically this is a variable length integer that for each byte, the lower seven bits is, [25:52.940 --> 25:58.940] uh, is part of the integer. And if there are more bits, then the eighth bit is set. And so [25:58.940 --> 26:03.240] if the eighth bit is set, then, you know, okay, I got to read another seven bits from the next [26:03.240 --> 26:08.380] byte and so on and so forth. And it can be arbitrarily long, right? And, and have a [26:08.380 --> 26:14.040] 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:33.160] So in the hello world dot mpy, we have a number of the, the intern strings that we want to have [26:33.160 --> 26:40.560] in there. Uh, and we'll see in there, we only, we only store the actual string data for strings [26:40.560 --> 26:46.560] that aren't already in the constant pool. So if something's already built into MicroPython, [26:46.560 --> 26:51.040] we don't redefine that string. We just make a reference to it. And so when we're creating our [26:51.040 --> 26:57.460] mpy file, we have to be mindful of which, which strings are new, essentially, which strings are [26:57.460 --> 27:07.550] what pool they come from. So frozen module key strings get mixed together in, um, in a, in a, [27:07.550 --> 27:15.650] in a mpy key string frozen constant pool, right? And so that's one of a few different pools in [27:15.650 --> 27:21.190] there that are all in a linked list where across the linked list, there's unique indexes for each [27:21.190 --> 27:33.130] string, right? So, um, so to find, to find that pool, the easiest thing to do is to go through, [27:33.130 --> 27:37.410] if you can find the main function, which you'd have if you're new to reverse engineering, [27:37.410 --> 27:42.230] maybe the tough part, but if you can kind of trace through the very beginnings of execution [27:42.230 --> 27:49.310] of that binary, if you can find main, there's mpy init and then key string init. And in the [27:49.310 --> 27:54.990] first four instructions in there, there is a reference to the key string frozen constant pool. [27:55.670 --> 28:00.150] Uh, you can search for a string that you know is part of that module. Like if you've just run [28:00.150 --> 28:05.410] strings on the binary, you'll have seen some of it and you can just do cross, cross references [28:05.410 --> 28:13.190] back from that if you really want to. So, so if we're going to parse these pools, we got to look [28:13.190 --> 28:19.290] at the data type for it. So a pool has a previous pointer to the previous pool in this linked [28:19.290 --> 28:26.450] list of pools. Uh, you can move backwards in the pools, but not forwards. And so, uh, that's why [28:26.450 --> 28:32.470] that first, uh, those first instructions in that key string init refer to the frozen constant pool [28:32.470 --> 28:38.050] because that's going to be the last one probably. Uh, there's a total previous length that you use [28:38.050 --> 28:44.230] to figure out where the indexes start in this pool. Uh, there's a few other fields that don't [28:44.230 --> 28:50.150] make a whole lot of difference, but at the end you have a list of hash values for the strings. [28:50.250 --> 28:55.190] Uh, you have a list of lengths for the strings, and then you have a list of the data, right? And so [28:55.190 --> 29:00.970] the string data is just going to be every string catenated together solidly with nothing in between [29:00.970 --> 29:07.990] because that lengths field in the previous, uh, the previous pointer or that, that list will tell [29:07.990 --> 29:12.830] you the lengths of each of them. And so you have to parse both of those arrays together to figure [29:12.830 --> 29:23.920] out, uh, where, what each string is. So with, uh, the previous pool is, the previous pool to the frozen [29:23.920 --> 29:31.680] pool is the npq string constant pool. The total previous length, uh, in our example is 3c2, [29:31.680 --> 29:39.120] so that's where the indexes start for the frozen modules. So any q string index, uh, greater than, [29:39.120 --> 29:45.400] so any q string greater than 962 is going to be in our frozen pool, which means that when we [29:45.400 --> 29:51.260] recreate our mpy, we're going to have to include that data into our mpy. Uh, anything less than [29:51.260 --> 29:59.690] that, we can just include the number and not re-encode the string. And so there's our list [29:59.690 --> 30:06.470] of indexes, and so there's a constant string, q string table data, uh, for hello world that you [30:06.470 --> 30:12.950] can locate through there, and that's the indexes. And so that first one, uh, uh, that first one, [30:12.950 --> 30:18.810] since it's greater than 962, we're going to have to encode that string hello world.py into our mpy [30:18.810 --> 30:25.530] file. But 7, which is just like the open tag module close tag, is already in the micropython [30:25.530 --> 30:32.170] binary, so we just include 7 and we don't re-encode it. And so this is how it looks when we re-encode [30:32.170 --> 30:37.670] it. Uh, this is, so we've already written the header, which is those first handful of bytes. [30:37.670 --> 30:44.710] The highlighted portion is the string table. Uh, there's a vunt where basically it defines [30:44.710 --> 30:50.950] whether or not it's a static entry or one that's defined in this mpy. So are we providing an index [30:50.950 --> 30:58.430] or are we providing a new string? And then the string follows. And that's why you see in that [30:58.430 --> 31:04.970] dump, you see, uh, the string data for the new strings, but not the built-in strings. [31:09.990 --> 31:16.390] So the next thing that you have to do is to, uh, build the object table. Now this is a more [31:16.390 --> 31:23.130] complex data structure, and especially as you get further from hello world, this is going to [31:23.130 --> 31:28.910] get more and more complicated. The more they're going to, the more they're using, uh, Python [31:28.910 --> 31:36.110] objects, the, the, the harder this part's going to become. But you can, like, follow through and, [31:36.110 --> 31:41.430] uh, and recreate these as well. Copy, really what you can do is you can copy these almost [31:41.430 --> 31:46.070] straight out of memory and put them into your binary and it'll just work without you necessarily [31:46.070 --> 31:51.930] having to understand them at first. Then once you get this thing fully recreated, you can load it [31:51.930 --> 31:58.590] into that Unix MicroPython interpreter and, uh, and start probing those objects from the REPL, [31:58.590 --> 32:10.390] which is much easier. And then finally, the byte code. So, oh, um, there's a format that for this [32:10.390 --> 32:16.310] code that we have to generate. If you look in mpytool.py's code, there's a function called [32:16.310 --> 32:22.570] read raw code that sort of hints to you as to how we need to build this byte code so that it can [32:22.570 --> 32:30.050] read it. Um, so the pointer to the code is the third field of that module, and so you see the [32:30.050 --> 32:36.670] frozen module, hello world there, and you see the QString table, the object table, and then you see [32:36.670 --> 32:42.090] the pointer to the first element of code, and that's for the prototype function for this object, [32:42.090 --> 32:51.530] right? And so each, each function, each, sorry, each piece of byte code can have children. So [32:52.790 --> 32:59.970] the prototype for this module is going to have a child of the go function that's inside of [32:59.970 --> 33:07.190] this module. And so it's going to contiguous, or continuously rather, or parse these elements of [33:07.190 --> 33:13.470] code, see whether they have children or not, and recursively follow down. And once you've [33:13.470 --> 33:18.650] parsed everything that doesn't have children, then you can walk back up that recursion and you've [33:18.650 --> 33:26.470] parsed all the byte code out. And so on the left there, you see the byte code for, um, for the, [33:26.470 --> 33:32.570] for the constructor, and on the right you see the byte code for the go function, which prints [33:32.570 --> 33:38.250] hello world in a loop, basically. And so one is the child of the other, essentially. [33:43.710 --> 33:49.350] So reconstructing this, uh, there are VUints for the kind, the length, and whether or not the code [33:49.350 --> 33:54.010] has children. So when we're putting this into our MPY binary, it needs to know how to reconstruct [33:54.010 --> 34:00.010] this into memory in a useful way for the interpreter. And so it's using those VUints to, uh, to do this. [34:00.010 --> 34:05.990] And so there's a kind, whether it's byte code or native code. Again, these things can be compiled [34:05.990 --> 34:10.970] down to where it's native ARM code for a function. And like I said, if that's the case, if you see [34:10.970 --> 34:16.930] something encoded like that, just, just drop your cursor and Ghidra or Idapro on that, hit [34:16.930 --> 34:21.930] disassemble, and you're off to the races, right? You can, you can read what it's doing a little [34:21.930 --> 34:27.110] bit easier. But if, if it's not, it's just, if it's byte code, Ghidra doesn't have a decoder [34:27.110 --> 34:32.690] for this, unless you want to write one. I didn't want to, uh, uh, uh, Karo back there probably will. [34:32.950 --> 34:40.630] Oh, uh, uh, and, uh, but, uh, but if you don't want to write a module to, to, to interpret this [34:40.630 --> 34:45.650] byte code, uh, you're, you're going to be left to pulling it out and dropping it in this file [34:45.650 --> 34:52.730] so the MPY tool can do this for us. Uh, the third, the third bit, is there a child for this block or [34:52.730 --> 35:00.950] not, right? And so that makes it recursively go through and read more code. So the one big problem [35:00.950 --> 35:06.270] with this, and, uh, I wanted to write a tool that would just sort of walk through a binary and do [35:06.270 --> 35:15.550] this, uh, but it's actually tricky to do because the length of the code is not referenced anywhere, [35:15.550 --> 35:21.570] right? The length of the code is defined by the fact that the code never exists, never steps [35:21.570 --> 35:27.110] outside of the bounds of its memory locations. So there's no length, it just starts execution [35:27.110 --> 35:31.930] there, and it'll just keep going as long as there's code, and as long as it doesn't return, [35:31.930 --> 35:36.430] and as long as it's looping around, it'll just sort of loop in that space. So there's no defined [35:36.430 --> 35:40.130] length for it, which, I don't know, maybe you could figure out a problem there, maybe you could [35:40.490 --> 35:45.550] exploit that in some way. But there's no length, and so it's hard in an automated fashion without [35:45.550 --> 35:50.810] parsing the byte code to know the length of it. And so you may have to eyeball that part of it. [35:50.810 --> 35:57.910] Or parse the byte code yourself, kind of trial and error. Each block of code is filed recursively [35:57.910 --> 36:05.230] by its own children. And so here you see in the first screenshot, we're encoding those integers [36:05.230 --> 36:16.670] and then plopping in the byte code for the constructor for the module. And then next, [36:16.670 --> 36:24.830] it has a child of the go function, and we go and parse that out, we put the vunts for it in there, [36:24.830 --> 36:29.970] and then we put the byte code in for the go function. Once you think you've created, [36:29.970 --> 36:35.790] and at this point, like if you take these slides and you go back, you'll see that this is bit for [36:35.790 --> 36:44.710] bit identical to the original mpy file for the hello world.mpy. We can use my Unix port to [36:44.710 --> 36:52.390] examine this. So we can load up the Unix port of MicroPython, import that mpy file, start looking [36:52.390 --> 36:57.570] at the objects, start looking at the modules, do the dict function to see what functions are [36:57.570 --> 37:06.690] available in there. We can call them. There is a tracing tool in, actually this is not, [37:06.690 --> 37:11.750] the tracing tool is in the next slide. This is another example that you can just sort of eyeball [37:11.750 --> 37:20.710] on your own of a, of a, this, this code right here will make a little light show on all the GPIO [37:20.710 --> 37:29.130] pins of a, of a, of a Pico. 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:42.290] That's the code that, this is the, this is the code that does this, but since it's doing GPIO, [37:43.490 --> 37:48.610] it won't work on the Unix MicroPython because it's doing like the pin function and stuff like [37:48.610 --> 37:54.210] that. You don't have GPIO on the Unix version of Micro, of MicroPython. So what you need to do is [37:54.210 --> 38:00.470] like if, if the code you pull out uses a function that's not there, you're going to have to implement [38:00.470 --> 38:05.050] some sort of stand, stand in there. And so then you can run it in the Unix port as long as you [38:05.050 --> 38:12.790] have those functions available. You can build MicroPython with SysSetTrace where you get a [38:12.790 --> 38:18.570] trace of execution. It's not quite what you'd expect. It's not a very detailed trace. It [38:18.570 --> 38:25.310] basically tells you which portion of the bytecode is executing in a very, in a very non-granular [38:25.310 --> 38:30.830] fashion. It just basically tells you which function is executing when, but you have to build, [38:30.830 --> 38:36.610] you have to define this when you're building your MicroPython binary or your MicroPython Unix port. [38:38.790 --> 38:45.210] The, the bytecode itself, this is the Hello World bytecode. Basically you're loading a global [38:45.210 --> 38:51.430] object of print, the print function. You're loading the Hello Cruel World string. You're [38:51.430 --> 38:57.610] calling the function. You pop the string off the top of the stack. You load the global time [38:57.610 --> 39:04.190] module. You load the method sleep from it. You load a constant integer of five because you want [39:04.190 --> 39:09.090] to wait for five seconds. You sleep for five seconds by calling the method. You pop the top [39:09.090 --> 39:14.830] off of that and then you jump back 17 bytes. And then you loop around and you do it again. And so [39:14.830 --> 39:24.190] that's the very basics of the language here. If you want to learn this bytecode, if you really [39:24.190 --> 39:28.650] want to take the time to step through bytecode of this and not just sort of interactively do it, [39:28.650 --> 39:35.710] you'll want to read through the MPY tool opcode class. And also, and in class in terms of like [39:35.710 --> 39:42.230] the class definition, not like a class that you sit down and learn. There's a vm.c in MicroPython [39:42.230 --> 39:48.070] that has the vm interpreter for this bytecode in there that you can read as well. This is not [39:48.070 --> 39:53.630] compatible with CPython, but familiarity with that would help. If you read through some Python [39:53.630 --> 40:02.170] bytecode tutorials, you'll understand what this is going. Conclusions on this, I'd really rather [40:02.170 --> 40:10.190] be reversing compiled C than this. Using MicroPython and creating bytecode out of your [40:10.190 --> 40:19.210] functions and stuff is probably almost a better obfuscation of your code than compiling a C [40:19.210 --> 40:25.750] firmware for your microcontroller. It's just annoying to try to read through this yourself. [40:25.750 --> 40:31.910] If you're more used to reading ARM assembly, if you're more used to reading decompiled C, [40:32.630 --> 40:39.830] it's doable though. And if you're really wanting to mess around with a device that does this, [40:39.830 --> 40:48.830] it's doable. These frozen modules are a great way to include project-specific stuff. [40:48.870 --> 40:53.130] Really the last stage of your development and deployment of a MicroPython firmware [40:53.130 --> 40:58.710] should be to put as many of your modules into this form as possible because it's just going [40:58.710 --> 41:05.390] to save memory and execution time. This is not a way to hide or obfuscate code or secrets though. [41:06.050 --> 41:11.290] Don't use it for that, especially now that this talk is out and this room of people know how to [41:11.290 --> 41:17.870] do it, right? So don't use this to go screw around with other people's CTFs in ways that [41:17.870 --> 41:24.050] ruin the fun for other people. But if you're like me and you're fun, you make your own fun, [41:24.050 --> 41:28.130] right? You don't really follow the rules on the CTF. You rather just play around with it and not [41:28.130 --> 41:35.830] even score any points. This is fun for that. It's only as well protected as access to your [41:35.830 --> 41:42.950] firmware. And so if you can dump from Flash, if you can dump from the REPL, if you can grab over [41:42.950 --> 41:48.790] the air updates for things, you can get at this stuff. So it's not a layer of protection. And you [41:48.790 --> 41:53.950] could probably automate this extraction process, but it's a bit tricky to figure out the lengths [41:53.950 --> 42:04.570] of some of those data structures too. So one thing that you should do if you are interested [42:04.570 --> 42:12.530] in this sort of stuff, if you like the idea of learning how to use Ghidra to step through all [42:12.530 --> 42:18.010] these data structures, if you want to learn how to sort of navigate around code in Ghidra, [42:18.010 --> 42:24.410] there's a great book from No Starch Press called The Ghidra Book, appropriately named, [42:25.050 --> 42:31.490] by Karen Antz and Chris Eagle. Karen Antz is in our audience here right now. She's been giving [42:31.490 --> 42:38.330] me the eye to mention this the whole time. She's actually going to be at the No Starch booth this [42:38.330 --> 42:46.310] afternoon doing a book signing. I want to say 3.30? 3.30. She's going to be at the booth over [42:46.310 --> 42:52.650] there at No Starch doing a book signing. Go buy that book and get her autograph. And she knows [42:52.650 --> 42:57.530] everything about MicroPython, frozen modules as well. Divert any questions about this, talk to [42:57.530 --> 43:07.050] her. So, no. Ask me the things. And so with that, I'm going to close this off. I appreciate y'all [43:07.050 --> 43:14.290] coming to this instead of Pawnee Awards, however poor of a choice that might have been. And I look [43:14.290 --> 43:17.090] forward to interacting with y'all. Thank you.