All right. Hello, everyone. All right, let's address the burning question in the room. 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. Maybe in the following presentation, but not today. This is Grand Theft Actions abusing self-hosted GitHub runners at scale. Hello, everybody. 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. All vulnerabilities mentioned during this talk have been remediated. 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. All right. Whoa, there we go. Adnan, I think we're good to go. All right, if you're wondering about us, so my name is Adnan Khan. For my day job, I work as a security engineer. I'm also a security researcher and bug bounty hunter on the side, and you can find some of my socials below. My name is John Stawinski. I'm a red team security engineer at Praetorian. I also do CICD security research on the side. 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. All right. 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. On one side, you have GitHub-hosted runners. These are built by GitHub, updated frequently, and as of writing, cover a wide variety of operating systems and architectures. The key thing with GitHub-hosted runners is that they are ephemeral, meaning they are torn down at the conclusion of each workflow job. On the other hand, you have self-hosted runners. 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. Both GitHub-hosted and self-hosted runners work to service GitHub Actions workflows that are triggered from repositories on GitHub. So, why are we here today? 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. 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. Some of these cases could have led to widespread critical supply chain attacks. 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. But it also raises a bigger question. Why were we in a position to execute supply chain attacks on all of these companies? 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. All right, so how do we get started with our vulnerability research campaign? Well, it all started all the way back in August of 2022 when I was working on a red team with my former employer. I was fairly new to red teaming at the time, and I tripped a canary and got us kicked out of the client network. Maybe that's happened to some of y'all. 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. And that got us back in, and we reached objective. 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. 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. So I started using Gato to identify GitHub Actions vulnerabilities during red team engagements. And then we started diving deeper into GitHub Actions abuse, especially around post-exploitation. 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. And Anand had an idea that really took it to the next level. 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. I demonstrated that vulnerability against GitHub Actions runner images repository, which was accepted as a critical vulnerability. After Anand hacked GitHub Actions runner images, we started looking at all public repositories and realized that these misconfigurations were everywhere. So we decided to team up. Our first joint operation was breaching Microsoft's perimeter by getting code execution on a domain-joined machine through Microsoft DeepSpeed. And then we launched a series of attacks, the next one of which we will cover today. Alright, so at a high level, there's three steps to discovering this vulnerability at scale. The first step is to search for candidate repositories that might be using a self-hosted runner. To do this, we used a combination of GitHub CodeSearch and Sourcegraph CodeSearch dorks. Both have pros and cons, so we used both and deduplicated the results to get a nice list of candidate repositories for further analysis. To do this, use the tool that's open source right now called GatoX. It uses automated workflow run log analysis to determine if there's a non-ephemeral self-hosted runner in use. 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? Self-hosted runner takeover is a special case of public Poison Pipeline execution. 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. 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. So, the first two aspects of this diagram are pretty straightforward. This last section, the GitHub Actions post-exploitation, is where we focus most of our research. Alright, so it's a tale as old as Unix time itself. Misconfigurations are amplified by insecure defaults. So, the default setting for a pull request approval on the pull request trigger is that it only requires approval for first-time contributors. 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. This is that bad insecure default. 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. This is one of our more complex and more... 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. Alright, so, what is PyTorch? 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. If someone were to backdoor PyTorch, this could allow compromising developers and organizations in that very much red-hot AIML space right now. In all seriousness, probably the hardest part of compromising PyTorch was searching through their workflows. They had something like over 90 workflows, over 15 GitHub secrets, and more than five self-hosted runners on their public repository. 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. When we confirm that a repository has a self-hosted runner, we look at the workflow logs, like Adnan talked about earlier. 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. 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. Alright, remember that default approval requirement? Well, for certain repositories that are very busy, you can often determine that just using contextual analysis. And let me tell you, PyTorch was very busy. So what you look for is a pull request submitted by a previous contributor that's from a fork pull request. You check that the workflow run associated with it was not approved, and that it ran on the pull request trigger. When all these things come together, it's very likely that the default approval requirements are in place. 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. 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. 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. 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. 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. 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. And then, way down here at the bottom, it talks about self-hosted runner security. And so, GitHub does clearly say they don't recommend using self-hosted GitHub runners on public repositories. And they have other articles out there, too, elaborating on this. But the point we want to drive is it's not obvious. 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. Alright, the first step was to infiltrate the contributor list. This is what's important to take advantage of those default approval settings. To infiltrate the contributor list, if you've been following our blogs at all, we always call the grammar police. So the grammar police is me and Adnan. We show up, and we run all their markdown files through Grammarly. Again, fixing a typo here and finding a grammatical error doesn't matter at all. To become a contributor, we also could make a legitimate, substantial code contribution to the PyTorch repository. But, that's a lot of work. So, what we do instead is we notice resolve was in the past tense, and it should have been in the present tense. So, we fix that for them, we did a pull request, and a few days later, it got merged by the maintainers. And then we are now officially contributors, which is not a security boundary. And this is the same process we did with all of our exploits. All right, so this is where the hacking starts. So, the step phase two was to install command and control on those non-ephemeral self-hosted runners attached to PyTorch. To do this, we leverage something we like to call runner-on-runner. In a nutshell, what that is, is we install another self-hosted runner on their self-hosted runner. This has the benefit of sending all traffic to the same domains and IP addresses that the original self-hosted runner uses. So, if there is any EDR or monitoring software on that runner, it's very likely that it will fly right under the radar. Oh yeah, so remember that default? This is where those defaults lead to an actual compromise. To do this, we just modified the workflow in our fork and had it just install our C2. The second runner is attached to our private C2 repository, so we can task it to execute commands. It's essentially a web shell. Using this web shell, we're able to look around the file system and learn about the host. And all of this is in preparation for phase three. Phase three is the most important phase of all of our CICD compromise. So, the great secret heist, self-hosted runner post-exploitation, is how you go from trivial RCE to complete supply chain attack. 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. And orgs just didn't care at all. 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. So, that is what we did. 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. 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. 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. So, it's very important to know that these tokens are only valid for the duration of each job within a workflow. As soon as that job concludes, the token is no longer valid. So, to conduct some of the post-exploitation attacks here, we need to work around this limitation. So, let's see how PyTorch, at the time, used or privileged the getup token. Okay, so this getup token has all the right permissions. 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. 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. So, the problem is that getup tokens from fork PRs only have read permissions. 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. So, the solution is we persist on the runner and then we capture a token from a future workflow. This is the setup we typically see. We have the workflow from our fork PR, no access to secrets, and a getup token with only read permissions. Then, we have a workflow from the base repository. This workflow does have access to secrets and the getup token has write permissions. Notice how they both execute on the same self-hosted runner. So, this is the reason that persistent, non-ephemeral, self-hosted runners are so bad, right? 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. If it's persistent and long-lived, then it will execute future workflows. So, we implant the runner, then we wait for future workflows from the base repo to execute on the runner. This is why we need to install persistence rather than just showing code execution. And then we compromise the getup token and any getup secrets used by subsequent workflows. 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. So, this base64 encoded token is that getup token that we were just talking about. So, we compromised that getup token. 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. 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. One of the most direct ways to demonstrate impact with a getup token is by modifying getup releases. So, getup releases is just one way for a user to download a release of an application from a repository. 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. Depending on the permissions of the getup token, it can allow you to modify releases and release assets. 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. We did not want to upload any backdoored assets. We're trying to identify supply chain attacks, not actually execute them. So, instead of tampering with assets, we just tampered with the release name. 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. 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. 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. 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. Obviously, that shouldn't be the case, but to demonstrate further impact, we wanted to look at getup secrets. Getup secrets are the crown jewels of any repo that uses getup actions. A lot of the times, they're overprivileged, and they can provide lateral movement opportunities beyond the getup repository. 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. Here, you can see that they're using some personal access tokens, which are always super interesting. They're using AWS keys, and there were a bunch of other secrets used by the PyTorch repository. And before we go any further, I want to quickly recap how we got here. 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. We used that access to execute a workflow that implanted three of their self-hosted runners. 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. 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. 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. Another problem is getup tokens aren't allowed to modify files in the .github slash workflows directory. That seems like a kind of random restriction by getup, but it's actually implemented a few years ago to prevent these exact attacks. 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. 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. So, this was our path here. If you look back at PyTorch's weekly.yaml workflow at the time, you can see it does two interesting things. One of them is that it calls this underscore update commit hash .yaml workflow. 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. So, let's look at update commit hash .yaml. 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. All right, so all we need to do now is use the getup token to make a new feature branch and modify that script. And we know that the workflow that calls it runs on a certain event, so we can take advantage of it. The modification that we introduced was quite simple. 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. This would prevent any other attackers from getting those secrets while we were conducting our POC. Okay, so what did we actually do? We used the getup token to create a new branch. Then on the new branch we added our payload to update commit hashes .py script. Then we used the getup token to trigger our payload via workflow dispatch event. This is because the token had actions write privileges. And then we could retrieve the encrypted secrets from the build logs, cancel the workflow, delete the workflow logs, and actually decrypt the secrets. So at the time when we were doing this operation I was absolutely terrified that I was going to forget to take a screenshot. So instead of taking screenshots I just screen recorded the whole thing. So I have like an 8 hour screen recording of all this stuff from PyTorch. And I condensed some of it into a video on the next slide just showing the getup personal access token exfiltration. So we'll walk you all through that. 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. This is from a future workflow that was executing on one of the compromised runners. So we're going to base64 decode that token and you can see it's just an authorization header within the getup token. Use the token to clone the repository and then we made a new branch in the PyTorch repository. So within our new branch we're going to modify that Python script that we identified as an injection point. 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. So we commit it to the repo. 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. 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. 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. 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, what they have access to, and what the potential impact is. Because if they just had read access to the repository, you know, there's not a real risk here to PyTorch. If they have other, you know, more sensitive privileges then it could be bad. So here we're running them through Gato. You can see one of the users is the PyTorch update bot user. The other is the PyTorch bot user. And they're both PyTorch organization members. The tokens have repo scope. So we can use this now to interact with the PyTorch repository directly. These are long-lived credentials and they had access to 93 repos within the PyTorch organization. A lot of which were private and did not have branch protection. So you could have done whatever you wanted to them. And there's a lot of paths here to supply chain compromise. Alright, that was a lot, right? Okay, so what can we do? So we have those two tokens so we can use them by themselves to simply introduce code into the main branch. One creates a pull request and have the other one approve and merge it. Boom. Supply chain compromise. Another form would be to backdoor a dependency within the PyTorch organization that's used by the primary PyTorch repo. And that could be a more subtle way to obtain that same final impact. 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. So really there's too many paths to count for how this could impact end users of PyTorch. If you want to see even more impact and you're not really into github, this section is for you. So, if you noticed a few slides ago when we were looking at their secrets, we saw that they also had AWS keys. 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. We repeated that process to grab their AWS keys. 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. But, everyone understands AWS and AWS releases. So, we grabbed these keys then we used the AWS CLI to authenticate and we confirmed that we were the PyTorch bot user. 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. We confirmed through the workflow logs that they could actually write to these S3 buckets. And, one weird thing that we weren't really expecting to see was there were PyTorch releases in these S3 buckets. 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. I think, like we said, this was hour 8 or 9 at this point and I just wanted to get the report in. So, I didn't really pay attention to these releases. 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? Like, what system was pulling releases from here? 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. What is it? Download.pytorch.org URL. So, a lot of the releases were coming from this URL. 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. 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. So, this is that kind of solar wind style supply chain attack for anyone that uses PyTorch in the future. Okay. That was a lot. We just did a lot. 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. So, we started by opening a typo fix PR in the PyTorch repository. 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. From one of these runners, we stole a GitHub token from a future build and we used that to do a bunch of stuff. First, we just modified that release by updating the release title with my name and reverting. 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. Through this process, we stole some secrets. We stole the PyTorch bot AWS keys and then we stole two GitHub personal access tokens used by PyTorch bot users. There were a bunch more secrets in there but we didn't think there was any need for additional demonstration of impact. Using the AWS keys, we could upload new PyTorch releases to this S3 bucket. Obviously, we did not do that. Then, using the GitHub personal access tokens, we could have modified PyTorch dependencies. We could have used them in conjunction to commit and then approve merges to main and done a lot of other bad stuff. 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. 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. We submitted this issue in August 2023 to Metabug Bounty. A month later, Meta said there is no update to provide. Two months later, Meta said they consider the issue mitigated. I was suspicious so I went back in and just executed some shell commands on the runners. I didn't do the full attack, but anyone can still compromise their runners. 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. Here's some proof. 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. 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. We actually ended up meeting with two of the PyTorch maintainers and they were very concerned about these issues. They were on top of it and they also expressed concern at the potential gaps in remediation. I'm not totally sure where the disconnect was, but it worried us that PyTorch was potentially vulnerable for this long. 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? Alright, well, now there is. So with a tool that I've open sourced this week called GatoX, it actually automates that self-holster runner takeover process. 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. 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. So let's walk through a quick video demo of how GatoX works to automate this self-holstered runner takeover process. So first I use it to enumerate a runner. So now I'm going to go ahead and fix a typo in this test repository here. So I go ahead and fix a typo and make that commit. So after I make the commit, I'm going to go ahead and create a pull request. So I check that I fixed the typo and I make a quick typo fix PR and now that's merged in. So after that's merged in with my attacker account, I'm going to go ahead and delete the fork. So because GatoX will create a new fork to throw the payload at the repository. So I'm going to quickly go ahead and do the web flow to delete the fork and I hit confirm. Alright, now I'm going to run GatoX. So here I've just configured it to deploy a runner on runner on a Linux x86 64-bit runner. So it's going to go ahead and create the payload within a gist and ask for confirmation because the next steps are overt. So I'm going to type confirm and hit enter. 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. 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. So as soon as it does that, it will drop into a nice and very convenient shell. 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. And you can see that output there. Alright, so now you know the techniques and now since I've open sourced it, you have a tool. 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. So there's not really a lot of motivation for people. So what I've done with GatoX is that on its wiki, I've created a little hall of fame. 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. Just make sure it's actually fixed because we don't want the hall of fame to be the hall of odays. As much as some people might like that. 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. 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. 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. Alright, so one thing you can do after getting persistence on a runner is solo run style build compromise. So, you get persistence and then you just have to modify scripts for the build or source code after it's checked out. 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. And here's another way that you could take GitHub release asset tampering a little further. 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. 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. 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. Alright, another technique you can add to your arsenal is something called a post checkout hook. So, what happens when a subsequent workflow that you need to get secrets from or token from runs only very infrequently? Well, you need something that can have these qualities. 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. 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. 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. So, this is going to give you 15 minutes to take advantage of that github token to perform post exploitation actions. 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. So, there's another type of attack called bone requests where you basically get execution within a privileged workflow. That's the same thing that happens with runner takeover. From there, there's different post exploitation techniques you can use. 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. So, kind of cool. And then, there's just too many different attacks to talk about. There's so many ways to achieve great impact. We talked about some of the permissions of the github token today, but there's a lot of other dangerous permissions out there. Some of them don't pose a risk, but others, like contents write and actions write, obviously pose a significant risk. Other permissions we have identified as dangerous and actually abused during our attacks are pages write, pull requests write, and packages write. And there's a lot of good documentation online if you want to learn more about what these actually do. 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. If that looks familiar, it's because that's exactly what we did during PyTorch. Alright, what if you have a workflow that looks like this? It runs on workflow dispatch, but then uses some of that input in a github script or run step. Well, this lets you do good old 90's style code injection. And I'm going to walk you through how this process works. And you only need a github token actions write to make this hop. 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. So, that's going to be our injection point there. See that arrow? So, here's an example payload thrown with a simple request script in Python. 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. 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. 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. If you only have contents write, there's still a lot of bad stuff you can do. You can modify non-protected branches a lot of times, which actually could end up in releases. You can modify releases directly, like we did when we signed my name. You can modify tags, which a lot of times reusable actions are referenced by tags, so that could open up some fun possibilities. And then you can also issue repository dispatch events to execute workflows. Alright, here's a really unique way to take advantage of contents write. 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. 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. Another way is instead of targeting the workflows, why not target the developer? So you could modify a unit test within a feature branch that a developer is working on and add an info stealer payload. So then when the workflow runs, the dev account's compromised and often in organizations, dev accounts compromise can be game over. Another way is if you have these two permissions for the token, you can get code modification in the protected default branch. So there's some preconditions here. The repository has to have the setting to allow get of actions to approve and merge pull requests. There can be at most one reviewer required for that pull request approval rule. And there cannot be any code owner protection rule sets enabled and enforced. When all of these are together, you can have an attack like this. 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. That leads to a good old supply chain compromise. Now, if you're on a runner, it's possible to steal the GitHub actions runtime token from a future workflow. This is where you'll have some attack paths for GitHub actions cache poisoning. I have a blog post on my website that goes into the details of that. 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. Okay, that's the end of some of the more advanced TTPs. 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. 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. It should be able to give you some good guidance. In our opinion, GitHub can do a few things better. 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. 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. If you're trying to defend your organization from these attacks, start with the easy stuff. Please change this radio button so that you require approval for all outside collaborators. We use the GitHub token extensively and almost all of our attacks set it to read-only. That will stop a lot of post-exploitation opportunities. And then use, enforce your devs to use fine-grained personal access tokens. We, like, we hate discovering fine-grained tokens on red team engagements because they only give you access to one repository. If we find a personal access token that's classic, we can do a lot of stuff. If it's fine-grained, sometimes that's the end of the road. Alright, there's also ways to secure yourself more by making sure that your selfless runners are ephemeral. So, GitHub maintains something called ActionsRunnerController, which allows you to automatically spin up and spin down self-hosted runners using Kubernetes. 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. 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. 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. 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. So, it's important to really consider the entirety of that environment when it comes to build agent security. Alright, so another way is by using GitHub's feature for runner group pinning. 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. So, with this, you have a lot of different options to restrict which workflows can even use the runner. And all of this has the ultimate effect of protecting your most privileged runners. We want to end our talk today by emphasizing that this talk and this research is not about PyTorch. So, we have some key takeaways here. If you've been zoning out, lock in for this last slide. 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. So, I think we've submitted over 20 high and critical bug bounty submissions. 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. Additionally, if you think that public CICD security is bad, internal CICD security is a nightmare. 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. It's almost become the new ADCS for us where internally everyone's vulnerable and compromising these CICD platforms can give you extensive privileges. Finally, what we need everyone to do is go learn about these attacks to protect your org from compromise. The main issue we see is just this lack of awareness. 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. Alright, the moral of the story here is learn about this stuff and don't let this be you. 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. We'd like to thank everyone who showed out today. Thank you so much for watching us and helping us raise awareness and for everyone watching online. Thank you. We'd like to thank DEF CON for giving us this platform. We wanted to speak here for a long time so this has been really fun. Please, we don't have time for questions, but come find us after if you want to talk more. Thank you everybody!