Survivor Survivor - 1 year ago 144
C Question

C shellcode execution issue

Sorry if I make mistakes, it's my first post here. And English is not my native linguish, but I'll do my best.

So I recently get interested in Metasploit, and I was trying to execute some shellcode from C code.

So i've generated with msfvenom a shellcode for LHOST = and LPORT = 714 (so if you want to run the shellcode, no problem because localhost) and selected C format for output.

Then I found this :
and this :

So what I did :

#include <stdio.h>
char code[] =

int main(int argc, char **argv)

int(*func) ();
func = (int(*) ()) code;
(int)(*func) ();


Compiled it, launched it.. And crash....
"Exception non gérée à 0x00338000 dans Shellcode.exe : 0xC0000005 : Violation d'accès lors de l'exécution à l'emplacement 0x00338000."

The crash is happening here : "(int)(*func) ();"

As I don't really understand what's the program is trying to do (I'm quite new to C), I don't know from where is the problem.. Is it my shellcode or is it the way it is called ?

And does someone has some documentation about executing shellcode in C/C++ ?

Thanks all for your help,


md5 md5
Answer Source

The bytes represent machine instructions. On ordinary machines, the memory is broken in different memory segments, and code will typically goes in a data segment. The call:

int(*func) ();
func = (int(*) ()) code;
(int)(*func) ();

tries to execute the code contained in these bytes.

However, modern operating systems provide usually by default a protection against executing code located in memory segments not designed for code execution (see here for example). You have to configure your compiler to disable it.

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