It depends on what you mean by obfuscating.
Here's an approach that lets you wrap scripts in compilable programs,
allowing you to turn them into binary executables.
Write a C program that looks like this:
#include <stdio.h>
#include <stdlib.h>
︙
FILE *sh;
sh = popen("/bin/sh", "w"); // Use "/bin/bash" if you need to,
// or whatever interpreter you need.
if (sh == (FILE *)NULL) {
perror("popen");
exit(1);
}
/* Embed your script here. */
fprintf(sh, "for x in red blue green\n");
fprintf(sh, "do\n");
fprintf(sh, " echo x = \"$x\"\n");
fprintf(sh, "done\n");
/* End of your script. */
fclose(sh);
If it’s more than just a few lines,
it may be more manageable to store it as a string array.
Then compile this program.
- If you want the program/script to be executable by non-root users,
make the binary execute-only (e.g., mode 710 or 711).
- Of course, root will be able to read the file regardless of its mode.
While the binary executable will appear at first glance
to be just a binary executable,
it is trivial to extract the text of the script with the
strings
command.
You can mitigate this a little by adding some lightweight
pseudo-encryption of the script text in the C program;
e.g., something trivial like XORing each byte with a constant
(ideally one with the 0x80 bit set).
This cannot provide real security;
since the program must contain the logic to "decrypt" the script,
any analyst can do the same thing.
This provides only a little "security through obscurity".
No solution will be foolproof,
because (at least for a shell script) users will be able to see
what commands are being run by using ps
in another window;
but that would give them only fragmentary glimpses at the script.
Of course, if the logic of your script is really sensitive,
the real answer is to translate it into a compilable language.
(Adapted from Avoiding users to corrupt and use a script.)
echo "echo 'Hello World'" | bash
orruby -e "puts 'Hello World'"
for a whole script so I don't need the actual file. Are there no proper solution for this kind of problem? – f00860 May 28 '14 at 14:17system()
call. – Bratchley May 28 '14 at 14:22