1 00:00:01,440 --> 00:00:03,540 Cool, okay, well, welcome everybody. 2 00:00:03,820 --> 00:00:05,540 We are going to be giving the talk 3 00:00:05,540 --> 00:00:07,320 Exploring Linux Memory Manipulation 4 00:00:07,320 --> 00:00:08,440 for Stealth and Evasion, 5 00:00:09,000 --> 00:00:10,760 Strategies to Bypass with Only No Exec 6 00:00:10,760 --> 00:00:12,120 and Digitalized Environments. 7 00:00:12,480 --> 00:00:14,580 Probably the title is self-explanatory, 8 00:00:14,820 --> 00:00:16,360 but what we are going to be doing 9 00:00:16,360 --> 00:00:17,940 is to abuse Linux memory 10 00:00:18,340 --> 00:00:21,880 in order to bypass some file system-based defenses, 11 00:00:22,180 --> 00:00:24,460 and also to get RCEs in, 12 00:00:24,580 --> 00:00:27,860 well, to try to make it easier to pivot 13 00:00:27,860 --> 00:00:30,620 where you are talking about digitalized environments. 14 00:00:30,860 --> 00:00:33,140 We will explain what this is if you don't know about it. 15 00:00:33,860 --> 00:00:37,240 So, for the people that read this title 16 00:00:37,240 --> 00:00:40,240 and doesn't understand a thing, don't worry, 17 00:00:40,320 --> 00:00:42,160 because we are going to be starting from the beginning. 18 00:00:42,580 --> 00:00:44,100 We are going to be explaining to you 19 00:00:44,100 --> 00:00:47,240 why we developed this technique and also how it works, 20 00:00:47,320 --> 00:00:48,880 so you should be able to follow everything. 21 00:00:49,120 --> 00:00:50,260 Also, we have a lot of demos. 22 00:00:50,660 --> 00:00:52,200 For the people that are already experts 23 00:00:52,200 --> 00:00:53,540 in things related to this, 24 00:00:53,680 --> 00:00:56,440 I will ask you to wait to the final firewalls 25 00:00:56,440 --> 00:00:57,760 because things are going to be 26 00:00:57,760 --> 00:00:58,940 getting harder and harder, 27 00:00:59,100 --> 00:01:00,460 so we are going to start easy, 28 00:01:00,620 --> 00:01:02,720 and then we are going to start doing the complicated stuff, 29 00:01:02,920 --> 00:01:04,280 so just wait for the end. 30 00:01:04,600 --> 00:01:06,920 Also, as you know, this is our first DEF CON. 31 00:01:07,120 --> 00:01:09,460 So far, this has been a great experience. 32 00:01:09,680 --> 00:01:11,980 We are amazed with the community, with the conference, 33 00:01:12,160 --> 00:01:14,640 so I would like to start this talk 34 00:01:14,640 --> 00:01:17,220 asking you for a round of applause just for DEF CON 35 00:01:17,220 --> 00:01:19,680 because this has been a really great, great conference. 36 00:01:28,080 --> 00:01:28,580 Cool. 37 00:01:28,820 --> 00:01:29,680 So who am I? 38 00:01:29,860 --> 00:01:31,440 All of you know about DEF CON, I'm sure. 39 00:01:31,740 --> 00:01:33,120 So I'm Carlos Polop. 40 00:01:33,260 --> 00:01:35,320 I'm technical leader at Halborn. 41 00:01:35,400 --> 00:01:36,580 I have a lot of certifications. 42 00:01:36,780 --> 00:01:38,340 I was the captain of the Spanish team 43 00:01:38,960 --> 00:01:42,140 in the European Cyber Security Conference a couple of years ago 44 00:01:42,140 --> 00:01:45,280 and also a member of the winning European team 45 00:01:45,280 --> 00:01:47,860 in the International Cyber Security Challenge last year. 46 00:01:48,060 --> 00:01:51,240 I'm also the author of Hack Tricks, Hack Tricks Clouds, and Peace. 47 00:01:53,680 --> 00:01:54,440 Thank you. 48 00:01:58,680 --> 00:02:02,720 And I am just a lost nerd in the wilderness. 49 00:02:04,640 --> 00:02:06,860 I'm still studying telecommunications engineering. 50 00:02:07,280 --> 00:02:08,260 I love Pwn. 51 00:02:08,800 --> 00:02:13,480 I love C, assembly, and Linux internals. 52 00:02:13,600 --> 00:02:17,180 And I don't think I have much more to say about me. 53 00:02:17,460 --> 00:02:18,600 So let's continue. 54 00:02:18,820 --> 00:02:19,280 He's great. 55 00:02:25,460 --> 00:02:27,460 OK, so what are we going to be talking 56 00:02:27,460 --> 00:02:27,680 about? 57 00:02:27,760 --> 00:02:29,900 We will start with an introduction about what 58 00:02:29,900 --> 00:02:30,280 the technique. 59 00:02:30,660 --> 00:02:32,880 We are going to present you the initial technique 60 00:02:32,880 --> 00:02:34,540 we developed called DDXSec. 61 00:02:34,680 --> 00:02:37,280 And then we are going to do a lot of demos. 62 00:02:37,560 --> 00:02:40,000 All these demos has been already recorded, 63 00:02:40,180 --> 00:02:43,200 but we are going to be doing all but one live, 64 00:02:43,380 --> 00:02:45,500 because we really like to do things live, 65 00:02:45,560 --> 00:02:46,760 even if they can fail. 66 00:02:46,900 --> 00:02:48,660 So let's hope it will go OK. 67 00:02:49,160 --> 00:02:50,720 And at the end, we have a surprise. 68 00:02:51,460 --> 00:02:54,900 We developed a new technique that we weren't expecting 69 00:02:55,760 --> 00:02:57,500 to present in this talk. 70 00:02:57,500 --> 00:03:01,720 But Diago, yesterday, finalized the initial step. 71 00:03:02,000 --> 00:03:03,520 So we are going to be doing a demo. 72 00:03:03,640 --> 00:03:04,980 And we will expect that it works. 73 00:03:06,200 --> 00:03:07,920 Yesterday worked most of the time. 74 00:03:08,120 --> 00:03:09,020 So let's see. 75 00:03:11,900 --> 00:03:13,780 OK, so cool. 76 00:03:14,120 --> 00:03:16,900 Everything started maybe two, three years ago. 77 00:03:17,120 --> 00:03:18,920 And the thing is that I was working in this company. 78 00:03:19,120 --> 00:03:20,560 And they decided to move all the containers 79 00:03:20,560 --> 00:03:21,480 to digitalized containers. 80 00:03:21,860 --> 00:03:23,400 And my boss asked me, hey, take a look 81 00:03:23,400 --> 00:03:27,260 to these new digitalized containers, because it is set. 82 00:03:27,260 --> 00:03:30,760 And he said that it's very complicated to compromise, 83 00:03:30,980 --> 00:03:33,440 even if there are vulnerabilities in web servers. 84 00:03:33,600 --> 00:03:36,460 In these containers, it's very complicated to compromise them. 85 00:03:36,620 --> 00:03:38,420 So take a look to them, and let's 86 00:03:38,420 --> 00:03:41,920 find out what an attacker could do in these kind of containers. 87 00:03:42,620 --> 00:03:44,300 So the question was, is digitalized 88 00:03:44,300 --> 00:03:45,140 the new hackable system? 89 00:03:45,360 --> 00:03:47,300 The reality is that in that time, 90 00:03:47,380 --> 00:03:49,460 I didn't know much about digitalized, also 91 00:03:49,460 --> 00:03:50,040 about containers. 92 00:03:50,280 --> 00:03:51,980 So when I started taking a look, 93 00:03:52,080 --> 00:03:55,480 I discovered that I don't even have a cell in these containers. 94 00:03:55,660 --> 00:03:56,280 I don't have a sage. 95 00:03:56,480 --> 00:03:57,120 I don't have bus. 96 00:03:57,120 --> 00:04:00,000 So it was like, man, even if someone finds a remote code 97 00:04:00,000 --> 00:04:01,540 execution here, they are not going 98 00:04:01,540 --> 00:04:03,640 to be able to execute cut, to execute the layers, 99 00:04:03,740 --> 00:04:06,240 to execute who am I. They cannot execute a thing. 100 00:04:06,360 --> 00:04:08,440 So to me, it looks like pretty legit. 101 00:04:08,560 --> 00:04:09,860 It's adding a lot of security. 102 00:04:10,460 --> 00:04:13,900 And I didn't know a way to bypass this at that time. 103 00:04:14,540 --> 00:04:17,860 Also, I started playing more with containers, 104 00:04:18,080 --> 00:04:18,520 with Kubernetes. 105 00:04:18,940 --> 00:04:20,860 And in Kubernetes, you have this easy flag 106 00:04:20,860 --> 00:04:23,540 that you can set to true, which is read only, which 107 00:04:23,540 --> 00:04:26,900 will make the file system read only everything except one 108 00:04:26,900 --> 00:04:29,620 of two folders that have the noexec bit, which 109 00:04:29,620 --> 00:04:33,600 means that if you manage to compromise a readexec container 110 00:04:33,600 --> 00:04:36,820 in Kubernetes, you're not going to be able to download binaries 111 00:04:36,820 --> 00:04:37,580 and execute them. 112 00:04:37,820 --> 00:04:40,200 Because in the only place where you can write, 113 00:04:40,320 --> 00:04:41,500 you have the noexec bit. 114 00:04:41,700 --> 00:04:44,680 And you don't know how annoying it is, as a red teamer, 115 00:04:44,740 --> 00:04:46,740 to compromise a Kubernetes container, 116 00:04:47,220 --> 00:04:49,680 see this Kubernetes token, and then being 117 00:04:49,680 --> 00:04:51,940 able to download kubectl and execute it. 118 00:04:52,100 --> 00:04:53,980 Of course, you can use KURL for insight. 119 00:04:54,100 --> 00:04:56,320 But you need to use KURL to contact the KUBE API, 120 00:04:56,320 --> 00:04:56,940 the API manually. 121 00:04:57,100 --> 00:04:58,120 And it's just annoying. 122 00:04:58,340 --> 00:05:00,560 So yeah, it makes your life much complicated. 123 00:05:00,840 --> 00:05:02,680 And at that time, I thought, hey, OK. 124 00:05:02,820 --> 00:05:04,740 So I cannot write in the file system. 125 00:05:04,960 --> 00:05:08,700 But what happens if I could load something in memory 126 00:05:08,700 --> 00:05:09,500 and execute it? 127 00:05:09,580 --> 00:05:11,820 There is nothing preventing me from doing that. 128 00:05:12,220 --> 00:05:13,440 And I knew at that time that there 129 00:05:13,440 --> 00:05:14,820 were a lot of different techniques 130 00:05:14,820 --> 00:05:15,720 for doing this in Windows. 131 00:05:15,820 --> 00:05:17,220 It's super easy to do this in Windows. 132 00:05:17,400 --> 00:05:18,340 But what about Linux? 133 00:05:19,060 --> 00:05:20,560 Well, there weren't so many. 134 00:05:20,760 --> 00:05:23,220 And the words that were in there weren't that reliable. 135 00:05:23,480 --> 00:05:25,940 So at that point, I just started my conversation with Diego. 136 00:05:25,940 --> 00:05:29,340 And well, he came up with a very strange idea 137 00:05:29,340 --> 00:05:31,160 that ended up working perfectly. 138 00:05:31,560 --> 00:05:33,480 But before getting into this technique, 139 00:05:33,640 --> 00:05:36,060 let me tell you about the state of the art in that time 140 00:05:36,410 --> 00:05:39,600 about how to load things in Linux memory and execute stuff. 141 00:05:40,160 --> 00:05:42,860 At some point, we found this blog from Sector 7 142 00:05:42,860 --> 00:05:46,020 that told you that if you manage to write things in memory, 143 00:05:46,160 --> 00:05:50,760 you could use this cell code using the syscall memfd create 144 00:05:50,760 --> 00:05:54,100 that will allow you to create a file in memory that 145 00:05:54,100 --> 00:05:55,720 is readable from the file system. 146 00:05:55,720 --> 00:05:58,840 So you could create a file in memory, load your binary, 147 00:05:58,980 --> 00:05:59,640 and execute it. 148 00:05:59,720 --> 00:06:02,260 This will allow you to bypass these protections. 149 00:06:02,620 --> 00:06:05,200 And also, while we were developing the technique 150 00:06:05,200 --> 00:06:06,420 that now Jao is going to explain, 151 00:06:06,660 --> 00:06:09,680 we found that David Buchanan was doing something very, very, 152 00:06:09,700 --> 00:06:11,560 very similar, but just smaller. 153 00:06:11,780 --> 00:06:15,020 And actually, he was using a couple of extra techniques 154 00:06:15,020 --> 00:06:18,560 that we ended up implementing in our final technique called 155 00:06:18,560 --> 00:06:19,180 DDXX. 156 00:06:19,340 --> 00:06:22,340 But at that point, that was the state of the art. 157 00:06:22,440 --> 00:06:23,520 You couldn't do much. 158 00:06:23,660 --> 00:06:25,080 And it was kind of complex. 159 00:06:25,080 --> 00:06:26,920 And dark, how to do this. 160 00:06:27,420 --> 00:06:29,100 So we came up with DDXX. 161 00:06:30,720 --> 00:06:34,160 So the idea that I had at the time 162 00:06:34,160 --> 00:06:42,860 was to trick DD into overwriting its own memory 163 00:06:42,860 --> 00:06:46,300 through the mem file in the Procfs file system. 164 00:06:48,060 --> 00:06:52,500 It turns out that somebody had that idea earlier, 165 00:06:52,600 --> 00:06:54,780 like three years before me. 166 00:06:55,080 --> 00:06:58,060 That was the post from Sector 7. 167 00:07:00,980 --> 00:07:05,660 Then I discovered that David Buchanan had a tweet 168 00:07:06,720 --> 00:07:17,060 where he used a way to write to the memory of the shell, 169 00:07:17,200 --> 00:07:19,480 of bash, or the ZSH. 170 00:07:22,320 --> 00:07:24,020 This has the benefit. 171 00:07:25,080 --> 00:07:33,320 But if I said earlier to glog florid to an actual css file, 172 00:07:34,260 --> 00:07:35,200 That's 173 00:07:35,200 --> 00:07:50,140 .ní بهU differences 174 00:07:50,140 --> 00:07:57,980 address right before the D doesn't exist. That was the problem. With bash we have the benefit 175 00:07:57,980 --> 00:08:06,160 that bash already exists. We use an inherited file descriptor and that way we can write into 176 00:08:06,160 --> 00:08:19,260 bash's memory. And we can check the mappings that bash has beforehand. So, well, Buchanan 177 00:08:19,260 --> 00:08:31,880 the tweet from Buchanan was what did was just write to bash memory to execute a native code, a 178 00:08:31,880 --> 00:08:40,680 shell code. It didn't allow to run a binary. So the tool that I made that I had to develop in 179 00:08:41,260 --> 00:08:49,240 shell scripting, what it did or what it does, did the exec is to parse the code that was 180 00:08:49,260 --> 00:08:57,040 in the binary and prepare a shell code that will perform the same process that the kernel does 181 00:08:57,040 --> 00:09:06,420 upon each call to exec DE. So we need to load the loader in memory. We need to load the binary 182 00:09:07,060 --> 00:09:19,080 because it's ELF has headers that say the kernel or the loader which portions of the binary need 183 00:09:19,080 --> 00:09:29,580 to be in memory and in which address and with what protections. So we create those mappings. 184 00:09:29,900 --> 00:09:37,880 We read the data of these binaries into these mappings. We fix the permissions. Then we prepare 185 00:09:37,880 --> 00:09:44,760 the stack. We need the kernel leaves, some information for the loader. It is called the 186 00:09:44,760 --> 00:09:49,060 auxiliary vector. This has the data like this. So we need to load the loader. We need to load the 187 00:09:49,080 --> 00:09:55,040 address like at which address is loaded the binary and where are the headers of the binary and 188 00:09:55,040 --> 00:10:01,680 stuff like that. So we prepare the stack and then jump to the loader's entry point. And the 189 00:10:01,680 --> 00:10:10,860 loader will parse the binary. We'll see which libraries are needed. Then load them and link 190 00:10:10,860 --> 00:10:18,140 them. And then jump into the binary's entry point. So we did the exec. We prepared all the 191 00:10:18,140 --> 00:10:19,060 stuff and let the library do its job. And then we prepare the stack. And then we prepare the 192 00:10:19,080 --> 00:10:32,260 loader and do the rest. So he's playing like very, very easy. Just so you get an idea of how 193 00:10:32,260 --> 00:10:37,640 complex this actually is, all the things that he said that he's preparing the memory and 194 00:10:37,640 --> 00:10:42,860 everything is actually a cell code that is going to be created using cell script. So he dynamically 195 00:10:43,370 --> 00:10:49,060 creates a cell code to load a binary inside memory using cell script. I mean, it's not just a 196 00:10:49,080 --> 00:10:53,500 simple code. If you read this, it looks like the code is sophisticated, but it is not. It's just 197 00:10:53,500 --> 00:11:00,720 super ugly. So yeah. Yeah, so you get the idea. Cool. So let's start with the first demo where we 198 00:11:00,720 --> 00:11:06,560 are going to be using DdExec to bypass these protections. So what we are going to be running, I 199 00:11:06,560 --> 00:11:12,520 have our spot, running this with this read only root file system, which meant the file system read 200 00:11:12,520 --> 00:11:17,300 only. And this allows us to download, for example, QTFL and execute it. And we are going to be 201 00:11:17,300 --> 00:11:24,520 executing QCTL using the DDXX technique. So I have the demos prepared here so I can copy 202 00:11:24,520 --> 00:11:32,020 paste and I don't break anything. We are going to get a cell. We are not still in the distroless 203 00:11:32,020 --> 00:11:47,280 part so we still have a cell. If I run mount, do you see this? Do you read this? Cool. You 204 00:11:47,280 --> 00:11:55,800 here is read right but it has no exec. So even if we have already here QCTL, if we try 205 00:11:56,560 --> 00:12:01,300 to execute it, we are not able. So what we are going to be doing is to load a binary 206 00:12:01,300 --> 00:12:10,980 with DDXX in memory and execute it. Let's start loading LS, for example. So what we 207 00:12:10,980 --> 00:12:16,300 are doing here is just basically for encoding LS, sending it to DDXX, and here we have the 208 00:12:16,300 --> 00:12:16,880 arc zero. 209 00:12:16,880 --> 00:12:21,060 And the arguments we want to pass in this case just does H. So it takes a couple of 210 00:12:21,060 --> 00:12:27,760 seconds to run because it needs to load in memory. And sooner or later here we just execute 211 00:12:27,760 --> 00:12:40,360 something from memory. So of course LS was already in the system so it wasn't that complicated 212 00:12:40,360 --> 00:12:45,820 to execute it. The thing about big binaries for DDXX is that it takes a couple of minutes 213 00:12:45,820 --> 00:12:46,200 to run. 214 00:12:46,200 --> 00:12:51,000 So here what you can see is that we are going to be running DDXX in this video, just because 215 00:12:51,000 --> 00:12:55,320 I don't want to waste five minutes of the talk waiting. So I have just cut the video 216 00:12:55,320 --> 00:12:59,840 and we'll show you that you can actually run QCTL. So if you check the last line, 217 00:12:59,960 --> 00:13:06,060 we are loading QCTL in DDXX. This is the magic of video. I just cut it and it just 218 00:13:06,060 --> 00:13:11,840 worked and you will have the help of QCTL. So you can load even big Golan binaries with 219 00:13:11,840 --> 00:13:12,400 this technique. 220 00:13:12,780 --> 00:13:12,980 Yeah. 221 00:13:14,000 --> 00:13:15,340 The problem with QCTL. 222 00:13:15,340 --> 00:13:20,560 The problem with QCTL is that it is a really large binary. It is like 40 megabytes. And 223 00:13:20,560 --> 00:13:31,640 because DDXX is programmed in shell scripting, all the processing of the binary is done with 224 00:13:31,640 --> 00:13:44,340 pipes between commands. And this makes everything really, really slow. So with QCTL, it lasted, 225 00:13:44,340 --> 00:13:46,860 running, like 10 minutes. 226 00:13:48,040 --> 00:13:51,900 So that's why we had to cut the footage. 227 00:13:56,200 --> 00:14:14,100 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 228 00:14:14,340 --> 00:14:25,200 representation of pushes or the shell's memory. So we seek through this file and then write with just a 229 00:14:25,200 --> 00:14:41,060 echo or a printf or something like that. So we can look for other seekers. I didn't see the slide. 230 00:14:44,340 --> 00:14:53,660 do we have a pointer? No, we didn't ask for it. Okay. So, POSIX file descriptors have a 231 00:14:53,660 --> 00:15:03,500 really cool quirk, which is that when they are inherited, they are inside the kernel, they 232 00:15:03,500 --> 00:15:10,580 are exactly the same file descriptor. The structure that describes the state of this file 233 00:15:10,580 --> 00:15:19,880 descriptor is the same. So, if we ‑‑ I have here, like, an example. We have ‑‑ we create 234 00:15:19,880 --> 00:15:29,380 a file, TXT, and we create a file descriptor, the file descriptor 3, with read permissions. And 235 00:15:29,380 --> 00:15:40,560 then we tell DD to write at offset 3. We tell it to write through this file descriptor. And 236 00:15:40,580 --> 00:15:49,640 we tell it to write zero bytes. When we read from this file descriptor, which was displaced, 237 00:15:50,930 --> 00:16:02,460 DD performed an lseek syscall in this file descriptor to move the pointer inside this file to 238 00:16:02,460 --> 00:16:10,560 the third byte. So, when we read, we will see that we are ‑‑ we're reading, actually, a 239 00:16:11,940 --> 00:16:22,760 three bytes after the start of the ‑‑ of the file. So, this is even ‑‑ there are two 240 00:16:22,760 --> 00:16:31,680 different processes. This works just because it is the same file descriptor. So, now we 241 00:16:31,680 --> 00:16:40,560 can just use DD to seek. And we can use other seekers. Like, for example, if we want to 242 00:16:40,580 --> 00:16:52,400 use, like, TAIL or CMP, files that ‑‑ commands that allow you to specify an offset at which 243 00:16:52,400 --> 00:17:03,500 start to reading, we can use them. They weren't useful before, because before, in the demo, in 244 00:17:03,500 --> 00:17:10,560 David's demo, DD was used not only to seek, but to write. So, we can use DD to read, to write, to 245 00:17:10,580 --> 00:17:16,580 write. And it didn't use this quick of the file descriptors. Using this quick of the file 246 00:17:16,580 --> 00:17:20,180 descriptors, we can just seek. And then use printf to write. 247 00:17:21,000 --> 00:17:21,480 right. 248 00:17:23,540 --> 00:17:25,380 So something fun about this technique 249 00:17:25,380 --> 00:17:27,220 is that now we don't depend on dd. 250 00:17:27,380 --> 00:17:29,120 So we found an idiot that was trying 251 00:17:29,120 --> 00:17:31,660 to search for this technique when we released it. 252 00:17:31,960 --> 00:17:34,560 And they were looking for dd using big numbers. 253 00:17:34,820 --> 00:17:37,740 So it was a very easy thing to bypass. 254 00:17:37,920 --> 00:17:40,600 You can even change the dd name to a different binary. 255 00:17:40,780 --> 00:17:43,660 But actually, now you can use 10 different common binaries 256 00:17:43,660 --> 00:17:46,200 in Linux just to move the file descriptor 257 00:17:46,700 --> 00:17:49,280 and write in any position you want in memory. 258 00:17:49,280 --> 00:17:52,320 So this actually allows you to bypass the only idiot that 259 00:17:52,320 --> 00:17:55,640 was catching this technique, and potentially the new idiot 260 00:17:55,640 --> 00:17:57,220 that will try to catch the technique. 261 00:17:57,600 --> 00:18:00,700 So let me show you very, very fast a demo about this. 262 00:18:00,840 --> 00:18:04,440 Because actually, in the same dd exec script 263 00:18:04,440 --> 00:18:06,880 that we have shown before, you can actually 264 00:18:06,880 --> 00:18:09,720 indicate in the seeker the binary you want to use. 265 00:18:09,920 --> 00:18:12,020 And it will be using internally this binary. 266 00:18:12,160 --> 00:18:16,400 So here we are using compare, and we could also be using xsd. 267 00:18:16,540 --> 00:18:17,180 There are more. 268 00:18:17,760 --> 00:18:18,800 There are more prepared. 269 00:18:18,800 --> 00:18:20,600 There are more deeper inside dd exec. 270 00:18:20,780 --> 00:18:22,880 So you can just go to the readme and take a look 271 00:18:22,880 --> 00:18:24,100 to the ones proposed in there. 272 00:18:24,300 --> 00:18:26,200 But actually, any binary that is executing 273 00:18:26,200 --> 00:18:29,100 on LST with a few other requirements 274 00:18:29,100 --> 00:18:31,820 is going to be able to allow you to just load anything 275 00:18:31,820 --> 00:18:32,260 in memory. 276 00:18:32,440 --> 00:18:35,620 So actually, we develop a very resilient technique 277 00:18:36,040 --> 00:18:37,400 in order to load things in memory 278 00:18:37,400 --> 00:18:38,800 and bypass these kind of protections. 279 00:18:40,100 --> 00:18:40,540 Cool. 280 00:18:41,220 --> 00:18:42,500 So let's move forward. 281 00:18:43,220 --> 00:18:45,280 Now let's just start talking about digitalized containers. 282 00:18:45,500 --> 00:18:48,100 So if you asked GPT-4 some months ago 283 00:18:48,800 --> 00:18:50,580 what is a digitalized container, he will tell you this. 284 00:18:50,740 --> 00:18:52,320 But basically, what you need to know 285 00:18:52,320 --> 00:18:55,640 is that it has the minimum needed packages to execute 286 00:18:55,640 --> 00:18:56,860 whatever you want to execute. 287 00:18:57,100 --> 00:18:59,720 So if you want to execute a Python web, 288 00:18:59,920 --> 00:19:01,160 it's going to have Python. 289 00:19:01,220 --> 00:19:03,060 It's going to be having Python dependencies. 290 00:19:03,160 --> 00:19:04,480 But it probably won't have bus. 291 00:19:04,620 --> 00:19:05,480 It won't have ls. 292 00:19:05,560 --> 00:19:06,460 It won't have cat. 293 00:19:06,940 --> 00:19:09,080 Let's say that it will just remove all the libraries 294 00:19:09,080 --> 00:19:11,960 and commands and packages that you don't really 295 00:19:11,960 --> 00:19:13,340 need to run what you want. 296 00:19:13,560 --> 00:19:16,200 So this is great, because it makes the container much, much 297 00:19:16,200 --> 00:19:18,660 more smaller, and also it makes it more secure. 298 00:19:18,800 --> 00:19:20,280 It makes it very, very complicated 299 00:19:20,730 --> 00:19:23,660 to exploit vulnerabilities, because we 300 00:19:23,660 --> 00:19:26,440 are used to executing stuff that we expect to be there. 301 00:19:27,020 --> 00:19:28,880 That's the problem I had at the beginning 302 00:19:28,880 --> 00:19:31,880 before I started thinking about the memory stuff. 303 00:19:32,400 --> 00:19:35,380 So let me show you real quick how a digitalized container 304 00:19:35,380 --> 00:19:36,020 looks like. 305 00:19:37,220 --> 00:19:39,680 So the previous demo, we have run them 306 00:19:39,680 --> 00:19:41,880 in my current Mac OS, which is ARM. 307 00:19:42,160 --> 00:19:44,960 These techniques work for both ARM and x64. 308 00:19:45,280 --> 00:19:48,180 Now we are going to go to a Kubernetes environment. 309 00:19:48,800 --> 00:19:51,820 In AWS, this also works in Kubernetes environments 310 00:19:51,820 --> 00:19:53,960 in the cloud that are in x64. 311 00:19:54,220 --> 00:19:56,940 So this way, you can see that all the techniques kind of work 312 00:19:56,940 --> 00:19:59,520 in both architectures. 313 00:19:59,940 --> 00:20:01,740 So I'm going to update the credentials. 314 00:20:03,040 --> 00:20:04,760 Actually, the pods that we are going 315 00:20:04,760 --> 00:20:07,000 to be using for the next demos are this one. 316 00:20:07,220 --> 00:20:10,780 We have node prototype pollution, digitalized container. 317 00:20:12,080 --> 00:20:14,820 And all of them have the read-only root file system. 318 00:20:14,980 --> 00:20:17,520 This is using the node digitalized container. 319 00:20:18,300 --> 00:20:18,780 This is using the node digitalized container. 320 00:20:18,780 --> 00:20:23,400 From Google, this is one using Python, another one 321 00:20:23,400 --> 00:20:24,000 using Python. 322 00:20:24,260 --> 00:20:27,880 And then we have a PHP that is using the new ChainWarp 323 00:20:27,880 --> 00:20:30,040 digitalized images, just so you can 324 00:20:30,040 --> 00:20:32,680 see that these techniques are working with both Google 325 00:20:32,680 --> 00:20:36,700 digitalized and ChainWarp digitalized container. 326 00:20:36,980 --> 00:20:39,320 And also, we have a Ubuntu machine inside the CERN 327 00:20:39,930 --> 00:20:41,160 Kubernetes environment, because we 328 00:20:41,160 --> 00:20:42,440 need to capture some reverse cells 329 00:20:42,440 --> 00:20:44,160 to mimic a real environment. 330 00:20:44,780 --> 00:20:46,640 So what I want to show you here is 331 00:20:46,640 --> 00:20:48,160 that we are going to be getting a cell 332 00:20:48,160 --> 00:20:50,700 inside the Flask digitalized container. 333 00:20:51,260 --> 00:20:53,120 In this case, we have a cell. 334 00:20:53,220 --> 00:20:55,080 Some digitalized containers, we have some cells. 335 00:20:55,180 --> 00:20:57,960 But if we execute LS, we don't have it. 336 00:20:58,400 --> 00:20:58,980 Who am I? 337 00:20:59,540 --> 00:21:03,580 God, it's C. Oops. 338 00:21:04,720 --> 00:21:05,960 Well, you imagine that. 339 00:21:06,100 --> 00:21:06,900 God, not phone. 340 00:21:07,040 --> 00:21:07,500 So yeah. 341 00:21:08,700 --> 00:21:09,140 Whatever. 342 00:21:09,360 --> 00:21:11,540 If you find a cell, just so you know, 343 00:21:11,600 --> 00:21:13,500 you can still use the buildings of the cell. 344 00:21:13,580 --> 00:21:16,300 So we could be reading the HCPathware using some 345 00:21:16,300 --> 00:21:17,340 and the read-by tool. 346 00:21:18,160 --> 00:21:21,420 And if we try, for example, to get a cell inside the node 347 00:21:22,320 --> 00:21:25,180 digitalized container, we are going 348 00:21:25,180 --> 00:21:26,820 to see that we don't even have a cell here. 349 00:21:27,040 --> 00:21:29,520 So this is how a digitalized container usually looks. 350 00:21:29,640 --> 00:21:31,880 Now, you should be familiar with digitalized. 351 00:21:33,120 --> 00:21:33,900 This is the demo. 352 00:21:34,180 --> 00:21:36,360 So far, the demos are working, so this is pretty good. 353 00:21:37,660 --> 00:21:38,940 OK, so let's move forward. 354 00:21:39,200 --> 00:21:41,320 Let's do this Python digitalized demo. 355 00:21:41,560 --> 00:21:43,520 This is the one I'm going to be putting in the video, 356 00:21:43,600 --> 00:21:45,560 because actually, we are not using our own technique. 357 00:21:45,680 --> 00:21:47,580 We're just using an old technique that already exists. 358 00:21:47,580 --> 00:21:48,860 But I want to show you anyway. 359 00:21:49,360 --> 00:21:52,600 The thing is that, well, if you find a common injection 360 00:21:52,600 --> 00:21:55,520 in a web cell, probably it has a cell. 361 00:21:55,740 --> 00:21:58,440 Just because usually, to execute commands 362 00:21:58,440 --> 00:22:01,040 from scripting languages, the cell is going to be used. 363 00:22:01,180 --> 00:22:02,980 So usually, you will find this. 364 00:22:03,480 --> 00:22:06,280 But anyway, because you don't really need a cell. 365 00:22:06,360 --> 00:22:08,420 If you are compromising a digitalized container 366 00:22:08,420 --> 00:22:10,700 and you want a reverse cell, even if you don't have bus, 367 00:22:10,900 --> 00:22:13,300 if the web server is running in Python, 368 00:22:13,460 --> 00:22:14,860 you can have a Python web cell. 369 00:22:14,960 --> 00:22:17,480 So you won't be running bus commands. 370 00:22:17,580 --> 00:22:19,280 But you will be able to run Python code, which 371 00:22:19,280 --> 00:22:20,820 is still pretty, pretty good. 372 00:22:21,260 --> 00:22:24,720 Also, scripting languages such as Python, Perl, and Ruby, 373 00:22:25,290 --> 00:22:28,300 by default, install some libraries that allows 374 00:22:28,300 --> 00:22:30,860 you to call row syscalls. 375 00:22:31,140 --> 00:22:32,980 And that's actually everything you 376 00:22:32,980 --> 00:22:35,620 need in order to load something in memory and execute it. 377 00:22:36,140 --> 00:22:39,340 So this project right here, fileless-elf-exec, 378 00:22:39,460 --> 00:22:40,220 is doing that. 379 00:22:40,280 --> 00:22:43,980 It is going to transfer some Python Ruby or Perl code. 380 00:22:44,460 --> 00:22:46,880 It's going to transfer a binary, wrap it 381 00:22:46,880 --> 00:22:49,540 into Python Perl or Ruby code, and allow 382 00:22:49,540 --> 00:22:51,480 you to execute from these scripting engines. 383 00:22:52,080 --> 00:22:56,920 So in these specific cases, in these specific digitalized 384 00:22:56,920 --> 00:22:58,900 cases, you won't need to use our technique. 385 00:22:59,060 --> 00:23:00,600 The scripting language is enough. 386 00:23:00,880 --> 00:23:02,580 So I just want to show you this real quick. 387 00:23:02,760 --> 00:23:04,260 And actually, we are going to be seeing a video. 388 00:23:04,580 --> 00:23:08,020 Here we have a vulnerable Python bot running our web server 389 00:23:08,020 --> 00:23:09,780 that I have put forward to my local host. 390 00:23:10,620 --> 00:23:11,840 ls doesn't exist. 391 00:23:12,180 --> 00:23:13,500 cat doesn't exist. 392 00:23:14,140 --> 00:23:16,860 Using the built-in command, we can see that we have Python. 393 00:23:16,860 --> 00:23:18,300 Of course, it is running in Flask. 394 00:23:20,160 --> 00:23:22,080 So we are going to be getting now, 395 00:23:22,180 --> 00:23:24,100 we are going to be abusing, oops, sorry. 396 00:23:24,180 --> 00:23:25,220 I wasn't expecting that. 397 00:23:26,340 --> 00:23:31,280 We are going to be getting a reversal now, a Python reversal. 398 00:23:31,640 --> 00:23:33,620 If you're going to be playing with distressed containers, 399 00:23:33,840 --> 00:23:35,440 forget about bus reversal. 400 00:23:35,640 --> 00:23:38,120 You're going to be getting no PHP Python reversals. 401 00:23:38,460 --> 00:23:39,400 Here we have it. 402 00:23:40,260 --> 00:23:43,800 Now what we are going to be doing is, just down here, 403 00:23:44,860 --> 00:23:46,180 we execute Ubuntu. 404 00:23:46,180 --> 00:23:49,460 We are going to be preparing kubectl to be executed with Python. 405 00:23:49,660 --> 00:23:51,880 So we have downloaded the project I just told you about, 406 00:23:52,020 --> 00:23:53,140 fileless-elf-exec. 407 00:23:53,360 --> 00:23:55,560 We are downloading here kubectl. 408 00:23:57,760 --> 00:24:01,540 Now we execute the fileless execute to prepare kubectl. 409 00:24:01,820 --> 00:24:06,300 And actually, this script looks just like this. 410 00:24:07,580 --> 00:24:11,360 So basically, what this is doing is getting the binary in base64, 411 00:24:11,680 --> 00:24:15,760 decoding it in memory, and just calling the syscall execla 412 00:24:16,180 --> 00:24:16,860 to execute it. 413 00:24:16,960 --> 00:24:19,480 Again, if you can call syscalls from the scripting language, 414 00:24:19,620 --> 00:24:22,500 you don't need to do extra stuff to call syscalls. 415 00:24:22,620 --> 00:24:23,600 You can just do it. 416 00:24:24,020 --> 00:24:26,240 So in our case, in our reversal, what we need to do 417 00:24:26,240 --> 00:24:29,620 is to create a children, because it's not whenever we end 418 00:24:30,140 --> 00:24:33,260 executing this comma, our reversal is going to be dying. 419 00:24:33,480 --> 00:24:36,760 So here I'm just changing the code to fork it, 420 00:24:36,840 --> 00:24:39,420 to execute kubectl in a children. 421 00:24:42,900 --> 00:24:45,120 I'm going fast, because we have a lot of things to do yet. 422 00:24:45,120 --> 00:24:46,560 We are just starting. 423 00:24:46,800 --> 00:24:51,680 So OK, now we are going to be re-exposing this Ubuntu server, 424 00:24:51,980 --> 00:24:54,340 the kubectl.py that we have prepared. 425 00:24:54,560 --> 00:24:56,360 In our Python reversal, we are going 426 00:24:56,360 --> 00:24:58,900 to be downloading this in memory again. 427 00:25:01,280 --> 00:25:07,040 And now, if we just exec it, we are 428 00:25:07,040 --> 00:25:10,200 going to be able to run kubectl in memory in Python. 429 00:25:10,480 --> 00:25:11,720 Again, we haven't used our technique, 430 00:25:11,820 --> 00:25:13,920 but just so you know, if you find a distroless with Ruby, 431 00:25:14,040 --> 00:25:14,840 Perl, or Python. 432 00:25:15,120 --> 00:25:16,980 You have a very easy way to just call syscalls 433 00:25:16,980 --> 00:25:18,660 and execute things in memory. 434 00:25:19,300 --> 00:25:21,080 But what will happen, for example, 435 00:25:21,260 --> 00:25:24,520 if we have a node server? 436 00:25:24,740 --> 00:25:27,040 We have an express web server with an RCE vulnerability. 437 00:25:28,140 --> 00:25:30,340 Well, there is no sh in the server, 438 00:25:30,440 --> 00:25:32,560 so we can still get a node reversal. 439 00:25:32,780 --> 00:25:35,860 Again, get used to getting this stuff in distroless. 440 00:25:36,040 --> 00:25:39,280 You can use node to enumerate the server. 441 00:25:39,480 --> 00:25:41,240 So you cannot execute ls, but you 442 00:25:41,240 --> 00:25:43,940 can create node functions that behave like ls. 443 00:25:43,960 --> 00:25:45,100 So you can enumerate. 444 00:25:45,100 --> 00:25:48,560 And node cannot call syscalls directly. 445 00:25:48,880 --> 00:25:52,480 But we could use a cell code like ddexec 446 00:25:52,480 --> 00:25:57,360 to modify a new node process to execute anything we want. 447 00:25:57,500 --> 00:26:00,660 So what we are doing here is to abuse the dedexec technique 448 00:26:00,660 --> 00:26:02,420 in order to load anything we want, 449 00:26:02,500 --> 00:26:05,680 even in nodes who cannot call syscalls. 450 00:26:06,040 --> 00:26:09,820 So in this case, I'm going to be using the cell code that 451 00:26:10,420 --> 00:26:13,680 used the syscall create-memfd that I told you 452 00:26:13,680 --> 00:26:14,360 about at the beginning. 453 00:26:14,360 --> 00:26:16,600 We are going to be creating a file in memory. 454 00:26:16,820 --> 00:26:19,100 Then we are going to be loading kubectl in it. 455 00:26:19,200 --> 00:26:21,320 And we are going to be able to execute kubectl 456 00:26:21,320 --> 00:26:23,980 in another distroless container in node. 457 00:26:25,080 --> 00:26:28,140 And actually, I'm going to try to do this demo live. 458 00:26:28,380 --> 00:26:30,280 So this is going to be a good one. 459 00:26:33,580 --> 00:26:33,980 Cool. 460 00:26:36,000 --> 00:26:37,200 So let me get. 461 00:26:37,380 --> 00:26:39,740 We are going to be in our target machine, Ubuntu. 462 00:26:40,420 --> 00:26:45,560 I'm going to be port forwarding port 3000 to my local machine, 463 00:26:45,660 --> 00:26:49,720 because that is where the vulnerable server is running. 464 00:26:49,960 --> 00:26:52,560 And here, I'm just abusing a prototype pollution 465 00:26:52,560 --> 00:26:54,940 to RC vulnerability in order to get a reversal. 466 00:26:55,120 --> 00:26:56,180 This is outside of the scope. 467 00:26:56,340 --> 00:26:58,560 But if you're interested about how to do this, 468 00:26:58,640 --> 00:27:00,700 you can check more information in Hacktricks. 469 00:27:01,720 --> 00:27:02,780 So it was executed. 470 00:27:04,200 --> 00:27:06,480 And I forgot to capture the reversal. 471 00:27:06,660 --> 00:27:08,160 So we have a real problem here. 472 00:27:09,260 --> 00:27:09,720 No problem. 473 00:27:09,740 --> 00:27:10,040 No worries. 474 00:27:10,160 --> 00:27:13,360 I just need to restart real quick, which 475 00:27:13,360 --> 00:27:14,400 is going to take like a minute. 476 00:27:16,400 --> 00:27:19,720 But now we can be prepared to capture the reversal. 477 00:27:21,340 --> 00:27:21,860 Yeah. 478 00:27:23,260 --> 00:27:26,540 Never forget the most important part of reversal, the listener. 479 00:27:28,460 --> 00:27:28,980 Yeah. 480 00:27:29,140 --> 00:27:30,080 Sorry about this. 481 00:27:30,240 --> 00:27:33,960 So Kubernetes takes a minute in order to restart the bot. 482 00:27:34,280 --> 00:27:36,240 So that's what we are now waiting for. 483 00:27:36,360 --> 00:27:38,380 But in the meantime, we can just prepare. 484 00:27:42,840 --> 00:27:44,740 I think he was looking at me angry. 485 00:27:47,580 --> 00:27:48,060 Cool. 486 00:27:48,180 --> 00:27:48,480 OK. 487 00:27:48,620 --> 00:27:50,920 I suppose this should be now ready. 488 00:27:52,440 --> 00:27:52,920 OK. 489 00:27:54,820 --> 00:27:55,600 Back on track. 490 00:27:55,940 --> 00:28:03,400 Actually, let me take a look if I have any other listener 491 00:28:03,400 --> 00:28:04,640 in there that could be bothering. 492 00:28:04,880 --> 00:28:05,720 I actually have one. 493 00:28:05,840 --> 00:28:07,060 So this wouldn't have worked again. 494 00:28:10,840 --> 00:28:11,240 OK. 495 00:28:18,380 --> 00:28:18,780 Executed. 496 00:28:18,940 --> 00:28:20,440 And we have our node reversal. 497 00:28:21,100 --> 00:28:21,500 Great. 498 00:28:22,800 --> 00:28:24,020 And now we go with the demo. 499 00:28:24,160 --> 00:28:29,140 So I told you that you can use node code, JavaScript code, 500 00:28:29,240 --> 00:28:30,680 in order to enumerate the query machine. 501 00:28:30,880 --> 00:28:32,320 So you could use, for example, the OS library 502 00:28:32,760 --> 00:28:35,380 to get TMPD or information about the architecture 503 00:28:35,380 --> 00:28:36,520 or the network information. 504 00:28:36,700 --> 00:28:38,880 So you can still enumerate this digitalized code. 505 00:28:39,320 --> 00:28:39,720 OK. 506 00:28:39,740 --> 00:28:42,220 And you can also execute things in memory, 507 00:28:42,340 --> 00:28:43,800 which is what we are going to be doing now. 508 00:28:43,940 --> 00:28:45,960 I have the demo here, so you can follow it better. 509 00:28:46,280 --> 00:28:48,980 So here, what we are going to be doing is importing some stuff. 510 00:28:49,200 --> 00:28:53,420 And we are going to create a new JavaScript file with the code 511 00:28:53,420 --> 00:28:56,400 that we want to run and the cell code that we want to run. 512 00:28:56,660 --> 00:28:59,060 So we are going to be using the syscall trick in order 513 00:28:59,060 --> 00:29:01,680 to know where we need to override inside the memory. 514 00:29:01,840 --> 00:29:04,260 We are going to be overriding the memory via ProgSelfMem. 515 00:29:04,480 --> 00:29:09,600 And this is the cell code that is going to be executing the syscall, 516 00:29:11,880 --> 00:29:12,840 create memfd. 517 00:29:13,480 --> 00:29:17,600 So we prepare the file through our rubber cell. 518 00:29:18,240 --> 00:29:22,660 We write it in the file system, and we check that it exists. 519 00:29:22,920 --> 00:29:24,280 So it exists right here. 520 00:29:25,400 --> 00:29:29,140 Now we call via fork this new child process that 521 00:29:29,140 --> 00:29:32,940 is going to be executed, our cell code. 522 00:29:33,200 --> 00:29:35,600 Now we want to check if this works, of course. 523 00:29:35,820 --> 00:29:37,920 So this is what the function is going to be doing. 524 00:29:38,020 --> 00:29:39,720 And we can see that we have a memfd. 525 00:29:39,740 --> 00:29:43,860 Memory file descriptor that we call dead, just for the sake 526 00:29:43,860 --> 00:29:45,300 of stillness, I guess. 527 00:29:45,720 --> 00:29:48,860 File descriptor 20, ProgID 59. 528 00:29:49,900 --> 00:29:51,280 So this should be working. 529 00:29:54,000 --> 00:29:56,780 OK, and now we are going to be creating this download function 530 00:29:56,780 --> 00:30:01,620 that basically is going to be able to download something 531 00:30:01,620 --> 00:30:05,080 from the internet, load it in the memfd, and run it. 532 00:30:06,840 --> 00:30:08,940 OK, we download it. 533 00:30:09,740 --> 00:30:10,460 This should be working. 534 00:30:10,560 --> 00:30:13,300 And now if I execute the memory file descriptor, 535 00:30:13,340 --> 00:30:15,060 it should be executing kubectl. 536 00:30:15,180 --> 00:30:17,680 And here you have the help of kubectl. 537 00:30:17,800 --> 00:30:20,040 So we were able to bypass these sort of protections 538 00:30:20,040 --> 00:30:22,060 by executing things in memory. 539 00:30:34,380 --> 00:30:39,720 So in order to make this more useful, I decided that I'm going 540 00:30:39,740 --> 00:30:44,680 to make a DDXec, a daemon of DDXec. 541 00:30:46,480 --> 00:30:51,180 So we can just load filelessly this program. 542 00:30:52,080 --> 00:30:56,380 Or in the end, I actually made it a shellcode. 543 00:30:56,600 --> 00:31:00,840 So we run this shellcode with any of these techniques 544 00:31:00,840 --> 00:31:04,500 that allow us to run native code. 545 00:31:05,540 --> 00:31:07,420 We run this shellcode. 546 00:31:07,560 --> 00:31:09,720 And we can tell this shellcode. 547 00:31:09,720 --> 00:31:13,360 This is a code to load a binary with the argument. 548 00:31:16,660 --> 00:31:22,880 And then just we will be able to run any program we want. 549 00:31:26,920 --> 00:31:27,360 OK. 550 00:31:29,720 --> 00:31:31,580 Cool, so basically this will allow 551 00:31:31,580 --> 00:31:33,060 us to use what I have showed you, 552 00:31:33,100 --> 00:31:35,540 but to load binaries more frequently or more usefully. 553 00:31:35,600 --> 00:31:37,580 Instead of doing everything once and again, 554 00:31:37,700 --> 00:31:39,380 it will allow us just to send the binary, 555 00:31:39,380 --> 00:31:40,380 and load it, and execute it. 556 00:31:40,860 --> 00:31:43,100 So in this final demo before the bonus, 557 00:31:43,200 --> 00:31:46,400 that I hope we have time for that, what we are going to have 558 00:31:46,400 --> 00:31:47,640 is a PHP digital list. 559 00:31:47,760 --> 00:31:50,500 We are going to be using the chain word image for this. 560 00:31:51,140 --> 00:31:52,660 In this case, we don't have a cell. 561 00:31:52,820 --> 00:31:54,860 We are going to be getting a PHP reversal. 562 00:31:55,320 --> 00:31:57,900 We are going to be loading the new daemon. 563 00:31:58,440 --> 00:32:00,660 And actually, we are going to finalize this demo, 564 00:32:00,800 --> 00:32:03,280 loading a busy box, because we are in this list. 565 00:32:03,700 --> 00:32:07,140 It will be very ironic if we can have a cell in this list. 566 00:32:07,280 --> 00:32:08,980 So that's what we are going to end up doing. 567 00:32:09,380 --> 00:32:18,080 So let me show you that in PHP, we don't have any bus, 568 00:32:18,180 --> 00:32:19,740 any SH, SH file, not phone. 569 00:32:20,300 --> 00:32:25,320 So we are going to be getting a PHP shell. 570 00:32:25,500 --> 00:32:28,760 And now we are going to be getting a PHP reversal. 571 00:32:29,580 --> 00:32:32,420 So imagine that we have compromised our PHP web 572 00:32:32,420 --> 00:32:37,700 application with an ERC, and we got a reversal with PHP. 573 00:32:39,920 --> 00:32:40,480 OK. 574 00:32:40,780 --> 00:32:41,380 We have it here. 575 00:32:42,460 --> 00:32:44,780 And now maybe you want to follow the explanation. 576 00:32:47,000 --> 00:32:47,560 OK. 577 00:32:47,840 --> 00:32:54,560 So what we are going to do now is execute another PHP, 578 00:32:55,200 --> 00:32:57,240 another interactive session of PHP, 579 00:32:57,520 --> 00:33:00,680 which we will communicate through a pipe with. 580 00:33:02,920 --> 00:33:04,780 We are creating the pipes. 581 00:33:05,680 --> 00:33:07,520 And now starting the process. 582 00:33:09,780 --> 00:33:10,480 OK. 583 00:33:11,310 --> 00:33:17,100 So this interactive PHP, we will send it commands 584 00:33:18,700 --> 00:33:22,620 to write a shellcode, also through the mem file 585 00:33:22,620 --> 00:33:28,880 in the procfs file system. 586 00:33:29,160 --> 00:33:34,500 And we will run this shellcode that I made, 587 00:33:34,620 --> 00:33:36,980 this demonized dd exec. 588 00:33:37,300 --> 00:33:39,360 We have the shellcode in. 589 00:33:39,360 --> 00:33:42,400 We have the shellcode in ARM 64 or x64. 590 00:33:44,040 --> 00:33:49,740 And now we have, OK. 591 00:33:50,200 --> 00:33:52,460 He made a mistake. 592 00:33:54,140 --> 00:33:54,900 Things happen. 593 00:33:56,460 --> 00:33:56,540 OK. 594 00:33:57,560 --> 00:33:58,200 I think we are good. 595 00:34:03,440 --> 00:34:04,860 This is from our complex lab. 596 00:34:04,940 --> 00:34:05,240 You'd think. 597 00:34:07,780 --> 00:34:08,340 OK. 598 00:34:11,520 --> 00:34:14,380 And now we are writing the shellcode, 599 00:34:14,460 --> 00:34:17,380 because there is a shellcode that is a stager that 600 00:34:17,380 --> 00:34:20,180 reads the other shellcode, because the second shellcode 601 00:34:20,180 --> 00:34:21,000 is really, really large. 602 00:34:21,420 --> 00:34:26,140 And now I have created this function that is called memexec. 603 00:34:27,060 --> 00:34:31,560 This function is the one that is used to communicate 604 00:34:32,000 --> 00:34:33,480 with this daemon. 605 00:34:34,300 --> 00:34:38,600 We can throw it an URI. 606 00:34:38,780 --> 00:34:39,340 OK. 607 00:34:39,340 --> 00:34:41,240 But we will put it back to a binary. 608 00:34:41,420 --> 00:34:44,240 It may also be a binary inside the own file system. 609 00:34:46,360 --> 00:34:50,540 And we will pass it the arguments and we can run any command. 610 00:34:51,560 --> 00:34:55,260 In this case, we are downloading them from my server. 611 00:34:58,200 --> 00:34:58,640 OK. 612 00:35:00,260 --> 00:35:03,780 And to finalize this demo, what we are going to be doing is load a 613 00:35:03,780 --> 00:35:06,020 Boonie box inside this rubber shell. 614 00:35:06,740 --> 00:35:09,320 So what we have here is that now we can, for example, use some of the novoons, 615 00:35:09,320 --> 00:35:20,040 call, for example, call set to enumerate environment or run POWD. Basically, we put a bus inside 616 00:35:20,040 --> 00:35:25,740 a distroless, which was the main goal of this, so we just defeated distroless security measures. 617 00:35:36,220 --> 00:35:38,640 . So now the bonus technique. 618 00:35:39,680 --> 00:35:50,320 This is another technique that is kind of old and new at the same time. The exec has 619 00:35:50,320 --> 00:35:58,000 a problem that we need to pass the file and load it. And a loader is really, really complex. 620 00:35:58,280 --> 00:36:08,620 I have skipped a lot of peculiarities that ELF have and extensions from ENU and other 621 00:36:08,640 --> 00:36:17,040 systems and things like that. They have others. That's mayhem. So you can have, like, compressed 622 00:36:17,040 --> 00:36:27,520 sections and lots of things. And the exec isn't doing that stuff. So another way to load 623 00:36:27,520 --> 00:36:38,620 filelessly a binary is to use DL open, but tricking the loader into thinking that it's 624 00:36:38,640 --> 00:36:42,560 a binary. The binary is somewhat that it isn't. 625 00:36:46,660 --> 00:37:00,360 Okay. So this is a technique that was presented in a paper, I think in 2004. So the idea that 626 00:37:00,360 --> 00:37:08,360 these people had was to ‑‑ you have the binary that you want to execute, raw, in memory, the 627 00:37:08,640 --> 00:37:18,740 ELF. In some way, you get to hook to the syscalls that the loader is going to execute. And then 628 00:37:18,740 --> 00:37:28,560 call DL open. Passing to this function some fake path. Since you are hooked to the syscalls, you 629 00:37:28,560 --> 00:37:38,080 can detect when the loader is trying to open this file path that you have given it. And return a 630 00:37:38,080 --> 00:37:46,320 fake file descriptor. And in subsequent syscalls, you can see if the file descriptor that the 631 00:37:46,320 --> 00:37:53,920 loader is using is the fake one that you gave it. And in that case, you fake the syscall. So in 632 00:37:53,920 --> 00:38:01,360 case that you read ‑‑ that the loader is trying to read from this file that doesn't exist in 633 00:38:01,360 --> 00:38:08,060 the file system, you can just mem copy from the place that you have given it. And then you can 634 00:38:08,080 --> 00:38:13,720 see that you have it in raw in memory and write it where the loader wants to read the data. 635 00:38:16,680 --> 00:38:29,840 Okay. So in the paper, they didn't discuss how to hook to the syscalls. There was someone, his 636 00:38:29,840 --> 00:38:38,060 alias was mimics, that in GitHub has an implementation that the syscalls can hook to the 637 00:38:38,080 --> 00:38:45,720 syscalls. They used code signatures to find the places that the calls to the wrapper functions 638 00:38:45,720 --> 00:38:55,880 that the wrapper functions to the syscalls. And overwrite that calls with calls to its own code. 639 00:38:56,340 --> 00:39:02,600 The problem with this is that code signatures may vary between libraries. And so this isn't 640 00:39:03,560 --> 00:39:08,060 particularly useful because then you ‑‑ if you go to ‑‑ if you go to the syscalls, you can get a 641 00:39:08,080 --> 00:39:12,360 code signature. But if you don't have access to a system, you need to download this library and get 642 00:39:12,360 --> 00:39:25,100 this code signatures. And it is really complicated. So my idea to hook to the syscalls was to install 643 00:39:25,100 --> 00:39:33,120 a wrapper, to install a signal handler for the signal, which is a signal that the kernel 644 00:39:33,120 --> 00:39:38,060 sends to a process. If this process tries to execute, the kernel will try to execute the signal 645 00:39:38,080 --> 00:39:43,500 with an instruction, an invalid instruction. And then go through the memory of the loader, 646 00:39:44,160 --> 00:39:49,860 looking for each syscall instruction, and replace it with an invalid instruction. 647 00:39:52,980 --> 00:40:01,180 So now we are just ‑‑ we have the hook that we need. And we can trick LD with just like the paper 648 00:40:04,560 --> 00:40:05,760 tool. Okay. 649 00:40:08,080 --> 00:40:20,480 So that's all. There's also disgusting to load libraries, but if you make a binary look like a 650 00:40:20,480 --> 00:40:30,400 library, you can also get LD to load this binary. Okay. 651 00:40:31,980 --> 00:40:35,900 So later we are going to show you the links to all these tools that we have just 652 00:40:35,900 --> 00:40:41,080 just show you, the DDX segment, DDLopen, but here I just want to show you that this is 653 00:40:41,080 --> 00:40:47,800 actually working, so we have here a Debian container that I have already downloaded, 654 00:40:48,080 --> 00:40:54,860 memdlopen.sh, and here what we are going to be loading basically is just ls-la passing 655 00:40:54,860 --> 00:40:59,420 in the standard in the binary. So with the ID like this you can see that it's actually 656 00:40:59,420 --> 00:41:05,140 much faster than the DDXX technique, and well, we still are working on this to improve some 657 00:41:05,140 --> 00:41:13,780 things. This is actually a project that I haven't dedicated a lot of time. This needs 658 00:41:13,780 --> 00:41:20,920 a lot of research. I have detected that it has problems with binaries that aren't position 659 00:41:20,920 --> 00:41:30,400 independent, but well, I think that I will manage to fix these issues. Cool. So you want 660 00:41:30,400 --> 00:41:30,880 to show the code? 661 00:41:35,140 --> 00:41:45,500 So this is the shell code. It is long, and it is complicated. Right now it is only implemented 662 00:41:45,500 --> 00:41:54,280 for ARM 64. There is a proof of concept written in C that is much, much more understandable. 663 00:41:54,620 --> 00:42:05,120 You can see the installation of the signal handler. We have also the shell code that 664 00:42:05,140 --> 00:42:16,860 is the replacement of the syscal instructions with invalid ones, and we can see here how 665 00:42:16,860 --> 00:42:22,620 this signal handler is taken from the stack. All the arguments that were intended for 666 00:42:22,820 --> 00:42:32,300 the syscal, checking which syscal it was, and faking each one. 667 00:42:34,220 --> 00:42:38,920 Cool, I think you have all this code in the repository with some comments to help you 668 00:42:38,920 --> 00:42:39,400 understand it. 669 00:42:39,540 --> 00:42:43,100 So actually the repository, you can find it in here. 670 00:42:44,580 --> 00:42:50,380 Here you have memdlopen, remote level injection, we are going to be sharing the slides but 671 00:42:50,380 --> 00:42:53,680 if you want to take a quick photo, I will leave it for 10 seconds. 672 00:42:57,800 --> 00:42:58,200 Cool. 673 00:42:59,160 --> 00:43:04,400 Also, the demos that I have been doing, you have them in this repo if you want to try 674 00:43:04,400 --> 00:43:04,980 them yourself. 675 00:43:05,500 --> 00:43:08,800 I hope to find some nice CTF in the future using this technique. 676 00:43:10,320 --> 00:43:11,000 What else? 677 00:43:11,500 --> 00:43:15,880 Well, also, wants to share with us a challenge you have. 678 00:43:16,460 --> 00:43:19,960 It is just a really small challenge. 679 00:43:21,020 --> 00:43:23,580 But I think it is kind of weird. 680 00:43:23,900 --> 00:43:25,260 It is really easy to solve. 681 00:43:25,380 --> 00:43:28,180 But to understand why it works is difficult. 682 00:43:28,180 --> 00:43:28,860 It is really interesting. 683 00:43:30,000 --> 00:43:34,120 I have shown it to, I posted it on Twitter and I think nobody liked it. 684 00:43:34,340 --> 00:43:39,960 So if you get to solve it and tell me why it works, I will give you a hug. 685 00:43:51,140 --> 00:43:54,480 Some final to end this talk. 686 00:43:54,940 --> 00:43:58,160 Me and my mates are preparing a hacktree AWM. 687 00:43:58,160 --> 00:44:01,680 This is the first red team expert certification that we expect to release in Q4. 688 00:44:01,840 --> 00:44:07,240 So if you are interested in AWS red teaming, just follow us in Twitter, LinkedIn or go 689 00:44:07,240 --> 00:44:07,780 to this page. 690 00:44:07,980 --> 00:44:12,040 And thank you very much to you people and to DEF CON for organizing this. 691 00:44:12,100 --> 00:44:13,680 It has been a great pleasure to be with you. 692 00:44:17,020 --> 00:44:17,480 . 693 00:44:17,480 --> 00:44:17,800 . 694 00:44:17,800 --> 00:44:18,120 . 695 00:44:18,120 --> 00:44:18,660 . 696 00:44:18,660 --> 00:44:18,740 . 697 00:44:20,040 --> 00:44:20,500 . 698 00:44:21,180 --> 00:44:21,640 . 699 00:44:21,640 --> 00:44:21,660 . 700 00:44:22,560 --> 00:44:23,020 .