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_ {
65 
66 
67 bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
68 {
69  void *state = FlowGetAppState(f);
70  if (state) {
71  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_HTTP2, state, tx_id);
72  if (tx) {
73  return rs_http2_log_json(tx, jb);
74  }
75  }
76  return false;
77 }
78 
79 static int JsonHttp2Logger(ThreadVars *tv, void *thread_data, const Packet *p,
80  Flow *f, void *state, void *txptr, uint64_t tx_id)
81 {
82  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)thread_data;
83 
84  if (unlikely(state == NULL)) {
85  return 0;
86  }
87 
88  JsonBuilder *js = CreateEveHeaderWithTxId(
89  p, LOG_DIR_FLOW, "http", NULL, tx_id, aft->http2log_ctx->eve_ctx);
90  if (unlikely(js == NULL))
91  return 0;
92 
93  jb_open_object(js, "http");
94  if (!rs_http2_log_json(txptr, js)) {
95  goto end;
96  }
97  jb_close(js);
98  OutputJsonBuilderBuffer(js, aft->ctx);
99 end:
100  jb_free(js);
101  return 0;
102 }
103 
104 static TmEcode JsonHttp2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
105 {
107  if (unlikely(aft == NULL))
108  return TM_ECODE_FAILED;
109 
110  if(initdata == NULL)
111  {
112  SCLogDebug("Error getting context for EveLogHTTP2. \"initdata\" argument NULL");
113  goto error_exit;
114  }
115 
116  /* Use the Output Context (file pointer and mutex) */
117  aft->http2log_ctx = ((OutputCtx *)initdata)->data;
118  aft->ctx = CreateEveThreadCtx(t, aft->http2log_ctx->eve_ctx);
119  if (!aft->ctx) {
120  goto error_exit;
121  }
122 
123  *data = (void *)aft;
124  return TM_ECODE_OK;
125 
126 error_exit:
127  SCFree(aft);
128  return TM_ECODE_FAILED;
129 }
130 
131 static TmEcode JsonHttp2LogThreadDeinit(ThreadVars *t, void *data)
132 {
133  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)data;
134  if (aft == NULL) {
135  return TM_ECODE_OK;
136  }
137 
138  FreeEveThreadCtx(aft->ctx);
139  /* clear memory */
140  memset(aft, 0, sizeof(JsonHttp2LogThread));
141 
142  SCFree(aft);
143  return TM_ECODE_OK;
144 }
145 
146 static void OutputHttp2LogDeinitSub(OutputCtx *output_ctx)
147 {
148  OutputHttp2Ctx *http2_ctx = output_ctx->data;
149  SCFree(http2_ctx);
150  SCFree(output_ctx);
151 }
152 
153 static OutputInitResult OutputHttp2LogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
154 {
155  OutputInitResult result = { NULL, false };
156  OutputJsonCtx *ojc = parent_ctx->data;
157 
158  OutputHttp2Ctx *http2_ctx = SCMalloc(sizeof(OutputHttp2Ctx));
159  if (unlikely(http2_ctx == NULL))
160  return result;
161 
162  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
163  if (unlikely(output_ctx == NULL)) {
164  SCFree(http2_ctx);
165  return result;
166  }
167 
168  http2_ctx->eve_ctx = ojc;
169 
170  output_ctx->data = http2_ctx;
171  output_ctx->DeInit = OutputHttp2LogDeinitSub;
172 
174 
175  result.ctx = output_ctx;
176  result.ok = true;
177  return result;
178 }
179 
181 {
182  /* also register as child of eve-log */
184  "eve-log", MODULE_NAME, "eve-log.http2",
185  OutputHttp2LogInitSub, ALPROTO_HTTP2, JsonHttp2Logger,
186  HTTP2StateClosed, HTTP2StateClosed,
187  JsonHttp2LogThreadInit, JsonHttp2LogThreadDeinit, NULL);
188 }
tm-threads.h
LOGGER_JSON_HTTP2
@ LOGGER_JSON_HTTP2
Definition: suricata-common.h:466
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonHttp2LogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-http2.c:63
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
Flow_::proto
uint8_t proto
Definition: flow.h:372
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:974
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
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
OutputJsonThreadCtx_
Definition: output-json.h:89
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:379
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:455
util-print.h
util-crypt.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
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:919
Packet_
Definition: decode.h:416
conf.h
EveHTTP2AddMetadata
bool EveHTTP2AddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-http2.c:67
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
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1074
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
OutputHttp2Ctx_
Definition: output-json-http2.c:56
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
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:180
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
OutputHttp2Ctx
struct OutputHttp2Ctx_ OutputHttp2Ctx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
debug.h
output.h
app-layer.h