Search code examples
cfunction-pointersshellcode

What does this C snippet mean?


I'm a noob at shellcodes and I'm trying to understand all scenarios. I have found many codes to test my shellcodes in C, but to my surprise they are very unreadeable and I can't understand neither this code:

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

Or this one:

int (*ret)() = (int(*)())code;
ret();

Is this an equivalent to eval() in Javascript? And how doest it work?


Solution

  • int (*func)(); is a pointer to a function (taking no parameters in C++) that returns an int.

    (*func)(); or just func(); calls the function pointed to.

    (int (*)()) code; is a nasty type cast telling the compiler that code is pointing to such a function. If it is not, but you still try to call the function, the program is invalid and anything can happen.

    "Anything" includes actually executing the shell code, or crashing, or something else. The language standard doesn't say.