Hello hackers. This is a talk about a bug in the Microchip. And I'm Nini Chen, a security researcher at DevCore. We are from Taiwan. And I'm also a member of Belson CTF team and a member of Undefined. Okay. Why? Why the router OS? Because we want to participate in the Ponzu on Torrento in 2022. As one of the targets, the router board of Microchip hasn't been exploited in the previous event. More correctly, no one tried to demo exploit against this target in the context. You may already know the rules of the event. Participants must find a pre-auth RCE with no user interaction to acquire the points of the target. Okay. So, let's talk about the router board of Microchip. And the SOHO's mesh app, a special category was introduced in 2022. Participants have to chain a router when site exploits and another device exploits to earn the points and bounty. The bounty and point in this category are relatively high. So, it's a good target for us to earn more to win the contest. Let's recap some backseting quickly. The router OS, the router board of Microchip is quite popular. And the router OS is the operating system of router board hardware. By scanning the Microchip, we can observe about 3 million device online. Router OS is a standalone operating system based on the Linux kernel. And Microchip also offers ISO images so we can run it as a VM. It's closed source and also a closed ecosystem. According to the download terms on Microchip's website, you pay the price for the operating system. So, you can pay 45 bucks to get a GPL source code. What a bargain. And currently, there are two version of router OS, the V7 and the V6. That should be considered two branches because their implementation are slightly different. And the cloud hosting router is the router OS image for for for VM. But only the AMD 64 version is available. Most of the services that are available on router OS are implemented by Microchip themselves. And they share a similar architecture. So, we name them Nova binary afterwards. And they use Nova message to talk to each other. Though the router OS is based on Linux kernel. Users are trapped inside a customized COI. And there's no official method to access the underlying Linux system. Only developers can install a backdoor package to access the Linux system for debugging purpose. An administrator can manage their routers via the web web panel or Winbox, which is a Windows UI application. It's also a popular attack service. Because the Winbox interface on the router can be used to talk to other Nova binary. Some of them can be accessed without authentication. To understand how people research the router OS, let's have a look at test CVEs of the router OS. There are a total of 80 CVEs. And 28 of them are pre-auth exploitable. And the victim is the router itself. 16 out of the 28 are those in various components. 8 out of 28 are DNS poisoning, allowing attacker to MITM or firewall evasion. Only 4 pre-auth vulnerabilities in history can run a lead to a share or login as admin directly without user interaction. These are what we care about. 3 out of 4 were found between 2017 and 2019. And 3 out of 4 are in the wild. Let's have a close look. CVE 2017-20149 aka Chimerae. It's one of the exploits leaked from the CIA in the 2017. By sending an HDTV request with crafty content lens we can corrupt stack to execute arbitrary code by slack- by stack clash. CVE 2018-7445 is a buffer overflow in SMB found by black box- black box forcing. CVE 2018-14847 is also one of the exploits leaked from the CIA in 2017. CVE 2018-14847 is also one of the exploits leaked from the CIA in 2017. This bug allows unauthenticated attackers to read arbitrary file on router os. It sounds not a big deal in the first place, but since the password stored on router os was from simply repeating x or password in plain text with the hash of the username and fixed string at that time. So the password can be recovered easily and a bug with arbitrary file reading is sufficient to lock in as admin. CVE-2021-41987 is a HIPAA overflow in the decode of the SEP service. This was discovered on an APTC2 server in 2021. As we can see, these vulnerabilities are mostly in the wild. We can't learn too much from them directly. So, let's take a look at some talks and articles about the RouterOS. In 2017, there are three talks and they are about the package format adopted by the RouterOS, the developer's backdoor, how the RouterOS stores the password, et cetera. Uh, talks are interesting and helpful to have a big picture of the RouterOS. In 2018, the talk, Bug Hunting in RouterOS, Analyze the Normal Message in Binary and JSON Formats, introduced the How WinBugs Client. Talk to the WinBugs Interface by Normal Message and found a post-old stack overflow. In 2019, the first one is the same as the previous one. So, the next one is Mikrotik Firewall and Net Bypass. It's a talk about the vulnerability in the WinBugs that can be used to scan the intranet if the WinBugs is exposed to the intranet. The next, Finding and Exploiting CVE, found a stack buffer overflow in SMB by black box passing and explained how they got the shell, whose layer is deployed on the RouterOS VM. And the Deep Dive Mikrotik Exploits is a security, uh, analytes about the CIA exploits and the massive exploit event based on the liquid payload. The next one Help Me Vulnerability. You Are My Only Home! Ad from Microsoft. release a tool to help users to jailbreak their device to check if leaky exploit was already compromising them. And the last one. RouterOS chain to root is uh it's about chaining DNS poisoning vulnerability and uh downgrade attack to get a share. Finally in 2022 Pauline Microchip in Zoola Limeline talk more about Nova message and disclose uh post auth vulnerability in triple W that can be used to jailbreak. They also released a tool to help researchers quickly get a share on the AMD 64 version of RouterOS with the vulnerability. It seems like the whole world is talking about the Nova message in the IPC of the RouterOS. So we also try to understand what it is. Let's talk about the IPC of RouterOS with an example. While a user logging through a telnet to access the console of RouterOS. The IPC of RouterOS is a TLS. The telnet will immediately call an EXECL to execute the logging process to ask the user for a username and password. Then the logging process will send a Nova message with your username and password. So the user process to check if they are correct. The user process also responds in Nova message. The Nova message consists of key value pairs and the key can only be numbers. The key the sys2 and the sysform are arrays containing the ID of Nova binary and the ID of the handler. The communication between two processes is something like this. Every message is sent to the loader via socket and the loader dispatches the message to the corresponding binary. Logging process send a request with a username and password to the user process. The ver- the value of sys2 means the target is the handler 4 and the binary ID is 13. And the process itself does- doesn't have to fill the sysform with its process ID because the loader will do this- will do that based on which the socket is received the message from. Next, uh the loader removes the target binary ID in sys2 as the source ID to sysform and then forward the message to the target process. The loader will then send the message to the sys2. When the user process receive the message from the socket, it also remove the handler from the sys2 and pass it to the corresponding handler. Then the handler does the rest of thing. We mentioned that a Winbox interface on the router OS is a popular attack service since it can be used to assess all the process. That's because the protocol Winbox client used to talk to the interface on the router is basically a Nova message in the TCP packet. So, if the target handler fail to check the sender's pri- privilege, that can cause problems. No- Nova message is type value mapping and can contain various types and also Nova message itself. Because the JSON format of the Nova message is duplicated, s- we only talk about the binary form today. Okay, the M2 at- at the beginning is the magic bytes for the Nova message. So, the Nova message. Then it use 4 bytes to describe a key. The first 3 bytes of key are id and the last byte is- is type. And the bytes following are the value for that key. So, this message's id types and values look like this. Types like bool can be expressed with one bit. Directly using the lower bit of the type bytes. To understand which binary corresponding to the id, you need to use the id in the sysform and sys2. We need to parse x3 file under the loader directory. It's kind of xml, but in binary form. If a binary was introduced by installing a package this id is on the loader DS of the package. So, in this can- can be found in the x3 file. Because the process is not persistent. The id is just a serial number like the S- sin define. To brok- is videos in theокой locking process we just mentioned. Other binaries also have their x3 files for different purpose. For example the x3 of triple w of triple w describe which URI should be handled by which servlet. It seems like it's a good target for fuzzing right? But we can't just fuzz it and expect that we get something after two months. Now, it's a spoiler. While we're writing our exploit with the vulnerability that I will introduce later, uh talk at POC 2022 MicroTik router OS security the forgotten IPC message. Though I think it's far from forgotten but anyway. The talk is about fuzzing and the nova message. And it founds lots of post-auth CVEs. By the number of CVEs it looks like the fuzzer has been running for a long time. A bit of an afterthought. I don't think we can get a better result if we do the same thing at that time. Not to mention that we only have two months. So, is it still possible to pull the router OS? Review this CVEs again. Most of the most of the vulnerability we think interesting were discovered many years ago. Researchers also found lots of vulnerability but as we can see the MicroTik becomes safer and safer. But in 2021, a vulnerability in the SCEP server we just mentioned was discovered by analyzing and in a wild exploit on the APT C2 server. Was it uh was it undiscovered until until then because it was not a default feature or is it just because people have overlooked something? Most talks and articles focus on jailbreakers and the fact that the jailbreakers are breaking. Analyzing the in the wild exploits and normal message in IPC. I have a summary after reading them and starting to reverse by ourself. That is, no one with sanity would like to dive into the details of normal binaries. I mean is expect for the exploits from the CIA and APT. Most of the research about bug hunting is fussing the protocol playing with normal message or fussing normal message. By the outcome, it seems like attackers are familiar with the router OS than us. We need more details about the normal message. And don't get me wrong, I'm not against fussing but we have to ensure we check everything to take to take advantage of the contest. I don't think the system is perfect now. There is a gap between researchers and attackers. So what was missed if we want to find some pre-auth C box in router OS. The first question is, where are the entry point to the customized IPC? Most of them can only access by authenticated user. We can foresee that playing with normal message in IPC will result in lots of post auth bugs. IPC just a part of implementation of the most functionality. We want to review the crucial part directly and carefully. For example, how does the DHCP process pass a DHCP packet to extract the info in it? Then, we have to mention the architecture of the normal message, normal binary. Every normal binary has a looper. The looper use port to check the status of socket and process event in each loop. Just like a regular event loop you've seen everywhere. Looper is responsible for the communication between the current process and the loader. The looper register a special callback for a unit socket which is used to communicate with loader. The special callback on MSG sock will dispatch the message to the corresponding handler once it received normal message from the socket. So, how does the handler work? While the looper receive a normal message, you dispatch it to the corresponding handler. Look at the right hand side. And let's assume that handler one is responsible for the normal message. And then you call a function in Vtable with the CMD prefix like corresponding to the command specified by the C-CMD. There are many functions here, but because the handler is the base class, commands relative to objects are uninfluenced. So, the handler will not be able to receive the message. So, you need to implement it. The CMD unknown is often overridden and used to extend the customized command. Sometimes the developers override the handle CMD instead of the CMD unknown. You may have already read some of this in a past research. But most of the time, the normal binary use the derived classes rather than the best handler class. The derived classes can store a number of messages. So, you can store a collection of objects of a specific type. For example, while the user is setting up a DHCP server, they will send a message to the handler zero of the DHCP process to create a DHCP server. The handler zero, which is an instance of a-map, calls the function on its Vtable with offset, hex 7c, to create a DHCP server object and insert it into a tree. The 7c is the offset to the constructor object. of the inner object. And you can find the offset of such function in other derived classes by reversing the cmd add object. In the previous example, the web panel sends a NOVA message to DHCP to create a DHCP server. But what if I told you that some functionality don't even use the NOVA message? Take the CDP and the LLDP for example. They are implemented in this cover. The looper register two callback functions. The looper keeps looping and check the status of sockets. If there are incoming packets, the looper call the callback function. The callback of CDP will check if the interface that receive the packet is not filtered by firewall rule. Then extract the information from the packet and store them directly into another ASEC map without sending NOVA message. So there's no NOVA message here. So, we started to review the router OS by enumerating functions and reversing them. Then we found something strange. One day, while plugging and unplugging some cable connect to the router, suddenly the log file show that RADVD crashed several times. So we tried to reproduce the crash by manually plugging and unplugging cables about a thousand times. But couldn't figure out the crash condition. The condition is unknown and the crash appears randomly. Instead of mindlessly unplugging and plugging the cables, we reverse the RADVD to see if we can find something interesting. We didn't find the root cause of the crash, but we got something better. So, what is RADVD? RADVD is a service about SLAAC of IPv6. Let's say a computer or a node wants to get an IPv6 address. It will first mutlcate the router-router solicitation to all router, asking for a network prefix. Then, if a router receives the RS, we will-it will mutlcate the router advertisement with network prefix to all nodes. After that, a node can calculate its IPv6 address by combining the network prefix and EUI64, which is calculated from the MAC address. Another scenario is that if an upstream router, router A, maybe it's an ISP, they give you a prefix with slash 48, which assign in advance by email or something. Then, you can use them with whatever-whatever-whatever you want. in your LAN. Then, here comes the link prefix and router prefix. First, the router B, act-act as a node on the interface that link to the router A. The router B asks router A for a prefix. The router A send a slash 64 prefix for router B to calculate its IPv6 address. Router B calculate an IPv6 address. Let's say it's 2001-2007-2007-2007. Router B will be using the colon DBA, colon colon one. And use it on the interface that links to- with router A. So now, the router A can route all traffic to the address on the slash 48 to router B. Now, when nodes want to get IPv6 and send RS to the router B, the router B just multicast the slash 48 prefix to nodes. Then the nodes act as usual. So the functionality of RADVD in router OS should have two functional- two functionality. It- first, it can act as a node to receive RA. And it can also multicast RA to the LAN. Let's follow the execution flow of RADVD to find a bug. The RADVD have a similar design pattern to the discard. The RADVD have a similar design pattern to the discard. They all have the sameregister method. The RADVD can register a We have a process that we mentioned earlier. You register a callback. The looper pull a socket in every loop. And if there is an incoming ICMPV6 sub packet the looper call the register callback. What callback do is roughly like this. It check if the packet is a valid RA or a valid RS. If it is an RA, then extract information and store them in the handler one, which is AMAP. And if it is an RA, it will multicase the RA immediately. Sorry, if it is RS, it will multicase RA immediately. There are three cases the router OS will multicase the prefix to nodes. The first one is when you receive the RAs from nodes. The second is when you receive an RA from upstream router. The last one is when you, uh let, sorry, the last one is the router will multicase prefix in a default period, which is in two hundred to six hundred seconds. And yes, the router will multicase the RA to nodes once it receives a RA. But it's hard to trace this behavior directly because this is about a subscription. I'll talk about it later. We didn't know that then. But the other thing is the router to case can be traced directly, directly. The RADVD calls send RA right after it receives an RS. The handler one register a timer to send RA periodically. So, we can find the send RA function by tracing these two methods. Following the send RA function, we found something interesting while the RADVD deal with DNS advisory. It will advise DNS to know that the RADVD nodes based on the DNS list it received from upstream. The ADD DNS function is used to flatten the DNS tree and then add it to the ICMPV6. The RA row is the ICMPV6 payload, which is the local variable with the size of four hundred ninety-six bytes. Step into the ADD DNS and we can immediately find out there is a probably, uh, stack bubble overflow. So, this is the program that's causing the stack bubble to overflow if it'stop location is big enough. The function at DNS to packet by memory copy without any, without any bundling check. The causes the stack bubble to overflow in if the DNS is speaking nice. The DNS addresses are from the RDNSS of the RA. But theEMX length of the RDNSS of an RA is 8 bytes. 8 bytes. Which is insufficient to override the return address. But if this is not a function, we have only nine first time, the router OS receive an RA. The RA DVD have to expire all RDNSS. That makes the RD- RA DVD flatten another tree to override more bytes fo- for us. So, what we have to do is we have to act as a bay neighbor. Let's send a router advertisement with a big RDNSS list to the target router. Then, send another router advi- advertisement with the big RDNSS list. Now, the return address is overri- overwritten with an IPv6 address in the RDNSS of the first RA. It's time to construct the exploits. And it looks easy because there is no protection enable. So, let's find a way to jump on the stack and run our RA. So, let's find a way to run our shellcode. That should be easy. Thank you. The overflow IPv6 addresses are sorted since they were stored in the tree. To make the order of the IP addresses stable to ensure the shellcode is good, we can use part of the IPv6 prefix as a serial number and put a jump and at the end of each address to jump over the prefix because they are not valid instructions. But, because of the delay slots of MIPS, which means, uh, the instruction after the branch instruction will always be executed before the branch instruction itself. We have to move the jump instruction forward. But, the other problem is that you cannot do a syscall in the delay slot. So, basically, this is a bad idea. It's actually a CTF 101 problem. Just make a prefix, uh, very interesting. So, let's have a look at the instruction. They won't affect our gadget or shellcode. Then, everything is fine. And because we didn't leak any stack address, plus, we didn't find any gadget that can be used to move stack pointer to register T9 for jumping instruction in the first place. So, what we did here is, first, write our gadget on memory then jump to that gadget. Finally, our shellcode would be executed. And then, we can use the capture to run the app. And then after that, we can run our executed. Sounds good. But there is iCache. The MIPS CPU has two cache. The instruction cache and the data cache. While writing our gadget on memory, we are actually writing on de-cache. Now, when we try to control the execution flow of executes, uh, sorry, to execute the gadget for running our shellcode, the process- the processor first check if the instruction are already in the iCache. Because we wrote our gadget on the data section, the cache always miss it. So, the memory content will be loaded into iCache. The cost of the process- the cost of the processor to execute a bunch of nob- nob until the program crash. To flush the de-cache, we need to make the processor content switch or exhaust the de-cache. The de-cache is a function whose size is 32 kilobytes. There is no sleep in RADVD. Other functions will also trap into the kernel, but they don't guarantee the context switch will always happen. To make our exploit consistently successful in maximizing the chance to win the game, we have to flush the 32 kilobyte de-cache. Luckily, the value in randomize VA space of router OS is 1, which means the heap is not randomized. The only things we need to do is do some memory allocation to extend the heap a-and write some gadget on it. We may run out of bytes for shellcode because of that. But, the DNS addresses were stored in a tree. They cost lots of memory space. The heap is already larger than 32 kilobytes during the process. So, we need to during the processing of a request. So we can call the memory copy by GOT hijacked to write 32 kilobytes garbage on the heap to flush the de-cache for our shulker directly. And the profit. The vulnerability was assigned CVE two thousand twenty-two thousand twenty-three. Three two one five four. And it's already been fixed. After that we found a vulnerable code had exist at least since the router OS v six. This means the vulnerability has exist since two thousand thirteen. Nine years until we found it and ten years until they fixed it. So no one with sanity would like to dive into the details of normal binary. QED you're welcome. So let's combine the exploit with our exploit from the Canon printer for the point to own. The attacking scenario is that an attacker is a bad neighbor of the micro ticker router sending crafty ICMP v six to attacking the router. And if we if we compromise the router successfully we can forward our payload to attack the printer behind the router. The whole set up can be simplified as such in the context. Just let our laptop and our computer do the job. So we can act act act as the upstream router of the target router. We can almost see our hundred k balting. But. Another but. Our exploit only working on Mac OS and fail on Ubuntu. Whether it's a VM or not. We try to debug the exploit by running the exploit on VM and recording the traffic. Down below traffic and replay it. It just fail as we expected. But we can see that the output is in the network. We also downed the traffic from Ubuntu and replay it on Mac. However, it somehow succeed. So we guess there is an OS that will reorder the packet. We put a thing for our router to monitor the traffic from Mac and Ubuntu. And because the AF packet is not affected by any firewall chance. The result should be reliable. But it looks like the the same. The last resort could be that I have to go to Torrento in person but we can't leave it like that because who knows if the bug will suddenly happen again during the contest even on the Mac OS. Eventually we find a problem. It's all about speed. The gaps look tiny and you may never notice in Wireshark but the difference between them is actually 390 times. The problem is not on Ubuntu, it's just because the Mac sends two packet too quickly causing rest condition. Plus, we were lazy and did not actually calculate how many bytes we need to override the return address. We just send a bunch of garbage and do the pattern matching. So the solution is put a sleep before sending a second RA and fixing the offset of our payload. Then it's very stable. The probability of sleep is very low. So the problem is that success is almost hundred percent. But how? The RADVD is only one thread. How could there be a rest condition? I didn't mention that while the RADVD parsing the incoming packet, the DNS address are stored in a vector. But somehow the handler pass a DNS tree to the add DNS function. What's the relationship between these two members? It's actually about why we can't find where the code bay function multicates the RADVD. So, let's look at the RADVD. So, let's look at the RADVD. So, let's look at the RADVD. So, let's look at the RADVD. So, let's look at the RADVD. So, there is a GOT report for storage associated with RA when you receive an RA packet. Let's look closely at the code bay function. There is an array responsible for storing something called the remote remote object. Then it looks straightforward. Just enter the new DNS vector then do RODNS on each of them and append the result to the DNS remote object vector. So, what is the remote object? The remote object is a mechanism to store common resources in the process and other process can access them via ID. For example, a DNS remote object is actually stored in the handle 2 of the resolver and the RADVD only preserve the ID of the object in handle 1. Now, let's talk about the subscription in Nova binary. The binary IP pool 6 is responsible for managing the IPv6 address pool. The DHCP subscribe to it so the DHCP can react to the change. It's done by sending a subscribe Nova message and the sys notify cmd can specify which command you want to subscribe. The F-E-O-O-O-B means notification. So, if there is another binary try to add an object to the handler 1 of IP pool 6 by sending a Nova message the add object of handler 1 will be executed. In most cases the add object will call send not-notifies to notify subscribers who subscribed to F-E-O-O-O-B to inform them that the IP pool 6 has changed. So, the F-E-O-O-O-B will have the same function as a RADVD. So, when the RADVD receive RA from an upstream router it call RO DNS to create DNS remote object for each IPv6 address in RDNS of RA. The cammed add object of handler 4 in the resolver is responsible for such a request and we'll add the IPV6 object in the handler 2. Also, because the handler 1 of RADVD subscribes to the resolver's handle 2, the The handler two will notify it with every DNS addresses it has now. So the handler one constructs the RA for LAN with these DNS addresses. The problem is in the implementation of RO-DNS. Let's send Nova message by post message which is non-blocking. It means the remote object won't get the object ID immediately. So if we send request too fast, the second RA arrives before the RA-DVD gets the object ID of the old remote object, the RA-DVD can only soft delete those old objects rather than really delete them. That cause the rest condition. This is what happen if we follow them step by step. Because both process are single thread, let's assume they are in the first loop of the looper while the RA-DVD receive the first RA from when. With one DNS address, the RA-DVD creates a remote object in a resolver by sending a Nova message. And the resolver will set up a timer after receiving the first request. Because it cannot tell how many 18 operations should be grouped. Issues- it just set a timer and send only one notification when the time's up. Apart from the RA-DVD, we can't use the RA-DVD in the from notification, the resolver should send a response back to tell RADVD the object ID of the remote object it just asked for. But if we send the second RA quickly before the resolver sends the object ID back, the RADVD can only mark the old DNS remote object as destroyed. Then keep going to create the DNS remote object for the old DNS in the second RA. Then we'll send a request to the resolver but as I say, we send the second RA too quickly. The resolver is still in the first loop which try to send the object ID it created in the first loop. So our request will stay in the socket. Now, the resolver sends the object ID back. The response is still in the socket, but the response handler, uh, is a callback re- registered by the first request, will handle the response. The handler finds out the object has already been marked as soft delete. So that it won't update the object ID. Instead, it will post another request to the resolver to delete that object. The request will will be processed in the third loop of the resolver after request of creating the DNS remote object for the second RA. In the second loop of the resolver, it processes the creation request with the DNS address from the second RA and the response with object ID. Now, time's up. The timer of the resolver think a group of 18 operations should have been done and it's time to notify everyone what DNS addresses it has. And because the handle one of RA DVD subscribes to the handle two of the resolvers, it receive the DNS addresses in the normal message. Then store them as a tree and use them to construct the RA for LAN in the RA routine. At the moment, the result of two requests is missing. That's why our first version of the exploit can only work on MacOS because we trigger the rest condition. It seems hard to achieve a rest condition because there is a timer, but the mechanism is actually more complex than I just explained. So, the rest will always happen as long as the two RAs arrive at the router in the short period. So, the pattern of the rest condition in a remote object is as follows. Use non-blocking method to create the lead remote object. And the target process subscribes to the remote object. Then we can mix the result of two requests into one. They might make us bypass some checks in the binary. We try to find other vulnerability with this pattern. So, that we can so that we could register the the LAN category of the router in . But we ran out of time and did not manage to find other vulnerability to exploit the LAN side of router OS. But at least we are the first team to exploit the SOHO smash up category successfully and also won the title of master of Pong. Okay. In summary, MikroTik re-implement everything with its own design IPC. The business logic is skated all over. We have to have an abstract concept of NOVA binary to find the most critical code to reversing. And there is a pre-auth RCE on WAN that has existed for nine years. Also, we introduced a pattern of REST condition in remote object due to non-blocking method to you. And the last thing, we will drop tools that we use in the Pong2Own events then to ease the reversing of router OS at GitHub. And that's it. Any question?