[00:13.610 --> 00:19.430] Yeah, hear me. Thank you guys for joining us today. It's the last day of DEF CON. Appreciate [00:19.430 --> 00:25.310] that. And thank you for joining to our talk. Your CI CD pipeline is vulnerable but it's not [00:25.310 --> 00:41.360] your fault. So without further ado, let's move on. We are not moving on. So my name is [00:41.360 --> 00:46.520] Elad, I'm a security researcher at PsyCode with seven years of experience. I'm mainly [00:46.520 --> 00:54.700] focusing on web application security and supply chain security and I'm love hacking random [00:54.700 --> 01:03.680] stuff, basically everything with an IP and I'm from PsyCode. Hi, my name is Orin. I hope the [01:03.680 --> 01:11.340] presentation will work. So I have also seven years of experience in cyber security and I'm [01:11.360 --> 01:16.200] working in cyber security. In my background I used to research Kerberos and networking but [01:16.200 --> 01:24.720] currently I focus on supply chain security on the same research team with Elad. Thank you Orin. [01:24.720 --> 01:30.520] So for our agenda today we're going to start and speak about CI CD platforms, about GitHub [01:30.520 --> 01:38.420] actions. Then we'll speak about command injections 101 and see how command injections appears and [01:38.420 --> 01:46.440] takes place in CI CD platforms. Then we'll tell the story of Buzzle, how we found a critical [01:46.440 --> 01:53.780] vulnerability, a CI CD vulnerability in a major Google owned open source project. We'll [01:53.780 --> 02:02.280] continue with the demo and then we'll go home with some take up, takeaways and a recap. So starting [02:02.280 --> 02:11.580] with GitHub actions and CI CD platforms. So what is even CI CD platforms? So they are building [02:11.580 --> 02:19.620] and testing uh tool meaning they can do all the manual actions that we as a person do. And there [02:19.620 --> 02:26.920] are events trigger meaning we have different SCMs like GitHub, GitLab and Bitbucket and they [02:26.920 --> 02:32.260] have some sort of features right? You can create a pull request, you can create an issue, [02:32.260 --> 02:40.440] you can create anything else and those events will trigger that pipeline. So there are many, [02:40.440 --> 02:47.020] many CI CD platforms. There are GitLab CI, GitHub actions, Jenkins and today we are going to [02:47.020 --> 02:54.660] speak about GitHub actions. And why we chose GitHub actions? There are so many Jenkins and [02:54.660 --> 03:04.080] others because GitHub actions have four million public pipelines around the ecosystem. That is [03:04.080 --> 03:13.020] huge and it's appealing for attack for in a huge attack vectors for attackers. So let's a little [03:13.020 --> 03:19.240] bit visualize this, right? So we have all the events in the left side of the screen. We can [03:19.240 --> 03:24.300] create a new commit to the repository, we can create a new issue, we can create a new pull [03:24.300 --> 03:31.920] request. Then we have the pipeline that will be triggered based on that, on those events. Then we [03:31.920 --> 03:39.840] can create any, any uh action that we wanna do. Meaning we can create a task that is logging in [03:39.840 --> 03:46.300] to our Google Cloud. We can create a task that builds a Docker image and push that to the [03:46.300 --> 03:54.280] registry. So basically we can do anything that we, that we want doing that pipeline one. And just [03:54.300 --> 04:01.660] to understand how the basic flow works, let's imagine that. We have a push, new push to the [04:01.660 --> 04:08.760] repository, right? New commit to the main branch, maybe a side branch, everything is okay. Based [04:08.760 --> 04:17.280] on that, the pipeline will start to run. That pipeline will log in to our cloud environment, [04:17.280 --> 04:23.860] could be any one of them, and maybe we'll create a new blob in a bucket. So pretty, pretty easy [04:23.860 --> 04:33.880] right? But how does the pipeline itself logs in to our cloud environment? Because we ourselves need [04:33.880 --> 04:40.120] some authentication tokens, right? Access tokens. So GitHub Actions have a key store that [04:40.120 --> 04:49.240] encrypt, encrypt and save those access tokens and decrypt them one time. So all the secrets are [04:49.240 --> 04:56.380] being saved, encrypted, and during one time they are being decrypted and being, and being used. So [04:56.900 --> 05:03.560] we can have our production environment, a staging environment keys, DB access tokens, all of [05:03.560 --> 05:11.160] those inside our pipeline key store. And probably think about that. It's appealing target for an [05:11.160 --> 05:18.480] attack, for attackers. Because if an attacker will be able to control over our pipeline, it will [05:18.480 --> 05:29.580] be able to steal those tokens and do whatever he wants, right? And among those tokens, we have a [05:29.580 --> 05:39.240] special one. The GitHub token. That GitHub token spawns in every pipeline that we create and can do [05:39.240 --> 05:45.340] any action in the repository itself. It can create new permits, it can create new releases, it can [05:45.340 --> 05:50.700] create new actions, new pull requests based on these permissions. So from an attacker [05:50.700 --> 05:58.020] perspective, if he steals that token, he can do whatever the maintainer does, whatever the owner [05:58.020 --> 06:05.360] can do. That GitHub token has a variety of permissions. It can do all that I mentioned uh [06:05.360 --> 06:12.980] previously and way more. So think about that. Stealing the GitHub action, the GitHub token with [06:12.980 --> 06:19.860] privileged permissions could gain an attacker a privileged permission to the repository itself [06:20.520 --> 06:29.900] helping everybody that is using that repository and all of its end users down the line. So to [06:30.560 --> 06:37.500] understand how attackers are able to take over those pipelines, Erwin will take the stage. [06:37.500 --> 06:48.650] Thank you Elad. So after understanding the basics of pipelines, what permissions they have and [06:48.650 --> 06:55.990] how do they run, now we can dive into the more interesting part of how command injections come [06:55.990 --> 07:04.550] into a form in CICD pipelines. So just for example I have this PHP code. It's not related to [07:04.550 --> 07:11.090] pipeline yet. Raise your hand if you ever encountered this kind of vulnerability. If you [07:11.090 --> 07:18.610] ever seen it. Okay. Perfect. So about half of the people has seen this vulnerable code. And [07:18.610 --> 07:28.210] what is the problem here? The problem is simple. This website will execute a command using [07:28.210 --> 07:36.950] unsanitized user controlled input. Right? So what that means is that a user could perform a [07:36.950 --> 07:48.250] command injection attack by providing a malicious user name. Very bad. We don't want that in our [07:48.250 --> 07:56.550] pipeline. And the same logic goes for pipelines. Because if we will use in our pipeline a user [07:56.550 --> 08:04.190] controlled input in an unsanitized way, that means that an attacker could gain access to our [08:04.190 --> 08:10.930] pipeline and extract the secrets and run inside our environment and do all the malicious things [08:10.930 --> 08:20.130] Elad have described before. So we would definitely don't want this to happen. Right? To [08:20.130 --> 08:28.390] understand how GitHub pipelines injections look like, we'll first have to get familiar with GitHub [08:28.390 --> 08:35.990] workflows. So GitHub workflow is a single step of the entire pipeline. And it is a YAML [08:35.990 --> 08:43.270] configuration file. That's it. That's everything we have to know. To see our first example of a [08:43.270 --> 08:50.110] GitHub workflow, this GitHub workflow is called test issue. And we can see the on end [08:50.130 --> 08:57.510] tree. On end tree means on what triggers will this pipeline be executed. So since it is using the [08:57.510 --> 09:05.470] issues keyword, it means that any issue related event on the repository will trigger the [09:05.470 --> 09:12.710] pipeline. So whether an issue is opened, assigned to someone else, closed, the pipeline will run. [09:13.430 --> 09:20.110] And here we can see a step of the pipeline. A step is a single instruction of the pipeline, [09:20.130 --> 09:28.470] and it will of course run under the same VM of the pipeline. And the run keyword means that we're [09:28.470 --> 09:37.770] simply going to use a bash command. Okay? So here we can see echo, which is print. And it will print [09:37.770 --> 09:45.610] the issues title. Okay? That's still basics. And what will happen is before the pipeline will run, [09:45.610 --> 09:52.770] GitHub will replace that placeholder, the GitHub event issue title, with the actual issues title. [09:53.910 --> 10:03.230] So for example, if someone has created this is a new issue, it will create an event that will trigger [10:03.230 --> 10:10.510] our pipeline. And then in the bit logs we will see that this is a new issue was printed because the [10:10.510 --> 10:19.270] pipeline used the user controlled input to run a command. So for example, I can have that issue [10:19.270 --> 10:27.850] title or that issue title. And I can also have emojis in the issue title. I don't know if it will print [10:27.850 --> 10:40.030] them or not. But another example for totally valid issue title would be that one. It works. It [10:40.030 --> 10:48.210] works totally fine on GitHub. Because it is not their fault. And what will happen here is the same idea [10:48.210 --> 10:57.490] from what we've previously seen in the PHP code. Here the pipeline will run this command under the [10:57.490 --> 11:07.010] run execution entry. Right? And it will concatenate more commands after the echo command, which [11:07.010 --> 11:14.770] will dump the memory of the pipeline and then send all the secrets to the to an attacker controlled [11:14.770 --> 11:21.090] server. So that's an example for something you wouldn't want to have in your organization. Because [11:21.090 --> 11:28.370] then an attacker will receive your PyP token, your Docker hub token, your GCP token, your GitHub [11:28.370 --> 11:36.990] token. So basically whatever you use in your pipeline. So what is the solution? The solution is [11:38.170 --> 11:49.950] pretty simple. It is from the GitHub official documentation. You just have to first set the value of the [11:49.950 --> 11:56.630] user controlled input to an environment variable. And then use the environment variable in the [11:56.630 --> 12:03.310] command execution. Okay? That's a simple solution for bash injection in general. And it also works [12:03.310 --> 12:18.930] in pipeline. Okay. That's it. Thank you very much. I mean, well, if it were not protected, we [12:18.930 --> 12:25.490] wouldn't be here today, right? So now after understanding the basics of command injection [12:25.490 --> 12:33.290] attacks, we can now explain the new attack vector we have invented that allowed us to find [12:33.310 --> 12:42.150] thousands of vulnerabilities in open source projects. And also in the Basel project, which is a [12:42.150 --> 12:51.910] flagship repository of Google which could affect millions of end users if being compromised. So [12:51.910 --> 13:00.210] let's begin with the story of Basel. This is the same workflow as we have seen before. And it will [13:00.210 --> 13:09.030] again be triggered on issue events. And since we're taking security very seriously in our [13:09.030 --> 13:14.650] organization, we follow the official documentation and before executing the command, we have [13:14.650 --> 13:23.950] first set uh the user controlled input to an environment variable. But then we have this new step. [13:24.520 --> 13:30.110] And here we can see the users keyword. And what the users keyword means is that we are [13:30.110 --> 13:38.330] importing logic from something called costume action to our project. So now it means that I can [13:38.830 --> 13:46.150] get vulnerable code that someone else has written and that will run under my pipeline's permissions. [13:47.530 --> 13:54.250] So costume actions in a word, it is pre-built logic. It's like you can think about it like a [13:54.250 --> 14:01.630] dependency in code. Someone else has written a module, a costume action, and then to create pipelines [14:01.630 --> 14:07.850] faster, I can just import it to my project and use it and I can pass parameters to it like I do to a [14:07.850 --> 14:14.570] function. It will return something or it can be like a void costume action that will just perform [14:14.570 --> 14:23.030] something. And that's it. Perfect. And for that reason, GitHub has created the GitHub marketplace, [14:23.030 --> 14:29.430] which is like the ecosystem for pipelines and there are many open source costume actions and there are [14:29.430 --> 14:37.290] thousands over thousands and you can just import and use them and deploy faster and work better. Well, you [14:37.290 --> 14:47.730] can work better, but safer? We'll have to figure out. So let's take my workflow, for example. I've [14:47.730 --> 14:55.410] written a workflow and I'm using one costume action as a dependency. But what happens is that this [14:55.410 --> 15:02.730] costume action happens to use another dependency itself. And that costume action dependency has three [15:02.730 --> 15:08.670] more dependencies. And this has more dependencies and more dependencies. And the next thing you know, [15:09.390 --> 15:17.710] you have tons of dependencies. And what could happen if one of the dependencies brings commands [15:17.730 --> 15:26.150] vulnerability to my project? Then I'm in real trouble, right? Because that means that now by injecting [15:26.150 --> 15:36.250] code, I could affect the pipeline because everything you see here is part of my dependencies. Everything [15:36.250 --> 15:44.130] runs on my pipeline's permission and environment. So dumping the secrets from there could mean that an [15:44.130 --> 15:49.350] attacker gained control to my organization and I wouldn't even know that I have imported this [15:49.350 --> 15:58.910] vulnerability. So how would it look like from the code's perspective? Let's say we have a workflow and it [15:58.910 --> 16:09.650] is using, importing action one. Action one is using action two. And action two contains the same [16:09.650 --> 16:20.290] vulnerability from the first, from the previous chapter. So that means that now, that means that now my [16:20.290 --> 16:29.110] pipeline has command injection vulnerability even though it's not my fault. So that brought the next [16:29.110 --> 16:30.410] question. [16:33.620 --> 16:42.440] Thank you, Erwin. So it's all working fine in this demo lab and visualization, right? But we are [16:42.460 --> 16:52.680] going to look at how do we exploit this in scale? Meaning going over the UI and going over one by one [16:52.680 --> 17:00.120] and checking all those post actions and navigating to different GitHub repositories, it's not [17:00.120 --> 17:11.280] scalable, right? It won't work. So we thought about different methods. The first phase, we are going to [17:11.280 --> 17:22.520] index all the GitHub atmosphere. Meaning all the workflows, all the custom actions, everything into one [17:22.520 --> 17:32.860] place. And how are we going to do that? Using Raven. Raven will allow us, will allow us to query all [17:32.860 --> 17:41.260] GitHub atmosphere into a single DB and will query and search result and vulnerability. And we [17:41.280 --> 17:48.140] will query all the activities based on that DB. So just a quick recap about Raven, which is an [17:48.140 --> 17:55.500] open source project now. It will download all the workflows in one GitHub ecosystem into a Redis [17:55.500 --> 18:05.640] DB. Then it will index them to a graph DB. And then we will be able to query anything that we want [18:05.640 --> 18:14.440] based on that results. And that's how it looks like. So we have all the connections, all the custom [18:14.440 --> 18:21.480] actions, all the workflows, all the jobs, all the steps, everything that we mentioned in a single place. [18:21.480 --> 18:30.260] That's amazing. And just a closer look, we have all the workflows which are connected to some steps and in [18:30.260 --> 18:38.860] the end of go, they are importing a single custom action. So by finding a vulnerability inside that [18:38.860 --> 18:49.220] action, will gain us a vulnerability inside all of those workflows. Insane. Second part, querying. [18:49.220 --> 18:58.080] Querying based of those results. We queried thousands of workflows. I start waiting in GitHub [18:58.080 --> 19:07.120] atmosphere. Meaning, so it will be an important one that will affect millions probably users. We found [19:07.120 --> 19:16.060] thousands of vulnerability in different severity levels. And today, I want to talk about one of them. [19:16.520 --> 19:24.960] Bazel. Bazel is a build and test tool that is widely used among the community. It's a project owned by [19:24.960 --> 19:32.880] Google. It's a flagship by them. It's used by Google internally. And a few other companies that you [19:32.880 --> 19:43.160] probably heard of. And now, for the exploitation phase. How do we take that finding and exploit it? [19:44.320 --> 19:50.940] Navigating to the repository itself and to the workflow itself, we see that that workflow doesn't seem [19:50.940 --> 19:58.660] vulnerable to anything. Right? We saw how command injections looks like by utilizing a user input [19:58.660 --> 20:07.260] inside the workflow itself. So, we don't have nothing like that. We only have a custom action in another [20:07.260 --> 20:18.160] repository managed by other maintainers and other developers and it passes two inputs. The first one, [20:18.160 --> 20:26.980] issue title. The second one, issue body. So, probably makes you think, right? Navigating to the custom [20:26.980 --> 20:34.780] action and that's we have it. The custom action, well, vulnerable, was vulnerable to a command [20:34.780 --> 20:42.840] injection vulnerability like we saw before. But wait a minute. Not only that. Remember the GitHub token [20:42.840 --> 20:48.140] that we saw before? That workflow has a full permission to the repository. So, we have to [20:48.160 --> 20:53.160] exploit that vulnerability. Meaning, exploiting that vulnerability will gain us a full permission to the [20:53.160 --> 21:02.560] repository. Not only compromising the repository itself, but compromising everyone that are using it. [21:03.940 --> 21:12.080] Let's move to the demo, right? So, in the demo, we'll have two sides of the screen. In the left side, we'll [21:12.080 --> 21:18.140] have a repository. A vulnerable one. And in the right side, we'll have an attacker server that's [21:18.160 --> 21:27.300] waiting to get a connection and waiting to run malicious commands inside that pipeline. So, at the [21:27.300 --> 21:35.760] left side of the screen, as I said before, we have a repository. Navigating to that repository and [21:35.760 --> 21:44.080] we'll be able to see that it's pretty empty one. It doesn't have any releases, none at all. And [21:44.080 --> 21:53.860] there with me is pretty empty. Keep that in mind. Scrolling back and a maintainer saw a new issue in [21:53.860 --> 22:01.600] that repository. That issue will contain some build logs and some events on the OS machine and what it [22:01.600 --> 22:07.940] doesn't know, it has one malicious command that will compromise the entire pipeline, inject [22:07.940 --> 22:16.560] malicious code, will connect to a remote server and will execute some more commands. So, by [22:16.560 --> 22:24.660] clicking milestone, the pipeline starts to run. Running the pipeline will execute our command [22:24.660 --> 22:33.120] injection. It will bring a malicious script from the attacker server as we can see and will dump all [22:33.120 --> 22:40.240] the secrets in the memory of that pipeline and will use that to create new releases, inject [22:40.240 --> 22:49.680] malicious code to that repository and way more. After the injection was successfully completed, we [22:49.680 --> 22:55.240] are navigating back to the repository itself. And you can see we have new release. By a GitHub [22:55.240 --> 23:02.260] action user, not even our user. Scrolling down and we have that APSEC village picture right in [23:02.260 --> 23:10.920] there. And from the user perspective, it's not only will hurt the repository itself, we can see [23:10.920 --> 23:19.260] that the user will clone the latest version and will do a simple make build command. But what he [23:19.260 --> 23:24.420] doesn't know that in the moment he will do a make build command, all of his environment [23:24.420 --> 23:31.260] variables will be sent to a remote attacker server with along with some system information, public [23:31.720 --> 23:40.400] private keys, DB information and everything that he has in his environment. That is insane. So not [23:40.400 --> 23:48.580] only hurting the repository itself that has 22,000 stars, it will hurt millions of end users down [23:48.580 --> 24:00.760] the line. Insane. So, so some recap and takeaways. The takeaways are separated to a blue and [24:00.760 --> 24:07.940] red. So everybody can take something from that. First one, insecure input. Don't just pass [24:07.940 --> 24:15.460] insecure input just to an inline script. Like you do in code and other methods, don't use [24:15.460 --> 24:24.280] untrusted user input in untrusted ways. The second one is custom actions and particularly composite [24:24.280 --> 24:30.740] actions. They are importing logic to inside your pipeline. So, be careful about that. Don't [24:32.600 --> 24:41.780] run a vulnerable one that will compromise your entire GitHub workflow. And the last one, do I [24:41.780 --> 24:50.520] need to stress that enough? Like everything in cyber security, least privilege principle. So, make [24:50.520 --> 24:58.540] sure your workflow has the minimum privilege it needs to be for each one. So, no extra [24:58.540 --> 25:05.980] permissions and that's it. Thank you for listening to us. We have more details, thank you. [25:13.890 --> 25:21.710] We also want to say that Raven is open source and it is equipped with uh a query library from our [25:21.710 --> 25:28.930] previous two years of research, GitHub action. So, if you want to use it either for um for bad [25:28.930 --> 25:36.230] bounties, red team or for internal uh security, you are more than welcome. We will share the QR [25:36.230 --> 25:39.670] code for the GitHub repository as well. Thank you.