linux - Linux系统调用的多个命令

标签 linux command-line-arguments system-calls execve

我正在尝试执行一个具有以下内容的程序(例如 target.c)

void foo(char * arg)
{
    char cmd[16];
    char par[16]; 
    char * p;
    strcpy(cmd, "ls --color -l ");
    strcpy(par, arg);
    printf("You can use \"%s %s\" to list the files in dir \"%s\"!\n",cmd, par, par);
    p = (char*)malloc(strlen(cmd) + strlen(par) + 2);
    strcpy(p, cmd);
    strcat(p, " ");
    strcat(p, par);        
    system(p);
}
int main(int argc, char ** argv)
{         
  int i;
  char test[256];
  if (argc > 1)
      foo(argv[1]);
  else
      printf("usage: %s dir\n", argv[0]);
  return 0;
  foo(test);
};

现在我试图通过从另一个程序调用它来获取 shell(从另一个程序调用它很重要,如下所示:

int main(int argc, char **argv)
{
    char * arrv[] = {NULL};
    char  *payload;
    int i; int j;
    char * argo[] = {"../targets/target1","sdknsd",NULL};
    strcpy(payload,"sd;/bin/sh");
    argo[1] = payload;
    i=fork();
    if(i == 0)
    {
        execve("../targets/target1" ,argo, arrv );
        exit(1);
    }
    else if(i == -1)
    {
        perror("fork()");
    }
}

我的问题是当我尝试执行目标并提供命令行参数时;/bin/sh 然后我得到了 shell,但在从 execve 调用的情况下却没有。 任何帮助将非常感激

好的,这是输出:

[hvalayap@localhost targets]$ ./target1 ds;/bin/sh 
ls: ds: No such file or directory
sh-2.05$

上面的程序将用户输入字符串附加到 ls 并将其传递给系统,因此 system(ls ds;/bin/sh "给我 shell

但是当我尝试对另一个程序(第二个程序)中的 execve 执行相同操作时,它不起作用 说找不到“ds”目录

最佳答案

仔细看看你的代码。 char *payload 在堆栈上,然后您在该地址strcpy,因此您覆盖了堆栈上的局部变量。您没有为此指针分配内存(例如 malloc 或使用本地静态缓冲区)。如果用户输入的字符串更长(例如 255 个符号),您会收到段错误错误。

顺便说一句:为什么不使用 snprintf 而不是 strcpy?我认为更加安全。

关于linux - Linux系统调用的多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26812659/

相关文章:

linux - 为 0 或 4(不是 1、2、3)指定匹配重复

c++ - 如何在带有 GDB GUI 前端的 ARM gdbserver 的 PC 上执行远程 gdb session ?

c++ - 从 XML(或类似的)生成 C/C++ 命令行参数解析代码

无法将命令行参数读入整数数组 C

linux - linux中的ioctl系统调用可以被信号打断吗?

c - 我怎样才能获得 Linux 系统调用的列表和它们自动采用的参数数量?

c - 装载机到底是什么以及在哪里?

linux - 为什么我收到 "Unable to find the checksum in the image"错误?

linux - 如何获取 CPU 使用率

c - 尝试从 argv[] 读取文件时出现核心段错误