1 00:00:00,000 --> 00:00:03,800 I'm Sergei Bratis. This is Rebecca Shapiro, who did all the work. 2 00:00:04,560 --> 00:00:08,560 I am a research assistant professor at Dartmouth. 3 00:00:08,840 --> 00:00:11,500 She is a PhD candidate in our lab. 4 00:00:13,400 --> 00:00:21,320 And, you know, for who we are, well, you know, we're here from the frozen north, 5 00:00:21,480 --> 00:00:26,600 the northern Appalachia, and it's a live free or die state. 6 00:00:30,220 --> 00:00:34,660 And this talk will be, as Travis just brilliantly introduced it, 7 00:00:34,740 --> 00:00:39,180 about the deep magic that is being used everywhere whenever a program runs 8 00:00:39,180 --> 00:00:45,180 and is severely underappreciated as any deep magic is. 9 00:00:46,090 --> 00:00:54,620 So what we're going to show you is that there is enough power in just the linker loader 10 00:00:54,620 --> 00:00:59,880 to be borrowed to write in the ELF metadata, 11 00:01:00,000 --> 00:01:05,300 just in ELF metadata, not malformed or anything, 12 00:01:06,060 --> 00:01:09,160 any program you might care to write, 13 00:01:09,320 --> 00:01:12,500 any transformation of your process memory, 14 00:01:12,740 --> 00:01:16,780 any transformation of your runtime that you care to do, 15 00:01:16,900 --> 00:01:21,800 which is to say it's Turing complete, it's got loops, and Bex built it. 16 00:01:23,720 --> 00:01:29,420 The interesting thing about this is, besides illustrating a philosophical principle 17 00:01:29,420 --> 00:01:29,980 on the ELF, 18 00:01:30,000 --> 00:01:33,340 which I will expound profusely in just a few moments, 19 00:01:34,340 --> 00:01:37,760 it happens before memory protections are set, 20 00:01:38,580 --> 00:01:41,620 and it doesn't care about ASLR at all. 21 00:01:41,960 --> 00:01:46,920 Because this is the linker loader, this is the thing that finds the dynamic symbols. 22 00:01:47,740 --> 00:01:49,540 It's the keeper of the names. 23 00:01:51,060 --> 00:01:53,800 And ASLR is nothing to it. 24 00:01:54,600 --> 00:01:59,040 So with that in mind, you know, I'm sure you can find some interesting, 25 00:01:59,760 --> 00:02:03,460 obfuscation, offensive, defensive uses of this thing. 26 00:02:03,680 --> 00:02:05,920 We're going to talk about the general mechanism, 27 00:02:06,000 --> 00:02:10,500 and what it teaches us about what our operating systems really are. 28 00:02:10,760 --> 00:02:12,540 Because this is what hacking is about. 29 00:02:13,020 --> 00:02:17,360 There is the computer as taught and programmed, 30 00:02:17,580 --> 00:02:21,120 and then there is a computer as it exists, really. 31 00:02:22,800 --> 00:02:26,240 We find that computer through exploiting it. 32 00:02:26,740 --> 00:02:29,020 If your system has not been exploited, then you can use it. 33 00:02:29,020 --> 00:02:30,620 You don't understand it. 34 00:02:32,020 --> 00:02:39,300 And exploits these days are no less than programs running on the target. 35 00:02:40,020 --> 00:02:45,000 They come in encoded as crafted data, although there are other delivery methods, 36 00:02:45,200 --> 00:02:46,300 but this is the most common. 37 00:02:46,820 --> 00:02:52,780 And they reliably cause the target to run a computation that it was never intended to run. 38 00:02:53,720 --> 00:02:59,000 It happens to do that by leveraging a bug, an error, 39 00:02:59,000 --> 00:03:02,000 a feature, these are known as primitives, 40 00:03:02,180 --> 00:03:09,340 and there is a brilliant introduction in FRAC 61, Article 6, by JP, 41 00:03:09,580 --> 00:03:12,920 that talks about this particular one, primitive, 42 00:03:13,540 --> 00:03:17,260 which looks like a really weird assembly instruction. 43 00:03:18,720 --> 00:03:21,620 AAB, A4, BMO, right? 44 00:03:21,720 --> 00:03:23,400 Four bytes, almost anywhere. 45 00:03:23,400 --> 00:03:30,140 And if you're familiar with format strings, percent n is that thing that you can leverage 46 00:03:30,640 --> 00:03:38,280 to cause the internals of the print formatter converter to do a computation for you that 47 00:03:38,280 --> 00:03:43,520 was never, ever expected and can be quite a nasty surprise to the owner of the system. 48 00:03:43,800 --> 00:03:52,480 So, in the slide deck on the discs, on the CDs that you got, I have a rather wordy set 49 00:03:52,480 --> 00:03:53,060 of slides. 50 00:03:53,060 --> 00:03:55,160 I have slides explaining why this is interesting. 51 00:03:55,380 --> 00:03:59,760 And then, of course, just a couple of hours before the con, I got this great idea. 52 00:03:59,920 --> 00:04:02,980 Hey, there'd be people who like HP Lovecraft here. 53 00:04:03,700 --> 00:04:04,240 Possibly. 54 00:04:06,660 --> 00:04:07,200 OK. 55 00:04:11,320 --> 00:04:12,800 So, weird machines. 56 00:04:13,700 --> 00:04:21,080 I'm going to talk about this term that got coined when I was giving a talk at Felix Lindner's 57 00:04:21,740 --> 00:04:23,040 Rikiri T-Lab's workshop. 58 00:04:23,060 --> 00:04:26,660 And, you know, that, I absolutely love this picture. 59 00:04:26,840 --> 00:04:35,580 This is what 20th century warfare was supposed to be in, you know, in the eyes of the artist 60 00:04:35,580 --> 00:04:36,760 from the 19th century. 61 00:04:37,180 --> 00:04:39,680 So talk about weird machines, cyber war. 62 00:04:39,860 --> 00:04:43,600 Probably our ideas of cyber war are going to be just as accurate. 63 00:04:46,280 --> 00:04:52,180 But coming back to what is the real computer that is in front of you? 64 00:04:52,180 --> 00:04:54,980 What is the real system as opposed to what is the model of it? 65 00:04:57,100 --> 00:04:59,820 We discover that with exploits. 66 00:04:59,940 --> 00:05:03,080 And then you ask yourself, well, what are exploits? 67 00:05:03,260 --> 00:05:04,800 Are they data or code? 68 00:05:05,540 --> 00:05:06,500 It's interesting. 69 00:05:06,880 --> 00:05:13,420 Exploits are, of course, proofs that your machine is capable of a computation that you 70 00:05:13,420 --> 00:05:14,340 have not foreseen. 71 00:05:14,640 --> 00:05:17,920 It's approved by construction, by shell dropping. 72 00:05:18,100 --> 00:05:20,380 You can't really argue with a root shell. 73 00:05:21,460 --> 00:05:22,160 To put it simply. 74 00:05:22,160 --> 00:05:22,300 It's a code. 75 00:05:22,420 --> 00:05:22,880 Fx. 76 00:05:23,380 --> 00:05:27,960 But exploits show up as inputs, but they run on the target. 77 00:05:28,160 --> 00:05:29,640 So what the hell is up with that? 78 00:05:30,600 --> 00:05:36,160 Is this a von Neumann machine or some strange Harvard architecture machine or maybe Miskatonic 79 00:05:36,160 --> 00:05:39,820 University of Arkham kind of an architecture? 80 00:05:40,340 --> 00:05:46,260 So an exploit is a weird stored data program running on the target. 81 00:05:47,400 --> 00:05:51,220 And if you think about your code as this. 82 00:05:51,320 --> 00:05:52,000 . . 83 00:05:52,160 --> 00:05:52,380 . . 84 00:05:52,380 --> 00:05:57,540 Lovecraftian entity that you just summoned up to work on your data building your mystic 85 00:05:57,540 --> 00:05:57,900 pyramid. 86 00:06:00,200 --> 00:06:02,220 This is actually Sotath. 87 00:06:02,500 --> 00:06:04,200 He is the gate. 88 00:06:04,780 --> 00:06:06,540 He holds the key. 89 00:06:06,660 --> 00:06:07,840 He is the key. 90 00:06:08,880 --> 00:06:09,480 So ... 91 00:06:09,480 --> 00:06:12,360 . . . . . . . . . . . . . . . So, yeah, we're not 92 00:06:12,360 --> 00:06:14,040 summoning anything in this editorial. 93 00:06:14,460 --> 00:06:14,720 . 94 00:06:14,720 --> 00:06:18,560 . . . . . . . . . . . . . . 95 00:06:18,560 --> 00:06:27,120 neither can Dartmouth. So this is your code. Its tentacles reach into your memory and fiddle 96 00:06:27,120 --> 00:06:33,240 with bits of data, pulling it up, pulling it down, rearranging them. And this is how 97 00:06:33,240 --> 00:06:37,840 you, you know, you build the computation that you want. But if you look carefully enough 98 00:06:37,840 --> 00:06:47,560 and this is how people think of their code, oh, there is code, there is data. But if you 99 00:06:47,560 --> 00:06:53,400 have a bunch of little octopi-like things, each with a small number of tentacles, working 100 00:06:53,400 --> 00:07:02,880 on a very small piece of your data that you control, and if some of them are interesting 101 00:07:02,880 --> 00:07:07,840 in that they present a bug or a feature or a primitive, then you can build a machine 102 00:07:08,380 --> 00:07:15,900 out of them by manipulating this metadata. To this machine, your metadata is byte code. 103 00:07:17,100 --> 00:07:23,400 The primitives themselves are the little implementations of that byte code. And this 104 00:07:23,400 --> 00:07:32,780 has been the hacker folklore of advanced exploitation since at least 2000. I take absolutely no 105 00:07:32,780 --> 00:07:40,780 claim except to naming it and dragging it out as a sort of a coherent body of folklore. 106 00:07:41,020 --> 00:07:46,160 If you don't take my word for it, look at Halberd Flake's talk. 107 00:07:46,240 --> 00:07:54,020 Which he gave at Infiltrate last year. Where have we seen that? Well, this is of course 108 00:07:54,020 --> 00:08:00,260 how virtual machines work. A virtual machine has this byte code and you implement those 109 00:08:00,260 --> 00:08:07,480 little byte codes. And the only difference between a weird machine that is what runs 110 00:08:07,480 --> 00:08:12,700 your exploit, the collection of your primitives that pass the control and data flow through 111 00:08:12,700 --> 00:08:15,440 some areas of memory that you control. 112 00:08:15,440 --> 00:08:24,760 Is that for a virtual machine, your byte code is usually contiguous. For a weird machine, 113 00:08:24,920 --> 00:08:32,760 it's not. So that tape is really broken. And so think of your target that does anything 114 00:08:32,760 --> 00:08:43,120 interesting as this rich dancing, singing, milk giving, wooly pig. Right? And it's also 115 00:08:43,120 --> 00:08:44,360 a code running pig. 116 00:08:45,440 --> 00:08:51,880 If you can, if there is enough complexity in the target, how is this for author's Clark 117 00:08:51,880 --> 00:08:57,480 of Fourth Law. Any complex enough memory reading writing program is indistinguishable from a 118 00:08:57,480 --> 00:09:04,500 virtual machine on its data, as long as you can craft the data. Input data for a complex 119 00:09:04,500 --> 00:09:11,700 enough program is indistinguishable from byte code. For the pieces of the grand monster 120 00:09:12,320 --> 00:09:15,420 that you manage to decompose, unwind the tentacles, etc. And so it's like if you take any of these 121 00:09:15,440 --> 00:09:21,900 tentacles of the monster and isolate which memory locations are serving for your implicit 122 00:09:21,900 --> 00:09:35,760 flows. And so the bit of philosophy is this. Code doesn't work on data. Data runs on code. 123 00:09:36,600 --> 00:09:42,980 Input strings run on your parser. As the tentacles reach down, there is strange 124 00:09:42,980 --> 00:09:53,100 bubblings in the monster, in the old one. And this state, the state changes that you 125 00:09:53,100 --> 00:09:59,840 create in it are your expected computation, are your exploit. You can make it explode. 126 00:10:00,220 --> 00:10:06,500 You can make it build not a mystic pyramid but something more useful, such as a root 127 00:10:06,500 --> 00:10:12,960 shell. Packets run on your TCP IP stack. Packets are little programs that run on your 128 00:10:12,960 --> 00:10:18,480 TCP IP stack. The changes in the state of the stack are what you're after. Heap metadata 129 00:10:19,030 --> 00:10:27,720 runs on the heap manager. Format strings run on the print. And ELF metadata runs on your 130 00:10:27,720 --> 00:10:42,940 runtime linker loader, which Bex is now going to present. So think of this entire process 131 00:10:42,960 --> 00:10:50,160 and this entire folklore as bringing the idea that you're programming weird machines when 132 00:10:50,160 --> 00:10:55,740 you're exploiting something and this is all over the place, really. This is the art of 133 00:10:55,740 --> 00:11:03,800 boring code and chaining up the primitives and creating, setting up, instantiating and 134 00:11:03,800 --> 00:11:09,820 programming weird machines. We're showing you a weird machine that's there and that's well 135 00:11:09,820 --> 00:11:12,940 formed. When it någonting, I don't mean shut out old machines. I mean leftover machine. choices to build it. The less disk shaping it, the less dirt. And the more data needed it, the less transparency. But ultimately work properly, that changes. We see 136 00:11:12,940 --> 00:11:22,220 would love to see its users for more interesting purposes with that bex will take you through the 137 00:11:22,220 --> 00:11:30,720 wonderful world of elf thank you sergey a magic forest so i'm just going to try there's a lot 138 00:11:30,720 --> 00:11:36,280 of information i want to give you um not enough time so i might end up skipping um but i really 139 00:11:36,280 --> 00:11:40,780 just want you to get an idea of what's going on and you can continue if you're interested look at 140 00:11:40,780 --> 00:11:46,720 the code i'll post a link um let's start off you know talking about the elves um what's point 141 00:11:46,720 --> 00:11:53,440 what it is point uh the linker and loader and so forth look at some private work um and try to 142 00:11:53,440 --> 00:11:58,540 give you enough background so you can understand how we're using these primitives and getting 143 00:11:58,540 --> 00:12:03,280 something that really works um outside of what you would normally do with elf metadata 144 00:12:04,360 --> 00:12:09,720 and hopefully you'll like hopefully you'll from the uh philosophical reverie 145 00:12:09,720 --> 00:12:10,760 that i did 146 00:12:10,780 --> 00:12:20,540 so what basically the point of elf is just how the different components of the gcc tool chain 147 00:12:20,540 --> 00:12:26,300 communicate um you have the assembler static linker and so forth but in the end there's a 148 00:12:26,300 --> 00:12:32,860 metadata that's structured so that each component knows how to act on on its input um how to lay it 149 00:12:32,860 --> 00:12:39,080 out how to compose it and so forth and mostly i'm looking at the runtime loader and that's all 150 00:12:39,080 --> 00:12:40,140 that's all i care about 151 00:12:41,800 --> 00:12:48,080 um there is a lot of information as in these alt files and i'm not using all of them not 152 00:12:48,080 --> 00:12:56,100 constructing crafting most of it it's but i'm looking with symbols um relocation entries and so 153 00:12:56,100 --> 00:13:01,780 forth um well that's pretty much all i'm working with and the dynamic uh linking information there's 154 00:13:01,780 --> 00:13:08,620 a dynamic table that helps tell the runtime loader where everything is um and that is what i work 155 00:13:08,620 --> 00:13:10,760 with for for the gcc tool chain and so forth um and i'm looking at the runtime loader and that's 156 00:13:10,780 --> 00:13:17,780 where this tool uh set i'm making or i'm calling a tool set and just to give you some idea of how 157 00:13:17,780 --> 00:13:23,500 many different sections are there in elf file each one with its own function in constructing 158 00:13:23,500 --> 00:13:32,580 the runtime in the modern binary more than 30 yes so all data and or code metadata so forth is 159 00:13:32,580 --> 00:13:37,340 contained in elf sections um except the section headers themselves and the segment headers 160 00:13:37,340 --> 00:13:40,760 themselves um typically there's one section header and then there's another section header 161 00:13:40,780 --> 00:13:46,300 per section and the header just helps you look up where in the file it's the section is supposed 162 00:13:46,300 --> 00:13:52,680 to be understand its permissions in memory um read write execute um where it should be loaded 163 00:13:52,680 --> 00:14:00,740 and so forth um and most sections contain either metadata and as a table um of the same type of 164 00:14:00,740 --> 00:14:06,460 metadata in a single section um null terminated strings for the string table mixed data uh 165 00:14:06,460 --> 00:14:10,760 something statically uh allocated um and and and and and and and and and and and and and and 166 00:14:10,780 --> 00:14:17,880 and the code itself which is called text and there are a couple sections i'm focusing on for 167 00:14:17,880 --> 00:14:23,880 this talk uh they are the most useful in trying to craft metadata and get some extra computation 168 00:14:23,880 --> 00:14:31,100 out of the runtime loader and first with the symbol table it symbol tables uh provide um 169 00:14:31,100 --> 00:14:40,700 information on all um variables all um functions and so forth so you know where it's located some 170 00:14:40,780 --> 00:14:47,260 times there's a value sometimes there's but there's other information on the size how to look it up 171 00:14:47,660 --> 00:14:54,540 for the for the linker um and then here's just some example symbols there is one that's a variable 172 00:14:54,540 --> 00:15:02,700 standard in and has a preset value um second one is a function also has some value in there and then 173 00:15:02,700 --> 00:15:07,800 the definition so this is the structure itself there's um a point a name which is actually a 174 00:15:07,800 --> 00:15:10,760 pointer into the string table there is some um a function and then there's a function which is a 175 00:15:10,780 --> 00:15:20,480 um some information about it uh value and size and uh if i may and think of this data as something 176 00:15:20,480 --> 00:15:30,020 that the dynamic linker loader eats and uh finds you the right library loads the right library and 177 00:15:30,020 --> 00:15:38,040 uh connects up the little stub that your call uh goes to uh in the procedure linkage table through 178 00:15:38,040 --> 00:15:39,240 the global offset table 179 00:15:40,780 --> 00:15:51,000 and uh through a little um uh this little stub the dynamic linker gets called finds uh the uh symbol 180 00:15:51,900 --> 00:16:01,360 and loads the symbol into memory and uh if uh since the lookup is actually by name there is a little 181 00:16:01,360 --> 00:16:08,460 hash table involved in that and it just jumps around that hash table until it finds the match 182 00:16:08,840 --> 00:16:10,540 so that it knows uh what 183 00:16:10,780 --> 00:16:21,980 symbol um is uh there uh to be loaded now the thing about this is that the complexity of these 184 00:16:21,980 --> 00:16:33,360 operations is pretty damn high and it starts resembling your uh lovecraftian old one or that 185 00:16:33,740 --> 00:16:40,620 woolly singing mill giving pig and so the question is should we manipulate that data what 186 00:16:40,780 --> 00:16:47,640 can we get? And I'm not really talking about buffer overflows. Assume that it's perfectly 187 00:16:47,640 --> 00:16:55,800 legal. It's just that some of those offsets are going to be pointing at other structures. 188 00:16:56,000 --> 00:17:05,360 And think of my own grandpa in the process. By creating this assortment of interlinked 189 00:17:05,360 --> 00:17:11,680 data, you can actually drive this thing into insanity or into executing any given program. 190 00:17:11,900 --> 00:17:18,280 So I will continue. As we like to say, however, the ELF metadata is just in our vision in 191 00:17:18,280 --> 00:17:26,980 the way we see it. It's the static linker's love letter to the runtime loader. So for 192 00:17:26,980 --> 00:17:32,380 relocation tables this is information from not only the runtime loader but the runtime 193 00:17:32,380 --> 00:17:35,340 linker. And it the rel is the data. And it's the data that's being sent to the runtime 194 00:17:35,340 --> 00:17:40,360 The first one I listed is for the runtime loader exclusively and every entry in its 195 00:17:40,360 --> 00:17:41,920 process during load time. 196 00:17:42,220 --> 00:17:45,480 And the second one is for the dynamic linker, the PLT. 197 00:17:45,680 --> 00:17:48,320 And that's processed as needed at runtime. 198 00:17:50,180 --> 00:17:56,620 Information contained in these entries include offset, where you are eventually writing the 199 00:17:56,620 --> 00:18:01,340 output of this operation and usually you calculate some sort of address, write it in. 200 00:18:02,180 --> 00:18:07,460 Info includes the symbol that if there's a symbol that the entry works with, it's a symbol 201 00:18:07,460 --> 00:18:07,760 number. 202 00:18:07,960 --> 00:18:10,360 Also the type of relocation entry is there. 203 00:18:10,660 --> 00:18:16,920 And then for AMD64, there's also addendum which allows you to, depending on the type 204 00:18:16,920 --> 00:18:20,940 of relocation entry, add another value into the final computation. 205 00:18:21,520 --> 00:18:25,100 Now imagine, this is the thing that keeps the ASLR working. 206 00:18:25,700 --> 00:18:31,260 So you've got this code that you've compiled and you're about to load at this address. 207 00:18:31,260 --> 00:18:33,500 Which is in the ELF file. 208 00:18:33,640 --> 00:18:35,520 But this is not where you want to load it. 209 00:18:35,620 --> 00:18:40,520 You want to randomize that base or you can't load the library at that address because another 210 00:18:40,520 --> 00:18:43,860 library is taking that up in the process's virtual address space. 211 00:18:44,320 --> 00:18:50,440 And so what you need and what you are getting out of this mechanism is a little automaton 212 00:18:51,280 --> 00:18:59,060 that goes and patches your binary code or data based on these data structures. 213 00:18:59,600 --> 00:19:00,980 So you look at this. 214 00:19:00,980 --> 00:19:06,760 You see a useful auxiliary mechanism for relocating data, moving it around in memory. 215 00:19:06,920 --> 00:19:13,440 When we look at this, we see this strange creature that touches, you know, lifts up 216 00:19:13,860 --> 00:19:19,400 those relocation entries and writes other things in memory. 217 00:19:19,820 --> 00:19:26,040 And by the way, we're inspired in this by the locreate, by scape. 218 00:19:26,260 --> 00:19:29,340 So look it up in uninformed number six. 219 00:19:29,440 --> 00:19:29,500 That's awesome. 220 00:19:34,080 --> 00:19:40,800 So again, what you see is a machine that you can program by crafting those relocation entries. 221 00:19:41,140 --> 00:19:45,380 Not making them illegal or anything. 222 00:19:45,740 --> 00:19:49,360 Just driving that engine to do something more. 223 00:19:49,640 --> 00:19:56,120 So two other sections that are interesting are the global offset table and the procedure 224 00:19:56,120 --> 00:19:56,980 linkage table. 225 00:19:57,300 --> 00:20:00,260 And the global offset table is merely a table of addresses. 226 00:20:00,260 --> 00:20:06,520 It's used by the linker to look up any LinkedIn functions that you do not know or exactly 227 00:20:06,520 --> 00:20:08,240 they reside until runtime. 228 00:20:09,100 --> 00:20:16,800 It also contains a pointer to the linker function that's actually invoked during linking, runtime 229 00:20:16,800 --> 00:20:17,100 linking. 230 00:20:17,340 --> 00:20:24,380 And this link map structure that contains everything you really need or the linker 231 00:20:24,380 --> 00:20:27,900 needs to perform the linking about for each. 232 00:20:28,040 --> 00:20:30,120 And there is one structure for each library for each. 233 00:20:30,260 --> 00:20:30,820 There's the ELF object. 234 00:20:30,980 --> 00:20:31,960 That is there. 235 00:20:32,100 --> 00:20:33,480 There is a link map structure. 236 00:20:34,180 --> 00:20:41,660 The PLT contains instructions that just work in tandem with the GOT to invoke the fixup 237 00:20:41,660 --> 00:20:45,760 function if needed or to invoke the function itself if it's been patched. 238 00:20:45,860 --> 00:20:51,600 Bex, I think you might want to actually start explaining those symbols. 239 00:20:52,180 --> 00:20:52,720 It's a global offset. 240 00:20:52,720 --> 00:20:54,900 This is a global offset table, obviously. 241 00:20:55,260 --> 00:20:57,960 You probably got the relocation table before. 242 00:20:59,600 --> 00:21:00,240 Which is hard. 243 00:21:00,260 --> 00:21:00,800 Which is that one. 244 00:21:01,540 --> 00:21:02,580 It's been so long. 245 00:21:03,020 --> 00:21:05,160 There's a procedure being linked to another procedure. 246 00:21:05,160 --> 00:21:05,540 Oh, yes. 247 00:21:05,560 --> 00:21:07,040 This is a procedure linkage table. 248 00:21:07,160 --> 00:21:07,400 Exactly. 249 00:21:09,920 --> 00:21:10,440 Okay. 250 00:21:10,500 --> 00:21:11,260 Shall we go on? 251 00:21:11,560 --> 00:21:11,980 Yes. 252 00:21:12,480 --> 00:21:19,500 And this dynamic table, which contains all the information needed by the runtime loader. 253 00:21:19,940 --> 00:21:25,280 And there's basically a tag that says what the value is. 254 00:21:26,160 --> 00:21:28,900 And I will ‑‑ these are a bunch of ones that we use. 255 00:21:29,280 --> 00:21:29,800 Okay. 256 00:21:29,800 --> 00:21:31,620 Let's see if we can find them in our work. 257 00:21:31,880 --> 00:21:32,100 Yeah. 258 00:21:32,180 --> 00:21:35,220 Remember, these are things that point at other things. 259 00:21:35,620 --> 00:21:39,720 These are offsets of strings in symbol tables. 260 00:21:40,080 --> 00:21:46,120 These are offsets into library files where that symbol live. 261 00:21:47,200 --> 00:21:53,720 These are offsets into a section where things need to be patched on relocation. 262 00:21:53,880 --> 00:21:57,980 Now imagine them having a great family. 263 00:21:57,980 --> 00:21:58,200 I'm going to continue. 264 00:21:58,720 --> 00:21:59,100 Yes. 265 00:21:59,800 --> 00:22:03,640 This might ‑‑ all this information is in the headers themselves. 266 00:22:03,940 --> 00:22:07,920 But there's a separate table that has a copy of this information just for the linker. 267 00:22:08,020 --> 00:22:13,740 And the dynamic linker and runtime loader, in the end, after it's loaded, only work with 268 00:22:13,740 --> 00:22:15,440 this table right here. 269 00:22:15,780 --> 00:22:20,780 So if it ever needs to know where the relocation entries are, it looks at the value of DT 270 00:22:20,780 --> 00:22:27,260 Rella and it knows the size, symbol, location of the symbol table, the location of the GOT, 271 00:22:27,780 --> 00:22:29,780 and anything else that's needed for cleaning. 272 00:22:29,780 --> 00:22:32,280 And that's what we call a clean execution in the runtime loading and linking stage. 273 00:22:34,940 --> 00:22:41,040 So just to give you an example of what happens during loading and linking, suppose we call 274 00:22:41,040 --> 00:22:41,720 exec on ping. 275 00:22:42,040 --> 00:22:47,580 What happens in the kernel is there's initial setup of the address space where we have the 276 00:22:47,580 --> 00:22:48,380 executable mapped. 277 00:22:48,620 --> 00:22:51,340 We have the loader itself mapped and a stack. 278 00:22:51,620 --> 00:22:55,500 And an entry point is set to invoke this loader. 279 00:22:56,200 --> 00:23:01,620 And so now in user space, the loader takes over and starts sending up more things. 280 00:23:01,760 --> 00:23:03,060 It looks at the needed libraries. 281 00:23:03,580 --> 00:23:07,800 It builds in a heap, stack ‑‑ oh, the stack was already there. 282 00:23:08,460 --> 00:23:11,280 And then it sets the entry point to the executable. 283 00:23:11,860 --> 00:23:17,420 And that is ‑‑ in that part, that's all we are working with in that part of the process. 284 00:23:18,500 --> 00:23:25,280 And you'll see that red is execute only, yellow is read only, green is read right, which is 285 00:23:25,280 --> 00:23:26,180 important to us. 286 00:23:26,720 --> 00:23:34,220 And each library has its own address space for the read only, read right, executable code. 287 00:23:34,740 --> 00:23:37,060 And this is an actual memory layout of ping. 288 00:23:37,360 --> 00:23:40,760 I took out some parts to fit on the screen. 289 00:23:41,080 --> 00:23:42,420 And the stack is at the bottom. 290 00:23:42,620 --> 00:23:44,420 You can't see, but it's on the slide. 291 00:23:44,940 --> 00:23:46,460 It's just hanging off the bottom. 292 00:23:48,700 --> 00:23:53,840 So going back to this, let's zoom in to the executable. 293 00:23:54,020 --> 00:23:55,260 And the nice thing is that the executable is a little bit smaller. 294 00:23:56,280 --> 00:24:01,540 Normally during run time ‑‑ sorry, normally during execution, even if you have ASLR, 295 00:24:01,840 --> 00:24:07,180 you have to actually put in extra compilation options to get the executable itself to be mapped 296 00:24:07,180 --> 00:24:08,600 in a different location. 297 00:24:10,220 --> 00:24:12,680 But I haven't really run into that. 298 00:24:12,880 --> 00:24:18,920 Even with ping, which is something that set UID root, the executable is always mapped to the same 299 00:24:18,920 --> 00:24:22,680 spot, at least in Ubuntu, that version that I was working with. 300 00:24:22,960 --> 00:24:24,820 So if we zoom in and see what's there. 301 00:24:25,440 --> 00:24:27,840 All these sections I was talking about are mapped in. 302 00:24:28,000 --> 00:24:29,980 Some in read, execute only space. 303 00:24:30,140 --> 00:24:31,000 Some in read only. 304 00:24:31,800 --> 00:24:35,220 And global offset table and data, so forth, in read right. 305 00:24:35,920 --> 00:24:38,840 And then something to note is the dynamic table itself. 306 00:24:39,720 --> 00:24:44,140 During the relocation process, I have found that I can write to it. 307 00:24:44,340 --> 00:24:47,480 I haven't actually looked to see at what point it's read only. 308 00:24:47,600 --> 00:24:51,540 But after everything is set up, this dynamic table is read only. 309 00:24:52,760 --> 00:25:01,040 So in our perspective, we are interested in these entries that we see, the symbol table, 310 00:25:01,220 --> 00:25:02,960 the relocation entries and so forth. 311 00:25:03,360 --> 00:25:07,540 And the DGOT we're interested in, which is actually writable. 312 00:25:09,660 --> 00:25:15,540 But then there's also the metadata, these link map structures, which is actually part of the 313 00:25:15,540 --> 00:25:18,440 linker loader memory and heap. 314 00:25:19,420 --> 00:25:26,520 And so these link map structures have, is what we end up working with to do what we need. 315 00:25:26,700 --> 00:25:30,840 This is not the entire structure definition, just things that we end up working with. 316 00:25:31,080 --> 00:25:34,040 It contains the base address of where the library is loaded. 317 00:25:34,240 --> 00:25:39,040 It actually contains pointers to the dynamic table entries that we're interested in. 318 00:25:39,880 --> 00:25:44,060 It contains a bit to say whether or not it's been relocated before. 319 00:25:45,720 --> 00:25:48,420 And some other things that we end up working with. 320 00:25:48,420 --> 00:25:55,380 Just to get this code, this engine working as something that's Turing complete. 321 00:25:55,760 --> 00:25:56,940 But I won't go into details. 322 00:25:57,460 --> 00:26:03,300 And then also, since every ELF object that's loaded has its own structure, it's in a doubly linked list. 323 00:26:03,580 --> 00:26:07,040 And so there's next and previous pointers in each link map structure. 324 00:26:07,440 --> 00:26:11,380 And that's how, they're ordered in how symbols should be looked up. 325 00:26:12,700 --> 00:26:16,240 So there are fun ways to craft ELF metadata. 326 00:26:17,300 --> 00:26:18,400 And may have a lot of fun ways to do that. 327 00:26:18,420 --> 00:26:24,200 And in fact, 61.8, as we used it to inject object files. 328 00:26:24,800 --> 00:26:29,400 You can intercept library calls to run injected code. 329 00:26:29,620 --> 00:26:32,500 And so there's been work by Cesar Mayhem in that. 330 00:26:34,240 --> 00:26:36,680 Resident in attacker built library. 331 00:26:36,920 --> 00:26:46,260 So you can inject code into a library and get that loaded either by playing with some environmental variables LD preload. 332 00:26:46,340 --> 00:26:48,400 Or you can play with the ELF structure. 333 00:26:48,420 --> 00:26:52,440 And put another entry in the dynamic table. 334 00:26:53,500 --> 00:26:54,840 And this is the point. 335 00:26:55,000 --> 00:27:00,080 You do not understand those structures until you make them do something interesting. 336 00:27:01,040 --> 00:27:04,640 Then you actually know what the code behind them. 337 00:27:04,680 --> 00:27:07,540 What that automaton is doing when it's getting that data. 338 00:27:07,780 --> 00:27:09,380 And so cheating the ELF by the GRUG. 339 00:27:09,440 --> 00:27:10,620 I totally recommend. 340 00:27:10,780 --> 00:27:11,640 This is a classic. 341 00:27:12,840 --> 00:27:15,300 That explained this for the first time. 342 00:27:15,420 --> 00:27:17,120 And this was for the longest time. 343 00:27:19,080 --> 00:27:22,900 This is a detailed excursion into how these things really work. 344 00:27:23,360 --> 00:27:27,440 And so Locreate is an inspiring worker. 345 00:27:27,720 --> 00:27:29,100 Helped inspire what we did. 346 00:27:29,360 --> 00:27:33,100 It actually uses relocation entries to pack and unpack binaries. 347 00:27:33,980 --> 00:27:41,440 And we're kind of taking that a step further by just using relocation entries to act as code itself. 348 00:27:41,540 --> 00:27:43,400 Or to have an effect. 349 00:27:43,500 --> 00:27:47,460 Some sort of Turing complete machine running in just runtime loading. 350 00:27:48,420 --> 00:27:50,560 But Locreate is a patcher. 351 00:27:50,640 --> 00:27:52,500 So it can go. 352 00:27:52,640 --> 00:27:53,780 It ran over code. 353 00:27:54,240 --> 00:27:56,200 And transformed the code. 354 00:27:56,340 --> 00:27:57,000 Because hey. 355 00:27:57,120 --> 00:27:58,200 If we're rewriting bytes. 356 00:27:58,440 --> 00:28:00,260 We need not rewrite just addresses. 357 00:28:00,620 --> 00:28:03,580 We can rewrite opcodes and immediate as well. 358 00:28:03,920 --> 00:28:06,800 And so how is that for an obfuscator. 359 00:28:06,800 --> 00:28:07,820 The obfuscator packer. 360 00:28:07,900 --> 00:28:08,340 Unpacker. 361 00:28:08,440 --> 00:28:11,660 But let's build something bigger with loops and branches. 362 00:28:11,960 --> 00:28:14,740 However in the tool chain we're working with. 363 00:28:14,800 --> 00:28:17,180 The text section is actually not writable. 364 00:28:17,280 --> 00:28:18,180 By the time. 365 00:28:18,420 --> 00:28:19,540 The relocation entries are processed. 366 00:28:19,860 --> 00:28:21,560 So he was working with PE. 367 00:28:21,860 --> 00:28:22,780 And it worked. 368 00:28:23,240 --> 00:28:24,720 So here's our warning. 369 00:28:24,940 --> 00:28:28,080 I actually am proud to take a little side note off of this. 370 00:28:28,240 --> 00:28:30,440 Yesterday I got it working with ASLR. 371 00:28:30,960 --> 00:28:33,260 With when the libraries and the stack were randomized. 372 00:28:33,580 --> 00:28:36,800 But this is specific to our architecture that we're using. 373 00:28:36,920 --> 00:28:37,680 AMD 64. 374 00:28:37,960 --> 00:28:38,920 On Ubuntu. 375 00:28:39,100 --> 00:28:41,620 There's no reason that it won't work on others. 376 00:28:42,180 --> 00:28:45,040 But the details are specific to this. 377 00:28:46,940 --> 00:28:48,060 So first. 378 00:28:48,240 --> 00:28:48,400 We're going to do a little bit of a demo. 379 00:28:48,420 --> 00:28:48,640 So first off. 380 00:28:48,920 --> 00:28:50,220 We need our symbol table. 381 00:28:50,360 --> 00:28:52,280 And relocation entries. 382 00:28:52,520 --> 00:28:54,040 In a place that we can read write. 383 00:28:54,600 --> 00:28:55,880 Normally it's in read only. 384 00:28:56,200 --> 00:28:58,260 So we inject it using the RC toolkit. 385 00:28:59,980 --> 00:29:00,700 Right there. 386 00:29:00,780 --> 00:29:01,400 Below the data. 387 00:29:01,660 --> 00:29:02,520 And there's space there. 388 00:29:02,740 --> 00:29:03,800 And it's a great toolkit. 389 00:29:04,040 --> 00:29:05,900 And takes care of any rewrite. 390 00:29:07,440 --> 00:29:09,140 Updating the headers for us. 391 00:29:11,240 --> 00:29:13,540 So for the relocation entries that are used. 392 00:29:13,740 --> 00:29:15,600 There's lots of them. 393 00:29:15,740 --> 00:29:16,900 And we only need three. 394 00:29:19,400 --> 00:29:20,440 There's copy. 395 00:29:20,760 --> 00:29:22,640 And basically what it does is. 396 00:29:23,820 --> 00:29:26,780 It acts as a mem copy. 397 00:29:27,160 --> 00:29:28,060 And you provide. 398 00:29:28,540 --> 00:29:29,840 You give it a symbol. 399 00:29:30,200 --> 00:29:32,540 And the symbol value is treated as a pointer. 400 00:29:33,460 --> 00:29:34,640 Where to copy from. 401 00:29:34,860 --> 00:29:35,960 The symbol is also. 402 00:29:36,100 --> 00:29:37,660 You get the size of how much to copy. 403 00:29:37,780 --> 00:29:38,960 And then you copy to the offset. 404 00:29:39,520 --> 00:29:41,020 Specified in the relocation entry. 405 00:29:41,960 --> 00:29:43,760 There's another version. 406 00:29:44,000 --> 00:29:46,340 Where you give a symbol. 407 00:29:46,500 --> 00:29:47,440 And an offset. 408 00:29:48,420 --> 00:29:49,180 With a value in the symbol. 409 00:29:49,360 --> 00:29:51,260 And copies that over into the offset. 410 00:29:51,560 --> 00:29:53,240 And it adds an addend. 411 00:29:53,300 --> 00:29:54,160 If there's one. 412 00:29:54,400 --> 00:29:56,280 And the base of the library. 413 00:29:56,760 --> 00:29:59,040 Since we're working in executable. 414 00:29:59,140 --> 00:30:00,240 The base is always zero. 415 00:30:01,320 --> 00:30:02,960 Unless it's actually randomized. 416 00:30:03,200 --> 00:30:04,420 It's mapped to the. 417 00:30:04,660 --> 00:30:05,760 Well it's actually not zero. 418 00:30:06,040 --> 00:30:09,040 It's I believe like 40,000. 419 00:30:09,240 --> 00:30:10,140 But yeah. 420 00:30:10,400 --> 00:30:13,260 If this doesn't look like bytecode to you. 421 00:30:13,500 --> 00:30:16,180 Then I must have confused everyone. 422 00:30:16,520 --> 00:30:16,760 Yeah. 423 00:30:18,420 --> 00:30:19,360 I'm not sure about anyone else. 424 00:30:20,340 --> 00:30:22,140 And then so the last one. 425 00:30:22,220 --> 00:30:23,700 Relative where it doesn't use a symbol. 426 00:30:23,880 --> 00:30:25,820 It's just addend plus a base. 427 00:30:25,900 --> 00:30:26,300 If there's one. 428 00:30:26,540 --> 00:30:28,800 And you write it into the offset. 429 00:30:28,960 --> 00:30:30,100 Specified in the entry. 430 00:30:30,760 --> 00:30:32,160 So we have arithmetic. 431 00:30:33,460 --> 00:30:33,980 Close. 432 00:30:34,500 --> 00:30:35,840 We do have arithmetic. 433 00:30:36,000 --> 00:30:36,980 That's very straightforward. 434 00:30:38,200 --> 00:30:39,680 Another thing we use. 435 00:30:39,700 --> 00:30:40,960 Is these indirect functions. 436 00:30:41,140 --> 00:30:46,240 That are supported by the runtime loader. 437 00:30:46,280 --> 00:30:47,600 And these are special types of. 438 00:30:48,540 --> 00:30:48,980 Symbols. 439 00:30:48,980 --> 00:30:50,540 In which the value of the symbol. 440 00:30:50,620 --> 00:30:52,540 Is actually some function. 441 00:30:52,660 --> 00:30:53,500 That runs. 442 00:30:53,780 --> 00:30:54,880 And whatever is returned. 443 00:30:55,160 --> 00:30:56,360 Gets written into the offset. 444 00:30:57,240 --> 00:30:58,820 And we found it useful. 445 00:31:00,280 --> 00:31:01,740 Because you need branching. 446 00:31:01,920 --> 00:31:03,180 Normally here I'd have the video. 447 00:31:03,340 --> 00:31:05,120 But a lot of you saw it already. 448 00:31:05,280 --> 00:31:06,640 Many, many years ago. 449 00:31:07,700 --> 00:31:09,140 You don't want to hear me sing. 450 00:31:09,180 --> 00:31:10,180 So what did we do? 451 00:31:10,380 --> 00:31:12,160 We decided to find the simplest. 452 00:31:12,160 --> 00:31:14,300 Most annoying. 453 00:31:15,080 --> 00:31:15,800 Programming language. 454 00:31:15,900 --> 00:31:16,480 To work with. 455 00:31:16,640 --> 00:31:18,720 And implement that in these relocation entries. 456 00:31:19,320 --> 00:31:21,340 So traditionally there's eight instructions. 457 00:31:22,140 --> 00:31:23,440 There's a notion of a tape. 458 00:31:23,640 --> 00:31:25,900 And a pointer into this tape. 459 00:31:26,040 --> 00:31:27,720 And you can write increment decrement values. 460 00:31:28,080 --> 00:31:30,180 And test and branch and output byte. 461 00:31:30,560 --> 00:31:31,820 So you have standard out. 462 00:31:31,980 --> 00:31:34,160 Or printf or whatnot. 463 00:31:34,440 --> 00:31:35,820 Or you can read in a byte. 464 00:31:36,520 --> 00:31:37,260 Get char. 465 00:31:38,020 --> 00:31:39,320 But we actually are not. 466 00:31:39,680 --> 00:31:40,700 Worrying about those two. 467 00:31:42,300 --> 00:31:45,120 And I can imagine implementing it. 468 00:31:45,200 --> 00:31:47,160 But that's not in our version. 469 00:31:47,780 --> 00:31:49,520 So as an example. 470 00:31:50,220 --> 00:31:53,140 If we do plus angle bracket minus. 471 00:31:53,320 --> 00:31:54,800 So we increment byte at the pointer. 472 00:31:54,960 --> 00:31:55,700 We move the pointer. 473 00:31:55,940 --> 00:31:57,220 Then we decrement the byte. 474 00:31:57,900 --> 00:31:59,060 This is what happens. 475 00:32:00,000 --> 00:32:01,580 We increment the byte. 476 00:32:01,720 --> 00:32:02,440 The tape pointer. 477 00:32:02,780 --> 00:32:03,800 It gets moved. 478 00:32:04,300 --> 00:32:05,220 We decrement. 479 00:32:05,420 --> 00:32:06,960 And it's fairly straightforward. 480 00:32:07,560 --> 00:32:11,040 It's just really hard to write anything useful in. 481 00:32:11,160 --> 00:32:12,140 Because that's hello world. 482 00:32:12,160 --> 00:32:12,960 You know. 483 00:32:14,600 --> 00:32:16,220 Think of it as the Turing machine. 484 00:32:16,300 --> 00:32:17,020 But more fun. 485 00:32:17,640 --> 00:32:20,000 Or more tingling in your brain. 486 00:32:20,220 --> 00:32:20,680 Fun. 487 00:32:21,280 --> 00:32:21,740 Fun. 488 00:32:22,080 --> 00:32:22,700 You know. 489 00:32:24,540 --> 00:32:26,220 So this is sort of the process. 490 00:32:26,660 --> 00:32:30,140 Of compiling brain fuck into elf metadata. 491 00:32:30,460 --> 00:32:31,420 You have an executable. 492 00:32:31,600 --> 00:32:32,480 That you specify. 493 00:32:33,640 --> 00:32:35,000 You have your brain fuck source. 494 00:32:35,500 --> 00:32:38,220 And there is some configuration that you need to do. 495 00:32:38,340 --> 00:32:40,520 And I've automated it as much as possible. 496 00:32:41,280 --> 00:32:41,960 You run. 497 00:32:42,160 --> 00:32:43,320 You invoke this compiler. 498 00:32:43,780 --> 00:32:45,080 And you get just another executable. 499 00:32:45,380 --> 00:32:46,780 And that's it. 500 00:32:46,840 --> 00:32:48,420 You hand it to the loader. 501 00:32:48,540 --> 00:32:49,480 So first of all. 502 00:32:49,540 --> 00:32:51,760 You write your exploit in brain fuck. 503 00:32:52,000 --> 00:32:52,420 Okay. 504 00:32:53,780 --> 00:32:56,340 Actually you can compile to brain fuck from C. 505 00:32:56,540 --> 00:32:58,720 Or your favorite functional language. 506 00:32:58,960 --> 00:33:00,600 I feel like we'll hit some limits though. 507 00:33:00,680 --> 00:33:01,440 On size. 508 00:33:01,640 --> 00:33:02,440 It's not the. 509 00:33:04,440 --> 00:33:05,800 We can do better. 510 00:33:05,920 --> 00:33:06,620 We can optimize. 511 00:33:07,100 --> 00:33:07,960 Oh my god. 512 00:33:08,000 --> 00:33:08,880 It's full of symbols. 513 00:33:09,820 --> 00:33:11,640 Well it's full of relocation entries. 514 00:33:12,080 --> 00:33:14,160 So this is the configuration. 515 00:33:15,340 --> 00:33:16,780 There's a lot of stuff we collect. 516 00:33:16,800 --> 00:33:18,080 At compile time automatically. 517 00:33:19,240 --> 00:33:20,060 And so the. 518 00:33:20,200 --> 00:33:22,720 From the dynamic table actually. 519 00:33:22,880 --> 00:33:23,840 So the address that they got. 520 00:33:25,600 --> 00:33:26,420 In theory. 521 00:33:26,520 --> 00:33:28,400 You can get some sort of ROP gadget. 522 00:33:28,580 --> 00:33:29,400 That returns zero. 523 00:33:30,720 --> 00:33:31,760 So it's not. 524 00:33:31,820 --> 00:33:33,400 We don't have it done automatically. 525 00:33:33,640 --> 00:33:34,900 But you can imagine if it exists. 526 00:33:36,620 --> 00:33:38,080 And the location of memory. 527 00:33:38,240 --> 00:33:40,120 Of some other things in the dynamic table. 528 00:33:40,580 --> 00:33:42,440 And then data collected at run time. 529 00:33:42,580 --> 00:33:43,520 Using everything else. 530 00:33:43,560 --> 00:33:45,120 Is the base address of lib C. 531 00:33:45,280 --> 00:33:46,000 The loader. 532 00:33:46,100 --> 00:33:47,540 And a stack location. 533 00:33:47,860 --> 00:33:49,280 Which that's new. 534 00:33:49,760 --> 00:33:52,620 We found there's the auxiliary vector. 535 00:33:52,860 --> 00:33:53,740 Which is basically. 536 00:33:54,860 --> 00:33:57,500 Well the loader's love note. 537 00:33:57,600 --> 00:33:58,660 To the executable itself. 538 00:33:58,860 --> 00:33:59,800 With all its arguments. 539 00:34:00,000 --> 00:34:00,960 Environmental variables. 540 00:34:01,100 --> 00:34:01,400 Etc. 541 00:34:06,620 --> 00:34:08,640 And so I can get to the loader. 542 00:34:08,660 --> 00:34:09,740 I can read this value. 543 00:34:09,880 --> 00:34:11,700 And get some offsets. 544 00:34:12,060 --> 00:34:13,280 Yeah isn't this nifty. 545 00:34:13,520 --> 00:34:15,060 Not only the executable knows. 546 00:34:15,220 --> 00:34:17,120 How to call the loader from its stubs. 547 00:34:17,680 --> 00:34:18,820 Obviously it needs to. 548 00:34:18,960 --> 00:34:20,980 But there is a pointer back as well. 549 00:34:21,400 --> 00:34:24,440 So I am not giving you the full setup. 550 00:34:24,680 --> 00:34:25,820 You need to read the code for that. 551 00:34:25,940 --> 00:34:27,080 I'm trying to simplify things. 552 00:34:27,420 --> 00:34:30,000 But to the basics of what you need. 553 00:34:30,040 --> 00:34:30,660 For a brain fuck. 554 00:34:31,060 --> 00:34:31,960 There's more you need. 555 00:34:32,020 --> 00:34:33,500 If you wanted to execute cleanly. 556 00:34:36,620 --> 00:34:37,380 And then have. 557 00:34:38,280 --> 00:34:40,040 Your executable itself complete. 558 00:34:41,240 --> 00:34:42,840 There you need more than just this. 559 00:34:43,280 --> 00:34:44,580 And I put a little note at the end. 560 00:34:44,740 --> 00:34:46,080 So for the symbol table. 561 00:34:46,260 --> 00:34:48,740 We inject a couple symbols. 562 00:34:49,480 --> 00:34:50,300 The address. 563 00:34:50,840 --> 00:34:53,060 There's one that holds the address of the tape head. 564 00:34:53,260 --> 00:34:54,960 Or wherever the tape head is pointing at. 565 00:34:55,840 --> 00:34:57,600 The copy of the tape head value. 566 00:34:57,740 --> 00:34:58,280 And address. 567 00:34:59,140 --> 00:34:59,580 Of. 568 00:35:01,600 --> 00:35:02,420 Oh yes. 569 00:35:02,700 --> 00:35:04,700 An address of this symbol. 570 00:35:04,820 --> 00:35:06,080 The copy of the tape head value. 571 00:35:06,620 --> 00:35:06,960 That we need. 572 00:35:07,420 --> 00:35:10,320 So that we can do some copy operations. 573 00:35:10,580 --> 00:35:11,420 So observe. 574 00:35:11,600 --> 00:35:13,740 The tape is now this. 575 00:35:15,020 --> 00:35:15,980 The tape. 576 00:35:16,120 --> 00:35:17,560 Is now spread through. 577 00:35:17,840 --> 00:35:19,580 The elf metadata. 578 00:35:19,980 --> 00:35:20,820 But that's okay. 579 00:35:21,000 --> 00:35:22,480 Because you see an RTLD. 580 00:35:22,640 --> 00:35:23,980 I see a bytecode interpreter. 581 00:35:24,300 --> 00:35:27,900 The last symbol is just the ROP gadget that we need. 582 00:35:27,980 --> 00:35:29,000 Something that returns zero. 583 00:35:29,200 --> 00:35:30,260 An address of that. 584 00:35:30,520 --> 00:35:32,860 And for the relocation entries. 585 00:35:33,000 --> 00:35:33,940 We actually have. 586 00:35:36,620 --> 00:35:38,320 We have some setup that's needed. 587 00:35:38,460 --> 00:35:39,160 So we can clean up. 588 00:35:39,680 --> 00:35:41,840 And so we can find all these libraries. 589 00:35:42,000 --> 00:35:42,920 Base addresses of libraries. 590 00:35:43,380 --> 00:35:44,820 And then we have all the instructions. 591 00:35:45,380 --> 00:35:46,960 Then we have some cleanup instructions. 592 00:35:47,680 --> 00:35:48,640 And I'm calling them instructions. 593 00:35:48,960 --> 00:35:50,040 They're just relocation entries. 594 00:35:50,140 --> 00:35:51,220 That do something useful. 595 00:35:51,760 --> 00:35:53,180 And then some cleaning. 596 00:35:53,340 --> 00:35:54,220 And then the originals. 597 00:35:54,240 --> 00:35:55,960 So we can actually have the executable. 598 00:35:56,160 --> 00:35:58,200 Execute after we do whatever we want. 599 00:35:59,560 --> 00:36:02,200 So this is how the tape pointer looks. 600 00:36:02,320 --> 00:36:03,060 To us. 601 00:36:06,620 --> 00:36:08,140 The tape head is pointing at. 602 00:36:08,180 --> 00:36:08,740 And a copy. 603 00:36:09,080 --> 00:36:10,520 And just as a note. 604 00:36:10,660 --> 00:36:12,880 Anything on the tape needs to be in read-writable memory. 605 00:36:15,260 --> 00:36:16,220 And yes. 606 00:36:16,560 --> 00:36:18,200 And not just the tape though. 607 00:36:18,260 --> 00:36:19,580 The relocation symbols as well. 608 00:36:19,700 --> 00:36:21,140 Because we edit them on the fly. 609 00:36:21,960 --> 00:36:23,540 But luckily this is the case. 610 00:36:23,720 --> 00:36:24,980 Because the memory protections. 611 00:36:25,220 --> 00:36:26,420 The bulk of the memory protections. 612 00:36:26,540 --> 00:36:27,360 Get set up later. 613 00:36:28,320 --> 00:36:29,340 So we need. 614 00:36:31,660 --> 00:36:33,240 We need two instructions. 615 00:36:33,780 --> 00:36:34,380 I'm sorry. 616 00:36:37,480 --> 00:36:38,600 Two instructions. 617 00:36:38,800 --> 00:36:40,000 Movement of the tape pointer. 618 00:36:42,260 --> 00:36:44,460 And I'll show this little demo. 619 00:36:44,640 --> 00:36:45,520 That's not a demo. 620 00:36:45,680 --> 00:36:46,580 It's a animation. 621 00:36:48,180 --> 00:36:49,940 So first we get. 622 00:36:50,200 --> 00:36:51,020 We calculate. 623 00:36:51,220 --> 00:36:54,320 We have the offset of tape pointer value. 624 00:36:55,020 --> 00:36:56,540 Basically the next symbol down. 625 00:36:57,080 --> 00:36:58,720 And then we have. 626 00:37:00,060 --> 00:37:02,300 So we can start writing to it. 627 00:37:02,400 --> 00:37:03,100 And copy it over. 628 00:37:05,460 --> 00:37:06,500 We have. 629 00:37:07,800 --> 00:37:09,840 so the symbol of the T pointer 630 00:37:09,840 --> 00:37:10,080 sorry 631 00:37:10,080 --> 00:37:13,720 what we do is the T pointer is a pointer 632 00:37:13,720 --> 00:37:15,620 to the symbol we read that out 633 00:37:16,200 --> 00:37:17,740 and we end up 634 00:37:17,740 --> 00:37:18,120 using 635 00:37:19,580 --> 00:37:20,620 good morning me 636 00:37:25,680 --> 00:37:26,660 so yes 637 00:37:26,660 --> 00:37:29,680 what happens here is 638 00:37:31,580 --> 00:37:33,660 since we control the 639 00:37:35,320 --> 00:37:35,840 relocation 640 00:37:35,840 --> 00:37:37,800 sections and we're 641 00:37:37,800 --> 00:37:39,160 using them as tape 642 00:37:39,160 --> 00:37:41,540 we have to embed the brain fog interpreter 643 00:37:41,540 --> 00:37:43,480 with all of 644 00:37:43,480 --> 00:37:45,540 six it's brain fog 645 00:37:45,540 --> 00:37:47,240 constructions into 646 00:37:47,240 --> 00:37:48,500 the 647 00:37:50,920 --> 00:37:53,600 pieces of code that 648 00:37:53,600 --> 00:37:55,320 work on the tape luckily 649 00:37:55,320 --> 00:37:56,660 we can 650 00:37:57,540 --> 00:37:58,980 craft those 651 00:37:58,980 --> 00:38:00,240 entries 652 00:38:01,740 --> 00:38:03,740 luckily we can craft those entries 653 00:38:03,740 --> 00:38:04,980 to point 654 00:38:04,980 --> 00:38:05,400 out 655 00:38:05,400 --> 00:38:06,020 at 656 00:38:06,660 --> 00:38:07,740 other entries 657 00:38:08,200 --> 00:38:10,080 so that we can start building loops 658 00:38:11,120 --> 00:38:12,400 and branches 659 00:38:12,400 --> 00:38:17,000 so instead of actually giving you a step-by-step since we're low on time 660 00:38:17,580 --> 00:38:19,940 i'm going to just point out some interesting things so 661 00:38:21,200 --> 00:38:24,020 in addition to subtraction we end up having to 662 00:38:24,880 --> 00:38:27,640 rewrite one of the relocation entries to get this done 663 00:38:28,680 --> 00:38:32,760 to do unconditional branches we've realized that 664 00:38:33,860 --> 00:38:35,140 when the 665 00:38:35,140 --> 00:38:35,620 uh... 666 00:38:35,620 --> 00:38:37,880 relocation entries are processed it actually 667 00:38:38,340 --> 00:38:42,020 steps through this link map structure and goes to the previous one and move to the previous one 668 00:38:42,540 --> 00:38:42,820 so 669 00:38:43,380 --> 00:38:48,480 what we need to do is set the previous to our current so we can start processing relocation entries in 670 00:38:48,480 --> 00:38:49,280 ourselves again 671 00:38:49,920 --> 00:38:51,460 so we have go to 672 00:38:52,100 --> 00:38:52,380 yes 673 00:38:52,380 --> 00:38:54,380 this is the primitive 674 00:38:54,380 --> 00:38:58,720 this is the implementation of the bytecode 675 00:38:59,080 --> 00:38:59,900 that we are 676 00:39:00,420 --> 00:39:00,720 uh... 677 00:39:01,080 --> 00:39:01,360 creating 678 00:39:02,080 --> 00:39:05,120 and uh... the fact that uh... a particular pointer is not going to be able to do that 679 00:39:05,140 --> 00:39:09,600 is pointing and uh... then is being followed to the next primitive 680 00:39:10,060 --> 00:39:11,400 enables us to do looping 681 00:39:11,400 --> 00:39:15,720 and so we have to do some extra work to make sure it loops again because 682 00:39:15,720 --> 00:39:18,180 after the first time memory permissions are set 683 00:39:18,640 --> 00:39:20,280 uh... it doesn't try to relocate twice 684 00:39:21,140 --> 00:39:23,020 and there's yeah that sets some 685 00:39:23,020 --> 00:39:25,460 permissions so we can't rewrite ourselves again 686 00:39:26,020 --> 00:39:26,540 uh... 687 00:39:26,540 --> 00:39:29,360 but we use this little chunk right here to end up 688 00:39:29,360 --> 00:39:34,200 clearing out this relocated bit so it will process itself again later 689 00:39:34,200 --> 00:39:35,700 which is very nice 690 00:39:35,700 --> 00:39:37,640 and so we have loops 691 00:39:39,940 --> 00:39:43,080 and we also need to rewrite uh... 692 00:39:44,490 --> 00:39:48,520 some of the relocation the dynamic table entries to point to where it should go 693 00:39:48,520 --> 00:39:51,960 next and the new size of the relocation entries that we're processing 694 00:39:53,090 --> 00:39:55,960 but again the functionality is helpfully already there 695 00:39:56,250 --> 00:39:57,180 yes this is what 696 00:39:57,180 --> 00:39:59,200 the relocator actually does 697 00:40:00,210 --> 00:40:04,160 all of itself so to do conditional branches we use the 698 00:40:04,200 --> 00:40:07,900 um... indirect function symbol and the interesting part is 699 00:40:08,320 --> 00:40:09,200 this shindex 700 00:40:09,200 --> 00:40:10,600 if it's 701 00:40:11,240 --> 00:40:14,500 one it treats it as an indirect function if it's zero 702 00:40:14,500 --> 00:40:18,200 it treats it as a whatever values in that symbol is just a value 703 00:40:18,200 --> 00:40:21,680 so we end up doing it is telling that to overwrite 704 00:40:23,440 --> 00:40:25,200 this end um... 705 00:40:25,640 --> 00:40:30,100 which basically end is where the relocation entries stopping process so we can either 706 00:40:30,100 --> 00:40:32,640 set it to zero so the next time the loop executes 707 00:40:32,920 --> 00:40:33,400 it stops 708 00:40:33,400 --> 00:40:37,940 or we can set it, it ends up being something way bigger than any of the relocation entries, 709 00:40:38,060 --> 00:40:41,160 and it will keep on going, and we can fix everything up in the end. 710 00:40:41,680 --> 00:40:43,100 And so we have branches. 711 00:40:43,160 --> 00:40:48,700 We have an instruction that behaves differently whether it reads a zero or a non-zero. 712 00:40:48,900 --> 00:40:52,700 So to get actual details of how we do it, I look at the code. 713 00:40:52,860 --> 00:40:53,400 It's on GitHub. 714 00:40:54,240 --> 00:40:58,020 I'm going to skip the demo because I want to move to something a little more practical. 715 00:40:59,120 --> 00:41:01,920 We injected a backdoor into ping. 716 00:41:03,440 --> 00:41:07,360 And so by doing this, we don't use the brain fuck. 717 00:41:07,440 --> 00:41:10,580 We actually create our own primitives to start following pointers. 718 00:41:11,880 --> 00:41:15,660 At a known place is a pointer to the link mech structure of the executable, 719 00:41:15,820 --> 00:41:19,620 and from there, we can start finding where the libraries are. 720 00:41:19,860 --> 00:41:26,400 And from there, we can figure out what addresses to write into the global offset table 721 00:41:26,400 --> 00:41:31,780 so when the functions are invoked, it invokes what we want instead of what the linker tells it to 722 00:41:31,780 --> 00:41:33,020 since there's a value there. 723 00:41:34,140 --> 00:41:42,500 And so what happens is we rewrite the symbol over and over and update the value through these relocation entries 724 00:41:42,500 --> 00:41:44,820 and how they work with the symbol. 725 00:41:45,160 --> 00:41:51,820 And what we end up getting, we can get the base address once we get where the link map structure is itself. 726 00:41:52,060 --> 00:41:56,840 And from there, we can find any function in the library that we're linking. 727 00:41:57,740 --> 00:41:59,540 And so I do want to demo this. 728 00:41:59,740 --> 00:42:03,060 And this is the version of ping that I have. 729 00:42:03,060 --> 00:42:08,400 It has this interesting argument, this type. 730 00:42:08,980 --> 00:42:15,240 And I actually have, the way we work this is we tell staircase comp to exec instead. 731 00:42:15,900 --> 00:42:17,440 And that's how we can get a shell. 732 00:42:17,620 --> 00:42:20,560 And we also need to tell it not to set UID back. 733 00:42:20,980 --> 00:42:23,360 Ping is set UID root. 734 00:42:23,540 --> 00:42:25,300 And then eventually drops privileges. 735 00:42:25,340 --> 00:42:27,440 We can trick it into not dropping privileges. 736 00:42:28,320 --> 00:42:32,780 So your output is all of your memory space in the process. 737 00:42:33,060 --> 00:42:35,660 And you can rewrite it any way you like. 738 00:42:35,820 --> 00:42:42,120 So why not rewrite it to give yourself root when a particular option is invoked? 739 00:42:42,660 --> 00:42:47,200 So instead of giving a live demo, because I'm a bumbling idiot when it comes to typing, 740 00:42:47,980 --> 00:42:50,660 here is a video pre-recorded demo. 741 00:42:54,320 --> 00:42:56,220 And you can't read it, but that's fine. 742 00:42:56,680 --> 00:43:01,240 So I'm just showing that there's this type that we're playing with. 743 00:43:02,260 --> 00:43:03,040 And we're going to do that. 744 00:43:03,060 --> 00:43:08,280 We end up, oh, so first we compiled our new relocations entries into there. 745 00:43:08,720 --> 00:43:10,340 And we set UID ping. 746 00:43:11,140 --> 00:43:13,160 So, because it needs to be set UID. 747 00:43:14,860 --> 00:43:20,160 And when you run it normally, this is running at double pace. 748 00:43:20,260 --> 00:43:20,740 I don't know why. 749 00:43:20,940 --> 00:43:22,100 I must have changed something. 750 00:43:23,100 --> 00:43:24,400 So let me pause for a moment. 751 00:43:25,000 --> 00:43:25,800 So let me pause. 752 00:43:26,920 --> 00:43:29,760 So ping works normally with the new relocation entries. 753 00:43:30,220 --> 00:43:31,600 Or at least you can ping normally. 754 00:43:31,720 --> 00:43:32,920 There's probably some other things that might. 755 00:43:33,060 --> 00:43:33,140 Break. 756 00:43:33,400 --> 00:43:34,280 But you can ping normally. 757 00:43:34,660 --> 00:43:40,700 But when you put in dash T and then we just give it backdoor dot T dot shell, 758 00:43:40,860 --> 00:43:43,980 it just invokes bash without any arguments. 759 00:43:44,400 --> 00:43:51,120 It's just otherwise you get crap in there because we are not using it properly. 760 00:43:52,340 --> 00:43:54,760 But what happens is once you evoke it with this, 761 00:43:54,880 --> 00:43:57,240 suddenly it calls exec on backdoor dot shell. 762 00:43:57,500 --> 00:44:01,340 And we prevented the root privileges from being dropped. 763 00:44:01,500 --> 00:44:02,940 And now you have a root shell. 764 00:44:06,540 --> 00:44:12,840 And we go on to just show that there is no difference in the code itself. 765 00:44:13,180 --> 00:44:17,500 And since it's in double time for some reason, maybe we'll have time to see this. 766 00:44:17,940 --> 00:44:20,100 There are differences because there's different sizes. 767 00:44:20,220 --> 00:44:21,200 We injected metadata. 768 00:44:21,740 --> 00:44:23,480 So SHAs are different. 769 00:44:23,940 --> 00:44:25,720 But the code itself is not. 770 00:44:26,320 --> 00:44:27,800 We only eject metadata. 771 00:44:30,320 --> 00:44:32,900 And I am going to probably. 772 00:44:32,900 --> 00:44:34,600 Not let this finish running. 773 00:44:35,060 --> 00:44:36,740 I'm going to skip ahead a little bit. 774 00:44:36,840 --> 00:44:37,880 So I can show you. 775 00:44:38,240 --> 00:44:42,940 Yeah, you'll trust us that if you dump out anything that could be disassembled 776 00:44:42,940 --> 00:44:46,700 and compare the disassemblies, they are exactly the same. 777 00:44:46,820 --> 00:44:54,000 The only difference in all of the program compiled to rewrite your memory at runtime is in the ELF. 778 00:44:54,060 --> 00:44:55,080 So there's difference in. 779 00:44:55,220 --> 00:44:57,440 So the left side is our injected version. 780 00:44:57,560 --> 00:45:00,220 The version of ping with the injected entries. 781 00:45:00,340 --> 00:45:01,620 And the right side is the original. 782 00:45:01,820 --> 00:45:02,220 And so. 783 00:45:02,320 --> 00:45:02,800 The. 784 00:45:02,900 --> 00:45:04,020 The original entries are still there. 785 00:45:04,180 --> 00:45:05,720 Ping still executes. 786 00:45:05,760 --> 00:45:08,240 But before all of them are our injected ones. 787 00:45:08,660 --> 00:45:10,200 And the same goes with the symbol table. 788 00:45:10,440 --> 00:45:13,640 But we inject our symbols after the original symbols. 789 00:45:13,940 --> 00:45:17,280 Because everything is referenced from the beginning of the symbol table. 790 00:45:17,580 --> 00:45:19,940 And we don't want to mess with the linking that it needs. 791 00:45:24,640 --> 00:45:26,200 Let's get out of full screen. 792 00:45:28,220 --> 00:45:29,220 F11, of course. 793 00:45:36,920 --> 00:45:38,860 So that's sort of everything in a nutshell. 794 00:45:39,020 --> 00:45:41,180 Again, I suggest going to our website. 795 00:45:41,320 --> 00:45:45,440 But I need to thank Sergey for leading this. 796 00:45:45,660 --> 00:45:49,740 And Sean, who's my actual thesis advisor at the moment. 797 00:45:50,200 --> 00:45:52,700 For putting up with me not doing the work he wants me to do. 798 00:45:52,940 --> 00:45:55,720 And Greg Q for doing a lot of great work. 799 00:45:55,820 --> 00:45:59,320 And the RSA toolkit for allowing me to inject relocation entries. 800 00:46:00,100 --> 00:46:00,880 Mayhem, Skippy. 801 00:46:01,100 --> 00:46:02,500 And then the noun project. 802 00:46:02,500 --> 00:46:04,400 Where I got all these cool little things. 803 00:46:05,780 --> 00:46:06,640 Thank you. 804 00:46:08,060 --> 00:46:09,300 Thank you. 805 00:46:09,300 --> 00:46:11,160 Thank you.