suricata
output-json-pgsql.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 Juliana Fajardini <jufajardini@oisf.net>
22  *
23  * Implement JSON/eve logging for app-layer Pgsql.
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 "output-json-pgsql.h"
48 #include "rust.h"
49 
50 #define PGSQL_LOG_PASSWORDS BIT_U32(0)
51 
52 typedef struct OutputPgsqlCtx_ {
53  uint32_t flags;
56 
57 typedef struct LogPgsqlLogThread_ {
61 
62 static int JsonPgsqlLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
63  void *txptr, uint64_t tx_id)
64 {
65  LogPgsqlLogThread *thread = thread_data;
66  SCLogDebug("Logging pgsql transaction %" PRIu64 ".", tx_id);
67 
68  JsonBuilder *jb =
69  CreateEveHeader(p, LOG_DIR_FLOW, "pgsql", NULL, thread->pgsqllog_ctx->eve_ctx);
70  if (unlikely(jb == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  jb_open_object(jb, "pgsql");
75  if (!rs_pgsql_logger(txptr, thread->pgsqllog_ctx->flags, jb)) {
76  goto error;
77  }
78  jb_close(jb);
79 
80  OutputJsonBuilderBuffer(jb, thread->ctx);
81  jb_free(jb);
82 
83  return TM_ECODE_OK;
84 
85 error:
86  jb_free(jb);
87  return TM_ECODE_FAILED;
88 }
89 
90 static void OutputPgsqlLogDeInitCtxSub(OutputCtx *output_ctx)
91 {
92  OutputPgsqlCtx *pgsqllog_ctx = (OutputPgsqlCtx *)output_ctx->data;
93  SCFree(pgsqllog_ctx);
94  SCFree(output_ctx);
95 }
96 
97 static void JsonPgsqlLogParseConfig(ConfNode *conf, OutputPgsqlCtx *pgsqllog_ctx)
98 {
99  pgsqllog_ctx->flags = ~0U;
100 
101  const char *query = ConfNodeLookupChildValue(conf, "passwords");
102  if (query != NULL) {
103  if (ConfValIsTrue(query)) {
104  pgsqllog_ctx->flags |= PGSQL_LOG_PASSWORDS;
105  } else {
106  pgsqllog_ctx->flags &= ~PGSQL_LOG_PASSWORDS;
107  }
108  } else {
109  pgsqllog_ctx->flags &= ~PGSQL_LOG_PASSWORDS;
110  }
111 }
112 
113 static OutputInitResult OutputPgsqlLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
114 {
115  OutputInitResult result = { NULL, false };
116  OutputJsonCtx *ojc = parent_ctx->data;
117 
118  OutputPgsqlCtx *pgsql_ctx = SCMalloc(sizeof(OutputPgsqlCtx));
119  if (unlikely(pgsql_ctx == NULL))
120  return result;
121 
122  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
123  if (unlikely(output_ctx == NULL)) {
124  SCFree(pgsql_ctx);
125  return result;
126  }
127 
128  pgsql_ctx->eve_ctx = ojc;
129 
130  output_ctx->data = pgsql_ctx;
131  output_ctx->DeInit = OutputPgsqlLogDeInitCtxSub;
132 
133  JsonPgsqlLogParseConfig(conf, pgsql_ctx);
134 
136 
137  SCLogDebug("PostgreSQL log sub-module initialized.");
138 
139  result.ctx = output_ctx;
140  result.ok = true;
141  return result;
142 }
143 
144 static TmEcode JsonPgsqlLogThreadInit(ThreadVars *t, const void *initdata, void **data)
145 {
146  LogPgsqlLogThread *thread = SCCalloc(1, sizeof(LogPgsqlLogThread));
147  if (unlikely(thread == NULL)) {
148  return TM_ECODE_FAILED;
149  }
150 
151  if (initdata == NULL) {
152  SCLogDebug("Error getting context for EveLogPgsql. \"initdata\" is NULL.");
153  goto error_exit;
154  }
155 
156  thread->pgsqllog_ctx = ((OutputCtx *)initdata)->data;
157  thread->ctx = CreateEveThreadCtx(t, thread->pgsqllog_ctx->eve_ctx);
158  if (!thread->ctx) {
159  goto error_exit;
160  }
161  *data = (void *)thread;
162 
163  return TM_ECODE_OK;
164 
165 error_exit:
166  SCFree(thread);
167  return TM_ECODE_FAILED;
168 }
169 
170 static TmEcode JsonPgsqlLogThreadDeinit(ThreadVars *t, void *data)
171 {
172  LogPgsqlLogThread *thread = (LogPgsqlLogThread *)data;
173  if (thread == NULL) {
174  return TM_ECODE_OK;
175  }
176  FreeEveThreadCtx(thread->ctx);
177  SCFree(thread);
178  return TM_ECODE_OK;
179 }
180 
182 {
183  /* PGSQL_START_REMOVE */
184  if (ConfGetNode("app-layer.protocols.pgsql") == NULL) {
185  SCLogDebug("Disabling Pgsql eve-logger");
186  return;
187  }
188  /* PGSQL_END_REMOVE */
189  /* Register as an eve sub-module. */
190  OutputRegisterTxSubModule(LOGGER_JSON_PGSQL, "eve-log", "JsonPgsqlLog", "eve-log.pgsql",
191  OutputPgsqlLogInitSub, ALPROTO_PGSQL, JsonPgsqlLogger, JsonPgsqlLogThreadInit,
192  JsonPgsqlLogThreadDeinit, NULL);
193 
194  SCLogDebug("PostgreSQL JSON logger registered.");
195 }
util-byte.h
tm-threads.h
PGSQL_LOG_PASSWORDS
#define PGSQL_LOG_PASSWORDS
Definition: output-json-pgsql.c:50
JsonPgsqlLogRegister
void JsonPgsqlLogRegister(void)
Definition: output-json-pgsql.c:181
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:353
output-json-pgsql.h
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:960
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
rust.h
LogPgsqlLogThread_::pgsqllog_ctx
OutputPgsqlCtx * pgsqllog_ctx
Definition: output-json-pgsql.c:58
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest.h
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:521
OutputPgsqlCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-pgsql.c:54
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
LogPgsqlLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-pgsql.c:59
util-debug.h
LogPgsqlLogThread_
Definition: output-json-pgsql.c:57
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:45
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:492
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:822
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:56
pkt-var.h
LOGGER_JSON_PGSQL
@ LOGGER_JSON_PGSQL
Definition: suricata-common.h:465
OutputInitResult_::ok
bool ok
Definition: output.h:46
LogPgsqlLogThread
struct LogPgsqlLogThread_ LogPgsqlLogThread
app-layer-parser.h
Packet_
Definition: decode.h:433
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:81
OutputPgsqlCtx_
Definition: output-json-pgsql.c:52
OutputPgsqlCtx_::flags
uint32_t flags
Definition: output-json-pgsql.c:53
OutputInitResult_
Definition: output.h:44
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
ALPROTO_PGSQL
@ ALPROTO_PGSQL
Definition: app-layer-protos.h:57
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:412
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:31
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputPgsqlCtx
struct OutputPgsqlCtx_ OutputPgsqlCtx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
debug.h
output.h
app-layer.h
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:798