suricata
output-json-sip.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Giuseppe Longo <giuseppe@glongo.it>
22  *
23  * Implement JSON/eve logging app-layer SIP.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "app-layer-sip.h"
48 #include "output-json-sip.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogSIPFileCtx_ {
56 
57 typedef struct LogSIPLogThread_ {
61 
62 void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id)
63 {
64  SIPState *state = FlowGetAppState(f);
65  if (state) {
66  SIPTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SIP, state, tx_id);
67  if (tx) {
68  rs_sip_log_json(tx, js);
69  }
70  }
71 }
72 
73 static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
74  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
75 {
76  SIPTransaction *siptx = tx;
77  LogSIPLogThread *thread = thread_data;
78 
79  JsonBuilder *js = CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "sip", NULL);
80  if (unlikely(js == NULL)) {
81  return TM_ECODE_OK;
82  }
83  EveAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
84 
85  if (!rs_sip_log_json(siptx, js)) {
86  goto error;
87  }
88  if (!jb_close(js)) {
89  goto error;
90  }
91 
92  MemBufferReset(thread->buffer);
93  OutputJsonBuilderBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
94  jb_free(js);
95 
96  return TM_ECODE_OK;
97 
98 error:
99  jb_free(js);
100  return TM_ECODE_FAILED;
101 }
102 
103 static void OutputSIPLogDeInitCtxSub(OutputCtx *output_ctx)
104 {
105  LogSIPFileCtx *siplog_ctx = (LogSIPFileCtx *)output_ctx->data;
106  SCFree(siplog_ctx);
107  SCFree(output_ctx);
108 }
109 
110 static OutputInitResult OutputSIPLogInitSub(ConfNode *conf,
111  OutputCtx *parent_ctx)
112 {
113  OutputInitResult result = { NULL, false };
114  OutputJsonCtx *ajt = parent_ctx->data;
115 
116  LogSIPFileCtx *siplog_ctx = SCCalloc(1, sizeof(*siplog_ctx));
117  if (unlikely(siplog_ctx == NULL)) {
118  return result;
119  }
120  siplog_ctx->file_ctx = ajt->file_ctx;
121  siplog_ctx->cfg = ajt->cfg;
122 
123  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
124  if (unlikely(output_ctx == NULL)) {
125  SCFree(siplog_ctx);
126  return result;
127  }
128  output_ctx->data = siplog_ctx;
129  output_ctx->DeInit = OutputSIPLogDeInitCtxSub;
130 
131  SCLogDebug("SIP log sub-module initialized.");
132 
134 
135  result.ctx = output_ctx;
136  result.ok = true;
137  return result;
138 }
139 
140 #define OUTPUT_BUFFER_SIZE 65535
141 
142 static TmEcode JsonSIPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
143 {
144  LogSIPLogThread *thread = SCCalloc(1, sizeof(*thread));
145  if (unlikely(thread == NULL)) {
146  return TM_ECODE_FAILED;
147  }
148 
149  if (initdata == NULL) {
150  SCLogDebug("Error getting context for EveLogSIP. \"initdata\" is NULL.");
151  SCFree(thread);
152  return TM_ECODE_FAILED;
153  }
154 
156  if (unlikely(thread->buffer == NULL)) {
157  SCFree(thread);
158  return TM_ECODE_FAILED;
159  }
160 
161  thread->siplog_ctx = ((OutputCtx *)initdata)->data;
162  *data = (void *)thread;
163 
164  return TM_ECODE_OK;
165 }
166 
167 static TmEcode JsonSIPLogThreadDeinit(ThreadVars *t, void *data)
168 {
169  LogSIPLogThread *thread = (LogSIPLogThread *)data;
170  if (thread == NULL) {
171  return TM_ECODE_OK;
172  }
173  if (thread->buffer != NULL) {
174  MemBufferFree(thread->buffer);
175  }
176  SCFree(thread);
177  return TM_ECODE_OK;
178 }
179 
181 {
182  /* Register as an eve sub-module. */
183  OutputRegisterTxSubModule(LOGGER_JSON_SIP, "eve-log", "JsonSIPLog",
184  "eve-log.sip", OutputSIPLogInitSub, ALPROTO_SIP,
185  JsonSIPLogger, JsonSIPLogThreadInit,
186  JsonSIPLogThreadDeinit, NULL);
187 
188  SCLogDebug("SIP JSON logger registered.");
189 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_::proto
uint8_t proto
Definition: flow.h:361
threads.h
OutputJsonCtx_
Definition: output-json.h:108
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:99
LogFileCtx_
Definition: util-logopenfile.h:52
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:53
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1429
output-json-sip.h
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:673
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1080
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
LOGGER_JSON_SIP
@ LOGGER_JSON_SIP
Definition: suricata-common.h:464
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1284
output-json.h
LogSIPLogThread_::siplog_ctx
LogSIPFileCtx * siplog_ctx
Definition: output-json-sip.c:58
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OUTPUT_BUFFER_SIZE
#define OUTPUT_BUFFER_SIZE
Definition: output-json-sip.c:140
LogSIPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-sip.c:59
LogSIPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-sip.c:54
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
app-layer-sip.h
MemBuffer_
Definition: util-buffer.h:27
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1037
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:38
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LogSIPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-sip.c:53
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
LogSIPLogThread
struct LogSIPLogThread_ LogSIPLogThread
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
JsonSIPLogRegister
void JsonSIPLogRegister(void)
Definition: output-json-sip.c:180
JsonSIPAddMetadata
void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id)
Definition: output-json-sip.c:62
LogSIPFileCtx
struct LogSIPFileCtx_ LogSIPFileCtx
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
LogSIPFileCtx_
Definition: output-json-sip.c:52
LogSIPLogThread_
Definition: output-json-sip.c:57
app-layer.h