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 "debug.h"
20 #include "detect.h"
21 #include "pkt-var.h"
22 #include "conf.h"
23 #include "threads.h"
24 #include "threadvars.h"
25 #include "tm-threads.h"
26 #include "util-unittest.h"
27 #include "util-buffer.h"
28 #include "util-debug.h"
29 #include "util-byte.h"
30 #include "output.h"
31 #include "output-json.h"
32 #include "app-layer.h"
33 #include "app-layer-parser.h"
34 #include "output-json-modbus.h"
35 #include "rust.h"
36 
37 typedef struct LogModbusFileCtx_ {
41 
42 typedef struct JsonModbusLogThread_ {
46 
47 static int JsonModbusLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
48  void *state, void *tx, uint64_t tx_id)
49 {
50  JsonModbusLogThread *thread = thread_data;
51 
52  JsonBuilder *js =
53  CreateEveHeader(p, LOG_DIR_FLOW, "modbus", NULL, thread->modbuslog_ctx->eve_ctx);
54  if (unlikely(js == NULL)) {
55  return TM_ECODE_OK;
56  }
57  if (!rs_modbus_to_json(tx, js)) {
58  jb_free(js);
59  return TM_ECODE_FAILED;
60  }
61  OutputJsonBuilderBuffer(js, thread->ctx);
62 
63  jb_free(js);
64  return TM_ECODE_OK;
65 }
66 
67 static void OutputModbusLogDeInitCtxSub(OutputCtx *output_ctx)
68 {
69  LogModbusFileCtx *modbuslog_ctx = (LogModbusFileCtx *)output_ctx->data;
70  SCFree(modbuslog_ctx);
71  SCFree(output_ctx);
72 }
73 
74 static OutputInitResult OutputModbusLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
75 {
76  OutputInitResult result = { NULL, false };
77  OutputJsonCtx *ajt = parent_ctx->data;
78 
79  LogModbusFileCtx *modbuslog_ctx = SCCalloc(1, sizeof(*modbuslog_ctx));
80  if (unlikely(modbuslog_ctx == NULL)) {
81  return result;
82  }
83  modbuslog_ctx->file_ctx = ajt->file_ctx;
84  modbuslog_ctx->eve_ctx = ajt;
85 
86  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
87  if (unlikely(output_ctx == NULL)) {
88  SCFree(modbuslog_ctx);
89  return result;
90  }
91  output_ctx->data = modbuslog_ctx;
92  output_ctx->DeInit = OutputModbusLogDeInitCtxSub;
93 
95 
96  SCLogDebug("modbus log sub-module initialized.");
97 
98  result.ctx = output_ctx;
99  result.ok = true;
100  return result;
101 }
102 
103 static TmEcode JsonModbusLogThreadInit(ThreadVars *t, const void *initdata, void **data)
104 {
105  if (initdata == NULL) {
106  SCLogDebug("Error getting context for EveLogModbus. \"initdata\" is NULL.");
107  return TM_ECODE_FAILED;
108  }
109 
110  JsonModbusLogThread *thread = SCCalloc(1, sizeof(*thread));
111  if (unlikely(thread == NULL)) {
112  return TM_ECODE_FAILED;
113  }
114 
115  thread->modbuslog_ctx = ((OutputCtx *)initdata)->data;
116  thread->ctx = CreateEveThreadCtx(t, thread->modbuslog_ctx->eve_ctx);
117  if (thread->ctx == NULL) {
118  goto error_exit;
119  }
120 
121  *data = (void *)thread;
122  return TM_ECODE_OK;
123 
124 error_exit:
125  SCFree(thread);
126  return TM_ECODE_FAILED;
127 }
128 
129 static TmEcode JsonModbusLogThreadDeinit(ThreadVars *t, void *data)
130 {
131  JsonModbusLogThread *thread = (JsonModbusLogThread *)data;
132  if (thread == NULL) {
133  return TM_ECODE_OK;
134  }
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_MODBUS, "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:39
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonModbusLogThread
struct JsonModbusLogThread_ JsonModbusLogThread
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
ALPROTO_MODBUS
@ ALPROTO_MODBUS
Definition: app-layer-protos.h:42
Flow_::proto
uint8_t proto
Definition: flow.h:372
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:350
LogFileCtx_
Definition: util-logopenfile.h:62
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
JsonModbusLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-modbus.c:44
rust.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
JsonModbusLogThread_::modbuslog_ctx
LogModbusFileCtx * modbuslog_ctx
Definition: output-json-modbus.c:43
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
LogModbusFileCtx
struct LogModbusFileCtx_ LogModbusFileCtx
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:455
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:838
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
Packet_
Definition: decode.h:416
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
JsonModbusLogThread_
Definition: output-json-modbus.c:42
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
LOGGER_JSON_MODBUS
@ LOGGER_JSON_MODBUS
Definition: suricata-common.h:456
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:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
LogModbusFileCtx_
Definition: output-json-modbus.c:37
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
debug.h
output.h
app-layer.h
LogModbusFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-modbus.c:38