#!/bin/bash
echo "hello"
echo "how are you"
echo "today"
how can i put the entire script above into a basic c program?
i do not want to translate the bash code to a c code. i want C to run the bash code. is this possible?
i found this on the internet but i'm not sure i understand it:
#include <stdio.h>
#include <stdlib.h>
#define SHELLSCRIPT "\
for ((i=0 ; i < 10 ; i++))\n\
do\n\
echo \"Count: $i\"\n\
done\n\
"
int main()
{
puts("Will execute sh with the following script :");
puts(SHELLSCRIPT);
puts("Starting now:");
system(SHELLSCRIPT);
return 0;
}
i'm looking for a C program where i can just insert the bash code into it and have it run. something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
puts("Will execute sh with the following script :");
puts(#!/bin/bash
echo "hello"
echo "how are you"
echo "today");
puts("Starting now:");
system(SHELLSCRIPT);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"
int main()
{
puts("Will execute sh with the following script :");
puts(SHELLSCRIPT);
puts("Starting now:");
system(SHELLSCRIPT);
return 0;
}
The #define SHELLSCRIPT directive is used in C to define a named constant: SHELLSCRIPT which contains the shell script.
The back slash \ at the end of each line is used to type the code in next line for better readability.
The new line \n is used to put the code in next line.
Double quotes " within the shell script has to be escaped \" to distinguish it between the original opening and closing double quotes of constant value.
Finally the system function is called by passing the named constant: SHELLSCRIPT as argument which contains the shell script.
the bash script i intend on embedding into this C program will be accepting several arguments from the command line. and it is a very big bash script. will C be able to catch the arguments?
meaning, after i have embedded the bash script into C, i will be running the C program from the command line like this:
the arguments are meant for the bash script. will C be able to pass it to the script?
if i was running the bash script alone without it being embedded into C, the bash script will accept and use the arguments just fine. but i just want to make sure when it is embedded in C there wont be problems.
No, that will not work. To make things simple I would suggest to write the bash script in a separate file and call it from C program by passing the required arguments:
that was my first option. but the whole goal here was to conceal the bash code. so i figure if i can embed it into C, i'll be able to hide the code. but if C is calling the script, the script will be open for everyone to read.
as i was typing this, something else occurred to me. i'm thinking, i can encode the script using a password. tell C to decode the script using the password i put in the C code. and then C can call the script after script has been decoded.
If you don't want anyone to know what your code does, you can't let them have any access at all to it.
If you distribute a binary to run on another's computer(s), anyone who really wants to is going to be able to figure out everything it does.
Although I will admit that there can be value in obfuscating what's going on, if only to prevent those just smart enough to be dangerous from damaging things....
It would be better to put the data in the C part. If you make it a global array, they can be quite large. You could write it into the script's standard input with popen.
you might want a decryption step between that and writing it of course, since people may be peeking in your file.
By definition your program includes complete instructions for decrypting itself. How could you possibly hope to hide it? At best you can obfuscate it. A truly determined person will find the contents.
If you could force people to login to your sever to use it, that could be made secure. the program wouldn't need to run on their machine, they wouldn't have access to the file.
The bash script is over 10 MB in size? I think this needs to be converted to a compiled program in C, etc. How can you possibly maintain a bash script that large?
I think most of the 10mb is file contents, not script code. A big tarball that's been base64'ed into text or something. It's embedded in the script with the hopes of obscuring it.