Welcome, welcome, welcome to DEF CON. My name is Dr. Bramall Brizendine. I am the director of the Verona Lab, which deals with vulnerability research. Creator of the Shell WOS, which allows us to utilize Windows syscalls in shellcode. I've created the JopRocket and RopRocket, both of which deal with return oriented programming and jump oriented programming. And I've also created an NSA funded research project called Sherem, which allows us to analyze, uh, shellcode. Uh, I've spoken at many different conferences in the past. Um, and I'm also an assistant professor of cyber security. I have a PhD in cyber operations, which is a highly technical degree. And all of those, uh, previous projects have, um, previously been presented at DEF CON, so check them out. My co-speaker is, uh, Shiva Shashank Kusuma. He is a research assistant and, uh, he has a master's in computer science and is pursuing a master's in cyber security. Uh, he's looking for internship opportunities for the summer as well. So, our agenda. What are we going to focus on? Well, firstly, we'll talk briefly about a general approach to shellcodeless ROP. How do we create unique patterns to facilitate process injection? Some generic ROP techniques for process injection. And then we will look at a sample process injection technique that has a, uh, highly novel solution to a rather significant problem. We'll have a demo and then we'll have another brief, uh, uh, process injection and then we'll, we will conclude. So, some, uh, preliminary words on our experiment. Now this, when we, we conducted this research, we did it on a contrived binary uh, one that is intentionally vulnerable and is filled with convenient gadgets because we want our work to be applicable not just to one particular vulnerable binary but to potentially, uh, any binary. So we want to discover everything that is possible. And this research is really focused on, uh, ROP from the standpoint of you compromise a binary. It's not something where we're trying to integrate ROP and some kind of custom malware or some kind of custom red team software. So let's introduce the idea of shellcode with ROP. What does it mean exactly? Well, it's this idea of instead of, uh, bypassing data execution prevention depth, we are instead going to invoke the, uh, functionality of a shellcode directly via ROP gadgets. And it's something that has very seldom been done in the Windows ecosystem. When you see it, it's almost always 2, 3 APIs chained together. But you'll see, uh, later today that we have, uh, vastly, uh, more APIs to achieve what we want to do. Some also, uh, some previous work on shellcode with ROP. There's a video link in the slides so I do encourage you to, uh, check it out if you're interested. So in terms of approaching shellcode with ROP, there are a couple approaches we could take. One is utilizing push add where we're going to populate values on, uh, certain registers, uh, with certain predefined values. Um, and then that causes them to be loaded onto the stack in a, a certain predefined order. And we'll also use move dereference or the sniper technique and this allows us to precisely write our values to the stack and we can then increment or decrement to the next, uh, slot that we want to provide values for. So those are both critically, uh, important for us. And I want to re-emphasize the importance of this idea of finding patterns because again, we're not really just approaching one process injection technique, but we want our work to be applicable broadly to many different techniques. How do we do this in terms of return oriented programming? So we really want to emphasize that, you know, we have these particular slots, uh, where we have, uh, registers going in a certain order. Uh, so we really, uh, weaponize this in terms of developing new patterns for different APIs, uh, both Windows APIs as well as native APIs to allow us to invoke those easily using return oriented programming. So, uh, and when using many different APIs, we want to be able to chain them together. Lar- kind of like the sense where you have a, uh, a ROP chain, you have different, uh, ROP gadgets chained together, we'll be chaining different APIs together. So you may get one, uh, return value, uh, in this case, uh, open process runtime address is provided to us in EAX. We can use that subsequently in a pattern for open process because, of course, we need to call open process. So a large part of our research is really focused on generating a lot of these different types of patterns. And actually we've de- developed more than 150 unique patterns to provide a lot of opportunities. Um, we'll talk more in depth about some of them and we'll give some examples. But historically, uh, some of the tools that have done this have been things like, uh, Mona, Mona, you know, Virtual Alloc, Virtual Protect, and you have a couple patterns. So it's not a significant number of patterns. So let's talk now briefly about some generic ROP techniques that we can use to help us with, uh, process injection. So one is the idea of using function output, using that function output as a way to, um, to be a parameter. So Gitproc address and little library often go hand in hand and they're very familiar and well known. Uh, so with a little library, we can get a handle to a module, which is the, the base address of a certain DLL, and that's required for Gitproc address. So we take the output from the one, which is going to be returned to us in EAX, and then we use that subsequently in, uh, Gitproc address. And we'll do this time and time again, so return values as well as other out parameters if you're dealing with native APIs. And then this gets a little bit more complex, so structures is parameters. Can we do that with return oriented programming? Yeah, we actually can. So with a structure, we need to look clearly at the, the definition of the structure, and then we need to build that byte by byte through return oriented programming. And we'll typically do that with a series of move dereferences, and we can just increment and decrement to the, to the next slot as we slowly build our, uh, structure. And in some cases we may actually have nested structures, so take a look at adjust token privileges. We have several nested structures, and it needs to be done exactly correctly, or else it simply will just not work. Um, one thing that is also, uh, immensely important is this idea of storing important values, um, addresses, handles, things which may be obtained by calling a Windows API. If we call a Windows API to obtain one of these values, well that takes a lot of effort. And if we needed it again, well of course we don't want to call it a second time, so we will make an effort to save that value somewhere in memory, and then we can retrieve it later. So we might have a common reference point, we move ESP to a location, maybe we advance forward 500 bytes, and we just start writing different values there. And that common, uh, reference point can, will change from gadget to gadget, but we can manually calculate that and make adjustments, uh, as need be. So, again, you'll have lots of important values and you can save them and then retrieve them subsequently, uh, later on. So another thing we did is, and this is actually, uh, completely novel, combining move dereference and push add in terms of building a pattern. So, we can use that to create a, a, a ROP chain pattern. Uh, push add, you can have a maximum of 4 parameters, 5 if you use ESP, but that can be rather, uh, challenging to do. Um, so we can do push add and then do the remainder, we can supply those via move dereference. Now, if you have the option to, uh, place your strings and data in the payload directly, then by all means do it, because it's going to save you effort. Again, if you have a common reference point, you can use these, you have a pointer to a string and you might be saying, well, what about EDR? Well, yes, EDR will be an issue at times and so this may not always be plausible. If you have no payload size restrictions, one option is to obfuscate some of this and then you could use a ROP to go and de-obfuscate it as need be. So, let's get into an actual, uh, process injection technique. Now this is based on something that I actually did at Hack in the Box Amsterdam 2023. That was all Windows sys calls and shell code. This obviously is not, it's return oriented programming. And so these are more, uh, these are Windows APIs and native APIs, it's not necessarily strikingly original. Um, but in any case, we have a new URL, we're going to create that, it's going to slither up inside of our victim target and external process and then we're going to write our shell code there and then cause it to launch. So, in terms of ROP, the actual complete chain of gadgets that we have, 29 gadgets. It's an extraordinary amount. Uh, I'm not familiar with any, uh, ROP chain in, in the Windows ecosystem that has anywhere near this level of gadgets. And so it's a lot of work to, to maintain these from, um, from API, API call to API call. And it would be a lot more actually if we did not use that technique of storing and retrieving certain values that we need. So, let's talk about load library. We can use that with, uh, push add in a pattern. So we load the particular values into, uh, registers and then we go and do push add. Then that causes it to go in a predefined, uh, location. And so let's see what happens exactly. So we, we have our stack values there and those correspond to our pattern. So we have red 8, that means we're going to do this ROP NOP. We're going to skip 8 bytes, so we skip that, we do a jump EDP dereference. We therefore invoke load library, we have a return address, we have a parameter, everything checks out. And then we get a result. Exact result that we want. So we get the base address of NTDLL. And then we can use that subsequently for git proc address, which, which is our next one. And we'll do the same thing, we'll populate different values into the registers that we can then call push add. And we get the runtime address, in this case we're doing create tool help 32 snapshot. An extremely important thing that we'll talk about momentarily. So these are the patterns for load library, we have a lot of them, 9. And git proc address, we have 11. So that gives us a lot of flexibilities because if you're trying to do something, you don't have a particular gadget, well you just give up. If you do then the chain is incomplete. But having multiple patterns, we can drill down deeper and maybe find something that actually works. In any case, our next API is create tool help 32 snapshot, which will allow us to take a snapshot of all the active processes that are out there. And it's immensely important, we need it to discover uh the different PIDs, we're gonna target one of those. So there are actually 11 patterns, so a lot of flexibility there in terms of how you can invoke it with the push add instruction. Um there's an example of the ROP and we got the push add at the bottom. Virtual alloc, um it's one way to allocate memory, yes some EDR can block it, there are other ways you could do it. Uh the pattern for that is very uh familiar, uh one of the most familiar goes back probably about 15 years. Um but with this we're gonna allocate a region of memory, we're gonna use that for a couple very uh important uh things. One of which is to build the process entry 32, the process entry 32 is where we're going to go in and check our PID, we're gonna check our uh string. So the first thing is we process 32 first, it's gonna kick everything off, it's gonna initiate the whole uh process. So how we're gonna do that, uh first we actually need to build the structure, so we can go ahead and build the structure via ROP and our move dereferences. Most of it are null bytes so it's relatively simple, we have a DW size, uh it's an arbitrary value so you can make it whatever you want. And then once we're done we get a pointer to that, we supply that pointer as the parameter. So all of that work and we just get a pointer to that. So again the idea of being able to uh dynamically locate something in memory and use that is kind of like a pointer, can be very useful. And then the patterns for process 32 first and process 32 next are actually interchangeable. So one of the biggest challenges is this idea of string comparison, how do we do that with ROP? Well the gadgets are not really very common so that can be very uh problematic. We need to identify a specific process otherwise you really can't proceed forward. So you're kind of at a, an uh, you can't really go forward without this. So we came up with a solution, we're going to build our own custom function in memory. It's an enumerate process function and we're actually writing the raw assembly. So remember we made a writable memory with virtual alloc, we can write our code there. You can do it other ways as well. And so this will cycle through process 32 next, it can do it hundreds of times in a loop until it finds the exact uh string, VU player. We can supply that and then once it does that it'll go ahead and exit out. So how do we write that? Well we simply just compile it, we can then write our bytes to memory using the move dereference technique one by one until they're all written. Make sure the code is executable and there are multiple ways in which you can do that. And then we need to supply the appropriate values so things like our HP snapshot, our VU player string, uh dereference process 32 address. And then how do we kick it off? Uh again multiple ways but in this case we're gonna do a simple push ret which will then push the address onto the stack and then return to that and then we can uh immediately start executing our custom code that we just wrote there. And so that'll invoke process 32 uh repeatedly until it finds our particular PID. In this case we have VU player. It's gonna look at offset 24 and once it finds offset 24 then again it's gonna break out of that loop. And um from there the base of the structure we can just go to simply offset 8. Offset 8 will then hold the PID. We can capture that PID. In this case it's 668. And how do we do that via return oriented programming? Well we just basically take exactly what I said, translate it to ROP. So some move dereferences, some POPs, uh sequence of 8 uh increments. And uh in this case we have a different PID B98 and uh we can supply that. Uh we can save that to memory so we can use that later. Remember the idea of save and then retrieve. And then that brings us to our next API and with that we're gonna have Shiva uh take the podium. Here we take this PID that we just obtained after all of that hard work and uh we are going to convert that into the handle to the process. We're gonna have this. It's impossible to move forward if you don't have a PID to your target process. And we don't want to just randomly inject into some other unknown process. Uh so this one we are going to use both push add as well as move dereference technique. So here we have multiple patterns for open process and then we'll follow that up after push add. And we also have a move dereference with the process ID. In this ROP chain the process ID has been provided by move dereference uh with everything else being done via push add. Next we'll open a handle to our current process with open process token. And then we'll use that so right here we have a handle and we are using ROP to create a pointer to hold the access token that we'll retrieve. With adjust token privileges uh we are going to take that token handle and we can then change the privileges as we need. We need to do this to avoid some problems that could happen otherwise. And this requires multiple complex structures such as token privileges. Such as uh where we can specify the AC privilege enabled and AC debug privilege. With adjust token privileges uh here we have a ROP. So we are setting disable all privileges to false. And what that means is that whatever we specify inside the new state structure will be then done. With adjust token privileges we can then go inspect where the debugger and see if it has all of our values. Like AC privilege enabled and AC debug privilege. And then everything is going according to the plan. Our goal with this is to take a common DLL URL mon, create a file for it and then go ahead and map that out. And then we can inject our payload into that. Then causing that to be executed. And we'll do this with these APIs which we'll be talking about. So create file A is the first. We get a handle to the URL mon which is our file. We are going to be using this later on. Uh but we are going to do this with sequence of moody references. And here we are providing the absolute address, the string pointer to that and also generic read. We are doing generic read to avoid some possible detections. And then NT create section, a lower level native API will allow us to then create a section object for URL mon DLL. And here we are doing page read only. And then with drop we are specifying the sec image. NT map your section. Now that we have the URL mon section handle, we can now map that view of the section into address space of view player. We'll do this purely through moody references because uh we have many nulls and also we have page read only. So it's a bit more limited. One more important thing is providing the base address or pointer to the base address. And that will receive a base address inside of view player. So that will be provided to us as you can see in this example which we'll be talking about later. So it is very important. Virtual protect EX will then allow us to change the target DLL URL mon to RWX. And so we are going to do this with sequence of moody references. Because remember we have limited permissions for it. And so we'll get the handle to the view player as well as the base address that we just retrieved. And then once it's done we'll have the page execute read right. Here is a route where we can provide the pointer to the base address. We then go and dereference that and we'll pull the base address out of that. And then we do the moody reference. Right process memory allows us to inject the cell code into the process memory of view player. We are doing this via push add as well as moody references. Which you can see right here so pointing to the cell code up there. And also providing the size. Same thing here we are, we have the cell code and the size and then the remaining parameters with push add. Right process memory at this point once it's done then our cell code will be injected into inside of urlmon which is an external process. So we have gone from one process to a completely different process. And the cell code is just sitting there waiting for us. So how do we cause it to execute? The answer is very simple. It's create remote thread. But the actual technique is not so simple. So we'll have a moody reference and also push add pattern. And then we'll have a sequence of moody references. We'll go inside the other space of view player and then we do the push add. So here is some of our ROP. We can provide the LP parameter as well as the address. And then we follow that up after push add. And finally with create remote thread our injected cell code will execute. We have achieved our goal. Let's do a demo. We start the things with create help 32 snapshot and we have been given a handle. Let's write the process function to memory byte by byte. We invoke the function using push thread. Now we are inside the function, special function that we created. And let's take the process entry. Let's see what we have as the first entry. We have system. Let's move forward until we find our desired PID. And we finally found the PID for view player. That's good. We'll keep moving forward. And we'll do the create file A. We'll get a handle from create file A. Let's verify what we have. We have the URL month. That's good. Let's map that out using NT map section and let's set the base address. Right now we don't have any base address. But right after the function execution we'll get the base address and base address has been provided. We'll change the permissions for the base address. And we'll see. We have the page execute read write permissions. We have the MZ. It's a PE file and let's inject the shellcode using write process memory. And let's see what we have. We have successfully injected the shellcode into external process. And finally create remote thread. We got it. Thank you Shiva. So we're going to wrap things up very briefly here. So I just want to say our goal is not to enumerate all process injection techniques. We've seen talks like that before. But we really want to provide a methodology in terms of how we can do that with ROP and focus on the practicalities. We want to provide a novel concept of building a enumerate processes function purely via ROP which can be extendable to other process injection techniques. And then create a rich set of patterns that we can utilize for that purpose. With that we'd like to thank you and invite you to check out the ROP rocket tool.