2013-02-28 115 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