start	end	text
0	1220	Welcome, everyone.
1620	4380	Unfortunately, the screen will not be working.
4400	6860	So some things might be hard to follow.
6860	10540	But just know that if you don't understand something, it's not you.
10540	11600	It's the screen.
12660	15860	So anyway, my name is Aleksandr Lazar.
15880	21220	I'm a security researcher at Bdefender and today we'll talk about cameras.
21440	23860	Specifically, Dahua devices.
23860	30100	How to get their firmware and two vulnerabilities that can be turned into code execution.
31420	33360	But first, why cameras?
33420	40080	Well, in the year 2025, people still expose their webcams to the Internet.
40900	46080	Most of those devices have simple exploits that are also reliable for multiple versions.
46080	49060	So it makes them the perfect targets for a botnet.
49500	58380	Then there were recent military reports that these security cameras are being hacked in order to follow logistic movements.
59420	65200	And another case where a ransomware gang used a camera to pivot inside the target network.
65600	69560	So maybe we should pay a little bit of attention to these security cameras.
70300	71680	But how do we do that?
72320	76320	Well, to check if a camera is secure, we need to know what runs on it.
76320	78040	And this means getting its firmware.
79020	80680	There are a couple of ways to do this.
80680	88900	We have the easy way by downloading it from the website or getting it through a UART shell or simply intercepting an update.
89440	91600	Then we have the hardware way.
91600	92600	Telepathy.
92880	93300	A.K.A.
93300	94480	memory reading.
95240	100800	So for Dahua we had multiple devices, three cameras and an XVR.
101300	104180	Two of the cameras and the XVR, it was okay.
104180	107220	They had their firmware online, we just got it from their website.
107620	110500	For the other camera, we had to read the flash ROM.
110500	117520	And to do that, first you search for the data sheet of the chip to find the pin out.
118200	120500	Then you have to connect the reader.
120500	123680	In this case we used a bus pirate, but anything should work.
124680	129160	And of course you have to desolder the chip first before connecting the reader because there might be interference.
130880	138660	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.
139060	145560	But okay, we got the firmware from the chip, we have the firmware from the websites, what do we do now?
145740	150160	Well, we don't want the firmware per se, we want the file system.
150440	152280	So we need to extract it.
152640	160620	Two of the cameras, we had an easy way with it because simply you extract the archive and use bin lock on the files.
161160	163280	The problem was with the other two devices.
164920	172160	The archive still contained files that had suggestive names like web.squashfs.
172360	176760	But when we used bin lock on them we only found U image headers.
177900	184040	The files were high entropy, so this means they are usually either compressed or encrypted.
184900	189360	There were also Zlib signatures when you used bin lock on them.
189480	198180	And these usually are false alarms, but when we extracted those we found strings, we found file names, even code.
198180	200530	It was JavaScript but still code.
201160	203760	So maybe not encrypted.
204640	210820	At first we thought it might be a custom compression scheme that you usually find on those devices.
211400	221900	But this was partially confirmed by the fact that the zip file signature which is PK was replaced by DH, presumably from Dahua.
222220	232180	Also we had U image headers on the file system files which is not at all common and especially on a web cam.
232760	238100	So the kernel wasn't readable either, so we had to go to the beginning, the boot loader.
239120	246020	It was in the same archive, it wasn't encrypted and it was a U boot variant so time to load it in Ghidra.
247120	251540	To do that you can use bin lock to see the layout of the file.
251640	257200	And in this case just ignore the U image header and go for the LZMA compressed data.
258300	262820	You then carve the file out with DD and extract it.
263020	272260	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.
273340	279500	Okay, so extraction looked good, we had strings, we had constants, so we had something valid.
281000	288140	Load the binary in Ghidra with the R64 architecture on Little India and this is specifically for XVRs of Dahua.
288580	293200	A base address of zero will work so time to have some fun.
295060	297940	Where do you start when you load the boot loader?
297940	306920	Well, you go for the constants, either strings or in this case because we suspected encryption we went for the AES substitution box.
307300	312580	So check its references and go up the call tree until something starts to make sense.
313100	320660	Here we found some strings like uboot encrypt file or firmware get key, so obviously we were on the right path.
322340	328180	Judging by those strings and the nested if structure of the function, the decoding was happening in four steps.
328180	335980	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.
336760	337520	Okay.
338100	340640	The first step is getting the firmware key.
341100	351060	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.
351480	360040	Those hard coded bytes appear to be the same on multiple devices but the first six bytes will be replaced with the processor name.
360220	364600	So this firmware key will be different for different device types.
365100	367340	Then we had three functions.
367480	372180	And you can deduce they are hashing functions by looking at the constants in them.
372600	374360	You need to find the algorithm still.
374620	380520	Well to do that you can get those constants, plug them into chat GPT and get the wrong answer.
381000	386060	And then with one option removed you will deduce it's shot to 56.
386760	394940	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.
396780	401680	So we have the 16 bytes buffer which is the firmware key and it's time for the key factors.
402280	407700	This step is different for the kernel and the file system but we'll focus on the file system.
409480	414200	So they just set an empty buffer and use it as a parameter to the next function.
414860	421280	The firmware key that we got from the first step will not be used here so we expect some other hard coded stuff.
421940	422900	Let's go deeper.
424080	436820	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.
436820	439640	But it gets better if you take it step by step.
440080	443940	First you need to find what those many function calls actually do.
444260	445840	And in this case it was pretty simple.
445840	447940	We had mem set and mem move.
448380	453120	And for basic functions like this you can pretty much deduce them from context.
453200	460100	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.
460300	465380	If you have a buffer and a size again you can guess it's a mem set .
465800	474220	You can pretty much guess and then check for those guesses and it makes this whole reversing process a whole lot quicker.
474860	479380	Okay, we have two functions left besides those mem copies and mem sets.
480380	484060	And the first one had bit wise operations.
484080	486840	Which and hard coded buffers.
487080	492560	You can look those buffers up and in this case we found a match with the AST tables.
493320	499220	Usually you might not find an exact match especially on AES because there are multiple implementations.
499220	503200	You have hardware accelerated or fast implementations.
503200	509840	But if some bytes match you can probably assume it's AES unless they modify some constants which happened before.
510020	511940	But I digress.
512080	517460	Okay, besides these T tables we had a loop that could go 10, 12 or 14 iterations.
517520	523180	So if you are familiar with the algorithm you will probably guess it's the AES key expansion part.
523640	531120	Okay, we have AES key expansion then the next function is probably some AES mode of operation.
531120	532680	But we need to find it.
533460	535900	This function had five parameters.
535900	540400	It had probably the key, size, input and output.
541000	548280	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.
548740	554520	Still, AES has multiple modes that work with an IV like CBC, CFB, OFB.
554560	557860	But you can again make assumptions.
558040	562440	You take the most common mode of operation which is CBC and then check for it.
563820	569180	Here we found something that looked like a high level AES CBC operation.
569180	577820	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.
578460	583780	We found something similar so we just said okay, it's AES 256 CBC.
584860	591700	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.
592400	599040	The IV was created in one wild loop and it consisted of six unique characters.
599740	607080	The key was created in two loops and this time the bytes were incremented from 20 to 30 and 23 to 33.
608280	614300	You still need the plain text and here we had two hard coded buffers, 32 ints and 32 bytes.
614300	619200	These are the buffers that will be different for the kernel decryption and file system decryption.
619500	627240	So those two buffers were added together, summed to 600 and divided again, cast to a byte and some other operations.
627320	629620	But we obtained the plain text.
630900	634740	Okay, 32 bytes plain text, we get a 32 byte cipher text.
634740	640980	The last 16 bytes of this cipher text will be concatenated with the firmware key that we got in the first step.
640980	650180	So at the end we have 32 bytes which from what we've already seen, it looks a lot like an AES key.
651860	657520	Okay, we have two steps left, the setting flag step and the U-boot decrypt file step.
657560	664000	And we take them together because it's easier to understand them and this setting flag step is not present on all devices.
666060	673360	So we have a function with either seven parameters or five parameters if the setting flag step is not present.
673360	676820	We'll go the XVR route which is with the seven parameters.
676820	678620	Two of those from flags.
678620	684000	Then we have the input address and its size and the output address and its size.
684140	686220	Those two addresses are the same.
686220	688820	So we will have in place decryption.
689120	691680	Then a flag for either encryption or decryption.
693440	694160	Okay.
694160	695860	So we have the function.
696060	696580	Yeah.
698360	703260	And because we already named some other functions, this time it will be much faster.
703260	714600	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.
714700	716820	So I wonder what could be in the else.
717580	718500	Okay.
718860	722660	We found a function for decryption but again we need parameters.
723100	728340	We have a size of 200 X so it means we decrypt 32 blocks at a time.
729280	732680	The IV will start at zero and it will be incremented.
732760	736240	They cast the first four bytes to an int and increment those.
737860	738280	Okay.
738280	742400	We also have the input pointer and the offset that is added to it.
742400	748560	So you decrypt 32 blocks, you increment the offset with 200 X and so on and so forth.
748900	755360	The problem was that at some point this offset will be incremented by more than 200 X.
755720	759640	So this means that some blocks will be skipped and not decrypted.
759720	763960	And because we have in place decryption, they will remain in the output unchanged.
764500	775940	So this basically solves the mystery of how we managed to find strings and file names and code inside the supposed encrypted block.
776640	779300	Those blocks were never encrypted, just partially.
780660	781420	Okay.
781680	792680	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.
794080	800420	If you remember earlier we have file system images with an image header which is 64 bytes in size.
800600	806700	This header has a size field and the value of this field matched the number of bytes following the header.
807100	815360	So again we carved those bytes out with DD, we plugged them into the decryption algorithm and great success.
815540	820260	We have the file system, we have the files, now what?
820700	821320	Okay.
821320	822800	Now we go ham.
823140	825440	First we map the attack surface.
825900	830700	The binary that does everything will be called either Sonya or challenge depending on the device.
830840	832860	This is where the listeners will be.
833200	844160	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.
844400	851460	If you actually look at the listeners in the binary, you see that they cram protocols on the same port.
851760	859020	So they use the reg X to parse the incoming packet and then select the correct handler for the respective protocol.
859960	870980	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.
871760	876120	The problem was that not all were enabled on all devices.
876280	879360	And most of them actually used authentication.
880340	888540	Still as always there are handlers that don't use authentication and one of them was of course found on HTTP.
889740	899080	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.
899300	907260	But there were a few handlers without authorization and one of them was called RPC2 upload file with name.
907260	912100	But despite the name, the problem wasn't that you could upload files.
912200	915740	Those files were just empty freeze that had a fixed path.
916200	918880	The problem was that they parsed the header incorrectly.
920640	926220	So usually you have the classical usage of the N string functions in C.
926540	933960	You copy a string into a buffer but then instead of using the size of the buffer you use the size of the string.
934320	941500	Here the CSEC header will overflow into the BSS section after only 34 hex bytes.
942320	944500	The binary crashes of course.
945560	950220	In BSS you mostly find global variables or static variables.
950540	957760	Here we have overwritten a pointer so I already thought I had to get out the maloc malificarum to exploit it.
958000	967760	But before we go deeper into the arcane magic that is heap exploitation, I wanted to check why the program actually crashed.
969320	977000	The pointer we overwritten turned out to hold some structure related to the sessions, the HTTP sessions.
977380	984540	First I thought I will recreate this structure in memory and give myself a session in order to increase the attack surface.
985280	987360	But the crashing function gave me hope.
987360	994380	So it was checking for the token expiration and if this token will be expired it will delete it.
994380	1001100	But this deletion happened with a function pointer that was stored in the same structure I wanted to overwrite.
1002380	1005160	So the plan should be simple now, right?
1005420	1011000	Rewrite the structure in memory and replace this cleanup function pointer with a system call maybe.
1011780	1016380	The problem was that I didn't have a debugger because I didn't have a shell.
1016860	1020080	Okay, I didn't have a debugger anyway but you will see why later.
1020820	1030760	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.
1031900	1037700	So first I tried static analysis but the results, not the best.
1037720	1048100	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.
1048380	1059280	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.
1059280	1064440	So I started rewriting the structure step-by-step one crash at a time.
1066100	1071160	Most of the crashes were just null pointer references or illegal memory accesses.
1071280	1082080	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.
1082700	1087420	If you do this 15 more times the program will still crash.
1087940	1092800	But this time it will be with the program counter not in the code section error.
1093100	1095360	And that is when you know you've made it.
1096120	1096860	Okay.
1096920	1098380	Where is the function pointer?
1098380	1099980	You replace it with system.
1100660	1102580	But we need a payload to run.
1103620	1110180	We didn't have reverse cell utilities on the camera like netcat but we had TFTP so we could download files.
1110800	1115660	The problem was the kernel didn't run unsigned binaries.
1115900	1118800	So we couldn't run just anything.
1119620	1122060	Pretty impressive protection for a webcam.
1122160	1127320	Still we could load libraries using the LD preload privilege escalation trick.
1128540	1135300	So we needed a shared library that was compiled for that specific architecture and thankfully we had a tool chain.
1135600	1138640	And if you remember earlier I said I didn't have a debugger.
1138640	1140580	But that's not for lack of trying.
1140880	1147780	Turns out it's much more difficult than it sounds to cross compile GDB server as a library that runs when it's loaded.
1148260	1149040	Still.
1149660	1157120	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.
1157420	1162020	So just be careful when you use build root to use a hard floating point.
1164220	1164880	Okay.
1164880	1171860	We compiled a bind shell, made a payload to download it with TFTP and we got root.
1171860	1173520	You can't see it but we had root.
1173520	1174460	Just trust me.
1177130	1177870	Okay.
1180980	1182520	We had one root.
1182860	1184160	Time for a second one.
1185520	1188460	The previous exploit was on the HTTP service.
1188980	1195260	And this one will be on ONVIF which is specific to IP cameras.
1195340	1199180	It's still HTTP but it uses some different parsers.
1199900	1205840	You have the classical ONVIF end points but everything interesting is usually behind authentication.
1206380	1212280	We had access to getter functions but as the name says they don't parse parameters.
1213420	1223460	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.
1223660	1225140	The available end points.
1225480	1227360	So we only control the host header.
1227360	1232260	This was the only thing that these end points parsed and guess what?
1232260	1234060	They did so incorrectly.
1234940	1240240	If this header didn't have a square bracket in it everything was good.
1240480	1244400	My header would be copied on the stack but the size was checked.
1244400	1245840	Only 64 bytes.
1246060	1257880	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.
1258120	1260880	So we had a classical stack based buffer overflow.
1261220	1263560	But we need to set some conditions for it.
1264000	1267280	We control seven registers from R4 to R11.
1267480	1271160	Then the function that causes the problem is string and copy.
1271160	1277440	So in our payload we only have access to one null byte and it must be the last null byte.
1278280	1279960	Why is that a problem?
1280260	1284180	The code of the camera is not position independent.
1284180	1287760	So on every run it will be loaded at the same address.
1288020	1291600	The problem is it's a low address so it contains a null byte in it.
1291800	1296300	This means our rope chain that we want to create can only contain one gadget.
1296820	1307340	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.
1308040	1310700	So we had a system gadget.
1310700	1315340	We could put an address to a payload in R6 which we control.
1315500	1318160	But still we don't have a payload.
1318420	1323820	You won't find some bind shell or reverse shell string on a camera.
1324640	1328440	So we need a way to store the payload and a place to store it.
1328700	1330380	The place should be easy.
1330500	1332460	We write into the BSS section.
1332860	1334120	It is writable.
1334280	1337340	No null bytes in its address because it sits higher up.
1337900	1339300	But how do we do it?
1339560	1349120	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.
1351560	1352060	Okay.
1352060	1353500	But where do we go?
1353940	1362220	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.
1362220	1365120	So we can make a rob gadget using them.
1365800	1367560	We didn't have a memory leak.
1367760	1371160	So we were calling one gadget.
1371160	1375180	Hopefully it would write four bytes, two bytes in memory.
1375180	1383320	But then it would crash because its epilogue would be different from the epilogue of the caller function.
1383320	1390760	So you have the caller function which calls the vulnerable function where you overwrite the return address and then go to the gadget.
1390760	1396800	The gadget does its stuff but then the stack pointer will be left in some random place.
1397320	1402320	So the program crashes, our memory is emptied, no more payload.
1403220	1412060	But if you understand this problem, you understand the solution because we need a gadget that has the same epilogue as the caller function.
1412220	1421080	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.
1421500	1431840	So the call execution flow will go back to the function above the caller function and our payload remains in memory.
1432740	1433400	Okay.
1433400	1436400	Such a gadget sounds hard to find.
1436540	1438640	But we had a 20 megabytes binary.
1438640	1441460	So we had plenty to choose from.
1441860	1448960	We used the instruction search window to find matching epilogues with the caller function.
1448960	1452360	And we found about 15, 60 functions.
1452520	1454360	So pretty good.
1455280	1463120	We chose one where a store instruction was as close as possible to the return instruction.
1463220	1466880	So we have the store instruction from R7 to R5.
1466880	1468480	We control both of those.
1468480	1472960	So this means we can write four bytes at a time in the BSS section.
1474460	1475300	Okay.
1476500	1478760	What payload do we use for system?
1478760	1482700	Well, the same payload we used for the other exploit.
1483840	1486620	With using LD preload we can load the library.
1487800	1489580	We have the payload here.
1489580	1491220	R4, R5, R7.
1491240	1492360	Controlled.
1492520	1494060	Then the gadget address.
1494680	1503040	And then after we write this payload in memory, after about 10 to 15 requests, we can jump to the system gadget.
1503300	1507800	And because we control the address of the payload, we put it in R6.
1508360	1510760	And again, root.
1511180	1512460	The second one.
1513140	1513960	Okay.
1514860	1516540	We have a timeline.
1516960	1519020	The team was great to work with.
1519020	1520840	They used PGP key.
1520880	1522480	They answer fast.
1522480	1524400	They actually fixed the problem.
1524760	1533580	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.
1533900	1535560	So, yeah, kudos to them.
1537860	1538660	Okay.
1538940	1539800	This was it.
1539800	1540860	Thank you for coming.
1540860	1542840	I hope you learned something.
1543320	1544740	Sorry for the screen.
1550700	1556200	And if you have questions, there might be questions, but go ahead.
1559000	1562760	If not, you can maybe find me at the IOT village sometimes.
1562760	1564840	But I won't guarantee it.
1564840	1566120	Thank you.
