start	end	text
1130	6870	Thank you for showing up, everybody. I appreciate this. I know that I'm on opposite of Pony Awards,
6870	13210	so if you're here for Pony Awards, you're in the right place. So this is Reverse Engineering
13970	20270	MicroPython Frozen Modules. My name is Wesley McGrew. I'm Senior Cyber Fellow with Martin
20270	27690	Federal. And this talk is going to be a lot of fun for those of you who like to hack on
27690	36810	badge life stuff. A lot of badge life badges use this Python interpreter. And we're able
36810	45890	to pick out some things that perhaps you wouldn't have otherwise. So the agenda for the talk,
45890	50890	we're going to be, we're going to introduce what MicroPython is. For those of you not
50890	56310	familiar, I'm pretty sure most of you know about Python in general. We're going to look
56310	63070	at MicroPython firmware on the Raspberry Pi Pico. So it's available for a lot of different
63070	68210	microcontrollers, but Pico is probably the easiest for you to set up experimentation in your lab
68210	77370	with. We're going to build a Hello World module for it, and we're going to compile it down into
77370	82530	bytecode, and then we're going to freeze it into the MicroPython firmware itself so that it's
82530	87650	harder for somebody to extract. We're going to talk about that freezing process and why it's
87650	94190	there. We're going to have a hands-on example of reconstructing that module from a compiled
94190	100610	firmware. We're going to do it with symbols and without symbols, so show you how to do it in an
100610	105210	environment where somebody's trying to actively keep you from doing it. We're going to do some
105210	109990	analysis of that extracted module and come to some conclusions about what this means for reverse
109990	118470	engineering it. So MicroPython is an implementation of Python 3 for microcontrollers, and it's very
118470	125170	interesting in that it's not a straight port of CPython. CPython is the Python you're familiar
125170	134390	with on your desktop system where it's an interpreter running bytecode based off of the
134390	142070	Python code that you write. And MicroPython completely just re-implements it from scratch.
143190	148170	There's probably reasons for doing that instead of starting from CPython.
148370	154790	Microcontrollers have limited resources and so things are optimized for that kind of environment.
155330	161170	There's a subset of Python standard libraries in there. There's not libraries for some of the
161170	165110	standard library stuff that doesn't make sense on a microcontroller, but there's also some
165110	172010	additional MicroPython only libraries. They're just for working with microcontroller hardware,
172010	180690	primarily like GPIO, like turn pin one on, pull it low, that sort of thing. There is a REPL, so a
180690	185590	command line you're used to with Python where you can just start typing Python code and have it
185590	190010	execute and sort of play around with the Python environment. You can do that with MicroPython
190010	195070	if you have a serial terminal on your microcontroller like you do with a Pico.
196070	202370	The use cases for it, you don't... I don't see it in anything that you can just like go to Lowe's
202370	209730	and buy for home automation, but you do see it in scientific application, industrial applications.
209730	217910	You see it in badges. And so like the last year's snacky badge at least used MicroPython.
217910	226250	And specifically the frozen modules are used by a lot of folks to have code cooked into the
226250	234450	firmware itself that's not in a module file on the file system. It's really nice. So what I'm
234450	239250	presenting here is not a mistake made with MicroPython. It's just a reverse engineering
239250	246250	talk. There's no vulnerability here as MicroPython is concerned. I like it. It's really easy to
246250	252410	create a prototype of an embedded device with MicroPython. And so you have three choices for
252410	259010	your code. You can have plain text that's on a small file system that's emulated in part of your
259010	264710	flash memory. You can... and you can read and write to that file system through the REPL.
265050	272010	You can have pre-compiled bytecode modules, and we'll talk about the structure of those.
272010	277710	But then you can also take those pre-compiled modules and cook them into the... into the
277710	282330	MicroPython firmware at build time. And that removes a lot of the metadata that makes it
282330	288430	easy to find those modules and disassemble them, right? There's tools for working with the other
288430	295030	portions of this. There's no tools for going back from a frozen module. And so in badge life,
295350	301070	a lot of CTFs that use badges will hold secrets and keys and code that they don't want you to
301070	308050	see as part of the CTF inside those frozen modules. The feature is there for efficiency.
308050	313590	So the feature is there so that you're not wasting additional memory loading dot py or
313590	319550	loading the dot mpy file into memory and doing things when it's already in memory because it's
319550	325690	on the flash. So it saves memory. It's convenient. You don't have to do any additional loads. It's
325690	334610	already there. But it's being abused, misused for obfuscating and hiding code and secrets inside
334610	341790	of these binaries. And we've got some quotes here from random forums and stuff. You probably
341790	346310	can't read them. They're small. I just grabbed a bunch of them and threw them up. But the gist
346310	353510	of all these quotes is people are asking, well, if I've got a password for a system, how do I
353510	358270	keep that away from my users? Or I've got a key. I've got a secret that I don't want them to
358270	365130	read my code. Their concerns are making this obfuscated. And people were responding to them
365130	368810	on these forums of like, you know, it's not perfect. And everybody acknowledges that this
368810	373390	isn't perfect. And it's not perfect, but you can make it harder to get at that stuff by cooking
373390	378830	it into a frozen module because nobody has the tools to disassemble it. And so let's make
378830	385210	something to disassemble it. It's a layer of protection for some folks, but it's really just
385210	390610	an obfuscation. The first thing that you have to do to get this thing working is to get a
390610	396310	firmware image. So that's out of scope for this talk. And it's tricky depending on the module,
396310	400350	depending on your microcontroller. You might not be able to get a flash image easy. That's a
400350	408570	whole talk on, you know, glitching and things like that. But since we're using Pico as the,
408570	415090	we can, we can just use the standard Pico tools for this demonstration. If you're following at
415090	423090	home with a Pico, you use the Pico tool info and it'll tell you about the, about the firmware of
423170	427410	a connected Pico. But it actually makes it a little too easy as you can see from the output
427410	432230	here. From the output, you have a list of frozen modules here. And you're like, well, that's,
432230	438230	that seems dumb. Seems like a really short talk. And what that's from is from Pico's, Pico
438570	445770	has binary information, quote unquote, binary information in the firmware image. And it can
445770	453250	be read with Pico tool. It can be encoded, it's encoded by MicroPython. Firmware can specify in
453250	459690	MicroPython that it's going to include additional fields in that Pico tool or in that binary
459690	467050	information. And MicroPython does this with simply a list of the frozen modules. And so,
467050	472210	again, that seems real easy. You can look at how to parse that from a tool that comes with
472210	478830	MicroPython that we're going to be using a lot here, NPY tool. This is used for freezing
478830	487890	modules, for inspecting NPY files. There's some stuff in there for how it encodes that, that
487890	494350	information. And so since this is too easy as a starting point, I mean, it's too easy because
494350	501150	you can strip this out. It's not necessary for the operation of the firmware, right? So other
501150	507530	platforms, other micro controls may not even have this sort of field, this sort of binary
507530	514590	information in the firmware. It's not used by MicroPython itself, so we can evade this analysis
514590	521030	by commenting out the code in the freezing tool that generates that list, right? And so to make
521030	525490	it harder on ourselves, right? Because we imagine the next step after we start ripping this
525490	530690	stuff out, people are going to start obfuscating this part. So we can rip this out and not have
530690	537930	those frozen modules listed. If you can get to a serial console REPL, you can also do help
537930	543670	modules and list them out. That is probably something that needs to be in there. I don't know
543670	549210	if you could patch out an evasion from, or a countermeasure against that. But that's, but it's
549210	554830	not always the case that you're going to be able to easily get the REPL on a microcontroller.
556610	562970	So for our example, we're going to have the minimum viable Python module here, a hello world
562970	570310	that we're going to demonstrate in plain text. We're going to demonstrate it as a compiled byte
570310	577470	code file on the file system. And then finally as a frozen module compiled into MicroPython and
577470	584370	look at what that looks like in the binary. Plain text is simple. You simply use the MP remote
584370	590550	tool to upload it into the file system on the microcontroller. There's a small, very small
590550	597570	file system that it stores those things in. You can list files on it and you import and go.
598690	607990	You can use the MPY cross compiler to compile your hello world dot PY into an MPY into PY file
607990	612610	and you wind up with the hex dump or you wind up with a file that has the following hex
612610	618230	representation. And so you can kind of see in there, you see our strings, you can see some
618230	623670	byte code in there and so you're like okay, this is a thing. Um, this immediate, doesn't
623670	629970	immediately look to you to be very useful, but the MPY tool can work with this format very well
629970	635550	and you can upload it into the file system and, and use it. And so if you find one on a file
635550	644430	system, use MPY tool to uh, to tinker around with it. As a frozen module, you can take that MPY
644430	650910	and rebuild the microcontroller or firmware from scratch. You can rebuild MicroPython from scratch
650910	658390	including that MPY that you created. It brings it in, it kind of uh, it brings it in as a set of C
658390	663150	structures and we'll talk about that in a little bit, but basically it's hard to go back. After
663150	667410	you've frozen it into the firmware, it's harder to pull it back. It's not just a file on the file
667410	678100	system anymore. But you can use it as a Python module just fine. So what can we learn from an
678100	684360	MPY file? Because our goal eventually here is going to be to take that uh, frozen module,
684360	690700	extract it from the firmware, recreate the MPY so that we can parse it. The MPY tool creates
690700	699660	um, uh, uh, the frozen module from the MPY. Uh, and so the MPYtool.py code has a lot of uh,
699660	706140	uh, algorithms and data structures in it for generating this. And so it does not generate the,
706140	711660	binary that's going to be in the firmware directly. It generates a set of C code that's
711660	717940	going to get statically linked into the MicroPython firmware. So um, and it's less coded and it's just
717940	722860	like a whole lot of data structure, a whole lot of structs, right? And those structs are going to
722860	727900	get compiled in and they're going to get arranged by the compiler into memory in various places that
727900	733940	are not immediately obvious to you from just reading the code. Uh, the same tool can be used
733940	740540	to dump info about an MPY file with a disassemble flag. So it does have a built in disassembler. Uh,
740540	745560	this is the only disassembler you get with MicroPython and it's only pulling from MPY files.
745560	751700	So if we get code out of the binary, we're going to need to disassemble it with this tool. And by
751700	759860	the way, the bytecode for MicroPython is not the same bytecode as CPython. It's definitely not the
759860	766000	same opcodes. It's many of the same, uh, it's many of the same opcodes with different, different values
766000	772000	and uh, not all of the same opcodes, right? So it's a from scratch implementation.
773820	781960	If we dump or disassemble an MPY file using the MPY tool, we get three main
781960	789840	things out of it. We get a string table, a module, a string table, an object table, and a set of code.
790740	794140	What you're looking at here is the string table and I'm going to talk to you about this,
794140	798480	this, um, intern string format that they have here.
799960	805420	For the code, there's two pieces of code. If you remember, we had a module that defined,
805420	811080	we defined a module and then we defined a go function in it or a hello world function in it.
811400	817340	And, um, so you're going to have two functions that have bytecode. The first one is going to
817340	822400	be a constructor and the next one is going to be your go function that actually repeats hello
822400	828420	world every, uh, five seconds. And you can see the, the bytecode in these and it's a stack-based
828420	833760	language. Basically you're, you're, you're constantly loading things out of different
833760	838700	data structures, putting them on the stack, calling other functions, pushing and popping
838700	845340	and looping around and doing things like that. And so again, if you read up on how a Python bytecode
845340	852000	works, it would make sense to you, but, uh, it's not exactly the same. So you can't use like the
852000	859600	body of Python disassembly tools on this or debugging tools for this. There's a really nice
859600	864800	colored output hex dump that you can get within PY tool that will sort of tell you what the
864800	868740	different parts of that hex dump mean and what, what, what they are, what they have, what they're
868740	877160	about. That'll help us when we're trying to recreate one of these. So if we can rebuild an
877160	881540	MPY of a frozen module out of information we extract from firmware, we've got something we
881540	886680	you can use to, to, to get some work done with it. Uh, it'll take some RE work, but it's not
886680	891800	because it's not there in a contiguous binary blob. It's structures we've got to walk, right?
891800	898300	We've got to find them in memory and walk them. Uh, we have to understand string interning. We
898300	902880	need to look at the byte, byte code briefly. It's just not going to be a byte code class or
902880	909420	anything. Um, but also another tool that we can use is we can build a version of MicroPython
909420	914760	that's the Unix port, quote unquote Unix port. You can build it for, for Windows, Linux or
914760	920940	whatever, but it's basically a desktop version of MicroPython, uh, that you can, that you can
920940	925940	build and load MPY files into, run them, analyze them and do things to where you're not just
925940	930320	restricted to working on the microcontroller itself when you're doing your reverse engineering.
931300	938140	So first off, uh, how we're storing our string constants. We're doing what's called string
938140	945300	interning. And so this is something that you do on very limited memory environments where you
945300	952180	want to avoid having the same constant string in memory more than once, right? You don't want to
952180	957960	have, uh, main or, or something like, or some other common string 50 different times in your
957960	962940	memory if your memory is in on the order of megabytes or kilobytes or something like that,
962940	972340	right? And so MicroPython, MicroPython calls these Q strings for unique, for unique strings.
972900	979540	Um, and basically Q string values, the way you refer to them is, is into an index, into a linked
979540	987580	list of pools of these strings, right? And so if you're wanting to refer to hello cruel world,
987580	996080	or you're wanting to refer to, uh, the, the, uh, the open tag module closed tag, uh, string,
996080	1001080	you're never going to refer to it as a pointer or you'll get a pointer to it, but you're going
1001080	1007460	to refer to it as, and pass it around as an index into these linked, linked pools.
1008160	1013900	And so there's a link, there's a set of static ones that are in the makeQStringData.py that's
1013900	1020300	used in the build process of MicroPython. Um, and those are constant across all your builds of
1020300	1026540	MicroPython. And then past that in the next pool, it's going to be stuff that's, varies between each
1026540	1035080	build depending on which frozen modules you include. So if you use NPY tool to create frozen
1035080	1042360	code generated from your NPY file, uh, it's going to create a strings, a string data structure.
1042360	1047100	And so first you've got an enum that has, uh, basically, you know, that's going to compile
1047100	1053840	down to one, two, three, four, basically a indexing hello cruel world, go hello world.py,
1053840	1058700	sleep time, all of those sorts of things. Then you're going to have hashes for them,
1058700	1064620	like a simple one byte hash, and then a link for each of them. Those are going to be placed
1064620	1071940	into pools. And so on the right hand side there, you see the frozen constant pool, uh, that, uh,
1071940	1077420	has, uh, has those five strings represented in there. There's also going to be a link to the
1077420	1087620	previous pool and also a previous pool size. So your QString indexes are global. They're globally
1087620	1093420	unique. And so you have to look at that previous pool size to know where the indexes start for
1093420	1099360	this pool. All the pools are linked together. The first constant pool starts at zero and it goes to
1099360	1105140	however many constants there are. And then the frozen pool, if it's next, starts at that number
1105140	1114940	plus one, plus two, plus three, plus four, plus five. So, um, and so this is another, this is
1114940	1122260	the, the QString table data, uh, here. So now you've got those, uh, these defined constants
1122260	1127160	for these. And so this is how you refer to the QStrings that you have. These are all your indexes
1127160	1136210	defined as constants. So the question is, how do we find the compiled module in a compiled firmware?
1136210	1140810	Now, if you're lucky, they built it with symbols, right? But we're not going to assume we're lucky
1140810	1144270	here. We're going to assume they stripped out that binary information. We're going to assume that
1144270	1150650	they stripped out the, uh, the symbols. And so for me, whatever, whenever I see somebody
1151210	1157510	in compiled code that I'm reverse engineering, and they're using, uh, an open source module,
1157510	1162950	like if in malware, if they're using OpenSSL or they're using, um, uh, something similar,
1162950	1170310	I'll try to build a copy of that SSL library myself of almost as close of a version as I can
1170310	1175790	get, and as close to the same options as I can figure for theirs, so that I can then use that
1175790	1181690	as sort of a Rosetta Stone for their module that doesn't have symbols, right? Because there's data
1181690	1187390	structures that I want to walk through there. And so the, one of the best things that you can do
1189290	1195410	is build a debug build of MicroPython that has a frozen module in it, not necessarily,
1195410	1198910	you don't have their frozen module, but it has a frozen module in it,
1198910	1205770	and use that to get your bearings in the binary, right? Uh, have it load at the same address and,
1205770	1210190	that sort of thing. And then you can have two copies of Ghidra up, up side by side,
1210190	1214410	and use one to figure out where the data structures are in the other.
1216070	1221650	And so in the debug build, it's called mpFrozenNames. The mpFrozenNames is a
1221650	1231590	list of the frozen modules on the system. In your, um, in your, uh, uh, uh, debug build,
1231590	1239490	you also have the frozen modules themselves, mpFrozenNpyContent. So you've got, um, there's
1239630	1245470	a lot of sort of built-in frozen modules. Uh, you'll see, um, like if you have an Adafruit
1246090	1252450	CircuitPython, uh, device, CircuitPython is based on MicroPython. So, uh, CircuitPython
1252450	1257090	devices will have other frozen modules in them, and you may not necessarily have the source code
1257090	1263910	to them, and they'll be in this list. Uh, uh, I think Lego makes a, uh, a device that has a
1263910	1269430	microcontroller in it that has MicroPython or CircuitPython on it, and they also have some
1269430	1275430	of their own closed source libraries that are, that are in this as well. And so this is easy
1275430	1280590	mode, right? So we found the list of names of modules. We found pointers to the modules
1280590	1289230	themselves, but we don't have the names of frozen names and frozen MPY content in our stripped
1289230	1295170	binary. So we have a stripped binary that we copied from a microcontroller, or we got from
1295170	1300730	an over-the-air update or something like that. We've got to find them in a non-debug build,
1300730	1305790	right? And so the way we can find them, if we, if we can ever get to the same things,
1305790	1311590	then we can start using those C data structures from the C files that these things, that from
1311590	1317150	the source code of MicroPython, the source code of other frozen modules, we can start using those
1317150	1322470	to sort of walk it and figure out what's different. But we've got to find those data structures first.
1323150	1330610	The easiest way to find these two data structures in a non-debug build is to, is to, uh, one of the
1330610	1337570	easiest ways for this is to just eyeball it. You can search for .py and just find sort of the list
1337570	1343230	of constants that have .py, especially if you know the other frozen modules and the other frozen
1343230	1348630	modules are consistent with the standard MicroPython ones. You can compare that list and
1348630	1354930	then whatever's additional on there are other additional frozen MicroPython modules. Another,
1355190	1360350	a little bit more, uh, resilient way of finding that that doesn't depend on you just sort of
1360350	1367190	eyeballing a binary, which I don't mind doing all day, but y'all might, uh, is to find it as a code
1367190	1375090	reference. And so there's a function that uses that list called mpfindFrozenModule and chances
1375090	1384910	are it's going to build in roughly the same location in your debug build as in your, uh,
1384910	1393350	right here. So you have this move, load, uh, this move, load, store combination here using this,
1393350	1398090	uh, address. That address is the address to the mpFrozenNames.
1400250	1405130	To find the frozen module content, there's all, there, you can't really eyeball it because it's
1405130	1411630	all binary content. Uh, but there's, uh, indirect reference in, in that same mod, in that same
1411630	1417350	function. So if you find those frozen names, eyeballing them, you can do a cross reference
1417350	1424810	in Ghidra to walk back to where mpfindFrozenModule is and say, okay, that matches up with the debug
1424810	1430530	build. Now, later on in that function, there's a bit of code that looks like this. This is a
1430530	1435830	reference to the frozen MPY content. And so now we have pointers to our mod, to the, to the base
1435830	1441230	data structures of our modules. So from here, it should be fairly simple work, just sort of
1441230	1445790	working through those structures and pulling it out. And what we want to do is we want to
1445790	1452570	recreate the hex of that original MPY file so that the rest of the work can be done with MPY tool.
1455380	1461640	The header for the MPY file is, is, uh, going to be the same. Usually you can just take that
1461640	1466180	from any of them and it's not super important as long as you get something that it passes the
1466180	1473380	checks and it goes. It starts with magic letter M, it goes with a major micropython version.
1474700	1480160	You want to have, you can have, so there's a flag in there for, or, or, and you can have it
1480160	1488640	per function, whether it's native or non-native bytecode. So you can have, you can have an MPY
1488640	1497580	module where the actual code of a function is native, say ARM or MIPS or whatever, or, uh, uh,
1497580	1504580	byte, like not bytecode, machine code, right? Now for my money, that would be a lot easier because
1504580	1510340	I can just, if I get a pointer to that, like Gaedra will, will decom, disassemble and decompile
1510340	1515820	that for me. And I'm, and I'm off to the races. I don't have to interpret bytecode. So we're going
1515820	1520540	with what I think is the harder example is if it's actual Python bytecode there.
1522740	1530940	And then, um, there's a data type that comes up frequently in this called a variably in,
1531360	1537740	a variably encoded unsigned integer, a VUINT. And this main, I don't know, maybe this is more
1537740	1543240	common in microcontrollers than, than I know, but, uh, but I had to take a close look at this.
1543240	1552940	And basically this is a variable length integer that for each byte, the lower seven bits is,
1552940	1558940	uh, is part of the integer. And if there are more bits, then the eighth bit is set. And so
1558940	1563240	if the eighth bit is set, then, you know, okay, I got to read another seven bits from the next
1563240	1568380	byte and so on and so forth. And it can be arbitrarily long, right? And, and have a
1568380	1574040	represent an arbitrarily large number, however much makes sense on your microcontroller.
1574160	1579540	And so this is a data type that, that MicroPython uses in a lot of its data structures.
1579600	1583480	And it can be a little bit of a mind bender to, to work with.
1586200	1593160	So in the hello world dot mpy, we have a number of the, the intern strings that we want to have
1593160	1600560	in there. Uh, and we'll see in there, we only, we only store the actual string data for strings
1600560	1606560	that aren't already in the constant pool. So if something's already built into MicroPython,
1606560	1611040	we don't redefine that string. We just make a reference to it. And so when we're creating our
1611040	1617460	mpy file, we have to be mindful of which, which strings are new, essentially, which strings are
1617460	1627550	what pool they come from. So frozen module key strings get mixed together in, um, in a, in a,
1627550	1635650	in a mpy key string frozen constant pool, right? And so that's one of a few different pools in
1635650	1641190	there that are all in a linked list where across the linked list, there's unique indexes for each
1641190	1653130	string, right? So, um, so to find, to find that pool, the easiest thing to do is to go through,
1653130	1657410	if you can find the main function, which you'd have if you're new to reverse engineering,
1657410	1662230	maybe the tough part, but if you can kind of trace through the very beginnings of execution
1662230	1669310	of that binary, if you can find main, there's mpy init and then key string init. And in the
1669310	1674990	first four instructions in there, there is a reference to the key string frozen constant pool.
1675670	1680150	Uh, you can search for a string that you know is part of that module. Like if you've just run
1680150	1685410	strings on the binary, you'll have seen some of it and you can just do cross, cross references
1685410	1693190	back from that if you really want to. So, so if we're going to parse these pools, we got to look
1693190	1699290	at the data type for it. So a pool has a previous pointer to the previous pool in this linked
1699290	1706450	list of pools. Uh, you can move backwards in the pools, but not forwards. And so, uh, that's why
1706450	1712470	that first, uh, those first instructions in that key string init refer to the frozen constant pool
1712470	1718050	because that's going to be the last one probably. Uh, there's a total previous length that you use
1718050	1724230	to figure out where the indexes start in this pool. Uh, there's a few other fields that don't
1724230	1730150	make a whole lot of difference, but at the end you have a list of hash values for the strings.
1730250	1735190	Uh, you have a list of lengths for the strings, and then you have a list of the data, right? And so
1735190	1740970	the string data is just going to be every string catenated together solidly with nothing in between
1740970	1747990	because that lengths field in the previous, uh, the previous pointer or that, that list will tell
1747990	1752830	you the lengths of each of them. And so you have to parse both of those arrays together to figure
1752830	1763920	out, uh, where, what each string is. So with, uh, the previous pool is, the previous pool to the frozen
1763920	1771680	pool is the npq string constant pool. The total previous length, uh, in our example is 3c2,
1771680	1779120	so that's where the indexes start for the frozen modules. So any q string index, uh, greater than,
1779120	1785400	so any q string greater than 962 is going to be in our frozen pool, which means that when we
1785400	1791260	recreate our mpy, we're going to have to include that data into our mpy. Uh, anything less than
1791260	1799690	that, we can just include the number and not re-encode the string. And so there's our list
1799690	1806470	of indexes, and so there's a constant string, q string table data, uh, for hello world that you
1806470	1812950	can locate through there, and that's the indexes. And so that first one, uh, uh, that first one,
1812950	1818810	since it's greater than 962, we're going to have to encode that string hello world.py into our mpy
1818810	1825530	file. But 7, which is just like the open tag module close tag, is already in the micropython
1825530	1832170	binary, so we just include 7 and we don't re-encode it. And so this is how it looks when we re-encode
1832170	1837670	it. Uh, this is, so we've already written the header, which is those first handful of bytes.
1837670	1844710	The highlighted portion is the string table. Uh, there's a vunt where basically it defines
1844710	1850950	whether or not it's a static entry or one that's defined in this mpy. So are we providing an index
1850950	1858430	or are we providing a new string? And then the string follows. And that's why you see in that
1858430	1864970	dump, you see, uh, the string data for the new strings, but not the built-in strings.
1869990	1876390	So the next thing that you have to do is to, uh, build the object table. Now this is a more
1876390	1883130	complex data structure, and especially as you get further from hello world, this is going to
1883130	1888910	get more and more complicated. The more they're going to, the more they're using, uh, Python
1888910	1896110	objects, the, the, the harder this part's going to become. But you can, like, follow through and,
1896110	1901430	uh, and recreate these as well. Copy, really what you can do is you can copy these almost
1901430	1906070	straight out of memory and put them into your binary and it'll just work without you necessarily
1906070	1911930	having to understand them at first. Then once you get this thing fully recreated, you can load it
1911930	1918590	into that Unix MicroPython interpreter and, uh, and start probing those objects from the REPL,
1918590	1930390	which is much easier. And then finally, the byte code. So, oh, um, there's a format that for this
1930390	1936310	code that we have to generate. If you look in mpytool.py's code, there's a function called
1936310	1942570	read raw code that sort of hints to you as to how we need to build this byte code so that it can
1942570	1950050	read it. Um, so the pointer to the code is the third field of that module, and so you see the
1950050	1956670	frozen module, hello world there, and you see the QString table, the object table, and then you see
1956670	1962090	the pointer to the first element of code, and that's for the prototype function for this object,
1962090	1971530	right? And so each, each function, each, sorry, each piece of byte code can have children. So
1972790	1979970	the prototype for this module is going to have a child of the go function that's inside of
1979970	1987190	this module. And so it's going to contiguous, or continuously rather, or parse these elements of
1987190	1993470	code, see whether they have children or not, and recursively follow down. And once you've
1993470	1998650	parsed everything that doesn't have children, then you can walk back up that recursion and you've
1998650	2006470	parsed all the byte code out. And so on the left there, you see the byte code for, um, for the,
2006470	2012570	for the constructor, and on the right you see the byte code for the go function, which prints
2012570	2018250	hello world in a loop, basically. And so one is the child of the other, essentially.
2023710	2029350	So reconstructing this, uh, there are VUints for the kind, the length, and whether or not the code
2029350	2034010	has children. So when we're putting this into our MPY binary, it needs to know how to reconstruct
2034010	2040010	this into memory in a useful way for the interpreter. And so it's using those VUints to, uh, to do this.
2040010	2045990	And so there's a kind, whether it's byte code or native code. Again, these things can be compiled
2045990	2050970	down to where it's native ARM code for a function. And like I said, if that's the case, if you see
2050970	2056930	something encoded like that, just, just drop your cursor and Ghidra or Idapro on that, hit
2056930	2061930	disassemble, and you're off to the races, right? You can, you can read what it's doing a little
2061930	2067110	bit easier. But if, if it's not, it's just, if it's byte code, Ghidra doesn't have a decoder
2067110	2072690	for this, unless you want to write one. I didn't want to, uh, uh, uh, Karo back there probably will.
2072950	2080630	Oh, uh, uh, and, uh, but, uh, but if you don't want to write a module to, to, to interpret this
2080630	2085650	byte code, uh, you're, you're going to be left to pulling it out and dropping it in this file
2085650	2092730	so the MPY tool can do this for us. Uh, the third, the third bit, is there a child for this block or
2092730	2100950	not, right? And so that makes it recursively go through and read more code. So the one big problem
2100950	2106270	with this, and, uh, I wanted to write a tool that would just sort of walk through a binary and do
2106270	2115550	this, uh, but it's actually tricky to do because the length of the code is not referenced anywhere,
2115550	2121570	right? The length of the code is defined by the fact that the code never exists, never steps
2121570	2127110	outside of the bounds of its memory locations. So there's no length, it just starts execution
2127110	2131930	there, and it'll just keep going as long as there's code, and as long as it doesn't return,
2131930	2136430	and as long as it's looping around, it'll just sort of loop in that space. So there's no defined
2136430	2140130	length for it, which, I don't know, maybe you could figure out a problem there, maybe you could
2140490	2145550	exploit that in some way. But there's no length, and so it's hard in an automated fashion without
2145550	2150810	parsing the byte code to know the length of it. And so you may have to eyeball that part of it.
2150810	2157910	Or parse the byte code yourself, kind of trial and error. Each block of code is filed recursively
2157910	2165230	by its own children. And so here you see in the first screenshot, we're encoding those integers
2165230	2176670	and then plopping in the byte code for the constructor for the module. And then next,
2176670	2184830	it has a child of the go function, and we go and parse that out, we put the vunts for it in there,
2184830	2189970	and then we put the byte code in for the go function. Once you think you've created,
2189970	2195790	and at this point, like if you take these slides and you go back, you'll see that this is bit for
2195790	2204710	bit identical to the original mpy file for the hello world.mpy. We can use my Unix port to
2204710	2212390	examine this. So we can load up the Unix port of MicroPython, import that mpy file, start looking
2212390	2217570	at the objects, start looking at the modules, do the dict function to see what functions are
2217570	2226690	available in there. We can call them. There is a tracing tool in, actually this is not,
2226690	2231750	the tracing tool is in the next slide. This is another example that you can just sort of eyeball
2231750	2240710	on your own of a, of a, this, this code right here will make a little light show on all the GPIO
2240710	2249130	pins of a, of a, of a Pico. Basically just starts strobing all the, the, the GPIO in a pattern.
2249130	2254730	If you have a dev board that has LEDs on all the pins, it'll do a little neat thing for you.
2254730	2262290	That's the code that, this is the, this is the code that does this, but since it's doing GPIO,
2263490	2268610	it won't work on the Unix MicroPython because it's doing like the pin function and stuff like
2268610	2274210	that. You don't have GPIO on the Unix version of Micro, of MicroPython. So what you need to do is
2274210	2280470	like if, if the code you pull out uses a function that's not there, you're going to have to implement
2280470	2285050	some sort of stand, stand in there. And so then you can run it in the Unix port as long as you
2285050	2292790	have those functions available. You can build MicroPython with SysSetTrace where you get a
2292790	2298570	trace of execution. It's not quite what you'd expect. It's not a very detailed trace. It
2298570	2305310	basically tells you which portion of the bytecode is executing in a very, in a very non-granular
2305310	2310830	fashion. It just basically tells you which function is executing when, but you have to build,
2310830	2316610	you have to define this when you're building your MicroPython binary or your MicroPython Unix port.
2318790	2325210	The, the bytecode itself, this is the Hello World bytecode. Basically you're loading a global
2325210	2331430	object of print, the print function. You're loading the Hello Cruel World string. You're
2331430	2337610	calling the function. You pop the string off the top of the stack. You load the global time
2337610	2344190	module. You load the method sleep from it. You load a constant integer of five because you want
2344190	2349090	to wait for five seconds. You sleep for five seconds by calling the method. You pop the top
2349090	2354830	off of that and then you jump back 17 bytes. And then you loop around and you do it again. And so
2354830	2364190	that's the very basics of the language here. If you want to learn this bytecode, if you really
2364190	2368650	want to take the time to step through bytecode of this and not just sort of interactively do it,
2368650	2375710	you'll want to read through the MPY tool opcode class. And also, and in class in terms of like
2375710	2382230	the class definition, not like a class that you sit down and learn. There's a vm.c in MicroPython
2382230	2388070	that has the vm interpreter for this bytecode in there that you can read as well. This is not
2388070	2393630	compatible with CPython, but familiarity with that would help. If you read through some Python
2393630	2402170	bytecode tutorials, you'll understand what this is going. Conclusions on this, I'd really rather
2402170	2410190	be reversing compiled C than this. Using MicroPython and creating bytecode out of your
2410190	2419210	functions and stuff is probably almost a better obfuscation of your code than compiling a C
2419210	2425750	firmware for your microcontroller. It's just annoying to try to read through this yourself.
2425750	2431910	If you're more used to reading ARM assembly, if you're more used to reading decompiled C,
2432630	2439830	it's doable though. And if you're really wanting to mess around with a device that does this,
2439830	2448830	it's doable. These frozen modules are a great way to include project-specific stuff.
2448870	2453130	Really the last stage of your development and deployment of a MicroPython firmware
2453130	2458710	should be to put as many of your modules into this form as possible because it's just going
2458710	2465390	to save memory and execution time. This is not a way to hide or obfuscate code or secrets though.
2466050	2471290	Don't use it for that, especially now that this talk is out and this room of people know how to
2471290	2477870	do it, right? So don't use this to go screw around with other people's CTFs in ways that
2477870	2484050	ruin the fun for other people. But if you're like me and you're fun, you make your own fun,
2484050	2488130	right? You don't really follow the rules on the CTF. You rather just play around with it and not
2488130	2495830	even score any points. This is fun for that. It's only as well protected as access to your
2495830	2502950	firmware. And so if you can dump from Flash, if you can dump from the REPL, if you can grab over
2502950	2508790	the air updates for things, you can get at this stuff. So it's not a layer of protection. And you
2508790	2513950	could probably automate this extraction process, but it's a bit tricky to figure out the lengths
2513950	2524570	of some of those data structures too. So one thing that you should do if you are interested
2524570	2532530	in this sort of stuff, if you like the idea of learning how to use Ghidra to step through all
2532530	2538010	these data structures, if you want to learn how to sort of navigate around code in Ghidra,
2538010	2544410	there's a great book from No Starch Press called The Ghidra Book, appropriately named,
2545050	2551490	by Karen Antz and Chris Eagle. Karen Antz is in our audience here right now. She's been giving
2551490	2558330	me the eye to mention this the whole time. She's actually going to be at the No Starch booth this
2558330	2566310	afternoon doing a book signing. I want to say 3.30? 3.30. She's going to be at the booth over
2566310	2572650	there at No Starch doing a book signing. Go buy that book and get her autograph. And she knows
2572650	2577530	everything about MicroPython, frozen modules as well. Divert any questions about this, talk to
2577530	2587050	her. So, no. Ask me the things. And so with that, I'm going to close this off. I appreciate y'all
2587050	2594290	coming to this instead of Pawnee Awards, however poor of a choice that might have been. And I look
2594290	2597090	forward to interacting with y'all. Thank you.
