start	end	text
0	4540	Hi everyone. There are more people than I expected at 10am.
4800	7280	Thank you very much for being here.
8480	13380	You are the ones who survived until the last day in DEFCON. Awesome.
14660	22800	Welcome to taking off the blindfold, detecting persistent threats on drytech edge devices.
23600	29460	We are part of the Faraday research team. My partner here is Octavio Geno-Tiempo.
29460	36080	I am Gaston Asnares. And we have some experience working with firmware.
36080	42300	We have a foundation in computer science and we like to play CDF.
42560	46860	As a resume, we like to break things and do research.
48880	54660	So, what are we going to see today? First, we are going to see a motivation for this research
54660	60060	and this talk. We are going to see some reverse engineering on drytech firmwares.
60640	64940	We are going to talk about vulnerabilities and attack vectors.
65560	71260	We are going to see mitigation strategies and at least we are going to see some conclusions
71260	82100	and takeaways. Okay, how this started? We had a call of a client saying that it was compromised.
82100	91220	And when we came to the scene, we saw a lot of outdated drytech equipment on the edge of the
91220	100980	network. Most of them were SOHO models that run RTOS. And we started making some analysis.
100980	109720	We found previous vulnerabilities with proven exploits on high-end models of the drytech
109720	117800	routers that runs Linux. And we started making some questions about it. Like, are these
117800	126020	vulnerabilities exploitable on the SOHO routers? There are other vulnerabilities that are known.
126020	130940	And the principal question was, were these routers the entry point of the attackers?
132620	140260	Okay, we know that there is an increasing threat of advanced attack targeting edge devices. We
140260	147440	could name some examples, like the SOHO RAT, that was a socketical malware targeting SOHO routers.
147620	153660	We also could name the HIATUS RAT, that was a malware targeting drytech routers, but
154050	164100	the high-end models. And at least we could name Benin Sartain, that was an NSA tool
164700	171060	targeting Cisco routers, that was leaked by the Shadow Brokers Group.
173030	183530	Okay, so what is the attack surface of these routers? We found over 5,100 exposed devices.
184540	190440	Mostly in the UK. And we found a lot of vulnerabilities. From an information
190440	196780	disclosure to an authenticated remote code execution. So, there is a lot of stuff.
198020	207500	Okay, so we had a Vigor 2925 in our office running MIPS 32 RTOS.
208620	214640	And we wanted to start making some research on that, but we had a problem.
214980	222120	The code is closed source. So, first we had to reverse engineer the firmware format.
222360	228620	We had to extract the firmwares, understand previous vulnerabilities, and then look for
228620	239250	new ones. And finally, determine if this was the attack vector or not. So, the first thing we did
239250	244810	was trying to extract this firmware using MinWalk, but it failed. It produced no results.
244830	249090	So, we tried to look for some specialized tools, and we found this one, DRYTOOLS.
249150	254410	So, this didn't work with new firmwares. The last commit was 12 years ago, and that's why.
255050	262370	So, luckily for us, we found a talk by Philippe Loret at Hexagon 2022, where he described that
262370	268390	by emulating DRYTEXTBOOKLOADER, we can extract, we can make it extract the RTOS kernel.
268390	272830	The disadvantage of this approach is that this is a manual process, and you have to
272830	279170	tweak the addresses for each firmware version. And the other problem that it has is that these
279170	285350	firmware images have other executable components, apart from the RTOS kernel, and file systems
285350	292050	that you cannot extract this way. So, our approach was to reverse engineer the format,
292050	297270	and to try to write a tool that can extract all firmware versions from different models,
297270	301680	extracting all file systems, and extracting all the executable components.
303150	307170	We named our tool DRYTEXT Arsenal. It's a collection of tools.
307170	313270	We wrote this in Python. It's open source. You can check it out at that repository.
313750	318250	And the idea is to support for all these new firmware versions.
318650	323710	And we will be showing, along these presentations, all the features that we implemented.
325590	331650	So, let's talk about the firmware format. It has two main sections. One is called the
331650	338350	binary section, and the other one the web section. Both start with a header that contains the size
338350	343330	and the pointer to the following section. But in the first one, we also have information about
343330	351610	the firmware version, and some metadata. So, the web section has a file system that
351610	356970	is compressed and contains all the static files that the router serves for the web UI.
357690	364530	In the first section, the binary section, we have the bootloader that is raw MIPS32 code.
364530	371930	This is the part that Philippe emulated. And then this is followed by the compressed RTOS kernel.
372250	376930	And after that, we have a very interesting part of the firmware, that is the DLM code.
376930	383430	This is also a file system, but it's compressed and encrypted. And we will talk a lot about it.
384550	391690	So, to work with this, we made a specification of the structure of the firmware using KTI Structs.
391690	396030	And the advantage of using this is that you can manipulate the firmware images with any
396510	400750	language that you want from the list. We used Python, but you can find this in the repo and
400750	408290	use the language that you prefer. And, well, by knowing all this, we started to tackle the
408290	415350	reverse engineering of the compression algorithm. So, during boot, the RTOS is decompressed to the
415350	421590	same memory space as the bootloader. And we did this following the steps described by Philippe
421590	429030	in his talk. But when we started reverse engineering this, we mapped all this in memory,
429030	435670	and then the RTOS after that. And that allowed us to discover that the decompression function
435670	440350	is used during runtime to decompress other things, for example, the file systems.
440830	446130	And we also discovered some interesting strings, like the one that you can see down to the right,
446130	452970	that says LC4 decompression failed. But if it was LC4, the compression algorithm, why we couldn't
453390	463530	work. And the reason for this is that while they use a standard LC4 compression block format,
463530	473730	they have written a custom frame format. So, it's just a custom magic that is followed by a
473730	482950	succession of block size and block data multiple times. So, we wrote a decompressor and a
482950	488010	we moved on to the most interesting part, that is the DLM section.
488570	495730	So, it turns out that the DLM stands for Dynamic Kernel Modules. So, this real-time operating
495730	505090	system has the ability to load code modules during runtime dynamically. And this feature
505090	512050	works independently from the firmware. So, during boot, we can see that some
512950	518870	the modules that come with the firmware image are loaded. There are three modules,
518870	527370	and they are called VR9 GeoIP, VR9 App-E, we will talk a lot about this one, and VR9 SSH.
527370	533670	So, the first one contains information about the geolocation of IPs. The second one is used
533670	540730	to block traffic from the users that are in the LAN. And the last one contains the implementation
540730	546670	of SSH server. And this is really interesting because this implementation is separate from
546670	557350	the firmware. So, these modules can be updated during runtime, and if you install a new version
557350	565250	of App Enforcement Module or SSH, these are loaded first and prevent the modules that come
565250	571710	with the firmware to be loaded. You can see down to the right that, for example, in this case,
571710	579790	the VR9 App-E module was loaded first, and then when the router tries to load the version that
579790	587130	comes in the firmware image, it will fail. So, doing some more reverse engineering,
587130	593370	we found out that the modules are compressed using the same algorithm that is used for the RTOS,
594250	600670	but they are also encrypted. So, we recognized that the algorithm used was a modified version
600670	606610	of XTAA, and this is a symmetric encryption algorithm. And what this means is that the
606610	612990	keys for decrypting these DLMs are present in the firmware. So, we looked for them, and we wrote a
612990	618510	decryptor and encryptor for the modules. So, if you take a DLM, you decrypt it using this, and
618510	624750	decompress it using the modified version of LC4, then what you get is a relocatable ELF.
625010	629630	And this is very interesting because all the external functions that this ELF uses,
630390	634850	they have to have their symbol names. So, they are really easy to reverse engineer.
637030	641230	And this also means that the firmware must have an ELF loader.
641230	646090	So, we looked for that code, and we found that there is also a symbol table.
646090	651490	This symbol table was described before by Philippe Loret, but only as an aid for reverse
651490	657210	engineering, because imagine that you have a table where you have function names followed by
657210	664710	function pointers. But now we know that this table is actually used by the ELF loader to link these
664710	672850	modules to the kernel. So, the advantage that this gives to us is that if we manage to write
672850	677430	our own modules, we can use all these functions without knowing their actual addresses.
679450	688590	So, we implemented all these in our tool. We have two functions, one called parse firmware
688590	693090	that will print a lot of information about the firmware image, and the other one called extract
693090	699790	that will extract all these pieces of the image. It will decrypt and decompress all the modules.
699790	707730	It will extract all the file systems for the web server, and it will generate a binary image
707730	711890	that contains the bootloader, followed by the RTOS kernel, but decompressed.
713410	718910	So, if you want to start reverse engineering this, you must know the loading address.
719210	725390	What we realized is that all dry type bootloaders start by calling the decompression function.
725450	728790	So, this function is always located near the start of the image,
728790	736710	and since all this has to be loaded aligned to the page, then we can search for the address of
736710	743130	this compression function, and if we remove the last three numbers, what we get is the loading
743130	747890	address of the whole image. So, with that, you can start your reverse engineering process.
748990	754770	We also implemented this in a functionality called find loading address,
754770	761170	and once we had all that, we moved on to try to find some vulnerabilities and attack vectors.
768700	775060	Okay, so let's talk something about vulnerabilities. We are going to show some of them.
776120	782480	Starting with the basics, we found that they had an insecure password storage. That means
782480	786520	they stored the passwords and the credentials in plain text in memory.
787070	793660	So, if someone had access to the memory, they could read the credentials or maybe
793660	801360	chain another exploit to leak them. We also found a non-constant time comparison
801910	811680	on the credential. So, they use a function that takes different amounts of time to compare
811680	818880	different passwords, depending on how much correct characters are in the string.
819380	823660	And that leads to a side attack channel, a timing attack.
825640	833500	Okay, so, DirectX implements a second-step authentication. That's really nice, but that
833500	840020	was predictable, because the code generation was done using a self-random function,
840540	850680	but with the seed, they use the time elapsed science boot. So, if you know for how long the
850680	857760	router is running, you could predict the second-factor authentication calls.
858540	865480	We also found an older reference in the HCP server that leads to a
866200	872500	denial-of-service attack, but the interesting thing about that is that it was exposed to one.
872500	878940	So, if the router was exposed to the internet, you could trigger that another reference.
880080	887320	And if we combine both vulnerabilities, we could predict the second-step authentication calls,
887320	895160	because we could make the router crash, and then we know the time elapsed science boot,
895160	902160	and we predict the calls. And that's what we are going to see in the next demo.
902840	906520	We could see on the right, the serial communication with the router.
907800	914320	Um, on the... no, that's on the left. On the right top is the script that we run.
914540	923380	It triggers the known reference, and the router is rebooting. It will take
923940	927960	like two or three reboots, because it crashed hard.
929440	937200	And on the right bottom, we have the web UI, where we are going to try to log in.
940460	947780	So, now it boots. We are going to wait until the router initializes itself.
949180	953380	Okay, and now we are trying some different calls.
959970	964750	And we were able to predict the call, and we are logged in.
968390	976790	Okay, so Octavio talked something about the APP Enforcement Signature feature. It's really great.
976790	985110	If you have an enterprise and you want to block your employees' web, or I don't know,
985110	990310	you could update the signatures in order to block that traffic.
991170	998130	And how this process is made, there are two ways. The first one is a manual. We will see
998130	1005130	on the right bottom that says import. If we click there, we could upload any file
1005910	1015110	with some checks. And the second way is the automatic way, in which we have to select
1015110	1026670	the update server or select the default one, and we have to schedule the updates. So, for example,
1026670	1033710	every day at some point of the day, it will check if there is some update for the signatures.
1036170	1045430	And how they implement these signatures, you could think in JSON formats or YAML or something
1045430	1054110	like that. But Drytech has the idea to implement this as a module. So, to update the signatures,
1054110	1063760	they upload an L file with code and everything. Okay, let's talk about some vulnerabilities
1063760	1074040	related to the modules. The first one is they not also update certificates with code, but they
1074040	1081500	don't check for certificates or have SSL enforcement. So, if someone manipulates the
1081500	1088980	configuration of the router or the DNS, maybe DNS hijacking, they could upload a module to
1088980	1098740	all the Drytech routers that has this feature. And we have found two post-authentication
1098740	1104620	endpoints. The first one is the one that I told in the last slide. And the second one is
1104620	1112840	that we found an endpoint to upload configurations files that doesn't have some checks, so they
1112840	1121240	allow us to upload an arbitrary DLM. Now it's not only the APPA module, but we could upload any
1121240	1131440	modules that we want. Maybe we could, I don't know, upload another SSH module. So, we have three
1131440	1136060	ways for an attacker to install malicious modules. The first one is the post-authentication
1136060	1144760	endpoints that we talked just in the last slide. We have the module upload process that is hijacking
1144760	1151500	the configuration or the DNS. And the last one is the supply chain. Because the modules are very
1151500	1159840	easy to craft, so if they are easily appended to to the firmware, and anyone with physical access
1159840	1168880	or, I don't know, exploiting some vulnerability, they could upload the module before selling it.
1171910	1177750	Okay, so knowing all of this, we could start making some modules. We implemented two commands.
1177750	1187470	The first one is the MIPS compile that is going to make a compilation of source code into a
1188190	1198290	ELF of the architecture of MIPS32. And we also have another command that is ELF to DLM. As the
1198290	1204190	name says, it will convert an ELF to a DLM, compressing and encrypting with the provided
1204190	1213190	keys. So, we could append some web headers so we could upload them through the web UI.
1214930	1224470	So, we are going to see a demo. We could see on the left the version of the APPA module.
1225250	1231710	On the right, in the middle, we are going to run a server, a custom server,
1231710	1234850	that is pointing the configuration of the auto-updates.
1242430	1249450	So, on the top right, we could see the serial interface with the router.
1250750	1257890	Okay, so once the server is up, we are going to trigger a new DLM.
1259350	1265390	So, it's triggered. We could see the router has fetched the new module in our server.
1265390	1270930	We could see the Faraday logo because it's our module. And if we log in again,
1271710	1276610	we could see that the version of the module has changed and we have some logs that said
1277390	1287750	that it doesn't allow this shit. Okay, let's make something more funnier.
1288570	1295330	We are going to make a SSH backdoor. Why not? Because we have the module.
1295330	1303030	So, the original behavior of the SSH module is the top one. When we have a new connection,
1303310	1309070	it's going to call the authentication function and the authentication function will say that
1309070	1317530	it fails or it authenticates. But what we are going to do is when a new connection enters,
1318310	1324810	when a new connection is open, that is going to call our hook and it's going to check our
1324810	1331410	credentials. So, if they are right, it authenticates. If not, it's going to call
1331410	1336830	the original function because we don't want to change all the behavior of the SSH module.
1339230	1344350	How is the roadmap for this? First, we have to do a reverse engineer of the SSH module
1344350	1348790	to find that function that makes the authentication. We are going to skip that
1348790	1353110	part and we are going to say that the target function is the SSH account and LDAP auth
1353110	1363150	function. After that, we have to write and compile the hook. We have to merge the hook
1363150	1370590	with the original module and we have to modify all the relocations in the entry table.
1371590	1374850	And last, we have to upload the DLM.
1376750	1384690	On the right, we can see the source code of our hook function that takes the same arguments as
1384690	1393870	the target function, an SSH instance, and a packet. And it will check for our credentials.
1394170	1400970	If they are right, it will return one that says it authenticates. If not, it will call the target
1400970	1409970	function. In order to compile this, we use our command MIPS compile.
1412510	1418410	And then, we have to merge both binaries, both relocatable ELFs.
1418950	1426350	So, there is a relocatable ELF. It has all the symbols with no fixed address.
1426350	1436270	So, they had a relocation table that will say everything that uses a symbol has to be modified
1437810	1446710	to point to that symbol after linking. So, we have all the symbols there. And once we merge it,
1446710	1459190	we have the symbols of both modules. We wrote a command to do this with the name of MIPS merge
1459190	1466290	that takes two ELF files and as an output has one file.
1468600	1476880	Okay. Now, we have to modify the relocation table. So, first, we find all the relocation that
1477840	1484820	talks about the target function. We can see on the top right the two examples. We are going to
1484820	1495840	use the first one. If we print the code that is on that address on the bottom left, we can see
1495840	1504220	that the jump is a jump instruction, but the jump instruction only has his opcode but doesn't have
1504220	1510480	an address where to jump. That is because the relocation entry will set which one is going to
1510480	1516780	be that address on link in time. And we can see there that is the target function. So, we modify
1516780	1531080	that entry to give us a result the right that points to the hack function. So, time to hack.
1531080	1539160	Yeah. Okay. So, on the top right, we have we are trying to log in with our credentials,
1539160	1546180	but we could see that it fails. So, on the bottom left, we upload our backdoor module.
1549100	1553900	And once it's uploaded, we could see that the file was written successfully,
1554460	1564210	but we have to reboot the router. And once it reboots, we are going to try again
1564210	1579440	with our credentials. We could see that the module was loaded. Okay. So, we tried to log in,
1579440	1586560	and we could see that we were in. And we could see some loss in the serial communication.
1593980	1600280	So, this attack that Gaston showed is really dangerous. Imagine that an attacker can craft
1600680	1609240	a modified SSH module, upload it to your router, and you have no way of detecting this. Moreover,
1609240	1615660	if you update the firmware, the module will be still there. It will persist. And even will persist
1615660	1619780	if you reset the configuration of the router, because it's stored in a separate file system
1619780	1626920	in the flash. So, we started to think about some mitigation strategies. The first one,
1626920	1632260	the obvious that we thought about was just to remove the feature, because who wants to have
1632260	1639820	dynamic modules on their router? But there might be some users that use this feature. So,
1639820	1646060	well, instead of nubbing all the calls to module loading during boot and during the
1646440	1655640	update process, we decided to think for a better solution. And we devised a mechanism
1656080	1662420	for checking in-memory integrity of the other modules that are loaded. This mechanism should
1662420	1666820	fetch signatures from a server, because the modules will get updates, so you have to update
1666820	1674860	the signatures. But this time, you have to check the certificates. So, once you got those
1674860	1681260	signatures, you have to find the loaded modules in memory, calculate the actual signatures,
1681260	1687340	and compare them. And if there is a mismatch, then you have to notify the user somehow,
1687340	1690960	and all this functionality should be included with the firmware.
1692060	1700280	So, what we did, as a proof of concept, was to use the open source module to introduce this code
1700280	1705060	to the router, because it's much easier to write a module than to patch the firmware,
1705060	1710240	because you have all these functions from the symbol table that you can use.
1710800	1718200	So, there are two things that we have to solve. The first one is how to calculate a
1718200	1725780	signature for a valid DLM, and the second one is to find the actual DLMs in memory and do the
1725780	1733380	comparison. So, we focused on that, and we accepted some limitations. We will not check
1733380	1738780	the signatures every time a module is loaded, as it should be. We will only check signatures
1738780	1744960	when our defensive module is loaded, and we will not try to notify users for now.
1746420	1752340	So, the first problem that we have to solve is how to calculate a hash or a signature for a DLM.
1752340	1759680	We will hash each signature with MD5, because MD5 was available on this table,
1759680	1764900	on this symbol table, and we will hash only the static sections, because the dynamic sections
1764900	1771020	will change once loaded. If the static section contains data, we will hash it right away,
1771020	1777140	but if it contains code, then, as Gaston explained during the module installation,
1777140	1783860	when it's linked, all the jams and the function calls will get the actual addresses of the
1783860	1789760	functions written to them. So, we will hash only the opcodes for these sections, and the opcodes
1789760	1799060	are the first six bits. So, once we got that, we have to find all the modules that are loaded.
1799380	1806380	When the ELF loader installs a module, it creates an object to represent the module,
1806380	1811280	and the space for this module is allocated using the linear allocator of the router.
1811620	1818100	So, this allocator maintains a list of all the chunks that it has assigned,
1818100	1825080	and if we manage to find this object that represents the DLM, we'll get the DLM name,
1825280	1830780	a pointer to the text section of that module, and a pointer to the main section that will get
1830780	1839760	executed after the module is loaded. So, the loader copies all the sections in a predefined
1839760	1845580	order, one after the other, after the text section. This order is usually defined by the compiler,
1845580	1853380	and we will assume that this order doesn't change. So, on the bottom, you can see a little
1853380	1859800	bit of the code of this defensive module, the part where we traverse the list of chunks from
1859800	1867200	this allocator, and we will check if each chunk corresponds to the module object. If the chunk
1867200	1872020	corresponds to the module object, we will print some information, and if this module happens to
1872020	1879580	be the SSH one, then we will calculate the hashes and compare them. So, here's the demonstration.
1879580	1886320	This router is the one that has been backdoored and has a modified version of the SSH. Then we
1886320	1893960	upload our defensive module using the UI on the left, and you can see right there that once it's
1893960	1899540	uploaded, it will try to find all the modules, printing their information, and when it reaches
1899540	1905280	the SSH module, it will start comparing the hashes for each section, and it will find that
1905280	1909600	there is a mismatch between the hashes on the second section.
1911920	1919240	So, to wrap this up, some conclusions and takeaways. All these vulnerabilities were found
1919240	1925760	by us analyzing only the bigger 2925 router, but they are present in several models because
1925760	1932000	all the SOHO routers use this real-time operating system, and even some of the
1932980	1940220	higher-end routers that run Linux emulate the same real-time operating system. So, we found
1940220	1944860	all these vulnerabilities and some more that we didn't show, and they are all patched by now,
1944860	1954180	but we don't have CEIDs yet. And, well, some conclusions on the offensive side. The code for
1954180	1960460	this real-time operating system is huge, it's really complex, and we only analyzed a little
1960460	1966840	bit of it. Imagine that the functions that are present in the symbol table are maybe not up to
1966840	1972040	five percent of all the functions in this firmware, so there are probably more vulnerabilities here
1972040	1978680	to be found. And that is why we are open-sourcing our tool, and we would love for you to try it,
1978680	1984020	and we would love to know if it's useful and if you find something. So, there's the repository
1984020	1990680	if you want to check it out. And now, some conclusions on the defensive side. We show
1990680	1995620	that it's possible to create a defensive module to check the integrity of other modules in memory,
1995620	2000760	but in an ideal situation, this feature should be implemented by the vendor. It should fetch
2000760	2007500	these updated signatures from a trusted server, verifying the certificates, and for all this to
2007500	2014260	work, if this feature is included in the firmware, also we must have a secure blockchain to trust
2014260	2022380	that this is working right. And to close, some takeaways. We think that closed-source firmware,
2022380	2028100	as this example, is just another case of security by security. All these vulnerabilities that we
2028100	2036360	have found are really simple, and they would have been reported sooner by the community if it were
2036360	2043540	easier to reverse-engineer these routers. So, some things such as plaintext password storage,
2043540	2049940	the use of insecure functions to compare sensitive data, predictable random number generation,
2049940	2057320	and even the lack of SSL certificate validation. So, given all the increase in attacks that we are
2057320	2062620	seeing to edge devices, it would be really important for vendors to include features
2062620	2067220	that facilitate observability, vulnerability research by the community, and to implement
2067220	2074000	threat detection solutions. So, this is it. Thank you for your time, first for coming so early,
2074000	2080380	and if you have any questions, please ask us. There's a mic here, if you want to ask.
2082600	2084940	The first question gets shared.
2089300	2090860	Well, thank you, guys.
2111810	2115710	Okay, the question was if there is memory protection between the modules and the
2115710	2120170	RTOS, and there's not. All these runs on the same memory space, so from a module,
2120170	2136840	you could modify the RTOS code and vice versa. Yeah. Well, thank you again for coming.
