start	end	text
0	3180	Ryan Edmonds is here to chat with you and he's going to tell you about what.
3180	5300	So let's welcome Ryan to the stage.
5580	7060	Thank you so much.
7060	8080	Thanks, Bruce.
9760	10720	All right.
10720	11540	Hi, everyone.
11540	17640	Welcome to my talk titled Disguised Delimit, Exploiting Synology NAS with Delimiters and Novel Tricks.
17640	18800	Let's get started.
19600	21000	Picture this.
21120	23900	You're at home one day and the doorbell rings.
24520	27020	You open the door and are handed a package.
27180	28960	It's addressed to you.
28960	35320	After cracking it open and rifling through the packing peanuts, you're greeted with a small black box.
35320	37020	What is it, you might ask?
37020	43280	A Synology B Station, a consumer version of the Synology NAS line, network attached storage.
43360	48280	And it's going to be a challenging adversary over the coming weeks of research.
48580	55520	That's the position that I found myself in last year when I wanted to participate in the Pwn2Own exploitation competition.
55560	61600	This talk will discuss what I learned while performing zero day research on Synology network attached storage.
61740	67320	And the end result of that research was a $40,000 prize at the Pwn2Own Ireland competition.
68020	69740	Let's talk overview.
69860	71340	First off, the introduction.
71340	72700	And we're in it right now.
72700	78640	Second, we're going to have a brief topic covering Synology NAS internals and attack surface.
78640	79900	Very high level.
80140	85620	Number three, we'll talk through discovering an unauthenticated vulnerability via case study.
85620	87960	This is the research I performed last year.
88000	96700	Four, we'll have a novel exploitation technique for Linux that I stumbled upon while developing the exploit targeting Synology last year.
96700	101240	And fifth, we'll have an exploit presentation so you can see everything come together in video.
101440	102680	Let's get started.
103320	104360	Who am I?
104360	105860	My name is Ryan Emmons.
105860	109160	I am a staff security researcher at Rapid7.
109160	118280	I have approximately four years of zero day research experience and my day to day work is zero day discovery and in day analysis at Rapid7.
118280	124900	I've reported bugs to notable organizations like Oracle, Microsoft and SonicWall in the past.
125660	128780	For this case study research, my project goals were clear.
128780	131960	I wanted to write an unauthenticated exploit.
132100	141200	It should target Synology DSM or disk station manager, the proprietary Linux based operating system that most or all Synology NAS devices run.
141740	147860	I wanted it to work in the default config, no extra changes necessary to make the device vulnerable.
148220	151720	I didn't want to employ any adversary in the middle threat models.
151720	155280	I just wanted to point and shoot remotely.
156060	162420	I wanted the exploit to be highly reliable as opposed to leveraging bugs that were less reliable.
162740	164940	And this was a tough one.
164940	170420	The timeline that I had to research was three and a half weeks due to the conference registration.
170420	172780	So I knew that I had to move quickly.
173760	176380	Let's talk about Synology DSM quickly.
176500	182120	So Synology DSM is the proprietary Linux based operating system used by Synology NAS.
182120	188040	It's used for file management, multimedia, collaboration, productivity, virtualization and surveillance.
188080	196640	It acts as the brain for any number of these workloads and you can see a screen shot there on the right of the web service, the desktop for Synology NAS.
197820	212200	When talking about the device footprint, a naive Shodan query for Synology DSM which encompasses the B station line and the disk station lines returns approximately one million public facing devices.
212260	216040	So these are devices likely to be vulnerable exposed to the Internet.
216260	219340	I knew that there was significant potential impact.
219340	221120	Tons of initial access.
221120	229820	If an exploit works on these devices, then an attacker could move laterally into the network of all of these organizations that had the device public.
231300	232160	All right.
232160	234360	That brings us to a high level.
234400	239300	We're going to talk through Synology DSM internals and attack surface on a high level.
239300	241240	So let's get started with that.
241800	249760	When we're talking about research targets, all Synology NAS runs some variation of disk station manager OS or DSM OS.
250000	259120	The most affordable new hardware options for security researchers are most likely the DS124 and the B station, both running a couple of hundred dollars more or less.
259680	266900	There's also a community project that's very helpful called virtual dash DSM to run DSM in a docker container.
266900	272800	So I recommend that if an individual wants to spin up a quick DSM instance, test exploits, pull binaries or whatnot.
273620	277760	Different Synology NAS devices have different default configurations.
277760	289200	For example, some disk stations automatically have all web services HTTPS whereas others have HTTP and the B station has a different configuration than most disk stations.
289420	293320	Different Synology NAS products also run different architectures.
293320	302020	So the B station is ARM whereas the disk station is x8664 and older disk stations are x86.
304040	312340	Synology offers a package manager, sort of an app store, to install add on packages for the operating system.
312340	322260	And despite these packages qualifying for the pony to own competition, I wanted to go for maximum possible impact so I wanted to stick to default packages only.
323700	328740	Synology B station does not expose SSH or other advanced services by default.
328740	331140	It's a consumer focused device.
331380	343860	In order to enable SSH, you have to go in and toggle Synology technology support remote access which is designed for Synology employees but it can be used to gain access to the root account on the operating system.
344400	349840	Local access can be enabled to configure local credentials and enable SNB, a useful target.
349840	355000	And the disk station line of products has an admin panel with an SSH toggle.
355000	357900	So it's a little more straightforward for security researchers.
359400	363700	Covering a brief history of exploitation at the time that I went into my project.
363700	373620	Pain in the NAS, exploiting cloud connectivity to pwn your NAS by clarity team 82 from 2023 and your NAS is not your NAS by dev core from 2022.
373620	396040	Both great pieces of research that worked in different ways and both were primarily applicable to an attacker in an adjacent network position, so already on the LAN most likely since clarity team 82 targeted an information leak in a discovery protocol on the LAN and the dev core exploit was binary exploitation targeting the Net-A-Talk service which is less likely to be exposed publicly.
397460	412080	Researcher Chen Chen presented an excellent overview of the JSON RPC like Synology web API at power of community in 2019 and this is a great primer for the web application although I didn't end up leaning too much into it during my own research.
412560	420000	When talking about remote attack surface the DSM web service is by far the most widely exposed attack surface.
420000	433600	It's enabled by default and virtually after administrator web authentication virtually all NAS functionality is exposed just via the web service so it's powerful if one is able to establish an authentication bypass or something of that nature.
434180	439540	So the question is what's reachable without authentication for the Synology DSM web service?
440680	448080	And that brings us to our case study, discovering an unauthenticated vulnerability in Synology NAS and we'll dive in now.
448920	458030	The DSM web service listens on ports 5000, 5001, 6600 and 6601 by default depending on the disk station that you got.
458480	461660	A lot of researchers have tested this attack surface.
461680	468600	In fact it was scoped for a few years prior to me picking up the project so I knew it would be reasonably hardened.
469140	472400	But the hope was that maybe we could do it in a new way.
474580	479400	Let's talk about the Synology DSM web service authentication workflow.
479720	481800	Here's the login portal that you see right here.
481800	485160	So step one for the B station is to input a user name.
485380	488280	We'll do that with the string user name in all caps.
488880	492460	Next we have to input a password and we'll just enter password.
492460	494620	It's not a valid set of credentials.
495220	500740	We'll click the blue button and the loading graphic will spin for about five seconds.
500980	507720	Now during that five second period some calculations are being performed under the hood to determine whether the user should be logged in or not.
508720	514120	And because we shouldn't be logged in, we have invalid credentials, a generic failure message appears.
514120	515820	The account or password is invalid.
515820	517060	Please try again.
517340	523100	Now I was curious, during that five second period, what exactly is going on under the hood of the B station?
523100	525840	What's it, what kind of calculations is it doing?
526400	529700	So I wanted to dive in further and monitor at the OS level.
530400	536660	In order to do that, I used the eBPF tool BPF trace with a simple script.
536700	546060	So any time the operating system processed a sys call, it would print the command line string as well as the entire list of environment variables set for that process.
546500	548540	It was very useful introspection.
549120	551460	So we'll kick off a BPF trace program.
552480	557040	And when we log in again, we see three executions take place.
557200	561400	And number one, number two and number three each do a unique thing.
561400	562960	So let's take a look now.
563880	571760	Number one, the first execution, we see command is null, which is unexpected at first until we dig a little bit deeper.
571900	576560	And we see that the program is a CGI binary execution.
576560	578120	So it makes a little more sense.
578120	590300	The environment variables are set, such as script underscore file name with entry.CGI and a number of additional common CGI environment variables such as remote method equals post, remote address, so on and so forth.
591700	593620	Now, that's that.
593620	595100	And it's not too exciting.
595100	597940	But what happens next is a little bit more interesting.
597940	600520	So let's take a look at execution number two.
602040	604920	This time we see the command string set.
605280	608820	It's seno CGI dash plugin dash web log in.
608820	611440	And it has the argument dash dash pre.
611440	612660	Note that.
613320	617920	We also see some additional environment variables set that are different from the previous binary.
618060	619460	User equals blank.
619460	621140	Type equals password.
621140	622660	Is known device equals no.
622660	632280	And then at the bottom, we see seno underscore plugin underscore args, a custom environment variable with a temp directory and a randomized file path.
632280	633840	So that immediately starts our fancy.
633840	635320	Seems very interesting.
636720	639360	And we'll explore that a bit more here in a moment.
639360	644500	The third execution when a user attempts to log in as seen through the BPF trace monitor.
644600	648060	We see the same binary is executed.
648060	651240	This time with the post argument.
651520	653000	So before it was pre.
653000	654540	And now it's post.
654800	658260	We also see that the environment has been populated to a degree.
658260	660320	User equals user name in all caps.
660320	661440	That's our tainted data.
661440	663740	That's our string that we provided as our user name.
663740	665660	We also see some additional information.
665660	667620	The same information we saw previously.
667960	670060	And we see status equals fail.
670060	671880	Result equals negative two.
671900	676600	So by this point in the process, we know that the authentication attempt has failed.
677560	686720	Once more, we see this interesting environment variable seno underscore plugin underscore args with a different file path in the same temp directory.
687380	691580	So we really at this point want to know what's inside of that directory, what's inside of those files.
691580	693500	And when we go to look, they're not there.
693500	696120	These are very ephemeral files that have already been deleted.
696480	702600	So in order to see what those files contain, we're going to have to follow in the footsteps of these two gentlemen and do some investigative work.
704660	708640	To do so, we're going to use inotify, the inotify API for Linux.
709040	710900	More specifically, inotify wait.
710900	716820	We'll monitor that temp directory and we'll look for creation, modification and deletion events in that directory.
717140	721520	Once we start that monitor, then we'll kick off another authentication attempt.
722880	725520	Inotify wait picks up a number of different events.
725520	730160	The creation and modification and deletion of a couple of files.
730480	736060	We want to know what's in these files and unfortunately, inotify is not quick enough to trigger catting the file.
736060	738940	So we'll do it in a hacky way.
740520	742160	We'll race with bash.
742380	745960	You don't have to know what this bash snippet is doing nor pay that much attention.
745980	752680	Essentially, it's constantly trying to grab those files and when they're written for a snap second, it prints them to the console.
752800	754520	File one and file two.
755300	760240	We're going to start this bash snippet and then we're going to attempt to log in again and see what it prints.
762120	764060	So, first file.
764140	766960	User equals type equals password.
766960	769240	Is known device equals no.
769240	770920	Pretty standard stuff.
771440	774400	The second file, user equals user name.
774400	775820	That's our data again.
775820	777000	Type equals password.
777000	778280	Session equals web UI.
778280	779360	So on and so forth.
779360	781600	And we see status equals fail.
781600	783320	Result equals negative two.
783660	789500	So these seem to be similar to the environment variables we saw previously for the second and process executions.
789980	800480	We also note once again that this is tainted data being written to a file before we've even authenticated which is rather exciting as an attacker even if it's not much of a capability at this point.
800480	805120	So user equals user name is our string and the user agent is also provided by the attacker.
807440	811380	Based on what we've discussed so far, the workflow is something like this.
811380	814740	The CGI binary which is native code executes.
814780	820180	Then the SenoCGI plugin dash web log in binary executes twice.
820180	824340	First with the pre argument and second with the post argument.
824340	827140	Making a total of three process executions.
827500	839320	And our hypothesis is that file number one is used to pass data between the CGI binary and the first pre execution and file two is used for the post execution.
839320	841780	And our data is written into those files.
843000	847140	We'll confirm the theory by going to var log and checking the log file.
847140	859940	We'll grep for user name in var log Seno plugin dot log and indeed we see that for plugin web log in, user equals user name is listed as one of the ARGs for the process.
860040	868540	And these environment variables are passed in to a root process execution as environment variables.
868580	871310	And they're considered arguments by the underlying operating system.
872500	874820	Now at this point, this is exciting.
874820	880200	We have the ability to potentially mess around in the environment variable world.
880460	894280	And I love injecting and so I pulled open my favorite web proxy and intercepted a log in request expecting to have some fun and unfortunately I was hit with some custom client side encryption which was unexpected.
894580	899140	So you can see that when I intercepted the request, there's this IK message parameter.
899140	900280	And don't squint too closely.
900280	901480	It's not too important.
902040	910140	There's cipher text, a cipher text argument and you can see that it mentions RSA, AES, things of that nature with a lot of sort of scrambled text.
910240	912280	So there's some encryption at play here.
913060	917520	And the question is, what's the deal with the custom encryption scheme on the log in page?
917520	919060	Because we want to have fun.
919700	927000	A researcher named Fabian Tamp published a blog post in 2021 about this exact custom Synology encryption routine which was very useful.
927000	928560	I recommend checking it out.
928620	944240	Essentially the scheme is layered RSA plus AES to satisfy customer encryption, customer data encryption and transit requirements when using the HTTP web service that started by default in many Synology disk station configurations.
944400	954300	So essentially Synology wanted to address this use case where the HTTP service starts by default and they want encryption so that these credentials aren't transmitted in plain text.
955260	962380	A series of network requests are performed to web APIs to gather the required cryptographic materials.
962600	969800	Then a client side JavaScript implementation is executed with the log in form data to encrypt it.
969800	974840	And lastly, the encrypted data is submitted and then eventually decrypted in the back end.
975880	982580	So in order to inject, we're either going to have to deal with this in some capacity or fall back on alternative tactics.
982940	988080	And let's fall back on alternative tactics as we don't have much time for this project in my position.
988080	992360	The DSM web application is Vue.js client side framework.
992580	998100	We can avoid this crypto soup side quest entirely by using a couple of tricks here.
998100	1008600	We'll install the Vue dev tools extension, the official developer tools and we'll install the Vue force dev browser extension which is an unofficial tool that's incredibly useful.
1008620	1014160	It tricks the official dev tools into thinking a Vue application isn't in production mode.
1014160	1023800	So it can be used to tamper a little bit with the application and then use Vue dev tools in its intended capacity to be able to modify client side fields and debug in a proper capacity.
1025360	1028380	Once we have all that configured, we can tamper usernames.
1028380	1033540	We'll find the DSM log in form object and we see there that username is a JSON string.
1033540	1039020	So we'll set it to username in all caps as we have been and submit our log in request.
1040640	1046820	Our bash snippet captures user equals username in the second file which is the same thing we previously observed.
1047200	1050680	So this is just getting a sense of what was happening before.
1051060	1052900	And now we'll inject a little bit.
1053160	1057060	We'll do a classic username line feed next line.
1057060	1061660	A classic line feed injection and hit submit and see what happens in our bash snippet.
1062460	1065880	This time something really notable happens.
1065880	1075300	We can see user equals plug in value start, username, line feed, next line, plug in value end.
1075300	1080300	Custom book end delimiters have been injected to encase the attacker provided data.
1080300	1089100	So rather than stripping out any sort of line feeds, they're instead saying, okay, we can expect this and we'll safely encompass them with these delimiters, one on each side.
1091080	1100840	We confirm this by checking the logs where we can see that args equals user equals username, line feed which was preserved in the log, next line.
1100840	1103520	But then, most importantly, there's a comma.
1103560	1107700	So you can see comma, type equals password, comma, session equals web UI.
1107700	1110240	And that's the delimiter between arguments.
1110240	1117600	We haven't escaped the context we were hoping to even though our line feed injection was preserved in the logs and on the back end.
1119340	1127880	So to recap, by default, each environment variable argument is on its own line as is the case if we submit user equals username.
1128300	1133700	When line feed characters are present, custom book end delimiters are automatically added.
1133700	1137740	User equals plug in value start, then our data, plug in value end.
1137740	1142480	And notably, we see type equals password, session equals web UI.
1142520	1155500	Other environment variables for this native code root process execution, it would be really nice to be able to escape our contained context and be able to inject new environment variables for this root process execution.
1156160	1160080	So let's inject our own delimiters as many of you may be thinking.
1160940	1164880	In order to craft an injection, first we need to know what we have to deal with.
1164880	1168140	User equals, that exists by default no matter what.
1168140	1175940	And if we have line feed characters in our data, user equals plug in value start, our data, plug in value end will be present.
1175940	1178020	This is what we're working with to begin with.
1178920	1180740	Let's craft an injection.
1180780	1183460	Plug in value start, username.
1183620	1186700	Plug in value end, we've injected a delimiter.
1186880	1189220	New variable equals plug in value start.
1192700	1199180	We're going to set it to test and then close out the context with the preexisting plug in value end delimiter.
1200520	1205400	When we log in like this, we see that the second file logs something interesting.
1205400	1209080	So it appears that the device has not escaped our content.
1209080	1214600	Username, plug in value end, new variable equals plug in value start, test, plug in value end.
1214600	1223340	And if we check the log files, indeed, under args, we see user equals username, comma, new variable equals test.
1223340	1227620	We've injected a new environment variable for this root process execution.
1227620	1229820	Pretty exciting without authentication.
1231560	1239100	So without authenticating, we can set arbitrary new environment variables for a root owned native code process execution.
1239100	1240620	It's a strong primitive.
1240620	1246520	However, we can't clobber or pollute log in success variables, which was something I tried right away.
1246520	1255120	And it's because the overwrite essentially happens when we can inject those, happens after the log in attempt is completed during the post execution and cleanup.
1256200	1262340	Moreover, reversing for Synology specific variables we might be able to target yields nothing.
1262400	1272400	There are no custom environment variables being checked for that might be injectable and there's nothing on that front that is exploitable based on my experience.
1273080	1276300	So the question really is, how can this be weaponized?
1276980	1286140	That brings us to the main section of this talk, weaponizing Linux environment variable control, particularly in a remote capacity as we've seen here.
1286140	1287560	Let's talk about it now.
1289140	1291940	And we'll cover existing techniques.
1292580	1297500	So by default, exploitation of environment variables often focuses on local threat models.
1297500	1301980	If you Google around, people usually tend to assume that you're talking in a local context.
1301980	1307640	However, there is quite a bit of work that's been done for remote environment variable exploitation on Linux.
1308040	1322600	Now, my disguise the limit, white paper that's being published today in tandem with this talk, references remote exploitation work by Orange Sci, Felix Wilhelm, Y4ER, Watchtower and LTAM, a security research firm.
1323040	1326200	So lots of good research that's been published on that topic.
1326940	1332440	In specific, let's talk about LTAM publications because they nicely encapsulate all known techniques.
1332740	1338480	The security firm LTAM has published some great research on remote environment variable exploitation on Linux.
1338660	1341820	Hacking with environment variables was published in 2020.
1341940	1349260	And in that, they documented a number of different techniques when the environment variables are being injected into a well-known process.
1349260	1352480	For example, Python, Perl, Node.js or Ruby.
1352480	1364080	If you're injecting environment variables for a Node.js process, you might be able to inject Node underscore options, which is evaluated by the Node runtime and can be used to establish remote code execution.
1365700	1370580	They also published remote LD preload exploitation in 2017.
1370580	1373120	And this is another great piece of research.
1373120	1381780	Typically, LD preload is an environment variable that can be set with ‑‑ that's pointing to a local native code library on disk.
1381980	1388920	And it can be used to load that library before any other libraries in order to establish remote code execution.
1388920	1395160	So if you have a clean disk write, you can upload a file, then load it and establish arbitrary remote code execution.
1396080	1402140	In their publication, they documented a novel scenario where they did not have a clean disk write.
1402820	1411220	Instead, they used LD preload to preload a mapped file descriptor in memory via proc FS, self FD.
1411220	1418760	So essentially, they performed a web request and in the body of the request, they had an ephemeral file, their malicious library.
1419620	1429820	Then, although the file was not uploaded to disk, they reflectively loaded it from memory using LD preload with proc FS and established remote code execution.
1429820	1431840	A really cool piece of research.
1431840	1441340	But unfortunately, during my work, I was not able to establish the ability to upload arbitrary files in a file descriptors mapped.
1442640	1445000	So that brings us to some local techniques historically.
1445000	1446380	Let's talk through those.
1446820	1452800	Researchers have targeted G lib C profiling features on Linux for local privilege escalation in the past.
1452800	1460680	In specific, I'll mention CVE 20103856, although the CVE details are not quite as important as the techniques used to exploit.
1461620	1473400	Tavis Ormandy abused PC profile underscore output, part of lib PC profile, and Todor Denev exploited mem usage underscore output in order to exploit this vulnerability.
1473520	1489520	So by setting these environment variables on systems with these libraries available, they can be used to write debugging files, profiling files, to arbitrary file paths, and this technique leverages a controlled file path, right?
1489520	1492700	However, an uncontrolled file content, right?
1492760	1502520	So an attacker that sets these environment variables can create arbitrarily named files anywhere on the system without any control of the contents of the files on the system.
1504100	1507900	So let's talk through our options based on what we've covered so far.
1508400	1511140	We could target technology specific variables.
1511140	1520980	For instance, if Synology checked for certain arguments that weren't present by default or things of that nature, an interpreted language process injection perhaps, but those are present.
1520980	1523680	Those were not viable based on my research.
1524060	1529540	We could get a file on disk or ephemerally mapped to then use LD preload to preload that library.
1529820	1538040	And unfortunately, try as I might, caching mechanisms, anything of that nature, I was unable to get an unauthenticated file upload on Synology disk station manager.
1539300	1548360	We could also leverage a profiling output variable to write a junk file with a controlled path for command execution.
1548660	1555770	This is in line with CVE-2024-3400 affecting a Palo Alto piece of software.
1556460	1563640	And what happened in that case was an arbitrary file write was possible to create file names that contained command injections.
1563780	1577500	Then once per hour, an automated telemetry collection service crawled that directory and unsafely concatenated the poisoned file names into a command string, thus establishing remote code execution.
1578100	1586960	Try as I might, I was unable to find a primitive of this nature, essentially a second vulnerability where file names were unsafely concatenated with command strings.
1587780	1591920	So that brings us to the point of needing a new technique.
1595310	1597070	What is LD debug?
1597470	1602250	LD debug is a central figure in this exploitation strategy that I'm presenting today.
1602250	1607350	It's been around since GLIB C2.1, circa 1999.
1607410	1610430	So for quite a while and it's available on most modern systems.
1610550	1615830	It outputs verbose debugging information about the operation of the Linux dynamic linker.
1616510	1619250	We'll see what some of the options it supports are now.
1620850	1626110	You can write all bindings, files, or libraries in the value.
1626110	1629310	And we'll stick to libraries for now in order to see that in action.
1631270	1638650	We'll run the ID command with LD debug set to libs to print the library search path information.
1639910	1645890	Down at the bottom, we see the expected UID information that we might expect from an ID command.
1645910	1648630	However, in between, there's some other interesting info.
1648750	1658150	In standard error, we see that there are five spaces, followed by a process ID number, followed by a colon, and then some information about the library search path.
1659190	1665270	So it's attempting to find the library libselinux.so.1, libc.so.6.
1665270	1677650	And one by one, it's searching known locations for libraries until it establishes where that library is at and it loads it and then prints a blank line with the exception of five spaces, a process ID number, and a colon.
1681040	1688440	Now, let's talk about LD debug output, which is a partner variable of the last environment variable we discussed.
1688580	1695900	LD debug output was implemented in the same circa 1999 version, and it is essentially a partner of LD debug.
1696400	1699560	Normally, LD debug output is written to standard error.
1699580	1709860	But if LD debug output is defined, then instead, the contents of the standard error messages are written into a file specified by the value of this environment variable.
1710720	1717900	Notably, it's written with the suffix period followed by the process ID number appended to the path name.
1717900	1719320	So let's see that now.
1720060	1722020	LD debug equals libs.
1722020	1727020	LD debug output equals var temp debug file, and we'll run ID once again.
1727860	1730800	This time, we don't see anything printed to standard error.
1730800	1732840	We see the expected ID output.
1733360	1736780	If we check in var temp, we can find our debug file.
1736780	1742100	In addition to the file name we specified, a period and process ID number has been appended.
1743100	1750420	We'll grab the first three lines of this file and confirm that it contains the output that we expect, the LD debug information.
1750420	1756120	So you can see there a different process ID number, but the same five spaces, process ID number, and a colon.
1757280	1761460	So at this point, we've accomplished something, although it's a little hard to say what.
1761520	1766240	We sit and think for a moment and try to determine what exactly we've accomplished at this point.
1766660	1774360	We've been able to write an arbitrary file, but we don't even control the full file extension, nor do we control any of the file contents.
1774620	1785300	And so we think to ourselves that it might be valuable to be able to control more of the file, and we're going to try to do that now in order to get at least a dirty file right, controlling some of the contents.
1787040	1789960	We'll fall back on LD debug to do this.
1789960	1796900	LD debug, as I mentioned, is an environment variable to load a library before any other libraries on Linux.
1797460	1800680	So we'll set an LD preload of the string test.
1802400	1807840	Immediately we see an error because the linker cannot find a library called test, and that is expected.
1808940	1815980	If we check inside of our file, we see that line one now has some ‑‑ some user input there.
1815980	1817680	You can see library equals test.
1817680	1819140	That's tainted data.
1819140	1822120	We see a second line where we don't control anything.
1824600	1827640	So let's talk through our existing capabilities.
1827840	1831400	We have the ability to write files to a path that we mostly control.
1831540	1834440	We do not control the created files extension.
1835060	1840620	We only control a string in the middle of a content line, just that test snippet in our example.
1841860	1844920	Exploitation would require a very lax parser.
1845160	1855540	Even if we could inject a PHP opening tag with some malicious code and then a PHP closing tag, the file extension is still not controlled and I wasn't able to find a way to escape that in any capacity.
1858080	1861240	Once again, we'll look in the mirror and think very hard.
1861920	1864020	What have we done up to this point that worked?
1864040	1865540	Inject delimiters.
1865760	1870640	So we're going to fall back on a common friend and try to inject some more delimiters to see what happens.
1871780	1876100	We'll set LD debug to libs, LD debug output to var temp out.
1876540	1884500	And this time we're going to LD preload, test preload, line feed, line 2, line feed, line 3 with the ID command.
1885900	1896680	So this time the error, which is robust, does indicate that object test preload, line feed, line 2, line feed, line 3 cannot be preloaded, which is a promising start.
1896680	1910140	And indeed if we check in our file, we see that the Linux dynamic linker does not properly handle line feed characters in a library search path ‑‑ in a library path when printing to LD debug.
1910140	1914480	So you can see that we have those five spaces still with the process ID number and a colon.
1914760	1916780	Then we have some uncontrolled content.
1916900	1919080	Test preload, which is our content .
1919080	1925480	And then on line 2, for a beautiful moment, we control all of the entire line, which is an exciting primitive.
1926200	1930140	We also see line 3, which then escapes additional uncontrolled content.
1930140	1931860	There's lots of junk written after that.
1931860	1935640	But for line 2, we control all of that content and that's important to us.
1936500	1941400	We have the ability to write files to a path that we mostly control, although we don't control the extension.
1942420	1946740	We do not create ‑‑ control any of that extension and also it's randomized.
1946740	1951760	So that number is going to change, making it difficult to predict in any exploitation strategy.
1952660	1958400	However, we do now control entire lines of the file's contents as we saw there with line 2.
1958400	1966800	And as it turns out, that's enough for remote code execution on most modern Linux systems when injecting into a root native code process.
1968820	1971700	Many of you may be familiar with the cron demon.
1971700	1976600	It's an automated tool in order to execute commands on a routine schedule.
1976600	1979440	It's installed by default on most Linux systems.
1979460	1982860	It parses files in etsy cron.d.
1983200	1985960	It is file extension agnostic.
1985960	1990400	So it doesn't matter what the file name is, just so long as it's in etsy cron.d.
1991100	1999960	It also has a permissive parser based on testing and often ignores malformed lines depending on what characters are in those lines.
2001420	2003780	Here's an example cron tab file.
2004060	2006560	You can see the first line is the hash symbol.
2006560	2007600	This is a comment.
2007600	2008980	That's a comment line.
2008980	2016300	And on line 2, we see an execution schedule of every one minute defined by those asterisks running as root to touch a file.
2016820	2020980	Now, note that the second line is delimited by spaces.
2021780	2024300	In between each character, there are some spaces.
2026500	2028120	Here's what we're working with.
2028120	2031960	On line 1, we have a bunch of garbage and we don't control most of the beginning of it.
2032140	2036420	On line 2, we have a number of asterisks and root and touch executed.
2039940	2043940	So we're able to execute this in a meaningful manner.
2044640	2053320	And in order to test, we're going to set LD preload to NOP line feed in order to escape that first line of junk.
2053440	2057860	Then an execution schedule and the user root and we're going to execute ID.
2057860	2061960	Then a line feed and then NOP to escape that third line of junk contents.
2063680	2066020	This time something unexpected happens.
2066140	2071020	We see that there are a number of asterisks spread out as different libraries.
2071020	2075780	So we expected this all to be one library, but instead it's thinking that there are a number of different libraries.
2076240	2082800	And when we check our file, we can see that it's attempting to find library NOP, line feed, asterisk.
2082980	2084480	And the rest of our payload is cut off.
2084480	2086660	So the question is, really, why is this happening?
2088100	2098200	When we look at documentation for LD preload, we see that the items of the list can be separated by spaces or colons and that there is no support for escaping either character.
2098480	2107680	So it's possible to LD preload multiple libraries and if you have a space present in your data, it says, all right, that's a delimiter for two separate libraries.
2107860	2113820	But the problem is that we need to inject spaces if we want to be able to write a crontab file and execute malicious code.
2114780	2118400	So this is the face that I made at that point in the research cycle.
2118400	2121980	So close but not quite there and not really sure what to do next.
2122360	2128520	So after thinking about it for a bit, I determined that the best course of action would be to inject more delimiters.
2128520	2132540	Delimiters are the name of the game with this project and we'll fall back on that classic friend.
2134100	2140800	Now, as it turns out, although it's not formally documented, cron daemon allows you to use tabs instead of spaces.
2140800	2153900	So we'll inject NOP, line feed, then an execution schedule and a user and a command ID delimited by tabs, then a line feed and then we'll add a NOP just to escape that additional content on the third line.
2155600	2160160	This time we can see that the linker error maintains something exciting.
2160160	2169000	So rather than splitting our payload out, we now have the second line, asterisks, delimited by tab, and then those additional fields for the crontab payload.
2170040	2179340	And if we check our file, we can see that indeed in the first line, we don't control all of the content, but on the second line we do when delimited by tabs.
2179440	2188120	So we see asterisk, asterisk, asterisk, every one minute the root user will execute ID delimited by tabs and then we escape the content on the third line.
2189900	2194260	So, let's craft an injection now based on what we know.
2194480	2198660	By default we're going to have user equals plugin value start presence.
2198660	2205120	And we're also going to have plugin value end, assuming that we inject at least one line feed character, which we will.
2206400	2208520	So here's a payload to try.
2208780	2210360	Username, plugin value end.
2210360	2213300	This is irrelevant, it's just necessary for the binary.
2213540	2222340	Then we'll say LD preload, an environment variable definition, equals plugin value start, NOP, line feed, that's to escape that first line of uncontrolled content.
2222340	2225720	Then a reverse shell payload for a crontab.
2225720	2232660	We can see that it's tab delimited and we're using a base 64 encoded string there to avoid bad character problems.
2232660	2236000	We'll decode that and execute it using the default shell.
2236280	2241620	Then a line feed character and the string NOP to escape the third line of uncontrolled content.
2242020	2245380	Plugin value end to end the definition of LD preload.
2245380	2247280	LD debug equals libs.
2247340	2251040	LD debug output equals etsy cron D hacks.
2251620	2257540	And lastly we'll define the escape variable which is just garbage but it's required in order to cleanly escape.
2257540	2264380	So we see plugin value start, NOP and then that initially injected plugin value end to close things out in a clean manner.
2265620	2278160	Now funny enough, this is a really interesting vulnerability because you can quite literally copy paste this payload and paste it into the user name field on the web service and hit log in and get a reverse root shell which is pretty awesome.
2278160	2283700	There's something to be said about not requiring any form of automation and just being able to copy paste a payload in my opinion.
2286240	2286800	All right.
2286800	2289880	That brings us to the exploit presentation where you'll be able to see it in action.
2289880	2290960	Let me pull up the video.
2297550	2298350	All right.
2300730	2301190	Okay.
2301190	2307950	So we'll start a listener with netcat on port 4444 and we'll launch our exploit against the web service.
2308690	2314110	So we can see that the malicious log in is performed and this is the log in that I just mentioned with that user name.
2314110	2318090	It will inject environment variables for that root owned process execution.
2319130	2325190	There's a little bit of a wait here because cron tab and cron demon has to run it every one minute but it won't run immediately.
2325570	2333330	We see we catch a reverse shell as the user root and we're on a disk station of which there are about a million public facing.
2333330	2334370	So pretty exciting.
2337070	2338470	Thank you so much.
2338530	2344790	And then we also touched the rapid 7 RCE file just for style.
2346410	2346990	All right.
2346990	2348350	Let me pull this back up.
2350190	2350990	Great.
2351130	2352750	That brings us to the end of the talk.
2352750	2359950	And I just wanted to extend a special thank you to everyone listed on this slide including some organizations and individuals that were cited as references.
2359950	2362930	Without these people, it would not have been possible to do this talk.
2362930	2367650	Huge shout out also to the people that helped out after this slide was finalized.
2367650	2369030	The team at rapid 7.
2369030	2370650	Huge thank you to all of them.
2370970	2373030	And lastly, thank you to all of you for attending.
2373030	2375670	I really appreciate it and you can find my info there.
2375670	2376930	Have a great rest of your day.
