// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//--------------------------------------------------------------------------
-/* alert_fast
- *
- * Purpose: output plugin for fast alerting
- *
- * Arguments: alert file
- *
- * Effect:
- *
- * Alerts are written to a file in the snort fast alert format
- *
- * Comments: Allows use of fast alerts with other output plugin types
- *
- */
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#include <mutex>
#include <vector>
#include "detection/detection_engine.h"
#define FAST_BUF (4*K_BYTES)
static THREAD_LOCAL TextLog* fast_log = nullptr;
+static once_flag init_flag;
#define S_NAME "alert_fast"
#define F_NAME S_NAME ".txt"
//-------------------------------------------------------------------------
// helper
+//-------------------------------------------------------------------------
static void load_buf_ids(
Inspector* ins, const std::vector<const char*>& keys, std::vector<unsigned>& ids)
}
}
+using BufferIds = std::vector<unsigned>;
+
//-------------------------------------------------------------------------
// logger stuff
//-------------------------------------------------------------------------
private:
void log_data(Packet*, const Event&);
+ static void set_buffer_ids(Inspector*);
+ const BufferIds& get_buffer_ids(Inspector*, Packet*);
+
private:
string file;
unsigned long limit;
bool packet;
- std::vector<unsigned> req_ids;
- std::vector<unsigned> rsp_ids;
+ static std::vector<unsigned> req_ids;
+ static std::vector<unsigned> rsp_ids;
};
+std::vector<unsigned> FastLogger::req_ids;
+std::vector<unsigned> FastLogger::rsp_ids;
+
FastLogger::FastLogger(FastModule* m)
{
file = m->file ? F_NAME : "stdout";
limit = m->limit;
packet = m->packet;
+}
- //-----------------------------------------------------------------
- // FIXIT-L generalize buffer sets when other inspectors get smarter
- // this is only applicable to http_inspect
- // could be configurable; and should be should be shared with u2
+//-----------------------------------------------------------------
+// FIXIT-L generalize buffer sets when other inspectors get smarter
+// this is only applicable to http_inspect
+// could be configurable; and should be should be shared with u2
+//-----------------------------------------------------------------
+void FastLogger::set_buffer_ids(Inspector* gadget)
+{
+ std::vector<const char*> req
+ { "http_method", "http_version", "http_uri", "http_header", "http_cookie", "http_client_body" };
- Inspector* ins = InspectorManager::get_inspector("http_inspect");
+ std::vector<const char*> rsp
+ { "http_version", "http_stat_code", "http_stat_msg", "http_uri", "http_header", "http_cookie" };
- if ( !ins )
- return;
+ load_buf_ids(gadget, req, req_ids);
+ load_buf_ids(gadget, rsp, rsp_ids);
+}
- std::vector<const char*> req
- { "http_method", "http_version", "http_uri", "http_header", "http_cookie",
- "http_client_body" };
+const BufferIds& FastLogger::get_buffer_ids(Inspector* gadget, Packet* p)
+{
+ // lazy init required because loggers don't have a configure (yet)
+ call_once(init_flag, set_buffer_ids, gadget);
- std::vector<const char*> rsp
- { "http_version", "http_stat_code", "http_stat_msg", "http_uri", "http_header",
- "http_cookie" };
- //-----------------------------------------------------------------
+ InspectionBuffer buf;
+ const std::vector<unsigned>& idv =
+ gadget->get_buf(HttpEnums::HTTP_BUFFER_RAW_STATUS, p, buf) ? rsp_ids : req_ids;
- load_buf_ids(ins, req, req_ids);
- load_buf_ids(ins, rsp, rsp_ids);
+ return idv;
}
void FastLogger::open()
// available if a response was processed by http_inspect
void FastLogger::log_data(Packet* p, const Event& event)
{
- bool log_pkt = true;
-
TextLog_NewLine(fast_log);
+
+ bool log_pkt = true;
const char* ins_name = "snort";
Inspector* gadget = nullptr;
+
if ( p->flow and p->flow->session )
{
snort::StreamSplitter* ss = p->flow->session->get_splitter(p->is_from_client());
ins_name = gadget->get_name();
}
}
- const char** buffers = gadget ? gadget->get_api()->buffers : nullptr;
+ const char** buffers = (gadget and !strcmp(ins_name, "http_inspect")) ? gadget->get_api()->buffers : nullptr;
if ( buffers )
{
- InspectionBuffer buf;
- const std::vector<unsigned>& idv = gadget->get_buf(HttpEnums::HTTP_BUFFER_RAW_STATUS,
- p, buf) ? rsp_ids : req_ids;
- bool rsp = (idv == rsp_ids);
+ const BufferIds& idv = get_buffer_ids(gadget, p);
for ( auto id : idv )
{
+ InspectionBuffer buf;
if ( gadget->get_buf(id, p, buf) )
LogNetData(fast_log, buf.data, buf.len, p, buffers[id-1], ins_name);
- log_pkt = rsp;
+ log_pkt = (idv == rsp_ids);
}
}
else if ( gadget )