suricata
output-json-http2.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2021 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 Philippe Antoine <p.antoine@catenacyber.fr>
22  *
23  * Implements HTTP2 JSON logging portion of the engine.
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-print.h"
37 #include "util-unittest.h"
38 
39 #include "util-debug.h"
40 #include "app-layer-parser.h"
41 #include "output.h"
42 #include "app-layer-http2.h"
43 #include "app-layer.h"
44 #include "util-privs.h"
45 #include "util-buffer.h"
46 
47 #include "util-logopenfile.h"
48 #include "util-crypt.h"
49 
50 #include "output-json.h"
51 #include "output-json-http2.h"
52 #include "rust.h"
53 
54 #define MODULE_NAME "LogHttp2Log"
55 
56 typedef struct OutputHttp2Ctx_ {
59 
60 
61 typedef struct JsonHttp2LogThread_ {
66 
67 
68 bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
69 {
70  void *state = FlowGetAppState(f);
71  if (state) {
72  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_HTTP2, state, tx_id);
73  if (tx) {
74  return rs_http2_log_json(tx, jb);
75  }
76  }
77  return false;
78 }
79 
80 static int JsonHttp2Logger(ThreadVars *tv, void *thread_data, const Packet *p,
81  Flow *f, void *state, void *txptr, uint64_t tx_id)
82 {
83  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)thread_data;
84 
85  if (unlikely(state == NULL)) {
86  return 0;
87  }
88 
89  JsonBuilder *js = CreateEveHeaderWithTxId(
90  p, LOG_DIR_FLOW, "http", NULL, tx_id, aft->http2log_ctx->eve_ctx);
91  if (unlikely(js == NULL))
92  return 0;
93 
94  /* reset */
95  MemBufferReset(aft->buffer);
96 
97  jb_open_object(js, "http");
98  if (!rs_http2_log_json(txptr, js)) {
99  goto end;
100  }
101  jb_close(js);
102  OutputJsonBuilderBuffer(js, aft->file_ctx, &aft->buffer);
103 end:
104  jb_free(js);
105  return 0;
106 }
107 
108 static TmEcode JsonHttp2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
109 {
111  if (unlikely(aft == NULL))
112  return TM_ECODE_FAILED;
113 
114  if(initdata == NULL)
115  {
116  SCLogDebug("Error getting context for EveLogHTTP2. \"initdata\" argument NULL");
117  goto error_exit;
118  }
119 
120  /* Use the Output Context (file pointer and mutex) */
121  aft->http2log_ctx = ((OutputCtx *)initdata)->data;
123  if (!aft->file_ctx) {
124  goto error_exit;
125  }
126 
128  if (aft->buffer == NULL) {
129  goto error_exit;
130  }
131 
132  *data = (void *)aft;
133  return TM_ECODE_OK;
134 
135 error_exit:
136  if (aft->buffer != NULL) {
137  MemBufferFree(aft->buffer);
138  }
139  SCFree(aft);
140  return TM_ECODE_FAILED;
141 }
142 
143 static TmEcode JsonHttp2LogThreadDeinit(ThreadVars *t, void *data)
144 {
145  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)data;
146  if (aft == NULL) {
147  return TM_ECODE_OK;
148  }
149 
150  MemBufferFree(aft->buffer);
151  /* clear memory */
152  memset(aft, 0, sizeof(JsonHttp2LogThread));
153 
154  SCFree(aft);
155  return TM_ECODE_OK;
156 }
157 
158 static void OutputHttp2LogDeinitSub(OutputCtx *output_ctx)
159 {
160  OutputHttp2Ctx *http2_ctx = output_ctx->data;
161  SCFree(http2_ctx);
162  SCFree(output_ctx);
163 }
164 
165 static OutputInitResult OutputHttp2LogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
166 {
167  OutputInitResult result = { NULL, false };
168  OutputJsonCtx *ojc = parent_ctx->data;
169 
170  OutputHttp2Ctx *http2_ctx = SCMalloc(sizeof(OutputHttp2Ctx));
171  if (unlikely(http2_ctx == NULL))
172  return result;
173 
174  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
175  if (unlikely(output_ctx == NULL)) {
176  SCFree(http2_ctx);
177  return result;
178  }
179 
180  http2_ctx->eve_ctx = ojc;
181 
182  output_ctx->data = http2_ctx;
183  output_ctx->DeInit = OutputHttp2LogDeinitSub;
184 
186 
187  result.ctx = output_ctx;
188  result.ok = true;
189  return result;
190 }
191 
193 {
194  /* also register as child of eve-log */
196  "eve-log", MODULE_NAME, "eve-log.http2",
197  OutputHttp2LogInitSub, ALPROTO_HTTP2, JsonHttp2Logger,
198  HTTP2StateClosed, HTTP2StateClosed,
199  JsonHttp2LogThreadInit, JsonHttp2LogThreadDeinit, NULL);
200 }
tm-threads.h
LOGGER_JSON_HTTP2
@ LOGGER_JSON_HTTP2
Definition: suricata-common.h:465
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:372
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:350
LogFileCtx_
Definition: util-logopenfile.h:60
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:974
rust.h
util-privs.h
app-layer-http2.h
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
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
JsonHttp2LogThread
struct JsonHttp2LogThread_ JsonHttp2LogThread
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
output-json.h
OutputRegisterTxSubModuleWithProgress
void OutputRegisterTxSubModuleWithProgress(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, int tc_log_progress, int ts_log_progress, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:378
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:457
util-print.h
util-crypt.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:919
Packet_
Definition: decode.h:415
conf.h
EveHTTP2AddMetadata
bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-http2.c:68
TmEcode
TmEcode
Definition: tm-threads-common.h:79
MODULE_NAME
#define MODULE_NAME
Definition: output-json-http2.c:54
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
OutputHttp2Ctx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-http2.c:57
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:1074
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
JsonHttp2LogThread_
Definition: output-json-http2.c:61
JsonHttp2LogThread_::http2log_ctx
OutputHttp2Ctx * http2log_ctx
Definition: output-json-http2.c:62
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
OutputHttp2Ctx_
Definition: output-json-http2.c:56
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
JsonHttp2LogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-http2.c:63
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
output-json-http2.h
JsonHttp2LogRegister
void JsonHttp2LogRegister(void)
Definition: output-json-http2.c:192
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
JsonHttp2LogThread_::buffer
MemBuffer * buffer
Definition: output-json-http2.c:64
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:82
OutputHttp2Ctx
struct OutputHttp2Ctx_ OutputHttp2Ctx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h