Hi everybody. Welcome to Please Inject Me, x64 code injection. So let's start. A little bit about me. My name is Alon. Been to the cyber security industry for about 7 years. Started at the IDF in the intelligent division and now working at Deep Instinct. At Deep Instinct I'm researching for new attack vectors, doing some heuristics and finding new ways to attack. Also reversing malware. So that's pretty much about me. Okay. So in this talk, first I'm going to give you a short introduction about code injection. So it's going to be really short. I'm going to talk about what is Inject Me, the new code injection technique I found. I'm going to talk a little bit about code injection. Then I'm going to give you a little bit of a technical background just to know things that you need to know before you can use the method to do the code injection. And at the end there's going to be a detailed flow of how to do the code injection. Okay. So what is code injection? Code injection is the term of inserting code from one process to another and then execute it in the context of the other process. So it looks like this. First we inject code to the target process, a message box, and then we execute the code. Okay. So through this presentation I'm going to use the terms target process and injecting process. Okay. Amazing. Okay. So why code injection is important? Okay. Code injection is important because it can be used for malicious and benign activities. Okay. So it can be used for malicious activities like for stealth. Okay. If a malware offer wants to hide the presence of malware and make it harder to find it it can inject the code into another process and that would be hard to detect it. It can be used for evasion. So we can inject into a security solution. Okay. And then we can bypass that security solution but if we inject it into the solution they want to scan the activity of the malware. We can also block the security also if let's say one program, only one program is allowed to do something and we inject to that program, we're going to be running in that context and then we can use the context to do something malicious and bypass the security solution. And at the end we can steal information, let's say phone grabbing in browsers and then we can see credit cards, data and things like that. For benign users, we can use it to insecure the solutions, we can use it to get the activity of a malware, okay? And the malware we can see what it does and if it does something malicious we can just inform the user and tell them that something bad happened. Also it can be used to add some functionality to a program so we can add something new that is not written in the code. And at the end we can also do some monitoring. So a security researcher, we can monitor the activity of a malware and then see what's going on. It's not only a malware, it can be for any program. Okay, so how it all started. I was researching few methods of code injection to do and then I thought to myself, why wouldn't I do my own code injection technique? Maybe I should look for it. So I thought a little bit about it and I had an idea, lucky for me it worked, the idea that I'm presenting today. So this new method is going to be used in x64, okay? The idea about behind it is we are going to use read process memory in the target process. It's going to execute the process memory and then read from the injecting process, the share code. This one I'm going to use this, the term injection is. Because we really, not really injecting code into the target process, we make it read from us. Amazing. Okay, so now a little bit of technical background. Read process memory. A WinAPI function in Windows, this function what it does, it just reads data from a process, it gets five parameters, H process, which is going to be the process we want to read from, LP base address, which is the address we want to read from, and LP buffer, which is the address we want to write the data we read to. And n size is going to be the size we want to read. And LP number of bytes is going to be the number of bytes that were actually read. Okay, now x64 calling convention. In x64 calling convention, the parameters are passed on the registers. So they are passed at RCX, RDX, R8 and R9. So um, we can pass these four parameter arguments to a function, and then when we execute it, the function is going to use those arguments. Arguments after the fourth parameter are passed on the stack. So if you see at this uh, diagram here, you can see that if we call read process memory, um, R6 is going to be the H process, RDX is going to be the LP base address, R8 is going to be LP buffer, R9 is going to be n size, and then LP number of bytes is going to be on the stack. So if we look at the stack, uh, the return address is going to be at the top of the stack, then there are four arguments that are ignored, the data there is not important, and then at the end there's going to be the fifth parameter at the stack. Um, so again, if we use set thread context, the function and set those registers, we can just manipulate the function to execute with those parameters. Okay, so let's go back to the stack. Okay, so through this presentation you're going to see this timeline. Um, it's going to show us where we are. Um, first we're going to talk about how to set up read process memory. Then we're going to encounter the first problem which is access violation. We'll then encounter another problem that we will need to create an infinite running thread. Uh, and at the end we're going to execute the whole method. Okay, so let's start. Read process memory. Okay, as I showed you earlier it gets five arguments. Okay, and as I already told you also, it only can get, we can only pass four arguments using the registers. Now, lucky for us, the fifth parameter can be null, then it is ignored. Um, and more lucky for us, if you use virtual alloc ex, the allocated memory is going to be all zeroed out. Um, so we can create a dummy stack that is going to contain that fifth argument. Okay, so let's go back to the argument. Um, so this is good for us. Okay, so let's start setting up read process memory. Um, so the first parameter we need to set up is H process. Okay, H process we can set it up by using duplicate handle. So we're going to duplicate the handle of the injecting process into the target process and then we have the, the handle inside the type process, we can use it as H process. Um, then we will allocate memory for our shellcode. Okay, the dummy stack we already allocated earlier. Um, the base address, um, of the shellcode we already know because it's in the injecting process and the size of the shellcode we already know because it's also, um, something we know in the injecting process. Okay, so let's say now that we actually read the, uh, called and executed read process memory. Uh, we are going to execute it and we are going to have our first problem which is access violation. So I'm going to, what's going to happen is we first call read process memory, the memory is read to the buffer and then we go to the return address. Now because the stack is all zeroed out, we are going to return to the address zero which is invalid and we're going to get access violation. Not nice. Um, but if we put as a return address a function like, like RTL exit user thread, we just exit the thread in a clean way. Um, when we execute read process memory, um, we're going to get access violation. Um, it will be called, memory is read to the buffer, we return to the address on the stack and the thread will execute in a clean way. Amazing. Okay, now we want RTL exit user thread to be on the return address in the stack. Okay? So to do this we have two steps. First we need to find a place where RTL exit user thread address exists. Okay? So let's first talk about, you know, the kernel 32 and NTDLL. Okay? Those are both DLLs in Windows that are loaded in each process at the same location. So the base address of, of, of them is similar in each and every process. Um, and kernel 32 also imports, uh, the function RTL exit user thread from NTDLL. So what that, that this means is if we're looking at the import address table and we find in our version, in the injecting process, we're looking at the same process version, um, the address of RTL exit user thread in the IIT, it's going to be at the same place in the target process. So we can copy that there using a method I'm going to describe, uh, right now. Okay, so to copy the address we're going to use the function NTQU APC thread. Okay? This function gets a thread handle which want to execute our code, uh, uh, in um, then it gets the function we want to execute and we are allowed to, to, to pass three arguments. Okay? So we are going to use the function RTL copy memory. Okay? Give it destination, source, and length. Then the target process is going to execute RTL copy, uh, memory. Now the destination is going to be the return address, uh, uh, of our dummy stack. The source is going to be the address of, uh, RTL exit user thread in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, in the, and the len is going to be, uh, 8 bytes which is the size of a pointer in x64. Okay, now a little side note, we can use this method of copying. Okay? We can use it to recreate the shellcode. So we just need to find, um, in our version of, let's say NTDLL the bytes of our shellcode one by one. And then we can just copy it in, the CSSF social code, we dont, we dont the target process and we can just recreate the shellcode there. Okay now let's finalize the the um execution. Um so my first idea was to set RIP. Um to read process memory, set all the other registers and then just execute it. But the truth when you do so um you get you do it on a suspended thread and then you resume it you get the exception status invalid parameter. Um and we don't want that cause that makes the the process crash and uh even if the something happen it it it it just will crash and won't work. Um and if we uh want to do something with a thread we want to do something uh to copy it to read process memory uh we need the thread to initialize before we can manipulate it. Okay? Uh another thing if we will start a thread uh and it won't be able to read process memory we need to suspend it it will just run terminate before we can manipulate it and that will be bad for us cause we won't have time to do our stuff. Um so to do it we need to create an infinitely running thread. Uh we'll create that thread and then we'll manipulate it um to run uh read process memory. Okay so when create remote thread is called um the things the first function is going to execute is RTL user thread start. Um this function first called to uh a function after that you just do other stuff and then does jump RBX which is going to be the entry point uh you deliver to create remote thread or create thread or whatever. And I guess you haven't seen it but RBX was never never never used here never RBX was never changed um during this time. So we are going to abuse that and we are going to use the opcode jumpRBX. So we are going to use the opcode jumpRBX um to do the infinitely running thread. So what we are going to do is we are going to allocate memory for jumpRBX then we will look for jumpRBX coupled opcode in our NTDLL version in the injecting process. We will copy it using the method described earlier to the target process. Then we'll create a remote thread that is suspended and it's going to point to the jumpRBX that we just copied. Um then using this method we are going to set RBX to point to jumpRBX and then resume the thread. So not what's going to happen is going to execute get to the jumpRBX we talked about earlier and then this is going to jump to the jumpRBX. RBX points to the same location so just going to jump over and over and over and over and over again. Uh and that's how you make it run infinitely. Okay so now let's uh execute the code. Execute the code injection. Um so now we will suspend the thread and see that the RIP really points to our jumpRBX upcodes. So we know that the thread was initialized and everything is okay. Um and then we'll start setting the thread okay so RCX is going to point to the duplicated interpret understand the duplicated enter we duplicated. RDX is going to point to the address of the shellcode in the injecting process. The buffer the LP buffer which is RAID going to point to the location we allocated for the share code. R9, which is the size is going to be where we size of the share code. Then RSP is going to point to the dummy stack we allocated. So the fifth parameter will be null. And RBX is going to point to read process memory. So when we are going to resume the thread, jumper BX is going to jump to read process memory. All the other parameters are already set up. So we're going to do that. We'll call wait for single object. Just to see that the thread was finished. When it's done, the share code is already copied into the target process. And now all we have to do is just use a function like create a mod thread and execute the share code. So now for demo. So we're going to run the demo. So in this demo, I'm going to open a notepad, execute the code injection method, and you're going to see at the notepad the command line in a message box, of course. The command line of the notepad that we opened. So here I open the notepad. Please inject me. We start running the program. So we're going to run the run. We find duplicate handle of our process to remote one. We allocated memory for share code. Then we allocate memory for our dummy stack. We will find out the exit user thread and copy it to the dummy stack. We will allocate memory for jumper BX. We will copy jumper BX into the local memory, find the process memory, then we create a mod thread that point to jumper BX. We'll set the thread context. Context will change. We will execute the process memory and now execute the share code. And when you look at the notepad, you can see the message box with the command line. So thank you very much for having me. I hope you enjoyed the talk. Special thanks for Maddy Stone for helping me doing it. The white paper is going to be published in the next few days. It's still not out, but you're more welcome to ask any questions. If I have time, I think I have. Thank you. Are there any questions? Okay. Okay. Thank you very much.