如何在终端中使用它:

时间:2014-03-10 22:37:19

标签: c terminal

我得到了一个moddified ls:

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

int main(int argc, char **argv){
    char command[50];
    strcpy(command,"/bin/ls ");
    gid_t egid = getegid();
    setregid(egid, egid);
    if(argc > 1) {
        if(strlen(argv[1]) > 40) {
            printf("The command you have given is too long, try again.\n");
            return 0;
        }
        strcat(command,argv[1]);
        system(command);
    }else{
        printf("This is a special NSA-modified 'ls' program. See 'man ls' for further details on how to use it.\n");
        printf("USAGE: %s [flags & files]\n",argv[0]);
    }
    return 0;
}

我必须执行一个名为get-code的程序,但我没有ls执行它的权限(修改后的ls与get-code程序在同一个目录中),所以我怎么能欺骗system()使用修改后的ls?

执行get-code

1 个答案:

答案 0 :(得分:0)

关键是这一行:strcat(command,argv[1]);你怎么能这样做,所以命令不仅仅是ls?

要在同一行上执行多个命令,可以使用;字符将它们分开。所以echo "hello"; echo "world"将让shell一个接一个地执行这两个命令。知道了,system(command);之后如何让get-code执行ls