]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3319: control, shell: add a command to set the network policy to be...
authorRon Dempster (rdempste) <rdempste@cisco.com>
Thu, 24 Mar 2022 17:58:44 +0000 (17:58 +0000)
committerRon Dempster (rdempste) <rdempste@cisco.com>
Thu, 24 Mar 2022 17:58:44 +0000 (17:58 +0000)
Merge in SNORT/snort3 from ~RDEMPSTE/snort3:command to master

Squashed commit of the following:

commit 3c3f144b75ada597b83130c7ce1613934d77b0ff
Author: Ron Dempster (rdempste) <rdempste@cisco.com>
Date:   Mon Mar 14 08:18:08 2022 -0400

    control, shell: add a command to set the network policy to be used by subsequent commands

src/control/control.cc
src/control/control.h
src/main/CMakeLists.txt
src/main/ac_shell_cmd.cc
src/main/modules.cc
src/main/network_module.cc [new file with mode: 0644]
src/main/network_module.h [new file with mode: 0644]
src/main/policy.h
src/main/shell.cc
src/main/shell.h
src/managers/inspector_manager.cc

index ff4964a911af9016d8c367aeb504b54dab61fffe..cfe7cfe41a591ead873df1b0f3f01d16710c73d7 100644 (file)
@@ -120,6 +120,9 @@ int ControlConn::read_commands()
     return commands_found;
 }
 
+void ControlConn::set_user_network_policy()
+{ shell->set_user_network_policy(); }
+
 int ControlConn::execute_commands()
 {
     int executed = 0;
index a211b46a5c463b7ca0a816372307813763f240c3..993986ac93d87110f84d58a7535fe1fdc7e10d7b 100644 (file)
@@ -60,6 +60,8 @@ public:
     int execute_commands();
     void shutdown();
 
+    void set_user_network_policy();
+
     SO_PUBLIC bool is_local() const { return local; }
     SO_PUBLIC bool respond(const char* format, va_list& ap);
     SO_PUBLIC bool respond(const char* format, ...) __attribute__((format (printf, 2, 3)));
index ebcd871db3a486005bf92528f39981c9bdfb0fd4..da6580c96c861ea2c5d3cd2dd37d865948cc32e9 100644 (file)
@@ -32,6 +32,8 @@ add_library (main OBJECT
     help.h
     modules.cc
     modules.h
+    network_module.cc
+    network_module.h
     oops_handler.cc
     oops_handler.h
     policy.cc
index 89b2ce1115af75014f22690516314220558ed178..ce0ca4851ec36c768ec9e151b2be68bc4e989576 100644 (file)
@@ -37,6 +37,7 @@ ACShellCmd::ACShellCmd(ControlConn* conn, AnalyzerCommand* ac) : AnalyzerCommand
 
 bool ACShellCmd::execute(Analyzer& analyzer, void** state)
 {
+    ctrlcon->set_user_network_policy();
     return ac->execute(analyzer, state);
 }
 
index 3fb387d60615535299b575a3754824adfb42f47c..a2d1347869ec61db7586ad4a01be30079eaf620f 100644 (file)
@@ -65,6 +65,7 @@
 #include "target_based/snort_protocols.h"
 #include "trace/trace_module.h"
 
+#include "network_module.h"
 #include "snort_config.h"
 #include "snort_module.h"
 #include "thread_config.h"
@@ -1007,88 +1008,6 @@ bool AttributeTableModule::set(const char*, Value& v, SnortConfig* sc)
     return true;
 }
 
-//-------------------------------------------------------------------------
-// network module
-//-------------------------------------------------------------------------
-
-static const Parameter network_params[] =
-{
-    { "checksum_drop", Parameter::PT_MULTI,
-      "all | ip | noip | tcp | notcp | udp | noudp | icmp | noicmp | none", "none",
-      "drop if checksum is bad" },
-
-    { "checksum_eval", Parameter::PT_MULTI,
-      "all | ip | noip | tcp | notcp | udp | noudp | icmp | noicmp | none", "all",
-      "checksums to verify" },
-
-    { "id", Parameter::PT_INT, "0:65535", "0",
-      "correlate unified2 events with configuration" },
-
-    { "min_ttl", Parameter::PT_INT, "1:255", "1",
-      "alert / normalize packets with lower TTL / hop limit "
-      "(you must enable rules and / or normalization also)" },
-
-    { "new_ttl", Parameter::PT_INT, "1:255", "1",
-      "use this value for responses and when normalizing" },
-
-    { "layers", Parameter::PT_INT, "3:255", "40",
-      "the maximum number of protocols that Snort can correctly decode" },
-
-    { "max_ip6_extensions", Parameter::PT_INT, "0:255", "0",
-      "the maximum number of IP6 options Snort will process for a given IPv6 layer "
-      "before raising 116:456 (0 = unlimited)" },
-
-    { "max_ip_layers", Parameter::PT_INT, "0:255", "0",
-      "the maximum number of IP layers Snort will process for a given packet "
-      "before raising 116:293 (0 = unlimited)" },
-
-    { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
-};
-
-#define network_help \
-    "configure basic network parameters"
-
-class NetworkModule : public Module
-{
-public:
-    NetworkModule() : Module("network", network_help, network_params) { }
-    bool set(const char*, Value&, SnortConfig*) override;
-
-    Usage get_usage() const override
-    { return CONTEXT; }
-};
-
-bool NetworkModule::set(const char*, Value& v, SnortConfig* sc)
-{
-    NetworkPolicy* p = get_network_policy();
-
-    if ( v.is("checksum_drop") )
-        ConfigChecksumDrop(v.get_string());
-
-    else if ( v.is("checksum_eval") )
-        ConfigChecksumMode(v.get_string());
-
-    else if ( v.is("id") )
-        p->user_policy_id = v.get_uint16();
-
-    else if ( v.is("min_ttl") )
-        p->min_ttl = v.get_uint8();
-
-    else if ( v.is("new_ttl") )
-        p->new_ttl = v.get_uint8();
-
-    else if (v.is("layers"))
-        sc->num_layers = v.get_uint8();
-
-    else if (v.is("max_ip6_extensions"))
-        sc->max_ip6_extensions = v.get_uint8();
-
-    else if (v.is("max_ip_layers"))
-        sc->max_ip_layers = v.get_uint8();
-
-    return true;
-}
-
 //-------------------------------------------------------------------------
 // inspection policy module
 //-------------------------------------------------------------------------
diff --git a/src/main/network_module.cc b/src/main/network_module.cc
new file mode 100644 (file)
index 0000000..b8d53a2
--- /dev/null
@@ -0,0 +1,128 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2014-2022 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.
+//--------------------------------------------------------------------------
+
+// network_module.cc author Ron Dempster <rdempste@cisco.com>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "network_module.h"
+
+#include <lua.hpp>
+
+#include "main/policy.h"
+#include "main/shell.h"
+#include "main/snort_config.h"
+#include "parser/config_file.h"
+
+using namespace snort;
+
+static const Parameter network_params[] =
+{
+    { "checksum_drop", Parameter::PT_MULTI,
+      "all | ip | noip | tcp | notcp | udp | noudp | icmp | noicmp | none", "none",
+      "drop if checksum is bad" },
+
+    { "checksum_eval", Parameter::PT_MULTI,
+      "all | ip | noip | tcp | notcp | udp | noudp | icmp | noicmp | none", "all",
+      "checksums to verify" },
+
+    { "id", Parameter::PT_INT, "0:65535", "0",
+      "correlate unified2 events with configuration" },
+
+    { "min_ttl", Parameter::PT_INT, "1:255", "1",
+      "alert / normalize packets with lower TTL / hop limit "
+      "(you must enable rules and / or normalization also)" },
+
+    { "new_ttl", Parameter::PT_INT, "1:255", "1",
+      "use this value for responses and when normalizing" },
+
+    { "layers", Parameter::PT_INT, "3:255", "40",
+      "the maximum number of protocols that Snort can correctly decode" },
+
+    { "max_ip6_extensions", Parameter::PT_INT, "0:255", "0",
+      "the maximum number of IP6 options Snort will process for a given IPv6 layer "
+      "before raising 116:456 (0 = unlimited)" },
+
+    { "max_ip_layers", Parameter::PT_INT, "0:255", "0",
+      "the maximum number of IP layers Snort will process for a given packet "
+      "before raising 116:293 (0 = unlimited)" },
+
+    { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
+};
+
+#define network_help  "configure basic network parameters"
+
+static int network_set_policy(lua_State* L)
+{
+    int user_id = luaL_optint(L, 1, 0);
+    Shell::set_network_policy_user_id(L, user_id);
+    return 0;
+}
+
+const Parameter network_set_policy_params[] =
+{
+    {"id", Parameter::PT_INT, "0:65535", 0, "user network policy id"},
+    {nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr}
+};
+
+const Command network_cmds[] =
+{
+    {"set_policy", network_set_policy, network_set_policy_params,
+        "set the network policy for commands given the user policy id"},
+    {nullptr, nullptr, nullptr, nullptr}
+};
+
+NetworkModule::NetworkModule() : snort::Module("network", network_help, network_params)
+{ }
+
+const Command* NetworkModule::get_commands() const
+{ return network_cmds; }
+
+bool NetworkModule::set(const char*, Value& v, SnortConfig* sc)
+{
+    NetworkPolicy* p = get_network_policy();
+
+    if ( v.is("checksum_drop") )
+        ConfigChecksumDrop(v.get_string());
+
+    else if ( v.is("checksum_eval") )
+        ConfigChecksumMode(v.get_string());
+
+    else if ( v.is("id") )
+        p->user_policy_id = v.get_uint16();
+
+    else if ( v.is("min_ttl") )
+        p->min_ttl = v.get_uint8();
+
+    else if ( v.is("new_ttl") )
+        p->new_ttl = v.get_uint8();
+
+    else if (v.is("layers"))
+        sc->num_layers = v.get_uint8();
+
+    else if (v.is("max_ip6_extensions"))
+        sc->max_ip6_extensions = v.get_uint8();
+
+    else if (v.is("max_ip_layers"))
+        sc->max_ip_layers = v.get_uint8();
+
+    return true;
+}
+
diff --git a/src/main/network_module.h b/src/main/network_module.h
new file mode 100644 (file)
index 0000000..403e568
--- /dev/null
@@ -0,0 +1,40 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2014-2022 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.
+//--------------------------------------------------------------------------
+
+// network_module.cc author Ron Dempster <rdempste@cisco.com>
+
+#ifndef NETWORK_MODULE_H
+#define NETWORK_MODULE_H
+
+#include "framework/module.h"
+
+
+class NetworkModule : public snort::Module
+{
+public:
+    NetworkModule();
+    ~NetworkModule() override = default;
+    bool set(const char*, snort::Value&, snort::SnortConfig*) override;
+
+    const snort::Command* get_commands() const override;
+
+    Usage get_usage() const override
+    { return CONTEXT; }
+};
+
+#endif
index 5956e2ff7d8d3614e334a8ceedc9512d78f0404e..c38a0f049abbc674469746f81ab7d9ec943f38dc 100644 (file)
@@ -55,6 +55,8 @@ struct PortTable;
 struct vartable_t;
 struct sfip_var_t;
 
+#define UNDEFINED_USER_POLICY_ID 65536
+
 typedef unsigned int PolicyId;
 typedef snort::GHash PortVarTable;
 
index 4807f5b85a0c990606e6d1acd4308c1aefbeeadd..5d56c5625b3c66b4404012318a2aabf63971fca1 100644 (file)
@@ -91,6 +91,8 @@ BaseConfigNode* Shell::s_current_node = nullptr;
 bool Shell::s_close_table = true;
 string Shell::lua_sandbox;
 
+const char* const Shell::lua_shell_id = "the_shell";
+
 // FIXIT-M Shell::panic() works on Linux but on OSX we can't throw from lua
 // to C++.  unprotected lua calls could be wrapped in a pcall to ensure lua
 // panics don't kill the process.  or we can not use lua for the shell.  :(
@@ -441,6 +443,9 @@ Shell::Shell(const char* s, bool load_defaults) :
     loaded = false;
     load_string(lua_bootstrap, false, "bootstrap");
     install_version_strings(lua);
+    Shell** shell_ud = static_cast<Shell**>(lua_newuserdata(lua, sizeof(Shell*)));
+    *(shell_ud) = this;
+    lua_setglobal(lua, lua_shell_id);
     bootstrapped = true;
 
     current_shells.pop();
@@ -563,6 +568,24 @@ void Shell::install(const char* name, const luaL_Reg* reg)
     luaL_register(lua, name, reg);
 }
 
+void Shell::set_network_policy_user_id(lua_State* L, uint32_t user_id)
+{
+    lua_getglobal(L, lua_shell_id);
+    Shell* shell = *static_cast<Shell**>(lua_touserdata(L, -1));
+    lua_pop(L, 1);
+    shell->network_user_policy_id = user_id;
+}
+
+void Shell::set_user_network_policy()
+{
+    if (UNDEFINED_USER_POLICY_ID > network_user_policy_id)
+    {
+        NetworkPolicy* np =
+            SnortConfig::get_conf()->policy_map->get_user_network(network_user_policy_id);
+        set_network_policy(np);
+    }
+}
+
 void Shell::execute(const char* cmd, string& rsp)
 {
     set_default_policy(SnortConfig::get_conf());
@@ -575,7 +598,10 @@ void Shell::execute(const char* cmd, string& rsp)
         err = luaL_loadbuffer(lua, cmd, strlen(cmd), "shell");
 
         if ( !err )
+        {
+            set_user_network_policy();
             err = lua_pcall(lua, 0, 0, 0);
+        }
     }
     catch (...)
     {
index a4a32930418d836dd1d5c7e3bf655021a7dcb7d2..07d534b3ba717ce7db1febfa4180edce3ce33a48 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "dump_config/config_data.h"
 #include "framework/parameter.h"
+#include "policy.h"
 
 struct lua_State;
 
@@ -69,6 +70,8 @@ public:
     lua_State* get_lua() const
     { return lua; }
 
+    void set_user_network_policy();
+
 public:
     static bool is_trusted(const std::string& key);
     static void allowlist_append(const char* keyword, bool is_prefix);
@@ -86,6 +89,8 @@ public:
     static void set_lua_sandbox(const char* s)
     { lua_sandbox = s; }
 
+    static void set_network_policy_user_id(lua_State*, uint32_t user_id);
+
 private:
     static void add_config_root_node(const std::string& root_name, snort::Parameter::Type type);
 
@@ -100,6 +105,7 @@ private:
     static BaseConfigNode* s_current_node;
     static bool s_close_table;
     static std::string lua_sandbox;
+    static const char* const lua_shell_id;
 
 private:
     void clear_allowlist()
@@ -137,6 +143,7 @@ private:
     Allowlist internal_allowlist;
     Allowlist allowlist_prefixes;
     ConfigData config_data;
+    uint32_t network_user_policy_id = UNDEFINED_USER_POLICY_ID;
     bool load_defaults;
 };
 
index 9c0307242894c9b78f43dd481e62d16e971cadeb..6db5bff7b310d29ba146ed2ac46f6799bfdcfcd4 100644 (file)
@@ -1285,14 +1285,20 @@ Inspector* InspectorManager::get_inspector(const char* key, Module::Usage usage,
         }
         else if (Module::CONTEXT == usage)
         {
-            TrafficPolicy* il = get_network_policy()->traffic_policy;
+            NetworkPolicy* np = get_network_policy();
+            if (!np)
+                return nullptr;
+            TrafficPolicy* il = np->traffic_policy;
             assert(il);
             PHInstance* p = il->get_instance_by_type(key, type);
             return p ? p->handler : nullptr;
         }
         else
         {
-            FrameworkPolicy* il = get_inspection_policy()->framework_policy;
+            InspectionPolicy* ip = get_inspection_policy();
+            if (!ip)
+                return nullptr;
+            FrameworkPolicy* il = ip->framework_policy;
             assert(il);
             PHInstance* p = il->get_instance_by_type(key, type);
             return p ? p->handler : nullptr;
@@ -1326,8 +1332,11 @@ Inspector* InspectorManager::get_service_inspector_by_id(const SnortProtocolId p
 
 bool InspectorManager::delete_inspector(SnortConfig* sc, const char* iname)
 {
+    NetworkPolicy* np = get_network_policy();
+    if (!np)
+        return false;
     FrameworkPolicy* fp =
-        sc->policy_map->get_network_policy(0)->get_inspection_policy()->framework_policy;
+        np->get_inspection_policy()->framework_policy;
     return fp->delete_inspector(sc, iname);
 }