static int get_resp(int *resp, int count) { char buf[STD_RESP_LEN + 1], *tmp; char **argv; int len, cmd, i; len = read(serverfd, buf, STD_RESP_LEN); if (len > 0) { buf[len] = '\0'; tmp = get_command(buf, &cmd); switch (cmd) { case DEAD: printf_die(stdout, "Killed: %s\n", 1, *tmp ? tmp : "unknown reason"); break; case OK: break; case ERROR: printf_die(stderr, "Error: %s\n", 2, *tmp ? tmp : "unknown error"); break; default: printf_die(stderr, "Error: unexpected response from the server\n", 2); break; } argv = malloc(count * sizeof(char *)); count = tokenize_args(tmp, count, argv); for (i = 0; i < count; i++) { if (str_isnumber(argv[i])) resp[i] = atoi(argv[i]); else resp[i] = -1; } free(argv); } else count = 0; return count; }
int main(int argc, char** argv) { po::variables_map vm; std::string extraction_directory; std::vector<std::string> selected_plugins, selected_categories; // Load the dynamic plugins. bfs::path working_dir(argv[0]); working_dir = working_dir.parent_path(); plugin::PluginManager::get_instance().load_all(working_dir.string()); // Load the configuration config conf = parse_config((working_dir / "manalyze.conf").string()); if (!parse_args(vm, argc, argv)) { return -1; } // Get all the paths now and make them absolute before changing the working directory std::set<std::string> targets = get_input_files(vm); if (vm.count("extract")) { extraction_directory = bfs::absolute(vm["extract"].as<std::string>()).string(); } // Break complex arguments into a list once and for all. if (vm.count("plugins")) { selected_plugins = tokenize_args(vm["plugins"].as<std::vector<std::string> >()); } if (vm.count("dump")) { selected_categories = tokenize_args(vm["dump"].as<std::vector<std::string> >()); } // Instantiate the requested OutputFormatter boost::shared_ptr<io::OutputFormatter> formatter; if (vm.count("output") && vm["output"].as<std::string>() == "json") { formatter.reset(new io::JsonFormatter()); } else // Default: use the human-readable output. { formatter.reset(new io::RawFormatter()); formatter->set_header("* Manalyze " MANALYZE_VERSION " *"); } // Set the working directory to Manalyze's folder. chdir(working_dir.string().c_str()); // Do the actual analysis on all the input files unsigned int count = 0; for (auto it = targets.begin() ; it != targets.end() ; ++it) { perform_analysis(*it, vm, extraction_directory, selected_categories, selected_plugins, conf, formatter); if (++count % 1000 == 0) { formatter->format(std::cout, false); // Flush the formatter from time to time, to avoid eating up all the RAM when analyzing gigs of files. } } formatter->format(std::cout); if (vm.count("plugins")) { // Explicitly unload the plugins plugin::PluginManager::get_instance().unload_all(); } return 0; }
/** * @brief Checks whether the given arguments are valid. * * This consists in verifying that: * - All the requested categories for the "dump" command exist * - All the requested plugins exist * - All the input files exist * - The requested output formatter exists * * If an error is detected, the help message is displayed. * * @param po::variables_map& vm The parsed arguments. * @param po::options_description& desc The description of the arguments (only so it can be * passed to print_help if needed). * @param char** argv The raw arguments (only so it can be passed to print_help if needed). * * @return True if the arguments are all valid, false otherwise. */ bool validate_args(po::variables_map& vm, po::options_description& desc, char** argv) { // Verify that the requested categories exist if (vm.count("dump")) { std::vector<std::string> selected_categories = tokenize_args(vm["dump"].as<std::vector<std::string> >()); const std::vector<std::string> categories = boost::assign::list_of("all")("summary")("dos")("pe")("opt")("sections") ("imports")("exports")("resources")("version")("debug")("tls")("config"); for (auto it = selected_categories.begin() ; it != selected_categories.end() ; ++it) { std::vector<std::string>::const_iterator found = std::find(categories.begin(), categories.end(), *it); if (found == categories.end()) { print_help(desc, argv[0]); std::cout << std::endl; PRINT_ERROR << "category " << *it << " does not exist!" << std::endl; return false; } } } // Verify that the requested plugins exist if (vm.count("plugins")) { std::vector<std::string> selected_plugins = tokenize_args(vm["plugins"].as<std::vector<std::string> >()); std::vector<plugin::pIPlugin> plugins = plugin::PluginManager::get_instance().get_plugins(); for (auto it = selected_plugins.begin() ; it != selected_plugins.end() ; ++it) { if (*it == "all") { continue; } auto found = std::find_if(plugins.begin(), plugins.end(), boost::bind(&plugin::name_matches, *it, _1)); if (found == plugins.end()) { print_help(desc, argv[0]); std::cout << std::endl; PRINT_ERROR << "plugin " << *it << " does not exist!" << std::endl; return false; } } } // Verify that all the input files exist. std::vector<std::string> input_files = vm["pe"].as<std::vector<std::string> >(); for (auto it = input_files.begin() ; it != input_files.end() ; ++it) { if (!bfs::exists(*it)) { PRINT_ERROR << *it << " not found!" << std::endl; return false; } } // Verify that the requested output formatter exists if (vm.count("output")) { auto formatters = boost::assign::list_of("raw")("json"); auto found = std::find(formatters.begin(), formatters.end(), vm["output"].as<std::string>()); if (found == formatters.end()) { print_help(desc, argv[0]); std::cout << std::endl; PRINT_ERROR << "output formatter " << vm["output"].as<std::string>() << " does not exist!" << std::endl; return false; } } return true; }