Hey, thanks everybody for showing up so early. So, yeah, I talk about extensor-based Qualcomm Wi-Fi chips and so why even looking at Wi-Fi chips? So, my opinion, these Wi-Fi chips are pretty powerful chips, so even though they are just intended to be to run, to do your Wi-Fi stuff, right, they are still general purpose CPUs, so you in theory can run any software on it, but the problem is the proprietary binaries which come with those chips, they make it difficult to run your own code. And why would we even want to run our own code? So, what we could do is enable additional functionality, I have a few examples in a few seconds, and we could also make security research easier. So, in case we manage to enable dynamic analysis, security researchers have it easier to identify bugs and report them to the vendors. So, some examples for additional features which we want to enable is, for example, we can enable monitor mode on chips which is not available by default. For example, this is monitor mode on a Qualcomm-based chip running on a Nexus 5. Which is not available by default, and then you can use your standard Wi-Fi tools like AeroDump and so on. We could also build a complete state machine for Berkeley packet filtering inside the firmware, and then compile our rules for packet filtering on the user space, upload them via the kernel to the firmware, and run them directly into firmware. So, that way we save some power when we do monitor mode, basically. And if we have access to even deeper layer firmware code, we can also implement stuff which is closer to the physical layer of Wi-Fi. So, here's an example of a pilot tone jammer, which only jams the pilot tone of OFDM to be very power efficient. So, now that we have all these great goals in mind, so let's take a few steps back and look at the bigger picture. So, in general, there are two kinds of Wi-Fi chips, so, FullMEC and SoftMEC chips. We will look at FullMEC chips in this talk. The biggest difference is that FullMEC chips run the MEC layer inside the firmware. This means, if you want to change something in the VMware or the MEC layer, you have to to change the F dried web func to the F persisting. The most frequent toi so those it's not possible to do these changes on the driver itself so the examples I have shown in the previous slides of stuff which we could want to enable this was based on on the the next one framework so this was I developed this or together as a colleague of mine again my time at the University so this enables us to so this framework will be used to modify procom chips and we even managed to get deep modifications working as this this Gemma I should earlier and you can you can get it at next month at org and apart from procom there was also work done on bracket 2020 about Intel based chips and there was also a talk about Hexabon hexagon based Qualcomm chips on Defcon 27 and bracket 2019 but this talk is the first talk to explain extensor based Qualcomm Wi-Fi firmware so some background how to do how our Wi-Fi socks structured so normally you have an application core and then you separately you have your Wi-Fi core so and optionally in some cases you also have like a real-time core which handles like time critical stuff like the distributed coordination function in Wi-Fi um and now for the the chip which we will look at um there are like several drivers um available so uh there's adh 10k by Qualcomm directly and also a corresponding firmware with that and then there's adh 10k CT by candela tech so this is interesting so candela tech bought the rights from Qualcomm a time ago some time ago and they have their own driver and their own firmware so they got the source code somehow from from Qualcomm and added additional features and so basically you have a complete separate kind of driver and firmware in addition to the original Qualcomm driver and firmware and lastly there's also the QCL QCA CLT driver by Qualcomm this is used for a factory processes and also what you can do is you can use the candela tech driver and run the qualcomm firmware that is also possible so this is the chip and the board I have looked at it's the IPQ 4019 it's based on a development board by eight devices this board is called habanero and in the middle of it you can see the the the jpr we are interested in and this is basically a development board for wi-fi enabled home routers so um what does this port so this chip look like from the inside so as i said this is um used for for wi-fi enabled home routers and one of the big vendors in germany is also using it so this is avm the fritz box and apart from this the chip consists of as i explained before also of an application core multiple wi-fi cores and the application core runs on openwrt in this case with a pretty old kernel and for wi-fi they have one core for 2.4 gigahertz and one for five gigahertz and they use a pcie to communicate with each other so now to the firmware itself as i said it's a extensa based firmware extensa is initially was initially developed by tensilica but now it was bought for by by cadence and it's on a little indian firmware it comes it consists of a rom part and a ram part the ram part of this firmware is stored in the file system of the open wrt and contains multiple segments one that later and it's lc77 compressed the rom can be patched and there is also a code swap mechanism which allows you to uh which allows the wi-fi core to evict code from its own memory space to the host memory space and of course there is no security enabled by default so no secure put no stack can raise no addressing randomization um what's also nice is that there is already comes this debug.fs which you can compile into your driver and this allows you to get direct memory access via this mem value file so you can directly read and write memory directly into the chip. There is also a debug mask which is very useful which you can set for example during initialization of the kernel module to increase the verbosity of the driver itself. Also the interfaces which are used between basically the application core and the Wi-Fi core, so there are two interfaces, the PMI interface, the bootloader management and messaging interface is used during boot up and it's implemented in ROM so basically during boot up this is used to start up the chip, then after boot up the WMI is used to communicate with the chip and this is mostly done to send a message to the driver and commands like please now start Wi-Fi scanning, do channel configuration and stuff like that. So this is how the loading looks like, so the driver offers two methods for loading firmware either via the PMI method or via a copy engine and because in our case we have a compressed firmware we need to use the PMI method and what's basically happening is that the firmware from the file system is being sent to the driver and then the firmware is passed through the driver via PMI to the Wi-Fi core and is then uncompressed on the Wi-Fi core itself. So this is the file structure of the firmware file, so basically there are two big parts, like this upper one and this lower one, they are identified with an IE header and then after this there is the segment header and this is identified with these maps. So if you look at the magic values here, it will tell you if it's compressed or not and on which address it should be loaded into the chip and it will also tell you what's the decompressed size of this segment. And if you notice already there are in this case the first part and the second part have the same address, so this means that they get overwritten. And then apart from the segment header there is also the submitter data as I just explained and then basically after this the real data starts and then it's compressed and after this the next metadata starts in the next part of the firmware data, which is also compressed. So basically why these two separate parts is, my assumption is that the first part is just needed for boot up and you can also see it's much smaller in size than the second part. And after this we can basically override it with the real firmware code. And if you look at the driver logs you will see this whole process being done twice and I assume this is done to load up the first Wi-Fi core and the second Wi-Fi core. If you look at the memory layout we can see that it's repeating. So if you just do a long enough read of your address space you will see these patterns and my assumption is that this is used to realize different memory access rights. So you can have the same memory on different offsets and depending on offset you can realize different access rights. Then to the extensor architecture, so there are two major things I want to point out. About this architecture, which were new to me and which also came with some problems. So the first one is the use of literal pools. So each load instruction, so this is just a load instruction, instead of being PC dependent it's basically independent of that and you just calculate an offset to a fixed literal base to get an immediate value. And in addition to that there is also a lot of other things that you can do. There are also like windowed registers and so this means that, so that way I explain it in a second, but what it's used for is basically to not needing to store and to save and restore your registers when you call a function. So here in this example you can, it's using call eight, so there can be different values here. Four or twelve or whatever. But my firmware used call eight and if you load a value in A10 and you call a function it will be available in A2 of the callee. Okay, so this is how these window registers look like. So basically you have these overlapping sections in your memory space and you have like way more registers than you need for one function and that way they can just shift the window for each function call if you have nested functions and they can basically just rename it and make it accessible to the next function. This is how literal pools work. So now the memory space is horizontal. So assuming we have a function myPatch which wants to call a function wlanMain. So here we, this will also be, so the assembler for this will look like this first load instruction which loads intermediate value into a register and then the call is just using this register to call the function. And if we want to get this immediate value we need to know the literal base. And then we can look up the offset for this function we want to call, here wlanMain in this case and then we get the actual address of the function. So for example here. And this is done for each load instruction. So this means, what's also the case is that this literal base needs to be set up somewhere and this is usually done at the very beginning of the firmware and this is a fixed value so for me it was x48001 and this is the code how it looks like. So you basically have the start of this literal pool and its size and then you store it into A2 and write this to the special register, the base. And so actually the offsets are negative. This is why you add the whole size to it and then a load instruction later on will have a negative offset. So basically the existing firmware does expect that this literal pool is used so if we want and this has some, this comes with some problems if you want to patch existing code. Another problem was that this lit base is not supported by disassemblers. So for example IDA77 added support for extensa but has, does ignore the lit base call. For Chitra there is an extensa plugin but it didn't support a lit base either. Radar2 ignored lit base, has support for extensa and there's also a binary ninja plugin but it ignored lit base as well. So what I need to do is create some patches for binary ninja in this case. So in this patch I just ignore the dependency of the program counter. And directly use this fixed lit base. And I have a similar, so this is the patch for binary ninja. And there's also, this is the patch for Chitra which works the same way. And I have a link to a git repository later on where you can find these patches. So now if you actually want to change some code in the firmware I decided to use this. The next one framework I have shown earlier. And I wanted to basically modify it to support qualcomm based wifi chips. And so this is how the framework looks like. It's pretty complicated but we only need a subset of this to make it work. So what we could do with the original next one was we could extract the ROM, flash patches. The RAM, U code for qualcomm based wifi chips. And we could use C to write our patches. And of course we could call existing firmware functions. And then in the end with this whole framework we were able to create a firmware file. Which we could then run on qualcomm based wifi chips. So how does this next one framework work on a high level? So we have our own patches which are in patch.com. We have a wrapper.c which is used to have some stubs for the functions which are already existing in the firmware. And we use gcc and a gcc plugin to compile the .o files. And also the plugin will help to create this next one .pre file. And this next one .pre file basically contains some metadata which we use later on. And for example. We can use this file with an .org script to create some linker scripts. And then we can use the linker scripts and the .o file to create an overall patch file. So this patch file basically includes everything which we need. And now after this we can use another .make file and the .elf file. To create basically a blueprint to copy out the relevant section of this .elf file. So we might not need everything. Just the patch we want to introduce. And some stuff we might want to override in the original function. And the original firmware. So now we have this .elf file and this .make file. Which is basically the blueprint for object copy. We can copy out the relevant sections. Then we can DD them into the firmware. The firmware binary. So this is how the original next one worked. So I need to do some changes to make it work for Qualcomm. So first of all there was no support for decompression. The Qualcomm based firmware had no compression. It was just the whole memory. The whole RAM of the chip basically uncompressed. It also needed to add support for multiple binaries. So as we have seen earlier. The firmware consists of multiple segments. And I needed to add support for that. And we also needed support for the lid base I mentioned earlier. And then if we make this happen. What we could do is compile and link our own patches. We could patch the second segment I showed earlier. And then we can compress it back into the second segment. Add some padding bytes. And then write the firmware file basically. So this is the plan. So I will not explain the decompression. This is pretty straight forward to implement. I will start with the support for multiple binaries. So I needed to extend the GCC plugin. Which creates this metadata file. The next one.prefile. So basically. What this GCC plugin does. It looks for this attribute in the source code. And this attribute tells basically where the following. The code after this goes into the firmware. In which chip this is used. And in which firmware version this is used. And I extended this basically to also include the target firmware file. So after compiling where should this go to. So which segment of the firmware file should this go into. So basically then after we compile everything. We have this new next one.prefile. And it basically contains the address which is here. Where our code should go. It has basically the type. So here it is a patch. Then if it is a dummy. It is basically an existing function in the firmware. The next column is the .o file. But this will be compiled into. This is the name of the text section in the .l file. And now we also added this. The name of this bin file. Where the compiled code should later on be copied into VRDD. So with this I already wanted to compile some patches. So my goal was to. Use this pretty simple patch. So just jump into some code. Into this function. And then write 1, 2, 3, 4. Basically into this memory location. And then jump to the original code. Which is wlan main in this case. I could use the ESP32 GCC compiler. To compile my stuff. And load it into the chip. And I used the debugfs. Which I have shown earlier. To check basically after the chip was up. If the memory address has changed accordingly. But this didn't work. And the reason is. This LID base. This LID base I have explained earlier. So basically all the load instructions. Expect this LID base to be set at the very beginning. Of the firmware code. So this means like. The chip which runs the existing firmware code. Expects a load to. To all notice this LID base. And I need basically now some way. To tell my code. Either like. There is this existing LID base. Please use this. And it's like already this filled up. But unfortunately there is like. No such parameter. Existing in GCC. So we need to do something else. And I just highlighted here. All the load instructions. Which are problematic. As I said. The offsets are not right. They need to use this LID base. So what we could do as an alternative. Is just avoid load instructions. And handcraft our assembly ourselves. So we just avoid all the references. And have basically no immediate value. So this is obviously. Not something we want to do. In the long run. So I came up with. Two possible solutions. Either as I said earlier. We can tell the linker somehow. Where the existing LID base is. And how full it is. Or we can use our own LID base value. So I decided to go with. The second option. I felt like this is more. More flexible in the long run. And what this implies. Is basically. That I set the LID base to zero. At the entry of every function. This can be done. Using the GCC plugin. And at the function exit. I need to reset it to its original value. And I needed to do this. Into the. Assembler itself. Because there is this. Target dependent feature. Which is called relaxation. So if you have a call to a function. This assembler will basically relax this. To a load and a call. And then we have again a call. Which is problematic. So how does this look like. In the assembly. So as I said. This relaxation is problematic. And after relaxation. We basically have these. A call is extended to two instructions. This load and this call. And the thing is. That in between those two. Basically we need to reset. The LID base to its original value. So that then. We can call the existing function. That the remaining code. Can run as intended. And. Yeah. It took me a while to find out. That I cannot manipulate this behavior in GCC. It needs to be patched in the assembler directly. Because it is target dependent. So how does this. Relaxation look like in the assembler. So basically you have this. Sort of a look up table. On the left hand side. It is looking for a pattern like this. It is looking for a call 8. And if it finds this kind of pattern. It will be relaxed to a load. And a call X8. So. What I basically need to do is. Find a place in the assembler. Where this. These. Build instructions. So basically the right hand side here. These are called build instructions. Where those are. Are iterated through. And then they are applied. And pushed on a stack. And all I did is basically. To look like is the current opcode. A call X. And was the previous opcode. A load instruction. And if this is the case. Then I can basically add. An additional instruction. Which will reset the lid base. With an WSR instruction. So now that we know all of this. And we have patched our assembler. We can go back to our original. Patching code. And we can use this. GCC plugin. And the patched assembler. To compile this. Much easier to look at code. Which is. Pretty nice. So this is how the assembly looks like basically. After we have. Saved our. Parameters. We will save. The current lid base. Into A15. We will write 0 into A14. Into the lid base value. And then we can do. All of our load instructions. And at the end we reset. Or we copy back. The value which we have. Stored in A15. Into the lid base value. Before we do the final call out. So there are still. Some open problems with this. So this implementation is not. Very good obviously. I take away two registers. For the assembler. It would be better to have this. Based on the stack. Based implementation. Also. There is like. This support of disassemblers. Which I mentioned earlier. So I have these patches. But still. For example you cannot use IDA. If you want to. And also there is no support. In this GCC I am using. For naked functions. So this is a problem. You need some way to. Sometimes to override. An existing call. To even. Get to your patch. Either you are lucky. And it is like. They are using the literal pool. Then you can just override. The value in the literal pool. But if not. If it is like a direct call. Then you need some way to. Just compile like a call. To your own code. So it is like a GCC version. GCC for extensor at least. Does not have this option. For naked functions. And also. What is also missing right now. Is that there is no text console. So you cannot. Just do a printf. And somehow dump this. In your host system. To do some basic debugging. But we could implement this ourselves now. So basically. I have this. Very creative name now. For my framework. Which is called QCAMON. Instead of Nexmon. So this is how the folder structure looks like. At the very top. You have some build tools. Then the disassembler patches. Which I mentioned earlier. For Chitra and Banner Ninja. And then there is a folder for the original filmwares. And a folder for our patches. Which we want to apply. The build tools contain. The patched assembly. And GCC. And also the GCC plugin. The firmware. Folders. Basically contains the original. Firmware. And it already comes with a structure. Which would allow. Different chips. And different firmware versions. And there is also a make file. Which already extracts the relevant parts. And decompresses them. And then at the end. We have our patch folder. And our source code. Which we want to introduce into the firmware. So now to a brief demo. So this will basically show. The code. I had in my slides earlier. So we just want to. Set like. 1234. Or store 1234. Into this memory address. And then jump to the original code. And in addition to. What we had shown earlier. We also need these two lines. To basically have an entry point. Into our firmware code. So all I'm doing here is. Creating basically. Overwriting stuff. In the literal pool. To point to my code. So everything is prepared. In the repository. You just need to run. Make. And this will already. Compile the C files. And .O files. It will also compile the wrapper. It will create the linker scripts. And create a .L file. And then use this blueprint. Make file I mentioned earlier. To copy out the relevant parts. In files. Then we can start reassembling. The whole firmware file. We are compressing it. Adding some padding bytes. Basically creating. The firmware file. How it is expected. By the WiFi chip. So here it is called. Firmware-5.bin And we can. I will now SSH into. This board. I have shown earlier. And in order to access the debug. We first need to set up. The WLAN0 interface. Now we can go into the debug. And. Look at the memory. Location. Basically without any modifications. So for this I can just use. DD. DD to. Copy out the relevant section. And use xdump to look at it. So here we see that. The first few bytes are. Not 1234. And now we install our. Modified firmware. Just copy it over via SCP. And we will. Need to. Remove and add. The PCIe driver. To. Apply the changes. Now we can SSH back. Into the router. Just checking the kernel log. Real quick. So here it is already setting up the regulatory domain. So it just looks quite good. So it did not crash on us. And we can go back into the debug. Set up the WLAN0. First. Go back to the debug. Then basically. We can. Copy out the. The same memory location. Which we just did a second ago. And have a look at it. Via the hexdump. As you can see here. We modified two whole bytes. So with this. A quick summary. And future work. I was able to modify. The next one framework. To make it work. With the Qualcomm based. Firmwares. I have the demo patch. I have shown. You can get it via this. This location. There is also the. The patches for binary ninja. And GDraw in there. The GCC plugin. And already a precompiled. Version of GCC. And also the patch. For the assembler. So this shows. That it is possible. To make modifications. On Qualcomm based firmwares. There are still some improvements. Which I need to do. To make modifications. Even more easier. I also want to look at this. Production software. At the beginning. So this QCA CLT. So this is basically using. This QDAL software. On a PC. To communicate. And you can get it from. Some sketchy Chinese servers. And then I want to see. If I can maybe enable. Some features which are used. In this production software. Also by myself. And I also want to look into. This code swap feature. I want to thank Martin Kort. AKA Problem Kaput. So he did some awesome. Gameboy Advance reverse engineering. And I had some. He helped me out in some cases. So it turns out. That he did not even reverse engineer. The main processor of the Gameboy Advance. But also like the. The WiFi chip used there. And this turns out to be like an older. Atheros based WiFi chip. Which he also reverse engineered. And Atheros later on. I think like lots of knowledge. From back there. Still went into their products. And I also want to thank Roku. AKA Arcane Nibble. I used her script to. To do the unzipping of. The firmware file. Yeah that's it from my side. Thanks everybody if you want to reach out. Here is my email address. Yeah if you have any questions. Shoot. I have also one more note. I have found. A firmware file. Which contains all debug symbols. For this IPQ 4019 chip. I have no easy way. To distribute this. Without the original repository. Being taken down. But if you want to poke it at yourself. Reach out to me. I will send you the link. Any questions? No? Okay then. Thanks everybody.