1

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:

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

#!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 :(
7
  • 3
    This approach seems odd; a shell script that executes a C program that opens a different shell. What problem are you trying to solve? Maybe someone can suggest a better approach. Commented Mar 22, 2015 at 18:46
  • its for a piece of work at uni lecturer wants a script that shows how we exploited a program - I have run a series of Linux commands separately I want to consolidate them all into 1 script to automatically run the commands I used. In this case - the program provided creates a shell I then need to run appropriate commands in this shell and was hoping I could do this using a single script (I know its easier not to) Commented Mar 22, 2015 at 18:53
  • 1
    Where does the C program come into that equation? A script to run a series of commands in sequence is just the commands in sequence in the file. Why involve this C program at all? Commented Mar 22, 2015 at 18:59
  • Are you trying to generate a shell commands into a file and then execute generated shell file? You just have to chmod u+x <generated script file> and then execute it with ./<generated script file>if it's generated in the current directory. This script will run on a child shell process. Commented Mar 22, 2015 at 18:59
  • Forget my previous comment I think this explains better: I have been given a C program with root permissions for example, it runs a shell - I want a bash script to firstly run the C program (which I can do) and then pass commands to the bash shell it generates (which I cannot seem to work out) Commented Mar 22, 2015 at 19:04

2 Answers 2

1

Your compiled C binary has the SETUID permission i suppose? 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. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.