1 00:00:01,130 --> 00:00:02,690 Thank you for showing up, everybody. 2 00:00:02,690 --> 00:00:03,910 I appreciate this. 3 00:00:03,910 --> 00: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. 4 00:00:11,130 --> 00:00:15,990 So this is Reverse Engineering MicroPython Frozen Modules. 5 00:00:16,010 --> 00:00:17,910 My name is Wesley McGrew. 6 00:00:17,970 --> 00:00:21,210 I'm Senior Cyber Fellow with Martin Federal. 7 00:00:21,470 --> 00: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. 8 00:00:28,670 --> 00:00:35,210 A lot of badge life badges use this Python interpreter. 9 00:00:35,850 --> 00:00:41,530 And we're able to pick out some things that perhaps you wouldn't have otherwise. 10 00:00:43,930 --> 00:00:50,230 So the agenda for the talk, we're going to be, we're going to introduce what MicroPython is. 11 00:00:50,230 --> 00:00:55,410 For those of you not familiar, I'm pretty sure most of you know about Python in general. 12 00:00:55,490 --> 00:01:00,470 We're going to look at MicroPython firmware on the Raspberry Pi Pico. 13 00:01:00,630 --> 00: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. 14 00:01:09,090 --> 00: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. 15 00:01:25,310 --> 00:01:28,550 We're going to talk about that freezing process and why it's there. 16 00:01:28,890 --> 00:01:35,170 We're going to have a hands-on example of reconstructing that module from a compiled firmware. 17 00:01:35,170 --> 00: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. 18 00:01:44,430 --> 00: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. 19 00:01:52,570 --> 00: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. 20 00:02:02,810 --> 00: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. 21 00:02:16,790 --> 00:02:22,070 And MicroPython completely just re-implements it from scratch. 22 00:02:23,190 --> 00:02:28,170 There's probably reasons for doing that instead of starting from CPython. 23 00:02:28,370 --> 00:02:34,790 Microcontrollers have limited resources and so things are optimized for that kind of environment. 24 00:02:35,330 --> 00:02:38,770 There's a subset of Python standard libraries in there. 25 00:02:38,770 --> 00: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. 26 00:02:49,230 --> 00: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. 27 00:02:58,250 --> 00: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. 28 00:03:08,390 --> 00:03:15,070 You can do that with MicroPython if you have a serial terminal on your microcontroller like you do with a Pico. 29 00:03:16,070 --> 00: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. 30 00:03:29,730 --> 00:03:32,770 You see it in badges. 31 00:03:32,770 --> 00:03:37,910 And so like the last year's snacky badge at least used MicroPython. 32 00:03:37,910 --> 00: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. 33 00:03:52,410 --> 00:03:53,830 It's really nice. 34 00:03:53,830 --> 00:03:57,710 So what I'm presenting here is not a mistake made with MicroPython. 35 00:03:57,710 --> 00:03:59,690 It's just a reverse engineering talk. 36 00:03:59,690 --> 00:04:03,710 There's no vulnerability here as MicroPython is concerned. 37 00:04:03,710 --> 00:04:04,930 I like it. 38 00:04:04,930 --> 00:04:10,310 It's really easy to create a prototype of an embedded device with MicroPython. 39 00:04:11,190 --> 00:04:13,350 And so you have three choices for your code. 40 00:04:13,350 --> 00:04:20,270 You can have plain text that's on a small file system that's emulated in part of your flash memory. 41 00:04:20,470 --> 00:04:24,710 You can... and you can read and write to that file system through the REPL. 42 00:04:25,050 --> 00:04:32,010 You can have pre-compiled bytecode modules, and we'll talk about the structure of those. 43 00:04:32,010 --> 00: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. 44 00:04:39,850 --> 00:04:46,210 And that removes a lot of the metadata that makes it easy to find those modules and disassemble them, right? 45 00:04:46,390 --> 00:04:49,450 There's tools for working with the other portions of this. 46 00:04:49,450 --> 00:04:52,970 There's no tools for going back from a frozen module. 47 00:04:53,050 --> 00: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. 48 00:05:05,910 --> 00:05:08,050 The feature is there for efficiency. 49 00:05:08,050 --> 00: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. 50 00:05:20,930 --> 00:05:23,150 So it saves memory. 51 00:05:23,150 --> 00:05:24,150 It's convenient. 52 00:05:24,150 --> 00:05:25,590 You don't have to do any additional loads. 53 00:05:25,590 --> 00:05:26,910 It's already there. 54 00:05:27,390 --> 00:05:35,950 But it's being abused, misused for obfuscating and hiding code and secrets inside of these binaries. 55 00:05:36,090 --> 00:05:41,470 And we've got some quotes here from random forums and stuff. 56 00:05:41,470 --> 00:05:43,170 You probably can't read them. 57 00:05:43,330 --> 00:05:44,290 They're small. 58 00:05:44,290 --> 00:05:45,830 I just grabbed a bunch of them and threw them up. 59 00:05:45,830 --> 00: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? 60 00:05:55,250 --> 00:05:56,290 Or I've got a key. 61 00:05:56,290 --> 00:05:59,630 I've got a secret that I don't want them to read my code. 62 00:05:59,810 --> 00:06:03,270 Their concerns are making this obfuscated. 63 00:06:03,290 --> 00:06:07,690 And people were responding to them on these forums of like, you know, it's not perfect. 64 00:06:07,690 --> 00:06:09,270 And everybody acknowledges that this isn't perfect. 65 00:06:09,270 --> 00: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. 66 00:06:18,090 --> 00:06:20,530 And so let's make something to disassemble it. 67 00:06:22,050 --> 00:06:26,470 It's a layer of protection for some folks, but it's really just an obfuscation. 68 00:06:27,210 --> 00:06:31,610 The first thing that you have to do to get this thing working is to get a firmware image. 69 00:06:31,710 --> 00:06:33,590 So that's out of scope for this talk. 70 00:06:33,590 --> 00:06:37,450 And it's tricky depending on the module, depending on your microcontroller. 71 00:06:37,450 --> 00:06:39,810 You might not be able to get a flash image easy. 72 00:06:39,810 --> 00:06:42,990 That's a whole talk on, you know, glitching and things like that. 73 00:06:42,990 --> 00:06:54,130 But since we're using Pico as the, we can, we can just use the standard Pico tools for this demonstration. 74 00:06:54,310 --> 00: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. 75 00:07:04,530 --> 00:07:07,690 But it actually makes it a little too easy as you can see from the output here. 76 00:07:07,690 --> 00:07:11,010 From the output, you have a list of frozen modules here. 77 00:07:11,110 --> 00:07:13,230 And you're like, well, that's, that seems dumb. 78 00:07:13,230 --> 00:07:14,970 Seems like a really short talk. 79 00:07:15,210 --> 00:07:24,970 And what that's from is from Pico's, Pico has binary information, quote unquote, binary information in the firmware image. 80 00:07:24,970 --> 00:07:27,990 And it can be read with Pico tool. 81 00:07:27,990 --> 00:07:31,210 It can be encoded, it's encoded by MicroPython. 82 00:07:31,270 --> 00: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. 83 00:07:41,250 --> 00:07:45,930 And MicroPython does this with simply a list of the frozen modules. 84 00:07:46,310 --> 00:07:48,750 And so, again, that seems real easy. 85 00:07:48,750 --> 00: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. 86 00:07:57,430 --> 00:08:02,230 This is used for freezing modules, for inspecting NPY files. 87 00:08:03,090 --> 00:08:09,050 There's some stuff in there for how it encodes that, that information. 88 00:08:09,690 --> 00: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. 89 00:08:16,490 --> 00:08:20,430 It's not necessary for the operation of the firmware, right? 90 00:08:20,430 --> 00: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. 91 00:08:29,870 --> 00: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? 92 00:08:40,490 --> 00:08:42,450 And so to make it harder on ourselves, right? 93 00:08:42,450 --> 00:08:48,470 Because we imagine the next step after we start ripping this stuff out, people are going to start obfuscating this part. 94 00:08:48,950 --> 00:08:52,410 So we can rip this out and not have those frozen modules listed. 95 00:08:53,970 --> 00:08:59,690 If you can get to a serial console REPL, you can also do help modules and list them out. 96 00:08:59,690 --> 00:09:02,670 That is probably something that needs to be in there. 97 00:09:03,170 --> 00:09:07,930 I don't know if you could patch out an evasion from, or a countermeasure against that. 98 00:09:08,010 --> 00: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. 99 00:09:16,610 --> 00: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. 100 00:09:28,130 --> 00:09:32,650 We're going to demonstrate it as a compiled byte code file on the file system. 101 00:09:33,030 --> 00:09:39,990 And then finally as a frozen module compiled into MicroPython and look at what that looks like in the binary. 102 00:09:40,790 --> 00:09:42,290 Plain text is simple. 103 00:09:42,290 --> 00:09:48,030 You simply use the MP remote tool to upload it into the file system on the microcontroller. 104 00:09:48,030 --> 00:09:53,510 There's a small, very small file system that it stores those things in. 105 00:09:53,530 --> 00:09:57,570 You can list files on it and you import and go. 106 00:09:58,690 --> 00: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. 107 00:10:13,870 --> 00: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. 108 00:10:22,150 --> 00: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. 109 00:10:34,110 --> 00:10:40,190 And so if you find one on a file system, use MPY tool to uh, to tinker around with it. 110 00:10:41,250 --> 00:10:48,750 As a frozen module, you can take that MPY and rebuild the microcontroller or firmware from scratch. 111 00:10:48,750 --> 00:10:53,790 You can rebuild MicroPython from scratch including that MPY that you created. 112 00:10:53,790 --> 00: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. 113 00:11:02,890 --> 00:11:06,190 After you've frozen it into the firmware, it's harder to pull it back. 114 00:11:06,190 --> 00:11:08,510 It's not just a file on the file system anymore. 115 00:11:08,510 --> 00:11:11,830 But you can use it as a Python module just fine. 116 00:11:16,200 --> 00:11:19,180 So what can we learn from an MPY file? 117 00:11:19,180 --> 00: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. 118 00:11:28,880 --> 00:11:34,960 The MPY tool creates um, uh, uh, the frozen module from the MPY. 119 00:11:34,960 --> 00: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. 120 00:11:43,560 --> 00:11:48,140 And so it does not generate the, binary that's going to be in the firmware directly. 121 00:11:48,140 --> 00:11:54,920 It generates a set of C code that's going to get statically linked into the MicroPython firmware. 122 00:11:55,320 --> 00: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? 123 00:12:01,620 --> 00: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. 124 00:12:12,000 --> 00:12:17,560 Uh, the same tool can be used to dump info about an MPY file with a disassemble flag. 125 00:12:17,560 --> 00:12:19,760 So it does have a built in disassembler. 126 00:12:19,880 --> 00:12:25,560 Uh, this is the only disassembler you get with MicroPython and it's only pulling from MPY files. 127 00:12:25,560 --> 00:12:30,680 So if we get code out of the binary, we're going to need to disassemble it with this tool. 128 00:12:30,840 --> 00:12:38,100 And by the way, the bytecode for MicroPython is not the same bytecode as CPython. 129 00:12:38,440 --> 00:12:40,860 It's definitely not the same opcodes. 130 00:12:40,860 --> 00: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? 131 00:12:49,680 --> 00:12:52,000 So it's a from scratch implementation. 132 00:12:53,820 --> 00:13:03,160 If we dump or disassemble an MPY file using the MPY tool, we get three main things out of it. 133 00:13:03,160 --> 00:13:09,840 We get a string table, a module, a string table, an object table, and a set of code. 134 00:13:10,740 --> 00: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. 135 00:13:19,960 --> 00:13:22,900 For the code, there's two pieces of code. 136 00:13:22,900 --> 00: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. 137 00:13:31,400 --> 00:13:36,400 And, um, so you're going to have two functions that have bytecode. 138 00:13:36,400 --> 00: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. 139 00:13:44,620 --> 00:13:49,060 And you can see the, the bytecode in these and it's a stack-based language. 140 00:13:49,060 --> 00: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. 141 00:14:01,260 --> 00: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. 142 00:14:10,800 --> 00:14:17,000 So you can't use like the body of Python disassembly tools on this or debugging tools for this. 143 00:14:18,600 --> 00: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. 144 00:14:29,300 --> 00:14:32,160 That'll help us when we're trying to recreate one of these. 145 00:14:33,840 --> 00: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. 146 00:14:44,640 --> 00:14:49,780 Uh, it'll take some RE work, but it's not because it's not there in a contiguous binary blob. 147 00:14:49,780 --> 00:14:51,800 It's structures we've got to walk, right? 148 00:14:51,800 --> 00:14:54,560 We've got to find them in memory and walk them. 149 00:14:54,840 --> 00:14:57,960 Uh, we have to understand string interning. 150 00:14:57,960 --> 00:15:00,720 We need to look at the byte, byte code briefly. 151 00:15:00,720 --> 00:15:03,640 It's just not going to be a byte code class or anything. 152 00:15:03,880 --> 00: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. 153 00:15:12,360 --> 00: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. 154 00:15:31,300 --> 00:15:36,200 So first off, uh, how we're storing our string constants. 155 00:15:36,260 --> 00:15:39,060 We're doing what's called string interning. 156 00:15:39,060 --> 00: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? 157 00:15:51,400 --> 00: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? 158 00:16:03,480 --> 00:16:12,340 And so MicroPython, MicroPython calls these Q strings for unique, for unique strings. 159 00:16:12,900 --> 00: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? 160 00:16:22,740 --> 00: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, 161 00:16:42,540 --> 00:16:47,460 and pass it around as an index into these linked, linked pools. 162 00:16:48,160 --> 00: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. 163 00:16:56,520 --> 00:17:01,240 Um, and those are constant across all your builds of MicroPython. 164 00:17:01,240 --> 00: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. 165 00:17:10,760 --> 00: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. 166 00:17:22,360 --> 00: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. 167 00:17:36,900 --> 00: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. 168 00:17:43,540 --> 00:17:45,620 Those are going to be placed into pools. 169 00:17:45,620 --> 00: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. 170 00:17:55,480 --> 00:18:01,740 There's also going to be a link to the previous pool and also a previous pool size. 171 00:18:01,820 --> 00:18:06,960 So your QString indexes are global. 172 00:18:06,960 --> 00:18:08,260 They're globally unique. 173 00:18:08,260 --> 00:18:14,140 And so you have to look at that previous pool size to know where the indexes start for this pool. 174 00:18:14,140 --> 00:18:15,860 All the pools are linked together. 175 00:18:15,860 --> 00:18:21,540 The first constant pool starts at zero and it goes to however many constants there are. 176 00:18:21,540 --> 00: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. 177 00:18:32,120 --> 00:18:38,480 So, um, and so this is another, this is the, the QString table data, uh, here. 178 00:18:38,480 --> 00:18:42,820 So now you've got those, uh, these defined constants for these. 179 00:18:42,820 --> 00:18:45,840 And so this is how you refer to the QStrings that you have. 180 00:18:45,840 --> 00:18:48,640 These are all your indexes defined as constants. 181 00:18:51,650 --> 00:18:56,210 So the question is, how do we find the compiled module in a compiled firmware? 182 00:18:56,210 --> 00:18:59,170 Now, if you're lucky, they built it with symbols, right? 183 00:18:59,170 --> 00:19:01,090 But we're not going to assume we're lucky here. 184 00:19:01,090 --> 00:19:03,570 We're going to assume they stripped out that binary information. 185 00:19:03,570 --> 00:19:06,630 We're going to assume that they stripped out the, uh, the symbols. 186 00:19:06,730 --> 00: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, 187 00:19:30,870 --> 00: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? 188 00:19:40,610 --> 00:19:43,950 Because there's data structures that I want to walk through there. 189 00:19:44,150 --> 00: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, 190 00:20:01,970 --> 00:20:02,670 right? 191 00:20:02,870 --> 00:20:06,410 Uh, have it load at the same address and, that sort of thing. 192 00:20:06,410 --> 00: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. 193 00:20:16,070 --> 00:20:20,010 And so in the debug build, it's called mpFrozenNames. 194 00:20:20,010 --> 00:20:24,090 The mpFrozenNames is a list of the frozen modules on the system. 195 00:20:26,130 --> 00:20:37,090 In your, um, in your, uh, uh, uh, debug build, you also have the frozen modules themselves, mpFrozenNpyContent. 196 00:20:37,090 --> 00:20:42,310 So you've got, um, there's a lot of sort of built-in frozen modules. 197 00:20:42,470 --> 00:20:50,730 Uh, you'll see, um, like if you have an Adafruit CircuitPython, uh, device, CircuitPython is based on MicroPython. 198 00:20:50,910 --> 00: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. 199 00:20:58,790 --> 00: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. 200 00:21:13,590 --> 00:21:15,490 And so this is easy mode, right ? 201 00:21:15,490 --> 00:21:18,210 So we found the list of names of modules. 202 00:21:18,210 --> 00: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. 203 00:21:29,950 --> 00: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. 204 00:21:37,910 --> 00:21:41,470 We've got to find them in a non-debug build, right? 205 00:21:41,650 --> 00: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. 206 00:21:59,770 --> 00:22:02,470 But we've got to find those data structures first. 207 00:22:03,150 --> 00: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. 208 00:22:13,310 --> 00: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. 209 00:22:26,930 --> 00:22:33,770 You can compare that list and then whatever's additional on there are other additional frozen MicroPython modules. 210 00:22:34,150 --> 00: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. 211 00:22:47,710 --> 00: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. 212 00:23:05,870 --> 00:23:14,470 So you have this move, load, uh, this move, load, store combination here using this, uh, address. 213 00:23:14,470 --> 00:23:18,090 That address is the address to the mpFrozenNames. 214 00:23:20,250 --> 00: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. 215 00:23:26,990 --> 00:23:32,010 Uh, but there's, uh, indirect reference in, in that same mod, in that same function. 216 00:23:32,010 --> 00: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. 217 00:23:45,630 --> 00:23:49,770 Now, later on in that function, there's a bit of code that looks like this. 218 00:23:49,770 --> 00:23:52,570 This is a reference to the frozen MPY content. 219 00:23:52,830 --> 00:23:57,110 And so now we have pointers to our mod, to the, to the base data structures of our modules. 220 00:23:57,110 --> 00:24:03,670 So from here, it should be fairly simple work, just sort of working through those structures and pulling it out. 221 00:24:03,670 --> 00: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. 222 00:24:15,380 --> 00:24:20,040 The header for the MPY file is, is, uh, going to be the same. 223 00:24:20,040 --> 00: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. 224 00:24:27,640 --> 00:24:33,380 It starts with magic letter M, it goes with a major micropython version. 225 00:24:34,700 --> 00: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. 226 00:24:44,080 --> 00: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? 227 00:25:01,100 --> 00: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. 228 00:25:11,080 --> 00:25:12,860 And I'm, and I'm off to the races. 229 00:25:12,860 --> 00:25:14,740 I don't have to interpret bytecode. 230 00:25:14,760 --> 00:25:20,540 So we're going with what I think is the harder example is if it's actual Python bytecode there. 231 00:25:22,740 --> 00: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. 232 00:25:35,380 --> 00: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. 233 00:25:43,240 --> 00: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. 234 00:25:54,920 --> 00:25:58,760 And if there are more bits, then the eighth bit is set. 235 00:25:58,760 --> 00: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. 236 00:26:05,520 --> 00:26:07,680 And it can be arbitrarily long, right? 237 00:26:07,680 --> 00:26:14,040 And, and have a represent an arbitrarily large number, however much makes sense on your microcontroller. 238 00:26:14,160 --> 00:26:19,540 And so this is a data type that, that MicroPython uses in a lot of its data structures. 239 00:26:19,600 --> 00:26:23,480 And it can be a little bit of a mind bender to, to work with. 240 00:26:26,200 --> 00: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. 241 00:26:34,200 --> 00: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. 242 00:26:43,580 --> 00:26:48,040 So if something's already built into MicroPython, we don't redefine that string. 243 00:26:48,040 --> 00:26:49,480 We just make a reference to it. 244 00:26:49,480 --> 00: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. 245 00:27:02,850 --> 00: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? 246 00:27:12,050 --> 00: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? 247 00:27:22,970 --> 00: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, 248 00:27:43,570 --> 00:27:48,670 if you can find main, there's mpy init and then key string init. 249 00:27:48,670 --> 00:27:54,990 And in the first four instructions in there, there is a reference to the key string frozen constant pool. 250 00:27:55,670 --> 00:27:59,230 Uh , you can search for a string that you know is part of that module. 251 00:27:59,230 --> 00: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. 252 00:28:09,490 --> 00:28:14,570 So, so if we're going to parse these pools, we got to look at the data type for it. 253 00:28:14,570 --> 00:28:20,510 So a pool has a previous pointer to the previous pool in this linked list of pools. 254 00:28:20,730 --> 00:28:24,310 Uh, you can move backwards in the pools, but not forwards. 255 00:28:24,310 --> 00: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. 256 00:28:35,170 --> 00:28:41,230 Uh, there's a total previous length that you use to figure out where the indexes start in this pool. 257 00:28:41,390 --> 00: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. 258 00:28:50,250 --> 00:28:54,930 Uh, you have a list of lengths for the strings, and then you have a list of the data, right? 259 00:28:54,930 --> 00: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. 260 00:29:09,090 --> 00:29:16,030 And so you have to parse both of those arrays together to figure out, uh, where, what each string is. 261 00:29:18,600 --> 00:29:26,560 So with, uh, the previous pool is, the previous pool to the frozen pool is the npq string constant pool. 262 00:29:26,560 --> 00:29:35,160 The total previous length, uh, in our example is 3c2, so that's where the indexes start for the frozen modules. 263 00:29:35,160 --> 00: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. 264 00:29:50,120 --> 00:29:54,460 Uh, anything less than that, we can just include the number and not re-encode the string. 265 00:29:58,250 --> 00: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. 266 00:30:09,430 --> 00: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. 267 00:30:19,390 --> 00: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. 268 00:30:30,090 --> 00:30:32,850 And so this is how it looks when we re-encode it. 269 00:30:32,950 --> 00:30:37,670 Uh, this is, so we've already written the header, which is those first handful of bytes. 270 00:30:37,670 --> 00:30:40,330 The highlighted portion is the string table. 271 00:30:40,510 --> 00: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. 272 00:30:49,050 --> 00:30:53,430 So are we providing an index or are we providing a new string? 273 00:30:54,870 --> 00:30:56,590 And then the string follows. 274 00:30:56,590 --> 00: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. 275 00:31:09,990 --> 00:31:14,930 So the next thing that you have to do is to, uh, build the object table. 276 00:31:14,930 --> 00: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. 277 00:31:24,470 --> 00: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. 278 00:31:33,050 --> 00:31:38,370 But you can, like, follow through and, uh, and recreate these as well. 279 00:31:38,370 --> 00: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. 280 00:31:47,890 --> 00: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. 281 00:32:04,410 --> 00:32:06,910 And then finally, the byte code. 282 00:32:06,930 --> 00:32:11,770 So, oh, um, there's a format that for this code that we have to generate. 283 00:32:11,770 --> 00: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. 284 00:32:23,630 --> 00: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, 285 00:32:42,090 --> 00:32:43,170 right? 286 00:32:43,270 --> 00:32:51,390 And so each, each function, each, sorry, each piece of byte code can have children. 287 00:32:51,390 --> 00: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. 288 00:33:01,470 --> 00: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. 289 00:33:12,090 --> 00: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. 290 00:33:21,270 --> 00: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. 291 00:33:35,270 --> 00:33:38,250 And so one is the child of the other, essentially. 292 00:33:43,710 --> 00:33:50,230 So reconstructing this, uh, there are VUints for the kind, the length, and whether or not the code has children. 293 00:33:50,230 --> 00: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. 294 00:33:56,790 --> 00:34:00,010 And so it's using those VUints to, uh, to do this. 295 00:34:00,010 --> 00:34:03,830 And so there's a kind, whether it's byte code or native code. 296 00:34:03,830 --> 00:34:08,770 Again, these things can be compiled down to where it's native ARM code for a function. 297 00:34:09,010 --> 00: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? 298 00:34:19,390 --> 00:34:22,910 You can, you can read what it's doing a little bit easier. 299 00:34:22,910 --> 00: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. 300 00:34:28,450 --> 00:34:32,690 I didn't want to, uh, uh, uh, Karo back there probably will. 301 00:34:32,950 --> 00: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. 302 00:34:48,110 --> 00:34:53,730 Uh, the third, the third bit, is there a child for this block or not, right? 303 00:34:53,730 --> 00:34:57,590 And so that makes it recursively go through and read more code. 304 00:34:58,990 --> 00: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? 305 00:35:16,050 --> 00: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. 306 00:35:25,150 --> 00: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. 307 00:35:35,550 --> 00: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. 308 00:35:42,250 --> 00: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. 309 00:35:48,610 --> 00:35:50,810 And so you may have to eyeball that part of it. 310 00:35:50,810 --> 00:35:54,750 Or parse the byte code yourself, kind of trial and error. 311 00:35:55,110 --> 00:35:59,470 Each block of code is filed recursively by its own children. 312 00:36:00,570 --> 00: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. 313 00:36:15,730 --> 00: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. 314 00:36:28,410 --> 00: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. 315 00:36:42,670 --> 00:36:45,630 We can use my Unix port to examine this. 316 00:36:45,810 --> 00: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. 317 00:36:58,670 --> 00:37:00,310 We can call them. 318 00:37:01,470 --> 00:37:08,910 There is a tracing tool in, actually this is not, the tracing tool is in the next slide. 319 00:37:09,010 --> 00: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. 320 00:37:23,570 --> 00:37:29,130 Basically just starts strobing all the, the, the GPIO in a pattern. 321 00:37:29,130 --> 00: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. 322 00:37:34,730 --> 00: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. 323 00:37:48,730 --> 00:37:53,110 You don't have GPIO on the Unix version of Micro, of MicroPython. 324 00:37:53,170 --> 00: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. 325 00:38:02,610 --> 00:38:06,630 And so then you can run it in the Unix port as long as you have those functions available. 326 00:38:08,050 --> 00:38:14,770 You can build MicroPython with SysSetTrace where you get a trace of execution. 327 00:38:14,770 --> 00:38:16,590 It's not quite what you'd expect. 328 00:38:16,590 --> 00:38:18,130 It's not a very detailed trace. 329 00:38:18,130 --> 00:38:25,770 It basically tells you which portion of the bytecode is executing in a very, in a very non-granular fashion. 330 00:38:25,770 --> 00: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. 331 00:38:38,790 --> 00:38:43,290 The, the bytecode itself, this is the Hello World bytecode. 332 00:38:43,290 --> 00:38:47,850 Basically you're loading a global object of print, the print function. 333 00:38:47,850 --> 00:38:51,030 You're loading the Hello Cruel World string. 334 00:38:51,030 --> 00:38:53,090 You're calling the function. 335 00:38:53,150 --> 00:38:55,850 You pop the string off the top of the stack. 336 00:38:55,850 --> 00:38:58,270 You load the global time module. 337 00:38:58,270 --> 00:39:00,330 You load the method sleep from it. 338 00:39:00,330 --> 00:39:05,410 You load a constant integer of five because you want to wait for five seconds. 339 00:39:05,410 --> 00:39:07,990 You sleep for five seconds by calling the method. 340 00:39:08,170 --> 00:39:12,170 You pop the top off of that and then you jump back 17 bytes. 341 00:39:12,430 --> 00:39:14,630 And then you loop around and you do it again. 342 00:39:14,630 --> 00:39:18,630 And so that's the very basics of the language here. 343 00:39:20,730 --> 00: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. 344 00:39:33,030 --> 00: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. 345 00:39:39,490 --> 00: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. 346 00:39:47,490 --> 00:39:51,950 This is not compatible with CPython, but familiarity with that would help. 347 00:39:51,950 --> 00:39:56,870 If you read through some Python bytecode tutorials, you'll understand what this is going. 348 00:40:00,330 --> 00:40:04,390 Conclusions on this, I'd really rather be reversing compiled C than this. 349 00:40:04,590 --> 00: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. 350 00:40:22,030 --> 00:40:25,750 It's just annoying to try to read through this yourself. 351 00:40:25,750 --> 00: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. 352 00:40:34,810 --> 00:40:41,050 And if you're really wanting to mess around with a device that does this, it's doable. 353 00:40:42,490 --> 00:40:48,830 These frozen modules are a great way to include project-specific stuff. 354 00:40:48,870 --> 00: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. 355 00:41:01,430 --> 00:41:05,390 This is not a way to hide or obfuscate code or secrets though. 356 00:41:06,050 --> 00: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? 357 00:41:12,550 --> 00: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. 358 00:41:19,730 --> 00:41:24,650 But if you're like me and you're fun, you make your own fun, right? 359 00:41:24,650 --> 00:41:26,790 You don't really follow the rules on the CTF. 360 00:41:26,790 --> 00:41:29,430 You rather just play around with it and not even score any points. 361 00:41:29,430 --> 00:41:31,370 This is fun for that. 362 00:41:32,830 --> 00:41:36,670 It's only as well protected as access to your firmware. 363 00:41:38,290 --> 00: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. 364 00:41:46,230 --> 00:41:48,050 So it's not a layer of protection. 365 00:41:48,490 --> 00: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. 366 00:41:57,830 --> 00: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, 367 00:42:22,510 --> 00:42:27,750 appropriately named, by Karen Antz and Chris Eagle. 368 00:42:27,750 --> 00:42:30,350 Karen Antz is in our audience here right now. 369 00:42:30,590 --> 00:42:35,850 She's been giving me the eye to mention this the whole time. 370 00:42:36,130 --> 00:42:40,830 She's actually going to be at the No Starch booth this afternoon doing a book signing. 371 00:42:40,830 --> 00:42:43,130 I want to say 3.30? 372 00:42:43,250 --> 00:42:44,410 3.30. 373 00:42:44,410 --> 00:42:47,930 She's going to be at the booth over there at No Starch doing a book signing. 374 00:42:47,930 --> 00:42:50,230 Go buy that book and get her autograph. 375 00:42:50,770 --> 00:42:55,730 And she knows everything about MicroPython, frozen modules as well. 376 00:42:55,730 --> 00:42:58,290 Divert any questions about this, talk to her. 377 00:42:58,590 --> 00:42:59,970 So, no. 378 00:43:00,470 --> 00:43:02,210 Ask me the things. 379 00:43:02,210 --> 00:43:05,850 And so with that, I'm going to close this off. 380 00:43:06,190 --> 00:43:12,150 I appreciate y'all coming to this instead of Pawnee Awards, however poor of a choice that might have been. 381 00:43:12,930 --> 00:43:16,090 And I look forward to interacting with y'all. 382 00:43:16,310 --> 00:43:17,090 Thank you.