start	end	text
0	1780	Okay, so let's get started.
6820	9680	Okay, so hi, I'm Tal Peleg.
9840	13600	I'm a security research team lead at Varonis.
14700	19860	I specialize on security currently, so AWS Azure, things like that.
21240	25060	And I really like breaking things and fixing them again.
25060	28020	I like making music and astrophysics.
28520	32700	And I'm Colby Abrams, so I work with Tal at Varonis.
33100	36280	As a cloud security researcher as well, I also do IS research.
36600	42240	And I'm really passionate about teaching, so I like teaching a lot, and specifically teaching cybersecurity.
43060	44620	So yeah, that's us.
45960	48440	Let's talk about what's about to happen, what we're going to talk about.
48440	54540	So we're going to start by talking just a little bit about how the vulnerability that we found works.
54780	56680	Just kind of the basic details.
56700	65860	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.
66600	73860	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.
76040	82340	If you want to take three things from this talk, first, we found a cool RC on Postgres.
82440	84480	We even ran it a bit on the cloud.
85720	95120	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.
95180	96820	Something could go wrong.
97220	103680	And three, collaborate before you're running a remote RC during vulnerability research.
105680	110560	Okay, so before we start, we're also going to do a little bit of background on Postgres.
110560	112880	So Postgres is a relational database.
112880	114500	It's open source.
114500	115900	It's extendable.
116720	118400	It's a really strong database.
118480	120980	It's really versatile.
121220	130800	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.
130800	133260	So that's just a little bit about Postgres.
134640	137100	So this story has two parts.
137220	144280	The first one is using Perl to modify environment variables of a Postgres session process.
145900	146420	So...
148760	150180	This isn't working.
151220	153120	But Postgres has extensions.
153120	156480	They allow adding functionality to the database.
157380	168600	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.
168880	170820	And there are two types of languages.
170820	173300	There are trusted languages and untrusted languages.
173360	177540	So untrusted languages have access to the system.
177540	180100	They can access the environment variables.
180100	183980	They can access files, open sockets, create processes.
183980	187320	And only a super user can run them for that reason.
187740	191900	And trusted languages have a limited access to the system.
192680	199100	Normal users that are not super users can only run trusted language functions.
199280	209540	What we were able to do is break that assumption by using the trusted Perl language, PL Perl, in order to change environment variables.
214850	218830	Something that should only be done by super users.
223870	226030	How does that work?
226690	236770	Perl has a magic hash map, a magic variable, the env hash map, and it allows managing environment variables.
236770	241830	So you can use it in order to read and write environment variables for the current process.
241830	251330	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.
251490	263710	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.
266140	270600	Okay, so at this point, we have a pretty strong primitive, right?
270600	274860	We can change environment variables, and usually we can leverage that into code execution.
275320	284020	The way we leveraged it this time into code execution is using another extension called PL Rust.
285880	288560	So, a little bit about Rust.
288560	298060	Rust is a compiled language, and what happens when you create a PL Rust function is that you're calling cargo.
298060	310340	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.
310800	318700	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.
319300	324260	And this is important because we can change environment variables, right?
324260	330940	So, we can kind of control this process using environment variables.
330940	336260	PL Rust does try to change and kind of sanitise the environment before running.
336260	339120	They were kind of aware of this, but you can see it's a denialist.
339120	348180	So , we have various different environment variables that are moved just before we run cargo.
348800	353260	Now, one of those environment variables is called Rusty Wrapper, right?
353260	363540	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.
363980	367260	Usually, it's meant to be a bash script that kind of...
368380	376000	well, actually, it's supposed to be a wrapper for Rust C, as the name hints.
376780	392480	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.
392480	398920	So, we were able to use CargoBuildRustyWrapper to change the binary that's being run when cargo is calling Rusty.
399200	408340	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.
408340	420980	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.
421040	422660	So, we have the RustGDB script.
422660	432520	It's going to be a script that's installed with Rust, and what it basically does is execute the RustGDB parameter.
432520	451300	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.
451300	468520	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.
468960	471900	So, let's see that in action, right?
475970	497430	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,
497430	511390	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.
511390	516290	We don't actually need to do anything in that function, we just need PL Rust to compile the function.
516810	533620	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.
542430	544070	Okay, so what's going on?
544070	546210	Well, you probably have some questions.
546550	548430	How did we find this vulnerability?
549030	552390	Why did we choose PL Perl and Rust?
552730	555470	And also, we promised you a Cloud RC.
555550	566150	So, the idea started after I disclosed a very impactful SQL injection on a database that was hosted on AWS RDS.
566150	572910	And I wanted to see what impact a real malicious actor could have on Postgres in RDS.
573430	583750	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.
583750	592650	So, we wanted to find a privileged escalation to gain super user access, or even better, try to run code on the underlying system.
592830	598710	So, now we have our objective, running shell commands on Postgres as a non-super user.
599550	613230	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,
613750	615890	or even moving laterally between Cloud customers.
615890	617770	So, we were looking for something like that.
620170	624050	So, extensions are a common area to look for vulnerabilities.
624450	628090	And we chose Perl, since it's part of the official Postgres repo.
628170	630530	So, it comes built in with many installations.
630530	632370	That means that it's a lot of impact.
633830	637830	Also, Perl is from the 80s, so there's a lot of code written in it.
638190	642370	And the 80s, security was much less common.
642370	645330	There was much less awareness.
646050	652790	And also, Perl has these built-in magic variables, and it's open source, so it's easier to look at.
653150	657710	So, now we have the option to edit environment variables with Postgres.
657710	658710	So, now what?
660490	666390	So, I mentioned that usually it's pretty easy to run code when you're changing environment variables.
667490	677790	Postgres actually creates some difficulties for us in this aspect, because there is an API to create background workers within Postgres.
677890	690790	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.
690790	699570	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.
701890	710510	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.
710890	724770	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.
724950	730910	So, what we looked for was, you know, the system function, or the execv family of functions.
731210	742170	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.
746340	749960	So, we promised CloudRCE.
749980	752640	We did briefly run code on RDS, right?
752640	760100	We were able to execute this on RDS, because they have both PLPerl and PLRust extensions available.
760680	762840	Why would we attack our own cloud database?
762840	778480	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.
779340	780680	And what did we find?
780680	783340	Well, it was a really locked-down environment, right?
783340	784580	We weren't able to do a lot of things.
784580	786380	We weren't able to access network.
786380	792540	And the other thing that we found is that the AWS incident response team is really good.
792540	810460	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.
810600	815100	And they were also able to shut down our database just in case we were a threat actor.
815100	818660	So, really good response from them.
820020	824880	Talk a little about how you ran code on RDS.
825480	826040	So, yeah.
826040	831740	So, we did run into some difficulties because of the limited environment.
832200	841920	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.
842140	847840	So, we had to find, like, different ways to kind of play with it a little bit more.
847840	856760	So , we had to use the bash-env parameter, which is a really interesting environment parameter that bash actually expands before bash starts.
856760	859280	So, that was really interesting.
859700	864940	And so, we kind of had to tweak it a little bit just because this environment was so, so limited.
868010	868710	All right.
868710	870930	So, we can't leave you without any takeaways.
871310	876790	So, first of all, if you're managing a database, keep your database up to date.
877050	878990	At least the minor version.
879010	884050	So , Postgres patched all the versions since version 12.
884730	888250	If you're using an older version than that, I don't know.
889210	892110	It's not supported anymore.
892670	893670	Do lease privileges.
893670	899530	So, if you don't need users to create functions or to install extensions, don't give them that access.
899790	907030	And limit the extensions that people can load by using the allowed extensions configuration parameter.
907050	909050	Don't allow extensions that you're not using.
909050	913910	So, if you're not using Perl or you're not using Rust, don't allow them, obviously.
916510	926730	So, for cloud providers, I think, and for the community, these kind of vulnerabilities aren't rare.
926870	933650	There's a Speckle umbrella story by Imran that you should look up that's really interesting where he did something similar on GCP.
933810	942070	We also recently found a vulnerability in Azure Cosmos for Postgres that also allows us to run code on the backend.
942290	946150	So, these vulnerabilities, they exist and they're not going away.
946630	948250	So, what can we do about that?
948250	952130	I think it's really important, right, to have community research.
952130	963010	And in this case, it's important to have responsible community research because you're accessing systems and accessing, you know, proprietary code, maybe.
963110	983090	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.
983590	986210	And I think that that's something that's really important.
986270	991250	And , you know, some cloud providers do allow this in some way or another.
991250	993770	But I think it should become more prevalent.
995730	1000470	Finally, some tips for ourselves, if you're a security researcher.
1001870	1008850	First of all, environment variables are fun, so you should look for custom environment variables that might allow you to run code.
1010110	1012590	Prepare before running code on sensitive systems.
1012590	1016370	So, collaborate with a cloud provider if you're about to enter a cloud-managed territory.
1017410	1019250	Remember to take pictures along the way.
1019250	1021670	We forgot to take pictures of most of the exploit.
1023030	1025870	Do give your database a good name.
1025870	1035010	So, our database was called Kobe, and that helped AWS contact him via LinkedIn when they thought that our AWS account was being attacked.
1035670	1039710	They also thought that we were researchers since it was research something.
1040010	1041510	And also, collaborate.
1041510	1046350	So, AWS, we're not too happy about having lots of unexpected alerts appearing right before the weekend.
1048590	1051430	And finally, set your goals before researching.
1051670	1059290	So, make an assessment of potential risks and think what an attacker would want from attacking the system.
1060110	1064110	So, to summarize, the three things that you should remember.
1064110	1068590	We found a cool RSC on Postgres, and it worked for a bit on the cloud.
1068590	1071950	Look for vulnerabilities anywhere, not only workloads.
1071950	1077530	And don't exploit on a major cloud service without telling them beforehand, just before the weekend.
1078950	1080830	Thank you very much.
1080830	1081670	Thank you, guys.
1089160	1091120	What happens now, questions?
1093260	1104960	If we have time for questions, then if someone has a question, otherwise we'll be outside.
