suricata
output-json-modbus.c
Go to the documentation of this file.
1 /* Copyright (C) 2019-2020 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 #include "suricata-common.h"
19 #include "detect.h"
20 #include "pkt-var.h"
21 #include "conf.h"
22 #include "threads.h"
23 #include "threadvars.h"
24 #include "tm-threads.h"
25 #include "util-unittest.h"
26 #include "util-buffer.h"
27 #include "util-debug.h"
28 #include "util-byte.h"
29 #include "output.h"
30 #include "output-json.h"
31 #include "app-layer.h"
32 #include "app-layer-parser.h"
33 #include "output-json-modbus.h"
34 #include "rust.h"
35 
36 typedef struct LogModbusFileCtx_ {
40 
41 typedef struct JsonModbusLogThread_ {
45 
46 static int JsonModbusLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
47  void *state, void *tx, uint64_t tx_id)
48 {
49  JsonModbusLogThread *thread = thread_data;
50 
51  JsonBuilder *js =
52  CreateEveHeader(p, LOG_DIR_FLOW, "modbus", NULL, thread->modbuslog_ctx->eve_ctx);
53  if (unlikely(js == NULL)) {
54  return TM_ECODE_OK;
55  }
56  if (!rs_modbus_to_json(tx, js)) {
57  jb_free(js);
58  return TM_ECODE_FAILED;
59  }
60  OutputJsonBuilderBuffer(js, thread->ctx);
61 
62  jb_free(js);
63  return TM_ECODE_OK;
64 }
65 
66 static void OutputModbusLogDeInitCtxSub(OutputCtx *output_ctx)
67 {
68  LogModbusFileCtx *modbuslog_ctx = (LogModbusFileCtx *)output_ctx->data;
69  SCFree(modbuslog_ctx);
70  SCFree(output_ctx);
71 }
72 
73 static OutputInitResult OutputModbusLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
74 {
75  OutputInitResult result = { NULL, false };
76  OutputJsonCtx *ajt = parent_ctx->data;
77 
78  LogModbusFileCtx *modbuslog_ctx = SCCalloc(1, sizeof(*modbuslog_ctx));
79  if (unlikely(modbuslog_ctx == NULL)) {
80  return result;
81  }
82  modbuslog_ctx->file_ctx = ajt->file_ctx;
83  modbuslog_ctx->eve_ctx = ajt;
84 
85  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
86  if (unlikely(output_ctx == NULL)) {
87  SCFree(modbuslog_ctx);
88  return result;
89  }
90  output_ctx->data = modbuslog_ctx;
91  output_ctx->DeInit = OutputModbusLogDeInitCtxSub;
92 
94 
95  SCLogDebug("modbus log sub-module initialized.");
96 
97  result.ctx = output_ctx;
98  result.ok = true;
99  return result;
100 }
101 
102 static TmEcode JsonModbusLogThreadInit(ThreadVars *t, const void *initdata, void **data)
103 {
104  if (initdata == NULL) {
105  SCLogDebug("Error getting context for EveLogModbus. \"initdata\" is NULL.");
106  return TM_ECODE_FAILED;
107  }
108 
109  JsonModbusLogThread *thread = SCCalloc(1, sizeof(*thread));
110  if (unlikely(thread == NULL)) {
111  return TM_ECODE_FAILED;
112  }
113 
114  thread->modbuslog_ctx = ((OutputCtx *)initdata)->data;
115  thread->ctx = CreateEveThreadCtx(t, thread->modbuslog_ctx->eve_ctx);
116  if (thread->ctx == NULL) {
117  goto error_exit;
118  }
119 
120  *data = (void *)thread;
121  return TM_ECODE_OK;
122 
123 error_exit:
124  SCFree(thread);
125  return TM_ECODE_FAILED;
126 }
127 
128 static TmEcode JsonModbusLogThreadDeinit(ThreadVars *t, void *data)
129 {
130  JsonModbusLogThread *thread = (JsonModbusLogThread *)data;
131  if (thread == NULL) {
132  return TM_ECODE_OK;
133  }
134  FreeEveThreadCtx(thread->ctx);
135  SCFree(thread);
136  return TM_ECODE_OK;
137 }
138 
139 bool JsonModbusAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
140 {
141  void *state = FlowGetAppState(f);
142  if (state) {
143  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_MODBUS, state, tx_id);
144  if (tx) {
145  return rs_modbus_to_json(tx, js);
146  }
147  }
148 
149  return false;
150 }
151 
153 {
154  /* Register as an eve sub-module. */
155  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonModbusLog", "eve-log.modbus",
156  OutputModbusLogInitSub, ALPROTO_MODBUS, JsonModbusLogger, JsonModbusLogThreadInit,
157  JsonModbusLogThreadDeinit, NULL);
158 
159  SCLogDebug("modbus json logger registered.");
160 }
util-byte.h
tm-threads.h
output-json-modbus.h
LogModbusFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-modbus.c:38
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonModbusLogThread
struct JsonModbusLogThread_ JsonModbusLogThread
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
ALPROTO_MODBUS
@ ALPROTO_MODBUS
Definition: app-layer-protos.h:42
Flow_::proto
uint8_t proto
Definition: flow.h:378
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
JsonModbusAddMetadata
bool JsonModbusAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-modbus.c:139
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
LogFileCtx_
Definition: util-logopenfile.h:65
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
JsonModbusLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-modbus.c:43
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
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:82
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
JsonModbusLogThread_::modbuslog_ctx
LogModbusFileCtx * modbuslog_ctx
Definition: output-json-modbus.c:42
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
LogModbusFileCtx
struct LogModbusFileCtx_ LogModbusFileCtx
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:479
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:796
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
Packet_
Definition: decode.h:425
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:81
JsonModbusLogThread_
Definition: output-json-modbus.c:41
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1148
OutputInitResult_
Definition: output.h:46
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
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:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
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
LogModbusFileCtx_
Definition: output-json-modbus.c:36
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:82
JsonModbusLogRegister
void JsonModbusLogRegister(void)
Definition: output-json-modbus.c:152
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h
LogModbusFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-modbus.c:37