The purpose of this post is to show you how you can encode a common shellcode using a custom encoder and embed it inside a Windows file. The main reason to do that is to try to avoid AV detection.
I’m going to generate a backdoor for FileZilla FTP with msfpayload. I’m going to skip how to manipulate the Windows binary, also how to allocate space and find the appropriate Code Cave because I’ve already explained it before in the last blog post.
Following that post you have to be able to modify one section of FileZilla to generate the Code Cave, JMP to it and save the registers and flags status. So you should be at this point.
This time is going to be encoded with a personal encoder that I wrote a couple of months ago. You can find it here:
I’m going to modify it a little bit and to compile it in Windows so I can learn new things.
So, we can start generating our msfpayload with this beautiful one line command:
That is our plain text shellcode. I’m going to encode it with my custom encoder. If you want to try it you can download it from here:
For this example I’ve modified the XOR keys a little bit. Here is the final code:
So right now we have our 287 bytes length payload encoded:
For the following step we are going to need an Assembly compiler, I used Flat Assembler. You can download it here:
We use our Assembly decoder to compile the code and verify that everything works fine:
If you are curious about how my decoder works, you can find more information about it in this post:
The following step is to extract the opcodes of the binary that we generated with objdump. Notice that I’ve used sed command to remove the “\x” symbols so we can copy this inside Olly.
Now that everything it’s ready and we verified that it works, we can copy the shellcode inside the binary.
In the image below you can see that I have placed our shellcode right after the snapshot of the registers and the flags.
Now we need to restore the execution flow after the shellcode. The first thing that we need to recover is the ESP value. We can’t hard-code the ESP value because the ASLR protection is present. So we have to find a relation between the actual value of ESP and the value in the moment that we saved the registers.
This is the process that you need to follow to do that:
We place a breakpoint and after the PUSHFD instruction and save the value of ESP that is:
Then we place another breakpoint after of the last instruction of our shellcode and we save again the value of ESP:
So we have to rest the second value to the first one:
22FF84 - 22FEE0 = A4
So the instruction that we need to do is:
ADD ESP, 0A4
We step into the next instruction and we verify that the ESP value it’s 22FF84 again. As you can see here we restored the ESP value correctly:
Now it’s moment to recover the flags and the registers.
Just after this, we need to put the instruction that we erased:
And after that a JMP to the instruction that is just below the instruction that we deleted:
We save the changes and we are ready to execute the file.
Our Multiple keys XOR decoder it’s working! 🙂
We check the AV detection rate in VirusTotal and we can see that we managed to bypass some AV’s. Not a bad result for this small decoder.