-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfakeshellcompiler.sh
More file actions
executable file
·62 lines (47 loc) · 1.34 KB
/
Copy pathfakeshellcompiler.sh
File metadata and controls
executable file
·62 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh -e
#licensed under the MIT licence
#see README file
if test ! -n "$1" ; then
echo 'Please supply command argument for file to '\"compile\" > /dev/stderr.
exit
fi
if test $1 = '-h' || test "$1" = '--help' ; then
echo 'usage: ./fakeshellcompiler <shellscript>'
echo 'creates a binary file named <shellscript>.out'
exit
fi
if test ! -f $1 ; then
echo "File '$1' does not exist or is not a regular file" > /dev/stderr
exit
fi
#quote the source to make it valid for inserting
#into C source
quotedShellFile=`sed 's/\\\\/\\\\\\\\/g' < $1 | #escape backslashes
sed 's/"/\\\\"/g' | #escape double quotes
sed 's/^/"/g' | # a double quote at the beginning
sed 's/$/\\\\n"/g'` # a backslash, n and double quote at the end
#choose a temporary file
tmpname=/tmp/fake_shell_build_file_`date +%N`.c
#create a C source file that calls a system command
#which runs the shell interpreter and
#interprets the constant string.
cat > $tmpname <<END
#include <unistd.h>
int main( int argc, char *argv[] ) {
char *file =
$quotedShellFile;
char* memory[argc+4];
memory[0] = "sh";
memory[1] = "-c";
memory[2] = file;
for (int i = 0; i < argc; ++i) {
memory[i+3] = argv[i];
}
memory[argc+3] = (char *) NULL;
execv("/bin/sh", memory);
}
END
#compile the c source down into a binary
cc -o $1.out $tmpname
#clean up the tmpfile
rm $tmpname