1 00:00:00,000 --> 00:00:01,220 Welcome, everyone. 2 00:00:01,620 --> 00:00:04,380 Unfortunately, the screen will not be working. 3 00:00:04,400 --> 00:00:06,860 So some things might be hard to follow. 4 00:00:06,860 --> 00:00:10,540 But just know that if you don't understand something, it's not you. 5 00:00:10,540 --> 00:00:11,600 It's the screen. 6 00:00:12,660 --> 00:00:15,860 So anyway, my name is Aleksandr Lazar. 7 00:00:15,880 --> 00:00:21,220 I'm a security researcher at Bdefender and today we'll talk about cameras. 8 00:00:21,440 --> 00:00:23,860 Specifically, Dahua devices. 9 00:00:23,860 --> 00:00:30,100 How to get their firmware and two vulnerabilities that can be turned into code execution. 10 00:00:31,420 --> 00:00:33,360 But first, why cameras? 11 00:00:33,420 --> 00:00:40,080 Well, in the year 2025, people still expose their webcams to the Internet. 12 00:00:40,900 --> 00:00:46,080 Most of those devices have simple exploits that are also reliable for multiple versions. 13 00:00:46,080 --> 00:00:49,060 So it makes them the perfect targets for a botnet. 14 00:00:49,500 --> 00:00:58,380 Then there were recent military reports that these security cameras are being hacked in order to follow logistic movements. 15 00:00:59,420 --> 00:01:05,200 And another case where a ransomware gang used a camera to pivot inside the target network. 16 00:01:05,600 --> 00:01:09,560 So maybe we should pay a little bit of attention to these security cameras. 17 00:01:10,300 --> 00:01:11,680 But how do we do that? 18 00:01:12,320 --> 00:01:16,320 Well, to check if a camera is secure, we need to know what runs on it. 19 00:01:16,320 --> 00:01:18,040 And this means getting its firmware. 20 00:01:19,020 --> 00:01:20,680 There are a couple of ways to do this. 21 00:01:20,680 --> 00:01:28,900 We have the easy way by downloading it from the website or getting it through a UART shell or simply intercepting an update. 22 00:01:29,440 --> 00:01:31,600 Then we have the hardware way. 23 00:01:31,600 --> 00:01:32,600 Telepathy. 24 00:01:32,880 --> 00:01:33,300 A.K.A. 25 00:01:33,300 --> 00:01:34,480 memory reading. 26 00:01:35,240 --> 00:01:40,800 So for Dahua we had multiple devices, three cameras and an XVR. 27 00:01:41,300 --> 00:01:44,180 Two of the cameras and the XVR, it was okay. 28 00:01:44,180 --> 00:01:47,220 They had their firmware online, we just got it from their website. 29 00:01:47,620 --> 00:01:50,500 For the other camera, we had to read the flash ROM. 30 00:01:50,500 --> 00:01:57,520 And to do that, first you search for the data sheet of the chip to find the pin out. 31 00:01:58,200 --> 00:02:00,500 Then you have to connect the reader. 32 00:02:00,500 --> 00:02:03,680 In this case we used a bus pirate, but anything should work. 33 00:02:04,680 --> 00:02:09,160 And of course you have to desolder the chip first before connecting the reader because there might be interference. 34 00:02:10,880 --> 00:02:18,660 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. 35 00:02:19,060 --> 00:02:25,560 But okay, we got the firmware from the chip, we have the firmware from the websites, what do we do now? 36 00:02:25,740 --> 00:02:30,160 Well, we don't want the firmware per se, we want the file system. 37 00:02:30,440 --> 00:02:32,280 So we need to extract it. 38 00:02:32,640 --> 00:02:40,620 Two of the cameras, we had an easy way with it because simply you extract the archive and use bin lock on the files. 39 00:02:41,160 --> 00:02:43,280 The problem was with the other two devices. 40 00:02:44,920 --> 00:02:52,160 The archive still contained files that had suggestive names like web.squashfs. 41 00:02:52,360 --> 00:02:56,760 But when we used bin lock on them we only found U image headers. 42 00:02:57,900 --> 00:03:04,040 The files were high entropy, so this means they are usually either compressed or encrypted. 43 00:03:04,900 --> 00:03:09,360 There were also Zlib signatures when you used bin lock on them. 44 00:03:09,480 --> 00:03:18,180 And these usually are false alarms, but when we extracted those we found strings, we found file names, even code. 45 00:03:18,180 --> 00:03:20,530 It was JavaScript but still code. 46 00:03:21,160 --> 00:03:23,760 So maybe not encrypted. 47 00:03:24,640 --> 00:03:30,820 At first we thought it might be a custom compression scheme that you usually find on those devices. 48 00:03:31,400 --> 00:03:41,900 But this was partially confirmed by the fact that the zip file signature which is PK was replaced by DH, presumably from Dahua. 49 00:03:42,220 --> 00:03:52,180 Also we had U image headers on the file system files which is not at all common and especially on a web cam. 50 00:03:52,760 --> 00:03:58,100 So the kernel wasn't readable either, so we had to go to the beginning, the boot loader. 51 00:03:59,120 --> 00:04:06,020 It was in the same archive, it wasn't encrypted and it was a U boot variant so time to load it in Ghidra. 52 00:04:07,120 --> 00:04:11,540 To do that you can use bin lock to see the layout of the file. 53 00:04:11,640 --> 00:04:17,200 And in this case just ignore the U image header and go for the LZMA compressed data. 54 00:04:18,300 --> 00:04:22,820 You then carve the file out with DD and extract it. 55 00:04:23,020 --> 00:04:32,260 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. 56 00:04:33,340 --> 00:04:39,500 Okay, so extraction looked good, we had strings, we had constants, so we had something valid. 57 00:04:41,000 --> 00:04:48,140 Load the binary in Ghidra with the R64 architecture on Little India and this is specifically for XVRs of Dahua. 58 00:04:48,580 --> 00:04:53,200 A base address of zero will work so time to have some fun. 59 00:04:55,060 --> 00:04:57,940 Where do you start when you load the boot loader? 60 00:04:57,940 --> 00:05:06,920 Well, you go for the constants, either strings or in this case because we suspected encryption we went for the AES substitution box. 61 00:05:07,300 --> 00:05:12,580 So check its references and go up the call tree until something starts to make sense. 62 00:05:13,100 --> 00:05:20,660 Here we found some strings like uboot encrypt file or firmware get key, so obviously we were on the right path. 63 00:05:22,340 --> 00:05:28,180 Judging by those strings and the nested if structure of the function, the decoding was happening in four steps. 64 00:05:28,180 --> 00:05:35,980 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. 65 00:05:36,760 --> 00:05:37,520 Okay. 66 00:05:38,100 --> 00:05:40,640 The first step is getting the firmware key. 67 00:05:41,100 --> 00:05:51,060 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. 68 00:05:51,480 --> 00:06:00,040 Those hard coded bytes appear to be the same on multiple devices but the first six bytes will be replaced with the processor name. 69 00:06:00,220 --> 00:06:04,600 So this firmware key will be different for different device types. 70 00:06:05,100 --> 00:06:07,340 Then we had three functions. 71 00:06:07,480 --> 00:06:12,180 And you can deduce they are hashing functions by looking at the constants in them. 72 00:06:12,600 --> 00:06:14,360 You need to find the algorithm still. 73 00:06:14,620 --> 00:06:20,520 Well to do that you can get those constants, plug them into chat GPT and get the wrong answer. 74 00:06:21,000 --> 00:06:26,060 And then with one option removed you will deduce it's shot to 56. 75 00:06:26,760 --> 00:06:34,940 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. 76 00:06:36,780 --> 00:06:41,680 So we have the 16 bytes buffer which is the firmware key and it's time for the key factors. 77 00:06:42,280 --> 00:06:47,700 This step is different for the kernel and the file system but we'll focus on the file system. 78 00:06:49,480 --> 00:06:54,200 So they just set an empty buffer and use it as a parameter to the next function. 79 00:06:54,860 --> 00:07:01,280 The firmware key that we got from the first step will not be used here so we expect some other hard coded stuff. 80 00:07:01,940 --> 00:07:02,900 Let's go deeper. 81 00:07:04,080 --> 00:07:16,820 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. 82 00:07:16,820 --> 00:07:19,640 But it gets better if you take it step by step. 83 00:07:20,080 --> 00:07:23,940 First you need to find what those many function calls actually do. 84 00:07:24,260 --> 00:07:25,840 And in this case it was pretty simple. 85 00:07:25,840 --> 00:07:27,940 We had mem set and mem move. 86 00:07:28,380 --> 00:07:33,120 And for basic functions like this you can pretty much deduce them from context. 87 00:07:33,200 --> 00:07:40,100 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. 88 00:07:40,300 --> 00:07:45,380 If you have a buffer and a size again you can guess it's a mem set . 89 00:07:45,800 --> 00:07:54,220 You can pretty much guess and then check for those guesses and it makes this whole reversing process a whole lot quicker. 90 00:07:54,860 --> 00:07:59,380 Okay, we have two functions left besides those mem copies and mem sets. 91 00:08:00,380 --> 00:08:04,060 And the first one had bit wise operations. 92 00:08:04,080 --> 00:08:06,840 Which and hard coded buffers. 93 00:08:07,080 --> 00:08:12,560 You can look those buffers up and in this case we found a match with the AST tables. 94 00:08:13,320 --> 00:08:19,220 Usually you might not find an exact match especially on AES because there are multiple implementations. 95 00:08:19,220 --> 00:08:23,200 You have hardware accelerated or fast implementations. 96 00:08:23,200 --> 00:08:29,840 But if some bytes match you can probably assume it's AES unless they modify some constants which happened before. 97 00:08:30,020 --> 00:08:31,940 But I digress. 98 00:08:32,080 --> 00:08:37,460 Okay, besides these T tables we had a loop that could go 10, 12 or 14 iterations. 99 00:08:37,520 --> 00:08:43,180 So if you are familiar with the algorithm you will probably guess it's the AES key expansion part. 100 00:08:43,640 --> 00:08:51,120 Okay, we have AES key expansion then the next function is probably some AES mode of operation. 101 00:08:51,120 --> 00:08:52,680 But we need to find it. 102 00:08:53,460 --> 00:08:55,900 This function had five parameters. 103 00:08:55,900 --> 00:09:00,400 It had probably the key, size, input and output. 104 00:09:01,000 --> 00:09:08,280 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. 105 00:09:08,740 --> 00:09:14,520 Still, AES has multiple modes that work with an IV like CBC, CFB, OFB. 106 00:09:14,560 --> 00:09:17,860 But you can again make assumptions. 107 00:09:18,040 --> 00:09:22,440 You take the most common mode of operation which is CBC and then check for it. 108 00:09:23,820 --> 00:09:29,180 Here we found something that looked like a high level AES CBC operation. 109 00:09:29,180 --> 00:09:37,820 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. 110 00:09:38,460 --> 00:09:43,780 We found something similar so we just said okay, it's AES 256 CBC. 111 00:09:44,860 --> 00:09:51,700 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. 112 00:09:52,400 --> 00:09:59,040 The IV was created in one wild loop and it consisted of six unique characters. 113 00:09:59,740 --> 00:10:07,080 The key was created in two loops and this time the bytes were incremented from 20 to 30 and 23 to 33. 114 00:10:08,280 --> 00:10:14,300 You still need the plain text and here we had two hard coded buffers, 32 ints and 32 bytes. 115 00:10:14,300 --> 00:10:19,200 These are the buffers that will be different for the kernel decryption and file system decryption. 116 00:10:19,500 --> 00:10:27,240 So those two buffers were added together, summed to 600 and divided again, cast to a byte and some other operations. 117 00:10:27,320 --> 00:10:29,620 But we obtained the plain text. 118 00:10:30,900 --> 00:10:34,740 Okay, 32 bytes plain text, we get a 32 byte cipher text. 119 00:10:34,740 --> 00:10:40,980 The last 16 bytes of this cipher text will be concatenated with the firmware key that we got in the first step. 120 00:10:40,980 --> 00:10:50,180 So at the end we have 32 bytes which from what we've already seen, it looks a lot like an AES key. 121 00:10:51,860 --> 00:10:57,520 Okay, we have two steps left, the setting flag step and the U-boot decrypt file step. 122 00:10:57,560 --> 00:11:04,000 And we take them together because it's easier to understand them and this setting flag step is not present on all devices. 123 00:11:06,060 --> 00:11:13,360 So we have a function with either seven parameters or five parameters if the setting flag step is not present. 124 00:11:13,360 --> 00:11:16,820 We'll go the XVR route which is with the seven parameters. 125 00:11:16,820 --> 00:11:18,620 Two of those from flags. 126 00:11:18,620 --> 00:11:24,000 Then we have the input address and its size and the output address and its size. 127 00:11:24,140 --> 00:11:26,220 Those two addresses are the same. 128 00:11:26,220 --> 00:11:28,820 So we will have in place decryption. 129 00:11:29,120 --> 00:11:31,680 Then a flag for either encryption or decryption. 130 00:11:33,440 --> 00:11:34,160 Okay. 131 00:11:34,160 --> 00:11:35,860 So we have the function. 132 00:11:36,060 --> 00:11:36,580 Yeah. 133 00:11:38,360 --> 00:11:43,260 And because we already named some other functions, this time it will be much faster. 134 00:11:43,260 --> 00:11:54,600 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. 135 00:11:54,700 --> 00:11:56,820 So I wonder what could be in the else. 136 00:11:57,580 --> 00:11:58,500 Okay. 137 00:11:58,860 --> 00:12:02,660 We found a function for decryption but again we need parameters. 138 00:12:03,100 --> 00:12:08,340 We have a size of 200 X so it means we decrypt 32 blocks at a time. 139 00:12:09,280 --> 00:12:12,680 The IV will start at zero and it will be incremented. 140 00:12:12,760 --> 00:12:16,240 They cast the first four bytes to an int and increment those. 141 00:12:17,860 --> 00:12:18,280 Okay. 142 00:12:18,280 --> 00:12:22,400 We also have the input pointer and the offset that is added to it. 143 00:12:22,400 --> 00:12:28,560 So you decrypt 32 blocks, you increment the offset with 200 X and so on and so forth. 144 00:12:28,900 --> 00:12:35,360 The problem was that at some point this offset will be incremented by more than 200 X. 145 00:12:35,720 --> 00:12:39,640 So this means that some blocks will be skipped and not decrypted. 146 00:12:39,720 --> 00:12:43,960 And because we have in place decryption, they will remain in the output unchanged. 147 00:12:44,500 --> 00:12:55,940 So this basically solves the mystery of how we managed to find strings and file names and code inside the supposed encrypted block. 148 00:12:56,640 --> 00:12:59,300 Those blocks were never encrypted, just partially. 149 00:13:00,660 --> 00:13:01,420 Okay. 150 00:13:01,680 --> 00:13:12,680 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. 151 00:13:14,080 --> 00:13:20,420 If you remember earlier we have file system images with an image header which is 64 bytes in size. 152 00:13:20,600 --> 00:13:26,700 This header has a size field and the value of this field matched the number of bytes following the header. 153 00:13:27,100 --> 00:13:35,360 So again we carved those bytes out with DD, we plugged them into the decryption algorithm and great success. 154 00:13:35,540 --> 00:13:40,260 We have the file system, we have the files, now what? 155 00:13:40,700 --> 00:13:41,320 Okay. 156 00:13:41,320 --> 00:13:42,800 Now we go ham. 157 00:13:43,140 --> 00:13:45,440 First we map the attack surface. 158 00:13:45,900 --> 00:13:50,700 The binary that does everything will be called either Sonya or challenge depending on the device. 159 00:13:50,840 --> 00:13:52,860 This is where the listeners will be. 160 00:13:53,200 --> 00:14:04,160 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. 161 00:14:04,400 --> 00:14:11,460 If you actually look at the listeners in the binary, you see that they cram protocols on the same port. 162 00:14:11,760 --> 00:14:19,020 So they use the reg X to parse the incoming packet and then select the correct handler for the respective protocol. 163 00:14:19,960 --> 00:14:30,980 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. 164 00:14:31,760 --> 00:14:36,120 The problem was that not all were enabled on all devices. 165 00:14:36,280 --> 00:14:39,360 And most of them actually used authentication. 166 00:14:40,340 --> 00:14:48,540 Still as always there are handlers that don't use authentication and one of them was of course found on HTTP. 167 00:14:49,740 --> 00:14:59,080 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. 168 00:14:59,300 --> 00:15:07,260 But there were a few handlers without authorization and one of them was called RPC2 upload file with name. 169 00:15:07,260 --> 00:15:12,100 But despite the name, the problem wasn't that you could upload files. 170 00:15:12,200 --> 00:15:15,740 Those files were just empty freeze that had a fixed path. 171 00:15:16,200 --> 00:15:18,880 The problem was that they parsed the header incorrectly. 172 00:15:20,640 --> 00:15:26,220 So usually you have the classical usage of the N string functions in C. 173 00:15:26,540 --> 00:15:33,960 You copy a string into a buffer but then instead of using the size of the buffer you use the size of the string. 174 00:15:34,320 --> 00:15:41,500 Here the CSEC header will overflow into the BSS section after only 34 hex bytes. 175 00:15:42,320 --> 00:15:44,500 The binary crashes of course. 176 00:15:45,560 --> 00:15:50,220 In BSS you mostly find global variables or static variables. 177 00:15:50,540 --> 00:15:57,760 Here we have overwritten a pointer so I already thought I had to get out the maloc malificarum to exploit it. 178 00:15:58,000 --> 00:16:07,760 But before we go deeper into the arcane magic that is heap exploitation, I wanted to check why the program actually crashed. 179 00:16:09,320 --> 00:16:17,000 The pointer we overwritten turned out to hold some structure related to the sessions, the HTTP sessions. 180 00:16:17,380 --> 00:16:24,540 First I thought I will recreate this structure in memory and give myself a session in order to increase the attack surface. 181 00:16:25,280 --> 00:16:27,360 But the crashing function gave me hope. 182 00:16:27,360 --> 00:16:34,380 So it was checking for the token expiration and if this token will be expired it will delete it. 183 00:16:34,380 --> 00:16:41,100 But this deletion happened with a function pointer that was stored in the same structure I wanted to overwrite. 184 00:16:42,380 --> 00:16:45,160 So the plan should be simple now, right? 185 00:16:45,420 --> 00:16:51,000 Rewrite the structure in memory and replace this cleanup function pointer with a system call maybe. 186 00:16:51,780 --> 00:16:56,380 The problem was that I didn't have a debugger because I didn't have a shell. 187 00:16:56,860 --> 00:17:00,080 Okay, I didn't have a debugger anyway but you will see why later. 188 00:17:00,820 --> 00:17:10,760 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. 189 00:17:11,900 --> 00:17:17,700 So first I tried static analysis but the results, not the best. 190 00:17:17,720 --> 00:17:28,100 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. 191 00:17:28,380 --> 00:17:39,280 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. 192 00:17:39,280 --> 00:17:44,440 So I started rewriting the structure step-by-step one crash at a time. 193 00:17:46,100 --> 00:17:51,160 Most of the crashes were just null pointer references or illegal memory accesses. 194 00:17:51,280 --> 00:18:02,080 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. 195 00:18:02,700 --> 00:18:07,420 If you do this 15 more times the program will still crash. 196 00:18:07,940 --> 00:18:12,800 But this time it will be with the program counter not in the code section error. 197 00:18:13,100 --> 00:18:15,360 And that is when you know you've made it. 198 00:18:16,120 --> 00:18:16,860 Okay. 199 00:18:16,920 --> 00:18:18,380 Where is the function pointer? 200 00:18:18,380 --> 00:18:19,980 You replace it with system. 201 00:18:20,660 --> 00:18:22,580 But we need a payload to run. 202 00:18:23,620 --> 00:18:30,180 We didn't have reverse cell utilities on the camera like netcat but we had TFTP so we could download files. 203 00:18:30,800 --> 00:18:35,660 The problem was the kernel didn't run unsigned binaries. 204 00:18:35,900 --> 00:18:38,800 So we couldn't run just anything. 205 00:18:39,620 --> 00:18:42,060 Pretty impressive protection for a webcam. 206 00:18:42,160 --> 00:18:47,320 Still we could load libraries using the LD preload privilege escalation trick. 207 00:18:48,540 --> 00:18:55,300 So we needed a shared library that was compiled for that specific architecture and thankfully we had a tool chain. 208 00:18:55,600 --> 00:18:58,640 And if you remember earlier I said I didn't have a debugger. 209 00:18:58,640 --> 00:19:00,580 But that's not for lack of trying. 210 00:19:00,880 --> 00:19:07,780 Turns out it's much more difficult than it sounds to cross compile GDB server as a library that runs when it's loaded. 211 00:19:08,260 --> 00:19:09,040 Still. 212 00:19:09,660 --> 00:19:17,120 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. 213 00:19:17,420 --> 00:19:22,020 So just be careful when you use build root to use a hard floating point. 214 00:19:24,220 --> 00:19:24,880 Okay. 215 00:19:24,880 --> 00:19:31,860 We compiled a bind shell, made a payload to download it with TFTP and we got root. 216 00:19:31,860 --> 00:19:33,520 You can't see it but we had root. 217 00:19:33,520 --> 00:19:34,460 Just trust me. 218 00:19:37,130 --> 00:19:37,870 Okay. 219 00:19:40,980 --> 00:19:42,520 We had one root. 220 00:19:42,860 --> 00:19:44,160 Time for a second one. 221 00:19:45,520 --> 00:19:48,460 The previous exploit was on the HTTP service. 222 00:19:48,980 --> 00:19:55,260 And this one will be on ONVIF which is specific to IP cameras. 223 00:19:55,340 --> 00:19:59,180 It's still HTTP but it uses some different parsers. 224 00:19:59,900 --> 00:20:05,840 You have the classical ONVIF end points but everything interesting is usually behind authentication. 225 00:20:06,380 --> 00:20:12,280 We had access to getter functions but as the name says they don't parse parameters. 226 00:20:13,420 --> 00:20:23,460 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. 227 00:20:23,660 --> 00:20:25,140 The available end points. 228 00:20:25,480 --> 00:20:27,360 So we only control the host header. 229 00:20:27,360 --> 00:20:32,260 This was the only thing that these end points parsed and guess what? 230 00:20:32,260 --> 00:20:34,060 They did so incorrectly. 231 00:20:34,940 --> 00:20:40,240 If this header didn't have a square bracket in it everything was good. 232 00:20:40,480 --> 00:20:44,400 My header would be copied on the stack but the size was checked. 233 00:20:44,400 --> 00:20:45,840 Only 64 bytes. 234 00:20:46,060 --> 00:20:57,880 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. 235 00:20:58,120 --> 00:21:00,880 So we had a classical stack based buffer overflow. 236 00:21:01,220 --> 00:21:03,560 But we need to set some conditions for it. 237 00:21:04,000 --> 00:21:07,280 We control seven registers from R4 to R11. 238 00:21:07,480 --> 00:21:11,160 Then the function that causes the problem is string and copy. 239 00:21:11,160 --> 00:21:17,440 So in our payload we only have access to one null byte and it must be the last null byte. 240 00:21:18,280 --> 00:21:19,960 Why is that a problem? 241 00:21:20,260 --> 00:21:24,180 The code of the camera is not position independent. 242 00:21:24,180 --> 00:21:27,760 So on every run it will be loaded at the same address. 243 00:21:28,020 --> 00:21:31,600 The problem is it's a low address so it contains a null byte in it. 244 00:21:31,800 --> 00:21:36,300 This means our rope chain that we want to create can only contain one gadget. 245 00:21:36,820 --> 00:21:47,340 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. 246 00:21:48,040 --> 00:21:50,700 So we had a system gadget. 247 00:21:50,700 --> 00:21:55,340 We could put an address to a payload in R6 which we control. 248 00:21:55,500 --> 00:21:58,160 But still we don't have a payload. 249 00:21:58,420 --> 00:22:03,820 You won't find some bind shell or reverse shell string on a camera. 250 00:22:04,640 --> 00:22:08,440 So we need a way to store the payload and a place to store it. 251 00:22:08,700 --> 00:22:10,380 The place should be easy. 252 00:22:10,500 --> 00:22:12,460 We write into the BSS section. 253 00:22:12,860 --> 00:22:14,120 It is writable. 254 00:22:14,280 --> 00:22:17,340 No null bytes in its address because it sits higher up. 255 00:22:17,900 --> 00:22:19,300 But how do we do it? 256 00:22:19,560 --> 00:22:29,120 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. 257 00:22:31,560 --> 00:22:32,060 Okay. 258 00:22:32,060 --> 00:22:33,500 But where do we go? 259 00:22:33,940 --> 00:22:42,220 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. 260 00:22:42,220 --> 00:22:45,120 So we can make a rob gadget using them. 261 00:22:45,800 --> 00:22:47,560 We didn't have a memory leak. 262 00:22:47,760 --> 00:22:51,160 So we were calling one gadget. 263 00:22:51,160 --> 00:22:55,180 Hopefully it would write four bytes, two bytes in memory. 264 00:22:55,180 --> 00:23:03,320 But then it would crash because its epilogue would be different from the epilogue of the caller function. 265 00:23:03,320 --> 00:23:10,760 So you have the caller function which calls the vulnerable function where you overwrite the return address and then go to the gadget. 266 00:23:10,760 --> 00:23:16,800 The gadget does its stuff but then the stack pointer will be left in some random place. 267 00:23:17,320 --> 00:23:22,320 So the program crashes, our memory is emptied, no more payload. 268 00:23:23,220 --> 00:23:32,060 But if you understand this problem, you understand the solution because we need a gadget that has the same epilogue as the caller function. 269 00:23:32,220 --> 00:23:41,080 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. 270 00:23:41,500 --> 00:23:51,840 So the call execution flow will go back to the function above the caller function and our payload remains in memory. 271 00:23:52,740 --> 00:23:53,400 Okay. 272 00:23:53,400 --> 00:23:56,400 Such a gadget sounds hard to find. 273 00:23:56,540 --> 00:23:58,640 But we had a 20 megabytes binary. 274 00:23:58,640 --> 00:24:01,460 So we had plenty to choose from. 275 00:24:01,860 --> 00:24:08,960 We used the instruction search window to find matching epilogues with the caller function. 276 00:24:08,960 --> 00:24:12,360 And we found about 15, 60 functions. 277 00:24:12,520 --> 00:24:14,360 So pretty good. 278 00:24:15,280 --> 00:24:23,120 We chose one where a store instruction was as close as possible to the return instruction. 279 00:24:23,220 --> 00:24:26,880 So we have the store instruction from R7 to R5. 280 00:24:26,880 --> 00:24:28,480 We control both of those. 281 00:24:28,480 --> 00:24:32,960 So this means we can write four bytes at a time in the BSS section. 282 00:24:34,460 --> 00:24:35,300 Okay. 283 00:24:36,500 --> 00:24:38,760 What payload do we use for system? 284 00:24:38,760 --> 00:24:42,700 Well, the same payload we used for the other exploit. 285 00:24:43,840 --> 00:24:46,620 With using LD preload we can load the library. 286 00:24:47,800 --> 00:24:49,580 We have the payload here. 287 00:24:49,580 --> 00:24:51,220 R4, R5, R7. 288 00:24:51,240 --> 00:24:52,360 Controlled. 289 00:24:52,520 --> 00:24:54,060 Then the gadget address. 290 00:24:54,680 --> 00:25:03,040 And then after we write this payload in memory, after about 10 to 15 requests, we can jump to the system gadget. 291 00:25:03,300 --> 00:25:07,800 And because we control the address of the payload, we put it in R6. 292 00:25:08,360 --> 00:25:10,760 And again, root. 293 00:25:11,180 --> 00:25:12,460 The second one. 294 00:25:13,140 --> 00:25:13,960 Okay. 295 00:25:14,860 --> 00:25:16,540 We have a timeline. 296 00:25:16,960 --> 00:25:19,020 The team was great to work with. 297 00:25:19,020 --> 00:25:20,840 They used PGP key. 298 00:25:20,880 --> 00:25:22,480 They answer fast. 299 00:25:22,480 --> 00:25:24,400 They actually fixed the problem. 300 00:25:24,760 --> 00:25:33,580 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. 301 00:25:33,900 --> 00:25:35,560 So, yeah, kudos to them. 302 00:25:37,860 --> 00:25:38,660 Okay. 303 00:25:38,940 --> 00:25:39,800 This was it. 304 00:25:39,800 --> 00:25:40,860 Thank you for coming. 305 00:25:40,860 --> 00:25:42,840 I hope you learned something. 306 00:25:43,320 --> 00:25:44,740 Sorry for the screen. 307 00:25:50,700 --> 00:25:56,200 And if you have questions, there might be questions, but go ahead. 308 00:25:59,000 --> 00:26:02,760 If not, you can maybe find me at the IOT village sometimes. 309 00:26:02,760 --> 00:26:04,840 But I won't guarantee it. 310 00:26:04,840 --> 00:26:06,120 Thank you.