Hello everyone, and thanks for joining this talk. Today, we're going to talk about Kubernetes, and specifically, how you can run successful attack simulation exercises. First of all, a few words about myself. My name is Leo. I'm a Senior Security Consultant at WithSecure, based in Manchester, UK. I'm focusing mostly on threat simulation and, generally speaking, collaborative security assessments. So, one challenge that our clients at WithSecure typically come to us with is how can we measure our capability to detect attacks in X environment? And the environments we typically see are of all sorts, Windows, Linux, mobile, macOS, on-prem cloud, and even container orchestration environments like Kubernetes. So, when a while ago, one of our more mature clients were looking to assess and improve their blue team capability against attacks in a Kubernetes environment, we approached this as we usually do, with a three-fold adversary simulation assessment. So, first, we did some threat modeling, just to better understand the risks to this novel platform. Then, we wrote some tooling in order to simulate some attacks at scale. And finally, we executed the test cases we designed, side-by-side with the blue team, researching along the way for means to improve visibility and alerting within the Kubernetes ecosystem. But this research didn't really stop there. It didn't stop after that project. We kept working on it in the background, and this is what we're talking about today. We'll start with a very gentle introduction to Kubernetes. I know this is Adversary Village. It's not a cloud conference. So, if you're not familiar with Kubernetes, fear not. We'll talk about the threats posed to a Kubernetes cluster, and why threat modeling is essential for the success of any security exercise. And then we'll dive headfirst into Kubernetes attack simulation topics. And we're going to look at tools, operational, planning, and eventually execution. And this talk isn't only for offensive security practitioners. So, we'll cover some basic Kubernetes detection concepts as well, to see things from the perspective of the SOC. We'll look at things like log sources, detection engineering, and so on. And finally, we'll demonstrate everything in an end-to-end simulation of an attack against the cluster, using the tool we will be releasing shortly after this talk. Okay, as promised, let's start with some introductions. We do that in order to introduce all the things that will come into play later, both in the slides and in the demo. But the thing is, if you're even remotely familiar with Kubernetes, you will know that it's complicated. So, just a word of caution. This is not going to be an in-depth analysis. It will be deliberately incomplete. In fact, it's probably going to be the fastest introduction to Kubernetes ever given, so please bear with me. So, our system in question, the Kubernetes cluster, consists of a set of worker machines. We call them nodes, right? And these nodes run containerized applications. They are managed by the control plane, over there on the left of the slide. And in order to run workloads, they need to have a container runtime installed, such as container D, or in older versions, Docker, as well as an agent called the kubelet, which communicates with the control plane and specifically the API server to receive instructions. Now, this component that I mentioned, the API server, is in the heart of the cluster. It's the front end that all internal and external parties communicate with. It exposes a REST API, which clients can talk to if they want to interact with Kubernetes. And I'm talking about clients like users or services, like CICD pipelines. But it's important to remember that humans most commonly use kubectl instead of just sending raw HTTP requests. Now, when it comes to running applications, the basic building block in Kubernetes is a pod. A pod can consist of one or more containers whose images are pulled from registries, as well as volumes, storage resources. What usually happens in the real world is that workloads aren't actually deployed as individual pods, but as managed collections of pods, like daemon certs or deployments. But beyond pods and deployments, there's a few more types of objects that Kubernetes understands, or resources, such as secrets or services. And all these resources can then be logically grouped into different namespaces for each different project, which you can think of like tenants in the cloud world. And finally, there's a third layer besides the physical and the logical, that of authorization, with the most common authorization model being RBAC, role-based access control. RBAC defines the potential entities and allows you to write policies for the permissions that these entities will have against resources. So in the context of RBAC, humans are represented as user objects that can authenticate using various mechanisms like MTLS client certificates, Active Directory, IAM roles, and so on. And you also get pods or external services that are associated with service accounts. And service accounts authenticate using tokens, JSON web tokens, JWTs. Okay, that was easy, right? One thing to remember, though, is that in Kubernetes, everything can be replaced. It's built to be extensible. So it is 100% certain that your mileage will vary and you will have numerous third-party technologies running in your cluster. Technologies like the ones tracked in the CNCF landscape, which introduce their own set of risks when deployed inside your cluster. So in order to tailor any security exercise to your environment with its own intricacies, it's essential you perform some threat modeling first. And that means answering a few questions like, what are the assets I must protect? And where would attacks come from? And why would attackers target Kubernetes in the first place? Let's start with an enumeration of the attack surfaces. This will also help us understand which levels we must execute in if we're looking to simulate these attacks. In the most typical scenario, attacks will originate from the container level, the code running within the pod. For example, think of a compromised web service or a backdoored image that found its way into your cluster somehow. And if that pod is assigned a service account, the attacker can talk to the control plane in a limited security context, hopefully, unless they manage to break out of the container. That way, they're on the node, the underlying host, with potential access to other Kubernetes resources from other namespaces and with even higher security privileges as they will most likely impersonate the kubelet. And of course, there's always a threat of attacks from the outside world. Think of scenarios like leaked or stolen credentials, a compromised service, a malicious user, a coerced user, and so on and so on. So once they're inside your cluster, what will they move towards? What's the final objective there? An obvious answer to this one is, of course, the workloads. If Kubernetes is the underlying platform that your critical workloads are running in, then it's a way into them. It's a way into the workload. So compromising Kubernetes could result in unauthorized access to the data. And we're talking about potentially sensitive data that the apps may be handling, like proprietary software, as you can see on this slide, that can be stolen or even backdoored to achieve a supply chain compromise. But actually, that's not the most common incentive. In the age of cryptocurrency, every compute resource makes for an attractive target for its immediate potential for financial returns. A compute, in and of itself, is therefore worth protecting. And you'll see that various instances have been recorded with this particular objective. Kubernetes clusters are usually also deployed as managed cloud services, or broadly speaking, in cloud infrastructure. So instead of going for the applications, a Kubernetes attacker might actually be looking for a way into the cloud infrastructure, into the underlying infrastructure. But we've also seen Kubernetes attack techniques across all phases of the intrusion lifecycle, including persistence methods that could enable APT-like surveillance through a long-term infection, and even for defense evasion purposes, as, after all, Kubernetes is yet another platform where an intruder can dwell in. And usually, it's a less monitored platform, which makes it a great place to hide if you want to evade containment measures. All right, so we know what adversaries are looking for. We know where they'll come from. Let's go ahead and simulate them. Now, there's a few different approaches to go about this. And instead of a stealthy, objective-based exercise, we chose to adopt more of a Purple Team methodology. And I know different people define Purple Teams differently, so let me use the definition given by the guys over at SpectreOps. It talks about collaboration between offense and defense with the aim to increase familiarity with TDPs. And for a novel environment like Kubernetes, we similarly decided it's more valuable to opt for a collaborative engagement and try to help the blue team understand attacker behaviors instead of vulnerabilities. And at WithSecure, we focus a bit more on detection. So when we're planning a Purple Team exercise, we do it in one of two ways, depending on what we want to achieve. So we could either aim to cover as many TDPs as possible, executing them one after the other in isolation, or we could chain a few attacks, similar to how a threat actor would move, to make it as realistic as possible. And even mimic a specific campaign from the ones we saw before. Okay, let's look at these two options in a bit more detail in the context of a Kubernetes exercise. So for Kubernetes specifically, we found that there aren't as many campaigns documented as there are for on-prem infrastructure. And as a result, there's not a lot of threat intelligence available. So when incidents do happen in other organizations, they make for a good opportunity for your organization to perform an emulation cycle, to perform a campaign. In that case, you probably know already which threat actor you want to emulate. But if not, the security researchers over at Wiz maintain this great matrix of cloud threats. And that also covers Kubernetes incidents. So it aggregates threat intelligence from various blog posts, like Unit 42, Aqua, Sysdig, CrowdStrike, all the ones we saw before. And you can collect all this threat intelligence, analyze it, and finally recreate an attack chain and execute this inside your cluster. Now, a nice way of documenting these different emulation plans is Jupyter Notebooks. And we've seen a bit in the demo how Jupyter Notebooks can also streamline execution of the attack simulation. But for more of a one-off assessment, or if you're looking to build a baseline of your detection capability against all the possible TDPs, then you first need a list of all the different TDPs for the platform in question. In 2020, Microsoft released the third matrix for Kubernetes, which was the first systematic categorization of Kubernetes techniques observed until then. And it seems to be actively maintained. There's also a Mitre ATT&CK matrix for containers, but it doesn't really care about the orchestration platform. And I actually got some very good ideas for attacks by looking at KubeHunter. Although it's a vulnerability scanner, it just had a long list of checks supported, so it gave me some very good ideas. But for more technical instructions on how to execute each TDP, I found the KubeNomicon to be a good source, so shout out to Graham Helton from Google for his project. And once you've designed these test cases, one good way of describing them is via a code format, allowing them to be maintained in a version control system like Git. And this is a well-known practice in DevSecOps. You know, attack definitions as code, detections as code, we can apply it here as well. This way, as your Kubernetes environment evolves, so does your repository of attacker test cases. Fantastic. So, we have planned everything. We are ready to get our hands dirty. Let's start bashing kubectl commands. No, that wouldn't scale very easily, but thankfully there are a few simulation frameworks out there to make our lives a bit easier. Starting with the familiar ART, Atomic Red Team by Red Canary. This is out of Citi Village, I'm sure you're all aware of it. Although it does support a few test cases on the container level, it's not really built for cloud environments, so it doesn't really support attacks against the Kubernetes control plane. On the other hand, Stratus Red Team was specifically made for the cloud and for attack simulations, so it even supports eight pure Kubernetes test cases. So it's actually a good choice for this particular purpose. However, you will find it's not very easy to write new test cases, and the fact that it runs outside of your cluster kind of restricts the number of different levels that it can execute in. One tool that can, in fact, run from within the cluster is Pirates by InGuardians. It's also widely known. Various talks, blog posts, webinars out there talking about it. It's so well known that it's actually been used by threat actors. But once again, the attacks it supports are hard-coded, and the tool itself doesn't really allow for customization. So overall, when we were doing this research, we realized that none of these tools performed exactly what we wanted. These options all had some limitations. So we decided to extend our own existing tool, Leonidas, to add Kubernetes support. And if you're not familiar with it, no, I did not write the original version. It's not named after me. That would be a little bit lame. I don't want to talk too much about Leonidas. It's an open-source tool. My colleague Nick Jones originally wrote the AWS version. And again, various presentations out there talking about it. What made Leonidas attractive for the challenge of Kubernetes attack simulations was some of its core principles, like the fact that it was built with extensibility in mind, and how friendly it was to write new disk cases without having to be a developer, as well as the ability to run within your environment and manage its own resources. And these were the concepts that we applied when building Kubernetes support for it. We wanted something running within the cluster, distributed as a disposable image, as an ephemeral image. We wanted something that analysts or threat hunters could trivially write new TDPs for. But we also wanted purple teams to have an easy way to execute them by just interacting with an API, as well as a way for operators and defenders to track execution of each action. And we can do that through the logs that are stored and shipped to the scene, along with all the other telemetry that the cluster generates. Now, alongside this new version of Leonidas, we are also releasing a set of Kubernetes test cases, 17 in total, not only to help you get started writing attack definitions, but also to showcase the tool's capabilities, like running kubectl commands, running OS level commands, and even applying custom YAML manifests. A lot of different options. Okay, lastly, I want to talk about operations teams and how they can apply the security monitoring concepts of the traditional SOC into container orchestration environment. You know, after all, the goal of any exercise is to upskill defenders and build the capability. The most important building block of security monitoring is arguably logs, all right? If you look at the official Kubernetes documentation, however, you'll find there are plenty of log sources available, but no standard architecture for what we call cluster-level logging. This is left to the users to implement. Roughly speaking, from all the different types, let's say we can categorize all of them into three different types of log sources. So first, we have logs from your application, logs from the code, which are exposed to container logs and pod logs. You can collect them with the kubectl logs commands, but they are ephemeral, which means that if the pod terminates, the logs are gone. We also have logs from Kubernetes components, like the API server, the kubelet, the container runtime. Just like container logs, these ones are also stored on the nodes, which means that, actually, in a production cluster, nodes will be disposable. If the VM is replaced, these logs are also gone. So you need to take them out of the host while you can. And thankfully, there's lots of solutions out there, agent-based solutions like Fluentd that can do that. And finally, we have logs that aren't particularly native to Kubernetes, but are still of interest for the security of the overall ecosystem. If your cluster is a managed service from a public cloud provider, then that cloud provider's logging and monitoring solutions are also crucial to monitor, like IAM logs or GuardDuty and stuff like that. And similarly, if you host your own internal image repositories, we found that logs from these image repositories could allow the correlation of certain attacker actions, and that's not even half of it. There's just a ridiculous amount of logs flowing throughout your cluster, but not all of them are useful for detecting attacks. The ones that are, are those selected here. It's those that answer not so much why something happened, but rather who, did what, when, and where from. And these security-relevant log sources map to the execution levels. If you remember from the previous slide, which provides us visibility on the container level, on the node level, and the control plane level. And alongside those, I've also highlighted the cloud IAM logs, because if you're running a managed cluster, identity is always of security interest. I want to take a couple of slides to zoom in on the most important log source, the true MVP of Kubernetes attack detection, the audit log. You can consider the audit log as the access logs of the API server, which simply means that if you're interacting with a control plane, you're getting recorded, with some exceptions. The resulting events can be formatted in JSON, and they answer the security investigation questions that we raised before. Important to mention, though, these are not enabled by default. To enable them, you need to define a node policy and reconfigure the API server, but you can't always do that, for example, in managed clusters. Some events will be very noisy, others will be more important, so you want to have more detail. And that's why there's a few different verbosity levels that you can set for each different type, each different event type. You can easily find a sample policy to get started with, like the one I'm linking in the bottom of the slide, but it's going to take some tuning to find the perfect mix. The API server can also be configured to store audit logs locally or send them to a hook, and both of these ways allow for wording to a scene. Once you do that, you now have the ability to write alert queries upon these logs, which brings us to the topic of detection engineering. In its simplest form, an alert for suspicious control plane activity could be captured by the verb and the resource, such as list, secrets. And this behavior can then be defined in a SIGMA rule for easier management and distribution. We found that there weren't many SIGMA rules out there available. In fact, there weren't any SIGMA rules out there available that operated on Kubernetes audit logs, so we published a few. And since this log source didn't really exist, we also introduced it by contributing a backend pipeline to the SIGMA ecosystem in order to convert rules to actual alerts that a scene can understand. But attacks could not only target the control plane, if you remember from the previous slide, they could originate from within the container. But luckily, there's a lot of research in this area, many solutions available, like Falco or Tetragon, that monitor containers by tapping into the kernel of the underlying node using eBPF. And this enables EDR-style analysis of the syscalls, which then allows the authoring of rules in an easy format for suspicious process command lines, for suspicious network activity, and other system-level events. All right, now it is time for the demo. So in the upcoming video, we'll see how we can simulate a fictitious threat actor who gets access to a cluster and performs some attacks. We'll do that using Leonidas for Kubernetes. And after we run the attack chain, we'll put our blue hats on, we'll put our defender hats on, the Viking hat, and look at Elastic and Falco, which we have set up, just to see what alerts have been raised. All right, let me switch to the demo video. Can you all see the video? No? Good thing I asked. Is it this way? Now I can't see how to maximize it all. Can I have some tech support here? Yeah, but I can't see the pointer, so I can maximize it. You reckon? Nice. The problem is that I can't see it. All right, let's do it. I won't... Yeah, but I want to talk over it. You can switch your display settings, so it's, yeah, duplicated. See it now? Awesome, fixed. All right, so first thing we're going to do is confirm connectivity to the Kubernetes cluster. We'll do that using a cluster info command. We see that, indeed, the Kubernetes cluster is up and running. We also have the dashboard up and running, and you can see we have a couple of namespaces already there, including one called DharmaProds, which has just one deployment in there. It's called HVACController, and once we click it, we'll see that it includes a couple of pods, one looking like a database, and the other one potentially doing something sensitive, HVACController. And now we're going to go ahead and deploy Leonidas into that cluster. To do that, we'll run a couple of generator commands. Generator is an accompanying tool that allows deployment of Leonidas. First, we're going to generate the Python code from the attack definitions that we have previously defined, and then we're going to generate the Kubernetes resources in the standard YAML manifest. These resources will then go ahead and kubectl apply into our cluster so that we can create the Leonidas deployment into our namespace, into DharmaProds. And you'll see that it started running. Once it's completed, once deployment is completed, we'll then expose the port. We're using a kubectl port forward command. KL, by the way, is an alias for kubectl. And we bring it locally to port 5000 so we can interact with it. And once this is done, indeed, we can access the API. It's the familiar API, if you looked at Leonidas before. There are two ways to interact with Leonidas. One of them is via this very simple client, providing the values in all these fields and then hitting execute. But instead, we're going to do it through a Jupyter notebook. And in order to do that, we first need to start the Jupyter service. This is going to open the port 8.8.8.8. Once we click it, we'll see the web UI of Jupyter, navigate to the right directory of the threat actors notebooks, and open our own notebook for OFA-8.15. Now, OFA-8.15 is our fictitious threat actor. Like we said before, execute some attacks against the Kubernetes cluster and first get some access to it. We've got a nice little visualization of it right here. First step for it is to simulate the initial access. Now, the typical scenario would be a leaked kubeconfig file. Actually, before we get to these attacks, let's look at the Defender tech, the Defender solutions we have up there, up and running. We do have Elastic, where we're forwarding audit logs from our cluster. Nicely visualized with the verb resource, like we said before. And we also have some alert rules configured. You can see on the right-hand side that these are all enabled. And just to make sure that nothing has fired just yet, we're going to refresh that page, see no hits, and we're ready to go. But before we start executing, let's also look at Falco. We've configured this on the nodes of the cluster to monitor the containers, like we said before. Cool. All right, let's start running the attack step-by-step then. So if we navigate back to the Jupyter Notebook, we start hitting run through every different cell. And the first step is to simulate the initial access. So the typical scenario is a leaked kubeconfig file. In that kubeconfig file, there might be a service account token, a JWT. And this token is what we're going to instantiate a client with, a client to Leonidas. So behind the scenes, this Jupyter Notebook is running some Python, bare-bones Python. We'll publish all of that, by the way, very soon. And this interacts with the Leonidas service running within the cluster. So the first thing, once the attacker has obtained access, would be to list their own permissions, just to see what they can do. You can do that using the self-subject review API. So we just run that cell then and look at the results. We observe in the results that we have some permissions on the pods. Cool permissions, create, read, update, delete on the right-hand side of the slide. And we also have some permissions on the secrets, which is already a problem. So using these permissions, we're going to go ahead and enumerate the pods. You'll see, like we saw before, patient DB, Leonidas deployment, and the HVAC controller. And using the permissions we have over the secrets, obviously, we're going to list the secrets. With some processing, with some post-processing, we'll see that one of them looks like a password, but we don't know yet. But putting the pieces together, we have the database pod, we have something that looks like a password. So why not run a MySQL dump command to get a dump and prepare it for exfiltration? Sure enough, we provide the password on the command line, the dump is being generated, and we're ready to send that back. Now, before we finish up with this attack, the attacker also tries to perform some destructive activity just to delete a deployment. But this activity will fail because, as we saw before, they do not have the permission to do so. And finally, just to wrap it up, just to wrap up the threat, the Jupyter Notebook will print the table of all the test cases that we executed for good bookkeeping, timestamps, success status, and the name of the test case. Cool. So, we'll now, as promised, return back to the detection capabilities, Blue Team Solutions, and we'll see that two alerts have actually fired, as promised. And the same thing stands for Falco, since it detected the MySQL dump command. Don't worry about the too many lines, it's just a misconfigured rule. Important thing to notice is that it did capture the MySQL dump command executing within the container. And that wraps up the demo. I do have one more slide. Awesome. All right, that one last slide. Hopefully you can see just the slide, not the speaking notes. Cool. All right. I want to close with just a few key takeaways. Now, building Kubernetes' detective capability is a journey, and the first step towards it is to understand the threats posed to your environment. Once this is done, we saw how the concepts and the methods of collaborative attack simulation can be applied to this world as well, in order to build defenses proactively. To this end, we're giving the world a Kubernetes native attack simulation framework, Leonidas for Kubernetes, a set of test cases to get started, along with their corresponding Sigma rules as a building block for detections. And that's all from me. Thank you very much for your time. You can find the links mentioned in this QR code, and you can find me on Twitter if you want to discuss further. Thanks to Adversary Village for having me.