WEBVTT

00:13.610 --> 00:15.190
Yeah, hear me.

00:15.190 --> 00:17.230
Thank you guys for joining us today.

00:17.230 --> 00:19.050
It's the last day of DEF CON.

00:19.050 --> 00:20.310
Appreciate that.

00:20.310 --> 00:22.670
And thank you for joining to our talk.

00:22.670 --> 00:26.410
Your CI CD pipeline is vulnerable but it's not your fault.

00:26.450 --> 00:29.550
So without further ado, let's move on.

00:32.800 --> 00:34.600
We are not moving on.

00:39.980 --> 00:45.720
So my name is Elad, I'm a security researcher at PsyCode with seven years of experience.

00:45.720 --> 00:59.400
I'm mainly focusing on web application security and supply chain security and I'm love hacking random stuff, basically everything with an IP and I'm from PsyCode.

01:00.900 --> 01:03.120
Hi, my name is Orin.

01:03.120 --> 01:05.260
I hope the presentation will work.

01:05.260 --> 01:12.800
So I have also seven years of experience in cyber security and I'm working in cyber security.

01:12.800 --> 01:22.220
In my background I used to research Kerberos and networking but currently I focus on supply chain security on the same research team with Elad.

01:22.980 --> 01:24.720
Thank you Orin.

01:24.720 --> 01:31.700
So for our agenda today we're going to start and speak about CI CD platforms, about GitHub actions.

01:31.720 --> 01:41.360
Then we'll speak about command injections 101 and see how command injections appears and takes place in CI CD platforms.

01:41.520 --> 01:53.260
Then we'll tell the story of Buzzle, how we found a critical vulnerability, a CI CD vulnerability in a major Google owned open source project.

01:53.400 --> 02:00.140
We'll continue with the demo and then we'll go home with some take up, takeaways and a recap.

02:00.840 --> 02:05.360
So starting with GitHub actions and CI CD platforms.

02:05.480 --> 02:09.480
So what is even CI CD platforms?

02:09.480 --> 02:18.780
So they are building and testing uh tool meaning they can do all the manual actions that we as a person do.

02:18.780 --> 02:29.480
And there are events trigger meaning we have different SCMs like GitHub, GitLab and Bitbucket and they have some sort of features right?

02:29.480 --> 02:37.880
You can create a pull request, you can create an issue, you can create anything else and those events will trigger that pipeline.

02:38.620 --> 02:42.100
So there are many, many CI CD platforms.

02:42.100 --> 02:49.520
There are GitLab CI, GitHub actions, Jenkins and today we are going to speak about GitHub actions.

02:49.860 --> 02:53.180
And why we chose GitHub actions?

02:53.180 --> 03:03.260
There are so many Jenkins and others because GitHub actions have four million public pipelines around the ecosystem.

03:03.260 --> 03:10.920
That is huge and it's appealing for attack for in a huge attack vectors for attackers.

03:11.860 --> 03:14.880
So let's a little bit visualize this, right?

03:14.880 --> 03:18.960
So we have all the events in the left side of the screen.

03:18.960 --> 03:24.940
We can create a new commit to the repository, we can create a new issue, we can create a new pull request.

03:25.040 --> 03:30.900
Then we have the pipeline that will be triggered based on that, on those events.

03:31.080 --> 03:36.460
Then we can create any, any uh action that we wanna do.

03:36.460 --> 03:41.920
Meaning we can create a task that is logging in to our Google Cloud.

03:41.920 --> 03:47.220
We can create a task that builds a Docker image and push that to the registry.

03:47.220 --> 03:52.980
So basically we can do anything that we, that we want doing that pipeline one.

03:53.720 --> 03:59.420
And just to understand how the basic flow works, let's imagine that.

03:59.840 --> 04:03.060
We have a push, new push to the repository, right?

04:03.060 --> 04:07.800
New commit to the main branch, maybe a side branch, everything is okay.

04:08.360 --> 04:12.080
Based on that, the pipeline will start to run.

04:13.400 --> 04:22.260
That pipeline will log in to our cloud environment, could be any one of them, and maybe we'll create a new blob in a bucket.

04:22.260 --> 04:24.900
So pretty, pretty easy right?

04:26.000 --> 04:31.920
But how does the pipeline itself logs in to our cloud environment?

04:31.920 --> 04:35.780
Because we ourselves need some authentication tokens, right?

04:35.780 --> 04:37.280
Access tokens.

04:37.280 --> 04:47.700
So GitHub Actions have a key store that encrypt, encrypt and save those access tokens and decrypt them one time.

04:47.700 --> 04:55.940
So all the secrets are being saved, encrypted, and during one time they are being decrypted and being, and being used.

04:56.080 --> 05:06.620
So we can have our production environment, a staging environment keys, DB access tokens, all of those inside our pipeline key store.

05:06.620 --> 05:09.460
And probably think about that.

05:09.500 --> 05:12.660
It's appealing target for an attack, for attackers.

05:12.660 --> 05:24.960
Because if an attacker will be able to control over our pipeline, it will be able to steal those tokens and do whatever he wants, right?

05:25.480 --> 05:30.660
And among those tokens, we have a special one.

05:30.900 --> 05:32.480
The GitHub token.

05:32.720 --> 05:41.600
That GitHub token spawns in every pipeline that we create and can do any action in the repository itself.

05:41.600 --> 05:49.580
It can create new permits, it can create new releases, it can create new actions, new pull requests based on these permissions.

05:49.580 --> 05:59.200
So from an attacker perspective, if he steals that token, he can do whatever the maintainer does, whatever the owner can do.

06:00.000 --> 06:03.100
That GitHub token has a variety of permissions.

06:03.300 --> 06:07.680
It can do all that I mentioned uh previously and way more.

06:07.680 --> 06:09.220
So think about that.

06:09.220 --> 06:27.600
Stealing the GitHub action, the GitHub token with privileged permissions could gain an attacker a privileged permission to the repository itself helping everybody that is using that repository and all of its end users down the line.

06:28.060 --> 06:37.500
So to understand how attackers are able to take over those pipelines, Erwin will take the stage.

06:37.500 --> 06:41.870
Thank you Elad.

06:43.610 --> 06:59.030
So after understanding the basics of pipelines, what permissions they have and how do they run, now we can dive into the more interesting part of how command injections come into a form in CICD pipelines.

06:59.770 --> 07:03.550
So just for example I have this PHP code.

07:03.550 --> 07:05.750
It's not related to pipeline yet.

07:05.750 --> 07:10.110
Raise your hand if you ever encountered this kind of vulnerability.

07:10.570 --> 07:12.410
If you ever seen it.

07:12.450 --> 07:13.390
Okay.

07:14.010 --> 07:14.750
Perfect.

07:14.750 --> 07:18.410
So about half of the people has seen this vulnerable code.

07:18.410 --> 07:20.270
And what is the problem here?

07:20.270 --> 07:23.450
The problem is simple.

07:24.090 --> 07:32.010
This website will execute a command using unsanitized user controlled input.

07:32.070 --> 07:32.950
Right?

07:33.050 --> 07:43.490
So what that means is that a user could perform a command injection attack by providing a malicious user name.

07:44.970 --> 07:46.730
Very bad.

07:46.730 --> 07:49.130
We don't want that in our pipeline .

07:49.130 --> 07:52.730
And the same logic goes for pipelines.

07:52.810 --> 08:12.730
Because if we will use in our pipeline a user controlled input in an unsanitized way, that means that an attacker could gain access to our pipeline and extract the secrets and run inside our environment and do all the malicious things Elad have described before.

08:12.730 --> 08:16.590
So we would definitely don't want this to happen.

08:16.830 --> 08:17.690
Right?

08:19.490 --> 08:29.550
To understand how GitHub pipelines injections look like, we'll first have to get familiar with GitHub workflows.

08:29.950 --> 08:34.490
So GitHub workflow is a single step of the entire pipeline.

08:34.810 --> 08:37.490
And it is a YAML configuration file.

08:37.490 --> 08:38.110
That's it.

08:38.110 --> 08:40.030
That's everything we have to know.

08:40.630 --> 08:47.270
To see our first example of a GitHub workflow, this GitHub workflow is called test issue.

08:47.810 --> 08:50.570
And we can see the on end tree.

08:50.570 --> 08:55.330
On end tree means on what triggers will this pipeline be executed.

08:55.650 --> 09:06.310
So since it is using the issues keyword, it means that any issue related event on the repository will trigger the pipeline.

09:06.310 --> 09:12.710
So whether an issue is opened, assigned to someone else, closed, the pipeline will run.

09:13.430 --> 09:16.890
And here we can see a step of the pipeline.

09:17.130 --> 09:24.990
A step is a single instruction of the pipeline, and it will of course run under the same VM of the pipeline.

09:25.430 --> 09:31.370
And the run keyword means that we're simply going to use a bash command.

09:32.150 --> 09:32.950
Okay?

09:32.950 --> 09:36.070
So here we can see echo, which is print.

09:36.070 --> 09:39.710
And it will print the issues title.

09:39.950 --> 09:40.150
Okay?

09:40.170 --> 09:42.550
That's still basics.

09:42.690 --> 09:52.770
And what will happen is before the pipeline will run, GitHub will replace that placeholder, the GitHub event issue title, with the actual issues title.

09:53.910 --> 10:04.830
So for example, if someone has created this is a new issue, it will create an event that will trigger our pipeline.

10:04.830 --> 10:14.330
And then in the bit logs we will see that this is a new issue was printed because the pipeline used the user controlled input to run a command.

10:16.510 --> 10:22.430
So for example, I can have that issue title or that issue title.

10:23.010 --> 10:26.650
And I can also have emojis in the issue title.

10:26.650 --> 10:28.970
I don't know if it will print them or not.

10:29.010 --> 10:35.470
But another example for totally valid issue title would be that one.

10:38.450 --> 10:39.750
It works.

10:39.750 --> 10:41.870
It works totally fine on GitHub.

10:41.870 --> 10:44.310
Because it is not their fault.

10:44.730 --> 10:51.610
And what will happen here is the same idea from what we've previously seen in the PHP code.

10:51.610 --> 11:00.650
Here the pipeline will run this command under the run execution entry.

11:00.710 --> 11:01.750
Right?

11:01.750 --> 11:15.930
And it will concatenate more commands after the echo command, which will dump the memory of the pipeline and then send all the secrets to the to an attacker controlled server.

11:16.030 --> 11:20.230
So that's an example for something you wouldn't want to have in your organization.

11:20.470 --> 11:29.470
Because then an attacker will receive your PyP token, your Docker hub token, your GCP token, your GitHub token.

11:29.470 --> 11:33.170
So basically whatever you use in your pipeline.

11:34.610 --> 11:36.990
So what is the solution?

11:36.990 --> 11:40.390
The solution is pretty simple.

11:40.530 --> 11:44.470
It is from the GitHub official documentation.

11:45.110 --> 11:53.150
You just have to first set the value of the user controlled input to an environment variable.

11:53.290 --> 11:58.070
And then use the environment variable in the command execution.

11:58.330 --> 11:58.930
Okay?

11:58.930 --> 12:02.370
That's a simple solution for bash injection in general.

12:02.370 --> 12:04.430
And it also works in pipeline.

12:05.530 --> 12:06.610
Okay.

12:06.610 --> 12:07.230
That's it.

12:07.230 --> 12:08.550
Thank you very much.

12:10.150 --> 12:20.410
I mean, well, if it were not protected, we wouldn't be here today, right?

12:20.410 --> 12:36.630
So now after understanding the basics of command injection attacks, we can now explain the new attack vector we have invented that allowed us to find thousands of vulnerabilities in open source projects.

12:37.030 --> 12:49.290
And also in the Basel project, which is a flagship repository of Google which could affect millions of end users if being compromised.

12:50.810 --> 12:54.030
So let's begin with the story of Basel.

12:56.210 --> 12:59.150
This is the same workflow as we have seen before.

12:59.150 --> 13:04.690
And it will again be triggered on issue events.

13:05.610 --> 13:19.450
And since we're taking security very seriously in our organization, we follow the official documentation and before executing the command, we have first set uh the user controlled input to an environment variable.

13:19.450 --> 13:23.950
But then we have this new step.

13:24.520 --> 13:27.030
And here we can see the users keyword.

13:27.030 --> 13:35.930
And what the users keyword means is that we are importing logic from something called costume action to our project.

13:36.930 --> 13:46.150
So now it means that I can get vulnerable code that someone else has written and that will run under my pipeline's permissions.

13:47.530 --> 13:52.410
So costume actions in a word, it is pre-built logic.

13:52.410 --> 13:55.870
It's like you can think about it like a dependency in code.

13:55.870 --> 14:08.070
Someone else has written a module, a costume action, and then to create pipelines faster, I can just import it to my project and use it and I can pass parameters to it like I do to a function .

14:08.070 --> 14:15.070
It will return something or it can be like a void costume action that will just perform something .

14:15.230 --> 14:15.990
And that's it.

14:15.990 --> 14:16.730
Perfect.

14:18.310 --> 14:36.090
And for that reason, GitHub has created the GitHub marketplace, which is like the ecosystem for pipelines and there are many open source costume actions and there are thousands over thousands and you can just import and use them and deploy faster and work better.

14:36.270 --> 14:39.970
Well, you can work better, but safer?

14:40.070 --> 14:43.290
We'll have to figure out.

14:44.350 --> 14:47.470
So let's take my workflow, for example.

14:47.470 --> 14:51.790
I've written a workflow and I'm using one costume action as a dependency.

14:52.650 --> 15:00.090
But what happens is that this costume action happens to use another dependency itself.

15:00.170 --> 15:04.150
And that costume action dependency has three more dependencies.

15:04.190 --> 15:07.350
And this has more dependencies and more dependencies.

15:07.350 --> 15:11.570
And the next thing you know, you have tons of dependencies.

15:11.570 --> 15:20.630
And what could happen if one of the dependencies brings commands vulnerability to my project?

15:20.630 --> 15:23.310
Then I'm in real trouble, right?

15:23.310 --> 15:36.030
Because that means that now by injecting code, I could affect the pipeline because everything you see here is part of my dependencies.

15:36.030 --> 15:40.010
Everything runs on my pipeline's permission and environment.

15:40.010 --> 15:51.470
So dumping the secrets from there could mean that an attacker gained control to my organization and I wouldn't even know that I have imported this vulnerability.

15:52.010 --> 15:55.810
So how would it look like from the code's perspective?

15:56.530 --> 16:03.250
Let's say we have a workflow and it is using, importing action one.

16:03.250 --> 16:06.890
Action one is using action two.

16:06.890 --> 16:15.790
And action two contains the same vulnerability from the first, from the previous chapter.

16:15.790 --> 16:26.130
So that means that now, that means that now my pipeline has command injection vulnerability even though it's not my fault.

16:26.810 --> 16:30.410
So that brought the next question.

16:33.620 --> 16:35.400
Thank you, Erwin.

16:35.400 --> 16:41.840
So it's all working fine in this demo lab and visualization, right?

16:41.840 --> 16:47.380
But we are going to look at how do we exploit this in scale?

16:47.480 --> 17:01.640
Meaning going over the UI and going over one by one and checking all those post actions and navigating to different GitHub repositories, it's not scalable, right?

17:01.640 --> 17:03.540
It won't work.

17:04.220 --> 17:08.460
So we thought about different methods.

17:09.340 --> 17:14.800
The first phase, we are going to index all the GitHub atmosphere.

17:14.800 --> 17:24.100
Meaning all the workflows, all the custom actions, everything into one place.

17:24.240 --> 17:26.500
And how are we going to do that?

17:26.500 --> 17:28.120
Using Raven.

17:28.380 --> 17:41.260
Raven will allow us, will allow us to query all GitHub atmosphere into a single DB and will query and search result and vulnerability.

17:41.260 --> 17:43.740
And we will query all the activities based on that DB.

17:44.260 --> 17:50.000
So just a quick recap about Raven, which is an open source project now.

17:50.020 --> 17:56.340
It will download all the workflows in one GitHub ecosystem into a Redis DB.

17:56.480 --> 18:00.240
Then it will index them to a graph DB.

18:00.240 --> 18:07.980
And then we will be able to query anything that we want based on that results.

18:08.680 --> 18:11.000
And that's how it looks like.

18:11.220 --> 18:21.480
So we have all the connections, all the custom actions, all the workflows, all the jobs, all the steps, everything that we mentioned in a single place.

18:21.480 --> 18:22.820
That's amazing.

18:23.040 --> 18:34.920
And just a closer look, we have all the workflows which are connected to some steps and in the end of go, they are importing a single custom action.

18:35.140 --> 18:45.200
So by finding a vulnerability inside that action, will gain us a vulnerability inside all of those workflows.

18:45.200 --> 18:46.320
Insane.

18:46.660 --> 18:49.220
Second part, querying.

18:49.220 --> 18:52.220
Querying based of those results.

18:53.040 --> 18:56.020
We queried thousands of workflows.

18:56.020 --> 18:59.240
I start waiting in GitHub atmosphere.

18:59.240 --> 19:05.640
Meaning, so it will be an important one that will affect millions probably users.

19:06.580 --> 19:11.420
We found thousands of vulnerability in different severity levels.

19:11.880 --> 19:16.060
And today, I want to talk about one of them.

19:16.520 --> 19:17.520
Bazel.

19:18.300 --> 19:23.240
Bazel is a build and test tool that is widely used among the community.

19:23.240 --> 19:25.500
It's a project owned by Google.

19:25.500 --> 19:28.700
It's a flagship by them.

19:28.700 --> 19:30.720
It's used by Google internally.

19:30.720 --> 19:34.420
And a few other companies that you probably heard of.

19:35.460 --> 19:38.500
And now, for the exploitation phase.

19:38.580 --> 19:43.160
How do we take that finding and exploit it?

19:44.320 --> 19:52.460
Navigating to the repository itself and to the workflow itself, we see that that workflow doesn't seem vulnerable to anything.

19:52.460 --> 19:53.000
Right?

19:53.000 --> 20:00.800
We saw how command injections looks like by utilizing a user input inside the workflow itself.

20:00.800 --> 20:03.660
So, we don't have nothing like that.

20:04.080 --> 20:16.580
We only have a custom action in another repository managed by other maintainers and other developers and it passes two inputs.

20:16.580 --> 20:19.340
The first one, issue title.

20:19.700 --> 20:21.560
The second one, issue body.

20:22.000 --> 20:25.180
So, probably makes you think, right?

20:25.640 --> 20:30.160
Navigating to the custom action and that's we have it.

20:30.960 --> 20:38.260
The custom action, well, vulnerable, was vulnerable to a command injection vulnerability like we saw before.

20:38.640 --> 20:39.940
But wait a minute.

20:39.940 --> 20:41.420
Not only that.

20:41.560 --> 20:44.300
Remember the GitHub token that we saw before?

20:44.960 --> 20:48.140
That workflow has a full permission to the repository.

20:48.140 --> 20:48.760
So, we have to exploit that vulnerability.

20:48.760 --> 20:54.220
Meaning, exploiting that vulnerability will gain us a full permission to the repository.

20:54.220 --> 21:02.560
Not only compromising the repository itself, but compromising everyone that are using it.

21:03.940 --> 21:06.000
Let's move to the demo, right?

21:06.960 --> 21:10.940
So, in the demo, we'll have two sides of the screen.

21:10.940 --> 21:13.240
In the left side, we'll have a repository.

21:13.240 --> 21:14.560
A vulnerable one.

21:14.560 --> 21:24.340
And in the right side, we'll have an attacker server that's waiting to get a connection and waiting to run malicious commands inside that pipeline.

21:26.140 --> 21:32.700
So, at the left side of the screen, as I said before, we have a repository.

21:33.380 --> 21:39.800
Navigating to that repository and we'll be able to see that it's pretty empty one.

21:39.800 --> 21:43.660
It doesn't have any releases, none at all.

21:43.660 --> 21:46.440
And there with me is pretty empty.

21:46.440 --> 21:48.460
Keep that in mind.

21:49.520 --> 21:55.340
Scrolling back and a maintainer saw a new issue in that repository.

21:55.520 --> 22:15.640
That issue will contain some build logs and some events on the OS machine and what it doesn't know, it has one malicious command that will compromise the entire pipeline, inject malicious code, will connect to a remote server and will execute some more commands.

22:15.720 --> 22:20.260
So, by clicking milestone, the pipeline starts to run.

22:20.660 --> 22:25.880
Running the pipeline will execute our command injection.

22:26.260 --> 22:45.240
It will bring a malicious script from the attacker server as we can see and will dump all the secrets in the memory of that pipeline and will use that to create new releases, inject malicious code to that repository and way more.

22:45.820 --> 22:52.500
After the injection was successfully completed, we are navigating back to the repository itself.

22:52.500 --> 22:54.580
And you can see we have new release.

22:54.580 --> 22:58.100
By a GitHub action user, not even our user.

22:58.460 --> 23:03.120
Scrolling down and we have that APSEC village picture right in there.

23:03.660 --> 23:17.500
And from the user perspective, it's not only will hurt the repository itself, we can see that the user will clone the latest version and will do a simple make build command.

23:17.960 --> 23:38.240
But what he doesn't know that in the moment he will do a make build command, all of his environment variables will be sent to a remote attacker server with along with some system information, public private keys, DB information and everything that he has in his environment.

23:38.240 --> 23:39.660
That is insane.

23:39.660 --> 23:49.840
So not only hurting the repository itself that has 22,000 stars, it will hurt millions of end users down the line.

23:50.260 --> 23:51.560
Insane.

23:52.000 --> 23:55.460
So, so some recap and takeaways.

23:56.460 --> 24:00.760
The takeaways are separated to a blue and red .

24:00.760 --> 24:03.720
So everybody can take something from that.

24:04.120 --> 24:06.800
First one, insecure input.

24:06.800 --> 24:11.840
Don't just pass insecure input just to an inline script.

24:11.840 --> 24:19.880
Like you do in code and other methods, don't use untrusted user input in untrusted ways.

24:20.140 --> 24:25.360
The second one is custom actions and particularly composite actions.

24:25.360 --> 24:29.060
They are importing logic to inside your pipeline.

24:29.060 --> 24:30.740
So, be careful about that.

24:30.740 --> 24:39.820
Don't run a vulnerable one that will compromise your entire GitHub workflow.

24:39.820 --> 24:43.420
And the last one, do I need to stress that enough?

24:43.420 --> 24:49.580
Like everything in cyber security, least privilege principle.

24:49.580 --> 24:56.840
So, make sure your workflow has the minimum privilege it needs to be for each one.

24:56.840 --> 25:02.340
So, no extra permissions and that's it.

25:02.360 --> 25:04.100
Thank you for listening to us.

25:04.100 --> 25:05.980
We have more details, thank you.

25:13.890 --> 25:24.890
We also want to say that Raven is open source and it is equipped with uh a query library from our previous two years of research, GitHub action.

25:24.890 --> 25:35.090
So, if you want to use it either for um for bad bounties, red team or for internal uh security, you are more than welcome.

25:35.090 --> 25:38.850
We will share the QR code for the GitHub repository as well.

25:38.850 --> 25:39.670
Thank you.

