簡體   English   中英

在Mac OS X上將Shell參數傳遞給C程序

[英]passing shell argument to c program on mac osx

出於教育目的,我正在嘗試運行一個使用緩沖區溢出來覆蓋函數指針地址的程序。 我已經確定要使用nm覆蓋的函數指針的位置。 然后,我想將新函數指針的地址傳遞給argv。 我正在嘗試使用

perl -e'打印“ 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。

我附上了程序的副本,以便於后續操作。 還有沒有更簡單的方法將格式化的字節直接傳遞到ac程序而不是使用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;
}

您將命令行參數與stdin混淆了。 您的命令:

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

就像約翰所說的那樣,您在用stdin混淆參數。 要將程序的輸出作為參數傳遞,可以通過將命令包裝在$(command)來使用命令替換,因此對於您的示例,您應該執行

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

使用bash,您可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM