Cool, okay, well, welcome everybody. We are going to be giving the talk Exploring Linux Memory Manipulation for Stealth and Evasion, Strategies to Bypass with Only No Exec and Digitalized Environments. Probably the title is self-explanatory, but what we are going to be doing is to abuse Linux memory in order to bypass some file system-based defenses, and also to get RCEs in, well, to try to make it easier to pivot where you are talking about digitalized environments. We will explain what this is if you don't know about it. So, for the people that read this title and doesn't understand a thing, don't worry, because we are going to be starting from the beginning. We are going to be explaining to you why we developed this technique and also how it works, so you should be able to follow everything. Also, we have a lot of demos. For the people that are already experts in things related to this, I will ask you to wait to the final firewalls because things are going to be getting harder and harder, so we are going to start easy, and then we are going to start doing the complicated stuff, so just wait for the end. Also, as you know, this is our first DEF CON. So far, this has been a great experience. We are amazed with the community, with the conference, so I would like to start this talk asking you for a round of applause just for DEF CON because this has been a really great, great conference. Cool. So who am I? All of you know about DEF CON, I'm sure. So I'm Carlos Polop. I'm technical leader at Halborn. I have a lot of certifications. I was the captain of the Spanish team in the European Cyber Security Conference a couple of years ago and also a member of the winning European team in the International Cyber Security Challenge last year. I'm also the author of Hack Tricks, Hack Tricks Clouds, and Peace. Thank you. And I am just a lost nerd in the wilderness. I'm still studying telecommunications engineering. I love Pwn. I love C, assembly, and Linux internals. And I don't think I have much more to say about me. So let's continue. He's great. OK, so what are we going to be talking about? We will start with an introduction about what the technique. We are going to present you the initial technique we developed called DDXSec. And then we are going to do a lot of demos. All these demos has been already recorded, but we are going to be doing all but one live, because we really like to do things live, even if they can fail. So let's hope it will go OK. And at the end, we have a surprise. We developed a new technique that we weren't expecting to present in this talk. But Diago, yesterday, finalized the initial step. So we are going to be doing a demo. And we will expect that it works. Yesterday worked most of the time. So let's see. OK, so cool. Everything started maybe two, three years ago. And the thing is that I was working in this company. And they decided to move all the containers to digitalized containers. And my boss asked me, hey, take a look to these new digitalized containers, because it is set. And he said that it's very complicated to compromise, even if there are vulnerabilities in web servers. In these containers, it's very complicated to compromise them. So take a look to them, and let's find out what an attacker could do in these kind of containers. So the question was, is digitalized the new hackable system? The reality is that in that time, I didn't know much about digitalized, also about containers. So when I started taking a look, I discovered that I don't even have a cell in these containers. I don't have a sage. I don't have bus. So it was like, man, even if someone finds a remote code execution here, they are not going to be able to execute cut, to execute the layers, to execute who am I. They cannot execute a thing. So to me, it looks like pretty legit. It's adding a lot of security. And I didn't know a way to bypass this at that time. Also, I started playing more with containers, with Kubernetes. And in Kubernetes, you have this easy flag that you can set to true, which is read only, which will make the file system read only everything except one of two folders that have the noexec bit, which means that if you manage to compromise a readexec container in Kubernetes, you're not going to be able to download binaries and execute them. Because in the only place where you can write, you have the noexec bit. And you don't know how annoying it is, as a red teamer, to compromise a Kubernetes container, see this Kubernetes token, and then being able to download kubectl and execute it. Of course, you can use KURL for insight. But you need to use KURL to contact the KUBE API, the API manually. And it's just annoying. So yeah, it makes your life much complicated. And at that time, I thought, hey, OK. So I cannot write in the file system. But what happens if I could load something in memory and execute it? There is nothing preventing me from doing that. And I knew at that time that there were a lot of different techniques for doing this in Windows. It's super easy to do this in Windows. But what about Linux? Well, there weren't so many. And the words that were in there weren't that reliable. So at that point, I just started my conversation with Diego. And well, he came up with a very strange idea that ended up working perfectly. But before getting into this technique, let me tell you about the state of the art in that time about how to load things in Linux memory and execute stuff. At some point, we found this blog from Sector 7 that told you that if you manage to write things in memory, you could use this cell code using the syscall memfd create that will allow you to create a file in memory that is readable from the file system. So you could create a file in memory, load your binary, and execute it. This will allow you to bypass these protections. And also, while we were developing the technique that now Jao is going to explain, we found that David Buchanan was doing something very, very, very similar, but just smaller. And actually, he was using a couple of extra techniques that we ended up implementing in our final technique called DDXX. But at that point, that was the state of the art. You couldn't do much. And it was kind of complex. And dark, how to do this. So we came up with DDXX. So the idea that I had at the time was to trick DD into overwriting its own memory through the mem file in the Procfs file system. It turns out that somebody had that idea earlier, like three years before me. That was the post from Sector 7. Then I discovered that David Buchanan had a tweet where he used a way to write to the memory of the shell, of bash, or the ZSH. This has the benefit. But if I said earlier to glog florid to an actual css file, That's .ní بهU differences address right before the D doesn't exist. That was the problem. With bash we have the benefit that bash already exists. We use an inherited file descriptor and that way we can write into bash's memory. And we can check the mappings that bash has beforehand. So, well, Buchanan the tweet from Buchanan was what did was just write to bash memory to execute a native code, a shell code. It didn't allow to run a binary. So the tool that I made that I had to develop in shell scripting, what it did or what it does, did the exec is to parse the code that was in the binary and prepare a shell code that will perform the same process that the kernel does upon each call to exec DE. So we need to load the loader in memory. We need to load the binary because it's ELF has headers that say the kernel or the loader which portions of the binary need to be in memory and in which address and with what protections. So we create those mappings. We read the data of these binaries into these mappings. We fix the permissions. Then we prepare the stack. We need the kernel leaves, some information for the loader. It is called the auxiliary vector. This has the data like this. So we need to load the loader. We need to load the address like at which address is loaded the binary and where are the headers of the binary and stuff like that. So we prepare the stack and then jump to the loader's entry point. And the loader will parse the binary. We'll see which libraries are needed. Then load them and link them. And then jump into the binary's entry point. So we did the exec. We prepared all the stuff and let the library do its job. And then we prepare the stack. And then we prepare the loader and do the rest. So he's playing like very, very easy. Just so you get an idea of how complex this actually is, all the things that he said that he's preparing the memory and everything is actually a cell code that is going to be created using cell script. So he dynamically creates a cell code to load a binary inside memory using cell script. I mean, it's not just a simple code. If you read this, it looks like the code is sophisticated, but it is not. It's just super ugly. So yeah. Yeah, so you get the idea. Cool. So let's start with the first demo where we are going to be using DdExec to bypass these protections. So what we are going to be running, I have our spot, running this with this read only root file system, which meant the file system read only. And this allows us to download, for example, QTFL and execute it. And we are going to be executing QCTL using the DDXX technique. So I have the demos prepared here so I can copy paste and I don't break anything. We are going to get a cell. We are not still in the distroless part so we still have a cell. If I run mount, do you see this? Do you read this? Cool. You here is read right but it has no exec. So even if we have already here QCTL, if we try to execute it, we are not able. So what we are going to be doing is to load a binary with DDXX in memory and execute it. Let's start loading LS, for example. So what we are doing here is just basically for encoding LS, sending it to DDXX, and here we have the arc zero. And the arguments we want to pass in this case just does H. So it takes a couple of seconds to run because it needs to load in memory. And sooner or later here we just execute something from memory. So of course LS was already in the system so it wasn't that complicated to execute it. The thing about big binaries for DDXX is that it takes a couple of minutes to run. So here what you can see is that we are going to be running DDXX in this video, just because I don't want to waste five minutes of the talk waiting. So I have just cut the video and we'll show you that you can actually run QCTL. So if you check the last line, we are loading QCTL in DDXX. This is the magic of video. I just cut it and it just worked and you will have the help of QCTL. So you can load even big Golan binaries with this technique. Yeah. The problem with QCTL. The problem with QCTL is that it is a really large binary. It is like 40 megabytes. And because DDXX is programmed in shell scripting, all the processing of the binary is done with pipes between commands. And this makes everything really, really slow. So with QCTL, it lasted, running, like 10 minutes. So that's why we had to cut the footage. So after some months, I came up with the idea that DD can be used just to seek through the file descriptor that points to the memfile, which is a representation of pushes or the shell's memory. So we seek through this file and then write with just a echo or a printf or something like that. So we can look for other seekers. I didn't see the slide. do we have a pointer? No, we didn't ask for it. Okay. So, POSIX file descriptors have a really cool quirk, which is that when they are inherited, they are inside the kernel, they are exactly the same file descriptor. The structure that describes the state of this file descriptor is the same. So, if we ‑‑ I have here, like, an example. We have ‑‑ we create a file, TXT, and we create a file descriptor, the file descriptor 3, with read permissions. And then we tell DD to write at offset 3. We tell it to write through this file descriptor. And we tell it to write zero bytes. When we read from this file descriptor, which was displaced, DD performed an lseek syscall in this file descriptor to move the pointer inside this file to the third byte. So, when we read, we will see that we are ‑‑ we're reading, actually, a three bytes after the start of the ‑‑ of the file. So, this is even ‑‑ there are two different processes. This works just because it is the same file descriptor. So, now we can just use DD to seek. And we can use other seekers. Like, for example, if we want to use, like, TAIL or CMP, files that ‑‑ commands that allow you to specify an offset at which start to reading, we can use them. They weren't useful before, because before, in the demo, in David's demo, DD was used not only to seek, but to write. So, we can use DD to read, to write, to write. And it didn't use this quick of the file descriptors. Using this quick of the file descriptors, we can just seek. And then use printf to write. right. So something fun about this technique is that now we don't depend on dd. So we found an idiot that was trying to search for this technique when we released it. And they were looking for dd using big numbers. So it was a very easy thing to bypass. You can even change the dd name to a different binary. But actually, now you can use 10 different common binaries in Linux just to move the file descriptor and write in any position you want in memory. So this actually allows you to bypass the only idiot that was catching this technique, and potentially the new idiot that will try to catch the technique. So let me show you very, very fast a demo about this. Because actually, in the same dd exec script that we have shown before, you can actually indicate in the seeker the binary you want to use. And it will be using internally this binary. So here we are using compare, and we could also be using xsd. There are more. There are more prepared. There are more deeper inside dd exec. So you can just go to the readme and take a look to the ones proposed in there. But actually, any binary that is executing on LST with a few other requirements is going to be able to allow you to just load anything in memory. So actually, we develop a very resilient technique in order to load things in memory and bypass these kind of protections. Cool. So let's move forward. Now let's just start talking about digitalized containers. So if you asked GPT-4 some months ago what is a digitalized container, he will tell you this. But basically, what you need to know is that it has the minimum needed packages to execute whatever you want to execute. So if you want to execute a Python web, it's going to have Python. It's going to be having Python dependencies. But it probably won't have bus. It won't have ls. It won't have cat. Let's say that it will just remove all the libraries and commands and packages that you don't really need to run what you want. So this is great, because it makes the container much, much more smaller, and also it makes it more secure. It makes it very, very complicated to exploit vulnerabilities, because we are used to executing stuff that we expect to be there. That's the problem I had at the beginning before I started thinking about the memory stuff. So let me show you real quick how a digitalized container looks like. So the previous demo, we have run them in my current Mac OS, which is ARM. These techniques work for both ARM and x64. Now we are going to go to a Kubernetes environment. In AWS, this also works in Kubernetes environments in the cloud that are in x64. So this way, you can see that all the techniques kind of work in both architectures. So I'm going to update the credentials. Actually, the pods that we are going to be using for the next demos are this one. We have node prototype pollution, digitalized container. And all of them have the read-only root file system. This is using the node digitalized container. This is using the node digitalized container. From Google, this is one using Python, another one using Python. And then we have a PHP that is using the new ChainWarp digitalized images, just so you can see that these techniques are working with both Google digitalized and ChainWarp digitalized container. And also, we have a Ubuntu machine inside the CERN Kubernetes environment, because we need to capture some reverse cells to mimic a real environment. So what I want to show you here is that we are going to be getting a cell inside the Flask digitalized container. In this case, we have a cell. Some digitalized containers, we have some cells. But if we execute LS, we don't have it. Who am I? God, it's C. Oops. Well, you imagine that. God, not phone. So yeah. Whatever. If you find a cell, just so you know, you can still use the buildings of the cell. So we could be reading the HCPathware using some and the read-by tool. And if we try, for example, to get a cell inside the node digitalized container, we are going to see that we don't even have a cell here. So this is how a digitalized container usually looks. Now, you should be familiar with digitalized. This is the demo. So far, the demos are working, so this is pretty good. OK, so let's move forward. Let's do this Python digitalized demo. This is the one I'm going to be putting in the video, because actually, we are not using our own technique. We're just using an old technique that already exists. But I want to show you anyway. The thing is that, well, if you find a common injection in a web cell, probably it has a cell. Just because usually, to execute commands from scripting languages, the cell is going to be used. So usually, you will find this. But anyway, because you don't really need a cell. If you are compromising a digitalized container and you want a reverse cell, even if you don't have bus, if the web server is running in Python, you can have a Python web cell. So you won't be running bus commands. But you will be able to run Python code, which is still pretty, pretty good. Also, scripting languages such as Python, Perl, and Ruby, by default, install some libraries that allows you to call row syscalls. And that's actually everything you need in order to load something in memory and execute it. So this project right here, fileless-elf-exec, is doing that. It is going to transfer some Python Ruby or Perl code. It's going to transfer a binary, wrap it into Python Perl or Ruby code, and allow you to execute from these scripting engines. So in these specific cases, in these specific digitalized cases, you won't need to use our technique. The scripting language is enough. So I just want to show you this real quick. And actually, we are going to be seeing a video. Here we have a vulnerable Python bot running our web server that I have put forward to my local host. ls doesn't exist. cat doesn't exist. Using the built-in command, we can see that we have Python. Of course, it is running in Flask. So we are going to be getting now, we are going to be abusing, oops, sorry. I wasn't expecting that. We are going to be getting a reversal now, a Python reversal. If you're going to be playing with distressed containers, forget about bus reversal. You're going to be getting no PHP Python reversals. Here we have it. Now what we are going to be doing is, just down here, we execute Ubuntu. We are going to be preparing kubectl to be executed with Python. So we have downloaded the project I just told you about, fileless-elf-exec. We are downloading here kubectl. Now we execute the fileless execute to prepare kubectl. And actually, this script looks just like this. So basically, what this is doing is getting the binary in base64, decoding it in memory, and just calling the syscall execla to execute it. Again, if you can call syscalls from the scripting language, you don't need to do extra stuff to call syscalls. You can just do it. So in our case, in our reversal, what we need to do is to create a children, because it's not whenever we end executing this comma, our reversal is going to be dying. So here I'm just changing the code to fork it, to execute kubectl in a children. I'm going fast, because we have a lot of things to do yet. We are just starting. So OK, now we are going to be re-exposing this Ubuntu server, the kubectl.py that we have prepared. In our Python reversal, we are going to be downloading this in memory again. And now, if we just exec it, we are going to be able to run kubectl in memory in Python. Again, we haven't used our technique, but just so you know, if you find a distroless with Ruby, Perl, or Python. You have a very easy way to just call syscalls and execute things in memory. But what will happen, for example, if we have a node server? We have an express web server with an RCE vulnerability. Well, there is no sh in the server, so we can still get a node reversal. Again, get used to getting this stuff in distroless. You can use node to enumerate the server. So you cannot execute ls, but you can create node functions that behave like ls. So you can enumerate. And node cannot call syscalls directly. But we could use a cell code like ddexec to modify a new node process to execute anything we want. So what we are doing here is to abuse the dedexec technique in order to load anything we want, even in nodes who cannot call syscalls. So in this case, I'm going to be using the cell code that used the syscall create-memfd that I told you about at the beginning. We are going to be creating a file in memory. Then we are going to be loading kubectl in it. And we are going to be able to execute kubectl in another distroless container in node. And actually, I'm going to try to do this demo live. So this is going to be a good one. Cool. So let me get. We are going to be in our target machine, Ubuntu. I'm going to be port forwarding port 3000 to my local machine, because that is where the vulnerable server is running. And here, I'm just abusing a prototype pollution to RC vulnerability in order to get a reversal. This is outside of the scope. But if you're interested about how to do this, you can check more information in Hacktricks. So it was executed. And I forgot to capture the reversal. So we have a real problem here. No problem. No worries. I just need to restart real quick, which is going to take like a minute. But now we can be prepared to capture the reversal. Yeah. Never forget the most important part of reversal, the listener. Yeah. Sorry about this. So Kubernetes takes a minute in order to restart the bot. So that's what we are now waiting for. But in the meantime, we can just prepare. I think he was looking at me angry. Cool. OK. I suppose this should be now ready. OK. Back on track. Actually, let me take a look if I have any other listener in there that could be bothering. I actually have one. So this wouldn't have worked again. OK. Executed. And we have our node reversal. Great. And now we go with the demo. So I told you that you can use node code, JavaScript code, in order to enumerate the query machine. So you could use, for example, the OS library to get TMPD or information about the architecture or the network information. So you can still enumerate this digitalized code. OK. And you can also execute things in memory, which is what we are going to be doing now. I have the demo here, so you can follow it better. So here, what we are going to be doing is importing some stuff. And we are going to create a new JavaScript file with the code that we want to run and the cell code that we want to run. So we are going to be using the syscall trick in order to know where we need to override inside the memory. We are going to be overriding the memory via ProgSelfMem. And this is the cell code that is going to be executing the syscall, create memfd. So we prepare the file through our rubber cell. We write it in the file system, and we check that it exists. So it exists right here. Now we call via fork this new child process that is going to be executed, our cell code. Now we want to check if this works, of course. So this is what the function is going to be doing. And we can see that we have a memfd. Memory file descriptor that we call dead, just for the sake of stillness, I guess. File descriptor 20, ProgID 59. So this should be working. OK, and now we are going to be creating this download function that basically is going to be able to download something from the internet, load it in the memfd, and run it. OK, we download it. This should be working. And now if I execute the memory file descriptor, it should be executing kubectl. And here you have the help of kubectl. So we were able to bypass these sort of protections by executing things in memory. So in order to make this more useful, I decided that I'm going to make a DDXec, a daemon of DDXec. So we can just load filelessly this program. Or in the end, I actually made it a shellcode. So we run this shellcode with any of these techniques that allow us to run native code. We run this shellcode. And we can tell this shellcode. This is a code to load a binary with the argument. And then just we will be able to run any program we want. OK. Cool, so basically this will allow us to use what I have showed you, but to load binaries more frequently or more usefully. Instead of doing everything once and again, it will allow us just to send the binary, and load it, and execute it. So in this final demo before the bonus, that I hope we have time for that, what we are going to have is a PHP digital list. We are going to be using the chain word image for this. In this case, we don't have a cell. We are going to be getting a PHP reversal. We are going to be loading the new daemon. And actually, we are going to finalize this demo, loading a busy box, because we are in this list. It will be very ironic if we can have a cell in this list. So that's what we are going to end up doing. So let me show you that in PHP, we don't have any bus, any SH, SH file, not phone. So we are going to be getting a PHP shell. And now we are going to be getting a PHP reversal. So imagine that we have compromised our PHP web application with an ERC, and we got a reversal with PHP. OK. We have it here. And now maybe you want to follow the explanation. OK. So what we are going to do now is execute another PHP, another interactive session of PHP, which we will communicate through a pipe with. We are creating the pipes. And now starting the process. OK. So this interactive PHP, we will send it commands to write a shellcode, also through the mem file in the procfs file system. And we will run this shellcode that I made, this demonized dd exec. We have the shellcode in. We have the shellcode in ARM 64 or x64. And now we have, OK. He made a mistake. Things happen. OK. I think we are good. This is from our complex lab. You'd think. OK. And now we are writing the shellcode, because there is a shellcode that is a stager that reads the other shellcode, because the second shellcode is really, really large. And now I have created this function that is called memexec. This function is the one that is used to communicate with this daemon. We can throw it an URI. OK. But we will put it back to a binary. It may also be a binary inside the own file system. And we will pass it the arguments and we can run any command. In this case, we are downloading them from my server. OK. And to finalize this demo, what we are going to be doing is load a Boonie box inside this rubber shell. So what we have here is that now we can, for example, use some of the novoons, call, for example, call set to enumerate environment or run POWD. Basically, we put a bus inside a distroless, which was the main goal of this, so we just defeated distroless security measures. . So now the bonus technique. This is another technique that is kind of old and new at the same time. The exec has a problem that we need to pass the file and load it. And a loader is really, really complex. I have skipped a lot of peculiarities that ELF have and extensions from ENU and other systems and things like that. They have others. That's mayhem. So you can have, like, compressed sections and lots of things. And the exec isn't doing that stuff. So another way to load filelessly a binary is to use DL open, but tricking the loader into thinking that it's a binary. The binary is somewhat that it isn't. Okay. So this is a technique that was presented in a paper, I think in 2004. So the idea that these people had was to ‑‑ you have the binary that you want to execute, raw, in memory, the ELF. In some way, you get to hook to the syscalls that the loader is going to execute. And then call DL open. Passing to this function some fake path. Since you are hooked to the syscalls, you can detect when the loader is trying to open this file path that you have given it. And return a fake file descriptor. And in subsequent syscalls, you can see if the file descriptor that the loader is using is the fake one that you gave it. And in that case, you fake the syscall. So in case that you read ‑‑ that the loader is trying to read from this file that doesn't exist in the file system, you can just mem copy from the place that you have given it. And then you can see that you have it in raw in memory and write it where the loader wants to read the data. Okay. So in the paper, they didn't discuss how to hook to the syscalls. There was someone, his alias was mimics, that in GitHub has an implementation that the syscalls can hook to the syscalls. They used code signatures to find the places that the calls to the wrapper functions that the wrapper functions to the syscalls. And overwrite that calls with calls to its own code. The problem with this is that code signatures may vary between libraries. And so this isn't particularly useful because then you ‑‑ if you go to ‑‑ if you go to the syscalls, you can get a code signature. But if you don't have access to a system, you need to download this library and get this code signatures. And it is really complicated. So my idea to hook to the syscalls was to install a wrapper, to install a signal handler for the signal, which is a signal that the kernel sends to a process. If this process tries to execute, the kernel will try to execute the signal with an instruction, an invalid instruction. And then go through the memory of the loader, looking for each syscall instruction, and replace it with an invalid instruction. So now we are just ‑‑ we have the hook that we need. And we can trick LD with just like the paper tool. Okay. So that's all. There's also disgusting to load libraries, but if you make a binary look like a library, you can also get LD to load this binary. Okay. So later we are going to show you the links to all these tools that we have just just show you, the DDX segment, DDLopen, but here I just want to show you that this is actually working, so we have here a Debian container that I have already downloaded, memdlopen.sh, and here what we are going to be loading basically is just ls-la passing in the standard in the binary. So with the ID like this you can see that it's actually much faster than the DDXX technique, and well, we still are working on this to improve some things. This is actually a project that I haven't dedicated a lot of time. This needs a lot of research. I have detected that it has problems with binaries that aren't position independent, but well, I think that I will manage to fix these issues. Cool. So you want to show the code? So this is the shell code. It is long, and it is complicated. Right now it is only implemented for ARM 64. There is a proof of concept written in C that is much, much more understandable. You can see the installation of the signal handler. We have also the shell code that is the replacement of the syscal instructions with invalid ones, and we can see here how this signal handler is taken from the stack. All the arguments that were intended for the syscal, checking which syscal it was, and faking each one. Cool, I think you have all this code in the repository with some comments to help you understand it. So actually the repository, you can find it in here. Here you have memdlopen, remote level injection, we are going to be sharing the slides but if you want to take a quick photo, I will leave it for 10 seconds. Cool. Also, the demos that I have been doing, you have them in this repo if you want to try them yourself. I hope to find some nice CTF in the future using this technique. What else? Well, also, wants to share with us a challenge you have. It is just a really small challenge. But I think it is kind of weird. It is really easy to solve. But to understand why it works is difficult. It is really interesting. I have shown it to, I posted it on Twitter and I think nobody liked it. So if you get to solve it and tell me why it works, I will give you a hug. Some final to end this talk. Me and my mates are preparing a hacktree AWM. This is the first red team expert certification that we expect to release in Q4. So if you are interested in AWS red teaming, just follow us in Twitter, LinkedIn or go to this page. And thank you very much to you people and to DEF CON for organizing this. It has been a great pleasure to be with you. . . . . . . . . .