WEBVTT

00:00.000 --> 00:03.180
Ryan Edmonds is here to chat with you and he's going to tell you about what.

00:03.180 --> 00:05.300
So let's welcome Ryan to the stage.

00:05.580 --> 00:07.060
Thank you so much.

00:07.060 --> 00:08.080
Thanks, Bruce.

00:09.760 --> 00:10.720
All right.

00:10.720 --> 00:11.540
Hi, everyone.

00:11.540 --> 00:17.640
Welcome to my talk titled Disguised Delimit, Exploiting Synology NAS with Delimiters and Novel Tricks.

00:17.640 --> 00:18.800
Let's get started.

00:19.600 --> 00:21.000
Picture this.

00:21.120 --> 00:23.900
You're at home one day and the doorbell rings.

00:24.520 --> 00:27.020
You open the door and are handed a package.

00:27.180 --> 00:28.960
It's addressed to you.

00:28.960 --> 00:35.320
After cracking it open and rifling through the packing peanuts, you're greeted with a small black box.

00:35.320 --> 00:37.020
What is it, you might ask?

00:37.020 --> 00:43.280
A Synology B Station, a consumer version of the Synology NAS line, network attached storage.

00:43.360 --> 00:48.280
And it's going to be a challenging adversary over the coming weeks of research.

00:48.580 --> 00:55.520
That's the position that I found myself in last year when I wanted to participate in the Pwn2Own exploitation competition.

00:55.560 --> 01:01.600
This talk will discuss what I learned while performing zero day research on Synology network attached storage.

01:01.740 --> 01:07.320
And the end result of that research was a $40,000 prize at the Pwn2Own Ireland competition.

01:08.020 --> 01:09.740
Let's talk overview.

01:09.860 --> 01:11.340
First off, the introduction.

01:11.340 --> 01:12.700
And we're in it right now.

01:12.700 --> 01:18.640
Second, we're going to have a brief topic covering Synology NAS internals and attack surface.

01:18.640 --> 01:19.900
Very high level.

01:20.140 --> 01:25.620
Number three, we'll talk through discovering an unauthenticated vulnerability via case study.

01:25.620 --> 01:27.960
This is the research I performed last year.

01:28.000 --> 01:36.700
Four, we'll have a novel exploitation technique for Linux that I stumbled upon while developing the exploit targeting Synology last year.

01:36.700 --> 01:41.240
And fifth, we'll have an exploit presentation so you can see everything come together in video.

01:41.440 --> 01:42.680
Let's get started.

01:43.320 --> 01:44.360
Who am I?

01:44.360 --> 01:45.860
My name is Ryan Emmons.

01:45.860 --> 01:49.160
I am a staff security researcher at Rapid7.

01:49.160 --> 01:58.280
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.

01:58.280 --> 02:04.900
I've reported bugs to notable organizations like Oracle, Microsoft and SonicWall in the past.

02:05.660 --> 02:08.780
For this case study research, my project goals were clear.

02:08.780 --> 02:11.960
I wanted to write an unauthenticated exploit.

02:12.100 --> 02:21.200
It should target Synology DSM or disk station manager, the proprietary Linux based operating system that most or all Synology NAS devices run.

02:21.740 --> 02:27.860
I wanted it to work in the default config, no extra changes necessary to make the device vulnerable.

02:28.220 --> 02:31.720
I didn't want to employ any adversary in the middle threat models.

02:31.720 --> 02:35.280
I just wanted to point and shoot remotely.

02:36.060 --> 02:42.420
I wanted the exploit to be highly reliable as opposed to leveraging bugs that were less reliable.

02:42.740 --> 02:44.940
And this was a tough one.

02:44.940 --> 02:50.420
The timeline that I had to research was three and a half weeks due to the conference registration.

02:50.420 --> 02:52.780
So I knew that I had to move quickly.

02:53.760 --> 02:56.380
Let's talk about Synology DSM quickly.

02:56.500 --> 03:02.120
So Synology DSM is the proprietary Linux based operating system used by Synology NAS.

03:02.120 --> 03:08.040
It's used for file management, multimedia, collaboration, productivity, virtualization and surveillance.

03:08.080 --> 03:16.640
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.

03:17.820 --> 03:32.200
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.

03:32.260 --> 03:36.040
So these are devices likely to be vulnerable exposed to the Internet.

03:36.260 --> 03:39.340
I knew that there was significant potential impact.

03:39.340 --> 03:41.120
Tons of initial access.

03:41.120 --> 03:49.820
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.

03:51.300 --> 03:52.160
All right.

03:52.160 --> 03:54.360
That brings us to a high level.

03:54.400 --> 03:59.300
We're going to talk through Synology DSM internals and attack surface on a high level.

03:59.300 --> 04:01.240
So let's get started with that.

04:01.800 --> 04:09.760
When we're talking about research targets, all Synology NAS runs some variation of disk station manager OS or DSM OS.

04:10.000 --> 04:19.120
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.

04:19.680 --> 04:26.900
There's also a community project that's very helpful called virtual dash DSM to run DSM in a docker container.

04:26.900 --> 04:32.800
So I recommend that if an individual wants to spin up a quick DSM instance, test exploits, pull binaries or whatnot.

04:33.620 --> 04:37.760
Different Synology NAS devices have different default configurations.

04:37.760 --> 04:49.200
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.

04:49.420 --> 04:53.320
Different Synology NAS products also run different architectures.

04:53.320 --> 05:02.020
So the B station is ARM whereas the disk station is x8664 and older disk stations are x86.

05:04.040 --> 05:12.340
Synology offers a package manager, sort of an app store, to install add on packages for the operating system.

05:12.340 --> 05:22.260
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.

05:23.700 --> 05:28.740
Synology B station does not expose SSH or other advanced services by default.

05:28.740 --> 05:31.140
It's a consumer focused device.

05:31.380 --> 05:43.860
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.

05:44.400 --> 05:49.840
Local access can be enabled to configure local credentials and enable SNB, a useful target.

05:49.840 --> 05:55.000
And the disk station line of products has an admin panel with an SSH toggle.

05:55.000 --> 05:57.900
So it's a little more straightforward for security researchers.

05:59.400 --> 06:03.700
Covering a brief history of exploitation at the time that I went into my project.

06:03.700 --> 06:13.620
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.

06:13.620 --> 06:36.040
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.

06:37.460 --> 06:52.080
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.

06:52.560 --> 07:00.000
When talking about remote attack surface the DSM web service is by far the most widely exposed attack surface.

07:00.000 --> 07:13.600
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.

07:14.180 --> 07:19.540
So the question is what's reachable without authentication for the Synology DSM web service?

07:20.680 --> 07:28.080
And that brings us to our case study, discovering an unauthenticated vulnerability in Synology NAS and we'll dive in now.

07:28.920 --> 07:38.030
The DSM web service listens on ports 5000, 5001, 6600 and 6601 by default depending on the disk station that you got.

07:38.480 --> 07:41.660
A lot of researchers have tested this attack surface.

07:41.680 --> 07:48.600
In fact it was scoped for a few years prior to me picking up the project so I knew it would be reasonably hardened.

07:49.140 --> 07:52.400
But the hope was that maybe we could do it in a new way.

07:54.580 --> 07:59.400
Let's talk about the Synology DSM web service authentication workflow.

07:59.720 --> 08:01.800
Here's the login portal that you see right here.

08:01.800 --> 08:05.160
So step one for the B station is to input a user name.

08:05.380 --> 08:08.280
We'll do that with the string user name in all caps.

08:08.880 --> 08:12.460
Next we have to input a password and we'll just enter password.

08:12.460 --> 08:14.620
It's not a valid set of credentials.

08:15.220 --> 08:20.740
We'll click the blue button and the loading graphic will spin for about five seconds.

08:20.980 --> 08:27.720
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.

08:28.720 --> 08:34.120
And because we shouldn't be logged in, we have invalid credentials, a generic failure message appears.

08:34.120 --> 08:35.820
The account or password is invalid.

08:35.820 --> 08:37.060
Please try again.

08:37.340 --> 08:43.100
Now I was curious, during that five second period, what exactly is going on under the hood of the B station?

08:43.100 --> 08:45.840
What's it, what kind of calculations is it doing?

08:46.400 --> 08:49.700
So I wanted to dive in further and monitor at the OS level.

08:50.400 --> 08:56.660
In order to do that, I used the eBPF tool BPF trace with a simple script.

08:56.700 --> 09:06.060
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.

09:06.500 --> 09:08.540
It was very useful introspection.

09:09.120 --> 09:11.460
So we'll kick off a BPF trace program.

09:12.480 --> 09:17.040
And when we log in again, we see three executions take place.

09:17.200 --> 09:21.400
And number one, number two and number three each do a unique thing.

09:21.400 --> 09:22.960
So let's take a look now.

09:23.880 --> 09:31.760
Number one, the first execution, we see command is null, which is unexpected at first until we dig a little bit deeper.

09:31.900 --> 09:36.560
And we see that the program is a CGI binary execution.

09:36.560 --> 09:38.120
So it makes a little more sense.

09:38.120 --> 09:50.300
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.

09:51.700 --> 09:53.620
Now, that's that.

09:53.620 --> 09:55.100
And it's not too exciting.

09:55.100 --> 09:57.940
But what happens next is a little bit more interesting.

09:57.940 --> 10:00.520
So let's take a look at execution number two.

10:02.040 --> 10:04.920
This time we see the command string set.

10:05.280 --> 10:08.820
It's seno CGI dash plugin dash web log in.

10:08.820 --> 10:11.440
And it has the argument dash dash pre.

10:11.440 --> 10:12.660
Note that.

10:13.320 --> 10:17.920
We also see some additional environment variables set that are different from the previous binary.

10:18.060 --> 10:19.460
User equals blank.

10:19.460 --> 10:21.140
Type equals password.

10:21.140 --> 10:22.660
Is known device equals no.

10:22.660 --> 10:32.280
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.

10:32.280 --> 10:33.840
So that immediately starts our fancy.

10:33.840 --> 10:35.320
Seems very interesting.

10:36.720 --> 10:39.360
And we'll explore that a bit more here in a moment.

10:39.360 --> 10:44.500
The third execution when a user attempts to log in as seen through the BPF trace monitor.

10:44.600 --> 10:48.060
We see the same binary is executed.

10:48.060 --> 10:51.240
This time with the post argument.

10:51.520 --> 10:53.000
So before it was pre.

10:53.000 --> 10:54.540
And now it's post.

10:54.800 --> 10:58.260
We also see that the environment has been populated to a degree.

10:58.260 --> 11:00.320
User equals user name in all caps.

11:00.320 --> 11:01.440
That's our tainted data.

11:01.440 --> 11:03.740
That's our string that we provided as our user name.

11:03.740 --> 11:05.660
We also see some additional information.

11:05.660 --> 11:07.620
The same information we saw previously.

11:07.960 --> 11:10.060
And we see status equals fail.

11:10.060 --> 11:11.880
Result equals negative two.

11:11.900 --> 11:16.600
So by this point in the process, we know that the authentication attempt has failed.

11:17.560 --> 11:26.720
Once more, we see this interesting environment variable seno underscore plugin underscore args with a different file path in the same temp directory.

11:27.380 --> 11:31.580
So we really at this point want to know what's inside of that directory, what's inside of those files.

11:31.580 --> 11:33.500
And when we go to look, they're not there.

11:33.500 --> 11:36.120
These are very ephemeral files that have already been deleted.

11:36.480 --> 11:42.600
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.

11:44.660 --> 11:48.640
To do so, we're going to use inotify, the inotify API for Linux.

11:49.040 --> 11:50.900
More specifically, inotify wait.

11:50.900 --> 11:56.820
We'll monitor that temp directory and we'll look for creation, modification and deletion events in that directory.

11:57.140 --> 12:01.520
Once we start that monitor, then we'll kick off another authentication attempt.

12:02.880 --> 12:05.520
Inotify wait picks up a number of different events.

12:05.520 --> 12:10.160
The creation and modification and deletion of a couple of files.

12:10.480 --> 12:16.060
We want to know what's in these files and unfortunately, inotify is not quick enough to trigger catting the file.

12:16.060 --> 12:18.940
So we'll do it in a hacky way.

12:20.520 --> 12:22.160
We'll race with bash.

12:22.380 --> 12:25.960
You don't have to know what this bash snippet is doing nor pay that much attention.

12:25.980 --> 12:32.680
Essentially, it's constantly trying to grab those files and when they're written for a snap second, it prints them to the console.

12:32.800 --> 12:34.520
File one and file two.

12:35.300 --> 12:40.240
We're going to start this bash snippet and then we're going to attempt to log in again and see what it prints.

12:42.120 --> 12:44.060
So, first file.

12:44.140 --> 12:46.960
User equals type equals password.

12:46.960 --> 12:49.240
Is known device equals no.

12:49.240 --> 12:50.920
Pretty standard stuff.

12:51.440 --> 12:54.400
The second file, user equals user name.

12:54.400 --> 12:55.820
That's our data again.

12:55.820 --> 12:57.000
Type equals password.

12:57.000 --> 12:58.280
Session equals web UI.

12:58.280 --> 12:59.360
So on and so forth.

12:59.360 --> 13:01.600
And we see status equals fail.

13:01.600 --> 13:03.320
Result equals negative two.

13:03.660 --> 13:09.500
So these seem to be similar to the environment variables we saw previously for the second and process executions.

13:09.980 --> 13:20.480
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.

13:20.480 --> 13:25.120
So user equals user name is our string and the user agent is also provided by the attacker.

13:27.440 --> 13:31.380
Based on what we've discussed so far, the workflow is something like this.

13:31.380 --> 13:34.740
The CGI binary which is native code executes.

13:34.780 --> 13:40.180
Then the SenoCGI plugin dash web log in binary executes twice.

13:40.180 --> 13:44.340
First with the pre argument and second with the post argument.

13:44.340 --> 13:47.140
Making a total of three process executions.

13:47.500 --> 13:59.320
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.

13:59.320 --> 14:01.780
And our data is written into those files.

14:03.000 --> 14:07.140
We'll confirm the theory by going to var log and checking the log file.

14:07.140 --> 14:19.940
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.

14:20.040 --> 14:28.540
And these environment variables are passed in to a root process execution as environment variables.

14:28.580 --> 14:31.310
And they're considered arguments by the underlying operating system.

14:32.500 --> 14:34.820
Now at this point, this is exciting.

14:34.820 --> 14:40.200
We have the ability to potentially mess around in the environment variable world.

14:40.460 --> 14:54.280
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.

14:54.580 --> 14:59.140
So you can see that when I intercepted the request, there's this IK message parameter.

14:59.140 --> 15:00.280
And don't squint too closely.

15:00.280 --> 15:01.480
It's not too important.

15:02.040 --> 15:10.140
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.

15:10.240 --> 15:12.280
So there's some encryption at play here.

15:13.060 --> 15:17.520
And the question is, what's the deal with the custom encryption scheme on the log in page?

15:17.520 --> 15:19.060
Because we want to have fun.

15:19.700 --> 15:27.000
A researcher named Fabian Tamp published a blog post in 2021 about this exact custom Synology encryption routine which was very useful.

15:27.000 --> 15:28.560
I recommend checking it out.

15:28.620 --> 15:44.240
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.

15:44.400 --> 15:54.300
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.

15:55.260 --> 16:02.380
A series of network requests are performed to web APIs to gather the required cryptographic materials.

16:02.600 --> 16:09.800
Then a client side JavaScript implementation is executed with the log in form data to encrypt it.

16:09.800 --> 16:14.840
And lastly, the encrypted data is submitted and then eventually decrypted in the back end.

16:15.880 --> 16:22.580
So in order to inject, we're either going to have to deal with this in some capacity or fall back on alternative tactics.

16:22.940 --> 16:28.080
And let's fall back on alternative tactics as we don't have much time for this project in my position.

16:28.080 --> 16:32.360
The DSM web application is Vue.js client side framework.

16:32.580 --> 16:38.100
We can avoid this crypto soup side quest entirely by using a couple of tricks here.

16:38.100 --> 16:48.600
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.

16:48.620 --> 16:54.160
It tricks the official dev tools into thinking a Vue application isn't in production mode.

16:54.160 --> 17:03.800
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.

17:05.360 --> 17:08.380
Once we have all that configured, we can tamper usernames.

17:08.380 --> 17:13.540
We'll find the DSM log in form object and we see there that username is a JSON string.

17:13.540 --> 17:19.020
So we'll set it to username in all caps as we have been and submit our log in request.

17:20.640 --> 17:26.820
Our bash snippet captures user equals username in the second file which is the same thing we previously observed.

17:27.200 --> 17:30.680
So this is just getting a sense of what was happening before.

17:31.060 --> 17:32.900
And now we'll inject a little bit.

17:33.160 --> 17:37.060
We'll do a classic username line feed next line.

17:37.060 --> 17:41.660
A classic line feed injection and hit submit and see what happens in our bash snippet.

17:42.460 --> 17:45.880
This time something really notable happens.

17:45.880 --> 17:55.300
We can see user equals plug in value start, username, line feed, next line, plug in value end.

17:55.300 --> 18:00.300
Custom book end delimiters have been injected to encase the attacker provided data.

18:00.300 --> 18:09.100
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.

18:11.080 --> 18:20.840
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.

18:20.840 --> 18:23.520
But then, most importantly, there's a comma.

18:23.560 --> 18:27.700
So you can see comma, type equals password, comma, session equals web UI.

18:27.700 --> 18:30.240
And that's the delimiter between arguments.

18:30.240 --> 18:37.600
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.

18:39.340 --> 18:47.880
So to recap, by default, each environment variable argument is on its own line as is the case if we submit user equals username.

18:48.300 --> 18:53.700
When line feed characters are present, custom book end delimiters are automatically added.

18:53.700 --> 18:57.740
User equals plug in value start, then our data, plug in value end.

18:57.740 --> 19:02.480
And notably, we see type equals password, session equals web UI.

19:02.520 --> 19:15.500
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.

19:16.160 --> 19:20.080
So let's inject our own delimiters as many of you may be thinking.

19:20.940 --> 19:24.880
In order to craft an injection, first we need to know what we have to deal with.

19:24.880 --> 19:28.140
User equals, that exists by default no matter what.

19:28.140 --> 19:35.940
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.

19:35.940 --> 19:38.020
This is what we're working with to begin with.

19:38.920 --> 19:40.740
Let's craft an injection.

19:40.780 --> 19:43.460
Plug in value start, username.

19:43.620 --> 19:46.700
Plug in value end, we've injected a delimiter.

19:46.880 --> 19:49.220
New variable equals plug in value start.

19:52.700 --> 19:59.180
We're going to set it to test and then close out the context with the preexisting plug in value end delimiter.

20:00.520 --> 20:05.400
When we log in like this, we see that the second file logs something interesting.

20:05.400 --> 20:09.080
So it appears that the device has not escaped our content.

20:09.080 --> 20:14.600
Username, plug in value end, new variable equals plug in value start, test, plug in value end.

20:14.600 --> 20:23.340
And if we check the log files, indeed, under args, we see user equals username, comma, new variable equals test.

20:23.340 --> 20:27.620
We've injected a new environment variable for this root process execution.

20:27.620 --> 20:29.820
Pretty exciting without authentication.

20:31.560 --> 20:39.100
So without authenticating, we can set arbitrary new environment variables for a root owned native code process execution.

20:39.100 --> 20:40.620
It's a strong primitive.

20:40.620 --> 20:46.520
However, we can't clobber or pollute log in success variables, which was something I tried right away.

20:46.520 --> 20:55.120
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.

20:56.200 --> 21:02.340
Moreover, reversing for Synology specific variables we might be able to target yields nothing.

21:02.400 --> 21:12.400
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.

21:13.080 --> 21:16.300
So the question really is, how can this be weaponized?

21:16.980 --> 21:26.140
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.

21:26.140 --> 21:27.560
Let's talk about it now.

21:29.140 --> 21:31.940
And we'll cover existing techniques.

21:32.580 --> 21:37.500
So by default, exploitation of environment variables often focuses on local threat models.

21:37.500 --> 21:41.980
If you Google around, people usually tend to assume that you're talking in a local context.

21:41.980 --> 21:47.640
However, there is quite a bit of work that's been done for remote environment variable exploitation on Linux.

21:48.040 --> 22:02.600
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.

22:03.040 --> 22:06.200
So lots of good research that's been published on that topic.

22:06.940 --> 22:12.440
In specific, let's talk about LTAM publications because they nicely encapsulate all known techniques.

22:12.740 --> 22:18.480
The security firm LTAM has published some great research on remote environment variable exploitation on Linux.

22:18.660 --> 22:21.820
Hacking with environment variables was published in 2020.

22:21.940 --> 22:29.260
And in that, they documented a number of different techniques when the environment variables are being injected into a well-known process.

22:29.260 --> 22:32.480
For example, Python, Perl, Node.js or Ruby.

22:32.480 --> 22:44.080
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.

22:45.700 --> 22:50.580
They also published remote LD preload exploitation in 2017.

22:50.580 --> 22:53.120
And this is another great piece of research.

22:53.120 --> 23:01.780
Typically, LD preload is an environment variable that can be set with ‑‑ that's pointing to a local native code library on disk.

23:01.980 --> 23:08.920
And it can be used to load that library before any other libraries in order to establish remote code execution.

23:08.920 --> 23:15.160
So if you have a clean disk write, you can upload a file, then load it and establish arbitrary remote code execution.

23:16.080 --> 23:22.140
In their publication, they documented a novel scenario where they did not have a clean disk write.

23:22.820 --> 23:31.220
Instead, they used LD preload to preload a mapped file descriptor in memory via proc FS, self FD.

23:31.220 --> 23:38.760
So essentially, they performed a web request and in the body of the request, they had an ephemeral file, their malicious library.

23:39.620 --> 23:49.820
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.

23:49.820 --> 23:51.840
A really cool piece of research.

23:51.840 --> 24:01.340
But unfortunately, during my work, I was not able to establish the ability to upload arbitrary files in a file descriptors mapped.

24:02.640 --> 24:05.000
So that brings us to some local techniques historically.

24:05.000 --> 24:06.380
Let's talk through those.

24:06.820 --> 24:12.800
Researchers have targeted G lib C profiling features on Linux for local privilege escalation in the past.

24:12.800 --> 24:20.680
In specific, I'll mention CVE 20103856, although the CVE details are not quite as important as the techniques used to exploit.

24:21.620 --> 24:33.400
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.

24:33.520 --> 24:49.520
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?

24:49.520 --> 24:52.700
However, an uncontrolled file content, right?

24:52.760 --> 25:02.520
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.

25:04.100 --> 25:07.900
So let's talk through our options based on what we've covered so far.

25:08.400 --> 25:11.140
We could target technology specific variables.

25:11.140 --> 25:20.980
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.

25:20.980 --> 25:23.680
Those were not viable based on my research.

25:24.060 --> 25:29.540
We could get a file on disk or ephemerally mapped to then use LD preload to preload that library.

25:29.820 --> 25:38.040
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.

25:39.300 --> 25:48.360
We could also leverage a profiling output variable to write a junk file with a controlled path for command execution.

25:48.660 --> 25:55.770
This is in line with CVE-2024-3400 affecting a Palo Alto piece of software.

25:56.460 --> 26:03.640
And what happened in that case was an arbitrary file write was possible to create file names that contained command injections.

26:03.780 --> 26:17.500
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.

26:18.100 --> 26:26.960
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.

26:27.780 --> 26:31.920
So that brings us to the point of needing a new technique.

26:35.310 --> 26:37.070
What is LD debug?

26:37.470 --> 26:42.250
LD debug is a central figure in this exploitation strategy that I'm presenting today.

26:42.250 --> 26:47.350
It's been around since GLIB C2.1, circa 1999.

26:47.410 --> 26:50.430
So for quite a while and it's available on most modern systems.

26:50.550 --> 26:55.830
It outputs verbose debugging information about the operation of the Linux dynamic linker.

26:56.510 --> 26:59.250
We'll see what some of the options it supports are now.

27:00.850 --> 27:06.110
You can write all bindings, files, or libraries in the value.

27:06.110 --> 27:09.310
And we'll stick to libraries for now in order to see that in action.

27:11.270 --> 27:18.650
We'll run the ID command with LD debug set to libs to print the library search path information.

27:19.910 --> 27:25.890
Down at the bottom, we see the expected UID information that we might expect from an ID command.

27:25.910 --> 27:28.630
However, in between, there's some other interesting info.

27:28.750 --> 27:38.150
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.

27:39.190 --> 27:45.270
So it's attempting to find the library libselinux.so.1, libc.so.6.

27:45.270 --> 27:57.650
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.

28:01.040 --> 28:08.440
Now, let's talk about LD debug output, which is a partner variable of the last environment variable we discussed.

28:08.580 --> 28:15.900
LD debug output was implemented in the same circa 1999 version, and it is essentially a partner of LD debug.

28:16.400 --> 28:19.560
Normally, LD debug output is written to standard error.

28:19.580 --> 28:29.860
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.

28:30.720 --> 28:37.900
Notably, it's written with the suffix period followed by the process ID number appended to the path name.

28:37.900 --> 28:39.320
So let's see that now.

28:40.060 --> 28:42.020
LD debug equals libs.

28:42.020 --> 28:47.020
LD debug output equals var temp debug file, and we'll run ID once again.

28:47.860 --> 28:50.800
This time, we don't see anything printed to standard error.

28:50.800 --> 28:52.840
We see the expected ID output.

28:53.360 --> 28:56.780
If we check in var temp, we can find our debug file.

28:56.780 --> 29:02.100
In addition to the file name we specified, a period and process ID number has been appended.

29:03.100 --> 29:10.420
We'll grab the first three lines of this file and confirm that it contains the output that we expect, the LD debug information.

29:10.420 --> 29:16.120
So you can see there a different process ID number, but the same five spaces, process ID number, and a colon.

29:17.280 --> 29:21.460
So at this point, we've accomplished something, although it's a little hard to say what.

29:21.520 --> 29:26.240
We sit and think for a moment and try to determine what exactly we've accomplished at this point.

29:26.660 --> 29:34.360
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.

29:34.620 --> 29:45.300
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.

29:47.040 --> 29:49.960
We'll fall back on LD debug to do this.

29:49.960 --> 29:56.900
LD debug, as I mentioned, is an environment variable to load a library before any other libraries on Linux.

29:57.460 --> 30:00.680
So we'll set an LD preload of the string test.

30:02.400 --> 30:07.840
Immediately we see an error because the linker cannot find a library called test, and that is expected.

30:08.940 --> 30:15.980
If we check inside of our file, we see that line one now has some ‑‑ some user input there.

30:15.980 --> 30:17.680
You can see library equals test.

30:17.680 --> 30:19.140
That's tainted data.

30:19.140 --> 30:22.120
We see a second line where we don't control anything.

30:24.600 --> 30:27.640
So let's talk through our existing capabilities.

30:27.840 --> 30:31.400
We have the ability to write files to a path that we mostly control.

30:31.540 --> 30:34.440
We do not control the created files extension.

30:35.060 --> 30:40.620
We only control a string in the middle of a content line, just that test snippet in our example.

30:41.860 --> 30:44.920
Exploitation would require a very lax parser.

30:45.160 --> 30:55.540
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.

30:58.080 --> 31:01.240
Once again, we'll look in the mirror and think very hard.

31:01.920 --> 31:04.020
What have we done up to this point that worked?

31:04.040 --> 31:05.540
Inject delimiters.

31:05.760 --> 31:10.640
So we're going to fall back on a common friend and try to inject some more delimiters to see what happens.

31:11.780 --> 31:16.100
We'll set LD debug to libs, LD debug output to var temp out.

31:16.540 --> 31:24.500
And this time we're going to LD preload, test preload, line feed, line 2, line feed, line 3 with the ID command.

31:25.900 --> 31:36.680
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.

31:36.680 --> 31:50.140
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.

31:50.140 --> 31:54.480
So you can see that we have those five spaces still with the process ID number and a colon.

31:54.760 --> 31:56.780
Then we have some uncontrolled content.

31:56.900 --> 31:59.080
Test preload, which is our content .

31:59.080 --> 32:05.480
And then on line 2, for a beautiful moment, we control all of the entire line, which is an exciting primitive.

32:06.200 --> 32:10.140
We also see line 3, which then escapes additional uncontrolled content.

32:10.140 --> 32:11.860
There's lots of junk written after that.

32:11.860 --> 32:15.640
But for line 2, we control all of that content and that's important to us.

32:16.500 --> 32:21.400
We have the ability to write files to a path that we mostly control, although we don't control the extension.

32:22.420 --> 32:26.740
We do not create ‑‑ control any of that extension and also it's randomized.

32:26.740 --> 32:31.760
So that number is going to change, making it difficult to predict in any exploitation strategy.

32:32.660 --> 32:38.400
However, we do now control entire lines of the file's contents as we saw there with line 2.

32:38.400 --> 32:46.800
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.

32:48.820 --> 32:51.700
Many of you may be familiar with the cron demon.

32:51.700 --> 32:56.600
It's an automated tool in order to execute commands on a routine schedule.

32:56.600 --> 32:59.440
It's installed by default on most Linux systems.

32:59.460 --> 33:02.860
It parses files in etsy cron.d.

33:03.200 --> 33:05.960
It is file extension agnostic.

33:05.960 --> 33:10.400
So it doesn't matter what the file name is, just so long as it's in etsy cron.d.

33:11.100 --> 33:19.960
It also has a permissive parser based on testing and often ignores malformed lines depending on what characters are in those lines.

33:21.420 --> 33:23.780
Here's an example cron tab file.

33:24.060 --> 33:26.560
You can see the first line is the hash symbol.

33:26.560 --> 33:27.600
This is a comment.

33:27.600 --> 33:28.980
That's a comment line.

33:28.980 --> 33:36.300
And on line 2, we see an execution schedule of every one minute defined by those asterisks running as root to touch a file.

33:36.820 --> 33:40.980
Now, note that the second line is delimited by spaces.

33:41.780 --> 33:44.300
In between each character, there are some spaces.

33:46.500 --> 33:48.120
Here's what we're working with.

33:48.120 --> 33:51.960
On line 1, we have a bunch of garbage and we don't control most of the beginning of it.

33:52.140 --> 33:56.420
On line 2, we have a number of asterisks and root and touch executed.

33:59.940 --> 34:03.940
So we're able to execute this in a meaningful manner.

34:04.640 --> 34:13.320
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.

34:13.440 --> 34:17.860
Then an execution schedule and the user root and we're going to execute ID.

34:17.860 --> 34:21.960
Then a line feed and then NOP to escape that third line of junk contents.

34:23.680 --> 34:26.020
This time something unexpected happens.

34:26.140 --> 34:31.020
We see that there are a number of asterisks spread out as different libraries.

34:31.020 --> 34:35.780
So we expected this all to be one library, but instead it's thinking that there are a number of different libraries.

34:36.240 --> 34:42.800
And when we check our file, we can see that it's attempting to find library NOP, line feed, asterisk.

34:42.980 --> 34:44.480
And the rest of our payload is cut off.

34:44.480 --> 34:46.660
So the question is, really, why is this happening?

34:48.100 --> 34:58.200
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.

34:58.480 --> 35:07.680
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.

35:07.860 --> 35:13.820
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.

35:14.780 --> 35:18.400
So this is the face that I made at that point in the research cycle.

35:18.400 --> 35:21.980
So close but not quite there and not really sure what to do next.

35:22.360 --> 35:28.520
So after thinking about it for a bit, I determined that the best course of action would be to inject more delimiters.

35:28.520 --> 35:32.540
Delimiters are the name of the game with this project and we'll fall back on that classic friend.

35:34.100 --> 35:40.800
Now, as it turns out, although it's not formally documented, cron daemon allows you to use tabs instead of spaces.

35:40.800 --> 35:53.900
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.

35:55.600 --> 36:00.160
This time we can see that the linker error maintains something exciting.

36:00.160 --> 36:09.000
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.

36:10.040 --> 36:19.340
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.

36:19.440 --> 36:28.120
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.

36:29.900 --> 36:34.260
So, let's craft an injection now based on what we know.

36:34.480 --> 36:38.660
By default we're going to have user equals plugin value start presence.

36:38.660 --> 36:45.120
And we're also going to have plugin value end, assuming that we inject at least one line feed character, which we will.

36:46.400 --> 36:48.520
So here's a payload to try.

36:48.780 --> 36:50.360
Username, plugin value end.

36:50.360 --> 36:53.300
This is irrelevant, it's just necessary for the binary.

36:53.540 --> 37:02.340
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.

37:02.340 --> 37:05.720
Then a reverse shell payload for a crontab.

37:05.720 --> 37:12.660
We can see that it's tab delimited and we're using a base 64 encoded string there to avoid bad character problems.

37:12.660 --> 37:16.000
We'll decode that and execute it using the default shell.

37:16.280 --> 37:21.620
Then a line feed character and the string NOP to escape the third line of uncontrolled content.

37:22.020 --> 37:25.380
Plugin value end to end the definition of LD preload.

37:25.380 --> 37:27.280
LD debug equals libs.

37:27.340 --> 37:31.040
LD debug output equals etsy cron D hacks.

37:31.620 --> 37:37.540
And lastly we'll define the escape variable which is just garbage but it's required in order to cleanly escape.

37:37.540 --> 37:44.380
So we see plugin value start, NOP and then that initially injected plugin value end to close things out in a clean manner.

37:45.620 --> 37:58.160
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.

37:58.160 --> 38:03.700
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.

38:06.240 --> 38:06.800
All right.

38:06.800 --> 38:09.880
That brings us to the exploit presentation where you'll be able to see it in action.

38:09.880 --> 38:10.960
Let me pull up the video.

38:17.550 --> 38:18.350
All right.

38:20.730 --> 38:21.190
Okay.

38:21.190 --> 38:27.950
So we'll start a listener with netcat on port 4444 and we'll launch our exploit against the web service.

38:28.690 --> 38:34.110
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.

38:34.110 --> 38:38.090
It will inject environment variables for that root owned process execution.

38:39.130 --> 38:45.190
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.

38:45.570 --> 38:53.330
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.

38:53.330 --> 38:54.370
So pretty exciting.

38:57.070 --> 38:58.470
Thank you so much.

38:58.530 --> 39:04.790
And then we also touched the rapid 7 RCE file just for style.

39:06.410 --> 39:06.990
All right.

39:06.990 --> 39:08.350
Let me pull this back up.

39:10.190 --> 39:10.990
Great.

39:11.130 --> 39:12.750
That brings us to the end of the talk.

39:12.750 --> 39:19.950
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.

39:19.950 --> 39:22.930
Without these people, it would not have been possible to do this talk.

39:22.930 --> 39:27.650
Huge shout out also to the people that helped out after this slide was finalized.

39:27.650 --> 39:29.030
The team at rapid 7.

39:29.030 --> 39:30.650
Huge thank you to all of them.

39:30.970 --> 39:33.030
And lastly, thank you to all of you for attending.

39:33.030 --> 39:35.670
I really appreciate it and you can find my info there.

39:35.670 --> 39:36.930
Have a great rest of your day.

