linux - 将命令传递到使用 bash 脚本运行 shell 的二进制文件中

标签 linux bash shell

刚接触 bash 脚本,所以遇到了一些初期问题,想知道是否有人可以解决我遇到的一些问题,我有一个名为 test 的简单 C 文件,它创建了一个 shell,如下所示:

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

int main()
{
  execl("/bin/sh", "/bin/sh", (void *) NULL);
  perror("exec");
 return 1;
}

我想创建一个 bash 脚本来执行我在下面完成的这个文件,但是在执行时我希望将命令发送到二进制文件创建的 shell - 这是否可能,我正在尝试以下无济于事:

#!bin/bash
/var/testfolder/test; # execute the test c file to spawn the shell
??? #don't know how to pass commands to the new shell created :(

最佳答案

我想你编译的 C 二进制文件具有 SETUID 权限?使用二进制文件和参数,您可以使用此二进制权限执行任何像这样的 shell:

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

int main( int argc, char ** argv )
{
    execv("/bin/sh", argv );
    perror("exec");
    return 1;
}

输出测试脚本以打印参数和当前进程:

$ ./test_execv ./test.sh foo bar
Executing $0: ./test.sh
Args $*: foo bar
process:
  PID TTY          TIME CMD
 3300 pts/1    00:00:08 bash
 3498 pts/1    00:00:00 sh
 3499 pts/1    00:00:00 ps

安全问题
如果您可以在 root shell 中执行脚本,那么任何人都可以执行此操作。我认为您只需添加一些具有 sudo 权限的脚本(仅需要那些脚本),即可从您所需的帐户以 root 身份运行。

关于linux - 将命令传递到使用 bash 脚本运行 shell 的二进制文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198324/

相关文章:

linux - 如何使用终端备份文件?

c++ - 使用 C 中系统调用的文件描述符

python - ALSA & Python - 捕获多个单声道音频输入

bash - 如何仅使用 bash(无编辑器)创建 Kubernetes YAML 部署对象?

c - 在 C 中为 shell 脚本使用变量

linux - 如何在忽略某些目录名称的同时比较两个目录?

bash - 使用 bash 命令行,如何将 "import package.name.*;"添加到许多 java 文件?

bash - 如何使用脚本 cd 进入目录?

linux - 将传递给 shell 脚本的参数 ${1} 保存在 shell 脚本中的变量中

shell - Jenkins 执行 shell 找不到 gradle 命令