diff --git a/.gitignore b/.gitignore index a2a9d70..5acec18 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ /gmon.out /profiling/* /profiling.* -/include/g_*.h +/include/g_* +/src/g_* diff --git a/Makefile b/Makefile index 6e78452..ac48fe6 100644 --- a/Makefile +++ b/Makefile @@ -59,6 +59,9 @@ $(ODIR)/%.o: $(SRCDIR)/%.cpp $(DEPS) $(ODIR)/main.o: $(SRCDIR)/main.cpp $(DEPS) $(IDIR)/g_version.h $(CC) $(CXXFLAGS) -c -o $@ $< +$(ODIR)/shellcode.o: $(SRCDIR)/shellcode.cpp $(DEPS) $(IDIR)/g_shellcode.h + $(CC) $(CXXFLAGS) -c -o $@ $< + $(ODIR)/debashify.o: $(SRCDIR)/debashify.cpp $(DEPS) $(IDIR)/g_shellcode.h $(CC) $(CXXFLAGS) -c -o $@ $< diff --git a/README.md b/README.md index fbe24e2..b03da27 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # lxsh -Extended shell linker for linking, processing and minimizing shell code +Extended shell linker for linking, processing and minifying shell code # Installing @@ -31,9 +31,9 @@ See [Build](#build). # Features -## Command extensions +## Linking -lxsh implements special linking commands that are resolved at linking. +lxsh implements special linking commands that are resolved at build time. These commands can be placed anywhere within the script like regular commands. - `%include` : allows to insert file contents @@ -41,20 +41,20 @@ These commands can be placed anywhere within the script like regular commands. > See `lxsh --help-commands` for more details -## Minimize code +## Minify code Reduce code size to a minimum without changing functionality with the `-m` option. -### Further minimizing +### Further minifying -The script can be further minimized by altering code elements. +The script can be further minified by altering code elements. This can cause some change in execution behavior if you are not careful. -Variable names can be minimized with `--minimize-var`, -use `--exclude-var` to exclude variables from being minimized (for example environment config). +Variable names can be minified with `--minify-var`, +use `--exclude-var` to exclude variables from being minified (for example environment config). -Function names can be minimized with `--minimize-fct`, -use `--exclude-fct` to exclude functions from being minimized. +Function names can be minified with `--minify-fct`, +use `--exclude-fct` to exclude functions from being minified. Unused functions and variables can be removed with `--remove-unused`. diff --git a/generate_shellcode.sh b/generate_shellcode.sh index f758da7..876e14d 100755 --- a/generate_shellcode.sh +++ b/generate_shellcode.sh @@ -5,7 +5,7 @@ tmpfile=${TMPDIR-/tmp}/lxsh_shellcodegen codedir=shellcode # $1 = file -minimize() { +minify() { if which lxsh >/dev/null 2>&1 ; then lxsh -m "$1" elif which shfmt >/dev/null 2>&1 ; then @@ -19,12 +19,21 @@ to_cstr() { sed 's|\\|\\\\|g;s|\"|\\\"|g' | sed ':a;N;$!ba;s/\n/\\n/g;' } -echo '#ifndef G_VERSION_H' > "$tmpfile" -echo '#define G_VERSION_H' >> "$tmpfile" + +cat > "$tmpfile" << EOF +#ifndef G_VERSION_H +#define G_VERSION_H +EOF + +unset all_fields for I in "$codedir"/*.sh do - printf '#define %s "%s\\n"\n' "$(basename "$I" | tr [:lower:] [:upper:] | tr '.' '_')" "$(minimize "$I" | to_cstr)" >> "$tmpfile" + field=$(basename "$I" | tr [:lower:] [:upper:] | tr '.' '_') + all_fields="$all_fields $field" + printf '#define %s "%s\\n"\n' "$field" "$(minify "$I" | to_cstr)" >> "$tmpfile" done + + echo "#endif" >> "$tmpfile" if [ "$(md5sum "$tmpfile" | cut -d' ' -f1)" != "$(md5sum "$file" | cut -d' ' -f1)" ] ; then diff --git a/include/minimize.hpp b/include/minify.hpp similarity index 57% rename from include/minimize.hpp rename to include/minify.hpp index eb6f61e..53b6011 100644 --- a/include/minimize.hpp +++ b/include/minify.hpp @@ -1,18 +1,18 @@ -#ifndef MINIMIZE_HPP -#define MINIMIZE_HPP +#ifndef MINIFY_HPP +#define MINIFY_HPP #include "struc.hpp" #include #include -void minimize_var(_obj* in, std::regex const& exclude); -void minimize_fct(_obj* in, std::regex const& exclude); +void minify_var(_obj* in, std::regex const& exclude); +void minify_fct(_obj* in, std::regex const& exclude); bool delete_unused_fct(_obj* in, std::regex const& exclude); bool delete_unused_var(_obj* in, std::regex const& exclude); void delete_unused(_obj* in, std::regex const& var_exclude, std::regex const& fct_exclude); -void minimize_quotes(_obj* in); +void minify_quotes(_obj* in); -#endif //MINIMIZE_HPP +#endif //MINIFY_HPP diff --git a/include/options.hpp b/include/options.hpp index a02ebd9..57edd46 100644 --- a/include/options.hpp +++ b/include/options.hpp @@ -5,12 +5,14 @@ extern ztd::option_set options; -extern bool opt_minimize; +extern bool opt_minify; extern bool g_cd; extern bool g_include; extern bool g_resolve; extern bool g_shebang; +void print_lxsh_cmd_help(); + void get_opts(); ztd::option_set gen_options(); diff --git a/include/processing.hpp b/include/processing.hpp index 083cde3..020a5cb 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -9,7 +9,7 @@ #include "struc.hpp" // constants -#define RESERVED_VARIABLES "HOME", "PATH", "SHELL", "PWD", "OPTIND", "OPTARG" +#define RESERVED_VARIABLES "HOME", "PATH", "SHELL", "PWD", "OPTIND", "OPTARG", "LC_.*", "LANG", "TERM", "RANDOM" // types typedef std::map countmap_t; diff --git a/include/shellcode.hpp b/include/shellcode.hpp new file mode 100644 index 0000000..4302863 --- /dev/null +++ b/include/shellcode.hpp @@ -0,0 +1,15 @@ +#ifndef SHELLCODE_HPP +#define SHELLCODE_HPP + +#include +#include + +struct lxsh_fct { + std::string arguments; + std::string description; + const char* code; +}; + +extern const std::map lxsh_shellcode_fcts; + +#endif //SHELLCODE_HPP diff --git a/shellcode/array_create.sh b/shellcode/array_create.sh index 315ff52..40cd1e3 100644 --- a/shellcode/array_create.sh +++ b/shellcode/array_create.sh @@ -1,4 +1,4 @@ -__lxsh_array_create() { +_lxsh_array_create() { printf "%s" "$1" shift 1 for N ; do diff --git a/shellcode/array_get.sh b/shellcode/array_get.sh index 262151c..bcf3607 100644 --- a/shellcode/array_get.sh +++ b/shellcode/array_get.sh @@ -1,4 +1,4 @@ -__lxsh_array_get() { +_lxsh_array_get() { if [ "$2" = "*" ] || [ "$2" = "@" ] ; then printf "%s" "$1" | tr '\t' ' ' else diff --git a/shellcode/array_set.sh b/shellcode/array_set.sh index 31a4e99..998ccd1 100644 --- a/shellcode/array_set.sh +++ b/shellcode/array_set.sh @@ -1,4 +1,4 @@ -__lxsh_array_set() +_lxsh_array_set() { [ "$2" -gt 0 ] && printf "%s\t" "$(printf "%s" "$1" | cut -f1-$2)" printf "%s" "$3" diff --git a/shellcode/map_create.sh b/shellcode/map_create.sh index 91aa608..afd554e 100644 --- a/shellcode/map_create.sh +++ b/shellcode/map_create.sh @@ -1,4 +1,4 @@ -__lxsh_map_create() { +_lxsh_map_create() { for I do printf "%s]%s\n" "$(echo "$I" | cut -d']' -f1 | cut -d '[' -f2)" "$(echo "$I" | cut -d '=' -f2-)" diff --git a/shellcode/map_get.sh b/shellcode/map_get.sh index 9be6188..3be60e0 100644 --- a/shellcode/map_get.sh +++ b/shellcode/map_get.sh @@ -1,4 +1,4 @@ -__lxsh_map_get() { +_lxsh_map_get() { if [ "$2" = \* ] || [ "$2" = @ ] ; then printf "%s" "$(printf "%s" "$1" | sort | cut -d ']' -f2-)" | tr '\n' ' ' else diff --git a/shellcode/map_set.sh b/shellcode/map_set.sh index 55a9af6..d59060f 100644 --- a/shellcode/map_set.sh +++ b/shellcode/map_set.sh @@ -1,4 +1,4 @@ -__lxsh_map_set() { +_lxsh_map_set() { printf "%s\n" "$1" | grep -v "^$2\]" if [ -n "$3" ] ; then printf "%s]%s\n" "$2" "$3" diff --git a/shellcode/random_string.sh b/shellcode/random_string.sh index 134da5a..e178921 100644 --- a/shellcode/random_string.sh +++ b/shellcode/random_string.sh @@ -1,3 +1,3 @@ -__lxsh_random_string() { +_lxsh_random_string() { env LC_CTYPE=C tr -dc 'a-zA-Z0-9' arrays[varname]) { - c = make_cmd_varindex("__lxsh_map_get", varname, index); + c = make_cmd_varindex("_lxsh_map_get", varname, index); params->need_map_get=true; } else { - c = make_cmd_varindex("__lxsh_array_get", varname, index); + c = make_cmd_varindex("_lxsh_array_get", varname, index); params->need_array_get=true; } @@ -429,12 +429,12 @@ bool debashify_array_get(arg* in, debashify_params* params) cmd* c; if(params->arrays[varname]) { - c = make_cmd_varindex("__lxsh_map_get", varname, index); + c = make_cmd_varindex("_lxsh_map_get", varname, index); params->need_map_get=true; } else { - c = make_cmd_varindex("__lxsh_array_get", varname, index); + c = make_cmd_varindex("_lxsh_array_get", varname, index); params->need_array_get=true; } @@ -465,15 +465,15 @@ bool debashify_array_set(cmd* in, debashify_params* params) // create cmd out of arguments arglist* args = parse_arglist( gen.c_str(), gen.size(), 0 ).first; cmd* c = new cmd(args); - // cmd first argument is __lxsh_X_create + // cmd first argument is _lxsh_X_create if(params->arrays[varname]) { - c->args->insert(0, new arg("__lxsh_map_create") ); + c->args->insert(0, new arg("_lxsh_map_create") ); params->need_map_create=true; } else { - c->args->insert(0, new arg("__lxsh_array_create") ); + c->args->insert(0, new arg("_lxsh_array_create") ); params->need_array_create=true; } subshell_subarg* sb = new subshell_subarg(new subshell(c)); @@ -506,12 +506,12 @@ bool debashify_array_set(cmd* in, debashify_params* params) cmd* c; if(params->arrays[varname]) { - c = make_cmd_varindex("__lxsh_map_get", varname, copy(index)); + c = make_cmd_varindex("_lxsh_map_get", varname, copy(index)); params->need_map_get=true; } else { - c = make_cmd_varindex("__lxsh_array_get", varname, copy(index)); + c = make_cmd_varindex("_lxsh_array_get", varname, copy(index)); params->need_array_get=true; } subshell_subarg* sb = new subshell_subarg(new subshell(c)); @@ -527,21 +527,21 @@ bool debashify_array_set(cmd* in, debashify_params* params) cmd* c = new cmd(new arglist); if(params->arrays[varname]) { - c->args->add(new arg("__lxsh_map_set") ); + c->args->add(new arg("_lxsh_map_set") ); params->need_map_set=true; } else { - c->args->add(new arg("__lxsh_array_set") ); + c->args->add(new arg("_lxsh_array_set") ); params->need_array_set=true; } - // __lxsh_array_set "$VAR" + // _lxsh_array_set "$VAR" c->args->add( make_arg("\"$"+varname+"\"") ); - // __lxsh_array_set "$VAR" N + // _lxsh_array_set "$VAR" N c->args->add( index ); - // __lxsh_array_set "$VAR" N value + // _lxsh_array_set "$VAR" N value c->args->add( value ); - // $(__lxsh_array_set "$VAR" N value) + // $(_lxsh_array_set "$VAR" N value) subshell_subarg* sb = new subshell_subarg(new subshell(c)); it->second = new arg("="); @@ -563,14 +563,14 @@ bool debashify_array_set(cmd* in, debashify_params* params) // create cmd out of arguments arglist* args = parse_arglist( gen.c_str(), gen.size(), 0 ).first; cmd* c = new cmd(args); - // cmd first argument is __lxsh_array_create + // cmd first argument is _lxsh_array_create if(params->arrays[varname]) { throw std::runtime_error("Cannot debashify VAR+=() on associative arrays"); } else { - c->args->insert(0, new arg("__lxsh_array_create") ); + c->args->insert(0, new arg("_lxsh_array_create") ); params->need_array_create=true; } // second arg is varname @@ -667,7 +667,7 @@ bool debashify_combined_redirects(block* in) // replace <() and >() /* REPLACE TO: - fifoN=${TMPDIR-/tmp}/lxshfifo_$(__lxsh_random_string 10) + fifoN=${TMPDIR-/tmp}/lxshfifo_$(_lxsh_random_string 10) mkfifo "$fifoN" ( {PSUB;} [>|<] "$fifoN" ; rm "$fifoN") & CMD "$fifoN" @@ -709,9 +709,9 @@ bool debashify_procsub(list* lst, debashify_params* params) std::string mkfifocmd="mkfifo"; for(uint32_t i=0; iadd( make_condlist( strf("__lxshfifo%u=$(__lxsh_random_tmpfile lxshfifo)", i) ) ); - mkfifocmd += strf(" \"$__lxshfifo%u\"", i); + // fifoN=${TMPDIR-/tmp}/lxshfifo_$(_lxsh_random_string 10) + lst_insert->add( make_condlist( strf("_lxshfifo%u=$(_lxsh_random_tmpfile lxshfifo)", i) ) ); + mkfifocmd += strf(" \"$_lxshfifo%u\"", i); } // mkfifo "$fifoN" lst_insert->add( make_condlist(mkfifocmd) ); @@ -724,13 +724,13 @@ bool debashify_procsub(list* lst, debashify_params* params) brace* cbr = new brace(st->sbsh->lst); // deindex list for delete st->sbsh->lst=nullptr; - // {PSUB;} > "$__lxshfifoN" - cbr->redirs.push_back( new redirect( affected_args[i].second ? "<" : ">", make_arg(strf("\"$__lxshfifo%u\"", i)) ) ); - // ( {PSUB;} > "$__lxshfifoN" ) + // {PSUB;} > "$_lxshfifoN" + cbr->redirs.push_back( new redirect( affected_args[i].second ? "<" : ">", make_arg(strf("\"$_lxshfifo%u\"", i)) ) ); + // ( {PSUB;} > "$_lxshfifoN" ) psub->lst->add( new condlist(cbr) ); - // ( {PSUB;} > "$__lxshfifoN" ; rm "$__lxshfifoN" ) - psub->lst->add( make_condlist(strf("rm \"$__lxshfifo%u\"", i)) ); - // ( {PSUB;} > "$__lxshfifoN" ; rm "$__lxshfifoN" ) & + // ( {PSUB;} > "$_lxshfifoN" ; rm "$_lxshfifoN" ) + psub->lst->add( make_condlist(strf("rm \"$_lxshfifo%u\"", i)) ); + // ( {PSUB;} > "$_lxshfifoN" ; rm "$_lxshfifoN" ) & condlist* pscl = new condlist(psub); pscl->parallel=true; lst_insert->add( pscl ); @@ -738,7 +738,7 @@ bool debashify_procsub(list* lst, debashify_params* params) // replace the arg delete affected_args[i].first->sa[0]; affected_args[i].first->sa[0] = new string_subarg("\""); - affected_args[i].first->add( new variable_subarg( new variable(strf("__lxshfifo%u", i)) ) ); + affected_args[i].first->add( new variable_subarg( new variable(strf("_lxshfifo%u", i)) ) ); affected_args[i].first->add( new string_subarg("\"") ); } lst->insert(li, *lst_insert ); diff --git a/src/generate.cpp b/src/generate.cpp index 8db7478..d5d02bd 100644 --- a/src/generate.cpp +++ b/src/generate.cpp @@ -13,7 +13,7 @@ bool is_sub_special_cmd(std::string in) std::string indented(std::string const& in, uint32_t ind) { - if(!opt_minimize) + if(!opt_minify) return indent(ind) + in; else return in; @@ -56,7 +56,7 @@ std::string pipeline::generate(int ind) ret += cmds[0]->generate(ind); for(uint32_t i=1 ; igenerate(ind); } @@ -72,16 +72,16 @@ std::string condlist::generate(int ind) for(uint32_t i=0 ; igenerate(ind); } if(ret=="") return ""; if(parallel) { - ret += opt_minimize ? "&" : " &\n"; + ret += opt_minify ? "&" : " &\n"; } else ret += '\n'; @@ -114,7 +114,7 @@ std::string redirect::generate(int ind) std::string ret=op; if(target!=nullptr) { - if(!opt_minimize) + if(!opt_minify) ret += ' '; ret += target->generate(0); } @@ -130,7 +130,7 @@ std::string block::generate_redirs(int ind, std::string const& _str) for(auto it: redirs) { std::string _r = it->generate(0); - if(opt_minimize && _r.size() > 0 && !is_num(_r[0]) && previous_isnt_num) + if(opt_minify && _r.size() > 0 && !is_num(_r[0]) && previous_isnt_num) ret.pop_back(); // remove one space if possible ret += _r + ' '; previous_isnt_num = ret.size()>1 && !is_num(ret[ret.size()-2]); @@ -185,7 +185,7 @@ std::string for_block::generate(int ind) ret += ops->generate(ind+1); ret += indented("done", ind); - if(opt_minimize && ret.size()>1 && !is_alpha(ret[ret.size()-2])) + if(opt_minify && ret.size()>1 && !is_alpha(ret[ret.size()-2])) ret.pop_back(); ret += generate_redirs(ind, ret); return ret; @@ -205,7 +205,7 @@ std::string while_block::generate(int ind) ret += ops->generate(ind+1); ret += indented("done", ind); - if(opt_minimize && ret.size()>1 && !is_alpha(ret[ret.size()-2])) + if(opt_minify && ret.size()>1 && !is_alpha(ret[ret.size()-2])) ret.pop_back(); ret += generate_redirs(ind, ret); return ret; @@ -216,10 +216,10 @@ std::string subshell::generate(int ind) std::string ret; // open subshell ret += '('; - if(!opt_minimize) ret += '\n'; + if(!opt_minify) ret += '\n'; // commands ret += lst->generate(ind+1); - if(opt_minimize && ret.size()>1) + if(opt_minify && ret.size()>1) ret.pop_back(); // ) can be right after command // close subshell ret += indented(")", ind); @@ -238,7 +238,7 @@ std::string shmain::generate(bool print_shebang, int ind) if(print_shebang && shebang!="") ret += shebang + '\n'; ret += lst->generate(ind); - if( opt_minimize && ret[ret.size()-1] == '\n') + if( opt_minify && ret[ret.size()-1] == '\n') ret.pop_back(); ret += generate_redirs(ind, ret); @@ -262,7 +262,7 @@ std::string function::generate(int ind) std::string ret; // function definition ret += name + "()"; - if(!opt_minimize) ret += '\n'; + if(!opt_minify) ret += '\n'; // commands ret += indented("{\n", ind); ret += lst->generate(ind+1); @@ -286,17 +286,17 @@ std::string case_block::generate(int ind) ret += it->generate(ind) + '|'; ret.pop_back(); ret += ')'; - if(!opt_minimize) ret += '\n'; + if(!opt_minify) ret += '\n'; // commands ret += cs.second->generate(ind+1); // end of case: ;; - if(opt_minimize && ret[ret.size()-1] == '\n') // ;; can be right after command + if(opt_minify && ret[ret.size()-1] == '\n') // ;; can be right after command ret.pop_back(); ret += indented(";;\n", ind+1); } // remove ;; from last case - if(this->cases.size()>0 && opt_minimize) + if(this->cases.size()>0 && opt_minify) { ret.erase(ret.size()-3, 2); } @@ -374,9 +374,9 @@ std::string arithmetic_subarg::generate(int ind) { std::string ret; ret += "$(("; - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += arith->generate(ind); - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += "))"; return ret; } @@ -389,15 +389,15 @@ std::string operation_arithmetic::generate(int ind) if(precedence) { ret += oper; - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += val1->generate(ind); } else { ret += val1->generate(ind); - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += oper; - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += val2->generate(ind); } return ret; @@ -407,9 +407,9 @@ std::string parenthesis_arithmetic::generate(int ind) { std::string ret; ret += '('; - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += val->generate(ind); - if(!opt_minimize) ret += ' '; + if(!opt_minify) ret += ' '; ret += ')'; return ret; } diff --git a/src/main.cpp b/src/main.cpp index c10d558..42f75af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ #include "parse.hpp" #include "options.hpp" #include "recursive.hpp" -#include "minimize.hpp" +#include "minify.hpp" #include "resolve.hpp" #include "processing.hpp" #include "debashify.hpp" @@ -39,13 +39,18 @@ void oneshot_opt_process(const char* arg0) printf("%s\n", VERSION_SHA); exit(0); } - else if(options["help-commands"]) + else if(options["help-link-commands"]) { print_include_help(); printf("\n\n"); print_resolve_help(); exit(ERR_HELP); } + else if(options["help-lxsh-commands"]) + { + print_lxsh_cmd_help(); + exit(ERR_HELP); + } } int main(int argc, char* argv[]) @@ -188,17 +193,17 @@ int main(int argc, char* argv[]) } // processing before output - // minimize + // minify if(options['m']) - opt_minimize=true; + opt_minify=true; if(options["remove-unused"]) delete_unused( sh, re_var_exclude, re_fct_exclude ); - if(options["minimize-quotes"]) - minimize_quotes(sh); - if(options["minimize-var"]) - minimize_var( sh, re_var_exclude ); - if(options["minimize-fct"]) - minimize_fct( sh, re_fct_exclude ); + if(options["minify-quotes"]) + minify_quotes(sh); + if(options["minify-var"]) + minify_var( sh, re_var_exclude ); + if(options["minify-fct"]) + minify_fct( sh, re_fct_exclude ); // other processing if(options["unset-var"]) add_unset_variables( sh, re_var_exclude ); diff --git a/src/minimize.cpp b/src/minify.cpp similarity index 94% rename from src/minimize.cpp rename to src/minify.cpp index ca8ae5b..3c339ae 100644 --- a/src/minimize.cpp +++ b/src/minify.cpp @@ -1,4 +1,4 @@ -#include "minimize.hpp" +#include "minify.hpp" #include "parse.hpp" @@ -105,7 +105,7 @@ bool is_this_quote(char c, bool is_doublequote) return c == '\''; } -void do_one_minimize_quotes(string_subarg* in, bool prev_is_var, bool start_quoted) +void do_one_minify_quotes(string_subarg* in, bool prev_is_var, bool start_quoted) { std::string& val = in->val; if(val.size() <= 1) @@ -191,7 +191,7 @@ void do_one_minimize_quotes(string_subarg* in, bool prev_is_var, bool start_quot } -bool r_minimize_useless_quotes(_obj* in) +bool r_minify_useless_quotes(_obj* in) { switch(in->type) { @@ -209,9 +209,9 @@ bool r_minimize_useless_quotes(_obj* in) if(vs->var != nullptr && vs->var->is_manip == false && vs->var->varname.size()>0 && !(is_in(vs->var->varname[0], SPECIAL_VARS) || is_alpha(vs->var->varname[0]) ) ) prev_is_var=true; } - if(t->sa.size()==1 && (ss->val=="\"\"" || ss->val=="''") ) // single argument as "" or '': don't minimize + if(t->sa.size()==1 && (ss->val=="\"\"" || ss->val=="''") ) // single argument as "" or '': don't minify continue; - do_one_minimize_quotes(ss, prev_is_var, i>0 && t->sa[i-1]->quoted); + do_one_minify_quotes(ss, prev_is_var, i>0 && t->sa[i-1]->quoted); } //if() } @@ -221,7 +221,7 @@ bool r_minimize_useless_quotes(_obj* in) return true; } -/** NAME MINIMIZING **/ +/** NAME MINIFYING **/ char nchar(uint32_t n) { @@ -282,7 +282,7 @@ strmap_t gen_minimal_map(countmap_t const& vars, set_t const& excluded) // calls -void minimize_var(_obj* in, std::regex const& exclude) +void minify_var(_obj* in, std::regex const& exclude) { // countmap_t vars; set_t excluded; @@ -296,7 +296,7 @@ void minimize_var(_obj* in, std::regex const& exclude) require_rescan_var(); } -void minimize_fct(_obj* in, std::regex const& exclude) +void minify_fct(_obj* in, std::regex const& exclude) { // countmap_t fcts, cmdmap; set_t allcmds, excluded, unsets; @@ -362,9 +362,9 @@ bool delete_unused_var(_obj* in, std::regex const& exclude) return false; } -void minimize_quotes(_obj* in) +void minify_quotes(_obj* in) { - recurse(r_minimize_useless_quotes, in); + recurse(r_minify_useless_quotes, in); } void delete_unused(_obj* in, std::regex const& var_exclude, std::regex const& fct_exclude) diff --git a/src/options.cpp b/src/options.cpp index 05e487b..5ecf536 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -1,9 +1,10 @@ #include "options.hpp" #include "processing.hpp" +#include "shellcode.hpp" ztd::option_set options = gen_options(); -bool opt_minimize=false; +bool opt_minify=false; bool g_cd=false; bool g_include=true; @@ -17,7 +18,8 @@ ztd::option_set gen_options() ztd::option("\r [Help]"), ztd::option('h', "help", false, "Display this help message"), ztd::option("version", false, "Display version"), - ztd::option("help-commands", false, "Print help for linker commands"), + ztd::option("help-link-commands", false, "Print help for special lxsh commands"), + ztd::option("help-lxsh-commands", false, "Print help for linker commands"), ztd::option("\r [Output]"), ztd::option('o', "output", true , "Output result script to file", "file"), ztd::option('c', "stdout", false, "Output result script to stdout"), @@ -25,15 +27,15 @@ ztd::option_set gen_options() ztd::option("no-shebang", false, "Don't output shebang"), ztd::option('J', "json", false, "Output the json structure"), ztd::option("\r [Processing]"), - ztd::option('m', "minimize", false, "Minimize code without changing functionality"), - ztd::option("minimize-quotes", false, "Remove unnecessary quotes"), + ztd::option('m', "minify", false, "Minify code without changing functionality"), + ztd::option("minify-quotes", false, "Remove unnecessary quotes"), ztd::option('C', "no-cd", false, "Don't cd when doing %include and %resolve"), ztd::option('I', "no-include", false, "Don't resolve %include commands"), ztd::option('R', "no-resolve", false, "Don't resolve %resolve commands"), ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"), ztd::option("\r [var/fct processing]"), - ztd::option("minimize-var", false, "Minimize variable names"), - ztd::option("minimize-fct", false, "Minimize function names"), + ztd::option("minify-var", false, "Minify variable names"), + ztd::option("minify-fct", false, "Minify function names"), ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing", "list"), ztd::option("exclude-fct", true, "List of matching regex to ignore for function processing", "list"), ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"), @@ -121,3 +123,11 @@ void print_resolve_help() printf("Options:\n"); opts.print_help(3,7); } + +void print_lxsh_cmd_help() +{ + for(auto it: lxsh_shellcode_fcts) + { + printf("%s %s\n%s\n\n", it.first.c_str(), it.second.arguments.c_str(), it.second.description.c_str()); + } +} diff --git a/src/shellcode.cpp b/src/shellcode.cpp new file mode 100644 index 0000000..45a5078 --- /dev/null +++ b/src/shellcode.cpp @@ -0,0 +1,8 @@ +#include "shellcode.hpp" + +#include "g_shellcode.h" + +const std::map lxsh_shellcode_fcts = { + { "_lxsh_random_string", { "N", "Generate a random alphanumeric string of length N. Default 20", RANDOM_STRING_SH} }, + { "_lxsh_random_tmpfile", { "N", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH} } +}; diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp index 8ef3c7e..68b5cc2 100644 --- a/src/struc_helper.cpp +++ b/src/struc_helper.cpp @@ -164,13 +164,13 @@ std::string arg::first_sa_string() std::vector arglist::strargs(uint32_t start) { std::vector ret; - bool t=opt_minimize; - opt_minimize=true; + bool t=opt_minify; + opt_minify=true; for(uint32_t i=start; igenerate(0)); } - opt_minimize=t; + opt_minify=t; return ret; }