1 00:00:01,900 --> 00:00:03,080 All right. 2 00:00:03,500 --> 00:00:04,800 Hello, everyone. 3 00:00:04,800 --> 00:00:07,620 All right, let's address the burning question in the room. 4 00:00:07,620 --> 00:00:13,880 No, we did not get access to the source code for Grand Theft Auto VI, nor do we know if the game will ever be released. 5 00:00:13,880 --> 00:00:16,440 Maybe in the following presentation, but not today. 6 00:00:16,860 --> 00:00:21,280 This is Grand Theft Actions abusing self-hosted GitHub runners at scale. 7 00:00:21,840 --> 00:00:23,240 Hello, everybody. 8 00:00:23,580 --> 00:00:29,640 Before we get started, we read online that as long as we have a disclaimer, nobody's allowed to sue us, so here we go. 9 00:00:29,640 --> 00:00:32,520 All vulnerabilities mentioned during this talk have been remediated. 10 00:00:32,740 --> 00:00:39,020 The views and opinions expressed are solely our own, and the content is not endorsed by, nor does it represent the views of our employers. 11 00:00:40,080 --> 00:00:40,860 All right. 12 00:00:40,920 --> 00:00:42,140 Whoa, there we go. 13 00:00:42,320 --> 00:00:44,060 Adnan, I think we're good to go. 14 00:00:46,900 --> 00:00:50,600 All right, if you're wondering about us, so my name is Adnan Khan. 15 00:00:50,600 --> 00:00:52,900 For my day job, I work as a security engineer. 16 00:00:52,900 --> 00:00:57,920 I'm also a security researcher and bug bounty hunter on the side, and you can find some of my socials below. 17 00:00:57,920 --> 00:00:59,800 My name is John Stawinski. 18 00:00:59,800 --> 00:01:01,940 I'm a red team security engineer at Praetorian. 19 00:01:02,120 --> 00:01:05,280 I also do CICD security research on the side. 20 00:01:05,320 --> 00:01:12,520 In the last year, I've watched Avatar the Last Airbender three times, the animated version, and in a past life, I was a collegiate wrestler. 21 00:01:13,900 --> 00:01:14,900 All right. 22 00:01:14,920 --> 00:01:22,040 All right, so we're going to be talking a lot about self-hosted runners today, so I want to make sure everyone has a background understanding of how GitHub runners work. 23 00:01:22,040 --> 00:01:24,360 On one side, you have GitHub-hosted runners. 24 00:01:24,360 --> 00:01:30,980 These are built by GitHub, updated frequently, and as of writing, cover a wide variety of operating systems and architectures. 25 00:01:30,980 --> 00:01:38,900 The key thing with GitHub-hosted runners is that they are ephemeral, meaning they are torn down at the conclusion of each workflow job. 26 00:01:38,900 --> 00:01:41,600 On the other hand, you have self-hosted runners. 27 00:01:41,600 --> 00:01:56,500 These are managed and secured entirely by the end user, and it's not surprising here that the path of least resistance when configuring one of these self-hosted runners is a non-ephemeral self-hosted runner that is actually the least secure. 28 00:01:56,500 --> 00:02:04,060 Both GitHub-hosted and self-hosted runners work to service GitHub Actions workflows that are triggered from repositories on GitHub. 29 00:02:04,720 --> 00:02:06,460 So, why are we here today? 30 00:02:06,460 --> 00:02:15,300 Well, all the organizations and projects that you see on the screen here use or at one point use self-hosted runners on their public GitHub repositories. 31 00:02:15,300 --> 00:02:24,880 Not only that, but they use them in an insecure manner, and over the last 12 months, we were able to identify vulnerabilities in these organizations and many more that we can't talk about. 32 00:02:24,880 --> 00:02:29,340 Some of these cases could have led to widespread critical supply chain attacks. 33 00:02:30,320 --> 00:02:39,480 You may be wondering if we just showed that last slide to flex our cool bones at DEF CON, and I just want to go on record saying that, yeah, pretty much. 34 00:02:39,660 --> 00:02:42,360 But it also raises a bigger question. 35 00:02:42,360 --> 00:02:46,520 Why were we in a position to execute supply chain attacks on all of these companies? 36 00:02:46,520 --> 00:02:56,380 And the reason behind that is that GitHub Actions provides this broad attack surface that exposes these organizations to compromise, especially when they use self-hosted runners. 37 00:02:57,360 --> 00:03:01,440 All right, so how do we get started with our vulnerability research campaign? 38 00:03:01,440 --> 00:03:07,980 Well, it all started all the way back in August of 2022 when I was working on a red team with my former employer. 39 00:03:07,980 --> 00:03:11,900 I was fairly new to red teaming at the time, and I tripped a canary and got us kicked out of the client network. 40 00:03:11,900 --> 00:03:13,460 Maybe that's happened to some of y'all. 41 00:03:13,720 --> 00:03:24,240 But while trying to get back in, we did some social engineering, and I found that we could use a GitHub access token that we obtained to execute a workflow and persist on a non-ephemeral self-hosted runner inside that client's network. 42 00:03:24,240 --> 00:03:26,520 And that got us back in, and we reached objective. 43 00:03:26,600 --> 00:03:34,780 That led to a talk at ShmooCon called Phantom of the Pipeline that was given in January of 2023, and the release of the original Gato tool. 44 00:03:35,520 --> 00:03:43,300 Around the time that Anand, Mason Davis, and Matt Chakassi were developing Gato, I joined them on the red team, and Gato was my entry to CICD security. 45 00:03:43,300 --> 00:03:48,380 So I started using Gato to identify GitHub Actions vulnerabilities during red team engagements. 46 00:03:48,380 --> 00:03:53,940 And then we started diving deeper into GitHub Actions abuse, especially around post-exploitation. 47 00:03:54,080 --> 00:04:01,300 And as we were doing our research, we were trying to brainstorm ways to blow this up and bring it from internal red team engagements to everybody. 48 00:04:01,300 --> 00:04:04,220 And Anand had an idea that really took it to the next level. 49 00:04:04,420 --> 00:04:16,900 So one of the little asterisks at the end of the ShmooCon presentation was that if you fixed a typo on a public repository that was using a non-ephemeral self-hosted runner, you could just make a pull request and modify the workflow file and get persistence. 50 00:04:16,900 --> 00:04:24,560 I demonstrated that vulnerability against GitHub Actions runner images repository, which was accepted as a critical vulnerability. 51 00:04:25,780 --> 00:04:33,300 After Anand hacked GitHub Actions runner images, we started looking at all public repositories and realized that these misconfigurations were everywhere. 52 00:04:33,300 --> 00:04:34,820 So we decided to team up. 53 00:04:34,820 --> 00:04:41,780 Our first joint operation was breaching Microsoft's perimeter by getting code execution on a domain-joined machine through Microsoft DeepSpeed. 54 00:04:41,780 --> 00:04:47,920 And then we launched a series of attacks, the next one of which we will cover today. 55 00:04:47,920 --> 00:04:53,340 Alright, so at a high level, there's three steps to discovering this vulnerability at scale. 56 00:04:53,340 --> 00:04:58,260 The first step is to search for candidate repositories that might be using a self-hosted runner. 57 00:04:58,260 --> 00:05:04,040 To do this, we used a combination of GitHub CodeSearch and Sourcegraph CodeSearch dorks. 58 00:05:04,040 --> 00:05:12,460 Both have pros and cons, so we used both and deduplicated the results to get a nice list of candidate repositories for further analysis. 59 00:05:12,660 --> 00:05:17,440 To do this, use the tool that's open source right now called GatoX. 60 00:05:17,440 --> 00:05:23,440 It uses automated workflow run log analysis to determine if there's a non-ephemeral self-hosted runner in use. 61 00:05:23,440 --> 00:05:30,820 Once you have the results, you can plan your next step, which is essentially, can you hack it and can you hack it for some good impact? 62 00:05:31,940 --> 00:05:37,840 Self-hosted runner takeover is a special case of public Poison Pipeline execution. 63 00:05:37,840 --> 00:05:47,180 So, Poison Pipeline execution at a high level is anytime you can get your own code executed by a GitHub Actions workflow, you can conduct a Poison Pipeline attack. 64 00:05:47,180 --> 00:05:56,340 In the case of self-hosted runner, we'll use this to deploy persistence on the self-hosted runner via a pull request and that opens up a ton of lateral movement and privilege escalation paths. 65 00:05:56,340 --> 00:05:59,920 So, the first two aspects of this diagram are pretty straightforward. 66 00:06:00,040 --> 00:06:05,280 This last section, the GitHub Actions post-exploitation, is where we focus most of our research. 67 00:06:06,040 --> 00:06:08,820 Alright, so it's a tale as old as Unix time itself. 68 00:06:09,160 --> 00:06:12,000 Misconfigurations are amplified by insecure defaults. 69 00:06:12,140 --> 00:06:20,720 So, the default setting for a pull request approval on the pull request trigger is that it only requires approval for first-time contributors. 70 00:06:20,720 --> 00:06:29,380 So , if someone has a pull request, no matter how trivial, merged into the default branch, they won't need approval for workload executions on their pull requests. 71 00:06:29,640 --> 00:06:32,740 This is that bad insecure default. 72 00:06:34,440 --> 00:06:44,580 Today, we're going to walk through playing with fire, which is what we've called our supply chain attack on the PyTorch repository, which began by compromising self-hosted runners. 73 00:06:44,580 --> 00:06:46,960 This is one of our more complex and more... 74 00:06:47,440 --> 00:06:55,200 more intricate attacks, so we're going to use it as a case study to teach technical TTPs of GitHub Actions exploitation and post-exploitation as we go. 75 00:06:55,700 --> 00:06:57,760 Alright, so, what is PyTorch? 76 00:06:57,760 --> 00:07:07,400 I'm sure a lot of you already know what it is, but it's a machine learning framer originally developed by Meta that's been open-sourced, and it's used by companies around the world like Google, Lockheed Martin, OpenAI, and more. 77 00:07:07,400 --> 00:07:15,320 If someone were to backdoor PyTorch, this could allow compromising developers and organizations in that very much red-hot AIML space right now. 78 00:07:16,040 --> 00:07:23,460 In all seriousness, probably the hardest part of compromising PyTorch was searching through their workflows. 79 00:07:23,460 --> 00:07:31,080 They had something like over 90 workflows, over 15 GitHub secrets, and more than five self-hosted runners on their public repository. 80 00:07:31,240 --> 00:07:46,880 And I vividly remember sitting at my desk, I had like three different notebooks out, and I was manually, pen and paper, mapping out the data flow through all these workflows, a lot of which were nested, in preparing for this attack, so that we could try to figure out the potential impact before we actually started. 81 00:07:47,800 --> 00:07:54,440 When we confirm that a repository has a self-hosted runner, we look at the workflow logs, like Adnan talked about earlier. 82 00:07:54,440 --> 00:08:02,620 So here's an example of the PyTorch repository, and in this screenshot, you can see in the workflow log, it has the runner name, the runner group name, and the machine name. 83 00:08:02,620 --> 00:08:11,760 So we see that this is a Jenkins runner, it's a self-hosted runner attached to their public repository in the default group, and this is one of the runners that we ended up compromising. 84 00:08:12,540 --> 00:08:15,500 Alright, remember that default approval requirement? 85 00:08:15,500 --> 00:08:21,140 Well, for certain repositories that are very busy, you can often determine that just using contextual analysis. 86 00:08:21,140 --> 00:08:23,540 And let me tell you, PyTorch was very busy. 87 00:08:23,540 --> 00:08:28,620 So what you look for is a pull request submitted by a previous contributor that's from a fork pull request. 88 00:08:28,620 --> 00:08:35,680 You check that the workflow run associated with it was not approved, and that it ran on the pull request trigger. 89 00:08:35,680 --> 00:08:40,300 When all these things come together, it's very likely that the default approval requirements are in place. 90 00:08:41,780 --> 00:08:51,080 GitHub warns against attaching self-hosted runners to public repositories for obvious reasons, but they don't make their documentation and their warnings obvious, and you kind of have to go out of your way. 91 00:08:51,080 --> 00:08:57,420 So part of the reason we think that so many repos publicly use self-hosted runners is because they don't end up seeing this documentation. 92 00:08:57,800 --> 00:09:05,460 In this next slide, we'll just walk through what it looks like to register a self-hosted runner and see what documentation you come across as a developer. 93 00:09:06,240 --> 00:09:14,400 So in my GitHub repository, I'm going to actions panel, I'm trying to register a self-hosted runner, but first, I'm going to look through their documentation, because I'm a good developer. 94 00:09:14,580 --> 00:09:22,560 And I click on these links, I don't see anything security related, so I click on this other link, and we get to a lot of GitHub self-hosted runner documentation. 95 00:09:22,600 --> 00:09:31,640 We learn about auto-scaling, usage limits, I think it lists architecture stuff, and then every API that the runner is going to use in its lifetime. 96 00:09:31,980 --> 00:09:36,480 And then, way down here at the bottom, it talks about self-hosted runner security. 97 00:09:36,480 --> 00:09:42,900 And so, GitHub does clearly say they don't recommend using self-hosted GitHub runners on public repositories. 98 00:09:42,900 --> 00:09:47,020 And they have other articles out there, too, elaborating on this. 99 00:09:47,020 --> 00:09:50,820 But the point we want to drive is it's not obvious. 100 00:09:50,820 --> 00:09:56,420 So chances are, if you're just registering a self-hosted runner to a public repo, you're not going to come across this documentation. 101 00:09:57,260 --> 00:10:00,820 Alright, the first step was to infiltrate the contributor list. 102 00:10:00,820 --> 00:10:04,520 This is what's important to take advantage of those default approval settings. 103 00:10:05,120 --> 00:10:09,840 To infiltrate the contributor list, if you've been following our blogs at all, we always call the grammar police. 104 00:10:09,840 --> 00:10:12,180 So the grammar police is me and Adnan. 105 00:10:12,180 --> 00:10:16,240 We show up, and we run all their markdown files through Grammarly. 106 00:10:16,240 --> 00:10:20,760 Again, fixing a typo here and finding a grammatical error doesn't matter at all. 107 00:10:20,760 --> 00:10:27,260 To become a contributor, we also could make a legitimate, substantial code contribution to the PyTorch repository. 108 00:10:27,520 --> 00:10:29,600 But, that's a lot of work. 109 00:10:29,860 --> 00:10:35,640 So, what we do instead is we notice resolve was in the past tense, and it should have been in the present tense. 110 00:10:35,640 --> 00:10:42,200 So, we fix that for them, we did a pull request, and a few days later, it got merged by the maintainers. 111 00:10:42,480 --> 00:10:46,060 And then we are now officially contributors, which is not a security boundary. 112 00:10:46,220 --> 00:10:49,120 And this is the same process we did with all of our exploits. 113 00:10:49,420 --> 00:10:51,580 All right, so this is where the hacking starts. 114 00:10:51,580 --> 00:10:58,400 So , the step phase two was to install command and control on those non-ephemeral self-hosted runners attached to PyTorch. 115 00:10:58,400 --> 00:11:01,880 To do this, we leverage something we like to call runner-on-runner. 116 00:11:01,960 --> 00:11:07,000 In a nutshell, what that is, is we install another self-hosted runner on their self-hosted runner. 117 00:11:07,000 --> 00:11:14,000 This has the benefit of sending all traffic to the same domains and IP addresses that the original self-hosted runner uses. 118 00:11:14,000 --> 00:11:19,440 So, if there is any EDR or monitoring software on that runner, it's very likely that it will fly right under the radar. 119 00:11:19,680 --> 00:11:21,080 Oh yeah, so remember that default? 120 00:11:21,080 --> 00:11:23,680 This is where those defaults lead to an actual compromise. 121 00:11:23,680 --> 00:11:29,520 To do this, we just modified the workflow in our fork and had it just install our C2. 122 00:11:30,180 --> 00:11:35,740 The second runner is attached to our private C2 repository, so we can task it to execute commands. 123 00:11:35,740 --> 00:11:37,340 It's essentially a web shell. 124 00:11:37,800 --> 00:11:43,160 Using this web shell, we're able to look around the file system and learn about the host. 125 00:11:43,160 --> 00:11:46,120 And all of this is in preparation for phase three. 126 00:11:46,840 --> 00:11:51,540 Phase three is the most important phase of all of our CICD compromise. 127 00:11:51,540 --> 00:11:59,980 So, the great secret heist, self-hosted runner post-exploitation, is how you go from trivial RCE to complete supply chain attack. 128 00:11:59,980 --> 00:12:11,940 And when we started our research, we submitted some reports and we saw some reports where researchers would get code execution on self-hosted runners, and then they'd say, hey, and by the way, you can probably do all these theoretical post-exploitation stuff. 129 00:12:12,280 --> 00:12:14,820 And orgs just didn't care at all. 130 00:12:14,820 --> 00:12:24,600 They should care if you're executing code on the runners, but it's kind of what they're supposed to do anyway, and so they didn't realize the actual impact of these attacks unless you go and demonstrate it. 131 00:12:24,600 --> 00:12:26,360 So, that is what we did. 132 00:12:26,880 --> 00:12:33,440 All right, so to understand how we were able to achieve some of this impact, I want to make sure everyone understands the magical getup token. 133 00:12:33,440 --> 00:12:43,700 So, this is a token that is used by all getup actions workflows to authenticate to GitHub for API or Git operations, and it's an OAuth bearer token that has multiple scopes. 134 00:12:43,700 --> 00:12:51,660 Some of these scopes can be read or write, and they're configured at the repository organization level or specified within each specific workflow file. 135 00:12:52,160 --> 00:12:58,620 So, it's very important to know that these tokens are only valid for the duration of each job within a workflow. 136 00:12:58,620 --> 00:13:01,660 As soon as that job concludes, the token is no longer valid. 137 00:13:01,660 --> 00:13:07,960 So, to conduct some of the post-exploitation attacks here, we need to work around this limitation. 138 00:13:08,240 --> 00:13:14,060 So, let's see how PyTorch, at the time, used or privileged the getup token. 139 00:13:14,480 --> 00:13:17,380 Okay, so this getup token has all the right permissions. 140 00:13:17,440 --> 00:13:27,300 This means we can, we have a lot of options to play with for some of our post-exploitation, and also, this runner right here is the same one that we now have C2 on. 141 00:13:28,640 --> 00:13:37,880 So, when a workflow uses the actions checkout step or some other steps, the getup token is stored on the self-hosted runner's file system, which we have compromised. 142 00:13:37,880 --> 00:13:42,380 So, the problem is that getup tokens from fork PRs only have read permissions. 143 00:13:42,520 --> 00:13:49,900 So, if we tried to take the getup token from the fork PR we used to actually implant the runners, it's pretty much useless to us. 144 00:13:49,900 --> 00:13:55,000 So, the solution is we persist on the runner and then we capture a token from a future workflow. 145 00:13:55,280 --> 00:13:56,880 This is the setup we typically see. 146 00:13:56,880 --> 00:14:02,780 We have the workflow from our fork PR, no access to secrets, and a getup token with only read permissions. 147 00:14:03,500 --> 00:14:07,020 Then, we have a workflow from the base repository. 148 00:14:07,160 --> 00:14:11,620 This workflow does have access to secrets and the getup token has write permissions. 149 00:14:11,620 --> 00:14:15,400 Notice how they both execute on the same self-hosted runner. 150 00:14:15,400 --> 00:14:19,580 So, this is the reason that persistent, non-ephemeral, self-hosted runners are so bad, right? 151 00:14:19,580 --> 00:14:26,960 If the runner's ephemeral and we implant it, it doesn't give us much access beyond secrets on the file system or something like that. 152 00:14:26,960 --> 00:14:31,120 If it's persistent and long-lived, then it will execute future workflows. 153 00:14:31,120 --> 00:14:36,980 So, we implant the runner, then we wait for future workflows from the base repo to execute on the runner. 154 00:14:36,980 --> 00:14:41,080 This is why we need to install persistence rather than just showing code execution. 155 00:14:41,300 --> 00:14:46,080 And then we compromise the getup token and any getup secrets used by subsequent workflows. 156 00:14:46,460 --> 00:15:00,800 So, in the PyTorch attack, you can see here, this was our C2 repository, so we were executing code on their self-hosted runners and we waited for a future workflow to execute from the base repo and then we just print out the getconfig file at runtime. 157 00:15:00,800 --> 00:15:04,960 So, this base64 encoded token is that getup token that we were just talking about. 158 00:15:04,960 --> 00:15:06,780 So, we compromised that getup token. 159 00:15:06,780 --> 00:15:14,280 Again, we could only use it for the duration of the build, but some of these jobs lasted a long time, so we were able to use it for a while. 160 00:15:14,560 --> 00:15:29,060 Alright, one of the first things we did after obtaining the getup token, which had actions write access, is we used it to delete the workflow run logs because if we got caught, then we likely wouldn't be able to show impact in order for Meta's triage team to understand the severity of the submission. 161 00:15:30,100 --> 00:15:35,540 One of the most direct ways to demonstrate impact with a getup token is by modifying getup releases. 162 00:15:35,540 --> 00:15:42,060 So, getup releases is just one way for a user to download a release of an application from a repository. 163 00:15:42,220 --> 00:15:54,620 What typically will happen is you can upload getup releases, so PyTorch uploads releases to their getup releases page, and then if you want to download PyTorch, one of the ways you can do that is by going to getup releases and downloading it there. 164 00:15:54,620 --> 00:16:00,780 Depending on the permissions of the getup token, it can allow you to modify releases and release assets. 165 00:16:00,780 --> 00:16:08,940 So, you could potentially backdoor a release asset, upload it, and then everyone who downloads and uses that asset will be running your malicious code. 166 00:16:09,660 --> 00:16:13,200 We did not want to upload any backdoored assets. 167 00:16:13,200 --> 00:16:16,500 We're trying to identify supply chain attacks, not actually execute them. 168 00:16:16,500 --> 00:16:20,440 So, instead of tampering with assets, we just tampered with the release name. 169 00:16:20,440 --> 00:16:28,740 So we sent this curl request using the getup API, and it uses the compromised getup token just to add my name to the end of the release. 170 00:16:28,740 --> 00:16:38,700 So, after we sent that request, you can see the latest PyTorch release at the time was now signed by yours truly, and all we did here was just take a screenshot and then revert it right away. 171 00:16:38,700 --> 00:16:45,260 Obviously, we didn't want this being seen by a lot of people, but this was our POC to show that we could tamper with getup releases if we wanted to. 172 00:16:46,000 --> 00:16:54,920 A problem that we've encountered with getup release tampering is organizations have came back to us and legitimately said, well, no one really uses our getup releases. 173 00:16:55,320 --> 00:17:02,300 Obviously, that shouldn't be the case, but to demonstrate further impact, we wanted to look at getup secrets. 174 00:17:02,380 --> 00:17:07,060 Getup secrets are the crown jewels of any repo that uses getup actions. 175 00:17:07,060 --> 00:17:13,900 A lot of the times, they're overprivileged, and they can provide lateral movement opportunities beyond the getup repository. 176 00:17:13,900 --> 00:17:24,440 So, we were back to being Charlie from It's Always Sunny, and we were searching through all those PyTorch workflows again, trying to see where getup secrets were used, and see if we could compromise them. 177 00:17:24,440 --> 00:17:28,760 Here, you can see that they're using some personal access tokens, which are always super interesting. 178 00:17:28,760 --> 00:17:33,800 They're using AWS keys, and there were a bunch of other secrets used by the PyTorch repository. 179 00:17:33,800 --> 00:17:37,660 And before we go any further, I want to quickly recap how we got here. 180 00:17:37,660 --> 00:17:49,900 So, remembering back to the grammar police, we came, we fixed the typo, that typo got merged, that made us a contributor to the repository, which was using the default setting, so we could execute arbitrary workflows. 181 00:17:50,040 --> 00:17:54,580 We used that access to execute a workflow that implanted three of their self-hosted runners. 182 00:17:54,780 --> 00:18:03,900 Then we stole this getup token from a future workflow executing on that runner, and now we're going to try to use this getup token to exfiltrate getup secrets. 183 00:18:04,280 --> 00:18:14,120 One problem that we had to work around is that the workflows that had the getup secrets that we wanted to take didn't actually run on the self-hosted runners that we were now sitting on. 184 00:18:14,120 --> 00:18:25,080 So, the solution here was to use the getup token to conduct pipeline privilege escalation and get arbitrary code execution within those workflows so we could get those very nice secrets. 185 00:18:25,960 --> 00:18:32,780 Another problem is getup tokens aren't allowed to modify files in the .github slash workflows directory. 186 00:18:32,780 --> 00:18:39,600 That seems like a kind of random restriction by getup, but it's actually implemented a few years ago to prevent these exact attacks. 187 00:18:39,600 --> 00:18:48,300 So, they want to stop attackers from doing what we're trying to do right now, and so you can't just make a new branch, modify a workflow to exfiltrate secrets, and then you're done. 188 00:18:48,520 --> 00:18:57,460 The solution here is you need to find a workflow that uses the getup secrets you're interested in that executes code from outside of this .github slash workflows directory. 189 00:18:57,460 --> 00:18:59,100 So, this was our path here. 190 00:18:59,300 --> 00:19:06,380 If you look back at PyTorch's weekly.yaml workflow at the time, you can see it does two interesting things. 191 00:19:06,380 --> 00:19:11,160 One of them is that it calls this underscore update commit hash .yaml workflow. 192 00:19:11,160 --> 00:19:19,820 The other is that it uses these update bot tokens and PyTorch bot tokens, which based on context, we were pretty sure were getup personal access tokens. 193 00:19:19,820 --> 00:19:22,720 So, let's look at update commit hash .yaml. 194 00:19:23,280 --> 00:19:38,580 This workflow is still in the restricted workflows directory, so we can't modify this workflow with the getup token, but it calls update commit hashes .py, which is in the .github slash scripts directory, so it's not in the restricted workflow directory. 195 00:19:38,580 --> 00:19:46,540 All right, so all we need to do now is use the getup token to make a new feature branch and modify that script. 196 00:19:46,540 --> 00:19:51,140 And we know that the workflow that calls it runs on a certain event, so we can take advantage of it. 197 00:19:52,340 --> 00:19:54,720 The modification that we introduced was quite simple. 198 00:19:54,720 --> 00:20:03,280 We saved off the tokens that it received via environment variables to files and then we used our public key to encrypt them and print them to the build logs. 199 00:20:03,280 --> 00:20:08,660 This would prevent any other attackers from getting those secrets while we were conducting our POC. 200 00:20:09,400 --> 00:20:10,820 Okay, so what did we actually do? 201 00:20:10,820 --> 00:20:13,360 We used the getup token to create a new branch. 202 00:20:13,500 --> 00:20:17,520 Then on the new branch we added our payload to update commit hashes .py script. 203 00:20:17,820 --> 00:20:22,020 Then we used the getup token to trigger our payload via workflow dispatch event. 204 00:20:22,020 --> 00:20:24,660 This is because the token had actions write privileges. 205 00:20:24,880 --> 00:20:31,740 And then we could retrieve the encrypted secrets from the build logs, cancel the workflow, delete the workflow logs, and actually decrypt the secrets. 206 00:20:31,740 --> 00:20:38,360 So at the time when we were doing this operation I was absolutely terrified that I was going to forget to take a screenshot. 207 00:20:38,360 --> 00:20:41,660 So instead of taking screenshots I just screen recorded the whole thing. 208 00:20:41,660 --> 00:20:45,460 So I have like an 8 hour screen recording of all this stuff from PyTorch. 209 00:20:45,460 --> 00:20:51,340 And I condensed some of it into a video on the next slide just showing the getup personal access token exfiltration. 210 00:20:51,340 --> 00:20:53,240 So we'll walk you all through that. 211 00:20:54,040 --> 00:21:03,100 Here we have our run on runner C2 and we're going to navigate to our command output and we're just going to get the base64 encoded getup token. 212 00:21:03,100 --> 00:21:07,300 This is from a future workflow that was executing on one of the compromised runners. 213 00:21:07,320 --> 00:21:13,120 So we're going to base64 decode that token and you can see it's just an authorization header within the getup token. 214 00:21:13,200 --> 00:21:18,740 Use the token to clone the repository and then we made a new branch in the PyTorch repository. 215 00:21:18,740 --> 00:21:25,140 So within our new branch we're going to modify that Python script that we identified as an injection point. 216 00:21:25,260 --> 00:21:37,760 Here with my really poor Vim skills I'm going to paste our new payload to exfiltrate those secrets and then once the payload is set we're going to actually commit this to the repository because the token has contents right. 217 00:21:37,760 --> 00:21:39,340 So we commit it to the repo. 218 00:21:39,340 --> 00:21:55,180 Now we're going to use a curl request just to trigger this workflow from our branch which has the injected code in it and then we're looking at the actual PyTorch repository now where we just triggered a workflow and we're going to see two base64 encoded encrypted blobs. 219 00:21:55,180 --> 00:22:04,980 So we're going to scroll down and we're going to get these blobs which is the output of our commands and then we're going to go to this other terminal and decrypt them. 220 00:22:04,980 --> 00:22:12,580 So first we're just going to base64 decode them and then we're going to save them to a file and we're going to use our private key locally to actually decrypt them and get the token. 221 00:22:12,580 --> 00:22:29,660 So once you see some output that starts with ghp underscore those are the personal access tokens and we're going to take them and run them through Gato which will do a bunch of automated enumeration using the GitHub APIs and then Gato will tell us who the owners are, 222 00:22:29,660 --> 00:22:32,840 what they have access to, and what the potential impact is. 223 00:22:32,840 --> 00:22:37,680 Because if they just had read access to the repository, you know, there's not a real risk here to PyTorch. 224 00:22:37,680 --> 00:22:42,360 If they have other, you know, more sensitive privileges then it could be bad. 225 00:22:42,360 --> 00:22:44,080 So here we're running them through Gato. 226 00:22:44,140 --> 00:22:46,920 You can see one of the users is the PyTorch update bot user. 227 00:22:46,920 --> 00:22:48,800 The other is the PyTorch bot user. 228 00:22:48,800 --> 00:22:51,120 And they're both PyTorch organization members. 229 00:22:51,120 --> 00:22:53,140 The tokens have repo scope. 230 00:22:53,140 --> 00:22:56,820 So we can use this now to interact with the PyTorch repository directly. 231 00:22:56,820 --> 00:23:02,280 These are long-lived credentials and they had access to 93 repos within the PyTorch organization. 232 00:23:02,300 --> 00:23:05,020 A lot of which were private and did not have branch protection. 233 00:23:05,020 --> 00:23:07,180 So you could have done whatever you wanted to them. 234 00:23:07,340 --> 00:23:10,500 And there's a lot of paths here to supply chain compromise. 235 00:23:11,160 --> 00:23:12,920 Alright, that was a lot, right? 236 00:23:12,920 --> 00:23:15,020 Okay, so what can we do? 237 00:23:15,020 --> 00:23:20,540 So we have those two tokens so we can use them by themselves to simply introduce code into the main branch. 238 00:23:20,540 --> 00:23:23,620 One creates a pull request and have the other one approve and merge it. 239 00:23:23,620 --> 00:23:23,960 Boom. 240 00:23:23,960 --> 00:23:25,240 Supply chain compromise. 241 00:23:25,400 --> 00:23:32,040 Another form would be to backdoor a dependency within the PyTorch organization that's used by the primary PyTorch repo. 242 00:23:32,040 --> 00:23:36,840 And that could be a more subtle way to obtain that same final impact. 243 00:23:36,860 --> 00:23:45,380 And finally, there's even more clever ways like smuggling malicious code into an in-progress feature branch as a developer is working on and then have that merged in. 244 00:23:45,380 --> 00:23:51,020 So really there's too many paths to count for how this could impact end users of PyTorch. 245 00:23:52,220 --> 00:23:58,280 If you want to see even more impact and you're not really into github, this section is for you. 246 00:23:58,280 --> 00:24:03,920 So, if you noticed a few slides ago when we were looking at their secrets, we saw that they also had AWS keys. 247 00:24:03,960 --> 00:24:11,940 We conducted a very similar attack to the one we just showed, right, where we do that kind of workflow hopping and injection and triggering with the github token. 248 00:24:11,940 --> 00:24:14,740 We repeated that process to grab their AWS keys. 249 00:24:14,740 --> 00:24:25,740 And the reason we wanted to do this was if you don't understand github, it can sometimes be hard to even see why code contributions to the main branch of a repository could be impactful. 250 00:24:25,740 --> 00:24:29,080 But, everyone understands AWS and AWS releases. 251 00:24:29,080 --> 00:24:35,660 So, we grabbed these keys then we used the AWS CLI to authenticate and we confirmed that we were the PyTorch bot user. 252 00:24:35,660 --> 00:24:43,140 So, we didn't want to poke around too much, but we did some basic AWS S3 LS commands and saw that this user had a lot of privileges. 253 00:24:43,140 --> 00:24:47,900 We confirmed through the workflow logs that they could actually write to these S3 buckets. 254 00:24:48,080 --> 00:24:54,640 And, one weird thing that we weren't really expecting to see was there were PyTorch releases in these S3 buckets. 255 00:24:54,640 --> 00:25:02,280 So, at the time, honestly I didn't think too much of it because I was kind of freaked out with the access we had and wanted to submit the report right away. 256 00:25:02,280 --> 00:25:07,180 I think, like we said, this was hour 8 or 9 at this point and I just wanted to get the report in. 257 00:25:07,180 --> 00:25:09,320 So, I didn't really pay attention to these releases. 258 00:25:09,320 --> 00:25:16,080 And then we were rehearsing like a week ago and we were talking and I was like, why were these releases in these S3 buckets? 259 00:25:16,080 --> 00:25:17,960 Like, what system was pulling releases from here? 260 00:25:17,960 --> 00:25:34,760 So, I went to the PyTorch website and just going to PyTorch.org or PyTorch.com, whatever it is, you can scroll down and you can see when they instruct users to install, download and install PyTorch, if you're using PIP or whatever, you're using this download. 261 00:25:36,180 --> 00:25:36,880 What is it? 262 00:25:36,880 --> 00:25:38,880 Download.pytorch.org URL. 263 00:25:38,880 --> 00:25:41,360 So, a lot of the releases were coming from this URL. 264 00:25:41,360 --> 00:26:03,080 So, we went to this URL in a new tab and we're just looking at the request and response headers and sure enough, in the headers, they confirmed that these were actually pulling releases from these S3 buckets and the layout of the release assets on that website were identical to the layout in the S3 buckets. 265 00:26:03,080 --> 00:26:18,480 So, basically , with those AWS keys, we could have uploaded our own PyTorch releases to these S3 buckets and then anyone who downloads PyTorch by following the instructions from the website is going to be downloading and potentially executing our malicious code. 266 00:26:18,480 --> 00:26:23,840 So, this is that kind of solar wind style supply chain attack for anyone that uses PyTorch in the future. 267 00:26:25,340 --> 00:26:26,300 Okay. 268 00:26:26,620 --> 00:26:27,600 That was a lot. 269 00:26:27,600 --> 00:26:28,760 We just did a lot. 270 00:26:28,860 --> 00:26:36,660 This is what it looks like put into a nice little red team diagram and if anyone got here late or fell asleep, we'll do a quick recap. 271 00:26:36,660 --> 00:26:40,760 So, we started by opening a typo fix PR in the PyTorch repository. 272 00:26:41,040 --> 00:26:51,300 That PR got merged and then we were a contributor so we submitted a malicious pull request that installed our runner-on-runner C2 on three different self-hosted GitHub runners. 273 00:26:51,660 --> 00:26:57,260 From one of these runners, we stole a GitHub token from a future build and we used that to do a bunch of stuff. 274 00:26:57,260 --> 00:27:02,260 First, we just modified that release by updating the release title with my name and reverting. 275 00:27:02,260 --> 00:27:14,320 Then, we created a feature branch in the PyTorch repository and we triggered additional workflows using that workflow dispatch event injecting through the scripts that were outside of the restricted workflows directory. 276 00:27:14,940 --> 00:27:17,220 Through this process, we stole some secrets. 277 00:27:17,220 --> 00:27:23,920 We stole the PyTorch bot AWS keys and then we stole two GitHub personal access tokens used by PyTorch bot users. 278 00:27:23,920 --> 00:27:30,100 There were a bunch more secrets in there but we didn't think there was any need for additional demonstration of impact. 279 00:27:30,380 --> 00:27:34,960 Using the AWS keys, we could upload new PyTorch releases to this S3 bucket. 280 00:27:34,960 --> 00:27:36,420 Obviously, we did not do that. 281 00:27:36,420 --> 00:27:41,080 Then, using the GitHub personal access tokens, we could have modified PyTorch dependencies. 282 00:27:41,220 --> 00:27:47,040 We could have used them in conjunction to commit and then approve merges to main and done a lot of other bad stuff. 283 00:27:47,720 --> 00:27:59,060 All of this goes to say, at the end of the day, there were a lot of supply chain compromised past year which would have affected most people using PyTorch both currently and in the future. 284 00:28:00,220 --> 00:28:13,420 You would think that this amount of demonstration of impact would be enough to convince PyTorch to immediately and urgently apply fixes and well, we'll let the disclosure timeline speak for itself. 285 00:28:13,420 --> 00:28:18,900 We submitted this issue in August 2023 to Metabug Bounty. 286 00:28:19,360 --> 00:28:23,820 A month later, Meta said there is no update to provide. 287 00:28:24,260 --> 00:28:27,900 Two months later, Meta said they consider the issue mitigated. 288 00:28:27,960 --> 00:28:35,080 I was suspicious so I went back in and just executed some shell commands on the runners. 289 00:28:35,080 --> 00:28:38,540 I didn't do the full attack, but anyone can still compromise their runners. 290 00:28:38,540 --> 00:28:46,380 Any contributor and based on the workflow logs and stuff, I don't think their configurations were still secure so we responded saying, hey, the issue is not fully mitigated. 291 00:28:46,380 --> 00:28:47,420 Here's some proof. 292 00:28:47,560 --> 00:29:01,240 Then another two months went by with really no word from Meta and we sent a strongly worded email expressing concerns on the remediation that has been implemented, which led to some back and forth. 293 00:29:01,240 --> 00:29:09,880 Then December 15th, Meta applied some other fixes which seemed to be sufficient and then they offered a call to discuss the remediation in depth. 294 00:29:10,020 --> 00:29:15,800 We actually ended up meeting with two of the PyTorch maintainers and they were very concerned about these issues. 295 00:29:15,800 --> 00:29:21,720 They were on top of it and they also expressed concern at the potential gaps in remediation. 296 00:29:21,800 --> 00:29:29,320 I'm not totally sure where the disconnect was, but it worried us that PyTorch was potentially vulnerable for this long. 297 00:29:31,100 --> 00:29:43,220 This attack took a lot of hands-on keyboard workflow modification, command and control, all that stuff, and so it kind of prompted us to say, hmm, is there an easier way to automate a lot of this? 298 00:29:43,800 --> 00:29:45,440 Alright, well, now there is. 299 00:29:45,440 --> 00:29:53,180 So with a tool that I've open sourced this week called GatoX, it actually automates that self-holster runner takeover process. 300 00:29:53,180 --> 00:30:03,360 So now, instead of all that work that you saw John have to do, which all you have to do is you fix a typo, and you become a contributor, then you run GatoX, and then one of two things happens. 301 00:30:03,500 --> 00:30:11,680 You either get a shell and you're on top of the world as hacker man, or you learn that approval is required and then you're sad because you didn't get to hack anything. 302 00:30:11,820 --> 00:30:18,360 So let's walk through a quick video demo of how GatoX works to automate this self-holstered runner takeover process. 303 00:30:18,800 --> 00:30:21,440 So first I use it to enumerate a runner. 304 00:30:21,440 --> 00:30:26,480 So now I'm going to go ahead and fix a typo in this test repository here. 305 00:30:26,660 --> 00:30:30,140 So I go ahead and fix a typo and make that commit. 306 00:30:31,040 --> 00:30:35,440 So after I make the commit, I'm going to go ahead and create a pull request. 307 00:30:35,440 --> 00:30:41,000 So I check that I fixed the typo and I make a quick typo fix PR and now that's merged in. 308 00:30:41,000 --> 00:30:45,420 So after that's merged in with my attacker account, I'm going to go ahead and delete the fork. 309 00:30:45,540 --> 00:30:49,760 So because GatoX will create a new fork to throw the payload at the repository. 310 00:30:49,760 --> 00:30:55,120 So I'm going to quickly go ahead and do the web flow to delete the fork and I hit confirm. 311 00:30:55,300 --> 00:30:57,320 Alright, now I'm going to run GatoX. 312 00:30:57,380 --> 00:31:03,540 So here I've just configured it to deploy a runner on runner on a Linux x86 64 -bit runner. 313 00:31:03,540 --> 00:31:10,160 So it's going to go ahead and create the payload within a gist and ask for confirmation because the next steps are overt. 314 00:31:10,260 --> 00:31:12,180 So I'm going to type confirm and hit enter. 315 00:31:12,580 --> 00:31:22,060 So as soon as I do that, it's going to create a draft pull request which is going to deploy that runner on runner payload onto that self hosted runner via fork PR. 316 00:31:22,120 --> 00:31:28,040 So it's going to take a second because it's now pulling for that runner to connect to GitHub and then check back into that repository. 317 00:31:28,100 --> 00:31:32,400 So as soon as it does that, it will drop into a nice and very convenient shell. 318 00:31:32,840 --> 00:31:48,920 Okay, now I'm just going to test this with uname-a and when I hit enter, what's happening under the hood is that GatoX is issuing a workflow dispatch event to that workflow in the C2 repository which is going to run code on that malicious self hosted runner. 319 00:31:48,920 --> 00:31:50,200 And you can see that output there. 320 00:31:50,960 --> 00:31:55,680 Alright , so now you know the techniques and now since I've open sourced it, you have a tool. 321 00:31:55,860 --> 00:32:06,480 And another thing I want to point out is that finding CICD misconfigurations in open source repositories can be a very thankless job because you submit a report, they fix a bug, and you don't even get a CVE. 322 00:32:06,480 --> 00:32:08,400 So there's not really a lot of motivation for people. 323 00:32:08,400 --> 00:32:13,020 So what I've done with GatoX is that on its wiki, I've created a little hall of fame. 324 00:32:13,020 --> 00:32:24,200 So if you use GatoX to find a pwn request, injection, or self hosted runner vulnerability in an open source project, and the maintainers fix it, then just make an issue and I'll add you your name to the wiki to get some credit. 325 00:32:24,200 --> 00:32:27,880 Just make sure it's actually fixed because we don't want the hall of fame to be the hall of odays. 326 00:32:27,880 --> 00:32:29,680 As much as some people might like that. 327 00:32:31,340 --> 00:32:39,740 Okay, so we showed all of the technical TTPs we used during our PyTorch attack but there are a bunch of other stuff we've used on other targets. 328 00:32:39,740 --> 00:32:47,340 And so we want to take these next few minutes to honor the die hard CICD nerds who are here to learn all the TTPs possible. 329 00:32:47,340 --> 00:32:52,060 So we're going to run through this pretty quickly but all the slides will be up there online so you can check it out after. 330 00:32:52,380 --> 00:32:57,900 Alright, so one thing you can do after getting persistence on a runner is solo run style build compromise. 331 00:32:58,020 --> 00:33:06,120 So, you get persistence and then you just have to modify scripts for the build or source code after it's checked out. 332 00:33:06,120 --> 00:33:17,580 And once you do that, the final build artifacts from that run are going to be poisoned but there's no it's not going to be linked back to the original source code so it's a very stealthy way to conduct a supply chain attack. 333 00:33:17,760 --> 00:33:22,480 And here's another way that you could take GitHub release asset tampering a little further. 334 00:33:22,540 --> 00:33:33,080 So, if you have that context write token, then all you have to do to tamper with releases on assets is use the API to delete the old asset and then upload a new one with a post request right on top of it. 335 00:33:33,140 --> 00:33:42,500 And the indicator of compromise here is just going to be the timestamp when a user is looking at it through the web interface so it's not very obvious. 336 00:33:42,500 --> 00:33:53,340 And if you want to see a tale of a bug bounty gone wrong and a program acting in some pretty bad faith, go check out my blog post on my report to the ASTAR network which is a tiny polkadot parachain. 337 00:33:59,940 --> 00:34:05,160 Alright, another technique you can add to your arsenal is something called a post checkout hook. 338 00:34:05,180 --> 00:34:13,900 So, what happens when a subsequent workflow that you need to get secrets from or token from runs only very infrequently? 339 00:34:13,900 --> 00:34:16,700 Well, you need something that can have these qualities. 340 00:34:16,700 --> 00:34:27,620 You need to be able to extend the build time you also don't want to break how the workflow works to inform someone that something is going wrong and you also want to get notified because you don't want to be pressing F5 for two weeks. 341 00:34:27,680 --> 00:34:35,620 Well, a quick solution here is to place a script like this into the git hooks directory of the repository in the original self hosted runner. 342 00:34:35,640 --> 00:34:43,300 This example here will just take that git config file, encode it and then send it off to your exfil URL and then sleep for 15 minutes. 343 00:34:43,300 --> 00:34:48,880 So, this is going to give you 15 minutes to take advantage of that github token to perform post exploitation actions. 344 00:34:49,180 --> 00:34:59,060 Alright, before we dive into other TTPs, it's important to remember how self hosted runner takeover falls within the landscape of broader github actions attack techniques. 345 00:34:59,060 --> 00:35:06,700 So, there's another type of attack called bone requests where you basically get execution within a privileged workflow. 346 00:35:06,700 --> 00:35:08,980 That's the same thing that happens with runner takeover. 347 00:35:09,020 --> 00:35:12,780 From there, there's different post exploitation techniques you can use. 348 00:35:12,780 --> 00:35:25,200 So , you can steal secrets, you could get the actions runtime token, which has some interesting attacks with cache poisoning as well as OIDC abuse, which I think the talk right after us in this track actually goes into a lot more detail there. 349 00:35:25,200 --> 00:35:26,560 So, kind of cool. 350 00:35:26,840 --> 00:35:29,720 And then, there's just too many different attacks to talk about. 351 00:35:29,720 --> 00:35:32,420 There's so many ways to achieve great impact. 352 00:35:33,300 --> 00:35:38,180 We talked about some of the permissions of the github token today, but there's a lot of other dangerous permissions out there. 353 00:35:38,180 --> 00:35:44,260 Some of them don't pose a risk, but others, like contents write and actions write, obviously pose a significant risk. 354 00:35:44,360 --> 00:35:51,440 Other permissions we have identified as dangerous and actually abused during our attacks are pages write, pull requests write, and packages write. 355 00:35:51,440 --> 00:35:55,060 And there's a lot of good documentation online if you want to learn more about what these actually do. 356 00:35:55,880 --> 00:36:04,120 If you have just actions write and contents write, you can create feature branches, modify files, execute in workflows, and then issue dispatches events to seal secrets. 357 00:36:04,120 --> 00:36:07,440 If that looks familiar, it's because that's exactly what we did during PyTorch. 358 00:36:08,000 --> 00:36:11,300 Alright, what if you have a workflow that looks like this? 359 00:36:11,300 --> 00:36:16,140 It runs on workflow dispatch, but then uses some of that input in a github script or run step. 360 00:36:16,140 --> 00:36:19,740 Well, this lets you do good old 90's style code injection. 361 00:36:19,740 --> 00:36:21,400 And I'm going to walk you through how this process works . 362 00:36:21,400 --> 00:36:25,120 And you only need a github token actions write to make this hop. 363 00:36:25,460 --> 00:36:35,300 So, here you can see that the input from the dispatch event has org and repository, and within that script step, repository is used by github context expression. 364 00:36:35,300 --> 00:36:37,640 So , that's going to be our injection point there. 365 00:36:37,640 --> 00:36:38,620 See that arrow? 366 00:36:38,760 --> 00:36:43,320 So, here's an example payload thrown with a simple request script in Python. 367 00:36:43,320 --> 00:36:52,500 The first thing here is to close out the script because we don't want a syntax error or have it to crash because we still need the code to finish in order for us to get our payload execution. 368 00:36:52,820 --> 00:37:05,840 The next thing, and this is where the actual payload is, is a JavaScript injection payload that essentially will pull code from a C2 URL and pipe it to bash. 369 00:37:05,840 --> 00:37:14,020 So, then you can just have arbitrary code there and get execution in that workflow, get the token, get the secrets, and then from there do more post exploitation. 370 00:37:17,510 --> 00:37:21,470 If you only have contents write , there's still a lot of bad stuff you can do. 371 00:37:21,470 --> 00:37:26,510 You can modify non-protected branches a lot of times, which actually could end up in releases. 372 00:37:26,530 --> 00:37:30,170 You can modify releases directly, like we did when we signed my name. 373 00:37:30,230 --> 00:37:36,770 You can modify tags, which a lot of times reusable actions are referenced by tags, so that could open up some fun possibilities. 374 00:37:36,770 --> 00:37:40,490 And then you can also issue repository dispatch events to execute workflows. 375 00:37:41,430 --> 00:37:45,650 Alright, here's a really unique way to take advantage of contents write. 376 00:37:46,030 --> 00:37:59,210 Alright, so one way is if you want to get execution on a workflow that only runs on the push trigger, you can add code to that feature branch that modifies a script that's called by that workflow. 377 00:37:59,210 --> 00:38:07,550 And the next time the developer will push changes, now you have arbitrary code execution within the context of that workflow and then you can jump further from there. 378 00:38:07,550 --> 00:38:11,210 Another way is instead of targeting the workflows, why not target the developer? 379 00:38:11,210 --> 00:38:19,130 So you could modify a unit test within a feature branch that a developer is working on and add an info stealer payload. 380 00:38:19,130 --> 00:38:26,330 So then when the workflow runs, the dev account's compromised and often in organizations, dev accounts compromise can be game over. 381 00:38:26,350 --> 00:38:34,030 Another way is if you have these two permissions for the token, you can get code modification in the protected default branch. 382 00:38:34,030 --> 00:38:35,590 So there's some preconditions here. 383 00:38:35,590 --> 00:38:40,690 The repository has to have the setting to allow get of actions to approve and merge pull requests. 384 00:38:40,730 --> 00:38:45,870 There can be at most one reviewer required for that pull request approval rule. 385 00:38:45,990 --> 00:38:51,610 And there cannot be any code owner protection rule sets enabled and enforced. 386 00:38:51,610 --> 00:38:54,950 When all of these are together, you can have an attack like this. 387 00:38:55,070 --> 00:39:06,430 Attacker gets the GitHub token, they use their account to create a fork pull request with malicious changes and then they approve that pull request with that capture token and merge that pull request. 388 00:39:06,430 --> 00:39:09,070 That leads to a good old supply chain compromise. 389 00:39:10,870 --> 00:39:16,410 Now, if you're on a runner, it's possible to steal the GitHub actions runtime token from a future workflow. 390 00:39:16,410 --> 00:39:20,370 This is where you'll have some attack paths for GitHub actions cache poisoning. 391 00:39:20,370 --> 00:39:24,430 I have a blog post on my website that goes into the details of that. 392 00:39:24,710 --> 00:39:37,510 And if there's an organization level runner group that shares that also private repositories are using, then there's paths to jump to private repositories and other runner groups and it just kind of blows those attack paths wide open. 393 00:39:38,550 --> 00:39:41,770 Okay, that's the end of some of the more advanced TTPs. 394 00:39:41,770 --> 00:39:49,890 If you're the type of person who likes big mind maps to tell you exactly what to do, I made one and I put it on my GitHub a few days ago, so you can go check that out. 395 00:39:49,890 --> 00:40:00,270 Obviously, this is not comprehensive, but if you're trying to get into GitHub actions exploitation and you have read or write access to a repo but don't really know what to look for or what to do next, check this out. 396 00:40:00,270 --> 00:40:01,950 It should be able to give you some good guidance. 397 00:40:03,110 --> 00:40:05,130 In our opinion, GitHub can do a few things better. 398 00:40:05,130 --> 00:40:13,370 They can increase warnings and awareness, so if you try to attach a selfless runner to a public repo, it'd be great if there were big red letters saying, be careful, this is dangerous. 399 00:40:13,370 --> 00:40:30,550 They can also improve their secure defaults, like we talked about with the workflow approval requirements, and then they could implement granular approval requirements so that maintainers don't need to select between three options, all of which have broad privileges and probably will hurt developer workflows one way or the other. 400 00:40:31,010 --> 00:40:35,530 If you're trying to defend your organization from these attacks, start with the easy stuff. 401 00:40:35,530 --> 00:40:40,430 Please change this radio button so that you require approval for all outside collaborators. 402 00:40:40,650 --> 00:40:45,890 We use the GitHub token extensively and almost all of our attacks set it to read-only. 403 00:40:45,890 --> 00:40:48,470 That will stop a lot of post-exploitation opportunities. 404 00:40:48,470 --> 00:40:52,370 And then use, enforce your devs to use fine-grained personal access tokens. 405 00:40:52,370 --> 00:40:59,390 We, like, we hate discovering fine-grained tokens on red team engagements because they only give you access to one repository. 406 00:40:59,390 --> 00:41:02,910 If we find a personal access token that's classic, we can do a lot of stuff. 407 00:41:02,910 --> 00:41:06,010 If it's fine-grained, sometimes that's the end of the road. 408 00:41:06,570 --> 00:41:12,770 Alright, there's also ways to secure yourself more by making sure that your selfless runners are ephemeral. 409 00:41:12,770 --> 00:41:20,330 So, GitHub maintains something called ActionsRunnerController, which allows you to automatically spin up and spin down self-hosted runners using Kubernetes. 410 00:41:20,610 --> 00:41:28,050 Also, a lot of cloud providers have auto-scaling groups that can be linked with self-hosted runners and have that also be done automatically. 411 00:41:28,050 --> 00:41:39,090 And finally, there's a lot of third parties that offer turnkey drop-in replacements for GitHub runners that are also ephemeral if you don't want to use just the GitHub-hosted runners. 412 00:41:39,150 --> 00:41:46,650 And one thing that's important to remember here is that ephemeral means the environment that the runner is in also has to be ephemeral. 413 00:41:46,650 --> 00:41:56,550 So, if there's an ephemeral runner, but it's using a non-ephemeral file share, then an attacker can simply jump to other workflows by saving certain files off. 414 00:41:56,550 --> 00:42:03,030 So , it's important to really consider the entirety of that environment when it comes to build agent security. 415 00:42:03,990 --> 00:42:08,450 Alright, so another way is by using GitHub's feature for runner group pinning. 416 00:42:08,450 --> 00:42:16,950 So, GitHub has a feature where if you have a runner group, you can actually pin it to a specific workflow, or even a specific SHA or reference of a workflow. 417 00:42:17,050 --> 00:42:22,430 So, with this, you have a lot of different options to restrict which workflows can even use the runner. 418 00:42:22,430 --> 00:42:26,450 And all of this has the ultimate effect of protecting your most privileged runners. 419 00:42:27,190 --> 00:42:33,350 We want to end our talk today by emphasizing that this talk and this research is not about PyTorch . 420 00:42:33,350 --> 00:42:35,490 So, we have some key takeaways here. 421 00:42:35,490 --> 00:42:38,150 If you've been zoning out, lock in for this last slide. 422 00:42:38,150 --> 00:42:46,850 What we did to PyTorch, we've been able to do to a lot of advanced mature organizations because of this lack of awareness around CICD agent security. 423 00:42:46,850 --> 00:42:50,950 So, I think we've submitted over 20 high and critical bug bounty submissions. 424 00:42:50,950 --> 00:43:01,090 Among them were critical vulnerabilities in GitHub Actions, Microsoft, PyTorch, TensorFlow, ASTAR, HydroGX, ByteDance, and a lot more that we're not allowed to talk about. 425 00:43:01,930 --> 00:43:08,250 Additionally, if you think that public CICD security is bad, internal CICD security is a nightmare. 426 00:43:08,250 --> 00:43:18,810 Almost not every Red Team engagement I've been on, but a lot of Red Team engagements were able to get access to a CICD platform and then escalate privileges and use it to reach objective. 427 00:43:18,810 --> 00:43:27,570 It's almost become the new ADCS for us where internally everyone's vulnerable and compromising these CICD platforms can give you extensive privileges. 428 00:43:28,310 --> 00:43:32,710 Finally , what we need everyone to do is go learn about these attacks to protect your org from compromise. 429 00:43:32,710 --> 00:43:35,090 The main issue we see is just this lack of awareness. 430 00:43:35,090 --> 00:43:44,750 So if devs, architects, execs, and everyone is learning about these attacks, they'll be able to implement controls that hopefully protect their organization from the next critical supply chain attack. 431 00:43:45,430 --> 00:43:49,770 Alright, the moral of the story here is learn about this stuff and don't let this be you. 432 00:43:52,590 --> 00:44:01,970 Alright, so we want to start off by thanking all of the bug bounty triage teams that handled some of the reports and handled them well and applied solid mitigations to their products. 433 00:44:02,070 --> 00:44:03,890 We'd like to thank everyone who showed out today. 434 00:44:03,890 --> 00:44:07,990 Thank you so much for watching us and helping us raise awareness and for everyone watching online. 435 00:44:08,130 --> 00:44:09,050 Thank you. 436 00:44:09,050 --> 00:44:11,330 We'd like to thank DEF CON for giving us this platform. 437 00:44:11,330 --> 00:44:14,150 We wanted to speak here for a long time so this has been really fun. 438 00:44:14,530 --> 00:44:18,230 Please, we don't have time for questions, but come find us after if you want to talk more. 439 00:44:18,370 --> 00:44:19,310 Thank you everybody!