Lyka Lyka -4 years ago 124
C++ Question

How is the shell code of a Buffer Overflow generated

The following codes got my curiosity. I always look, search, and study about the exploit so called "Buffer overflow". I want to know how the code was generated. How and why the code is running?

char shellcode[] = "\x31\xd2\xb2\x30\x64\x8b\x12\x8b\x52\x0c\x8b\x52\x1c\x8b\x42"

int main(int argc, char **argv){
int (*f)();
f = (int (*)())shellcode;(int)(*f)();

Thanks a lot fella's. ^_^

Answer Source

A simple way to generate such a code would be to write the desired functionality in C. Then compile it (not link) using say gcc as your compiler as

gcc -c shellcode.c

This will generate an object file shellcode.o . Now you can see the assembled code using objdump

odjdump -D shellcode.o

Now you can see the bytes corresponding to the instructions in your function. Please remember though this will work only if your shellcode doesn't call any other function or doesn't reference any globals or strings. That is because the linker has yet not been invoked. If you want all the functionality, I will suggest you generate a shared binary (.so on *NIX and dll on Windows) while exporting the required function. Then you can find the start point of the function and copy bytes from there. You will also have to copy the bytes of all other functions and globals. You will also have to make sure that the shared library is compiled as a position independent library.

Also as mentioned above this code generated is specific to the target and won't work as is on other platforms.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download