2012-08-03 69 views
0

爲了教育目的,我試圖運行一個使用緩衝區溢出來覆蓋函數指針地址的程序。我已經確定了我想用nm覆蓋的函數指針的位置。然後我想將新函數指針的地址傳遞給argv。我試圖使用在mac osx上將shell參數傳遞給c程序

perl -e'print'aa \ xc0 \ x0c \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \「'| .a.out

其中\ xc0 \ x0c \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \是小端存儲的新函數指針地址,而aa只是填充char緩存。問題是,這似乎並沒有將輸出管道輸出到a.out,因爲argc總是1.我也試過

perl -e'print'aa \ xc0 \ x0c \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ n「'> a.bin cat a.bin - | ./a.out

和ARGC仍爲1

我重視我的程序的副本,方便以下。 還有一種更簡單的方法可以將格式化的字節直接傳遞給c程序而不是使用perl,而無需更改我的程序的當前結構?

所以我可以做./a.out並讓它運行?

感謝

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

typedef struct user_s{ 
    char name[2]; 
    void (*print_name)(); 
}user; 

user a; 

void print_name1(){ 
    printf("hello\n"); 
} 

void print_name2(){ 
    printf("hi\n"); 
} 

void usage(char * msg){ 
    printf("usage: %s <name>\n", msg); 
} 

void fill_name (char * name_to_fill, char *filler){ 
    int i, len; 
    len = strlen(filler); 
    printf("length of filler is %d\n", len); 
    for (i = 0 ; i < len; i ++){ 
     *(name_to_fill + i) = *(filler+i); 
    } 
} 

int main(int argc, char * argv[]){ 
    printf("argc = %d",argc); 
    if (argc != 2){ 
     usage(argv[0]); 
     return 0; 
    } 
    a.print_name = print_name1; 
    a.print_name(); 
    fill_name(a.name, argv[1]); 
    a.print_name(); 
    return 1; 
} 
+0

做一個CTF? :D我喜歡那些。我會用python來做,因爲它的交互性更好,並且更容易玩。 – Wug 2012-08-03 21:59:17

+0

'os.execvp(「./a.out」,[「./a.out」,「aa \ x00 \ x00」+ address_bytes])'請注意,「aa」後面還有兩個額外的字符。該結構的成員可能是4個字節對齊。如果這不起作用,請嘗試8字節對齊方式,如果沒有失敗,請切換字節順序並再次嘗試。 – Wug 2012-08-03 22:08:36

回答

2

你混淆的命令行參數:標準輸入。你的命令:

perl -e 'print "aa\xc0\x0c\x00\x00\x01\x00\x00\x00\"'| ./a.out

會寫程序的標準輸入。如果你想要做同樣的事情作爲命令行參數,請嘗試:

perl -e 'print "aa\xc0\x0c\x00\x00\x01\x00\x00\x00\"'| xargs ./a.out

見:http://ss64.com/bash/xargs.html

+0

真棒!謝謝 :) – user1573235 2012-08-03 22:30:12

1

像約翰說,你混淆了標準輸入參數。要通過程序的輸出作爲參數,你可以通過$(command)包裹它使用命令替換,所以對你的例子中,你應該做的

./a.out $(perl -e 'print "aa\xc0\x0c\x00\x00\x01\x00\x00\x00\"') 
1

用bash,那麼你可以這樣做:

./a.out $'aa\xc0\x0c\x00\x00\x01\x00\x00\x00'