suricata
output-json-sip.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 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_ {
62 
63 void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id)
64 {
65  SIPState *state = FlowGetAppState(f);
66  if (state) {
67  SIPTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SIP, state, tx_id);
68  if (tx) {
69  rs_sip_log_json(tx, js);
70  }
71  }
72 }
73 
74 static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
75  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
76 {
77  SIPTransaction *siptx = tx;
78  LogSIPLogThread *thread = thread_data;
79 
80  JsonBuilder *js = CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "sip", NULL);
81  if (unlikely(js == NULL)) {
82  return TM_ECODE_OK;
83  }
84  EveAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
85 
86  if (!rs_sip_log_json(siptx, js)) {
87  goto error;
88  }
89 
90  MemBufferReset(thread->buffer);
91  OutputJsonBuilderBuffer(js, thread->file_ctx, &thread->buffer);
92  jb_free(js);
93 
94  return TM_ECODE_OK;
95 
96 error:
97  jb_free(js);
98  return TM_ECODE_FAILED;
99 }
100 
101 static void OutputSIPLogDeInitCtxSub(OutputCtx *output_ctx)
102 {
103  LogSIPFileCtx *siplog_ctx = (LogSIPFileCtx *)output_ctx->data;
104  SCFree(siplog_ctx);
105  SCFree(output_ctx);
106 }
107 
108 static OutputInitResult OutputSIPLogInitSub(ConfNode *conf,
109  OutputCtx *parent_ctx)
110 {
111  OutputInitResult result = { NULL, false };
112  OutputJsonCtx *ajt = parent_ctx->data;
113 
114  LogSIPFileCtx *siplog_ctx = SCCalloc(1, sizeof(*siplog_ctx));
115  if (unlikely(siplog_ctx == NULL)) {
116  return result;
117  }
118  siplog_ctx->file_ctx = ajt->file_ctx;
119  siplog_ctx->cfg = ajt->cfg;
120 
121  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
122  if (unlikely(output_ctx == NULL)) {
123  SCFree(siplog_ctx);
124  return result;
125  }
126  output_ctx->data = siplog_ctx;
127  output_ctx->DeInit = OutputSIPLogDeInitCtxSub;
128 
129  SCLogDebug("SIP log sub-module initialized.");
130 
132 
133  result.ctx = output_ctx;
134  result.ok = true;
135  return result;
136 }
137 
138 #define OUTPUT_BUFFER_SIZE 65535
139 
140 static TmEcode JsonSIPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
141 {
142  LogSIPLogThread *thread = SCCalloc(1, sizeof(*thread));
143  if (unlikely(thread == NULL)) {
144  return TM_ECODE_FAILED;
145  }
146 
147  if (initdata == NULL) {
148  SCLogDebug("Error getting context for EveLogSIP. \"initdata\" is NULL.");
149  goto error_exit;
150  }
151 
153  if (unlikely(thread->buffer == NULL)) {
154  goto error_exit;
155  }
156 
157  thread->siplog_ctx = ((OutputCtx *)initdata)->data;
158 
159  thread->file_ctx = LogFileEnsureExists(thread->siplog_ctx->file_ctx, t->id);
160  if (!thread->file_ctx) {
161  goto error_exit;
162  }
163  *data = (void *)thread;
164 
165  return TM_ECODE_OK;
166 
167 error_exit:
168  if (thread->buffer != NULL) {
169  MemBufferFree(thread->buffer);
170  }
171  SCFree(thread);
172  return TM_ECODE_FAILED;
173 }
174 
175 static TmEcode JsonSIPLogThreadDeinit(ThreadVars *t, void *data)
176 {
177  LogSIPLogThread *thread = (LogSIPLogThread *)data;
178  if (thread == NULL) {
179  return TM_ECODE_OK;
180  }
181  if (thread->buffer != NULL) {
182  MemBufferFree(thread->buffer);
183  }
184  SCFree(thread);
185  return TM_ECODE_OK;
186 }
187 
189 {
190  /* Register as an eve sub-module. */
191  OutputRegisterTxSubModule(LOGGER_JSON_SIP, "eve-log", "JsonSIPLog",
192  "eve-log.sip", OutputSIPLogInitSub, ALPROTO_SIP,
193  JsonSIPLogger, JsonSIPLogThreadInit,
194  JsonSIPLogThreadDeinit, NULL);
195 
196  SCLogDebug("SIP JSON logger registered.");
197 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
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:365
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonCommonSettings_
Definition: output-json.h:91
LogFileCtx_
Definition: util-logopenfile.h:60
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:53
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
output-json-sip.h
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1102
LogSIPLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-sip.c:58
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
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:846
output-json.h
LogSIPLogThread_::siplog_ctx
LogSIPFileCtx * siplog_ctx
Definition: output-json-sip.c:59
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
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:138
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
LogSIPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-sip.c:60
LogSIPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-sip.c:54
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
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:1032
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
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:409
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:102
LogSIPLogThread
struct LogSIPLogThread_ LogSIPLogThread
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
JsonSIPLogRegister
void JsonSIPLogRegister(void)
Definition: output-json-sip.c:188
JsonSIPAddMetadata
void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id)
Definition: output-json-sip.c:63
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