From: Josh Date: Mon, 25 Aug 2014 18:53:48 +0000 (-0400) Subject: updating Snort2Lua. Reflects updated Snort++ X-Git-Tag: 3.0.0-233~1419^2~6^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9394c1a99de648f73b1d9aed5dee05a0fe9bdde2;p=thirdparty%2Fsnort3.git updating Snort2Lua. Reflects updated Snort++ --- diff --git a/tools/snort2lua/config_states/CMakeLists.txt b/tools/snort2lua/config_states/CMakeLists.txt index 7657ee351..9c5a8b774 100644 --- a/tools/snort2lua/config_states/CMakeLists.txt +++ b/tools/snort2lua/config_states/CMakeLists.txt @@ -1,6 +1,7 @@ add_library( config_states + config_alertfile.cc config_binding.cc config_checksums.cc config_classification.cc diff --git a/tools/snort2lua/config_states/Makefile.am b/tools/snort2lua/config_states/Makefile.am index ea5794cf3..933defccb 100644 --- a/tools/snort2lua/config_states/Makefile.am +++ b/tools/snort2lua/config_states/Makefile.am @@ -4,6 +4,7 @@ AUTOMAKE_OPTIONS=foreign no-dependencies noinst_LIBRARIES = libconfig_states.a libconfig_states_a_SOURCES = \ +config_alertfile.cc \ config_binding.cc \ config_checksums.cc \ config_classification.cc \ diff --git a/tools/snort2lua/config_states/config_one_string_option.cc b/tools/snort2lua/config_states/config_one_string_option.cc index a69605ec2..4c797af4a 100644 --- a/tools/snort2lua/config_states/config_one_string_option.cc +++ b/tools/snort2lua/config_states/config_one_string_option.cc @@ -58,36 +58,31 @@ public: } // get length (stringstream will not read spaces...which we want) - const std::streamoff pos = stream.tellg(); - stream.seekg(0, stream.end); - const std::streamoff length = stream.tellg() - pos; - stream.seekg(pos); + std::string arg_s = util::get_remain_data(stream); - // read argument - char *arg_c = new char[length + 1]; - stream.read(arg_c, length); - arg_c[length] = '\0'; - std::string arg_s(arg_c); - delete[] arg_c; - util::trim(arg_s); + if (arg_s.empty()) + { + data_api.failed_conversion(stream, ""); + return false; + } - bool retval; table_api.open_table(*lua_table); if((lua_option != nullptr) && snort_option->compare(*lua_option)) { table_api.add_diff_option_comment("config " + *snort_option + ":", *lua_option); - retval = table_api.add_option(*lua_option, arg_s); + table_api.add_option(*lua_option, arg_s); } else { - retval = table_api.add_option(*snort_option, arg_s); + table_api.add_option(*snort_option, arg_s); } table_api.close_table(); - return retval; + stream.setstate(std::ios::eofbit); // done parsing this line + return true; } private: @@ -123,21 +118,6 @@ static const std::string react = "react"; static const std::string output = "output"; - -/************************************************* - ****************** alert_file ***************** - *************************************************/ - -static const std::string alertfile = "alertfile"; -static const std::string alert_file = "alert_file"; -static const ConvertMap alertfile_api = -{ - alertfile, - config_string_ctor<&alertfile, &alerts, &alert_file>, -}; - -const ConvertMap* alertfile_map = &alertfile_api; - /************************************************* ******************* bpf_file ****************** *************************************************/ diff --git a/tools/snort2lua/config_states/config_profile.cc b/tools/snort2lua/config_states/config_profile.cc index b613664d9..171d4aa32 100644 --- a/tools/snort2lua/config_states/config_profile.cc +++ b/tools/snort2lua/config_states/config_profile.cc @@ -62,6 +62,11 @@ bool Profilers::convert(std::istringstream& data_stream) if (!(arg_stream >> keyword)) tmpval = false; + else if (!keyword.compare("filename")) + { + table_api.add_deleted_comment("profile_*: filename ..."); + } + else if (!keyword.compare("print")) { table_api.add_diff_option_comment("print", "count"); @@ -99,29 +104,16 @@ bool Profilers::convert(std::istringstream& data_stream) tmpval = table_api.add_option("sort", val); } - else if (!keyword.compare("filename")) - { - table_api.open_table("file"); - tmpval = parse_string_option("name", arg_stream); - - std::string append; - if ((arg_stream >> append) && - (!append.compare("append"))) - { - if (!table_api.add_option("append", true)) - tmpval = false; - } - - table_api.close_table(); - } - else { tmpval = false; } - if (retval && !tmpval) + if (!tmpval) + { + data_api.failed_conversion(data_stream, keyword); retval = false; + } } table_api.close_table(); diff --git a/tools/snort2lua/data/data_types/dt_option.h b/tools/snort2lua/data/data_types/dt_option.h index d7c90cb45..994128397 100644 --- a/tools/snort2lua/data/data_types/dt_option.h +++ b/tools/snort2lua/data/data_types/dt_option.h @@ -34,7 +34,8 @@ public: Option(std::string name, std::string val, int depth); virtual ~Option(); - inline std::string get_name(){ return name; }; + inline std::string get_name() + { return name; }; // overloading operators friend std::ostream &operator<<( std::ostream&, const Option &); diff --git a/tools/snort2lua/data/data_types/dt_table.cc b/tools/snort2lua/data/data_types/dt_table.cc index c3db05e1a..3c3673df3 100644 --- a/tools/snort2lua/data/data_types/dt_table.cc +++ b/tools/snort2lua/data/data_types/dt_table.cc @@ -137,6 +137,16 @@ bool Table::add_list(std::string name, std::string next_elem) return var->add_value(next_elem); } +bool Table::has_option(const std::string opt_name) +{ + for (Option* o : options) + if (!opt_name.compare(o->get_name())) + return true; + + return false; +} + + bool Table::has_option(Option opt) { for (Option* o : options) diff --git a/tools/snort2lua/data/data_types/dt_table.h b/tools/snort2lua/data/data_types/dt_table.h index 162cfd291..ea41144a6 100644 --- a/tools/snort2lua/data/data_types/dt_table.h +++ b/tools/snort2lua/data/data_types/dt_table.h @@ -47,6 +47,7 @@ public: bool add_option(std::string, std::string val); bool add_list(std::string, std::string next_elem); void add_comment(std::string comment); + bool has_option(const std::string); friend std::ostream &operator<<( std::ostream&, const Table &); diff --git a/tools/snort2lua/data/dt_table_api.cc b/tools/snort2lua/data/dt_table_api.cc index 1efdb3090..43f0fd6da 100644 --- a/tools/snort2lua/data/dt_table_api.cc +++ b/tools/snort2lua/data/dt_table_api.cc @@ -201,6 +201,17 @@ bool TableApi::add_comment(std::string comment) return true; } +bool TableApi::option_exists(const std::string name) +{ + if (open_tables.size() == 0) + { + data_api.developer_error("Must open table before calling option_exists() !!"); + return false; + } + + return open_tables.top()->has_option(name); +} + bool TableApi::add_diff_option_comment(std::string orig_var, std::string new_var) { std::string error_string = "option change: '" + orig_var + "' --> '" diff --git a/tools/snort2lua/data/dt_table_api.h b/tools/snort2lua/data/dt_table_api.h index 9b86c0020..403bea5fe 100644 --- a/tools/snort2lua/data/dt_table_api.h +++ b/tools/snort2lua/data/dt_table_api.h @@ -65,6 +65,11 @@ void print_tables( std::ostream &out); bool empty() { return (tables.size() == 0); } + +/* + * Accessing and choosing specific tables. + */ + // open a table at the topmost layer. i.e., the table will not be nested inside any other table. void open_top_level_table(std::string name); // open a nested named table --> 'name = {...}') @@ -74,9 +79,15 @@ void open_table(); // close the nested table. go to previous table level void close_table(); -// ADDING DATA AND FIELDS TO CURRENT TABLE void swap_tables(std::vector& new_tables); + +/* + * Adding/accessing data to the specific table chosen above!! + * These methods will all throw a developer warning if called without + * selecting a table! + */ + // add an string, bool, or int option to the table. --> table = { name = var |'var'}; bool add_option(const std::string name, const std::string val); bool add_option(const std::string name, const int val); @@ -95,6 +106,9 @@ bool add_deleted_comment(std::string dep_var); bool add_unsupported_comment(std::string unsupported_var); +// return true if this name exists as an option name for the selected table +bool option_exists(const std::string name); + private: std::vector tables; diff --git a/tools/snort2lua/utils/s2l_util.cc b/tools/snort2lua/utils/s2l_util.cc index 648d6ca45..4df0301f1 100644 --- a/tools/snort2lua/utils/s2l_util.cc +++ b/tools/snort2lua/utils/s2l_util.cc @@ -183,6 +183,26 @@ bool get_string(std::istringstream& stream, } } + +std::string get_remain_data(std::istringstream& stream) +{ + // get string length + const std::streamoff pos = stream.tellg(); + stream.seekg(0, stream.end); + const std::streamoff length = stream.tellg() - pos; + stream.seekg(pos); + + // read argument + char *arg_c = new char[length + 1]; + stream.read(arg_c, length); + arg_c[length] = '\0'; + std::string arg_s(arg_c); + delete[] arg_c; + util::trim(arg_s); + return arg_s; +} + + std::string get_rule_option_args(std::istringstream& stream) { std::string args = std::string(); diff --git a/tools/snort2lua/utils/s2l_util.h b/tools/snort2lua/utils/s2l_util.h index 90eb5ddc6..0b59c187a 100644 --- a/tools/snort2lua/utils/s2l_util.h +++ b/tools/snort2lua/utils/s2l_util.h @@ -57,7 +57,8 @@ std::string &trim(std::string &s); bool file_exists(const std::string& name); -/* Takes in a stream and a string of delimeters. The function will extract the charachters +/* + * Takes in a stream and a string of delimeters. The function will extract the charachters * from the stream until it hits one of the delimeters. The substring will be set to the * third parameter. The stream itself will point to the chrachter after the first delim. * @@ -75,6 +76,14 @@ bool get_string(std::istringstream& data_stream, std::string& option, const std::string delimeters); +/* + * Returns the rest of the data_streams data as one argument. + * Usefule when parsing filenames with spaces or other + * characters which can get removed by c++ libraries + * + * NO SIDE EFFECTS + */ +std::string get_remain_data(std::istringstream& data_stream); std::string get_rule_option_args(std::istringstream& data_stream);