Hi everyone. There are more people than I expected at 10am. Thank you very much for being here. You are the ones who survived until the last day in DEFCON. Awesome. Welcome to taking off the blindfold, detecting persistent threats on drytech edge devices. We are part of the Faraday research team. My partner here is Octavio Geno-Tiempo. I am Gaston Asnares. And we have some experience working with firmware. We have a foundation in computer science and we like to play CDF. As a resume, we like to break things and do research. So, what are we going to see today? First, we are going to see a motivation for this research and this talk. We are going to see some reverse engineering on drytech firmwares. We are going to talk about vulnerabilities and attack vectors. We are going to see mitigation strategies and at least we are going to see some conclusions and takeaways. Okay, how this started? We had a call of a client saying that it was compromised. And when we came to the scene, we saw a lot of outdated drytech equipment on the edge of the network. Most of them were SOHO models that run RTOS. And we started making some analysis. We found previous vulnerabilities with proven exploits on high-end models of the drytech routers that runs Linux. And we started making some questions about it. Like, are these vulnerabilities exploitable on the SOHO routers? There are other vulnerabilities that are known. And the principal question was, were these routers the entry point of the attackers? Okay, we know that there is an increasing threat of advanced attack targeting edge devices. We could name some examples, like the SOHO RAT, that was a socketical malware targeting SOHO routers. We also could name the HIATUS RAT, that was a malware targeting drytech routers, but the high-end models. And at least we could name Benin Sartain, that was an NSA tool targeting Cisco routers, that was leaked by the Shadow Brokers Group. Okay, so what is the attack surface of these routers? We found over 5,100 exposed devices. Mostly in the UK. And we found a lot of vulnerabilities. From an information disclosure to an authenticated remote code execution. So, there is a lot of stuff. Okay, so we had a Vigor 2925 in our office running MIPS 32 RTOS. And we wanted to start making some research on that, but we had a problem. The code is closed source. So, first we had to reverse engineer the firmware format. We had to extract the firmwares, understand previous vulnerabilities, and then look for new ones. And finally, determine if this was the attack vector or not. So, the first thing we did was trying to extract this firmware using MinWalk, but it failed. It produced no results. So, we tried to look for some specialized tools, and we found this one, DRYTOOLS. So, this didn't work with new firmwares. The last commit was 12 years ago, and that's why. So, luckily for us, we found a talk by Philippe Loret at Hexagon 2022, where he described that by emulating DRYTEXTBOOKLOADER, we can extract, we can make it extract the RTOS kernel. The disadvantage of this approach is that this is a manual process, and you have to tweak the addresses for each firmware version. And the other problem that it has is that these firmware images have other executable components, apart from the RTOS kernel, and file systems that you cannot extract this way. So, our approach was to reverse engineer the format, and to try to write a tool that can extract all firmware versions from different models, extracting all file systems, and extracting all the executable components. We named our tool DRYTEXT Arsenal. It's a collection of tools. We wrote this in Python. It's open source. You can check it out at that repository. And the idea is to support for all these new firmware versions. And we will be showing, along these presentations, all the features that we implemented. So, let's talk about the firmware format. It has two main sections. One is called the binary section, and the other one the web section. Both start with a header that contains the size and the pointer to the following section. But in the first one, we also have information about the firmware version, and some metadata. So, the web section has a file system that is compressed and contains all the static files that the router serves for the web UI. In the first section, the binary section, we have the bootloader that is raw MIPS32 code. This is the part that Philippe emulated. And then this is followed by the compressed RTOS kernel. And after that, we have a very interesting part of the firmware, that is the DLM code. This is also a file system, but it's compressed and encrypted. And we will talk a lot about it. So, to work with this, we made a specification of the structure of the firmware using KTI Structs. And the advantage of using this is that you can manipulate the firmware images with any language that you want from the list. We used Python, but you can find this in the repo and use the language that you prefer. And, well, by knowing all this, we started to tackle the reverse engineering of the compression algorithm. So, during boot, the RTOS is decompressed to the same memory space as the bootloader. And we did this following the steps described by Philippe in his talk. But when we started reverse engineering this, we mapped all this in memory, and then the RTOS after that. And that allowed us to discover that the decompression function is used during runtime to decompress other things, for example, the file systems. And we also discovered some interesting strings, like the one that you can see down to the right, that says LC4 decompression failed. But if it was LC4, the compression algorithm, why we couldn't work. And the reason for this is that while they use a standard LC4 compression block format, they have written a custom frame format. So, it's just a custom magic that is followed by a succession of block size and block data multiple times. So, we wrote a decompressor and a we moved on to the most interesting part, that is the DLM section. So, it turns out that the DLM stands for Dynamic Kernel Modules. So, this real-time operating system has the ability to load code modules during runtime dynamically. And this feature works independently from the firmware. So, during boot, we can see that some the modules that come with the firmware image are loaded. There are three modules, and they are called VR9 GeoIP, VR9 App-E, we will talk a lot about this one, and VR9 SSH. So, the first one contains information about the geolocation of IPs. The second one is used to block traffic from the users that are in the LAN. And the last one contains the implementation of SSH server. And this is really interesting because this implementation is separate from the firmware. So, these modules can be updated during runtime, and if you install a new version of App Enforcement Module or SSH, these are loaded first and prevent the modules that come with the firmware to be loaded. You can see down to the right that, for example, in this case, the VR9 App-E module was loaded first, and then when the router tries to load the version that comes in the firmware image, it will fail. So, doing some more reverse engineering, we found out that the modules are compressed using the same algorithm that is used for the RTOS, but they are also encrypted. So, we recognized that the algorithm used was a modified version of XTAA, and this is a symmetric encryption algorithm. And what this means is that the keys for decrypting these DLMs are present in the firmware. So, we looked for them, and we wrote a decryptor and encryptor for the modules. So, if you take a DLM, you decrypt it using this, and decompress it using the modified version of LC4, then what you get is a relocatable ELF. And this is very interesting because all the external functions that this ELF uses, they have to have their symbol names. So, they are really easy to reverse engineer. And this also means that the firmware must have an ELF loader. So, we looked for that code, and we found that there is also a symbol table. This symbol table was described before by Philippe Loret, but only as an aid for reverse engineering, because imagine that you have a table where you have function names followed by function pointers. But now we know that this table is actually used by the ELF loader to link these modules to the kernel. So, the advantage that this gives to us is that if we manage to write our own modules, we can use all these functions without knowing their actual addresses. So, we implemented all these in our tool. We have two functions, one called parse firmware that will print a lot of information about the firmware image, and the other one called extract that will extract all these pieces of the image. It will decrypt and decompress all the modules. It will extract all the file systems for the web server, and it will generate a binary image that contains the bootloader, followed by the RTOS kernel, but decompressed. So, if you want to start reverse engineering this, you must know the loading address. What we realized is that all dry type bootloaders start by calling the decompression function. So, this function is always located near the start of the image, and since all this has to be loaded aligned to the page, then we can search for the address of this compression function, and if we remove the last three numbers, what we get is the loading address of the whole image. So, with that, you can start your reverse engineering process. We also implemented this in a functionality called find loading address, and once we had all that, we moved on to try to find some vulnerabilities and attack vectors. Okay, so let's talk something about vulnerabilities. We are going to show some of them. Starting with the basics, we found that they had an insecure password storage. That means they stored the passwords and the credentials in plain text in memory. So, if someone had access to the memory, they could read the credentials or maybe chain another exploit to leak them. We also found a non-constant time comparison on the credential. So, they use a function that takes different amounts of time to compare different passwords, depending on how much correct characters are in the string. And that leads to a side attack channel, a timing attack. Okay, so, DirectX implements a second-step authentication. That's really nice, but that was predictable, because the code generation was done using a self-random function, but with the seed, they use the time elapsed science boot. So, if you know for how long the router is running, you could predict the second-factor authentication calls. We also found an older reference in the HCP server that leads to a denial-of-service attack, but the interesting thing about that is that it was exposed to one. So, if the router was exposed to the internet, you could trigger that another reference. And if we combine both vulnerabilities, we could predict the second-step authentication calls, because we could make the router crash, and then we know the time elapsed science boot, and we predict the calls. And that's what we are going to see in the next demo. We could see on the right, the serial communication with the router. Um, on the... no, that's on the left. On the right top is the script that we run. It triggers the known reference, and the router is rebooting. It will take like two or three reboots, because it crashed hard. And on the right bottom, we have the web UI, where we are going to try to log in. So, now it boots. We are going to wait until the router initializes itself. Okay, and now we are trying some different calls. And we were able to predict the call, and we are logged in. Okay, so Octavio talked something about the APP Enforcement Signature feature. It's really great. If you have an enterprise and you want to block your employees' web, or I don't know, you could update the signatures in order to block that traffic. And how this process is made, there are two ways. The first one is a manual. We will see on the right bottom that says import. If we click there, we could upload any file with some checks. And the second way is the automatic way, in which we have to select the update server or select the default one, and we have to schedule the updates. So, for example, every day at some point of the day, it will check if there is some update for the signatures. And how they implement these signatures, you could think in JSON formats or YAML or something like that. But Drytech has the idea to implement this as a module. So, to update the signatures, they upload an L file with code and everything. Okay, let's talk about some vulnerabilities related to the modules. The first one is they not also update certificates with code, but they don't check for certificates or have SSL enforcement. So, if someone manipulates the configuration of the router or the DNS, maybe DNS hijacking, they could upload a module to all the Drytech routers that has this feature. And we have found two post-authentication endpoints. The first one is the one that I told in the last slide. And the second one is that we found an endpoint to upload configurations files that doesn't have some checks, so they allow us to upload an arbitrary DLM. Now it's not only the APPA module, but we could upload any modules that we want. Maybe we could, I don't know, upload another SSH module. So, we have three ways for an attacker to install malicious modules. The first one is the post-authentication endpoints that we talked just in the last slide. We have the module upload process that is hijacking the configuration or the DNS. And the last one is the supply chain. Because the modules are very easy to craft, so if they are easily appended to to the firmware, and anyone with physical access or, I don't know, exploiting some vulnerability, they could upload the module before selling it. Okay, so knowing all of this, we could start making some modules. We implemented two commands. The first one is the MIPS compile that is going to make a compilation of source code into a ELF of the architecture of MIPS32. And we also have another command that is ELF to DLM. As the name says, it will convert an ELF to a DLM, compressing and encrypting with the provided keys. So, we could append some web headers so we could upload them through the web UI. So, we are going to see a demo. We could see on the left the version of the APPA module. On the right, in the middle, we are going to run a server, a custom server, that is pointing the configuration of the auto-updates. So, on the top right, we could see the serial interface with the router. Okay, so once the server is up, we are going to trigger a new DLM. So, it's triggered. We could see the router has fetched the new module in our server. We could see the Faraday logo because it's our module. And if we log in again, we could see that the version of the module has changed and we have some logs that said that it doesn't allow this shit. Okay, let's make something more funnier. We are going to make a SSH backdoor. Why not? Because we have the module. So, the original behavior of the SSH module is the top one. When we have a new connection, it's going to call the authentication function and the authentication function will say that it fails or it authenticates. But what we are going to do is when a new connection enters, when a new connection is open, that is going to call our hook and it's going to check our credentials. So, if they are right, it authenticates. If not, it's going to call the original function because we don't want to change all the behavior of the SSH module. How is the roadmap for this? First, we have to do a reverse engineer of the SSH module to find that function that makes the authentication. We are going to skip that part and we are going to say that the target function is the SSH account and LDAP auth function. After that, we have to write and compile the hook. We have to merge the hook with the original module and we have to modify all the relocations in the entry table. And last, we have to upload the DLM. On the right, we can see the source code of our hook function that takes the same arguments as the target function, an SSH instance, and a packet. And it will check for our credentials. If they are right, it will return one that says it authenticates. If not, it will call the target function. In order to compile this, we use our command MIPS compile. And then, we have to merge both binaries, both relocatable ELFs. So, there is a relocatable ELF. It has all the symbols with no fixed address. So, they had a relocation table that will say everything that uses a symbol has to be modified to point to that symbol after linking. So, we have all the symbols there. And once we merge it, we have the symbols of both modules. We wrote a command to do this with the name of MIPS merge that takes two ELF files and as an output has one file. Okay. Now, we have to modify the relocation table. So, first, we find all the relocation that talks about the target function. We can see on the top right the two examples. We are going to use the first one. If we print the code that is on that address on the bottom left, we can see that the jump is a jump instruction, but the jump instruction only has his opcode but doesn't have an address where to jump. That is because the relocation entry will set which one is going to be that address on link in time. And we can see there that is the target function. So, we modify that entry to give us a result the right that points to the hack function. So, time to hack. Yeah. Okay. So, on the top right, we have we are trying to log in with our credentials, but we could see that it fails. So, on the bottom left, we upload our backdoor module. And once it's uploaded, we could see that the file was written successfully, but we have to reboot the router. And once it reboots, we are going to try again with our credentials. We could see that the module was loaded. Okay. So, we tried to log in, and we could see that we were in. And we could see some loss in the serial communication. So, this attack that Gaston showed is really dangerous. Imagine that an attacker can craft a modified SSH module, upload it to your router, and you have no way of detecting this. Moreover, if you update the firmware, the module will be still there. It will persist. And even will persist if you reset the configuration of the router, because it's stored in a separate file system in the flash. So, we started to think about some mitigation strategies. The first one, the obvious that we thought about was just to remove the feature, because who wants to have dynamic modules on their router? But there might be some users that use this feature. So, well, instead of nubbing all the calls to module loading during boot and during the update process, we decided to think for a better solution. And we devised a mechanism for checking in-memory integrity of the other modules that are loaded. This mechanism should fetch signatures from a server, because the modules will get updates, so you have to update the signatures. But this time, you have to check the certificates. So, once you got those signatures, you have to find the loaded modules in memory, calculate the actual signatures, and compare them. And if there is a mismatch, then you have to notify the user somehow, and all this functionality should be included with the firmware. So, what we did, as a proof of concept, was to use the open source module to introduce this code to the router, because it's much easier to write a module than to patch the firmware, because you have all these functions from the symbol table that you can use. So, there are two things that we have to solve. The first one is how to calculate a signature for a valid DLM, and the second one is to find the actual DLMs in memory and do the comparison. So, we focused on that, and we accepted some limitations. We will not check the signatures every time a module is loaded, as it should be. We will only check signatures when our defensive module is loaded, and we will not try to notify users for now. So, the first problem that we have to solve is how to calculate a hash or a signature for a DLM. We will hash each signature with MD5, because MD5 was available on this table, on this symbol table, and we will hash only the static sections, because the dynamic sections will change once loaded. If the static section contains data, we will hash it right away, but if it contains code, then, as Gaston explained during the module installation, when it's linked, all the jams and the function calls will get the actual addresses of the functions written to them. So, we will hash only the opcodes for these sections, and the opcodes are the first six bits. So, once we got that, we have to find all the modules that are loaded. When the ELF loader installs a module, it creates an object to represent the module, and the space for this module is allocated using the linear allocator of the router. So, this allocator maintains a list of all the chunks that it has assigned, and if we manage to find this object that represents the DLM, we'll get the DLM name, a pointer to the text section of that module, and a pointer to the main section that will get executed after the module is loaded. So, the loader copies all the sections in a predefined order, one after the other, after the text section. This order is usually defined by the compiler, and we will assume that this order doesn't change. So, on the bottom, you can see a little bit of the code of this defensive module, the part where we traverse the list of chunks from this allocator, and we will check if each chunk corresponds to the module object. If the chunk corresponds to the module object, we will print some information, and if this module happens to be the SSH one, then we will calculate the hashes and compare them. So, here's the demonstration. This router is the one that has been backdoored and has a modified version of the SSH. Then we upload our defensive module using the UI on the left, and you can see right there that once it's uploaded, it will try to find all the modules, printing their information, and when it reaches the SSH module, it will start comparing the hashes for each section, and it will find that there is a mismatch between the hashes on the second section. So, to wrap this up, some conclusions and takeaways. All these vulnerabilities were found by us analyzing only the bigger 2925 router, but they are present in several models because all the SOHO routers use this real-time operating system, and even some of the higher-end routers that run Linux emulate the same real-time operating system. So, we found all these vulnerabilities and some more that we didn't show, and they are all patched by now, but we don't have CEIDs yet. And, well, some conclusions on the offensive side. The code for this real-time operating system is huge, it's really complex, and we only analyzed a little bit of it. Imagine that the functions that are present in the symbol table are maybe not up to five percent of all the functions in this firmware, so there are probably more vulnerabilities here to be found. And that is why we are open-sourcing our tool, and we would love for you to try it, and we would love to know if it's useful and if you find something. So, there's the repository if you want to check it out. And now, some conclusions on the defensive side. We show that it's possible to create a defensive module to check the integrity of other modules in memory, but in an ideal situation, this feature should be implemented by the vendor. It should fetch these updated signatures from a trusted server, verifying the certificates, and for all this to work, if this feature is included in the firmware, also we must have a secure blockchain to trust that this is working right. And to close, some takeaways. We think that closed-source firmware, as this example, is just another case of security by security. All these vulnerabilities that we have found are really simple, and they would have been reported sooner by the community if it were easier to reverse-engineer these routers. So, some things such as plaintext password storage, the use of insecure functions to compare sensitive data, predictable random number generation, and even the lack of SSL certificate validation. So, given all the increase in attacks that we are seeing to edge devices, it would be really important for vendors to include features that facilitate observability, vulnerability research by the community, and to implement threat detection solutions. So, this is it. Thank you for your time, first for coming so early, and if you have any questions, please ask us. There's a mic here, if you want to ask. The first question gets shared. Well, thank you, guys. Okay, the question was if there is memory protection between the modules and the RTOS, and there's not. All these runs on the same memory space, so from a module, you could modify the RTOS code and vice versa. Yeah. Well, thank you again for coming.