Welcome, everyone. Unfortunately, the screen will not be working. So some things might be hard to follow. But just know that if you don't understand something, it's not you. It's the screen. So anyway, my name is Aleksandr Lazar. I'm a security researcher at Bdefender and today we'll talk about cameras. Specifically, Dahua devices. How to get their firmware and two vulnerabilities that can be turned into code execution. But first, why cameras? Well, in the year 2025, people still expose their webcams to the Internet. Most of those devices have simple exploits that are also reliable for multiple versions. So it makes them the perfect targets for a botnet. Then there were recent military reports that these security cameras are being hacked in order to follow logistic movements. And another case where a ransomware gang used a camera to pivot inside the target network. So maybe we should pay a little bit of attention to these security cameras. But how do we do that? Well, to check if a camera is secure, we need to know what runs on it. And this means getting its firmware. There are a couple of ways to do this. We have the easy way by downloading it from the website or getting it through a UART shell or simply intercepting an update. Then we have the hardware way. Telepathy. A.K.A. memory reading. So for Dahua we had multiple devices, three cameras and an XVR. Two of the cameras and the XVR, it was okay. They had their firmware online, we just got it from their website. For the other camera, we had to read the flash ROM. And to do that, first you search for the data sheet of the chip to find the pin out. Then you have to connect the reader. In this case we used a bus pirate, but anything should work. And of course you have to desolder the chip first before connecting the reader because there might be interference. Flash ROM didn't work for this particular brand of chips, so we wrote a Python script using the commands that we found in the data sheet. But okay, we got the firmware from the chip, we have the firmware from the websites, what do we do now? Well, we don't want the firmware per se, we want the file system. So we need to extract it. Two of the cameras, we had an easy way with it because simply you extract the archive and use bin lock on the files. The problem was with the other two devices. The archive still contained files that had suggestive names like web.squashfs. But when we used bin lock on them we only found U image headers. The files were high entropy, so this means they are usually either compressed or encrypted. There were also Zlib signatures when you used bin lock on them. And these usually are false alarms, but when we extracted those we found strings, we found file names, even code. It was JavaScript but still code. So maybe not encrypted. At first we thought it might be a custom compression scheme that you usually find on those devices. But this was partially confirmed by the fact that the zip file signature which is PK was replaced by DH, presumably from Dahua. Also we had U image headers on the file system files which is not at all common and especially on a web cam. So the kernel wasn't readable either, so we had to go to the beginning, the boot loader. It was in the same archive, it wasn't encrypted and it was a U boot variant so time to load it in Ghidra. To do that you can use bin lock to see the layout of the file. And in this case just ignore the U image header and go for the LZMA compressed data. You then carve the file out with DD and extract it. The LZMA utilities might complain about some corrupt data on these specific devices, but you can use the Python modules, they don't complain, just do their job. Okay, so extraction looked good, we had strings, we had constants, so we had something valid. Load the binary in Ghidra with the R64 architecture on Little India and this is specifically for XVRs of Dahua. A base address of zero will work so time to have some fun. Where do you start when you load the boot loader? Well, you go for the constants, either strings or in this case because we suspected encryption we went for the AES substitution box. So check its references and go up the call tree until something starts to make sense. Here we found some strings like uboot encrypt file or firmware get key, so obviously we were on the right path. Judging by those strings and the nested if structure of the function, the decoding was happening in four steps. First we had the firmware, getting the firmware key, then setting key factors, then setting flags, then uboot encrypt file or so they call it. Okay. The first step is getting the firmware key. And I will show you the function but what basically happens there is that we have hard coded bytes that are copied inside a buffer. Those hard coded bytes appear to be the same on multiple devices but the first six bytes will be replaced with the processor name. So this firmware key will be different for different device types. Then we had three functions. And you can deduce they are hashing functions by looking at the constants in them. You need to find the algorithm still. Well to do that you can get those constants, plug them into chat GPT and get the wrong answer. And then with one option removed you will deduce it's shot to 56. Okay so they got a 32 bytes hash, they wanted a firmware key that's 16 bytes so they just store the first byte with the last one and so on and so forth. So we have the 16 bytes buffer which is the firmware key and it's time for the key factors. This step is different for the kernel and the file system but we'll focus on the file system. So they just set an empty buffer and use it as a parameter to the next function. The firmware key that we got from the first step will not be used here so we expect some other hard coded stuff. Let's go deeper. Here we will see, I mean you don't see but you see a nice little function with some bit wise operations, buffers that are hard coded and it can be a bit daunting at first when you see such a function. But it gets better if you take it step by step. First you need to find what those many function calls actually do. And in this case it was pretty simple. We had mem set and mem move. And for basic functions like this you can pretty much deduce them from context. For example, if you have two buffers as a parameter and a size you can probably guess it's a mem copy or a mem move. If you have a buffer and a size again you can guess it's a mem set . You can pretty much guess and then check for those guesses and it makes this whole reversing process a whole lot quicker. Okay, we have two functions left besides those mem copies and mem sets. And the first one had bit wise operations. Which and hard coded buffers. You can look those buffers up and in this case we found a match with the AST tables. Usually you might not find an exact match especially on AES because there are multiple implementations. You have hardware accelerated or fast implementations. But if some bytes match you can probably assume it's AES unless they modify some constants which happened before. But I digress. Okay, besides these T tables we had a loop that could go 10, 12 or 14 iterations. So if you are familiar with the algorithm you will probably guess it's the AES key expansion part. Okay, we have AES key expansion then the next function is probably some AES mode of operation. But we need to find it. This function had five parameters. It had probably the key, size, input and output. The fifth one could either be a flag for encryption or decryption but in this case we had a buffer so it's probably an IV. Still, AES has multiple modes that work with an IV like CBC, CFB, OFB. But you can again make assumptions. You take the most common mode of operation which is CBC and then check for it. Here we found something that looked like a high level AES CBC operation. So first you store the IV with the plain text then you call the block function and then the resulting cipher text will be the new IV. We found something similar so we just said okay, it's AES 256 CBC. Still, you need to find the parameters and you just go at the bottom, take each parameter one by one and see where it's modified. The IV was created in one wild loop and it consisted of six unique characters. The key was created in two loops and this time the bytes were incremented from 20 to 30 and 23 to 33. You still need the plain text and here we had two hard coded buffers, 32 ints and 32 bytes. These are the buffers that will be different for the kernel decryption and file system decryption. So those two buffers were added together, summed to 600 and divided again, cast to a byte and some other operations. But we obtained the plain text. Okay, 32 bytes plain text, we get a 32 byte cipher text. The last 16 bytes of this cipher text will be concatenated with the firmware key that we got in the first step. So at the end we have 32 bytes which from what we've already seen, it looks a lot like an AES key. Okay, we have two steps left, the setting flag step and the U-boot decrypt file step. And we take them together because it's easier to understand them and this setting flag step is not present on all devices. So we have a function with either seven parameters or five parameters if the setting flag step is not present. We'll go the XVR route which is with the seven parameters. Two of those from flags. Then we have the input address and its size and the output address and its size. Those two addresses are the same. So we will have in place decryption. Then a flag for either encryption or decryption. Okay. So we have the function. Yeah. And because we already named some other functions, this time it will be much faster. We have the buffer up there, we have the AES key expansion function and then in an if else statement we have the AESCBC encrypt function that we have already seen. So I wonder what could be in the else. Okay. We found a function for decryption but again we need parameters. We have a size of 200 X so it means we decrypt 32 blocks at a time. The IV will start at zero and it will be incremented. They cast the first four bytes to an int and increment those. Okay. We also have the input pointer and the offset that is added to it. So you decrypt 32 blocks, you increment the offset with 200 X and so on and so forth. The problem was that at some point this offset will be incremented by more than 200 X. So this means that some blocks will be skipped and not decrypted. And because we have in place decryption, they will remain in the output unchanged. So this basically solves the mystery of how we managed to find strings and file names and code inside the supposed encrypted block. Those blocks were never encrypted, just partially. Okay. To recap, we have the key from the first two steps, we have the IV, we have the algorithm which is AS to 56 CBC and we just need to find the input. If you remember earlier we have file system images with an image header which is 64 bytes in size. This header has a size field and the value of this field matched the number of bytes following the header. So again we carved those bytes out with DD, we plugged them into the decryption algorithm and great success. We have the file system, we have the files, now what? Okay. Now we go ham. First we map the attack surface. The binary that does everything will be called either Sonya or challenge depending on the device. This is where the listeners will be. If you only check the open ports with N map for example, you will only find three open ports for HTTP, RTSP and DHAP which is a custom protocol for Dahua. If you actually look at the listeners in the binary, you see that they cram protocols on the same port. So they use the reg X to parse the incoming packet and then select the correct handler for the respective protocol. So after seeing that, we discovered about 10 to 16 protocols depending on the device like UPNP, web socket handlers, RTSP over HTTP from 10 to 16. The problem was that not all were enabled on all devices. And most of them actually used authentication. Still as always there are handlers that don't use authentication and one of them was of course found on HTTP. On HTTP a client can use cookies, sessions, authorization headers with all kinds of hashes and most of them were pretty well implemented I might say. But there were a few handlers without authorization and one of them was called RPC2 upload file with name. But despite the name, the problem wasn't that you could upload files. Those files were just empty freeze that had a fixed path. The problem was that they parsed the header incorrectly. So usually you have the classical usage of the N string functions in C. You copy a string into a buffer but then instead of using the size of the buffer you use the size of the string. Here the CSEC header will overflow into the BSS section after only 34 hex bytes. The binary crashes of course. In BSS you mostly find global variables or static variables. Here we have overwritten a pointer so I already thought I had to get out the maloc malificarum to exploit it. But before we go deeper into the arcane magic that is heap exploitation, I wanted to check why the program actually crashed. The pointer we overwritten turned out to hold some structure related to the sessions, the HTTP sessions. First I thought I will recreate this structure in memory and give myself a session in order to increase the attack surface. But the crashing function gave me hope. So it was checking for the token expiration and if this token will be expired it will delete it. But this deletion happened with a function pointer that was stored in the same structure I wanted to overwrite. So the plan should be simple now, right? Rewrite the structure in memory and replace this cleanup function pointer with a system call maybe. The problem was that I didn't have a debugger because I didn't have a shell. Okay, I didn't have a debugger anyway but you will see why later. We also had another camera that had shell access but that camera didn't have the HTTP service so we couldn't do some parallel reconstruction on it. So first I tried static analysis but the results, not the best. We had linked lists, dictionaries, pointers to pointers, some structures had cookies in them that were checked so it was very hard getting it right in one shot. But thankfully this camera had a TX pin enabled so when it crashed it printed the registers and the registers are usually all you need to make an exploit. So I started rewriting the structure step-by-step one crash at a time. Most of the crashes were just null pointer references or illegal memory accesses. When I met one of those I just looked at the program counter, the register that caused the crash and made that register point inside my structure so it wouldn't crash anymore. If you do this 15 more times the program will still crash. But this time it will be with the program counter not in the code section error. And that is when you know you've made it. Okay. Where is the function pointer? You replace it with system. But we need a payload to run. We didn't have reverse cell utilities on the camera like netcat but we had TFTP so we could download files. The problem was the kernel didn't run unsigned binaries. So we couldn't run just anything. Pretty impressive protection for a webcam. Still we could load libraries using the LD preload privilege escalation trick. So we needed a shared library that was compiled for that specific architecture and thankfully we had a tool chain. And if you remember earlier I said I didn't have a debugger. But that's not for lack of trying. Turns out it's much more difficult than it sounds to cross compile GDB server as a library that runs when it's loaded. Still. With that effort left me with a good tool chain and I will show you the command but I can't and I won't spell it because it's very long. So just be careful when you use build root to use a hard floating point. Okay. We compiled a bind shell, made a payload to download it with TFTP and we got root. You can't see it but we had root. Just trust me. Okay. We had one root. Time for a second one. The previous exploit was on the HTTP service. And this one will be on ONVIF which is specific to IP cameras. It's still HTTP but it uses some different parsers. You have the classical ONVIF end points but everything interesting is usually behind authentication. We had access to getter functions but as the name says they don't parse parameters. We had the get capabilities and get service capabilities and those would take the host header and use it to display the end points to a client. The available end points. So we only control the host header. This was the only thing that these end points parsed and guess what? They did so incorrectly. If this header didn't have a square bracket in it everything was good. My header would be copied on the stack but the size was checked. Only 64 bytes. If we imitated an IPV6 and put a square bracket in our host header the size checking would be skipped and everything copied on the stack. So we had a classical stack based buffer overflow. But we need to set some conditions for it. We control seven registers from R4 to R11. Then the function that causes the problem is string and copy. So in our payload we only have access to one null byte and it must be the last null byte. Why is that a problem? The code of the camera is not position independent. So on every run it will be loaded at the same address. The problem is it's a low address so it contains a null byte in it. This means our rope chain that we want to create can only contain one gadget. And usually when you only have one gadget at your disposal you go for a system gadget because it's just easier and you have the greatest chances of getting code execution. So we had a system gadget. We could put an address to a payload in R6 which we control. But still we don't have a payload. You won't find some bind shell or reverse shell string on a camera. So we need a way to store the payload and a place to store it. The place should be easy. We write into the BSS section. It is writable. No null bytes in its address because it sits higher up. But how do we do it? Well, maybe instead of going for the system gadget which would be useless, we can find a gadget that actually writes our payload into memory. Okay. But where do we go? In a stack overflow where you don't have a null byte, you usually need some kind of memory leak to find the addresses of libraries. So we can make a rob gadget using them. We didn't have a memory leak. So we were calling one gadget. Hopefully it would write four bytes, two bytes in memory. But then it would crash because its epilogue would be different from the epilogue of the caller function. So you have the caller function which calls the vulnerable function where you overwrite the return address and then go to the gadget. The gadget does its stuff but then the stack pointer will be left in some random place. So the program crashes, our memory is emptied, no more payload. But if you understand this problem, you understand the solution because we need a gadget that has the same epilogue as the caller function. So when we jump to our gadget, our gadget writes something on the stack and then the stack pointer will be left in its proper place. So the call execution flow will go back to the function above the caller function and our payload remains in memory. Okay. Such a gadget sounds hard to find. But we had a 20 megabytes binary. So we had plenty to choose from. We used the instruction search window to find matching epilogues with the caller function. And we found about 15, 60 functions. So pretty good. We chose one where a store instruction was as close as possible to the return instruction. So we have the store instruction from R7 to R5. We control both of those. So this means we can write four bytes at a time in the BSS section. Okay. What payload do we use for system? Well, the same payload we used for the other exploit. With using LD preload we can load the library. We have the payload here. R4, R5, R7. Controlled. Then the gadget address. And then after we write this payload in memory, after about 10 to 15 requests, we can jump to the system gadget. And because we control the address of the payload, we put it in R6. And again, root. The second one. Okay. We have a timeline. The team was great to work with. They used PGP key. They answer fast. They actually fixed the problem. And they even gave us a list of devices affected which I don't think I've actually seen other IOT vendors do when we report something to them. So, yeah, kudos to them. Okay. This was it. Thank you for coming. I hope you learned something. Sorry for the screen. And if you have questions, there might be questions, but go ahead. If not, you can maybe find me at the IOT village sometimes. But I won't guarantee it. Thank you.