From 588aae09e9d0f14b99bdd6c9d9153fedc3c0f3dd Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 26 Mar 2021 13:41:04 +0100 Subject: [PATCH] implement lxsh commands + reorder processing order --- include/processing.hpp | 2 ++ src/main.cpp | 73 ++++++++++++++++++++++-------------------- src/processing.cpp | 20 ++++++++++++ 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/include/processing.hpp b/include/processing.hpp index 020a5cb..9e668a2 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -31,6 +31,7 @@ extern bool b_gotvar, b_gotfct, b_gotcmd; /** map get functions (optimizations) **/ + // rescans void require_rescan_all(); void require_rescan_var(); @@ -72,6 +73,7 @@ bool r_delete_var(_obj* in, set_t* vars); /** Processing **/ +void insert_lxsh_commands(shmain* sh); void add_unset_variables(shmain* sh, std::regex const& exclude); #endif //PROCESSING_HPP diff --git a/src/main.cpp b/src/main.cpp index 42f75af..9fc3783 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -187,27 +187,6 @@ int main(int argc, char* argv[]) } } // end of argument parse - if(options["debashify"]) - { - debashify(sh); - } - - // processing before output - // minify - if(options['m']) - opt_minify=true; - if(options["remove-unused"]) - delete_unused( sh, re_var_exclude, 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 ); - // list outputs if(options["list-var"]) list_vars(sh, re_var_exclude); @@ -220,25 +199,49 @@ int main(int argc, char* argv[]) else if(options["list-cmd"]) list_cmds(sh, regex_null); // output - else if(options['o']) // file output - { - std::string destfile=options['o']; - // resolve - to stdout - if(destfile == "-") - destfile = "/dev/stdout"; - // output - std::ofstream(destfile) << sh->generate(g_shebang, 0); - // don't chmod on /dev/ - if(destfile.substr(0,5) != "/dev/") - ztd::exec("chmod", "+x", destfile); - } else if(options['J']) { std::cout << gen_json_struc(sh) << std::endl; } - else // to console + else { - std::cout << sh->generate(g_shebang, 0); + // modifiers + insert_lxsh_commands(sh); + if(options["debashify"]) + debashify(sh); + + // processing before output + // minify + if(options['m']) + opt_minify=true; + if(options["remove-unused"]) + delete_unused( sh, re_var_exclude, 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 ); + + if(options['o']) // file output + { + std::string destfile=options['o']; + // resolve - to stdout + if(destfile == "-") + destfile = "/dev/stdout"; + // output + std::ofstream(destfile) << sh->generate(g_shebang, 0); + // don't chmod on /dev/ + if(destfile.substr(0,5) != "/dev/") + ztd::exec("chmod", "+x", destfile); + } + else // to console + { + std::cout << sh->generate(g_shebang, 0); + } } } #ifndef NO_PARSE_CATCH diff --git a/src/processing.cpp b/src/processing.cpp index 0ceb372..c9bb979 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -5,6 +5,8 @@ #include "recursive.hpp" #include "parse.hpp" #include "util.hpp" +#include "shellcode.hpp" +#include "struc_helper.hpp" // Global regex @@ -399,6 +401,24 @@ bool r_delete_var(_obj* in, set_t* vars) return true; } +void insert_lxsh_commands(shmain* sh) +{ + cmdmap_get(sh, regex_null); + bool req_rescan=false; + for(auto it: lxsh_shellcode_fcts) + { + if(m_cmds.find(it.first) != m_cmds.end()) + { + sh->lst->insert(0, make_condlist(it.second.code)); + req_rescan=true; + } + } + if(req_rescan) + require_rescan_all(); +} + +/** JSON **/ + std::string quote_string(std::string const& in) { return '"' + stringReplace(in, "\"", "\\\"") + '"';