]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #960 in SNORT/snort3 from dump_buffer to master
authorTom Peters (thopeter) <thopeter@cisco.com>
Wed, 19 Jul 2017 20:31:36 +0000 (16:31 -0400)
committerTom Peters (thopeter) <thopeter@cisco.com>
Wed, 19 Jul 2017 20:31:36 +0000 (16:31 -0400)
Squashed commit of the following:

commit 3a0feab9993894fc00bcfdebb77e11b537ca0e35
Author: mdagon <mdagon@cisco.com>
Date:   Thu Jun 29 11:40:05 2017 -0400

    Rule evaluation trace utility

13 files changed:
src/detection/CMakeLists.txt
src/detection/Makefile.am
src/detection/context_switcher.cc
src/detection/detect_trace.cc [new file with mode: 0644]
src/detection/detect_trace.h [new file with mode: 0644]
src/detection/detection_engine.cc
src/detection/detection_options.cc
src/detection/fp_create.cc
src/detection/fp_create.h
src/detection/fp_detect.cc
src/main/snort_debug.cc
src/main/snort_debug.h
src/ports/port_group.h

index 1f7631a57b1000d8fa46272ed0b3009607b86f63..823b9a746d4dc40050e6b040fcf85c62ef473038 100644 (file)
@@ -5,6 +5,7 @@ set (DETECTION_INCLUDES
     detection_engine.h
     detection_options.h
     detection_util.h
+    detect_trace.h
     ips_context.h
     regex_offload.h
     rule_option_types.h
@@ -22,6 +23,7 @@ add_library (detection STATIC
     detection_options.cc
     detection_options.h
     detection_util.cc
+    detect_trace.cc
     fp_config.cc
     fp_config.h
     fp_create.cc
index df48f48dbd1499def7d1b76987e572f8b4278c7d..2b23711d542a6b3210e54318e180b1ab574ebb69 100644 (file)
@@ -9,6 +9,7 @@ detection_defines.h \
 detection_engine.h \
 detection_options.h \
 detection_util.h \
+detect_trace.h \
 ips_context.h \
 regex_offload.h \
 rule_option_types.h \
@@ -24,6 +25,7 @@ detection_engine.cc \
 detection_options.cc \
 detection_options.h \
 detection_util.cc \
+detect_trace.cc \
 fp_config.cc \
 fp_config.h \
 fp_create.cc \
index f6fe56d946e373d2a16b8f50cfd7814cd68e4647..c3b01a65137eadda83fc91744781979c03c7203f 100644 (file)
@@ -30,6 +30,7 @@
 #include "main/snort_debug.h"
 #include "utils/stats.h"
 
+#include "detect_trace.h"
 #include "ips_context.h"
 
 #ifdef UNIT_TEST
@@ -73,7 +74,7 @@ void ContextSwitcher::start()
 {
     assert(busy.empty());
     assert(idle.size() > 0);
-    trace_logf(detection, "%" PRIu64 " cs::start %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::start %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, idle.back()->get_slot(), idle.size(), busy.size());
     busy.push_back(idle.back());
     idle.pop_back();
@@ -82,7 +83,7 @@ void ContextSwitcher::start()
 void ContextSwitcher::stop()
 {
     assert(busy.size() == 1);
-    trace_logf(detection, "%" PRIu64 " cs::stop %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::stop %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, busy.back()->get_slot(), idle.size(), busy.size());
     idle.push_back(busy.back());
     busy.pop_back();
@@ -90,7 +91,7 @@ void ContextSwitcher::stop()
 
 void ContextSwitcher::abort()
 {
-    trace_logf(detection, "%" PRIu64 " cs::abort (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::abort (i=%zu, b=%zu)\n",
         pc.total_from_daq, idle.size(), busy.size());
     for ( unsigned i = 0; i < hold.capacity(); ++i )
     {
@@ -110,7 +111,7 @@ void ContextSwitcher::abort()
 IpsContext* ContextSwitcher::interrupt()
 {
     assert(!idle.empty());
-    trace_logf(detection, "%" PRIu64 " cs::interrupt %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::interrupt %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, idle.back()->get_slot(), idle.size(), busy.size());
     busy.push_back(idle.back());
     idle.pop_back();
@@ -120,7 +121,7 @@ IpsContext* ContextSwitcher::interrupt()
 IpsContext* ContextSwitcher::complete()
 {
     assert(!busy.empty());
-    trace_logf(detection, "%" PRIu64 " cs::complete %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::complete %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, busy.back()->get_slot(), idle.size(), busy.size());
     idle.push_back(busy.back());
     busy.pop_back();
@@ -130,7 +131,7 @@ IpsContext* ContextSwitcher::complete()
 unsigned ContextSwitcher::suspend()
 {
     assert(!busy.empty());
-    trace_logf(detection, "%" PRIu64 " cs::suspend %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::suspend %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, busy.back()->get_slot(), idle.size(), busy.size());
     IpsContext* c = busy.back();
     busy.pop_back();
@@ -143,7 +144,7 @@ unsigned ContextSwitcher::suspend()
 void ContextSwitcher::resume(unsigned slot)
 {
     assert(slot <= hold.capacity());
-    trace_logf(detection, "%" PRIu64 " cs::resume %u (i=%zu, b=%zu)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " cs::resume %u (i=%zu, b=%zu)\n",
         pc.total_from_daq, slot, idle.size(), busy.size());
     busy.push_back(hold[slot]);
     hold[slot] = nullptr;
diff --git a/src/detection/detect_trace.cc b/src/detection/detect_trace.cc
new file mode 100644 (file)
index 0000000..84df1cb
--- /dev/null
@@ -0,0 +1,171 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2017-2017 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.
+//--------------------------------------------------------------------------
+
+// detect_trace.cc author Maya Dagon <mdagon@cisco.com>
+
+#include "detect_trace.h"
+
+#include <cctype>
+#include <string>
+
+#include "log/log.h"
+#include "main/snort_debug.h"
+#include "main/thread.h"
+#include "utils/stats.h"
+#include "utils/util.h"
+#include "packet_io/active.h"
+
+#include "fp_create.h"
+#include "treenodes.h"
+
+using namespace std;
+
+Trace TRACE_NAME(detection);
+
+#ifdef DEBUG_MSGS
+
+const uint64_t trace_buffer = TRACE_BUFFER_MINIMAL | TRACE_BUFFER_VERBOSE;
+
+static THREAD_LOCAL char* cursor_name = nullptr;
+static THREAD_LOCAL unsigned cursor_pos = -1;
+static THREAD_LOCAL Packet* pkt = nullptr;
+
+void clear_trace_cursor_info()
+{
+    if (cursor_name != nullptr)
+    {
+        snort_free(cursor_name);
+        cursor_name = nullptr;
+    }
+    cursor_pos = -1;
+}
+
+void print_pkt_info(Packet* p)
+{
+    const char* dir;
+    string dst_addr, src_addr;
+    unsigned src_port = 0, dst_port = 0;
+
+    pkt = p; //save packet pointer for later
+
+    if ( pkt->is_from_client() )
+        dir = "C2S";
+    else if ( pkt->is_from_server() )
+        dir = "S2C";
+    else
+        dir = "UNK";
+
+    if ( pkt->has_ip() or pkt->is_data() )
+    {
+        src_addr = string(pkt->ptrs.ip_api.get_src()->ntoa());
+        dst_addr = string(pkt->ptrs.ip_api.get_dst()->ntoa());
+    }
+
+    if ( pkt->proto_bits & (PROTO_BIT__TCP|PROTO_BIT__UDP) )
+    {
+        src_port = pkt->ptrs.sp;
+        dst_port = pkt->ptrs.dp;
+    }
+
+    trace_logf(detection, TRACE_RULE_EVAL,"packet %" PRIu64 " %s %s:%u %s:%u\n",
+        pc.total_from_daq, dir, src_addr.c_str(), src_port, dst_addr.c_str(), dst_port);
+}
+
+void print_pattern(const PatternMatchData* pmd)
+{
+    string hex, txt, opts;
+
+    get_pattern_info(pmd, pmd->pattern_buf, pmd->pattern_size, hex, txt, opts);
+    trace_logf(detection, TRACE_RULE_EVAL,
+        "Fast pattern %s[%u] = '%s' |%s| %s\n",
+        pm_type_strings[pmd->pm_type],  pmd->pattern_size,
+        txt.c_str(), hex.c_str(), opts.c_str());
+}
+
+void dump_buffer(const uint8_t* buff, unsigned len)
+{
+    if (!trace_enabled(detection_trace, trace_buffer))
+        return;
+
+    if (len == 0)
+    {
+        trace_log(detection, "Buffer dump - empty buffer\n");
+        return;
+    }
+
+    assert (pkt != nullptr);
+    LogNetData(buff, len, pkt);
+}
+
+void node_eval_trace(const detection_option_tree_node_t* node, const Cursor& cursor)
+{
+    const char* name = cursor.get_name();
+    unsigned pos = cursor.get_pos();
+
+    if (node->option_type != RULE_OPTION_TYPE_LEAF_NODE )
+    {
+        trace_logf(detection, TRACE_RULE_EVAL,
+            "Evaulating option %s, cursor name %s, cursor position %u\n",
+            ((IpsOption*)node->option_data)->get_name(), name, pos);
+    }
+    else
+    {
+        trace_logf(detection, TRACE_RULE_EVAL, "Reached leaf, cursor name %s, cursor position %u\n",
+                                 name, pos);
+    }
+
+    if (!trace_enabled(detection_trace, trace_buffer))
+        return;
+
+    if (trace_enabled(detection_trace, TRACE_BUFFER_VERBOSE))
+    {
+        dump_buffer(cursor.buffer() + pos, cursor.length());
+    }
+    else if ((pos != cursor_pos) || strcmp(cursor_name, name))
+    {
+        cursor_pos = pos;
+        snort_free(cursor_name);
+        cursor_name = snort_strdup(name);
+        dump_buffer(cursor.buffer() + pos, cursor.length());
+    }
+}
+
+#else
+
+void clear_trace_cursor_info()
+{
+}
+
+void print_pkt_info(Packet*)
+{
+}
+
+void print_pattern(const PatternMatchData*)
+{
+}
+
+void dump_buffer(const uint8_t*, unsigned)
+{
+}
+
+void node_eval_trace(const detection_option_tree_node_t*, const Cursor&)
+{
+}
+
+#endif
+
diff --git a/src/detection/detect_trace.h b/src/detection/detect_trace.h
new file mode 100644 (file)
index 0000000..1cb034d
--- /dev/null
@@ -0,0 +1,52 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2017-2017 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.
+//--------------------------------------------------------------------------
+
+// detect_trace.h author Maya Dagon
+
+#ifndef DETECT_TRACE_H
+#define DETECT_TRACE_H
+
+// Detection trace utility
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "framework/cursor.h"
+#include "main/snort_types.h"
+
+#include "detection_options.h"
+#include "pattern_match_data.h"
+
+enum
+{
+       TRACE_DETECTION_ENGINE = 0x1,
+       TRACE_RULE_EVAL = 0x2,
+       TRACE_BUFFER_MINIMAL = 0x4,
+       TRACE_BUFFER_VERBOSE = 0x8,
+       TRACE_RULE_VARS = 0x10
+};
+       
+void clear_trace_cursor_info();
+void print_pkt_info(Packet* p);
+void print_pattern(const PatternMatchData* pmd);
+void dump_buffer(const uint8_t* buff, unsigned len);
+void node_eval_trace(const detection_option_tree_node_t* node, const Cursor& cursor);
+
+#endif
+
index b976cbe9e46dd11d92910096da13325c6ff69409..a02e36f8bf396c750329353bdd38b574725e69de 100644 (file)
 #include "context_switcher.h"
 #include "detection_util.h"
 #include "detect.h"
+#include "detect_trace.h"
 #include "fp_config.h"
 #include "fp_detect.h"
 #include "ips_context.h"
 #include "regex_offload.h"
 
-Trace TRACE_NAME(detection);
-
 static THREAD_LOCAL RegexOffload* offloader = nullptr;
 static THREAD_LOCAL DataPointer next_file_data = { nullptr, 0 };
 static THREAD_LOCAL uint64_t context_num = 0;
@@ -211,12 +210,12 @@ void DetectionEngine::idle()
     {
         while ( offloader->count() )
         {
-            trace_logf(detection, "%" PRIu64 " de::sleep\n", pc.total_from_daq);
+            trace_logf(detection, TRACE_DETECTION_ENGINE,  "%" PRIu64 " de::sleep\n", pc.total_from_daq);
             const struct timespec blip = { 0, 1 };
             nanosleep(&blip, nullptr);
             onload();
         }
-        trace_logf(detection, "%" PRIu64 " de::idle (r=%d)\n", pc.total_from_daq, offloader->count());
+        trace_logf(detection,  TRACE_DETECTION_ENGINE, "%" PRIu64 " de::idle (r=%d)\n", pc.total_from_daq, offloader->count());
         offloader->stop();
     }
 }
@@ -226,7 +225,7 @@ void DetectionEngine::onload(Flow* flow)
     while ( flow->is_offloaded() )
     {
         const struct timespec blip = { 0, 1 };
-        trace_logf(detection, "%" PRIu64 " de::sleep\n", pc.total_from_daq);
+        trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " de::sleep\n", pc.total_from_daq);
         nanosleep(&blip, nullptr);
         onload();
     }
@@ -245,7 +244,7 @@ void DetectionEngine::onload()
     IpsContext* c = sw->get_context(id);
     assert(c);
 
-    trace_logf(detection, "%" PRIu64 " de::onload %u (r=%d)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " de::onload %u (r=%d)\n",
         pc.total_from_daq, id, offloader->count());
 
     Packet* p = c->packet;
@@ -275,7 +274,7 @@ bool DetectionEngine::offload(Packet* p)
     assert(p->context == sw->get_context());
     unsigned id = sw->suspend();
 
-    trace_logf(detection, "%" PRIu64 " de::offload %u (r=%d)\n",
+    trace_logf(detection, TRACE_DETECTION_ENGINE, "%" PRIu64 " de::offload %u (r=%d)\n",
         pc.total_from_daq, id, offloader->count());
 
     p->flow->set_offloaded();
index e1c25a261ff3f8835fde6ee293569806f0767f27..96e230eec5c8a70f91116df6667b382d38b4179f 100644 (file)
@@ -32,6 +32,8 @@
 
 #include "detection_options.h"
 
+#include <string>
+
 #include "filters/detection_filter.h"
 #include "framework/cursor.h"
 #include "hash/sfhashfcn.h"
@@ -41,7 +43,9 @@
 #include "latency/packet_latency.h"
 #include "latency/rule_latency_state.h"
 #include "log/messages.h"
+#include "main/modules.h"
 #include "main/snort_config.h"
+#include "main/snort_debug.h"
 #include "main/thread_config.h"
 #include "managers/ips_manager.h"
 #include "parser/parser.h"
 #include "protocols/packet_manager.h"
 #include "utils/util.h"
 
-#include "detection_defines.h"
-#include "fp_detect.h"
-#include "pattern_match_data.h"
-
 #include "detection_defines.h"
 #include "detection_engine.h"
 #include "detection_util.h"
+#include "detect_trace.h"
 #include "fp_create.h"
 #include "fp_detect.h"
 #include "ips_context.h"
@@ -365,6 +366,8 @@ int detection_option_node_evaluate(
     if ( !eval_data || !eval_data->p || !eval_data->pomd )
         return 0;
 
+    node_eval_trace(node, cursor);
+
     auto p = eval_data->p;
     auto pomd = eval_data->pomd;
 
@@ -383,6 +386,8 @@ int detection_option_node_evaluate(
                 !(p->packet_flags & PKT_IP_RULE_2ND) &&
                 !(p->proto_bits & (PROTO_BIT__TEREDO|PROTO_BIT__GTP)) )
             {
+                trace_log(detection, TRACE_RULE_EVAL,
+                    "Was evaluated before, returning last check result\n");
                 return last_check.result;
             }
         }
@@ -440,7 +445,9 @@ int detection_option_node_evaluate(
                     DebugFormat(DEBUG_DETECT,
                         "[**] SID %u not matched because of service mismatch (%d!=%d [**]\n",
                         sig_info.sid, app_proto, sig_info.services[0].service_ordinal);
-
+                    trace_logf(detection, TRACE_RULE_EVAL,
+                        "SID %u not matched because of service mismatch %d!=%d \n",
+                        sig_info.sid, app_proto, sig_info.services[0].service_ordinal);
                     break;  // out of case
                 }
             }
@@ -458,9 +465,13 @@ int detection_option_node_evaluate(
                 bool f_result = true;
 
                 if ( otn->detection_filter )
+                {
+                    trace_log(detection, TRACE_RULE_EVAL,
+                        "Evaluating detection filter\n");
                     f_result = !detection_filter_test(otn->detection_filter,
                         p->ptrs.ip_api.get_src(), p->ptrs.ip_api.get_dst(),
                         p->pkth->ts.tv_sec);
+                }
 
                 if ( f_result )
                 {
@@ -470,11 +481,21 @@ int detection_option_node_evaluate(
                     {
                         PatternMatchData* pmd = (PatternMatchData*)eval_data->pmd;
                         int pattern_size = pmd ? pmd->pattern_size : 0;
+#ifdef DEBUG_MSGS
+                        const SigInfo& si = otn->sigInfo;
+                        trace_logf(detection, TRACE_RULE_EVAL,
+                            "Matched rule gid:sid:rev %u:%u:%u\n", si.gid, si.sid, si.rev);
+#endif
+
                         fpAddMatch((OtnxMatchData*)pomd, pattern_size, otn);
                     }
                     result = rval = DETECTION_OPTION_MATCH;
                 }
             }
+#ifdef DEBUG_MSGS
+            else
+                trace_log(detection, TRACE_RULE_EVAL, "Header check failed\n");
+#endif
 
             break;
         }
@@ -530,11 +551,13 @@ int detection_option_node_evaluate(
 
         if ( rval == DETECTION_OPTION_NO_MATCH )
         {
+            trace_log(detection, TRACE_RULE_EVAL, "no match\n");
             state.last_check.result = result;
             return result;
         }
         else if ( rval == DETECTION_OPTION_FAILED_BIT )
         {
+            trace_log(detection, TRACE_RULE_EVAL, "failed bit\n");
             eval_data->flowbit_failed = 1;
             // clear the timestamp so failed flowbit gets eval'd again
             state.last_check.flowbit_failed = 1;
@@ -547,11 +570,18 @@ int detection_option_node_evaluate(
             // so nodes below this don't alert.
             tmp_noalert_flag = eval_data->flowbit_noalert;
             eval_data->flowbit_noalert = 1;
+            trace_log(detection, TRACE_RULE_EVAL, "flowbit no alert\n");
         }
 
         // Back up byte_extract vars so they don't get overwritten between rules
+        trace_log(detection, TRACE_RULE_VARS, "Rule options variables: \n");
         for ( int i = 0; i < NUM_IPS_OPTIONS_VARS; ++i )
+        {
             GetVarValueByIndex(&(tmp_byte_extract_vars[i]), (int8_t)i);
+            trace_logf_wo_name(detection, TRACE_RULE_VARS, "var[%d]=%d ", i,
+                tmp_byte_extract_vars[i]);
+        }
+        trace_log_wo_name(detection, TRACE_RULE_VARS, "\n");
 
         if ( PacketLatency::fastpath() )
         {
index baa302247ed3045fff18b5d3767c4eb5d58cea71..0c983bbd185d0e51f8ceeb03f4e82b658fbf3cee 100644 (file)
@@ -47,6 +47,7 @@
 #include "utils/util.h"
 
 #include "detection_options.h"
+#include "detect_trace.h"
 #include "fp_config.h"
 #include "fp_utils.h"
 #include "pattern_match_data.h"
@@ -54,6 +55,8 @@
 #include "service_map.h"
 #include "treenodes.h"
 
+using namespace std;
+
 static unsigned mpse_count = 0;
 static const char* s_group = "";
 
@@ -66,11 +69,6 @@ static void print_nfp_info(const char*, const OptTreeNode*);
 static void print_fp_info(const char*, const OptTreeNode*, const PatternMatchData*,
     const char* pattern, int pattern_length);
 
-static const char* const pm_type_strings[PM_TYPE_MAX] =
-{
-    "packet", "alt", "key", "header", "body", "file"
-};
-
 static int finalize_detection_option_tree(SnortConfig* sc, detection_option_tree_root_t* root)
 {
     if ( !root )
@@ -707,11 +705,9 @@ static int fpCreateRuleMaps(SnortConfig* sc, RulePortTables* p)
     if (fpCreateInitRuleMap(sc->prmIcmpRTNX, p->icmp.src, p->icmp.dst, p->icmp.any))
         return -1;
 
-
     if (fpCreateInitRuleMap(sc->prmTcpRTNX, p->tcp.src, p->tcp.dst, p->tcp.any))
         return -1;
 
-
     if (fpCreateInitRuleMap(sc->prmUdpRTNX, p->udp.src, p->udp.dst, p->udp.any))
         return -1;
 
@@ -1365,8 +1361,7 @@ static void fp_print_service_rules(SnortConfig* sc, SFGHASH* cli, SFGHASH* srv,
 static void fp_print_service_rules_by_proto(SnortConfig* sc, srmm_table_t* srmm)
 {
     for ( int i = SNORT_PROTO_IP; i < SNORT_PROTO_MAX; ++i )
-        fp_print_service_rules(sc, srmm->to_srv[i], srmm->to_cli[i],
-        sc->proto_ref->get_name(i));
+        fp_print_service_rules(sc, srmm->to_srv[i], srmm->to_cli[i], sc->proto_ref->get_name(i));
 }
 
 static void fp_sum_port_groups(PortGroup* pg, unsigned c[PM_TYPE_MAX])
@@ -1610,11 +1605,9 @@ static void print_nfp_info(const char* group, const OptTreeNode* otn)
         group, otn->sigInfo.gid, otn->sigInfo.sid, otn->sigInfo.rev);
 }
 
-static void print_fp_info(
-    const char* group, const OptTreeNode* otn, const PatternMatchData* pmd,
-    const char* pattern, int pattern_length)
+void get_pattern_info(const PatternMatchData* pmd,
+    const char* pattern, int pattern_length, string& hex, string& txt, string& opts)
 {
-    std::string hex, txt;
     char buf[8];
 
     for ( int i = 0; i < pattern_length; ++i )
@@ -1623,12 +1616,22 @@ static void print_fp_info(
         hex += buf;
         txt += isprint(pattern[i]) ? pattern[i] : '.';
     }
-    std::string opts = "(";
-    if ( pmd->is_fast_pattern() ) opts += " user";
-    if ( pmd->fp_only ) opts += " only";
-    if ( pmd->is_negated() ) opts += " negated";
+    opts = "(";
+    if ( pmd->is_fast_pattern() )
+        opts += " user";
+    if ( pmd->fp_only )
+        opts += " only";
+    if ( pmd->is_negated() )
+        opts += " negated";
     opts += " )";
+}
+
+static void print_fp_info(const char* group, const OptTreeNode* otn, const PatternMatchData* pmd,
+    const char* pattern, int pattern_length)
+{
+    std::string hex, txt, opts;
 
+    get_pattern_info(pmd, pattern, pattern_length, hex, txt, opts);
     LogMessage("FP %s %u:%u:%u %s[%d] = '%s' |%s| %s\n",
         group, otn->sigInfo.gid, otn->sigInfo.sid, otn->sigInfo.rev,
         pm_type_strings[pmd->pm_type], pattern_length,
index 76d8950b996626ed8663d378535ad3f5f36febe8..c8f7ec777728a794a242cc9c12dd09ba885a33aa 100644 (file)
@@ -27,6 +27,7 @@
 
 // this is where rule groups are compiled and MPSE are instantiated
 
+#include <string>
 #include "ports/port_group.h"
 
 struct SnortConfig;
@@ -51,6 +52,9 @@ struct NCListNode
 */
 int fpCreateFastPacketDetection(SnortConfig*);
 void fpDeleteFastPacketDetection(SnortConfig*);
+void get_pattern_info(const PatternMatchData* pmd,
+    const char* pattern, int pattern_length, std::string& hex, std::string& txt,
+    std::string& opts);
 
 #endif
 
index b69f9a7f1003b97a78f5b526b98706eeb1952c44..5b291807ca60b95008f6a1c7cb573daa801ffbf6 100644 (file)
@@ -39,8 +39,6 @@
 
 #include "fp_detect.h"
 
-#include "detection/detection_engine.h"
-#include "detection/ips_context.h"
 #include "events/event.h"
 #include "filters/rate_filter.h"
 #include "filters/sfthreshold.h"
@@ -50,6 +48,7 @@
 #include "latency/rule_latency.h"
 #include "log/messages.h"
 #include "log/packet_tracer.h"
+#include "main/modules.h"
 #include "main/snort.h"
 #include "main/snort_config.h"
 #include "main/snort_debug.h"
 #include "utils/stats.h"
 #include "utils/util.h"
 
-#include "detect.h"
-#include "detection_options.h"
-#include "detection_util.h"
-#include "fp_config.h"
-#include "fp_create.h"
-#include "pattern_match_data.h"
-#include "pcrm.h"
-#include "service_map.h"
-
 #include "context_switcher.h"
+#include "detect.h"
+#include "detect_trace.h"
 #include "detection_util.h"
 #include "detection_engine.h"
 #include "detection_options.h"
@@ -313,7 +305,7 @@ int fpEvalRTN(RuleTreeNode* rtn, Packet* p, int check_ports)
 
     // FIXIT-L maybe add a port test here ...
 
-    DebugFormat(DEBUG_DETECT, "[*] Rule Head %p\n", (void*) rtn);
+    DebugFormat(DEBUG_DETECT, "[*] Rule Head %p\n", (void*)rtn);
 
     if (!rtn->rule_func->RuleHeadFunc(p, rtn, rtn->rule_func, check_ports))
     {
@@ -345,11 +337,14 @@ static int detection_option_tree_evaluate(detection_option_tree_root_t* root,
     Cursor c(eval_data->p);
     int rval = 0;
 
+    trace_log(detection, TRACE_RULE_EVAL, "Starting tree eval\n");
+
     for ( int i = 0; i < root->num_children; ++i )
     {
         // Increment number of events generated from that child
         rval += detection_option_node_evaluate(root->children[i], eval_data, c);
     }
+    clear_trace_cursor_info();
 
     return rval;
 }
@@ -370,6 +365,8 @@ static int rule_tree_match(
     eval_data.flowbit_failed = 0;
     eval_data.flowbit_noalert = 0;
 
+    print_pattern(pmx->pmd);
+
     {
         Profile rule_profile(rulePerfStats);
         /* NOTE: The otn will be the first one in the match state. If there are
@@ -755,7 +752,8 @@ private:
     unsigned count;
     unsigned flushed;
 
-    struct Node {
+    struct Node
+    {
         void* user;
         void* tree;
         void* list;
@@ -804,11 +802,17 @@ bool MpseStash::process(MpseMatch match, void* context)
 
     pmqs.tot_inq_flush += flushed;
 
+#ifdef DEBUG_MSGS
+    if (count == 0)
+        trace_log(detection, TRACE_RULE_EVAL, "Fast pattern processing - no matches found\n");
+#endif
+
     for ( unsigned i = 0; i < count; ++i )
     {
         Node& node = queue[i];
 
         // process a pattern - case is handled by otn processing
+        trace_logf(detection, TRACE_RULE_EVAL,"Processing pattern match #%d\n", i+1);
         int res = match(node.user, node.tree, node.index, context, node.list);
 
         if ( res > 0 )
@@ -866,6 +870,7 @@ static inline int search_data(
     omd->data = buf; omd->size = len;
     MpseStash* stash = omd->p->context->stash;
     stash->init();
+    dump_buffer(buf, len);
     so->search(buf, len, rule_tree_queue, omd, &start_state);
     stash->process(rule_tree_match, omd);
     if ( PacketLatency::fastpath() )
@@ -880,7 +885,13 @@ static inline int search_buffer(
     if ( gadget->get_fp_buf(ibt, omd->p, buf) )
     {
         if ( Mpse* so = omd->pg->mpse[pmt] )
+        {
+            trace_logf(detection, TRACE_RULE_EVAL,
+                "inspector %s, buffer type %s\n",
+                gadget->get_name(),pm_type_strings[pmt]);
+
             search_data(so, omd, buf.data, buf.len, cnt);
+        }
     }
     return 0;
 }
@@ -897,6 +908,8 @@ static int fp_search(
 
     bool user_mode = snort_conf->sopgTable->user_mode;
 
+    trace_log(detection, TRACE_RULE_EVAL, "Fast pattern search\n");
+
     if ( (!user_mode or type < 2) and p->data and p->dsize )
     {
         // ports search raw packet only
@@ -944,7 +957,10 @@ static int fp_search(
             DataPointer file_data = p->context->file_data;
 
             if ( file_data.len )
+            {
+                trace_log(detection, TRACE_RULE_EVAL, "Searching file data\n");
                 search_data(so, omd, file_data.data, file_data.len, pc.file_searches);
+            }
         }
     }
     return 0;
@@ -975,6 +991,8 @@ static inline int fpEvalHeaderSW(PortGroup* port_group, Packet* p,
     uint16_t tmp_dsize;
     FastPatternConfig* fp = snort_conf->fast_pattern_config;
 
+    print_pkt_info(p);
+
     if (ip_rule)
     {
         tmp_payload = p->data;
@@ -1023,6 +1041,7 @@ static inline int fpEvalHeaderSW(PortGroup* port_group, Packet* p,
             {
                 Profile rule_profile(rulePerfStats);
                 Profile rule_nfp_eval_profile(ruleNFPEvalPerfStats);
+                trace_log(detection, TRACE_RULE_EVAL, "Testing non-content rules\n");
                 rval = detection_option_tree_evaluate(
                     (detection_option_tree_root_t*)port_group->nfp_tree, &eval_data);
             }
@@ -1074,7 +1093,7 @@ static inline void fpEvalHeaderIp(Packet* p, OtnxMatchData* omd)
     if ( ip_group )
         fpEvalHeaderSW(ip_group, p, 0, 1, 0, omd);
 
-    if  (any )
+    if (any )
         fpEvalHeaderSW(any, p, 0, 1, 0, omd);
 }
 
@@ -1125,13 +1144,13 @@ static inline void fpEvalHeaderUdp(Packet* p, OtnxMatchData* omd)
         p->ptrs.sp,p->ptrs.dp,(void*)src,(void*)dst,(void*)any);
 
     if ( dst )
-        fpEvalHeaderSW(dst, p, 1, 0, 0, omd) ;
+        fpEvalHeaderSW(dst, p, 1, 0, 0, omd);
 
     if ( src )
-        fpEvalHeaderSW(src, p, 1, 0, 0, omd) ;
+        fpEvalHeaderSW(src, p, 1, 0, 0, omd);
 
     if ( any )
-        fpEvalHeaderSW(any, p, 1, 0, 0, omd) ;
+        fpEvalHeaderSW(any, p, 1, 0, 0, omd);
 }
 
 static inline bool fpEvalHeaderSvc(Packet* p, OtnxMatchData* omd, int proto)
index 325e61b05a5d9f5ea8e5af8c8b057acdb3bb6707..7b216679e48dd68e562eb8f70f6f6d7d6edad44b 100644 (file)
@@ -104,21 +104,29 @@ static inline void trace_vprintf(const char* name, Trace mask, const char* file,
     if ( !trace_enabled(mask, flags) )
         return;
 
-    char buf[STD_BUF];
+    char buf[STD_BUF]; 
     int buf_len = sizeof(buf);
     char* buf_ptr = buf;
-
-    int size = snprintf(buf, buf_len, "%s: ", name);
-    if ( size > 0 && size < buf_len )
-    {
-        buf_ptr += size;
-        buf_len -= size;
+    int size;
+       
+    if (name)
+    {   
+        size = snprintf(buf, buf_len, "%s: ", name);
+        if ( size >= buf_len )
+            size = buf_len - 1;
+        if ( size > 0 )
+        {
+            buf_ptr += size;
+            buf_len -= size;
+        }
     }
-
+       
     if ( file )
     {
         size = snprintf(buf_ptr, buf_len, "%s:%d: ", file, line);
-        if ( size > 0 && size < buf_len )
+        if ( size >= buf_len )
+            size = buf_len - 1;
+        if ( size > 0 )
         {
             buf_ptr += size;
             buf_len -= size;
index 17c8d62cfd12041f5274531597917169feee12c5..e49c6017de35fb48b4e0dc116b73019e90802847 100644 (file)
@@ -155,9 +155,15 @@ static inline void trace_print(const char* name, Trace mask, const char* file,
 #define trace_log(tracer, ...) \
     trace_print(#tracer, tracer##_trace, nullptr, 0, __VA_ARGS__)
 
+#define trace_log_wo_name(tracer, ...) \
+    trace_print(nullptr, tracer##_trace, nullptr, 0, __VA_ARGS__)
+
 #define trace_logf(tracer, ...) \
     trace_printf(#tracer, tracer##_trace, nullptr, 0, __VA_ARGS__)
 
+#define trace_logf_wo_name(tracer, ...) \
+    trace_printf(nullptr, tracer##_trace, nullptr, 0, __VA_ARGS__)
+
 #define trace_debug(tracer, ...) \
     trace_print(#tracer, tracer##_trace, __FILE__, __LINE__, __VA_ARGS__)
 
@@ -189,7 +195,9 @@ private:
 
 #else
 #define trace_log(tracer, ...)
+#define trace_log_wo_name(tracer, ...)
 #define trace_logf(tracer, ...)
+#define trace_logf_wo_name(tracer, ...)
 #define trace_debug(tracer, ...)
 #define trace_debugf(tracer, ...)
 
index 492eb8a9ce257feb84bc69a3d57f45c4d8bb7fed..e8eb87d13740e6ffca8a2a22392b8f36769307ff 100644 (file)
@@ -33,7 +33,7 @@
 
 enum PmType
 {
-    PM_TYPE_PKT,
+    PM_TYPE_PKT = 0,
     PM_TYPE_ALT,
     PM_TYPE_KEY,
     PM_TYPE_HEADER,
@@ -42,6 +42,11 @@ enum PmType
     PM_TYPE_MAX
 };
 
+const char* const pm_type_strings[PM_TYPE_MAX] =
+{
+    "packet", "alt", "key", "header", "body", "file"
+};
+
 struct RULE_NODE
 {
     RULE_NODE* rnNext;