这个缓冲函数在做什么?

我有一些代码具有以下function:

//some code before // buf is a char[] containing shellcode ((void(*)( ))buf)( ); //Not sure how this works 

任何人都能描述上述function实际上做了什么以及如何做?
在语法上它也相当混乱!

如果您希望查看完整的源代码,完整代码将执行shellcode,并且是众所周知且广泛使用的安全模块 *的一部分。 如果它有任何区别, gcc -z execstack在编译时使用。

谢谢。

*(第3页的来源)

它将buf转换为函数并运行它,就像它是一个返回void并且不带参数的函数一样。 基本上运行shellcode。

从文章的源代码:

 #include  #include  const char code[] = "\x31\xc0" /* Line 1: xorl %eax,%eax */ "\x50" /* Line 2: pushl %eax */ "\x68""//sh" /* Line 3: pushl $0x68732f2f */ "\x68""/bin" /* Line 4: pushl $0x6e69622f */ "\x89\xe3" /* Line 5: movl %esp,%ebx */ "\x50" /* Line 6: pushl %eax */ "\x53" /* Line 7: pushl %ebx */ "\x89\xe1" /* Line 8: movl %esp,%ecx */ "\x99" /* Line 9: cdql */ "\xb0\x0b" /* Line 10: movb $0x0b,%al */ "\xcd\x80" /* Line 11: int $0x80 */ ; int main(int argc, char **argv) { char buf[sizeof(code)]; strcpy(buf, code); ((void(*)( ))buf)( ); } 

它将code的内容复制到buf ,按顺序进行布局。 前几行设置了函数序言(设置堆栈等)。 它看起来像机器,就像在buf布局的代码是相同的,如果它实际上是一个函数。 在转换时,编译器允许您实际从buf开始调用该函数。 不是很神奇不是吗? 但它在概念上很简单。

该语句将buf转换为指向函数的指针(类型为void(*)() ),然后调用该函数。

 buf // `buf` decays to a pointer to the first element of `buf` (void(*)())buf // this pointer has its type changed to `void(*)()` // (a pointer to a function taking no arguments and returning void) ((void(*)())buf)(); // this function is called 

buf转换为函数指针,然后调用该函数。 void是返回类型。 最后一组parens是arg将去的地方,如果有的话。