1 00:00:00,000 --> 00:00:01,780 Okay, so let's get started. 2 00:00:06,820 --> 00:00:09,680 Okay, so hi, I'm Tal Peleg. 3 00:00:09,840 --> 00:00:13,600 I'm a security research team lead at Varonis. 4 00:00:14,700 --> 00:00:19,860 I specialize on security currently, so AWS Azure, things like that. 5 00:00:21,240 --> 00:00:25,060 And I really like breaking things and fixing them again. 6 00:00:25,060 --> 00:00:28,020 I like making music and astrophysics. 7 00:00:28,520 --> 00:00:32,700 And I'm Colby Abrams, so I work with Tal at Varonis. 8 00:00:33,100 --> 00:00:36,280 As a cloud security researcher as well, I also do IS research. 9 00:00:36,600 --> 00:00:42,240 And I'm really passionate about teaching, so I like teaching a lot, and specifically teaching cybersecurity. 10 00:00:43,060 --> 00:00:44,620 So yeah, that's us. 11 00:00:45,960 --> 00:00:48,440 Let's talk about what's about to happen, what we're going to talk about. 12 00:00:48,440 --> 00:00:54,540 So we're going to start by talking just a little bit about how the vulnerability that we found works. 13 00:00:54,780 --> 00:00:56,680 Just kind of the basic details. 14 00:00:56,700 --> 00: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. 15 00:01:06,600 --> 00: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. 16 00:01:16,040 --> 00:01:22,340 If you want to take three things from this talk, first, we found a cool RC on Postgres. 17 00:01:22,440 --> 00:01:24,480 We even ran it a bit on the cloud. 18 00:01:25,720 --> 00: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. 19 00:01:35,180 --> 00:01:36,820 Something could go wrong. 20 00:01:37,220 --> 00:01:43,680 And three, collaborate before you're running a remote RC during vulnerability research. 21 00:01:45,680 --> 00:01:50,560 Okay, so before we start, we're also going to do a little bit of background on Postgres. 22 00:01:50,560 --> 00:01:52,880 So Postgres is a relational database. 23 00:01:52,880 --> 00:01:54,500 It's open source. 24 00:01:54,500 --> 00:01:55,900 It's extendable. 25 00:01:56,720 --> 00:01:58,400 It's a really strong database. 26 00:01:58,480 --> 00:02:00,980 It's really versatile. 27 00:02:01,220 --> 00: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. 28 00:02:10,800 --> 00:02:13,260 So that's just a little bit about Postgres. 29 00:02:14,640 --> 00:02:17,100 So this story has two parts. 30 00:02:17,220 --> 00:02:24,280 The first one is using Perl to modify environment variables of a Postgres session process. 31 00:02:25,900 --> 00:02:26,420 So... 32 00:02:28,760 --> 00:02:30,180 This isn't working. 33 00:02:31,220 --> 00:02:33,120 But Postgres has extensions. 34 00:02:33,120 --> 00:02:36,480 They allow adding functionality to the database. 35 00:02:37,380 --> 00: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. 36 00:02:48,880 --> 00:02:50,820 And there are two types of languages. 37 00:02:50,820 --> 00:02:53,300 There are trusted languages and untrusted languages. 38 00:02:53,360 --> 00:02:57,540 So untrusted languages have access to the system. 39 00:02:57,540 --> 00:03:00,100 They can access the environment variables. 40 00:03:00,100 --> 00:03:03,980 They can access files, open sockets, create processes. 41 00:03:03,980 --> 00:03:07,320 And only a super user can run them for that reason. 42 00:03:07,740 --> 00:03:11,900 And trusted languages have a limited access to the system. 43 00:03:12,680 --> 00:03:19,100 Normal users that are not super users can only run trusted language functions. 44 00:03:19,280 --> 00: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. 45 00:03:34,850 --> 00:03:38,830 Something that should only be done by super users. 46 00:03:43,870 --> 00:03:46,030 How does that work? 47 00:03:46,690 --> 00:03:56,770 Perl has a magic hash map, a magic variable, the env hash map, and it allows managing environment variables. 48 00:03:56,770 --> 00:04:01,830 So you can use it in order to read and write environment variables for the current process. 49 00:04:01,830 --> 00: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. 50 00:04:11,490 --> 00: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. 51 00:04:26,140 --> 00:04:30,600 Okay, so at this point, we have a pretty strong primitive, right? 52 00:04:30,600 --> 00:04:34,860 We can change environment variables, and usually we can leverage that into code execution. 53 00:04:35,320 --> 00:04:44,020 The way we leveraged it this time into code execution is using another extension called PL Rust. 54 00:04:45,880 --> 00:04:48,560 So, a little bit about Rust. 55 00:04:48,560 --> 00:04:58,060 Rust is a compiled language, and what happens when you create a PL Rust function is that you're calling cargo. 56 00:04:58,060 --> 00: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. 57 00:05:10,800 --> 00: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. 58 00:05:19,300 --> 00:05:24,260 And this is important because we can change environment variables, right? 59 00:05:24,260 --> 00:05:30,940 So, we can kind of control this process using environment variables. 60 00:05:30,940 --> 00:05:36,260 PL Rust does try to change and kind of sanitise the environment before running. 61 00:05:36,260 --> 00:05:39,120 They were kind of aware of this, but you can see it's a denialist. 62 00:05:39,120 --> 00:05:48,180 So , we have various different environment variables that are moved just before we run cargo. 63 00:05:48,800 --> 00:05:53,260 Now, one of those environment variables is called Rusty Wrapper, right? 64 00:05:53,260 --> 00: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. 65 00:06:03,980 --> 00:06:07,260 Usually, it's meant to be a bash script that kind of... 66 00:06:08,380 --> 00:06:16,000 well, actually, it's supposed to be a wrapper for Rust C, as the name hints. 67 00:06:16,780 --> 00: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. 68 00:06:32,480 --> 00:06:38,920 So, we were able to use CargoBuildRustyWrapper to change the binary that's being run when cargo is calling Rusty. 69 00:06:39,200 --> 00: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. 70 00:06:48,340 --> 00: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. 71 00:07:01,040 --> 00:07:02,660 So, we have the RustGDB script. 72 00:07:02,660 --> 00: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. 73 00:07:12,520 --> 00: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. 74 00:07:31,300 --> 00: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. 75 00:07:48,960 --> 00:07:51,900 So, let's see that in action, right? 76 00:07:55,970 --> 00: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, 77 00:08:17,430 --> 00: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. 78 00:08:31,390 --> 00:08:36,290 We don't actually need to do anything in that function, we just need PL Rust to compile the function. 79 00:08:36,810 --> 00: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. 80 00:09:02,430 --> 00:09:04,070 Okay, so what's going on? 81 00:09:04,070 --> 00:09:06,210 Well, you probably have some questions. 82 00:09:06,550 --> 00:09:08,430 How did we find this vulnerability? 83 00:09:09,030 --> 00:09:12,390 Why did we choose PL Perl and Rust? 84 00:09:12,730 --> 00:09:15,470 And also, we promised you a Cloud RC. 85 00:09:15,550 --> 00:09:26,150 So, the idea started after I disclosed a very impactful SQL injection on a database that was hosted on AWS RDS. 86 00:09:26,150 --> 00:09:32,910 And I wanted to see what impact a real malicious actor could have on Postgres in RDS. 87 00:09:33,430 --> 00: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. 88 00:09:43,750 --> 00: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. 89 00:09:52,830 --> 00:09:58,710 So, now we have our objective, running shell commands on Postgres as a non-super user. 90 00:09:59,550 --> 00: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, 91 00:10:13,750 --> 00:10:15,890 or even moving laterally between Cloud customers. 92 00:10:15,890 --> 00:10:17,770 So, we were looking for something like that. 93 00:10:20,170 --> 00:10:24,050 So, extensions are a common area to look for vulnerabilities. 94 00:10:24,450 --> 00:10:28,090 And we chose Perl, since it's part of the official Postgres repo. 95 00:10:28,170 --> 00:10:30,530 So, it comes built in with many installations. 96 00:10:30,530 --> 00:10:32,370 That means that it's a lot of impact. 97 00:10:33,830 --> 00:10:37,830 Also, Perl is from the 80s, so there's a lot of code written in it. 98 00:10:38,190 --> 00:10:42,370 And the 80s, security was much less common. 99 00:10:42,370 --> 00:10:45,330 There was much less awareness. 100 00:10:46,050 --> 00:10:52,790 And also, Perl has these built-in magic variables, and it's open source, so it's easier to look at. 101 00:10:53,150 --> 00:10:57,710 So, now we have the option to edit environment variables with Postgres. 102 00:10:57,710 --> 00:10:58,710 So, now what? 103 00:11:00,490 --> 00:11:06,390 So, I mentioned that usually it's pretty easy to run code when you're changing environment variables. 104 00:11:07,490 --> 00:11:17,790 Postgres actually creates some difficulties for us in this aspect, because there is an API to create background workers within Postgres. 105 00:11:17,890 --> 00: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. 106 00:11:30,790 --> 00: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. 107 00:11:41,890 --> 00: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. 108 00:11:50,890 --> 00: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. 109 00:12:04,950 --> 00:12:10,910 So, what we looked for was, you know, the system function, or the execv family of functions. 110 00:12:11,210 --> 00: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. 111 00:12:26,340 --> 00:12:29,960 So, we promised CloudRCE. 112 00:12:29,980 --> 00:12:32,640 We did briefly run code on RDS, right? 113 00:12:32,640 --> 00:12:40,100 We were able to execute this on RDS, because they have both PLPerl and PLRust extensions available. 114 00:12:40,680 --> 00:12:42,840 Why would we attack our own cloud database? 115 00:12:42,840 --> 00: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. 116 00:12:59,340 --> 00:13:00,680 And what did we find? 117 00:13:00,680 --> 00:13:03,340 Well, it was a really locked-down environment, right? 118 00:13:03,340 --> 00:13:04,580 We weren't able to do a lot of things. 119 00:13:04,580 --> 00:13:06,380 We weren't able to access network. 120 00:13:06,380 --> 00:13:12,540 And the other thing that we found is that the AWS incident response team is really good. 121 00:13:12,540 --> 00: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. 122 00:13:30,600 --> 00:13:35,100 And they were also able to shut down our database just in case we were a threat actor. 123 00:13:35,100 --> 00:13:38,660 So, really good response from them. 124 00:13:40,020 --> 00:13:44,880 Talk a little about how you ran code on RDS. 125 00:13:45,480 --> 00:13:46,040 So, yeah. 126 00:13:46,040 --> 00:13:51,740 So, we did run into some difficulties because of the limited environment. 127 00:13:52,200 --> 00: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. 128 00:14:02,140 --> 00:14:07,840 So, we had to find, like, different ways to kind of play with it a little bit more. 129 00:14:07,840 --> 00: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. 130 00:14:16,760 --> 00:14:19,280 So, that was really interesting. 131 00:14:19,700 --> 00:14:24,940 And so, we kind of had to tweak it a little bit just because this environment was so, so limited. 132 00:14:28,010 --> 00:14:28,710 All right. 133 00:14:28,710 --> 00:14:30,930 So, we can't leave you without any takeaways. 134 00:14:31,310 --> 00:14:36,790 So, first of all, if you're managing a database, keep your database up to date. 135 00:14:37,050 --> 00:14:38,990 At least the minor version. 136 00:14:39,010 --> 00:14:44,050 So , Postgres patched all the versions since version 12. 137 00:14:44,730 --> 00:14:48,250 If you're using an older version than that, I don't know. 138 00:14:49,210 --> 00:14:52,110 It's not supported anymore. 139 00:14:52,670 --> 00:14:53,670 Do lease privileges. 140 00:14:53,670 --> 00:14:59,530 So, if you don't need users to create functions or to install extensions, don't give them that access. 141 00:14:59,790 --> 00:15:07,030 And limit the extensions that people can load by using the allowed extensions configuration parameter. 142 00:15:07,050 --> 00:15:09,050 Don't allow extensions that you're not using. 143 00:15:09,050 --> 00:15:13,910 So, if you're not using Perl or you're not using Rust, don't allow them, obviously. 144 00:15:16,510 --> 00:15:26,730 So, for cloud providers, I think, and for the community, these kind of vulnerabilities aren't rare. 145 00:15:26,870 --> 00: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. 146 00:15:33,810 --> 00:15:42,070 We also recently found a vulnerability in Azure Cosmos for Postgres that also allows us to run code on the backend. 147 00:15:42,290 --> 00:15:46,150 So, these vulnerabilities, they exist and they're not going away. 148 00:15:46,630 --> 00:15:48,250 So, what can we do about that? 149 00:15:48,250 --> 00:15:52,130 I think it's really important, right, to have community research. 150 00:15:52,130 --> 00: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. 151 00:16:03,110 --> 00: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. 152 00:16:23,590 --> 00:16:26,210 And I think that that's something that's really important. 153 00:16:26,270 --> 00:16:31,250 And , you know, some cloud providers do allow this in some way or another. 154 00:16:31,250 --> 00:16:33,770 But I think it should become more prevalent. 155 00:16:35,730 --> 00:16:40,470 Finally, some tips for ourselves, if you're a security researcher. 156 00:16:41,870 --> 00: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. 157 00:16:50,110 --> 00:16:52,590 Prepare before running code on sensitive systems. 158 00:16:52,590 --> 00:16:56,370 So, collaborate with a cloud provider if you're about to enter a cloud-managed territory. 159 00:16:57,410 --> 00:16:59,250 Remember to take pictures along the way. 160 00:16:59,250 --> 00:17:01,670 We forgot to take pictures of most of the exploit. 161 00:17:03,030 --> 00:17:05,870 Do give your database a good name. 162 00:17:05,870 --> 00: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. 163 00:17:15,670 --> 00:17:19,710 They also thought that we were researchers since it was research something. 164 00:17:20,010 --> 00:17:21,510 And also, collaborate. 165 00:17:21,510 --> 00:17:26,350 So, AWS, we're not too happy about having lots of unexpected alerts appearing right before the weekend. 166 00:17:28,590 --> 00:17:31,430 And finally, set your goals before researching. 167 00:17:31,670 --> 00:17:39,290 So, make an assessment of potential risks and think what an attacker would want from attacking the system. 168 00:17:40,110 --> 00:17:44,110 So, to summarize, the three things that you should remember. 169 00:17:44,110 --> 00:17:48,590 We found a cool RSC on Postgres, and it worked for a bit on the cloud. 170 00:17:48,590 --> 00:17:51,950 Look for vulnerabilities anywhere, not only workloads. 171 00:17:51,950 --> 00:17:57,530 And don't exploit on a major cloud service without telling them beforehand, just before the weekend. 172 00:17:58,950 --> 00:18:00,830 Thank you very much. 173 00:18:00,830 --> 00:18:01,670 Thank you, guys. 174 00:18:09,160 --> 00:18:11,120 What happens now, questions? 175 00:18:13,260 --> 00:18:24,960 If we have time for questions, then if someone has a question, otherwise we'll be outside.