We are going to hear now from Jeff. But first, a couple of announcements. No smoking or vaping in this room of anything of any kind at any time. If you manage to sneak in outside beverages, shame on you. And if you are going to consume anything that you brought in, then it needs to be water. And that is all. That is all. Yeah? Cool. So Jeff here came in from New York. He is giving kind of an update on a talk that he has given before. He is unfortunately not a first-time speaker. But it's good that he came back and was not terrified into never returning. So let's give Jeff a big round of applause. Have a good time, man. Thank you. Hi, folks. So I'd just like to start by saying that this is sort of a 45-minute condensed snapshot of a hundred and five-minute talk submission. So this is going to get pretty dense. So I'm Jeff. I work for NCC Group doing a lot of research and other hacking stuff. And one of the things that brought me to this topic was the fact that I've done a lot of PCAP packet processing stuff and also dynamic instrumentation. And this kind of work has actually tickled both of those for me. So we're going to be covering a whole bunch of things. And part of, a big part of this talk is setup and then the rest of it is kind of the whole bunch of very, I would say, fundamental, primitive techniques that can be used to build up some very nasty things. So first, eBPF. It is extended BPF. But what is all of this BPF nonsense? So BPF is Berkeley Packet Filter. It is this sort of bytecode instruction set virtual machine that runs in kernel space, ideally, to process packets in plain. And the idea is that when you run TCP dump and you give it a port filter, TCP dump doesn't want to, you know, process through all the packets and you don't, and it's a lot of, it's very expensive to send all of them down to user space for it to print them out or not. So it sends up a little program to the kernel and then the kernel decides which packets based on that program to actually send back down to user space to TCP dump. eBPF, on the other hand, this extended stuff is sort of, takes the same general idea but extends a very limited instruction set to just something that basically is kind of almost one-to-one. with x86. And it's to the point that you can actually basically compile, like, C code down to it. It's used for a lot of things. You can still do the packet processing stuff, but it's also being used for kind of dynamic tracing along the lines of a detrace on other Unix systems. And everything it does is basically done through this BPF syscall. The main two things you do with it are you load programs and maps and the programs are the code and the maps are these kind of internal data structures that map, that allow you to share data between the code that runs in the kernel and code that you have that runs in user space. So eBPF s, instruction set architecture is like, super featureful. Um, and because of that there's a whole bunch of verification stuff that's done in the kernel to make sure that it doesn't crash the kernel or hang it and stuff like that. Um, I'm not really talking about that too much in this talk. I talked about it in my previous talk. We're just gonna kind of go straight into doing stuff with bbh- bpf and simply hope that the, the code works in the first place. doesn't get rejected. So, um, the general idea with this is you create one of these eBPF programs and then, uh, you create some maps, you hook them together, and ideally they run. All of these things are file descriptors on the user land side and, uh, the, the programs will, you get attached them to various facets of the system using very specific APIs and then they kind of get called in line to process the events, uh, when they happen. So, uh, most of the interesting eBPF stuff requires Capsys admin, the, uh, sort of god root privilege of root that really makes root root. Um, but you can still do stuff without it, um, specifically, uh, socket filters. Um, there are other types, but they require other, other privileges to actually use. Um, the main thing within this restricted runtime, uh, that you need, that you do that's useful is you call these helper functions that are essentially APIs to the kernel to actually do heavy lifting that you otherwise can't do in your little restricted environment. Um, and then the code is, is verified, um, both for like loops and stuff, but also to make sure that the arguments you're passing to those helper functions aren't gonna like, you know, crash the kernel or corrupt memory inside the kernel. Um, so why eBPF? Um, why am I doing all this stuff? So, it's got a lot of interesting APIs and things to play around with. It was created for kind of high performance packet processing. Now it's just being applied to anything and everything in the kernel because programmatic logic is the best logic. Um, and it, it really only has kind of two modes. There's no, no in-between right now. There is work on it, but it's not really done and it's kind of very just scratched the surface. So basically, when you're running your eBPF code, it's either running like super unprivileged or it is all of the privileges. And there really isn't, isn't anything between, um, which makes it really hard to sandbox to the point that, you know, if you're actually using this in, in say a container, um, to use it, it, like you have to turn off, certain things are not working. Um, so, um, you have to turn off all the security just to use it properly and then that leaves you a very vulnerable target to, to attack. Um, so why evil eBPF? Uh, because there are a lot of fun things we can do with these fancy new APIs that the people who made them were not really thinking of because they were, uh, trying to move real fast. Uh, so what does this talk about? Uh, shenanigans. And, and what I mean by that is, uh, we're going to be doing things along the lines of, uh, obfuscated, uh, communication channels, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, between processes that are really hard for someone on the system to kind of track unless they're using very similar technologies to follow them and root kits. Lots of root kits. Uh, so, we're going to talk, start with a bit of tooling on what we need to do to get all of this stuff working up and running and then we're going to kind of jump straight into the meat of it. So, you know, we want to build all this stuff, we want to get this eBPF code running, but, you know, we have to, we have to actually have like a tool chain that does stuff. So we have to, you know, set, figure out a way to compile code into this eBPF code. Um, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, um, people thinking about running on the eBPF stuff um, actually get it to load into the kernel. Uh, we have to set up, uh, you know, comms between the kernel and user space code and eBPF code, uh, and then potentially we're also wanna, we're also worrying about um, portability across systems because if we're building this thing, um, to target and run on a system that is not our own we can't necessarily assume that it's going to be amenable to run, uh, us running our code on it. Um, so we want to keep things, uh, very lightweight and small. So, uh, as with many things in Linux, there are a lot of choices to, uh, shoot your various digits off with. Um, so, uh, you kind of have to pick a, a way that you're going to do a bunch of eBPF stuff. Um, and, uh, heads up, I, I went with the LLVM clang approach, but I think it's important to kind of go, cover all of them and what, what they're good at and what they're not good at. So at a high level, you know, you can do raw eBPF instructions, um, by hand using this kind of C macro domain-specific language. Um, it's, it's often used for very simple examples. It's, it's hard to build up bigger stuff with it. Um, you could use this, uh, LLVM tool chain to compile C code into these eBPF architecture ELF binaries. Um, there's a lot of infrastructure to build it, uh, built into the Linux kernel, but it's, uh, it's, build infrastructure's a little slow to get it done, so there are also, uh, tool chains to do it out of tree, out of Linux tree. Um, but then you have to manage all your headers. Um, and then at a high level, there are things like BCC and GoBPF, which, uh, essentially, uh, have a bunch of instrumentation that happens to your C code, and they do a bunch of stuff to kind of make a, a, a variant of C that allows for easier auto, um, registering of things. And then the rest of the code you interact with from either Python or, or Go on the, the user land side. After that, you know, generally at a high level, just interact with eBPF APIs. There are raw syscalls because libcs don't actually ship, um, wrapper stub functions to call these things. Um, but then there's libBPF, which is maintained in the kernel, which basically solves that problem and provides a couple of other nifty things. And then this magicBPFLoad.c, which I'll get to in a bit. So, uh, rawBPF, um, is very unsanitary. Um, I wouldn't really recommend it for building anything complicated, um, but you're going to be able to generate BPF code that the LLVM toolchain isn't going to build and the kernel may not be expecting because it may be expecting the sorts of things that the LLVM toolchain is going to build and not stuff you cobble together by hand. So I'll leave you with that. Um, and then, uh, the LLVM stuff is, is pretty simple, um, assuming you've got a basic toolchain that can kind of just build the stuff for you. Um, I've got a lot of code snippets. I'm not necessarily going to walk through all of them. They're kind of more for reference material. Um, so don't feel like you have to read and understand every line of code that appears in this talk. So, um, I'm going to go ahead and show you what I've got. Um, so I've kind of avoided using the Linux kernel, uh, toolchains just because it's really slow and I, I like to make small changes and rebuild quickly. So I've been using this XDP project, XDP tutorial repo, um, from what appears to be Facebook developers who've been doing stuff with, um, packet processing and I'm not using this for packet processing all that much, but it works. Um, it's a very hackable build system. Um, and then once you actually have your binary, you need to actually load it. So an interesting thing about how this stuff works is that to reference those maps, um, you actually have to take the, um, the instructions and then inline into them, map, uh, file descriptors and then load it. So you actually have to create the maps first, um, and then shunt them into the, the bytecode and then load that into the kernel. And the libBPF and BPF load, uh, do a lot of magic to pull the code out of sections of the binary and pass it up to the kernel. Then there's the high level APIs. They're really useful for things like tracing. They make a lot of stuff very easy. They take care of a lot of heavy lifting, but they also do a lot of magic and it can be hard to figure out what's going on at the deeper layers of these things. And they also require a very extended runtime presence to have access to headers and their own libraries and do things dynamically. So you basically have to get the whole thing on top of a system to use it in the first place. Um, so I, I went with the LLVM Clang approach, um, by modifying the make files of that XDP tutorial repo, I've been able to make statically linked binaries that also statically link in their, um, eBPF ELF file into it. And then I've been using, um, memfd syscall APIs to actually load that buffer as a file path, um, due to how those, uh, libraries want to load it from a file path and not just straight from a buffer. Uh, BCC, goBPF can't really reasonably do this kind of stuff. I just want to drop a binary on, not have like an extended everything. Um, but BCC is really useful to get started with a lot of this stuff. Um, and it's useful for kernel tracing, but it doesn't support all the kernel tracing APIs I'll actually be talking about today. So, let's talk a little bit about doing bad things with IPC. And, and this really means, um, obscuring communications, leading people astray and sending data without sending it and reading data without reading it. So, uh, to talk a little bit about the maps, um, the maps basically are what you use to share data between your programs in the kernel and the user land stuff. But it turns out you actually don't need to attach them to a BPF program to use them. Um, you can just make the calls from user land to store data off, off process. Um, and then the maps are actually, you know, they're just file descriptors and you just make a special syscall to them to interact with them. So, because they're file descriptors, we can just pass them between processes using things like Unix domain sockets or binder if you're on Android and have access to this stuff. And basically this allows us to do a very, um, interesting form of IPC where we'll send the file descriptors across and then, uh, we'll have the other process kind of write into the map and then we in the original process will read from it or vice versa. So, you can basically just have a couple of index, indices in your map, uh, spread out. Um, so in this case we have, uh, two, two slots in the map. They're each 256 bytes. They're indexed by like just a regular unsigned integer. And, uh, you just kind of, uh, associate them with, with one particular process who's writer, reader, um, sender, receiver. And you just write to them using BPF map update elem and you read from it out, uh, using BPF map, uh, lookup elem. And both of these are just kind of wrappers around that same BPF syscall. It just has a bunch of different subcommands. Um, word of warning though, all of the things about these maps are managed by the kernel including the sizes of the values. So, um, if you're blindly receiving these file descriptors from untrusted processes, you're very potentially easily going to run into problems. So, um, if we go back to this example, you'll note that in the BPF map update elem call here, we don't actually pass a size, but we've made sure that the buffer is 256 bytes even though we've only put hello world which is a couple of bytes. The reason for this is that the kernel knows that it, the entry is 256 bytes. It's going to read those 256 bytes from whatever pointer you give it. So, if that buffer isn't big enough, it's going to start reading the values after it and vice versa. When you're trying to pull data out, if your buffer isn't big enough to hold the max value, it's just going to clobber, clobber wherever you put the buffer. Um, or after it really. Um, but, uh, there's a way to deal with this. Um, BPF is very kind of reflective. You can query the kernel for all sorts of metadata about the programs including the size of these things. So you can just dynamically allocate the amount of size that you need. Um, so, the programs are a little bit in interesting because they're actually just sort of single functions and every time you have really a separate function it's actually treated as a separate program. Um, and generally you call a bunch of static inline functions that just get kind of kind of bundled into the one program. And, it turns out, you can actually have multiple of these at the same time the same execution context. You make a special BPF map called a program array and you can fill it with these file descriptors from user space. Then, in the kernel side you can call this BPF tail call function which will essentially just jump the context over and it will never return. So if there is a file descriptor for a program in the map at the indices your index you're trying to call it'll just jump to it and never come back and if it isn't it'll just fall through and keep executing. So the interesting thing about this is that these program array maps can actually be updated dynamically at run time. So you can just kind of keep updating it and each call will just call a new uh function whenever it sees it. Um so what this means is is that we can just pass over some maps to another process. Have them kind of dynamically fill in uh their own programs that will uh take the spots in that program array and then every time sort of the event happens such as like a packet is received um it will call their code which can then send it back to the system and then send a message back to us. And so the idea here is you actually need to send two um map file descriptors. You need to send the program array they're gonna write into and then you actually need to send the map um to to put in that it's going to write to because it doesn't have a global context awareness of the file descriptors up in the eBPF side. So each program is associated with its own maps. They uh they simply can call each other separately through the arrays. Um so in this case we have a very simple program that all it does is call this BPF tailcall. And then the idea is that we send the file descriptors over to the the other process. In our own process we're actually just gonna set up like something like a TCP socket server and we're actually just gonna keep sending packets to ourselves so that at regular intervals we can we can trigger the functionality to run in the kernel and then we'll pull the data out from uh from the map afterwards. The the the thing with this is that why we need the two file descriptors is that you will uh first uh in your regular code that the one uh that the writer is gonna be that uh be dynamically updating um you'll be defining your own map and generally speaking the way that the the libraries work is they'll just kind of create a map for it. You actually don't want to use that map. You have to actually go through that code after the fact, iterate over it and shunt in the the actual map that you want to be writing to which you got over the Unix domain socket or binder or etc. Um and then reload that program and then you put uh you use BPF map update elem to shunt the program's file descriptor into the uh the program array. Um and so it's a demo for this. Um basically to to start we have a couple things going on here. I'm gonna briefly explain. On the right side we have the code that actually does the updating um and the iteration of um all the functionality uh so it's it's iterating through the instructions and every time there's one that would have a map file descriptor it puts in the map that the map file descriptor that's received and then at the end it does uh it loads the program and then it updates the uh the uh the uh the uh the uh the uh the uh the uh the uh the the map entry. And in the middle we have the uh the actual code that's gonna be dynamically updated injected in. It just prints out uh DEFCON 27. And on the left uh we have the original programs which are the main entry point and then kind of this fallback implementation that prints waiting until uh until it gets overwritten. And so in this example uh we're just gonna s- uh start the thing up and uh the first couple times it's got that waiting printing out and now it's been updated dynamically after the fact. And so this is the first time that we've been able to do this essentially allows us to uh do a bunch of very uh dynamic updates so uh uh so that we can just sneak data in in places that people aren't gonna be looking. You could do all sorts of other stuff where you could actually have data that's being sent over the socket that then is being uh then is being uh potentially transformed in kernel and then written out as the real message to the BPF buffer. So if someone's just looking at what's going on in the socket it that's not actually the data that the application is really operating on. So we're just looking at what's going on. Um so to talk a little bit more about the sockets, the socket filters, um they are really special. Because they are the only ones you can use freely um from unprivileged processes. So um I say this unprivileged, docker actually blocks the BPF2 syscall unless you're running cap sysadmin um for whatever reason. But in general this is an unprivileged uh call and doesn't require anything. Um it's also super poorly documented as it turns out. Um so I'm gonna go ahead and show you a little bit more of what's going on. So I'm going to show you a little bit more of what's going on. So I'm going to show you a little bit more of what's going on. So I'm going to show you a little bit more of what's going on. So privilege processes can create um like raw IP sockets. This is you know what ping does to send out those those inet packets over the internet raw. Um but without privileges you know you can create normal TCP UDP sockets, unix domain sockets, either stream sockets or datagram. When you attach a socket filter program to it uh either the eBPF or the classic BPF um different things happen. So for the raw sockets you the the program will actually see the packets from the beginning of their ethernet frame and for kind of the regular IP based ones like TCP UDP it'll see them from the start of the transport header. So the TCP header, the UDP header. For unix sockets it actually just the the start of the the array into the buffer is actually just like the data payload itself. Additionally while these uh socket filters can't modify the packets directly they can drop them uh which would just completely break TCP. Um but they can also truncate them. Um and so that can cause that can cause very interesting things and you can potentially build a very complicated uh set up that might fool Wireshark where you say uh drop a packet that contains like the real message and then uh with a modified uh TCP stack on a remote host you have full control over you send another magic packet that will actually be accepted and then when someone tries to in Wireshark reassemble the stream uh they see something different from what your application actually processed because it was reading from the internet. Um and so that can cause the packets to be taken from the packets and not from the socket. Um so this leads to an interesting uh just attack in general like you can also read without reading. Like you don't actually have to call, read or any receive sys call on the socket because these events get called every time the packet is received not when the userland process actually tries to read from the socket. So if you just set up a socket server and you never actually receive on it you can have someone send you data and you can actually take your packet and enter it into the socket. and then if someone's uh trying to look at your data not through like TCP dump but through like strace they won't see the data because strace actually doesn't print out the strings that are passed over the um the BPF syscall for uh reads and writes as it turns out. They'll just see a random pointer and they won't know what's going on. Um so it's fairly simple to do this you just kind of register your socket filter to it and then every time that a packet comes in you just write it down to the map that gets read out every so often by the userland process which then never just it never calls read. Uh you can also do this in the reverse direction. So you can uh you can write to things and then block the data from actually going out such that it won't actually send packets but every write will then be shunted into the the memory in the kernel and then depending on if you've already changed this with like the map communication you can then leak the data back out to another map file descriptor every time you hit write and and anyone who's on that computer will like be none the wiser about it. Um you can also use these techniques using other BPF programs that can actually write to the packets. So they might be able to hide data in and then take it back out so that everything looks normal at the userland level but like what actually goes over the network or what doesn't go over the network is sort of some secret data that's hard to see what's going on. Um but they they require privileges and you can do all sorts of other crazy stuff with them too. So as a demo of this um I have this uh piece of code here that is that runs a regular TCP socket server and it never calls receive, read, receive from, receive message all of these kind of standard sys calls for reading data from a socket and on the right side I just echo into like telnet to the to the service. So I'm running strace on this to prove that like no none of these sys calls are actually being issued. And so if someone were looking for these they just would not see anything happen. So we've now like we get the data from this the the forty one forty two forty three that's all just hex for you know capital A B C D et cetera and uh the signal stuff that goes on at the top is just because that's how I'm uh doing the I'm checking I'm pulling at the uh the eBPF buffer I have a signal handler that pulls at it like once a second. So I have not actually if you look at this I have not issued any of these standard reading sys calls in the socket and yet I have somehow obtained the data that was sent to it. Um you can do all sorts of stuff to build up on this to hide data from people who depending on how they're trying to um you can do all sorts of stuff to look at what you're doing. So let's talk a little bit about the um the kernel tracing. So eBPF uh basically supports a whole bunch of modes to do dynamic instrumentation of kernel functionality so that you can kind of see the data that's flowing through. Um this is very privileged um and can be used to you know compromise systems as it turns out. But uh you know these things can read arbitrary kernel memory and user space memory and this presents us an interesting opportunity for a covert IPC on a system we've really kind of posed. So we can actually just read data out of processes that they were never actually going to send to the kernel. They never attempted to send it there it was just sitting in memory and they're not issuing any sys calls. Also what we can do is they can issue bad sys calls that are going to get rejected and the data is not actually going to make it through the kernel so like if they do a send on a bad file descriptor it's not going to result in a packet and so tcpdub's not going to see it. But we can. So we've got a lot of data that's going to get rejected. So I uh I like to do this mind reading trick uh with the code uh where uh because close is kind of a magic sys call it it takes a file descriptor and that's it. File descriptors have to be non-negative they're non-negative integers. So anytime it receives a negative integer it just rejects it and doesn't do anything and it's kind of idempotent. So uh what you can do here is you can hook closed and you can just wait for like certain magic negative file descriptors to start a stateful handshake with a bit of code. So uh I like to do this particular um process and uh after that just start unmarshalling data out of these negative file descriptors to communicate with with yourself basically. And you know if someone's looking at this probably they don't really look at closed. If they see it they might think something's wrong but are they really gonna like attempt to figure out what's going on here? Maybe maybe not. So that's a neat trick but like this stuff can also just absolutely corrupt memory. Um so there is this special BPF probe write user helper function uh which I talked about in my previous talk and this is sort of the thing that gives us rootkit capability. So when you use this it raises an event that like goes out on dmessage but it's so useful. Um you can also when you're doing these things you can actually abort syscalls at entry. Uh syscalls and a couple of other um special functions in the kernel that have this special macro that kind of denotes that they can be um aborted. But basically this BPF override return allows you to just bail out of actually executing the syscall so the kernel just doesn't do it. And you can also give an arbitrary return value. So um you know we need to write things that are useful. Um and most of the interesting uh data that are in syscalls are pointers to user land memory. Um so we can actually potentially overwrite strings and other structs that are being uh sent to the kernel or written back by the kernel. Um and uh we can also just prevent syscalls from reaching the kernel. So there are a couple of of high level uh uh things that we can do that are useful. Um one is a variance of of this attack. Um this kind of way of building a root kit. Um you can redirect syscalls. You can modify them. You can make opens for a like a a a shell file, a shell script open a path that is something you control somewhere else. So it just opens and reads your code. You could also every time they read you could just send bad data back to it. Um and just lie. So you can uh after the kernel has written the code you can you can stomp over it with your own data. You can also just fake the return by aborting the syscall. Um and so you can um you can call and write the data yourself. And you can also just completely black hole the data so they just can't communicate with the outside world. Um so in the first one where we're going to be modifying the data that's being sent to the kernel. Um generally speaking the way this works is you set kind of a tracing hook. A k probe is on the syscall entry. A k rep probe gets called uh on the syscall return after the kernel has like processed it. And generally speaking uh you know when you're when you're hooking these syscalls all the calls across the entire system regardless of container make it they're all sharing the one kernel. They're all using the same syscalls. All of them go through that. And so you potentially are having a lot of throughput going through there. And so if you're going to be messing around with this you actually want to be very careful you're not going to crash stuff. So you want to you want to kind of filter through and determine if the particular process that's calling in and it's particular inputs are stuff you actually want to mess with otherwise you might just crash the system accidentally. Um when you do this uh you could just you could just overwrite it and then be done with it. Um and then you can deal with it. But if you want to be sneakier you actually want to persist the stack data or or or keep data wherever it was from the uh the user land process that's being sent up. Um so that on the return after the syscall is has finished uh like processing but before context returns back to user space you can actually try and write back their data to it so if they checked it after the fact it would look clean like nothing happened. And the way that you want to do this um is you want to use the process ID uh thread group ID and then you want to use the process ID thread group ID and then maybe a file descriptor as well if you're keeping track of it because uh all of this is essentially stateless other than the data that you maintain in your BPF maps. So you kind of can't maintain context without using those maps and the only anchor point you have for context is this kind of very basic information about the process and kernel thread it's using. Um but that's re that's good enough actually as it turns out. Um and the the opposite approach where you're just modifying the data you do basically exactly the same stuff um word for word but uh you you do your filtering to determine whether or not you're going to do stuff at the entry point because the entry point is the one that actually receives the arguments and at the end um you can either have persisted the arguments and then decide at the end if you want to do something or not or you could have just decided at the beginning. But it's at the end after the syscall has been processed that you need to write the data because otherwise if you overwrite their stuff the kernel is just going to overwrite you again. Um and then nothing's going to happen. Um but other other than that it's basically the same exact approach. Blackholing everything is fun uh because you you can just block the syscalls but you can do so much more than that because you can just pretend to be the kernel essentially and write in arbitrary data as if they had succeeded. And so they think they're communicating out they think they've alerted like the IDS or or whatever it is that something bad is going on they think they're writing out to like their their log stash or whatever it hasn't happened it's not happening. They think it's happened it isn't. Um so we that's that's really useful depending on if you're trying to like reverse something and you don't want to let it kind of communicate out whereas like if you're attempting to use seccomp to deny stuff you know it's going to know that it got rejected and it's going to be able to act accordingly. Um so there's one other limitation to this probewrite user call. Um you can't write non-writable pages. So you know we can't just clobber the text section with shellcode unfortunately. Um at least for like properly compiled programs. If they've got bad. Bad protections on their sections then or they jit stuff a lot. Um things change. Uh but we can't assume that for all processes. Um so this limits us to kind of what's in the stack the heap other sections that are writable. Um things like function pointers uh save file descriptors. Maybe they have they generate scripts like dynamically or shell commands that we could write into. Um things like that. But we can't we can't guarantee that these things are there. The only thing we can really kind of at a high level guarantee exists is they have return addresses. Um mostly. So I put together kind of a very uh concrete way of of writing ROP payloads into user land processes from eBPF um kernel tracing stuff. And this is kind of the stuff you want to do uh you can do for real real rootkits to get in like PID1 which is what I did in my previous talk. But uh to really cover the whole thing there's a lot of things to do. So you can either start with kind of one of two ways of looking at it. You can say that you want to have one payload that you want to indiscriminately inject into all processes. Um and then you can say that you want to do that you need to kind of have it be working on something like a shared library that they're all going to load like their libc. So glibc is a great target for this because it's got all sorts of wacky functionality built into it. Um like a dlopen implementation which allows you to dynamically load a shared library from a file path. Um and the moment a shared library is loaded that actually gives it arbitrary code execution right then and there. So all you have to do is scan for some gadgets put it together um bada bing bada boom you you have code execution if you can get your ROP chain to actually like load it into a shared library. Um so that's what we're into a process. Um very reliable um they have every gadget you could ever want to make this across every version of the binary I've looked at across multiple different distros. Um then you know you wanna do your ROP payload but maybe maybe you're not gonna be dyna- you're generating like this maybe you're dealing with a static linked binary maybe you're dealing with one that does code gen and where you're getting called from isn't necessarily stuff that you can really know what's going on easily so um I'll talk a little bit about dynamic uh generation of of ROP gadgets and things. So when we do this eBPF stuff we need to you know pick some syscalls that ideally our target process will be calling and we're gonna register up some kprobes krep probes on it um just so that we can get stuff we can get a user land context to be able to get the ability to one have pointers to uh to their user land and also actually be in the right kind of processing context to write the data to them. Um so then within the code much like before we kind of need to sift through and uh see if there's anything that we can do to see that the particular calls are actually coming from processes we want. Um the only system you could very easily the only process you could very easily detect is PID1 because it's PID1 across the entire system regardless of containers and namespacing and things like that the real PID1 is always PID1 for real. Um so it's a very easy target. Um but other than that you know if you wanna know that someone's doing something in a specific uh specific process so if you're gonna change the world view of a process um and not target others you kinda need to watch what it's doing and build up some state about it to know what it's doing. So that's kind of the first step and then you know that it's actually the one that you wanna hook. Um then there are two step threes here on purpose. The first one is when you're running this kind of pre-generated route payload and the second one is when you're gonna try and dynamically generate it on the fly. So in the former there's in your your kprobes uh when they get called they actually receive all of the user land uh registers that were uh that were the state of the registers when the syscall was made to trap up to the kernel. And so we can just like pull out the instruction pointer um and and we know that that is probably that's gonna be made in the libc somewhere so we know exactly the offset from the start just by what the address of the syscall instruction was. And like then we know exactly where we are. Um if in the other side depending on kind of how ridiculous the binary you're dealing with is um you could do the same thing but the the instructions at where it's executing may not all like be that useful. They may be you know in the heap somewhere where there's not actually a whole bunch of code it's just a bunch of jitted code. Um and so you probably wanna uh scan through the stack. And so you can do this uh you just kind of look at valid stack offsets from where the um the stack pointer was uh stack pointer register. And then you essentially wanna look back and see if the thing that the like the former instruction that would have been um the one before uh so when you like in x86 when you make a call instruction um it's going to increment the uh the uh the uh the uh the uh the uh the uh the address and then put that in the stack. So what you return into is the next instruction. We need the previous instruction that actually issued the call. And so we can then try and detect what that instruction was. And if it was a call and it actually looks like it went to where we are then everything's good. But it may have actually been a PLT entry. And those are actually uh those are like the entries to dynamically linked functions like your libc. And so those um are the call will actually go to a jump. And the jump will go to where where the the code that your you were it was formally executing before it made the syscall was. And so you need to just parse those instructions a little bit to dump out kind of the offsets and things. And then then you know where you are. Um after that in both cases um you can just uh go backwards as far as you can until you get page faults essentially that are free for you and don't crash anything. And then you know where the memory region started. And then you can just scan the data straight forward and dump it all out until you again reach a page that's not read writable etcetera. After that you potentially are going to uh generate your uh your payload based off of this. So you've you've dumped out all of the raw memory of the process. You know exactly where it sits. Um you can just attempt to find gadgets and build them into some generic payload that does whatever you want it to do. Um you just need to make sure there's a clean up routine. Um after that uh we go back in with another hook and we need to do the stack skimming stuff again because we need to uh find out uh where we were so that when we we write in the raw payload this time it's actually at the right place so that when the syscall returns then the kind of syscall stub will then attempt to return back in user space and it'll jump right into a raw payload. Um before we do this though uh we need to back up the memory from user space because we're just gonna clobber all of it and we need to return cleanly so that thing doesn't crash after our code runs. Um and so what we do is we we not only need to get the general stack space we're writing to we also need to kind of back up all the space we're potentially going to clobber as part of a ROP chain itself. Um then after that we write it in we return the ROP chain starts executing and it's it's done it's whatever it's magic task is that's up to you. Um and then it starts it's coordinated clean up routine and what this needs to do what what I like to do with this is I like to do one of those um hooks on close to signal that I it should uh the kernel side should write back most of the stack. Not all of it because we have a couple of ROP gadgets we still need. So the last ROP uh as we do this we're actually gonna write some new ROP gadgets that we need to back up. So we're gonna write some new ROP gadgets past the end of the stack um and those are gonna help with our clean up routine. The remaining ROP gadgets that are still there that we haven't overwritten with the backed up data uh those will then execute once we return back to user space and they'll exist simply to shift the stack pointer to where the new ROP gadgets we we put are which weren't actually in what what's considered the stack until we shifted the stack pointer. The new ROP gadgets will then exist to kind of overwrite and uh restore the backed up data to the the last ROP gadget that we didn't overwrite and then it will then be able to uh set a return value for all the way back to the thing we actually hooked on um and then it will uh uh shift the stack pointer back to where it needs to be and then the code will return back into whatever it actually called the original syscall wrapper in the first place and everything's clean and it it never know it didn't know what happened. Um but there's a limitation on these these kprobe krep probe tracepoint APIs. Um they all use the sysfs uh an app which doesn't have the sysfs but it can be used to do a lot of other things as well. Um it's a special mounted file system that does a lot of magic stuff on Linux. A lot of things that're under it. Um but docker by default has an app armor profile that blocks access to this and docker also doesn't by default mount it as a fully writable um mount which you otherwise need to do this this kprobe tracing stuff. However EDPF actually is another type of tracing program that doesn't interact with the sysfs at all. And there hasn't actually been all that much tooling built on it yet. Um even bcc um the the main like instrumentation um stuff. And so that the uh the framework doesn't actually support these. Raw tracepoints. These are magic. Uh and uh it's got a very complicated process for setting it up. Step one. You call this b- the BPF syscall with the raw tracepoint open subcommand and you literally just give it the name of the registered tracepoint event you want to attach to. That's it. That's it. That's all it is. Um you also you know have a BPF program that you've loaded and then you know put it in there as well but like that that's we've already done that before. Um so Eminem once once said uh and Moby you can get stomped by Obi. Uh Moby is the sort of upstream open source code base that Docker is built from and so we're gonna break out of that um in a way that otherwise hasn't been displayed publicly um because everyone who's been showing uh Docker breakouts using BPF stuff that isn't otherwise exploiting like a kernel vulnerability has basically turned off app armor to do it. Um which like we don't need to. We'll just leave it on. I don't care. Um so this is a slight modification of the previous ones we've been talking about. Um which is necessary because of the way the raw tracepoints work. You can't actually trace arbitrary syscall events with them like you can with kprobe. Um they'll probably add it in the future. They just only did it for regular tracepoints not the raw tracepoints. What we can hook on are these sort of primordial syscall events. Sysenter and sysexit. Uh which essentially get all of the same data anyway. It just means that we need to reimple- implement the hooks. So instead of having like a separate BPF program for each like function return, we just have one for like the entry to all syscalls and one for the end uh the return of all syscalls. And we just use like a switch on top of the uh the syscall ID and just handle it differently. So we can write essentially all the same code. Um and it all just works. And even though we can't necessarily have a very fancy DSL that gives us all the arguments as actual like function parameters. They're all just in the registers anyway. So we get the register state. You know RDI, RSI, RDX, et cetera. And your standard AMD 64 calling convention. Like those are the first couple of of arguments past any syscall. So um this is very easy. The one thing that you need to make sure to do is again you need to save that state. Because in the return we have like absolutely zero context other than that we know the particular um process ID, thread group ID again. So in this particular case we're just going to serialize like all the register state and the syscall ID and then we're going to shove it in there. We're going to index into this uh map like uh it's a it's a hash map data type so we can we don't have to be like regular indexes from zero. We can just use the um process ID, thread group ID in there. And uh and everything's good. And then in the return the first thing we do is we just see if we even have a state associated with us. Because if we don't then we didn't even attempt to do anything on the entry. We don't care about the return. We just bail out and let the let the syscall happen. But if it's something that we want to be dealing with then we're actually going to start processing again with another switch on top of the syscall ID and then we're potentially going to process the data. Um so my example here I'm doing uh basically the same sort of uh using uh the uh the syscall hooking stuff uh that does I'm not I'm not just writing into memory um like to do a ROP chain. I'm just overwriting um say file IO uh without things realizing it. So in this particular case um I've started up a docker container. The only thing I've added to it is the sysadmin capability. Um that's it. I haven't turned off app armor. I've got a binary in there, I'm about to run it. It's just got some some payload there. I'm going to cat this file that otherwise doesn't exist. And mind you I'm catting it on the outside. Um then what this payload does is it actually uh overrides crontab whenever any file tries to read it. And so basically we're going to now load our code and now when we try to load crontab now we have our own thing, our own code in the top level cron tab of the host outside of the docker container. So now we're just going to wait a little bit for cron on the system to pick it up and start executing our code and then now um we've we've completely successfully broken out of the docker container using nothing but capsys admin. We haven't turned off any of the actual security mechanisms. Um we just gave a regular privilege that people do whenever they're going to do like fancy BPF stuff. Um we've just broken out. We're done. Like we've escaped. There we go. So what can, what can we do to defend against this stuff? Um you could remove or blacklist this BPF2 syscall entirely. Docker sort of tries to do this when it's unprivileged but the moment you add capsys admin it dynamically updates it's syscall filter that uses seccomp BPF to actually allow it back in because you need to do stuff. Um unfortunately it's not going to work because modern Linux systems are increasingly relying on BPF stuff. Like system D relies on it so if you're in it you're not going to be able to do anything with it. You know you don't want your init to crash right at the beginning of boot because you've decided to you know mess around with your kernel. That would be bad. Um so and then you know you need to really be prepared for what someone can do when they when they have access. So you can actually just log all of these eBPF programs. Um using that same API that we pulled out um the sizes of the maps. Uh if you're privileged you can actually just dump out the content of the maps. You can dump out all of the code of the programs as uh when they're loaded. Um BPF tool uh is a utility to do this. It's it's very simple. Unfortunately because they all use the BPF to syscall um they are susceptible so someone's got one of those K probes in there. They can start overwriting the response data and lie to it and they can hide what's actually going on. So once it's in there it's kind of it's still game over. Um but we can actually ourself use tracing um to see things that maybe are bad. So we can look for when like there are eBPF maps that are being transferred in uh processes. We can look for uh you know eBPF maps that aren't actually associated with eBPF programs. We can look for uh you know we can look for just when eBPF programs are being sent between processes. I didn't talk about this too much because it's it's kind of it's a one to one. So every time you want to send a new message you have to kind of send a new file descriptor over. I'd rather have like a one setup um in the example with the call where we just send a couple maps over and then no one sends any magic file descriptors to each other. They just magically appear and start executing. It's much sneakier. But you could you could do the other the other way around. And then you can also just look for when there are unexpected uh eBPF programs being attached to things that they shouldn't be and when there are unexpected eBPF programs being attached to things that they shouldn't be and when there are unexpected you know eBPF tracing programs that are being added that's probably a sign that something bad is going on. Um but it's honestly it's unclear how much more common these operations will get. So they may not be as kind of um anomalies in the near future as they are now. Um so the more APIs that we have the more problems that we have uh because there's more kind of chicanery people can do with them. Um and even the unprivileged APIs uh can enable really like screwy behaviors to evade people who are trying to see what what's happening on a system. And once you get the privilege eBPF it's like impossible to stop. Um and honestly a good number of these APIs really shouldn't even require these privileges in the first place. There is work being done that to kind of change this. But like they they shouldn't you shouldn't have to require Capsys admin to do a lot of things that really don't require it. Because a lot of them are essentially analogous to raw packet IO. Um so they like they shouldn't require that. Uh and if you do that makes those programs a softer target because they then need to be less and less sandboxed to be able to work properly because of all the stuff that gets layered on here. Um I'm personally waiting for a special eBPF map type that allows us to just generically pass file descriptors across processes but I I doubt that one's gonna happen. Um I'd like to thank Andy and JKF uh for the help uh with a lot of this research. And a wise man uh once said you can't hide secrets from the future using math. I think a simpler version of this that also holds true is you simply can't hide from the future. Uh I don't have time for questions uh but I'd be happy to talk about all of this stuff in the next few minutes. Thank you. Another research I'm doing at NCC group. Um somewhere else uh feel free to find me. I'm also on Twitter. Um happy to talk. Um thank you.