6 die "\n\nusage: prepare_inject.pl [-w output_file_name inject_object] [-e command {arg_list} ]\n\n".
7 "You must either indicate to write out an injected file with -w or to execute a command,".
8 " with -e, or both.\n\n".
9 "\t'output_file_name' is what the name of the inject_object will be when it is written out to the guest.\n\n".
10 "\t'inject_object' is the file that will be written out to the guest. This could be a text file, program, or ".
11 "really anything.\n\n".
12 "\t'command' is the fully qualified path name for a file within the guest to execute, either by itself, ".
13 "or after a specified inject_object is written out.\n\n";
16 &GetOptions("w:s{2}" => \@write_opts, "e:s{,}" => \@exec_opts, "output:s" => \$out_name) or usage();
18 usage() unless (@exec_opts || @write_opts);
29 $hfile .= "#define DO_FORKEXEC\n";
30 $hfile .= "#define CMD \"$cmd\"\n";
32 $numargs = scalar(@exec_opts);
33 $hfile .= "char * const args[".$numargs."] = {\"".join('","', @exec_opts)."\"};\n";
38 $out_file = $write_opts[0];
39 $inject_file = $write_opts[1];
41 $hfile .= "#define DO_WRITE\n";
42 $hfile .= "#define FILE_NAME \"$out_file\"\n";
45 $size = `ls -l $inject_file | cut -f5 -d ' '`;
46 $hfile .= "#define FILE_LENGTH $size\n";
48 # generate a string from the file, char * inject_file = string
49 open FILE, $inject_file or die $!;
52 while (($n = read FILE, $data, 1) != 0) {
53 $buf .= "\\x" . unpack("H8", $data);
57 $hfile .= "char * inject_file = \"$buf\";\n\n\n";
62 # write out the h file
63 open (W, ">generated.h") or die $!;
67 print "running special inject code compilation and linking...\n";
68 # compile with generated h file and inject_code_template.c with peter's script
69 $compile_cmd = "perl compile-for-static-user-level-injection.pl -32 ";
70 $compile_cmd .= "--output=$out_name " if defined($out_name);
71 $compile_cmd .= "inject_code_template.c";