I was reading the article Tips for Evading Anti-Virus During Pen Testing and was surprised by given Python program:
from ctypes import *
shellcode = '\xfc\xe8\x89\x00\x00....'
memorywithshell = create_string_buffer(shellcode, len(shellcode))
shell = cast(memorywithshell, CFUNCTYPE(c_void_p))
shell()
The shellcode is shortened. Can someone explain what is going on? I’m familiar with both Python and C, I’ve tried read on the ctypes module, but there are two main questions left:
-
What is stored in
shellcode?
I know this has something to do with C (in the article it is an shellcode from Metasploit and a different notation for ASCII was chosen), but I cannot identify whether if it’s C source (probably not) or originates from some sort of compilation (which?). -
Depending on the first question, what’s the magic happening during the cast?
Have a look at this shellcode, I toke it from here (it pops up a MessageBoxA):
Compile it an hook it under any debugger, I’ll use gdb:
Disassemble the main function to see that different between calling
real_functionandfunction:There are two
call, let’s make a break point at<main+31>to see what is loaded in eax:Look at the first 3 bytes of the data that the address in eax continues:
So the CPU will
call 0x402000, the beginning of our shell code at0x402000, lets disassemble what ever at0x402000:As you see, a shellcode is nothing more than assembly instructions, the only different is in the way you write these instructions, it uses special techniques to make it more portable, for example never use a fixed address.
The python equivalent to the above program: