简体   繁体   English

将命令传递到使用bash脚本运行Shell的Binary中

[英]Passing Commands into a Binary that runs a shell using a bash script

New to bash scripting so having a little teething problem and was wondering if someone could clear up some trouble I'm having, I have a simple C file called test that creates a shell as seen below: bash脚本的新手,所以有一个小问题,并且想知道是否有人可以解决我遇到的麻烦,我有一个简单的C文件,称为test,它创建了一个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;
}

I want to create a bash script to execute this file which I have done below, but then on execution I then wish to send commands to the shell that the binary creates - is this possible I am trying the following to no avail: 我想创建一个bash脚本来执行此文件,该文件我已经在下面完成了,但是在执行时,我希望将命令发送到该二进制文件创建的外壳-这是否可能使我无济于事:

#!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 :(

Your compiled C binary has the SETUID permission i suppose? 我想您编译的C二进制文件具有SETUID权限吗? With the binary and arguments you can execute any shell like this with this binary permission: 使用二进制文件和参数,您可以使用以下二进制权限执行任何这样的外壳程序:

#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;
}

Output of a test script to print arguments and current process: 输出测试脚本以输出参数和当前过程:

$ ./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

Security issue 安全问题
If you can execute a script to a root shell, anyone can do this. 如果您可以对根shell执行脚本,那么任何人都可以这样做。 I think you just have to add some of your scripts (only those needed) with a sudo permission to be run as root from your the needed account. 我认为您只需要添加一些具有sudo权限的脚本(仅需要的脚本)即可从所需帐户作为root用户运行。

Try this: 尝试这个:

#!/bin/bash
/var/testfolder/test <<EOF
cmdtopass1
cmdtopass2
cmdtopass3
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM