00:00:00.133,00:00:07.107 >> Hey everybody. Eh, welcome to “Please Inject-Me, a x64 Code Injection”. So let’s start. Eh, 00:00:07.107,00:00:13.113 a little about me. Eh, my name is Alon. I’ve been to cybersecurity industry for about 00:00:13.113,00:00:19.853 7 years. Ehm, started at the IDF in the Intelligent Division, and now working at Deep Instinct. 00:00:19.853,00:00:24.858 Ehm, at Deep Instinct I’m, eh, researching for new attack vectors, doing some heuristics, 00:00:27.327,00:00:32.332 ehm, and ehm, finding, eh, new ways to attack. Ehm also reversing malwares, eh, so 00:00:35.068,00:00:40.073 that’s pretty much about me. Okay, so any stock, eh, first I’m going to give you a short 00:00:43.110,00:00:47.681 introduction about code injections, so it’s going to be really short. I’m going to talk 00:00:47.681,00:00:53.420 about what is Inject-Me, eh, the new code inject- injection technique I found. Ehm, then I’m 00:00:53.420,00:00:57.257 going to give you a little bit, eh, of technical back -background just to know eh 00:00:57.257,00:01:01.895 things that you need to know before you can abuse the method, eh, to do the code injection. 00:01:01.895,00:01:06.900 And at the end there’s going to be, ehm, a little flow of how to do the code injection. Okay, so 00:01:11.772,00:01:17.611 what is code injection? Code injection is the term of inserting code from one process 00:01:17.611,00:01:23.850 to another, and then execute it in the context of the other process. So it looks like this: 00:01:23.850,00:01:30.490 first inject code to the target process, eh, message box, and then we execute a code, okay? So 00:01:30.490,00:01:35.729 through this presentation I’m going to use the terms, ehm, target process and injecting 00:01:35.729,00:01:40.734 process, okay? I’m exit. Okay, so why code injection is important? Okay, ehm, code 00:01:45.706,00:01:52.546 injection is important because it can be used for malicious and benign eh, eh, activities, okay? 00:01:52.546,00:01:58.051 Ehm, so it can be used for malicious activities like for stealth, okay, if a malware 00:01:58.051,00:02:03.156 alpha wants to hide the presence of the malware and make it harder to find it, eh, it can 00:02:03.156,00:02:08.328 inject the code into another process, and that would be hard to detect it. Eh, it can be used 00:02:08.328,00:02:13.333 for evasions, so we can inject into a security solution, okay, and then we can bypass the 00:02:16.570,00:02:21.208 security solution, but if injecting to the solution they want scan the activity of the 00:02:21.208,00:02:25.679 malware. It can also, if let’s say one program, only one program is allowed to do 00:02:25.679,00:02:29.616 something and we inject to that program, we’re going to be running in that context, and 00:02:29.616,00:02:35.188 then we can use that context to do something malicious and bypass the security solution, 00:02:35.188,00:02:40.861 and at the end we can steal information, let’s say phone grabbing in browsers, ehm, and 00:02:40.861,00:02:47.467 then we can see credit cards, um, data, and things like that. Ehm, for benign users, ehm, we 00:02:47.467,00:02:54.307 can use it to in security solutions, we can use it to, ehm, get, ehm, the activity of 00:02:54.307,00:02:59.546 malware, okay. Eh, in the malware we can see how it what it does, and if it does 00:02:59.546,00:03:04.951 something malicious we can just inform the user and that something bad happened. Eh, 00:03:04.951,00:03:10.791 also, it can be used to add some functionality to a program so we can add something new that is 00:03:10.791,00:03:17.197 not written in the code. Ehm, and at the end we could also do some monitoring, so a security 00:03:17.197,00:03:22.202 researcher can monitor the activity of a malware, and then see eh what’s going on. It’s not 00:03:24.237,00:03:29.242 only a malware, it can be for any program. Okay, so how it all started. Eh, I was researching 00:03:32.712,00:03:37.717 few methods eh of code injection to do heuristics, ehm and then I thought to myself, why wouldn’t 00:03:40.487,00:03:47.060 I do my own code injection technique? Ehm, maybe I should look for it. So, I thought it a 00:03:47.060,00:03:52.866 little bit about it, and I had uh a idea, and lucky for me it worked- the idea that I’m 00:03:52.866,00:03:57.871 presenting today. Ehm, so this new method is going to be used in x64, okay? Ehm, the idea 00:04:02.609,00:04:08.515 about eh behind it is we are going to use ReadProcessMemory in the target process. It’s 00:04:08.515,00:04:13.487 going to execute ReadProcessMemory and then read from the injecting process, the 00:04:13.487,00:04:19.092 shellcode. Ehm, this why we going to use this the term “injection-less”, because we 00:04:19.092,00:04:25.198 really eh not really injecting code into the target process, we make it read from us. Amazing. 00:04:25.198,00:04:30.203 Okay, so now a little bit of technical background. ReadProcessMemory, a WINAPI 00:04:32.439,00:04:39.212 function in Windows, ehm this function what it does it just reads data from a process, it 00:04:39.212,00:04:44.217 gets five parameters hProcess, which is the process we want to read from, lpBaseAddress, which 00:04:46.486,00:04:52.792 is the address we want to read from, okay, lpBuffer, which is the address we want to write the 00:04:52.792,00:04:58.098 data with to, and nSize is going to be the size we want to read, and lpNumberOfBytes is going to 00:04:58.098,00:05:03.036 be the number of bytes that were actually read. Okay, now x64 calling convention. In x64 00:05:08.675,00:05:14.614 calling convention, the parameters are passed on the registers, so they pass that 00:05:14.614,00:05:19.619 RCX, RDX, R8, and R9. Um, we can pass these four parameter arguments to a function, and 00:05:23.924,00:05:28.695 then when we execute the -the function, it’s going to use those arguments. Arguments after 00:05:28.695,00:05:34.234 the fourth parameter are passed on the stack so if you see this uh background here, you could 00:05:34.234,00:05:39.506 see that if we call ReadProcessMemory, ehm R6 is going to be the hProcess, RD is 00:05:39.506,00:05:44.511 going to be the LP based address, R8 is going to be lpbuffer, R9 going to be nSize 00:05:46.613,00:05:52.919 and then lpNumberofBytes is going to be on the stack. So if we look at the stack, eh the 00:05:52.919,00:05:58.925 return address is going to be at the top of the stack then the four arguments that are ignored, 00:05:58.925,00:06:04.231 the data there is not important and then at the end is going to be the fifth parameter at the 00:06:04.231,00:06:09.236 stack. Ehm, so again if we use separate contexts, the function and set those registers we can 00:06:12.439,00:06:18.645 just manipulate the function to execute with those paraments. Okay, so through this 00:06:18.645,00:06:23.083 presentation, you’re going to see this timeline. Ehm, it’s going to show us where we are. 00:06:23.083,00:06:28.888 Ehm first we’re going to talk about how to setup ReadProcessMemory then we’re 00:06:28.888,00:06:34.594 going to encounter the first problem which is access violation. We then eh encounter 00:06:34.594,00:06:39.432 another problem that we will need to create an infinite running thread eh and at the 00:06:39.432,00:06:46.339 end, we’re going to execute the whole method. Okay, so let’s start. ReadProcessMemory, okay 00:06:46.339,00:06:52.946 as I show you earlier it gets five arguments okay and as I already told you also it only 00:06:52.946,00:06:59.286 can get , we can only pass four arguments using the registers. Now, lucky for us, the fifth 00:06:59.286,00:07:05.992 parameter can be null, then it is it is ignored. Ehm, and more lucky for us if use 00:07:05.992,00:07:10.997 VirtualAllocEx, the allocated memory is going to be all zeroed out. Em, so we can create a 00:07:13.933,00:07:18.938 dummy stack that is going to contain that fifth argument ehm, so this is good for us. Okay, so 00:07:23.143,00:07:27.881 let’s start setting up ReadProcessMemory. Ehm, so the first parameter we need to setup 00:07:27.881,00:07:33.420 is hProcess okay. hProcess we can set it up by using DuplicateHandle. So w’re going 00:07:33.420,00:07:40.293 to duplicate the handle of the injecting process into the target process and then we have 00:07:40.293,00:07:47.000 the the handle inside the typo so we can use it as hProcess, ehm then we will allocate memory 00:07:47.000,00:07:52.005 for -for shellcode okay. The dummy stack that we already allocated earlier, ehm the base 00:07:54.541,00:07:59.112 address ehm of the shellcode we already know because it is in the injecting process and the 00:07:59.112,00:08:03.083 size of the shellcode we already know because it’s also ehm something we know in the 00:08:03.083,00:08:08.088 injecting process. Okay, so let’s say now that we actually read the eh eh called and 00:08:11.191,00:08:16.162 executed ReadProcessMemory eh we’re going to execute it and we’re going to have our first 00:08:16.162,00:08:20.033 problem which is access violation. So what’s going to happen is we first call 00:08:20.033,00:08:25.572 ReadProcessMemory, the memory is read to the buffer, and then we go to the return address. Now 00:08:25.572,00:08:30.810 because this stack is all zeroed out, we’re going to return to the address zero which is 00:08:30.810,00:08:35.815 invalid and we’re going to get access violation, not nice. Em, but if you put as the return 00:08:39.052,00:08:45.625 address a function like like RtlExitUserThread, we just exit the thread in a clean way ehm 00:08:45.625,00:08:50.330 when we execute ReadProcessMemory, it will be called memories read to the 00:08:50.330,00:08:55.969 buffer, we return to the address on the stack and that thread will execute in a clean way, 00:08:55.969,00:09:00.907 amazing. Okay, now we want RtlExitUserThread to be on the return address in the stack. 00:09:06.146,00:09:12.152 Okay, so to do this we have two steps. First we need to find a place were RtlExitUserThread 00:09:12.152,00:09:14.154 address exists okay. So let’s first talk about kernel32 and ntdll okay those are both dll’s 00:09:14.154,00:09:16.156 in Windows that are loaded in each process at the same location so the base address of 00:09:16.156,00:09:21.161 of of them is similar in each and every process. Ehm, and kernel32 also imports eh the 00:09:37.243,00:09:43.883 function RtlExitUserThread from ntdll. So what that that this means is if we look in at the 00:09:43.883,00:09:49.956 import address table and we find in our version, in the injecting process version, em the other 00:09:49.956,00:09:56.596 RtlExitUserThread in IAT is going to be at the same place in the target process so we can 00:09:56.596,00:10:01.601 copy that there using a method I’m going to discuss eh right now. Okay, so to copy the 00:10:04.304,00:10:06.306 address we’re going to use the function NtQueueApcThread. Okay, this function gets uh thread 00:10:06.306,00:10:09.309 handle which run to execute our code eh eh in that thread em then it gets the function we 00:10:09.309,00:10:11.311 want to execute and we are allowed to to pass three arguments. Okay, so we are going 00:10:11.311,00:10:13.313 to use the function RtlCopyMemory okay, give it destination, source, and length 00:10:13.313,00:10:18.318 then the target process is going to execute out RTlCopy eh Memory. Now the destination is 00:10:30.163,00:10:34.067 going to be the return address eh eh of our dummy stack, the source is going to be the 00:10:34.067,00:10:38.404 address of eh RtlExitUserThread. In our version of kernel32 IAT, and the length is going to be i8 00:10:38.404,00:10:42.942 byte which is the size of a pointer in F64. Okay, now a little side note, we can use 00:10:42.942,00:10:47.280 this method of copying okay. We can use it to recreate the shellcode so we just need to 00:10:47.280,00:10:51.251 find ehm in our version of let’s say ntdll the byte of our shellcode one-by-one and then we 00:10:51.251,00:10:54.754 can just copy it in the eh target process and we can just recreate the shellcode there. 00:10:54.754,00:10:56.756 Okay, now let’s finalize the the ehm execution. Ehm, so my first idea was to set RIP ehm to 00:10:56.756,00:10:58.758 ReadProcessMemory set all the other registers and then just execute it. But the truth when 00:10:58.758,00:11:03.696 you do so ehm you get you do it on a suspended thread and then you resume it you get exception 00:11:12.005,00:11:17.677 status invalid parameter. Ehm and we don’t want that cause that makes the the process crash 00:11:17.677,00:11:22.682 and eh even if the something happen it it it it just will crash and won’t work. Ehm, and 00:11:25.885,00:11:30.890 if we eh want to do something with the thread, we want to do something eh to copy to it 00:11:56.249,00:12:01.187 ReadProcessMemory, eh we need the thread to initialize before we can manipulate it okay? Eh 00:12:12.165,00:12:17.704 another thing if we start a thread, eh and it won’t be suspended, it will just run, 00:12:17.704,00:12:22.342 terminate before we can manipulate it and that would be bad for us cause we won’t have 00:12:22.342,00:12:29.048 time to do our stuff. Ehm, so to do it, we need to create an infinitely running thread. Eh, 00:12:29.048,00:12:34.053 we’ll create that thread and then we’ll manipulate it ehm to run eh ReadProcessMemory. Okay, 00:12:37.123,00:12:42.962 so when creating what thread is called, ehm the things the first function is going to execute is 00:12:42.962,00:12:47.967 RltUserThreadStart ehm this function first called to eh function after that you just do 00:12:50.470,00:12:56.743 other stuff and then does jump a bit which is going to be the entry point eh you deliver to 00:12:56.743,00:13:03.016 create a multi thread or create thread okay or whatever. And I guess you haven’t seen it but 00:13:03.016,00:13:08.021 RBX was never never never used here, never RBX was never changed ehm during this time so 00:13:11.591,00:13:17.230 we’re going to abuse that and we’re going to use the opcode jump RBX ehm to do that 00:13:17.230,00:13:21.601 infinitely running thread. So what we’re going to do is we’re going to allocate memory for 00:13:21.601,00:13:26.606 jump RBX then we will look for jump RBX copicode- upcode in our ntdll version in the injecting 00:13:29.809,00:13:34.814 process. We will copy it using the method described earlier to the target process then we’ll 00:13:37.884,00:13:42.689 create a remote thread that is suspended and it’s going to point to the jump RBX that we 00:13:42.689,00:13:47.694 just copied. Ehm, then using SetThreadContext you are going to set RBX to point to jump RBX 00:13:50.963,00:13:55.868 and then resume the thread. So now, what's going to happen is going to execute, get to the 00:13:55.868,00:14:00.807 jump RBX we talked about earlier and then this is going to jump to the jump RBX, RBX points to 00:14:03.009,00:14:07.814 the same location so just going to jump over and over and over and over and over again. Eh and 00:14:07.814,00:14:12.819 that’s how you make it run infinitely. Okay, so now lets eh execute the code, let’s just 00:14:15.121,00:14:21.127 execute the code injection. Ehm, so now we will suspend the thread and see the the RIP 00:14:21.127,00:14:25.264 really points to our jump RBX opcode so we know that the thread was initialized and 00:14:25.264,00:14:31.871 everything is okay. Ehm, and then we’ll start setting the thread okay so RCX is going to 00:14:31.871,00:14:36.876 point to the duplicated handle we duplicated. RDX is going to point to the address of the 00:14:39.145,00:14:45.017 shellcode in the injecting process. The buffer, the lpbuffer which is R8 going to 00:14:45.017,00:14:50.022 point to the location we allocated for the shellcode. Ehm, R9 which is ehm the size is 00:14:52.625,00:14:57.630 going to be ehm where we ehm the size of the shellcode. Then RSP is going to point to the dummy 00:15:01.000,00:15:07.507 stack we allocated eh so the fifth parameter will be null. Ehm, and RBX is going to point 00:15:07.507,00:15:13.679 to ReadProcessMemory so when we are going to resume the thread, jump RBX is going to jump to 00:15:13.679,00:15:20.086 ReadProcessMemory. All the other parameters are already setup ehm so we’re going to do that . 00:15:20.086,00:15:25.758 We’ll call WaitForSingleObject ehm just to see that the third was finished. When it’s done, 00:15:25.758,00:15:29.595 the shellcode is already copied into the ehm target process and now all we have to do is just 00:15:29.595,00:15:31.597 use a function like CreateRemoteThread thread and uh execute the shellcode. So, now 00:15:31.597,00:15:33.599 for demo. So in this demo, I’m going to open a notepad, execute the code inj- injection method 00:15:33.599,00:15:35.601 and you’re going to see at the notepad the command line in in a message box of course ehm, the 00:15:35.601,00:15:37.603 command line of the eh notepad that we opened. So here I opened the notepad with inject me 00:15:37.603,00:15:39.605 without running the program we find a duplicate handle of our process to remote one, we 00:15:39.605,00:15:41.607 allocated memory for shellcode then we allocate memory for our dummy stack. We will find out 00:15:41.607,00:15:43.609 RtlExitUserThread and copy it to the dummy stack, we will allocate memory for jump RBX, we 00:15:43.609,00:15:45.611 will copy jump RBX into the allocated memory, find ReadProcessMemory then we create 00:15:45.611,00:15:47.613 a remote thread that point to jump RBX will set the thread context, context was changed, we 00:15:47.613,00:15:50.449 will execute ReadProcessMemory and now execute the shellcode. And when you get the notepad, 00:15:50.449,00:15:56.355 you can see the message box with a command line. [Applause] So thank you very much for having 00:15:56.355,00:16:01.294 me and I hope you enjoyed the talk. Eh, special thanks for eh Maddy Stone for helping me doing 00:16:58.351,00:17:03.289 it. Ehm, the white paper is going to be published in the next few days, it’s still not up 00:17:10.363,00:17:15.368 but eh you’re more welcome eh to ask any questions ehm if I have time, I think I have. Ehm, thank 00:17:18.838,00:17:23.843 you. [Applause] Are there any questions? Okay, okay thank you very much.