But yeah, let's get the show started. Build to hack, hack to build. Who am I? My name is Chris. I'm a security engineer at Heroku. We're pretty much remote code execution as a service. If you don't know what I'm talking about, go look at the platform and you'll see what I mean. Find me on Twitter, Brompony. But yeah, I like hacking stuff. I like building stuff. I am lazy, so I build stuff that helps me hack stuff quicker. Whether it's web, Windows, Android, and recently I've been looking at containers. They're a lot of fun. So this is what we're gonna look at. Problem, some existing research. I'll introduce to you Bob. So it's Bob with a silent T. And we'll look at some stuff that Bob can do. And yeah, this slide's more for me just to actually remind myself what it is that I actually share with you today. So we might deviate a bit, but I'll try to bring it back in. So what are the problems? So in a modern cloud, DevOps, SDLC, Agile environment, there was one big question that I had to ask myself quite often was, how do we identify and exploit container vulnerabilities? So if you're a pen test, or if you know, you got a shell in a box and you want to break out, you want to pwn some stuff, you know, how do you identify and exploit container vulnerabilities? But then in an engineering environment, how do we test, secure, and monitor our containers? So I come from a red teaming consulting background, and I do more engineering. So I'm constantly trying to break our platform. And when you've got thousands of containers, and you don't always want to pop shells and stuff, how do you do security at scale? So these two questions are very much aligned, but they're pretty much asking the same thing, which is how do we basically identify and pwn stuff, and break out of containers? So in terms of existing research, these are just categories of types of tools. This list is by no means complete, but if you're in a terminal, you got a shell, and you want to find out if you're in a container, am I contained? Identify as the SACCOM, CAPS, LXE, or Docker environment that you're in, and it will tell you, very important. Then there's a whole bunch of Google container tools. I highly recommend you look at their repo. Great stuff in terms of static analysis for container images, be it a Docker or an LXE image. Clay is really good, if you want to look for vulnerabilities in images, it's a really great tool. And when it comes to benchmarking, Aqua Security has done a lot of good work there, if you actually want to benchmark your images and your security controls that are in place. Now you'll see that from this list, there's not really much there in terms of actually breaking out and pwning containers. So, I came up, I had this problem that I experienced, that was the existing research that I had, so generally when I like to solve a problem, I like to just pwn everything if I can, so that's why I did Bob, you can find it on GitHub, it's written in Go, yeah the hipsters were doing it, so I figured let's see why the hipsters are doing everything in Go, I'm gonna, slightly ironic by my hat and beard, but you know I'm in denial. So basically like I said, I'm lazy and I just wanted something, you know when you're writing the same exploits over and over again and doing the same breakouts, you know, you kind of want to automate that stuff. So Bob will give you the ability to auto-pwn, so these kids are gonna love that, sorry I had to say it, but it's definitely a thing. Also helps you do not only breaking out of containers, but think Linux post exploitation, when there's a lot of things that you want to do when you get into it, you want to automate it, so Bob will help you do that. And of course you can do all these things in a pentastic kind of way, so if you want shells, you get shells, but if you also want to, you know, help your engineering environment, so set things up with turn codes and plug into your CR environment, Bob's also designed for that, and I'll show you what I mean by that. So that's Bob, like I said we're gonna be pwning stuff. When you're pwning containers, the hollow world of pwning containers is exploiting docker.sock, it's a Unix domain socket that docker uses to orchestrate basically containers, if that thing is ever exposed, you find it, it is game overs. So basically it comes from people wanting to use docker within docker, don't do that, use that, there's better ways to do it originally, but basically this allowed us to do a local privilege to root and basically break out of the container. It's a really fun, you know, way to get into container exploitation. So how do you do it? What are the manual steps? So first you gotta find docker.sock, so it's a Unix domain socket, it's speaking HTTP, it's basically a RESTful API on a domain socket. Then you want to interact with the socket with curl or a docker client or netcat. Once you find it and you can interact with it, you then want to create a new container and mount the hosts up, uh, file system in your container with the dash v command. Then run that new container and then use chroot or chroot or troot as I found this week, there are multiple pronunciations for that. Um, and there you go. You are then pretty much root on the host and you have access to the host file system. And now you know if it's Linux and you access the file system, everything's a file, it's game overs. So that's the manual step. Now you don't want to do that all the time. Excuse me. But now things get a little bit tricky in containers. Because when you're living off the land, especially in containers, the commands that you need, for example the found command, might not be in the container. So if you're using really minimal images like the docker image or the alpine image, that's the thing, you might not have the tools to actually do the stuff that you need to do. Like what if there's no ifconfig or ipadder. Sure you can read from procfs but this stuff can slow you down. Especially if you need to find docker, uh, the docker socket. It's not always at var on docker.sock, it could be at mu slash bob. If you don't have the found command, you don't have recursive searches, how are you actually going to find the socket? So this is where syscalls come in. So on the left there, those are the syscalls that the found command makes use of. And on the right there, that's what bob does in go code to do pretty much the found command. The nice thing about go is that when you start interacting uh, uh, with the operating system, go makes use of syscalls. It won't actually make use of actual shell commands. Some languages do that. So nice thing is, is that if the command isn't there, you can use the syscalls and the syscalls will be there to allow you to perform the actions that you want to do. So that, that's what we do with bob. And I'm going to show you a demo now. Um, these are pre-recorded. I'm done doing live demos because I just ran out of sacrifices all the time. Um, so what we're going to do here is, um, at the bottom terminal, so I know it's a little bit small, but basically that's our host. And we're just going to watch a directory on the host. Um, so you see there's one file called bob linux. In the top, we're going to run in, in a container. So we're just saying a docker run and then we're mounting a socket. Turn off the light. Yeah, unfortunately that's, can we turn the lights down? Can we? No. Yes. Yeah. Unfortunately I can't make it bigger because the resolution will go off the board. We can just break the lights. Ah. Ooh. A room full of hackers and you just turn the lights on and you get that reaction. It's magic. So we're in a container now and we're going to run bob. And you know, if you're inside a container, you want to find socket. So we're just saying dash socket equals true. And now what Bob's going to do is going to go and look for all the Unix domain sockets. Now you found a Unix domain socket. What now? What do you do? So of course you want to auto-pwn this stuff. So I'm now providing dash auto-pwn equals true. And what Bob's now going to do is it's going to look for the domain sockets that it can find. And it's going to try and pop it basically. And what you'll see now, Bob is now giving me a TTY. So this terminal that I'm in now is not actually a terminal on the host. You'll see that that's a OSX host there. And I'm now going to create a file inside a temp folder and that file is now going to appear at the bottom terminal which is on our host. And we'll see that appear in a bit. So now I'm in a TTY that Bob has provided me. And you'll see that file has now appeared on the underlying host. And within the container. Now once you're done with that, you can now exit Bob and now you're back into your container's terminal. So that's one way that, you know, instead of doing those steps that I mentioned earlier, you can just basically say auto-pwn and boom, your root on the host. Of course if the docker socket is not authenticated or protected, often it's not. So automatically just auto-pwn all that stuff. So it's quite nice to get a TTY on the host. If you want think pen test, you want to get a shell on the host. Also, if you want to test maybe in your CI environment, if there are exploitable sockets in your pre-production staging environment, you can tell Bob to do this and we'll make use of return codes. So what we'll do here is we have a CI environment and we're going to run the same thing. So what's happening over here in the top right, I've configured my CI environment to run Bob and I've provided the auto-pwn but you'll see that I've added the CRCD equals true flag. So what that means is, don't drop into a shell, but if you successfully exploit that vulnerability, set the return code greater than zero. Because if you're familiar with CI environments, fail tests are marked as any test that has a return code greater than zero. So if your CI environment runs a command, a test, and the return code is greater than zero, the test has failed. So Bob will do that. So this is very useful for engineers and for CI environments. And if you look on the left, the test is running and we'll see that Bob picks up a couple of Unix domain sockets, but because they're not exploitable, they're not docker sockets, you'll see Bob's found them, the test will actually pass. So this is quite a nice thing that you can do if, you know, it doesn't make sense to try to get a shell now. Like if you're doing thousands of, uh, push, uh, push requests, pull requests a day, you know, you're not going to want shells in this context. But from an engineering environment, you want to test, you know, are there exposed docker sockets? Is this thing pwnable? So you don't want your engineers focusing on understanding, okay, now I've got a, you know, exploit docker sockets, like, you know, just run it automagically and then you can find out if that's the case. So it's very useful in CI environments. But of course, there's more to pwn, there's more to will than docker sockets. It's called privileged containers. Anybody raise a hand, privileged container in here? Cool. So thank you for putting your hand up, but it's worse when you're at a talk and someone's like, can you please put your hand up if you have, you know, seen X or A or B or C. It's always the worst. But, um, privileged containers are pretty much the worst thing in the world of containers. Why? Cue awkward silence. The case for that is, I know, I just try to hide behind comedy and it's always an epic fail. So privileged containers are basically a, a, a, I was going to say another word there, but it's not a good thing. Um, and it's very dangerous when it comes to your syscalls that you have access to and the Linux capabilities. And this basically means very, very easy container escapes. But now you're in a privileged container, how do you break out? So, I'm going to show you a new feature that I released this week. Um, what this does is Bob makes use of C group controllers and we're inside a privileged container. And what we're going to do is we're going to abuse the C group Linux release agent, uh, capability. And what that means is that we're going to get command execution on the underlying host. This is how C groups work because we have access to the mount syscall and also because the Linux capability sysadmin has not been dropped, we can abuse that. So basically we mount the RDMA C group controller, we then kill off a process and then basically we get command execution. So you'll see now I'm using the pwn group, uh, host, uh, command. I'm saying hostname and I've got a hostname of, uh, docker desktop. So now I'm getting command execution, blah, blah, blah. Fun stuff. Let's get a shell. So at the bottom, I'm on my jump box, standard Netcat listener. The great thing, if you're using Dockon OS X, be aware that your VMs, uh, your containers are running in a Linux kit VM. That VM by default comes with Netcat enabled, of course, because it's got busy box, but of course it's got the dash E flag is enabled. The evil command. You do not want Netcat on your boxes with that. So it makes getting shells really easy. So we're gonna tell it, Bob, pwn the C group, so we're in the container, run this Netcat command, connect back to our jump box, and what's gonna happen is that we're gonna get a shell on the underlying host which happens to be the Linux kit VM. And in an OS X environment, that's basically game overs. So, cause we're running in a privileged container, this is really easy. Trail of bits did a great write up on this. Uh, C group, uh, pwnage, um, it's really interesting. And you'll see now we've got a shell at the bottom, and you'll see we're on a host called Docker desktops. If you see that host name, that is the VM that, uh, Docker for OS X uses. And now we're in the host file system. And if you ever want to find out where your container is living on the underlying host, look at the mount command, and if you look there, you'll see var lib docker overlay 2. That's where your container is living. And what I'm actually gonna do now is I'm gonna create a file in the Linux kit VM, and you'll see that it'll appear inside my container. So I'm in a container, and you'll see the OS does not like it, because there's some Docker weirdness, but you can see hello is over here, and we created it on our jump box. So basically, pwning C group, you break out of the container, and you basically root on underlying host. If you're running something on a Debian system, it's running bare metal, you'll end up actually being root on the host. So of course, if you added the CRCD flag to Bob with this, uh, it would exploit this, but then set the return code. So you wouldn't be getting shells, we can just execute a command that you want. So pretty useful, privileged containers, um, they are out there. But when it comes to containers, um, there's also environment variables, very, very good place to look when you wanna pwn containers, you'd be surprised what's in environment variables. So there's good old env, if you type that in the terminal, that's what you'll get. But there's also the procfs in Linux. Procfs, it, it, it, it's given me a lot of this hair loss that you see over here today. Um, but basically if you read proc and then the PID of the process environment, you'll get access to the environment variables that have been set for that process. Now this gets interesting, because env is not always cleared, and things might be living in proc PID. And this is when, uh, ephemerality becomes a problem. So you might do like a PS orcs and see, okay, there's a process with PID 99, you go read proc 99 in Viron, there'll be, there might be nothing there because the process is dead. So you gotta be really quick when you're doing this. Bob does this really quickly via OS and Viron, and Bob will go ahead and check the env, OS.env, so like typing env in your terminal, and go in the procfs and analyze it. Because sometimes there's the case where the env in your terminal's been cleared, but the proc in Viron, uh, mapping has not been cleared. So sometimes you can find a whole bunch of stuff, because of course, you know, environment variables, it's, you know, the next gen, uh, secret storage, uh, mechanism. So that's pretty much what the Go code looks like if you want to process procfs. It's really nice and minimal. Um, I'm a big fan of Go, um, as you can probably hear or see. But how do we actually go about doing this? So we're gonna run a container, and I should probably press play, that's a good place to start. And we're gonna add in an environment variable. So it's called touch of approval and super secret password. So what Bob will do by default, if you do the recon flag, if you don't pass a word list, Bob will look for password and secret. And you can see now Bob has said, okay, we've now found these PIDs that have this, uh, key term in proc in Viron, and Bob will then look for the process that it exists in. And that's by default. But what you can also do with Bob is you can also supply a word list. So you can tell Bob, you know what, analyze all the processes that you have access to and use the, use for, uh, look for these words in this word list. So we're gonna provide a word list now, and inside that word list were the terms password, YOLO, secret, find me, and Bob went ahead and looked at ENV and in all the processes inside there. Now, this is where, uh, return codes also happen. So if Bob has found a keyword in a process environment that you're looking for, the return code will be greater than zero. That's what I'm gonna show you now. So now you're gonna use a word list where there's nothing inside. And you can see Bob didn't find any of the entries inside the word list, inside any of the processes. And then you'll see the return code now will be zero because Bob didn't find any of those key terms. So that's all fine and dandy. How do we also do this in a CI environment? So you definitely want to scan your environment variables in your pre-production or production environments because you never know what's gonna be lingering there. So in this case, and these configs are all the same on the, uh, CI environments that you're using. So we're using a shell script here and we're gonna be running two Bob commands. So we're gonna be doing the socket auto-po. So we're gonna try to see if we have any vulnerable sockets and we're gonna be scanning our environment variables. And Bob will go ahead and do that on every git push and every pull request or however you've configured your CI environments. So let me just fast forward this. This is, in true Chris fashion, I made too much content for what I have. So I'm like, let's get to the really cool fun stuff. And of course you can't double up speed with the videos that I make. I'm just challenged in that way. So in every CI environment, Bob is gonna look for these key terms. It is regex as well. So you can say, okay, look for S3 tokens or whatever. It's, it's regex. Um, the world will definitely burn at some point. So we can see Bob has gone ahead and run those two commands. And the test will pass because from the previous tests, we know that there are no exploitable docker sockets. And in the container that we are running, now remember in our, in this CI environment, all our tests are running in containers. That's how we configured it. And now we know great stuff that test passed our containers in pre-production. Do not have exploitable docker sockets. And the environment variables that are there do not have any of the key terms that we specified. But of course, clouds, uh, clouds, yeah. Clouds love to host containers. Um, and of course we've got metadata services. So the 169254 address, it exists in GCP or AWS, Azure. And basically if you can get access to this, you can do a whole bunch of stuff. It depends on the platform. So for example, that call request in AWS, if there's an IAM role associated to the instance, you get the credentials for that. It'll, it changes. It varies on GCP. Um, there's a whole, yeah, that is a whole world and talk right there on what you can do with metadata services. Excuse me. So you really want to find these things. But also, if you think Kubernetes, there's also APIs out there. Metadata service is basically an API. And you want to look for these things. Also you want to find out what is on your network ranges for your containers. For example, EC2 classic, still around. If you use EC2 classic, um, and this is from the documentation, your, all the hosts, every client that uses EC2 classic is all on one big flat network. Yes, I know. What year is this? Um, so if you are running anything on EC2 classic, your host is exposed on that 10 range. So, you know, if you're in container environments or any post exploitation environment, you want to find what APIs or what stuff's available. But if we go back to the container quirks now, you might not have nmap in the container. You might not have wget or cull. You might not have the tools that you need to do the um, foot printing that you want to do. So of course, uh, Bob has a useful feature. So you can tell Bob, go ahead and uh, look for uh, metadata services if you don't have wget or cull, um, and all the interesting stuff. So you just add the um, metadata flag and Bob by default will look for two endpoints, uh, kubernetes control plane endpoints and the 169254. So you can see now in this point, in this container, those two endpoints weren't there. But now we're going to tell Bob, use the word list and see if the container can hit heroku.com. Um, and we're going to specify the endpoint list and then Bob's just going to process that list and see, okay, can I issue a successful HTTP request to that host? And there we go. We see Bob says, we got a response from heroku.com and if we look at the return code, it's one. So you can use this in your CI environments as well. If you want to check what the egress is, you want to see, okay, I know that my, my kubernetes or my control planes are sitting on these IPs. Tell Bob to scan those IPs in your prepod or in your production and Bob will tell you, oh, by the way, I can, I can, I can interact with this endpoint, which I shouldn't. And of course, when you're on a host and you want to break out or just pwn more stuff, it's pretty useful when you don't have wget or curl. So there's also binary hijacking. This is awesome because a lot of orchestration environments explicitly trust the commands that they provide with. So if you run docker exec on a running container, docker exec doesn't go and check, am I going to run the legit ls command on the container? It just runs ls. So if you're in control of that container, you can overwrite the ls command to write your command. And we can do some pretty cool stuff with that. An example is kubectl or kubectl, there's another pronunciation. You learn something new in Vegas every year. So with kubectl, it executes the containers or the, the pods, um, tar command. That was recently exploited and used in an interesting chain by Twistlock, I highly recommend. But because as an attacker, you had access to the tar command on the host, you could mess with kubernetes and basically break out. So, pretty interesting. Important to remember that at some point in a container's lifetime, a binary might be executed. By binary I mean command. So maybe the power off command or the IP adder command might be executed in your container. So what do we do? We hijack all the binaries. So be careful, you can really break stuff here, uh, you're overwriting binaries. Um, so it's not too friendly for production environments, um, so I've been told. So, if you want to do this, what do you do? So these are the manual steps. So you've got to find out if you have read or write permissions to burn or sbin or use sbin in the container. You want to create a malicious executable that does something. So either a webhook to verify execution or you want to fuzz something, you can go ahead and do that. You then want to replace every binary that's in sbin or bin with your malicious executable and then basically you just, you wait and uh, you can loot some hosts. So, in terms of payloads, Bob will do this by default. We use webhooks, why? Because sometimes, you know, if you execute, if you put it in a shell payload and then you set up your handler and then it could be one minute, it could be 24 hours until you get a callback. Webhooks are useful because it will, it will confirm exploits ability. In this case, we're going to say, okay, tell me which command was called and what parameters were sent to it. That's how you would do it in bash. If you want a Linux host on a container, you, if you were to write a bash script, that's what it would look like. Um, so how do we actually do this and what do we get from this? So you could get a callback in one minute or five minute or next week, it really depends. Um, I've, what I've successfully achieved with this kind of attack is getting, uh, tokens provided to call commands. So some cloud platforms will say, okay, this client's running a container, it's been 24 hours, let's back up the content of the container and let's push it to an S3 bucket. And of course, you know, it's not an open S3 bucket, they authenticate it as they should. But because they're running the call command inside my container, I've overwritten the call command and actually calling my command and then because they're using a token, they have to give the token to the call command that they're running in my container to back up the contents. So you can find some interesting stuff over there. You can also prevent logic, so often client environments when you hijack binaries, hijack the power off command, a lot of orchestrators say, okay, this person has had two hours of compute, turn off that instance. Okay, cool, but now you've just hijacked the power off command and that orchestrator thinks that they've executed the actual legit power off command, but you've hijacked it, so you might get more compute time. And general blockage, you can really break stuff with this kind of stuff. So hijacking binaries, it's fun. So enough of me talking, let's actually see this in action. So on the right, we're in a container and basically I'm telling Bob, go ahead and use the hijack command and I know that text is really small, I apologize, you can throw stuff at me later about that. But we're telling Bob, hijack all the binaries in the host, in the container, and execute this command. So Bob is going to configure the host such that any command that's been hijacked, it's going to execute a call command and that's going to hit a web end point, which is the logs that you see over here. So I executed the ID command and you'll see that I got a call back over here. I was told, so I'm going to say ID hello, and then my logs on my web server say, okay, someone executed the ID command on the container and they sent the parameters, hello, y'all. So what does it look like if we were to run maybe docker exec on this running container? So that's what we're going to do now. So this could be kubectl copy, execute the talk command, so we're going to run the ID command via docker exec and then you'll see on the left we just got a call back because we've overwritten, we've hijacked the ID command. So that gets a lot of fun, that gets pretty interesting. I enjoy doing that, you'd be surprised as to what you can get. So Bob automates that whole task and it's a lot of fun. But of course, there's more. What about all the CVs and all the lovely stuff that we have in docker? Yes, you did just see shell shock and bash bug and you probably like what year is it? That's actually quite a useful thing to actually look on containers because you'd be surprised how old some infrastructure is and if you, yeah, in infosec you know people don't patch things. So that's a good payload to know, especially in container environments, especially for LXC containers. There's a whole bunch of stuff, that's some really useful CVs, but one of my recent favorite ones is the run C pwnage that came out really cool. Basically similar to hijacking binaries but here you are hijacking the run C binary on the underlying host. That's what the vulnerability allows you to do. So basically from your container you overwrite the run C binary on the underlying host and then you run in the context of that and because it's owned by root you run as root. Now this is slightly destructive, so yeah, I always call this a last resort exploit because you can actually break the underlying run time because you're breaking the run C binary. For docker and LXC it gets a bit tricky, sometimes you need to be a privileged container, sometimes you need root. This is because LXC and docker treat privileged containers very differently. I won't get into that but it's really interesting. And yeah, what you're going to do in this case, and Bob is pretty useful, if you want to verify if a host is exploitable to this because you can't actually trigger your payload being executed. The payload is only executed in this vulnerability when someone runs a command inside your container. So if someone's running a kubectl copy or someone's running a docker exec. So you could wait. So in this case we're going to give a very nice webhook. So we're going to tell Bob, oh I hate cotton mouth, sorry excuse me. And I should probably play, auto play. If only someone invented that by now. So we're in a container in the top and we're going to tell Bob try and exploit CVE blah blah blah blah blah blah blah blah. And the payload that we're going to give is a curl command so we're going to tell if this successfully is exploited we should get a call back on our host. So that will be the terminal on the left. So that's our curl command we're saying execute this. So now we've run the payload. Now you got to wait with this payload. So now to trigger it I'm going to run a docker exec and that's at the bottom terminal on that container. So you can't trigger this. And basically I'm going to run an exec and I'm just going to get a shell. Because you know I want to administer this container that I'm working from. So I'm going to run bin sh and you'll see Bob will exit at the top. So it says finished and now we've got a call back in the bottom left. So now that tells us okay this particular container was vulnerable to that CVE and we now got a call back. So now you could go ahead and get another container on that platform and then you know give your shell payload. So pretty useful. If you want to do that exploit manually there is a bit of work to it. But Bob will take care of that for you. Awesome. Conclusion. Yes and I haven't had my time cut yet because that always happens. So Bob can be used by both pen testers and engineers as you can see. I can use it to get shells um or to verify exploits ability. Uh you can also use it within your SDLC environment so security at scale is really really hard. And quite an important point here is that we are testing for two kinds of vulnerabilities. Immediate, so like getting a shell immediately or we are also getting delayed. So remember the hijacking binaries or the rancy that is delayed. I have ten minutes. Was that ten minutes Jeff? Awesome. This is like, this never happens. I apologize. Your ears must be, I'm going really fast as well. Caffeine, I blame that. And of course it being ten o'clock in the morning. It's like ah get everything out before everyone falls asleep. So you see I digress. Now I need to come back. Yes Chris what were you saying? Feedback, yes that thing. So you gonna be able to get feedback on two kinds of vulnerabilities. So if you hijack all the binaries, you have a web end point, you can get it at some time and also with the rancy stuff. There are a couple other features that I have not touched on today. So there is some stuff that like if you are on a host and you want to push stuff to S3 buckets for data exfil, it's pretty useful. I added that recently to Bob. Um there's also quite a cool way that you can scrape metadata stuff. So if you know if you've ever gotten access to a metadata services that's a real pain to get the actual content. There's a nice trick that you can do on GCP uh legacy metadata services. You just basically connect on port 80 with net cat. Don't send the host header. Enter, enter and blah. All the metadata stuff is yours. So I built that into Bob to do it automatically. So yeah there's a couple other things. It's all on the GitHub repo. So all the documentation is there. Um I haven't gone through all the functionality. But yeah it's all there. You can find it. Uh all the binary releases are there. And yes I thank you for your time. It's early Saturday morning. And yeah I'm going to end it there. Thank you so much. Audience Applause