/src/suricata7/src/output-json-quic.c
Line | Count | Source |
1 | | /* Copyright (C) 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 | | * Implements JSON/eve logging for Quic app-layer. |
22 | | */ |
23 | | |
24 | | #include "suricata-common.h" |
25 | | #include "detect.h" |
26 | | #include "pkt-var.h" |
27 | | #include "conf.h" |
28 | | #include "threads.h" |
29 | | #include "threadvars.h" |
30 | | #include "tm-threads.h" |
31 | | #include "util-unittest.h" |
32 | | #include "util-buffer.h" |
33 | | #include "util-debug.h" |
34 | | #include "util-byte.h" |
35 | | #include "output.h" |
36 | | #include "output-json.h" |
37 | | #include "app-layer.h" |
38 | | #include "app-layer-ssl.h" |
39 | | #include "app-layer-parser.h" |
40 | | #include "output-json-quic.h" |
41 | | #include "rust.h" |
42 | | |
43 | | typedef struct LogQuicFileCtx_ { |
44 | | LogFileCtx *file_ctx; |
45 | | OutputJsonCtx *eve_ctx; |
46 | | bool log_ja4; |
47 | | } LogQuicFileCtx; |
48 | | |
49 | | typedef struct JsonQuicLogThread_ { |
50 | | LogQuicFileCtx *quiclog_ctx; |
51 | | OutputJsonThreadCtx *ctx; |
52 | | } JsonQuicLogThread; |
53 | | |
54 | | static int JsonQuicLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, |
55 | | void *tx, uint64_t tx_id) |
56 | 0 | { |
57 | 0 | JsonQuicLogThread *thread = thread_data; |
58 | |
|
59 | 0 | JsonBuilder *js = |
60 | 0 | CreateEveHeader(p, LOG_DIR_PACKET, "quic", NULL, thread->quiclog_ctx->eve_ctx); |
61 | 0 | if (unlikely(js == NULL)) { |
62 | 0 | return TM_ECODE_OK; |
63 | 0 | } |
64 | | |
65 | 0 | LogQuicFileCtx *quic_ctx = thread->quiclog_ctx; |
66 | 0 | if (!rs_quic_to_json(tx, quic_ctx->log_ja4, js)) { |
67 | 0 | jb_free(js); |
68 | 0 | return TM_ECODE_FAILED; |
69 | 0 | } |
70 | 0 | OutputJsonBuilderBuffer(js, thread->ctx); |
71 | |
|
72 | 0 | jb_free(js); |
73 | 0 | return TM_ECODE_OK; |
74 | 0 | } |
75 | | |
76 | | static void OutputQuicLogDeInitCtxSub(OutputCtx *output_ctx) |
77 | 0 | { |
78 | 0 | LogQuicFileCtx *quiclog_ctx = (LogQuicFileCtx *)output_ctx->data; |
79 | 0 | SCFree(quiclog_ctx); |
80 | 0 | SCFree(output_ctx); |
81 | 0 | } |
82 | | |
83 | | static OutputInitResult OutputQuicLogInitSub(ConfNode *conf, OutputCtx *parent_ctx) |
84 | 0 | { |
85 | 0 | OutputInitResult result = { NULL, false }; |
86 | 0 | OutputJsonCtx *ajt = parent_ctx->data; |
87 | |
|
88 | 0 | LogQuicFileCtx *quiclog_ctx = SCCalloc(1, sizeof(*quiclog_ctx)); |
89 | 0 | if (unlikely(quiclog_ctx == NULL)) { |
90 | 0 | return result; |
91 | 0 | } |
92 | 0 | quiclog_ctx->file_ctx = ajt->file_ctx; |
93 | 0 | quiclog_ctx->eve_ctx = ajt; |
94 | |
|
95 | 0 | OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx)); |
96 | 0 | if (unlikely(output_ctx == NULL)) { |
97 | 0 | SCFree(quiclog_ctx); |
98 | 0 | return result; |
99 | 0 | } |
100 | | |
101 | | /* In 7.0.x, ja4 hash is only logged when requested */ |
102 | 0 | quiclog_ctx->log_ja4 = false; |
103 | 0 | const char *ja4 = ConfNodeLookupChildValue(conf, "ja4"); |
104 | 0 | if (ja4 && ConfValIsTrue(ja4)) { |
105 | 0 | quiclog_ctx->log_ja4 = true; |
106 | 0 | } |
107 | 0 | output_ctx->data = quiclog_ctx; |
108 | 0 | output_ctx->DeInit = OutputQuicLogDeInitCtxSub; |
109 | |
|
110 | 0 | AppLayerParserRegisterLogger(IPPROTO_UDP, ALPROTO_QUIC); |
111 | |
|
112 | 0 | result.ctx = output_ctx; |
113 | 0 | result.ok = true; |
114 | 0 | return result; |
115 | 0 | } |
116 | | |
117 | | static TmEcode JsonQuicLogThreadInit(ThreadVars *t, const void *initdata, void **data) |
118 | 0 | { |
119 | 0 | if (initdata == NULL) { |
120 | 0 | SCLogDebug("Error getting context for EveLogQuic. \"initdata\" is NULL."); |
121 | 0 | return TM_ECODE_FAILED; |
122 | 0 | } |
123 | | |
124 | 0 | JsonQuicLogThread *thread = SCCalloc(1, sizeof(*thread)); |
125 | 0 | if (unlikely(thread == NULL)) { |
126 | 0 | return TM_ECODE_FAILED; |
127 | 0 | } |
128 | | |
129 | 0 | thread->quiclog_ctx = ((OutputCtx *)initdata)->data; |
130 | 0 | thread->ctx = CreateEveThreadCtx(t, thread->quiclog_ctx->eve_ctx); |
131 | 0 | if (thread->ctx == NULL) { |
132 | 0 | goto error_exit; |
133 | 0 | } |
134 | | |
135 | 0 | *data = (void *)thread; |
136 | 0 | return TM_ECODE_OK; |
137 | | |
138 | 0 | error_exit: |
139 | 0 | SCFree(thread); |
140 | 0 | return TM_ECODE_FAILED; |
141 | 0 | } |
142 | | |
143 | | static TmEcode JsonQuicLogThreadDeinit(ThreadVars *t, void *data) |
144 | 0 | { |
145 | 0 | JsonQuicLogThread *thread = (JsonQuicLogThread *)data; |
146 | 0 | if (thread == NULL) { |
147 | 0 | return TM_ECODE_OK; |
148 | 0 | } |
149 | 0 | FreeEveThreadCtx(thread->ctx); |
150 | 0 | SCFree(thread); |
151 | 0 | return TM_ECODE_OK; |
152 | 0 | } |
153 | | |
154 | | bool JsonQuicAddMetadata(const Flow *f, const uint32_t sig_flags, uint64_t tx_id, JsonBuilder *js) |
155 | 2.93k | { |
156 | 2.93k | void *state = FlowGetAppState(f); |
157 | 2.93k | if (state) { |
158 | 2.93k | void *tx = AppLayerParserGetTx(f->proto, ALPROTO_QUIC, state, tx_id); |
159 | 2.93k | if (tx) { |
160 | 2.93k | return rs_quic_to_json(tx, sig_flags & SIG_FLAG_JA4, js); |
161 | 2.93k | } |
162 | 2.93k | } |
163 | | |
164 | 0 | return false; |
165 | 2.93k | } |
166 | | |
167 | | void JsonQuicLogRegister(void) |
168 | 33 | { |
169 | | /* Register as an eve sub-module. */ |
170 | 33 | OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonQuicLog", "eve-log.quic", |
171 | 33 | OutputQuicLogInitSub, ALPROTO_QUIC, JsonQuicLogger, JsonQuicLogThreadInit, |
172 | 33 | JsonQuicLogThreadDeinit, NULL); |
173 | | |
174 | 33 | SCLogDebug("quic json logger registered."); |
175 | 33 | } |