WEBVTT

00:00.000 --> 00:01.780
Okay, so let's get started.

00:06.820 --> 00:09.680
Okay, so hi, I'm Tal Peleg.

00:09.840 --> 00:13.600
I'm a security research team lead at Varonis.

00:14.700 --> 00:19.860
I specialize on security currently, so AWS Azure, things like that.

00:21.240 --> 00:25.060
And I really like breaking things and fixing them again.

00:25.060 --> 00:28.020
I like making music and astrophysics.

00:28.520 --> 00:32.700
And I'm Colby Abrams, so I work with Tal at Varonis.

00:33.100 --> 00:36.280
As a cloud security researcher as well, I also do IS research.

00:36.600 --> 00:42.240
And I'm really passionate about teaching, so I like teaching a lot, and specifically teaching cybersecurity.

00:43.060 --> 00:44.620
So yeah, that's us.

00:45.960 --> 00:48.440
Let's talk about what's about to happen, what we're going to talk about.

00:48.440 --> 00:54.540
So we're going to start by talking just a little bit about how the vulnerability that we found works.

00:54.780 --> 00:56.680
Just kind of the basic details.

00:56.700 --> 01:05.860
And then later on in the talk, we're going to kind of talk a little bit more about how we found it, what our methodology was, what we found was interesting.

01:06.600 --> 01:13.860
And of course, we're going to share with you a little bit about what we found out along the way, and what we think needs to be done now.

01:16.040 --> 01:22.340
If you want to take three things from this talk, first, we found a cool RC on Postgres.

01:22.440 --> 01:24.480
We even ran it a bit on the cloud.

01:25.720 --> 01:35.120
Two, you should be looking for RCs everywhere, not only in workloads, which are popular now, but any other managed service is now made of so many microservices.

01:35.180 --> 01:36.820
Something could go wrong.

01:37.220 --> 01:43.680
And three, collaborate before you're running a remote RC during vulnerability research.

01:45.680 --> 01:50.560
Okay, so before we start, we're also going to do a little bit of background on Postgres.

01:50.560 --> 01:52.880
So Postgres is a relational database.

01:52.880 --> 01:54.500
It's open source.

01:54.500 --> 01:55.900
It's extendable.

01:56.720 --> 01:58.400
It's a really strong database.

01:58.480 --> 02:00.980
It's really versatile.

02:01.220 --> 02:10.800
And because of these reasons, a lot of cloud providers have, or I think almost all the cloud providers have, an instance of managed Postgres, which is why we were kind of looking at it.

02:10.800 --> 02:13.260
So that's just a little bit about Postgres.

02:14.640 --> 02:17.100
So this story has two parts.

02:17.220 --> 02:24.280
The first one is using Perl to modify environment variables of a Postgres session process.

02:25.900 --> 02:26.420
So...

02:28.760 --> 02:30.180
This isn't working.

02:31.220 --> 02:33.120
But Postgres has extensions.

02:33.120 --> 02:36.480
They allow adding functionality to the database.

02:37.380 --> 02:48.600
And one of these types of extensions are language extensions, which allow writing custom functions in programming languages such as SQL, C, Python, Rust, and Perl.

02:48.880 --> 02:50.820
And there are two types of languages.

02:50.820 --> 02:53.300
There are trusted languages and untrusted languages.

02:53.360 --> 02:57.540
So untrusted languages have access to the system.

02:57.540 --> 03:00.100
They can access the environment variables.

03:00.100 --> 03:03.980
They can access files, open sockets, create processes.

03:03.980 --> 03:07.320
And only a super user can run them for that reason.

03:07.740 --> 03:11.900
And trusted languages have a limited access to the system.

03:12.680 --> 03:19.100
Normal users that are not super users can only run trusted language functions.

03:19.280 --> 03:29.540
What we were able to do is break that assumption by using the trusted Perl language, PL Perl, in order to change environment variables.

03:34.850 --> 03:38.830
Something that should only be done by super users.

03:43.870 --> 03:46.030
How does that work?

03:46.690 --> 03:56.770
Perl has a magic hash map, a magic variable, the env hash map, and it allows managing environment variables.

03:56.770 --> 04:01.830
So you can use it in order to read and write environment variables for the current process.

04:01.830 --> 04:11.330
And we assume that something that's embedded so much inside of the Perl language wouldn't be overridden by PL Perl, and we were right.

04:11.490 --> 04:23.710
So we can use a function like the next one, like this, in order to modify the path environment variable of our current session process.

04:26.140 --> 04:30.600
Okay, so at this point, we have a pretty strong primitive, right?

04:30.600 --> 04:34.860
We can change environment variables, and usually we can leverage that into code execution.

04:35.320 --> 04:44.020
The way we leveraged it this time into code execution is using another extension called PL Rust.

04:45.880 --> 04:48.560
So, a little bit about Rust.

04:48.560 --> 04:58.060
Rust is a compiled language, and what happens when you create a PL Rust function is that you're calling cargo.

04:58.060 --> 05:10.340
Cargo is the package manager for Rust, so cargo is going to download the different packages that you need and kind of set up the environment, and then it's going to call Rust C, which is the compiler for Rust.

05:10.800 --> 05:18.700
So again, we're going to be running cargo when we create the PL Rust function, and then cargo is going to call Rust C.

05:19.300 --> 05:24.260
And this is important because we can change environment variables, right?

05:24.260 --> 05:30.940
So, we can kind of control this process using environment variables.

05:30.940 --> 05:36.260
PL Rust does try to change and kind of sanitise the environment before running.

05:36.260 --> 05:39.120
They were kind of aware of this, but you can see it's a denialist.

05:39.120 --> 05:48.180
So , we have various different environment variables that are moved just before we run cargo.

05:48.800 --> 05:53.260
Now, one of those environment variables is called Rusty Wrapper, right?

05:53.260 --> 06:03.540
So, Rusty Wrapper, what Rusty Wrapper does is, basically, instead of calling Rust C, once you've set Rusty Wrapper, cargo is going to call a different binary.

06:03.980 --> 06:07.260
Usually, it's meant to be a bash script that kind of...

06:08.380 --> 06:16.000
well, actually, it's supposed to be a wrapper for Rust C, as the name hints.

06:16.780 --> 06:32.480
So, they did remove that from the environment, and that would have been really nice for us because we can change the binary that's being run, but what they didn't remove is CargoBuildRustyWrapper, which is a similar environment variable that has the exact same effect.

06:32.480 --> 06:38.920
So, we were able to use CargoBuildRustyWrapper to change the binary that's being run when cargo is calling Rusty.

06:39.200 --> 06:48.340
Now, we can only change the binary, and with Postgres, we don't have a lot of control over what's happening, so we can't really change anything else.

06:48.340 --> 07:00.980
We can just run a binary, and we needed to find something that would be able to run kind of like a script that we want to run, or some kind of injection where we could inject shellcodes.

07:01.040 --> 07:02.660
So, we have the RustGDB script.

07:02.660 --> 07:12.520
It's going to be a script that's installed with Rust, and what it basically does is execute the RustGDB parameter.

07:12.520 --> 07:31.300
So, another environment variable that we can set, and when we change CargoBuildRustyWrapper to actually call the RustGDB script, we're going to be able to inject bash commands into the RustGDB script with the RustGDB parameter, and we can see that right here.

07:31.300 --> 07:48.520
So, again, cargo runs Rusty, or we're going to change it with CargoBuildRustyWrapper, and at this point, we can't change the parameters that are passed, we can't inject commands, but we can change it when we run the RustGDB script, because we can change the RustGDB contents.

07:48.960 --> 07:51.900
So, let's see that in action, right?

07:55.970 --> 08:17.430
So, first, we're just going to create the extensions that we need to create in order for this to work, so we're going to create the PL Rust extension, and we're going to create the PL Perl extension, and then, right after that, we're going to create a PL Perl function that what it's going to do is it's going to edit the environment variable CargoBuildRustyWrapper to point to RustGDB,

08:17.430 --> 08:31.390
and then, after that, we can change the RustGDB parameter to kind of contain the Bash script, the Bash commands that we want to run, and then we're just going to create a PL Rust function.

08:31.390 --> 08:36.290
We don't actually need to do anything in that function, we just need PL Rust to compile the function.

08:36.810 --> 08:53.620
So, once we've done that, we can run this Perl function, and as you'll see in a moment, at first, it looks like some kind of error, but we do get the STD out, and we have successfully run code on a Postgres instance.

09:02.430 --> 09:04.070
Okay, so what's going on?

09:04.070 --> 09:06.210
Well, you probably have some questions.

09:06.550 --> 09:08.430
How did we find this vulnerability?

09:09.030 --> 09:12.390
Why did we choose PL Perl and Rust?

09:12.730 --> 09:15.470
And also, we promised you a Cloud RC.

09:15.550 --> 09:26.150
So, the idea started after I disclosed a very impactful SQL injection on a database that was hosted on AWS RDS.

09:26.150 --> 09:32.910
And I wanted to see what impact a real malicious actor could have on Postgres in RDS.

09:33.430 --> 09:43.750
So, when you create a database in RDS, Amazon gives you an admin user, and while it can manage the schema and data, it's not a super user.

09:43.750 --> 09:52.650
So, we wanted to find a privileged escalation to gain super user access, or even better, try to run code on the underlying system.

09:52.830 --> 09:58.710
So, now we have our objective, running shell commands on Postgres as a non-super user.

09:59.550 --> 10:13.230
And this is especially interesting in the Cloud, since breaking that barrier of trust between the user and the Cloud provider, and running in areas that are under the Cloud provider's responsibility, may potentially allow exfiltrating Cloud credentials,

10:13.750 --> 10:15.890
or even moving laterally between Cloud customers.

10:15.890 --> 10:17.770
So, we were looking for something like that.

10:20.170 --> 10:24.050
So, extensions are a common area to look for vulnerabilities.

10:24.450 --> 10:28.090
And we chose Perl, since it's part of the official Postgres repo.

10:28.170 --> 10:30.530
So, it comes built in with many installations.

10:30.530 --> 10:32.370
That means that it's a lot of impact.

10:33.830 --> 10:37.830
Also, Perl is from the 80s, so there's a lot of code written in it.

10:38.190 --> 10:42.370
And the 80s, security was much less common.

10:42.370 --> 10:45.330
There was much less awareness.

10:46.050 --> 10:52.790
And also, Perl has these built-in magic variables, and it's open source, so it's easier to look at.

10:53.150 --> 10:57.710
So, now we have the option to edit environment variables with Postgres.

10:57.710 --> 10:58.710
So, now what?

11:00.490 --> 11:06.390
So, I mentioned that usually it's pretty easy to run code when you're changing environment variables.

11:07.490 --> 11:17.790
Postgres actually creates some difficulties for us in this aspect, because there is an API to create background workers within Postgres.

11:17.890 --> 11:30.790
So, extensions, when they're creating another process, or doing anything like that, they're supposed to use the API that Postgres provides, which means that we're not creating the process directly.

11:30.790 --> 11:39.570
And if we're not creating the process directly, it's not going to inherit the environment variables that we edited, which means that we can't actually execute code this way.

11:41.890 --> 11:50.510
So, what we needed to find was we needed to find an extension, or something in Postgres that was going to run code, or run a binary directly without using this API.

11:50.890 --> 12:04.770
And for this, we kind of used GitHub search and Copilot to kind of look through the open source extensions that exist, and find somewhere in the code where that's happening, and try to cause that to happen as a non-superuser.

12:04.950 --> 12:10.910
So, what we looked for was, you know, the system function, or the execv family of functions.

12:11.210 --> 12:22.170
And in Rust, we looked for the command function that creates new commands, and that's how we found out that Rust is actually running cargo directly.

12:26.340 --> 12:29.960
So, we promised CloudRCE.

12:29.980 --> 12:32.640
We did briefly run code on RDS, right?

12:32.640 --> 12:40.100
We were able to execute this on RDS, because they have both PLPerl and PLRust extensions available.

12:40.680 --> 12:42.840
Why would we attack our own cloud database?

12:42.840 --> 12:58.480
So, like we said earlier, we were interested to see if we could leverage it to access network, or if we could, you know, get in cross-tenant access, or see if there were any interesting tokens that we could have stolen.

12:59.340 --> 13:00.680
And what did we find?

13:00.680 --> 13:03.340
Well, it was a really locked-down environment, right?

13:03.340 --> 13:04.580
We weren't able to do a lot of things.

13:04.580 --> 13:06.380
We weren't able to access network.

13:06.380 --> 13:12.540
And the other thing that we found is that the AWS incident response team is really good.

13:12.540 --> 13:30.460
So, within a very short time of us running code, we had, like, emails from our CEO and emails from AWS, and they found me on LinkedIn, and we had all different kinds of, like, people asking us what we were doing.

13:30.600 --> 13:35.100
And they were also able to shut down our database just in case we were a threat actor.

13:35.100 --> 13:38.660
So, really good response from them.

13:40.020 --> 13:44.880
Talk a little about how you ran code on RDS.

13:45.480 --> 13:46.040
So, yeah.

13:46.040 --> 13:51.740
So, we did run into some difficulties because of the limited environment.

13:52.200 --> 14:01.920
There were binaries that, like, the Rust GDB script needed that it didn't have, so it wouldn't run and get to the point where it was running the Rust GDB parameter.

14:02.140 --> 14:07.840
So, we had to find, like, different ways to kind of play with it a little bit more.

14:07.840 --> 14:16.760
So , we had to use the bash-env parameter, which is a really interesting environment parameter that bash actually expands before bash starts.

14:16.760 --> 14:19.280
So, that was really interesting.

14:19.700 --> 14:24.940
And so, we kind of had to tweak it a little bit just because this environment was so, so limited.

14:28.010 --> 14:28.710
All right.

14:28.710 --> 14:30.930
So, we can't leave you without any takeaways.

14:31.310 --> 14:36.790
So, first of all, if you're managing a database, keep your database up to date.

14:37.050 --> 14:38.990
At least the minor version.

14:39.010 --> 14:44.050
So , Postgres patched all the versions since version 12.

14:44.730 --> 14:48.250
If you're using an older version than that, I don't know.

14:49.210 --> 14:52.110
It's not supported anymore.

14:52.670 --> 14:53.670
Do lease privileges.

14:53.670 --> 14:59.530
So, if you don't need users to create functions or to install extensions, don't give them that access.

14:59.790 --> 15:07.030
And limit the extensions that people can load by using the allowed extensions configuration parameter.

15:07.050 --> 15:09.050
Don't allow extensions that you're not using.

15:09.050 --> 15:13.910
So, if you're not using Perl or you're not using Rust, don't allow them, obviously.

15:16.510 --> 15:26.730
So, for cloud providers, I think, and for the community, these kind of vulnerabilities aren't rare.

15:26.870 --> 15:33.650
There's a Speckle umbrella story by Imran that you should look up that's really interesting where he did something similar on GCP.

15:33.810 --> 15:42.070
We also recently found a vulnerability in Azure Cosmos for Postgres that also allows us to run code on the backend.

15:42.290 --> 15:46.150
So, these vulnerabilities, they exist and they're not going away.

15:46.630 --> 15:48.250
So, what can we do about that?

15:48.250 --> 15:52.130
I think it's really important, right, to have community research.

15:52.130 --> 16:03.010
And in this case, it's important to have responsible community research because you're accessing systems and accessing, you know, proprietary code, maybe.

16:03.110 --> 16:23.090
But I really think it's important to create a framework where we can have this kind of research to make sure that even if somebody executes one of these vulnerabilities, we're able to test that they're not able to access network resources and not able to get cross-tenant access.

16:23.590 --> 16:26.210
And I think that that's something that's really important.

16:26.270 --> 16:31.250
And , you know, some cloud providers do allow this in some way or another.

16:31.250 --> 16:33.770
But I think it should become more prevalent.

16:35.730 --> 16:40.470
Finally, some tips for ourselves, if you're a security researcher.

16:41.870 --> 16:48.850
First of all, environment variables are fun, so you should look for custom environment variables that might allow you to run code.

16:50.110 --> 16:52.590
Prepare before running code on sensitive systems.

16:52.590 --> 16:56.370
So, collaborate with a cloud provider if you're about to enter a cloud-managed territory.

16:57.410 --> 16:59.250
Remember to take pictures along the way.

16:59.250 --> 17:01.670
We forgot to take pictures of most of the exploit.

17:03.030 --> 17:05.870
Do give your database a good name.

17:05.870 --> 17:15.010
So, our database was called Kobe, and that helped AWS contact him via LinkedIn when they thought that our AWS account was being attacked.

17:15.670 --> 17:19.710
They also thought that we were researchers since it was research something.

17:20.010 --> 17:21.510
And also, collaborate.

17:21.510 --> 17:26.350
So, AWS, we're not too happy about having lots of unexpected alerts appearing right before the weekend.

17:28.590 --> 17:31.430
And finally, set your goals before researching.

17:31.670 --> 17:39.290
So, make an assessment of potential risks and think what an attacker would want from attacking the system.

17:40.110 --> 17:44.110
So, to summarize, the three things that you should remember.

17:44.110 --> 17:48.590
We found a cool RSC on Postgres, and it worked for a bit on the cloud.

17:48.590 --> 17:51.950
Look for vulnerabilities anywhere, not only workloads.

17:51.950 --> 17:57.530
And don't exploit on a major cloud service without telling them beforehand, just before the weekend.

17:58.950 --> 18:00.830
Thank you very much.

18:00.830 --> 18:01.670
Thank you, guys.

18:09.160 --> 18:11.120
What happens now, questions?

18:13.260 --> 18:24.960
If we have time for questions, then if someone has a question, otherwise we'll be outside.

