Coverage Report

Created: 2025-11-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/output-json-modbus.c
Line
Count
Source
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_ {
37
    LogFileCtx *file_ctx;
38
    OutputJsonCtx *eve_ctx;
39
} LogModbusFileCtx;
40
41
typedef struct JsonModbusLogThread_ {
42
    LogModbusFileCtx *modbuslog_ctx;
43
    OutputJsonThreadCtx *ctx;
44
} 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
0
{
49
0
    JsonModbusLogThread *thread = thread_data;
50
51
0
    JsonBuilder *js =
52
0
            CreateEveHeader(p, LOG_DIR_FLOW, "modbus", NULL, thread->modbuslog_ctx->eve_ctx);
53
0
    if (unlikely(js == NULL)) {
54
0
        return TM_ECODE_OK;
55
0
    }
56
0
    if (!rs_modbus_to_json(tx, js)) {
57
0
        jb_free(js);
58
0
        return TM_ECODE_FAILED;
59
0
    }
60
0
    OutputJsonBuilderBuffer(js, thread->ctx);
61
62
0
    jb_free(js);
63
0
    return TM_ECODE_OK;
64
0
}
65
66
static void OutputModbusLogDeInitCtxSub(OutputCtx *output_ctx)
67
0
{
68
0
    LogModbusFileCtx *modbuslog_ctx = (LogModbusFileCtx *)output_ctx->data;
69
0
    SCFree(modbuslog_ctx);
70
0
    SCFree(output_ctx);
71
0
}
72
73
static OutputInitResult OutputModbusLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
74
0
{
75
0
    OutputInitResult result = { NULL, false };
76
0
    OutputJsonCtx *ajt = parent_ctx->data;
77
78
0
    LogModbusFileCtx *modbuslog_ctx = SCCalloc(1, sizeof(*modbuslog_ctx));
79
0
    if (unlikely(modbuslog_ctx == NULL)) {
80
0
        return result;
81
0
    }
82
0
    modbuslog_ctx->file_ctx = ajt->file_ctx;
83
0
    modbuslog_ctx->eve_ctx = ajt;
84
85
0
    OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
86
0
    if (unlikely(output_ctx == NULL)) {
87
0
        SCFree(modbuslog_ctx);
88
0
        return result;
89
0
    }
90
0
    output_ctx->data = modbuslog_ctx;
91
0
    output_ctx->DeInit = OutputModbusLogDeInitCtxSub;
92
93
0
    AppLayerParserRegisterLogger(IPPROTO_TCP, ALPROTO_MODBUS);
94
95
0
    SCLogDebug("modbus log sub-module initialized.");
96
97
0
    result.ctx = output_ctx;
98
0
    result.ok = true;
99
0
    return result;
100
0
}
101
102
static TmEcode JsonModbusLogThreadInit(ThreadVars *t, const void *initdata, void **data)
103
0
{
104
0
    if (initdata == NULL) {
105
0
        SCLogDebug("Error getting context for EveLogModbus. \"initdata\" is NULL.");
106
0
        return TM_ECODE_FAILED;
107
0
    }
108
109
0
    JsonModbusLogThread *thread = SCCalloc(1, sizeof(*thread));
110
0
    if (unlikely(thread == NULL)) {
111
0
        return TM_ECODE_FAILED;
112
0
    }
113
114
0
    thread->modbuslog_ctx = ((OutputCtx *)initdata)->data;
115
0
    thread->ctx = CreateEveThreadCtx(t, thread->modbuslog_ctx->eve_ctx);
116
0
    if (thread->ctx == NULL) {
117
0
        goto error_exit;
118
0
    }
119
120
0
    *data = (void *)thread;
121
0
    return TM_ECODE_OK;
122
123
0
error_exit:
124
0
    SCFree(thread);
125
0
    return TM_ECODE_FAILED;
126
0
}
127
128
static TmEcode JsonModbusLogThreadDeinit(ThreadVars *t, void *data)
129
0
{
130
0
    JsonModbusLogThread *thread = (JsonModbusLogThread *)data;
131
0
    if (thread == NULL) {
132
0
        return TM_ECODE_OK;
133
0
    }
134
0
    FreeEveThreadCtx(thread->ctx);
135
0
    SCFree(thread);
136
0
    return TM_ECODE_OK;
137
0
}
138
139
bool JsonModbusAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
140
9.32k
{
141
9.32k
    void *state = FlowGetAppState(f);
142
9.32k
    if (state) {
143
9.32k
        void *tx = AppLayerParserGetTx(f->proto, ALPROTO_MODBUS, state, tx_id);
144
9.32k
        if (tx) {
145
9.22k
            return rs_modbus_to_json(tx, js);
146
9.22k
        }
147
9.32k
    }
148
149
100
    return false;
150
9.32k
}
151
152
void JsonModbusLogRegister(void)
153
33
{
154
    /* Register as an eve sub-module. */
155
33
    OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonModbusLog", "eve-log.modbus",
156
33
            OutputModbusLogInitSub, ALPROTO_MODBUS, JsonModbusLogger, JsonModbusLogThreadInit,
157
33
            JsonModbusLogThreadDeinit, NULL);
158
159
33
    SCLogDebug("modbus json logger registered.");
160
33
}