Alright now they're gonna talk about the uh a little bit of new uh new shell coding and a little humor along the way. Go ahead fellas. Hi, hello DEF CON. How are you today? Brilliant. Very glad to be here today. We're gonna talk about the ABC of next generation shell coding. A lot of interesting things. So let me just say a few words of disclaimer before I begin. Don't look at what's written there. We are going to have a deep dive into the dark arts of shell coding. We'll use brute force mathematics, wizardry, bit of dancing. We'll make your head spin at some point. And the idea will be to build up obscure incantations to make computers do things they shouldn't do. We'll conjure monsters, give you nightmares and hopefully you'll stay until the end. So that you know why we do all these things. So just a few words about who we are. Uh the few of us. You've got Adrien, Georges Axel and myself. Uh we we work at university we work we work at researchers in security. And the point of the talk today more precisely is creative methods and I insist on creative. To write shell codes, exploitation code, and to use them to create a new shell code. And we will illustrate that on an architecture that is not even easy to find actually right now. Because it's uh not yet deployed very much. So just a reminder. Who who amongst you has ever written a shell code? Just raise your hand for me. Well that's half. Okay. So you know how it works. So you think at least. Um for the other half this is what the shell code is. It is essentially code that you wrote or found in uh targets memory and that you want to jump to. And usually it's it it pops a shell. So that's why you call it a shell code. And then once once you have a shell you do whatever you like. To to actually jump to the shell code uh you have to trigger vulnerability, a buffer overflow, a use after free, a type confusion whatever. Um but the typical scenario is that your target uh runs a program uh and then you have to get rid of the other program, the program accepts user input and so you write user input, you carefully craft user input so as to inject your code in memory and jump to it. So you have a nice picture on the screen there, the target's memory uh gets the payload inserted and then you jump to it using the vulnerability. So that's very nice. The issue with that is that it's not that easy. For those of you who actually wrote shellcodes you know it's not trivial. Um there are constraints because it has to pass as user input so you can't have uh terminating zeros in in your strings, there might be stack protections, you may have limited memory, you may not know where the shell code is in memory, that's that's annoying really. It turns out you can work around these constraints. You can you can always succeed nevertheless uh using clever techniques we are not going to talk about. The techniques to bypass existing mit- mitigations because well they're well known and that's not the point of the talk today. What I'm going to talk about is the fact that the shellcode such as this thing does not look like user input. I mean perhaps you guys input such things but the normal human beings which are not here today would not. And there are several things that give it away. Uh things such as um the presence of not-passing-inputs. Uh there are several things which are not there. So, the presence of nop characters, nop instructions in the code the non-printable characters in it. The presence of suspicious sub strings such as bnsh. And the fact that you have bits that I mean look like well known malware for instance. Side to side. So this is suspicious and this is detectable. Which means antivirus or blue teams or I mean your annoying neighbours will find out that what you're writing is actually a shellcode and perhaps make it uh, a problem for you. So we try to stay under the radar. And one idea to do that, one illustration of that, to pass as human inputs, especially for strings, is that you want your shellcode to be written using, for instance, just ASCII characters, ASCII printable characters. Or perhaps just alphanumeric characters for username, for instance. Perhaps English words. If you just want to write poetry, you feel inspired, it turns out to be a shellcode. Or why not even Shakespeare's quote? So, why would you do that? Well, for one, if it really looks like it's just English text, it does not trigger alarm that much. And you have plausible deniability. You could just say, this is poetry. This is the lyrics of my next song. Right? It's, it is, it is not an NSA implant. Well, of course it is. Anyway. But keyboard line is, it's just just, obviously, a very, very, very, very, very crud. Uh, it's also less likely to be escaped, or broken. Because it is already text, it doesn't have any special character, so. Your exploit might work better. And if everything else fails, you can always try the pick up line at the bar. Okay? So do try that. The only question remaining is, is that feasible? Can I, write my code, my programs, my eternal blue, using only English words? Yes. S. Take the x86 instruction set for instance and just look at what the letters look like when you disassemble. You've got for capital letters A to O you've got uh increment and decrement operations. For the others you've got push and pop, so stack operations. You've got jumps, you've got XOR, so you can actually do a lot of things. Right? It turns out uh Ricks and others have shown the x86 ISA is extremely nice and smooth when you try to write alphanumeric code. You've got everything you need and you can even make it work on 64 bits architecture almost trivially. So here is a full shellcode that works on x86 64 written entirely using letters and and numbers. Very fine, you can print that on a t-shirt right? Just, that's what you should do. Now, let's take a look at the x86 code and see how it works. You can actually go further than that. Well, you can even go further and uh for example here we will see how to do for some uh some English shell coding. So it has been published uh almost exactly 10 years ago by Mason and uh and others. Uh the idea is uh to do exactly the same as previously but now this time you will generate an English compatible subset of x86. So, this is exactly alphanumeric as before but you have even more than x86 code to do. So, you can have spaces, you can have punctuation, uh you can have uh columns, semicolons, you can have dashes, uh you can have some special characters. Uh and for example, it gives you even more instructions than uh than that. For example, if you look at point, you can have more uh operands available as before. And if you look for example at space you have one more opcode uh which gives you the end instruction. So, we have more than that. So, the fundamental idea behind that is that you do a normal shellcode so you write a small decoder with those instructions then you cut it into small snippets of code that would fit into English words then you have some gaps you uh and those gaps you can jump from each snippet to another using the jump instructions you can see there and just the idea is to fill those gaps with something that makes your shellcode look like English uh English text. Of course this is done using Markov chains uh so Markov chains fundamentally are just uh the auto completion uh feature on your iPhones so you write a word and then it gives you some other words so it can give you some pretty nice text uh if you write uh some uh some uh some SMS's with uh that and uh and you just have a text that looks like English code uh that looks like you just have a shellcode that looks like English text you send it to the to the to the vulnerability application and you enjoy and you have your root shell. So let's have a little demo for that. So this is what I did on my computer. So here we would go for a standard uh set user ID exploitation so the ID is you have uh the ID behind it is that you have a program that would be executed as a root program but it can be executed as a root program but it can be executed by the standard user for example if you want to change your password then it has to be a root action that has to be done by uh the standard user. So you have a program executing on root so here we give it the set user ID permission and when we switch to the user so we can have our program here that says that you have the small s in the permissions that says you it gets executed as root and the ID is to send a shellcode to it so that it does something that it was supposed to do. So here we have our English text here we just write it we send it and we have the uh root uh shell that pops out so we can check that indeed we are root here. More generally uh when we speak about x86 uh it's almost fully solved so the ID is that you have uh uh for example msf phenom so you just say I want uh some shellcode on x86 with this restrictions on the instruction set so I want only alphanumeric, I want alpha plus uh some characters, I want something that looks like a , I want something that looks like a path and this kind of thing. And it automatically generates you what ever is required so that uh it everything goes well. In principle we could even write uh some fully functional shellcodes from only Shakespeare works uh but uh what we will be speaking of uh we could think uh that that is more uh a not a about uh in the next uh part of the talk would not be on x86 uh because now we have more and more devices that are running on something else than x86 uh and I'll give uh I'll give uh it to Ray. Yeah so challenge for you by the way the the Shakespeare circle. We did not do it, do it for us. Now we're going to take risks and as was just mentioned what powers most devices today is slowly drifting away from x86 including phones, including voting machines, including several interesting things that we'd like to run shell codes on. Um and to do that we need to look at the way for instance risk instructions such as ARM um work. It turns out that you cannot use the techniques we just described on ARM. The reasons are you do not have any more single character instructions. We do not have as many addressing modes. We do not have as many uh uh uh uh uh addressing modes. In particular we lack, we lack the memory to memory addressing modes. And we have constraints on operands that make it very tricky and actually so far impossible to write shell codes for RISC-V. Uh does not work uh on on on ARM actuators as as well. Um so I'm going to talk about three approaches very quickly about the two the two uh compilation and emulation technique and a bit more about unpacking technique. So three ways around this limitations that allow us to work nevertheless. The compilation approach, the first approach consists in compiling assembly code directly to the constrained instruction set. So directly to alphanumeric for instance uh operations. The the good things about it is that it may be possible to compile to one instruction set uh easily. That's the moth fuscator for instance written by Christopher Thomas uh does that. The the the main disadvantage of the moth fuscator is that it can do it in multiple ways. You write an example of such an approach is that uh it does not work when the constraints are on the operands and not on the opcodes. And also who wants to write the compiler? I mean if if Chris Domas is in the in the room do it. Please. By all means but we won't. That's just a lifetimes work. Second approach the emulation way. To do that you write an interpreter for some language, you write your payload in that interpreted language and uh you just run that. out. The thing is you have to write the interpreter um once and once that's done, well you can reuse it for different payloads. Works quite well, it's feasible, it's been done by Yunnan and and Filipets uh for ARM 7. They did a brainfuck interpreter and um that works. The issue with that is what? Well it's interpreted. Which means it's toothless, you cannot really call syscalls, you cannot really do fancy stuff that you'd like to do with the shellcode. Right? So this leaves us with a third approach which we introduced some years ago. And the idea is a several step process. So let me just take some time to explain that one. The first step is that your payload will be encoded in a constraint compliant way. So for instance if you want an alphanumeric shellcode you would first encode it in some uh alphanumeric code. Alphanumeric way. You hide it as you can see on the top right picture. Then you look at the ISA you're targeting and you identify high level constraint compliant constructs. So a set of instructions that that that fits with your constraints and allows you to do some basic operations. Building blocks, zeroing a register, increasing a register. And using these building blocks you build an unpacker. Something that decodes the alphanumeric code. And then you decode in memory, in self modifying code, the payload. Write a minimal unpacker because we don't want to spend too much time on that and we just don't care about the unpacker that much. We want to run the payload. So the unpacker decodes and executes the payload. This is it straight there. Sorry. Oh. So on a on ARM v8 this is the demo of this approach. Very quickly I'll show you how to run it. Um we run, sorry so you've got the application there. Uh this application takes a string as input. For instance a user name. We paste there the shellcode which is written with this unpacker strategy. Um so here is the shellcode. You can see it's mostly letters. We run that. We just paste it. And once that's run. We run the shellcode. We run the shellcode. We run the target. It unpacks in memory. Executes it. And here is the shadow. Or the target. Okay so now that you saw how you can we can we can bypass the limitations of usual ARM processes. Uh as everyone is turning around from AX86 we're gonna turn our attention to RISC-V for various reasons. Thank you. So RISC-V. Maybe you've never heard about it. It is a new architecture. Uh basically it is a once again a RISC architecture. Very much like MIPS if you heard about it. It aimed at being open source and also open hardware. And it is still very work in progress. By this I mean that the specification is not completely done yet. There is very few silicon available. But hopefully uh in a few few years we will see RISC-V everywhere. There are many companies interested in it. So we meant to be seen but hopefully it is the architecture of the future. We do have one issue with RISC-V when it comes to alpha numeric shellcoding. It is that it makes our job much much harder. So let's look at what is available for us in alpha numeric RISC-V. So first we can load a RISC-V from the RISC-V to the few constant with typically the load ins- load immediate and load upper immediate. Then we have small increments. If you combine both of them it means you can load quite a lot of values in registers. Then we have some branches both conditional and unconditional but only forward branches. We do not have any backward branches so that's an issue. Then we have a single uh arithmetic instruction which is a write shift. Why not? And then we have a system register write. Uh the issue with this instruction is that it is only available at higher privileged levels. Typically it would work if you uh are attacking uh Linux or your operating system but not just a simple program on it. So we will just forget about them since we want something quite generic. And finally we do have a single uh arithmetic instruction miscellaneous floating point operations. So as you've seen we have no loops because no backward jump, no store and no syscall. Eh well we start. And I can even tell you it is not even Turing complete. So let's look at what uh typical RISC-V instruction is. If you look at the seven low bits of the binary representation of the instruction you have the opcode, the code and seven bits is exactly what ASCII character is. So we will just allow ourselves uh one more single printable character. As a spoiler I can tell you that there are three useful printable characters that can go uh make us go out of uh no loop, no store and no syscall issue. We have hash, slash and tick. Typically hash will give us uh regular stores. With regular stores we can write our unpacker. So let's look at how it works uh for uh writing alphanumeric plus hash uh shellcode on RISC-V. So here's the architecture we have three stages. Uh on the left is the stage one. First we have some initialization. Then we do use a forward jump which is a jump and link. Eh with a jump and link it means that you can actually get the PC of a shellcode which is quite useful if you do not know exactly where your shellcode is in memory. Since we have a forward jump we have some wasted space. So we use the wasted space to uh put the encoded payload. Then we write our unpacker. This is the hard thing to do. But we won't unpack directly the payload. We will first unpack a stage two. So the reason we do this is because uh it is difficult to write a generic unpacker but writing an emp- unpacker for a stage two is a very good idea. So we have our stage two. Our stage two is much more straight forward. It is just a simple decoded loop which because now we have loops because we just unpacked something. So uh stage two will unpack the final encoded payload and then we've won. We have something which works. So uh let me just show you a little demo on the only silicon available right now which is the Hi5 Unleashed board. Okay. So we see basically what the shellcode looks like. Uh you can see that basically all the hashes that correspond to our store instructions. This time uh we assume we have a valuable uh network application. We will just send it uh send our shellcode on the socket. As you can see we've send it. We now have a root shell. We can check that we are indeed working. So uh wrote and if we check the CPU type it is indeed a RISC-V CPU in the middle. That's all. Well let's go a little bit dirtier uh so we have seen what uh hash can do so it gives you uh standard stores uh so now we will switch to the other character which is to another character which is slash uh which can be really useful when you are writing for example url urls or path uh in uh in a Linux uh operating systems. Uh of course switching to hash uh to slash instead of slash of hash uh does not give us standard stores anymore so we have to find a new write a memory writing primitive uh to compensate for that. Of course slash is not taken uh out of nowhere uh because this uh character gives us atomic operations uh so it gives us atomic operations uh so we have two uh mainly useful atomic operations so the first one for example aq3 slash gives us atomic or and the other is atomic end. Fundamentally an atomic or operation reads 64 bits from the memory, stores it in a register and then stores back to the memory the same uh value or with another register. So the end is exactly the same with the end operation instead of or. Of course uh so given that I can read and write 64 bits into the memory so this is a memory writing primitive. So the idea is just to uh to write my stage two with those instructions. However in RISC-V uh there is a little constraint for uh atomic operations which was not there for stores uh and it says that the address held in uh the register must be naturally aligned to the size of the operand and if the address is not naturally aligned a misaligned extra exception will be generated. So that's fine it's six it's eight bytes I have to align it at eight bytes so the idea is I have a pointer to which I write to I write my eight bytes then I increase this pointer by eight and I continue writing it. So we have to use some add immediate instruction that will allow us to increase the pointer. So we look at the available instructions we look for the add immediate then we take the shortest one and of course the shortest add is of 16. So we are fucked now so we will have to find a way to go out of it. Uh the solution is to use uh 16 byte chunks because we are obliged to move our pointer by 16 bytes out of which only the eight are controllable. So the idea is we will use six only out of them so it's even better uh and we will put an instruction at the beginning uh that will be our real instruction of the stage two then an op-like operation and then we will put a jump instruction and then a jump instruction that will jump to the next block. Uh here we decided to put two bytes and two bytes instead of four bytes of instruction because it was easier to build uh the shellcode and just because we are lazy so. So using some black magic uh so I will explain all of this all of it uh step by step so here is uh the example of uh some code that allows you to to write exactly one block to load into the murray one block and uh we will use uh some gdb uh over uh uh some uh type was uh uh beamer uh to look how what it does exactly. So other black magic here uh we load uh in the initialization section uh magic value in uh the TP uh register which is 804100004 and let's go step by step to it. So first we would zero S4 then we would do the atomic end to the uh chunk which in practice would zero all the uh the first 8 bytes of the chunk which is exactly what we want. Then we would do the R with the register that has the magic value. So it loads A031 into the memory which is exactly what we want because this is a jump 12 which will jump to the next block. Then you load a magic value into A0. You shift it by 12. You subtract 10 out of it and then you do again an atomic R uh to the memory which would load load into the the chunk 97A0 and 0005. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0. 97A0 is exactly add A4 A4 SP which is one of the instructions of the stage two and 0005 is the NOP operation which is exactly what we want. So the idea behind it is that you do exactly the same for every instruction of your stage two. So you had a load of a immediate instruction, you shifted by an amount and then you put some add uh immediate instructions, so small or bigger uh on 42 bits uh uh the instruction of the stage and uh you just brute force on all those instruction sequences so at the end it will allow you to load one value into the the the chunk so if you have several instru- instruction sequences that do the same thing you keep the shortest one and if your stage 2 does not fit into the instruction sequences you found so you just modify it when you tweak it a little bit and this will give you. So here is exactly the stage 2 so sorry I had no place uh for putting it uh vertically so just please turn your head 90 degrees uh so here if we look at it you have exactly uh the instructions uh ok so let me put it back uh in the right order so you have the the body of the loop so everybody knows what's in the body of the loops so let me take it out. Here it becomes normal I think. Ok so let's get back so you have the jump instructions you have your nop instructions so you have uh left shift at the end of the loop and then which is which shifts uh a register that we do not care about so it's a nop like instruction and you have the real stuff here which is exactly the stage 2 and we had some you have the 2 bytes instructions and there is one instruction that is 4 bytes long which is the fence instruction which allows you to clear the cache uh if you have uh self-modifying code this is absolutely essential uh and for this we just hand wrote the the instruction sequence and it's only one instruction though so that's fine. So. Let's get back to the demo. So here. So we still have our shellcode here so you can see the slash uh characters that tell you that it's an atomic operation and we will send it to the same application that has another filter now so instead of filtering out all uh the hash characters so it will only keep the slash characters. So we send it we got our shell so we do ID this is root and if we check again the CPU so it's again re-scribe. Ok so let us look at this nice quote from xkcd either you're handing out raw floating point variables sorry or you've built a database to track individual atoms in either case please stop. Well I'm very pleased to tell you that we are not going to build a database to track individual atoms. Which means we are going to have fun with floating points. The last character, Dick. Gives us gives us floating point stores. And uh it's really difficult to work with. So as we went as we went on we only want to change the in-packer so other parts of our architecture do not change but instead of using regular stores or atomic store we need to write our first in-packer with floating point stores. So uh floating point stores in other words is a way to bring our point one for people who need it. Uh floating point representation in memory uh has three fields, mantissa, exponent and the sign and the mathematical representation of this uh binary representation is very roughly. Mantissa times two to the power of the exponent plus the sign bit. It is very rough but it's much enough for this presentation. So our idea to write the unpacker is to first load some floating point value from the memory. Since it is from the memory it means that it must be alphanumeric. Then do some computation and hopefully at the end we have a chunk of our of our stage two in uh of our register which we can just store it to memory. We repeat this for each chunk and we have our unpacker. Obviously the issue here is which value do I pick? And which value do I pick? And which value do I pick? And which value do I pick? And which computation do I do? Well uh let's look at what is available on alphanumeric quiz 5 on the term of floating point operations. So first we have loads and stores. Well that's a good thing. And then we have we need to find our operation to work on those loaded values. So first we have quad to double conversions. But since we do not have double to quad it's like not super useful. Then we have sign manipulation. So we need to find our operation to work on those loaded values and also we have a lot of manipulation such as for example taking the absolute value of a floating point register. But it will only change a single bit in the register. So it's not super useful. And finally we do we do have fused multiplier adds instructions which has multiple variants. So uh fused multiplier add is an operation which has 3 inputs and 1 output. And basically it combines uh multiply and uh add in a single instruction. So uh fused multiplier add uh it's A times B plus C and the variants have some minus sign in the middle. So for example if you have the instruction fmsub uh ft6 fs2 ft4 fa0 it means that the floating point register t6 will be set to the result of h2 times t4 minus a0. So uh here's how we want to store as chunk our stage 2. Let's say we want to store the 16 bit value abcd in exact decimal. On the height you can see our uh fuse multiplier instruction and we need to set A, B and C which must all be alphanumeric such that uh those contains abcd in the end. So first we'll just take a random thing for A. Okay why not. Then same thing we will take a random thing for B. And at this time at this point we only have a single input left so we can mathematically solve for it and in this time if we want R to have abcd in very low bits it means that C is something quite difficult. BZ and non-alphanumeric character so doesn't work. So what we do is that we try again. We take another B again solve mathematically on it. This time we are lucky. C is alphanumeric as you can see it is bboqccz6 and this time we have a good result in R. So you you might want to ask huh how long do I have to try to find good B's? Well not that much. Only 10,000 times. And since we're doing it on a computer 10,000 tries is like nothing. So it's like really efficient. And I don't have a proof of it for you here but I can tell you just trust must be that it works if I change ABCD to anything. It works for all 16 bit values. And even better than that, when we w-wrote our thing, we saw that we could actually control much more than 16 bits. As you can see, uh, at the height, uh, on the left of the, just before ABCD, we have lots of zeros, which means we can actually control all those bits, otherwise they would be random. So, we can actually have all 48 bits, which means that we've free- free 64 bits value, we get 48 bits of output. Well, we have quite a good impactor here. So, we do it for every part of our stage 2, and then, again, we have an impactor, and all the rest works fine. So, again, a little demo. So, you have already seen it, but this time it is with tech. So, uh, on top, you can see the encoded payload, on the bottom, you can see the unpacker, with all techs corresponding to a floating point store. Once again, we are sending it to our vulnerable application, which, this time, accept alpha numeric plus tech. We get our root shell. As you can see, we are root. And, once again, you can guess it, this is the same CPU, it is still a RISC-V CPU. Okay, I hope you did not expect that. Um, so, we, we went through different new techniques to write code. We focused on alpha numeric, which, as you can probably imagine, these are tricks that some of them weren't known before. We tried to bring you to navigate the yoga of writing constrained shell code. To avoid filters, to fool ID's, to avoid code that IDS's, and humans as well. To target specific applications. Um, as we mentioned, the x86 environment is already quite mature. So, this is a sort of problem there, almost. But, new architectures, and in particular RISC-V, is something that's, that's gaining momentum. And we need to keep up. It would be unacceptable that it goes public before we have attacks for it. Right? So, we showed that it is possible to write alpha numeric shell codes. Uh, even on very constrained instruction sets. And, what we described to you, the MPacker, was the hard part, really. The decoder was the hard part. And now, what remains is just to put your payload, any payload, arbitrary payload. This is a world first, by the way. So, more than tricks and techniques, uh, we have introduced new approaches that are basically free to use. And, uh, we have, uh, the, uh, can be transported to other architectures. And for those of you who are really curious how to use that uh for once do come to uh our talk next week. Uh the do read the paper that we're gonna that has been published uh yesterday. All the code is open source you can actually find everything there if you're curious. And you have no excuse whatsoever so no get hashing and slashing and ticking for fun and for profit. Uh read the code send us a friendly email. Thank you very much, your friendly neighborhood hackers.