Rule::Rule() : num_hdr_data(0),
is_bad_rule(false),
- is_comment(false)
+ is_comment(false), old_http_rule(false)
{
}
void Rule::make_comment()
{ is_comment = true; }
+void Rule::set_old_http_rule()
+{ old_http_rule = true; }
+
void Rule::add_option(const std::string& keyword)
{
RuleOption* r = new RuleOption(keyword);
options.push_back(r);
}
+std::string Rule::get_option(const std::string& keyword)
+{
+ for (auto option : options)
+ {
+ if (option->get_name() == keyword)
+ return option->get_value();
+ }
+ return std::string();
+}
+
+void Rule::update_option(const std::string& keyword, std::string& val)
+{
+ for (auto option : options)
+ {
+ if (option->get_name() == keyword)
+ {
+ option->update_value(val);
+ break;
+ }
+ }
+}
+
void Rule::add_suboption(const std::string& keyword)
{ options.back()->add_suboption(keyword); }
bool add_hdr_data(const std::string& data);
void add_option(const std::string& keyword);
void add_option(const std::string& keyword, const std::string& data);
+ std::string get_option(const std::string& keyword);
+ void update_option(const std::string& keyword, std::string& val);
void add_suboption(const std::string& keyword);
void add_suboption(const std::string& keyword, const std::string& val);
void set_curr_options_buffer(const std::string& buffer, bool add_option);
void add_comment(const std::string& comment);
void bad_rule();
void make_comment();
+ void set_old_http_rule();
+ bool is_old_http_rule() { return old_http_rule; }
friend std::ostream& operator<<(std::ostream&, const Rule&);
std::size_t num_hdr_data;
bool is_bad_rule;
bool is_comment;
+ bool old_http_rule;
};
#endif
virtual ~RuleOption();
inline const std::string& get_name() { return name; }
+ inline const std::string& get_value() { return value; }
+ inline void update_value(std::string& new_value) { value = new_value;}
bool add_suboption(const std::string& name);
bool add_suboption(const std::string& name, const std::string& val);
curr_rule->add_option(opt_name, val);
}
+std::string RuleApi::get_option(const std::string& keyword)
+{
+ if (!curr_rule)
+ return std::string();
+
+ return curr_rule->get_option(keyword);
+}
+
+void RuleApi::update_option(const std::string& keyword, std::string& val)
+{
+ if (!curr_rule)
+ return;
+
+ curr_rule->update_option(keyword, val);
+}
+
void RuleApi::add_suboption(const std::string& keyword)
{
if (curr_rule)
curr_rule->add_comment(comment);
}
+void RuleApi::old_http_rule()
+{
+ if (!curr_rule)
+ begin_rule();
+
+ curr_rule->set_old_http_rule();
+}
+
+bool RuleApi::is_old_http_rule()
+{
+ if (!curr_rule)
+ return false;
+
+ return curr_rule->is_old_http_rule();
+}
+
std::ostream& operator<<(std::ostream& out, const RuleApi& data)
{
if (DataApi::is_default_mode())
void update_rule_action(const std::string& new_type);
void add_option(const std::string& keyword);
void add_option(const std::string& keyword, const std::string& val);
+ std::string get_option(const std::string& keyword);
+ void update_option(const std::string& keyword, std::string& val);
void add_suboption(const std::string& keyword);
void add_suboption(const std::string& keyword, const std::string& val);
void set_curr_options_buffer(const std::string& buffer, bool add_option=false);
void add_comment(const std::string& comment);
void make_rule_a_comment();
void bad_rule(std::istringstream& stream, const std::string& bad_option);
+ void old_http_rule();
+ bool is_old_http_rule();
private:
static std::size_t error_count;
rule_dnp3_obj.cc
rule_dsize.cc
rule_file_data.cc
+ rule_gid_sid.cc
rule_http_encode.cc
rule_isdataat.cc
rule_metadata.cc
--- /dev/null
+//--------------------------------------------------------------------------
+// Copyright (C) 2018-2018 Cisco and/or its affiliates. All rights reserved.
+//
+// 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_gid_sid.cc author Maya Dagon <mdagon@cisco.com>
+
+//
+// Handle special case of deprecated gid 120:
+// Rules were moved to gid 119, with sids starting from 35.
+//
+// In case the rule is using gid 120 - convert it to gid 119 and update
+// sid.
+// Handle 2 cases: sid was read before/after gid.
+
+#include <sstream>
+
+#include "conversion_state.h"
+#include "helpers/converter.h"
+#include "rule_states/rule_api.h"
+#include "helpers/s2l_util.h"
+
+namespace rules
+{
+namespace
+{
+class Gid : public ConversionState
+{
+public:
+ Gid(Converter& c) : ConversionState(c) { }
+ bool convert(std::istringstream& data_stream) override;
+};
+
+class Sid : public ConversionState
+{
+public:
+ Sid(Converter& c) : ConversionState(c) { }
+ bool convert(std::istringstream& data_stream) override;
+ static void convert_sid(std::string& sid, std::istringstream& data, RuleApi& rule_api);
+};
+} // namespace
+
+//
+// Gid
+//
+
+bool Gid::convert(std::istringstream& data_stream)
+{
+ std::string gid = util::get_rule_option_args(data_stream);
+
+ const std::string old_http_gid("120");
+ if (gid.compare(old_http_gid) == 0)
+ {
+ const std::string nhi_gid("119");
+ gid.assign(nhi_gid);
+ rule_api.old_http_rule();
+
+ // Update sid
+ std::string sid = rule_api.get_option("sid");
+ if (!sid.empty())
+ {
+ Sid::convert_sid(sid, data_stream, rule_api);
+ rule_api.update_option("sid", sid);
+ }
+ }
+ rule_api.add_option("gid", gid);
+ return set_next_rule_state(data_stream);
+}
+
+//
+// Sid
+//
+
+void Sid::convert_sid(std::string& sid, std::istringstream& data_stream, RuleApi& rule_api)
+{
+ int sid_num;
+ try
+ {
+ sid_num = std::stoi(sid);
+ }
+ catch (...)
+ {
+ rule_api.bad_rule(data_stream, "sid - invalid input, expecting int type");
+ return;
+ }
+ const int sid_offset = 34;
+ sid.assign(std::to_string(sid_num + sid_offset));
+}
+
+bool Sid::convert(std::istringstream& data_stream)
+{
+ std::string sid = util::get_rule_option_args(data_stream);
+
+ if (rule_api.is_old_http_rule())
+ convert_sid(sid, data_stream, rule_api);
+
+ rule_api.add_option("sid", sid);
+ return set_next_rule_state(data_stream);
+}
+
+/**************************
+ ******* A P I ***********
+ **************************/
+
+static ConversionState* ctor(Converter& c)
+{ return new Gid(c); }
+
+static const ConvertMap rule_gid =
+{
+ "gid",
+ ctor,
+};
+
+const ConvertMap* gid_map = &rule_gid;
+
+static ConversionState* sid_ctor(Converter& c)
+{ return new Sid(c); }
+
+static const ConvertMap rule_sid =
+{
+ "sid",
+ sid_ctor,
+};
+
+const ConvertMap* sid_map = &rule_sid;
+} // namespace rules
+
const ConvertMap* msg_map = &rule_msg;
-/************************************
- ********** G I D ******************
- ************************************/
-
-static const std::string gid = "gid";
-static const ConvertMap rule_gid =
-{
- gid,
- unchanged_rule_ctor<& gid>,
-};
-
-const ConvertMap* gid_map = &rule_gid;
-
-/************************************
- ********** S I D *****************
- ************************************/
-
-static const std::string sid = "sid";
-static const ConvertMap rule_sid =
-{
- sid,
- unchanged_rule_ctor<& sid>,
-};
-
-const ConvertMap* sid_map = &rule_sid;
-
/************************************
********** R E V *****************
************************************/