From: Josh Date: Wed, 16 Jul 2014 21:18:55 +0000 (-0400) Subject: Merge branch 'master' of /nfs/home/rucombs/Snort++ into snort2lua X-Git-Tag: 3.0.0-233~1175^2~20^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ee8cbec70ecf45d17fa58c0bc1d9279f440403ce;p=thirdparty%2Fsnort3.git Merge branch 'master' of /nfs/home/rucombs/Snort++ into snort2lua Conflicts: lua/snort.lua --- ee8cbec70ecf45d17fa58c0bc1d9279f440403ce diff --cc tools/snort2lua/conversion_state.h index 08fd43a9a,7a78f29bc..e3d7baa1a --- a/tools/snort2lua/conversion_state.h +++ b/tools/snort2lua/conversion_state.h @@@ -258,22 -245,23 +258,18 @@@ protected inline bool set_next_rule_state(std::istringstream& stream) { std::string keyword; - int pos = stream.tellg(); - std::getline(stream, keyword, ':'); - util::trim(keyword); - - if (keyword.empty()) - return true; - // std::getline(stream, keyword, ':'); - // if (keyword.find_first_not_of(' ') == std::string::npos) - // return true; - - do + while(std::getline(stream, keyword, ':')) { - util::trim(keyword); - int semi_colon_pos = keyword.find(';'); + std::size_t semi_colon_pos = keyword.find(';'); if (semi_colon_pos != std::string::npos) { - // found a nested option without a colon - // 2 == last charachter of option + semi_colon - stream.seekg(pos + semi_colon_pos +2); + // found an option without a colon, so set stream + // to semi-colon + std::streamoff off = 1 + (std::streamoff)(pos) + + (std::streamoff)(semi_colon_pos); + stream.seekg(off); keyword = keyword.substr(0, semi_colon_pos); } diff --cc tools/snort2lua/data/dt_comment.cc index 1d58edb38,9d567d4b7..64956069c --- a/tools/snort2lua/data/dt_comment.cc +++ b/tools/snort2lua/data/dt_comment.cc @@@ -127,12 -124,9 +127,12 @@@ std::ostream &operator<<( std::ostream if (str.size() == 0) out << "\n"; + else if (c.type == Comments::CommentType::MULTI_LINE) - util::sanitize_multi_line_string(util::ltrim(str)); ++ util::sanitize_lua_string(util::ltrim(str)); + while(!str.empty()) { - int substr_len = max_line_length; + std::size_t substr_len = max_line_length; // determine the first space before 80 charachters // if there are no spaces, print the entire string diff --cc tools/snort2lua/data/dt_rule.cc index 2100f0bde,932439be7..72b113890 --- a/tools/snort2lua/data/dt_rule.cc +++ b/tools/snort2lua/data/dt_rule.cc @@@ -21,7 -21,6 +21,8 @@@ #include "data/dt_rule.h" +#include "data/dt_data.h" // included for print mode ++#include "util/util.h" Rule::Rule() : num_hdr_data(0), @@@ -130,25 -125,23 +131,26 @@@ std::ostream &operator<<( std::ostream else out << " "; -- out << rule.hdr_data[i]; ++ std::string tmp = rule.hdr_data[i]; ++ out << util::sanitize_lua_string(tmp); } - out << " ("; - first_line = true; - - for (auto* r : rule.options) + if (!rule.options.empty()) { - if (first_line) - first_line = false; - else - out << ";"; - out << " " << (*r); - } + out << " ("; + first_line = true; - out << " )"; + for (auto* r : rule.options) + { + if (first_line) + first_line = false; + else + out << ";"; + out << " " << (*r); + } + out << " )"; + } return out; } diff --cc tools/snort2lua/data/dt_var.cc index bd3940a53,ca5f027fd..660c34af8 --- a/tools/snort2lua/data/dt_var.cc +++ b/tools/snort2lua/data/dt_var.cc @@@ -139,7 -137,7 +139,7 @@@ std::ostream& operator<<( std::ostream print_newline(out, count, whitespace); -- util::sanitize_multi_line_string(v->data); ++ util::sanitize_lua_string(v->data); out << "[[ "; count += 3; diff --cc tools/snort2lua/rule_states/CMakeLists.txt index 29658b0db,0cd5e0783..f0c97c323 --- a/tools/snort2lua/rule_states/CMakeLists.txt +++ b/tools/snort2lua/rule_states/CMakeLists.txt @@@ -7,6 -6,6 +7,7 @@@ add_library( rule_state rule_metadata.cc rule_pcre.cc rule_unchanged.cc ++ rule_urilen.cc rule_api.cc rule_api.h ) diff --cc tools/snort2lua/rule_states/rule_unchanged.cc index 0f17e6151,51361891b..d783e9535 --- a/tools/snort2lua/rule_states/rule_unchanged.cc +++ b/tools/snort2lua/rule_states/rule_unchanged.cc @@@ -548,19 -548,33 +548,6 @@@ static const ConvertMap rule_threshold const ConvertMap* threshold_map = &rule_threshold; --/************************************ -- ************* URILEN ************* -- ************************************/ -- --static const std::string urilen = "urilen"; --static const ConvertMap rule_urilen = --{ -- urilen, -- unchanged_rule_ctor<&urilen>, --}; -- --const ConvertMap* urilen_map = &rule_urilen; - - -/************************************ - *********** FILE_DATA ************ - ************************************/ - -static const std::string file_data = "file_data"; -static const ConvertMap rule_file_data = -{ - file_data, - unchanged_rule_ctor<&file_data, false>, -}; - -const ConvertMap* file_data_map = &rule_file_data; -- /************************************ *********** BYTE_TEST ************ ************************************/ diff --cc tools/snort2lua/rule_states/rule_urilen.cc index 000000000,000000000..79713f8c5 new file mode 100644 --- /dev/null +++ b/tools/snort2lua/rule_states/rule_urilen.cc @@@ -1,0 -1,0 +1,107 @@@ ++/* ++** Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved. ++ * Copyright (C) 2002-2013 Sourcefire, Inc. ++ * ++ * This program is free software; you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License Version 2 as ++ * published by the Free Software Foundation. You may not use, modify or ++ * distribute this program under any other version of the GNU General ++ * Public License. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with this program; if not, write to the Free Software ++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ++ */ ++// rule_urilen.cc author Josh Rosenbaum ++ ++#include ++#include ++ ++#include "conversion_state.h" ++#include "util/converter.h" ++#include "rule_states/rule_api.h" ++#include "util/util.h" ++ ++namespace rules ++{ ++ ++namespace { ++ ++ ++class Urilen : public ConversionState ++{ ++public: ++ Urilen(Converter* cv, LuaData* ld) : ConversionState(cv, ld) {}; ++ virtual ~Urilen() {}; ++ virtual bool convert(std::istringstream& data); ++}; ++ ++} // namespace ++ ++bool Urilen::convert(std::istringstream& data_stream) ++{ ++ std::string args; ++ std::string value; ++ bool retval = true; ++ ++ args = util::get_rule_option_args(data_stream); ++ std::istringstream arg_stream(args); ++ ++ // if there are no arguments, the option had a colon before a semicolon. ++ // we are therefore done with this rule. ++ if (util::get_string(arg_stream, value, ",")) ++ { ++ retval = ld->add_rule_option("urilen", value); ++ ld->select_option("urilen"); ++ ++ if (util::get_string(arg_stream, value, ",")) ++ { ++ bool tmpval = true; ++ ++ if (!value.compare("raw")) ++ tmpval = ld->add_rule_option_before_selected("http_raw_uri"); ++ ++ else if (!value.compare("norm")) ++ tmpval = ld->add_rule_option_before_selected("http_uri"); ++ ++ else ++ ld->bad_rule("invalid arguments: " + args, data_stream); ++ ++ if (retval && !tmpval) ++ retval = false; ++ } ++ } ++ else ++ { ++ ld->bad_rule("urilen: option required", data_stream); ++ } ++ ++ ld->unselect_option(); ++ return set_next_rule_state(data_stream) && retval; ++} ++ ++/************************** ++ ******* A P I *********** ++ **************************/ ++ ++ ++static ConversionState* ctor(Converter* cv, LuaData* ld) ++{ ++ return new Urilen(cv, ld); ++} ++ ++static const std::string urilen = "urilen"; ++static const ConvertMap rule_urilen = ++{ ++ "urilen", ++ ctor, ++}; ++ ++const ConvertMap* urilen_map = &rule_urilen; ++ ++} // namespace rules diff --cc tools/snort2lua/util/util.cc index 1f344c4cb,101a96033..0d89823a6 --- a/tools/snort2lua/util/util.cc +++ b/tools/snort2lua/util/util.cc @@@ -57,10 -56,10 +57,10 @@@ const ConvertMap* find_map(const std::v return nullptr; } --std::string &sanitize_multi_line_string(std::string &s) ++std::string &sanitize_lua_string(std::string &s) { - int found = s.find("]]"); + std::size_t found = s.find("]]"); while (found != std::string::npos) { s.insert(found + 1, " "); diff --cc tools/snort2lua/util/util.h index ff449a1fa,12b9647ad..71f40e9ab --- a/tools/snort2lua/util/util.h +++ b/tools/snort2lua/util/util.h @@@ -64,32 -64,9 +64,32 @@@ inline std::string &trim(std::string &s return ltrim(rtrim(s)); } + +// return true if this file exists. False otherwise. +bool file_exists (const std::string& name); + +/* 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. + * + * PARAMS: + * data_stream - the data stream from which to find a substring. + * delimeters - The string of delimeters. + * options - The found substring will be place in this parameter. If the + * stream is empty or no charachters have been extracted, then + * this parameter wil be set to an empty string. + * RETURNS: + * True - when the string is found. + * False - whenma substing was unable to be extracted. + */ +bool get_string(std::istringstream& data_stream, std::string& option, std::string delimeters); + + std::string get_rule_option_args(std::istringstream& data_stream); -// remove any ']]' from this string. -std::string &sanitize_multi_line_string(std::string &s); + +// remove any ']]' and double spaces from this string. - std::string &sanitize_multi_line_string(std::string &s); ++std::string &sanitize_lua_string(std::string &s); + // find the location of the first space before max_str_lenght. // if no space exists before max_str_length, return the first space // after max_length. Otherwise, return std::string::npos