2013-02-28 117 views
0

我有一些代码,具有以下功能:这是什么缓冲区功能?

//some code before 
// buf is a char[] containing shellcode 

((void(*)())buf)(); //Not sure how this works 

谁能描述一下上面的函数实际上做,以及如何?
从句法上讲,它也相当混乱!

完整代码执行shellcode并且是众所周知和广泛使用的Security module *的一部分,如果您希望查看完整的源代码。如果它有任何区别gcc -z execstack在编译期间被使用。

谢谢。

*(第3页来源)

+0

它将buf转换为函数指针并调用结果函数。 – wildplasser 2013-02-28 00:41:23

回答

3

它铸造buf到功能并运行它,如果它是一个返回void和不带参数的函数。基本上运行shellcode。

从文章中的源代码:的code

#include <stdlib.h> 
#include <stdio.h> 

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)(); 
} 

它复制内容到buf,铺设出顺序。前几行设置了函数序言(设置堆栈等)。它看起来像在机器上,buf中列出的代码是相同的,如果它实际上是一个函数。当被铸造时,编译器允许你实际上调用函数从buf开始。相当了不起,不是吗?但它在概念上很简单。

+0

这是一段可爱的代码,只是这个概念对我的小脑袋有点太刺耳了!欢呼的答案! – 2013-02-28 01:24:17

+0

@EdGeorge当我第一次在我的生活中看到shellcode时 - 我几乎把我的裤子弄脏了!那时我们没有堆栈溢出来询问这些事情。 – 2013-02-28 01:25:49

+0

阅读并理解关于如何生成代码的21页文档目前需要2天(并且计数...) – 2013-02-28 01:29:02

0

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

1

该声明将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