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 "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 #include "app-layer-parser.h"
40 #include "output.h"
41 #include "app-layer-http2.h"
42 #include "app-layer.h"
43 #include "util-privs.h"
44 #include "util-buffer.h"
45 
46 #include "util-logopenfile.h"
47 
48 #include "output-json.h"
49 #include "output-json-http2.h"
50 #include "rust.h"
51 
52 #define MODULE_NAME "LogHttp2Log"
53 
54 typedef struct OutputHttp2Ctx_ {
57 
58 
59 typedef struct JsonHttp2LogThread_ {
63 
64 
65 bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
66 {
67  void *state = FlowGetAppState(f);
68  if (state) {
69  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_HTTP2, state, tx_id);
70  if (tx) {
71  return rs_http2_log_json(tx, jb);
72  }
73  }
74  return false;
75 }
76 
77 static int JsonHttp2Logger(ThreadVars *tv, void *thread_data, const Packet *p,
78  Flow *f, void *state, void *txptr, uint64_t tx_id)
79 {
80  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)thread_data;
81 
82  if (unlikely(state == NULL)) {
83  return 0;
84  }
85 
86  JsonBuilder *js = CreateEveHeaderWithTxId(
87  p, LOG_DIR_FLOW, "http", NULL, tx_id, aft->http2log_ctx->eve_ctx);
88  if (unlikely(js == NULL))
89  return 0;
90 
91  jb_open_object(js, "http");
92  if (!rs_http2_log_json(txptr, js)) {
93  goto end;
94  }
95  jb_close(js);
96  OutputJsonBuilderBuffer(js, aft->ctx);
97 end:
98  jb_free(js);
99  return 0;
100 }
101 
102 static TmEcode JsonHttp2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
103 {
105  if (unlikely(aft == NULL))
106  return TM_ECODE_FAILED;
107 
108  if(initdata == NULL)
109  {
110  SCLogDebug("Error getting context for EveLogHTTP2. \"initdata\" argument NULL");
111  goto error_exit;
112  }
113 
114  /* Use the Output Context (file pointer and mutex) */
115  aft->http2log_ctx = ((OutputCtx *)initdata)->data;
116  aft->ctx = CreateEveThreadCtx(t, aft->http2log_ctx->eve_ctx);
117  if (!aft->ctx) {
118  goto error_exit;
119  }
120 
121  *data = (void *)aft;
122  return TM_ECODE_OK;
123 
124 error_exit:
125  SCFree(aft);
126  return TM_ECODE_FAILED;
127 }
128 
129 static TmEcode JsonHttp2LogThreadDeinit(ThreadVars *t, void *data)
130 {
131  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)data;
132  if (aft == NULL) {
133  return TM_ECODE_OK;
134  }
135 
136  FreeEveThreadCtx(aft->ctx);
137  /* clear memory */
138  memset(aft, 0, sizeof(JsonHttp2LogThread));
139 
140  SCFree(aft);
141  return TM_ECODE_OK;
142 }
143 
144 static void OutputHttp2LogDeinitSub(OutputCtx *output_ctx)
145 {
146  OutputHttp2Ctx *http2_ctx = output_ctx->data;
147  SCFree(http2_ctx);
148  SCFree(output_ctx);
149 }
150 
151 static OutputInitResult OutputHttp2LogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
152 {
153  OutputInitResult result = { NULL, false };
154  OutputJsonCtx *ojc = parent_ctx->data;
155 
156  OutputHttp2Ctx *http2_ctx = SCMalloc(sizeof(OutputHttp2Ctx));
157  if (unlikely(http2_ctx == NULL))
158  return result;
159 
160  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
161  if (unlikely(output_ctx == NULL)) {
162  SCFree(http2_ctx);
163  return result;
164  }
165 
166  http2_ctx->eve_ctx = ojc;
167 
168  output_ctx->data = http2_ctx;
169  output_ctx->DeInit = OutputHttp2LogDeinitSub;
170 
172 
173  result.ctx = output_ctx;
174  result.ok = true;
175  return result;
176 }
177 
179 {
180  /* also register as child of eve-log */
182  OutputHttp2LogInitSub, ALPROTO_HTTP2, JsonHttp2Logger, HTTP2StateClosed,
183  HTTP2StateClosed, JsonHttp2LogThreadInit, JsonHttp2LogThreadDeinit, NULL);
184 }
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonHttp2LogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-http2.c:61
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
Flow_::proto
uint8_t proto
Definition: flow.h:378
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:934
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
util-privs.h
app-layer-http2.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1148
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
JsonHttp2LogThread
struct JsonHttp2LogThread_ JsonHttp2LogThread
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
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:380
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:477
util-print.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
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:879
Packet_
Definition: decode.h:428
conf.h
EveHTTP2AddMetadata
bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-http2.c:65
TmEcode
TmEcode
Definition: tm-threads-common.h:83
MODULE_NAME
#define MODULE_NAME
Definition: output-json-http2.c:52
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
OutputHttp2Ctx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-http2.c:55
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1146
OutputInitResult_
Definition: output.h:46
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
JsonHttp2LogThread_
Definition: output-json-http2.c:59
JsonHttp2LogThread_::http2log_ctx
OutputHttp2Ctx * http2log_ctx
Definition: output-json-http2.c:60
OutputHttp2Ctx_
Definition: output-json-http2.c:54
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
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:178
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:456
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
OutputHttp2Ctx
struct OutputHttp2Ctx_ OutputHttp2Ctx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h