suricata
output-json-dnp3.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-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 #include "suricata-common.h"
19 #include "debug.h"
20 #include "detect.h"
21 #include "pkt-var.h"
22 #include "conf.h"
23 
24 #include "threads.h"
25 #include "threadvars.h"
26 #include "tm-threads.h"
27 
28 #include "util-print.h"
29 #include "util-unittest.h"
30 #include "util-buffer.h"
31 #include "util-debug.h"
32 
33 #include "app-layer.h"
34 #include "app-layer-parser.h"
35 #include "app-layer-dnp3.h"
36 #include "app-layer-dnp3-objects.h"
37 
38 #include "detect-dnp3.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 #include "output-json-dnp3.h"
44 
45 typedef struct LogDNP3FileCtx_ {
46  uint32_t flags;
50 
51 typedef struct LogDNP3LogThread_ {
55 
56 static void JsonDNP3LogLinkControl(JsonBuilder *js, uint8_t lc)
57 {
58  jb_set_bool(js, "dir", DNP3_LINK_DIR(lc));
59  jb_set_bool(js, "pri", DNP3_LINK_PRI(lc));
60  jb_set_bool(js, "fcb", DNP3_LINK_FCB(lc));
61  jb_set_bool(js, "fcv", DNP3_LINK_FCV(lc));
62  jb_set_uint(js, "function_code", DNP3_LINK_FC(lc));
63 }
64 
65 static void JsonDNP3LogIin(JsonBuilder *js, uint16_t iin)
66 {
67  if (iin) {
68  jb_open_array(js, "indicators");
69 
70  int mapping = 0;
71  do {
72  if (iin & DNP3IndicatorsMap[mapping].value) {
73  jb_append_string(js, DNP3IndicatorsMap[mapping].name);
74  }
75  mapping++;
76  } while (DNP3IndicatorsMap[mapping].name != NULL);
77  jb_close(js);
78  }
79 }
80 
81 static void JsonDNP3LogApplicationControl(JsonBuilder *js, uint8_t ac)
82 {
83  jb_set_bool(js, "fir", DNP3_APP_FIR(ac));
84  jb_set_bool(js, "fin", DNP3_APP_FIN(ac));
85  jb_set_bool(js, "con", DNP3_APP_CON(ac));
86  jb_set_bool(js, "uns", DNP3_APP_UNS(ac));
87  jb_set_uint(js, "sequence", DNP3_APP_SEQ(ac));
88 }
89 
90 /**
91  * \brief Log the items (points) for an object.
92  *
93  * TODO: Autogenerate this function based on object definitions.
94  */
95 static void JsonDNP3LogObjectItems(JsonBuilder *js, DNP3Object *object)
96 {
97  DNP3Point *item;
98 
99  TAILQ_FOREACH(item, object->points, next) {
100  jb_start_object(js);
101 
102  jb_set_uint(js, "prefix", item->prefix);
103  jb_set_uint(js, "index", item->index);
104  if (DNP3PrefixIsSize(object->prefix_code)) {
105  jb_set_uint(js, "size", item->size);
106  }
107 
108  OutputJsonDNP3SetItem(js, object, item);
109  jb_close(js);
110  }
111 }
112 
113 /**
114  * \brief Log the application layer objects.
115  *
116  * \param objects A list of DNP3 objects.
117  * \param jb A JsonBuilder instance with an open array.
118  */
119 static void JsonDNP3LogObjects(JsonBuilder *js, DNP3ObjectList *objects)
120 {
121  DNP3Object *object;
122 
123  TAILQ_FOREACH(object, objects, next) {
124  jb_start_object(js);
125  jb_set_uint(js, "group", object->group);
126  jb_set_uint(js, "variation", object->variation);
127  jb_set_uint(js, "qualifier", object->qualifier);
128  jb_set_uint(js, "prefix_code", object->prefix_code);
129  jb_set_uint(js, "range_code", object->range_code);
130  jb_set_uint(js, "start", object->start);
131  jb_set_uint(js, "stop", object->stop);
132  jb_set_uint(js, "count", object->count);
133 
134  if (object->points != NULL && !TAILQ_EMPTY(object->points)) {
135  jb_open_array(js, "points");
136  JsonDNP3LogObjectItems(js, object);
137  jb_close(js);
138  }
139 
140  jb_close(js);
141  }
142 }
143 
144 void JsonDNP3LogRequest(JsonBuilder *js, DNP3Transaction *dnp3tx)
145 {
146  JB_SET_STRING(js, "type", "request");
147 
148  jb_open_object(js, "control");
149  JsonDNP3LogLinkControl(js, dnp3tx->request_lh.control);
150  jb_close(js);
151 
152  jb_set_uint(js, "src", DNP3_SWAP16(dnp3tx->request_lh.src));
153  jb_set_uint(js, "dst", DNP3_SWAP16(dnp3tx->request_lh.dst));
154 
155  jb_open_object(js, "application");
156 
157  jb_open_object(js, "control");
158  JsonDNP3LogApplicationControl(js, dnp3tx->request_ah.control);
159  jb_close(js);
160 
161  jb_set_uint(js, "function_code", dnp3tx->request_ah.function_code);
162 
163  if (!TAILQ_EMPTY(&dnp3tx->request_objects)) {
164  jb_open_array(js, "objects");
165  JsonDNP3LogObjects(js, &dnp3tx->request_objects);
166  jb_close(js);
167  }
168 
169  jb_set_bool(js, "complete", dnp3tx->request_complete);
170 
171  /* Close application. */
172  jb_close(js);
173 }
174 
175 void JsonDNP3LogResponse(JsonBuilder *js, DNP3Transaction *dnp3tx)
176 {
177  if (dnp3tx->response_ah.function_code == DNP3_APP_FC_UNSOLICITED_RESP) {
178  JB_SET_STRING(js, "type", "unsolicited_response");
179  }
180  else {
181  JB_SET_STRING(js, "type", "response");
182  }
183 
184  jb_open_object(js, "control");
185  JsonDNP3LogLinkControl(js, dnp3tx->response_lh.control);
186  jb_close(js);
187 
188  jb_set_uint(js, "src", DNP3_SWAP16(dnp3tx->response_lh.src));
189  jb_set_uint(js, "dst", DNP3_SWAP16(dnp3tx->response_lh.dst));
190 
191  jb_open_object(js, "application");
192 
193  jb_open_object(js, "control");
194  JsonDNP3LogApplicationControl(js, dnp3tx->response_ah.control);
195  jb_close(js);
196 
197  jb_set_uint(js, "function_code", dnp3tx->response_ah.function_code);
198 
199  if (!TAILQ_EMPTY(&dnp3tx->response_objects)) {
200  jb_open_array(js, "objects");
201  JsonDNP3LogObjects(js, &dnp3tx->response_objects);
202  jb_close(js);
203  }
204 
205  jb_set_bool(js, "complete", dnp3tx->response_complete);
206 
207  /* Close application. */
208  jb_close(js);
209 
210  jb_open_object(js, "iin");
211  JsonDNP3LogIin(js, (uint16_t)(dnp3tx->response_iin.iin1 << 8 | dnp3tx->response_iin.iin2));
212  jb_close(js);
213 }
214 
215 static int JsonDNP3LoggerToServer(ThreadVars *tv, void *thread_data,
216  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
217 {
218  SCEnter();
219  LogDNP3LogThread *thread = (LogDNP3LogThread *)thread_data;
220  DNP3Transaction *tx = vtx;
221 
222  if (tx->has_request && tx->request_done) {
223  JsonBuilder *js =
224  CreateEveHeader(p, LOG_DIR_FLOW, "dnp3", NULL, thread->dnp3log_ctx->eve_ctx);
225  if (unlikely(js == NULL)) {
226  return TM_ECODE_OK;
227  }
228 
229  jb_open_object(js, "dnp3");
230  JsonDNP3LogRequest(js, tx);
231  jb_close(js);
232  OutputJsonBuilderBuffer(js, thread->ctx);
233  jb_free(js);
234  }
235 
237 }
238 
239 static int JsonDNP3LoggerToClient(ThreadVars *tv, void *thread_data,
240  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
241 {
242  SCEnter();
243  LogDNP3LogThread *thread = (LogDNP3LogThread *)thread_data;
244  DNP3Transaction *tx = vtx;
245 
246  if (tx->has_response && tx->response_done) {
247  JsonBuilder *js =
248  CreateEveHeader(p, LOG_DIR_FLOW, "dnp3", NULL, thread->dnp3log_ctx->eve_ctx);
249  if (unlikely(js == NULL)) {
250  return TM_ECODE_OK;
251  }
252 
253  jb_open_object(js, "dnp3");
254  JsonDNP3LogResponse(js, tx);
255  jb_close(js);
256  OutputJsonBuilderBuffer(js, thread->ctx);
257  jb_free(js);
258  }
259 
261 }
262 
263 static void OutputDNP3LogDeInitCtxSub(OutputCtx *output_ctx)
264 {
265  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
266  LogDNP3FileCtx *dnp3log_ctx = (LogDNP3FileCtx *)output_ctx->data;
267  SCFree(dnp3log_ctx);
268  SCFree(output_ctx);
269 }
270 
271 #define DEFAULT_LOG_FILENAME "dnp3.json"
272 
273 static OutputInitResult OutputDNP3LogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
274 {
275  OutputInitResult result = { NULL, false };
276  OutputJsonCtx *json_ctx = parent_ctx->data;
277 
278  LogDNP3FileCtx *dnp3log_ctx = SCCalloc(1, sizeof(*dnp3log_ctx));
279  if (unlikely(dnp3log_ctx == NULL)) {
280  return result;
281  }
282  dnp3log_ctx->eve_ctx = json_ctx;
283 
284  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
285  if (unlikely(output_ctx == NULL)) {
286  SCFree(dnp3log_ctx);
287  return result;
288  }
289  output_ctx->data = dnp3log_ctx;
290  output_ctx->DeInit = OutputDNP3LogDeInitCtxSub;
291 
292  SCLogInfo("DNP3 log sub-module initialized.");
293 
295 
296  result.ctx = output_ctx;
297  result.ok = true;
298  return result;
299 }
300 
301 
302 static TmEcode JsonDNP3LogThreadInit(ThreadVars *t, const void *initdata, void **data)
303 {
304  LogDNP3LogThread *thread = SCCalloc(1, sizeof(*thread));
305  if (unlikely(thread == NULL)) {
306  return TM_ECODE_FAILED;
307  }
308 
309  if (initdata == NULL) {
310  SCLogDebug("Error getting context for DNP3. \"initdata\" is NULL.");
311  goto error_exit;
312  }
313 
314  thread->dnp3log_ctx = ((OutputCtx *)initdata)->data;
315  thread->ctx = CreateEveThreadCtx(t, thread->dnp3log_ctx->eve_ctx);
316  if (thread->ctx == NULL) {
317  goto error_exit;
318  }
319 
320  *data = (void *)thread;
321 
322  return TM_ECODE_OK;
323 
324 error_exit:
325  SCFree(thread);
326  return TM_ECODE_FAILED;
327 }
328 
329 static TmEcode JsonDNP3LogThreadDeinit(ThreadVars *t, void *data)
330 {
331  LogDNP3LogThread *thread = (LogDNP3LogThread *)data;
332  if (thread == NULL) {
333  return TM_ECODE_OK;
334  }
335  FreeEveThreadCtx(thread->ctx);
336  SCFree(thread);
337  return TM_ECODE_OK;
338 }
339 
341 {
342  /* Register direction aware eve sub-modules. */
344  "JsonDNP3Log", "eve-log.dnp3", OutputDNP3LogInitSub, ALPROTO_DNP3,
345  JsonDNP3LoggerToServer, 0, 1, JsonDNP3LogThreadInit,
346  JsonDNP3LogThreadDeinit, NULL);
348  "JsonDNP3Log", "eve-log.dnp3", OutputDNP3LogInitSub, ALPROTO_DNP3,
349  JsonDNP3LoggerToClient, 1, 1, JsonDNP3LogThreadInit,
350  JsonDNP3LogThreadDeinit, NULL);
351 }
LogDNP3LogThread_
Definition: output-json-dnp3.c:51
LogDNP3FileCtx_::flags
uint32_t flags
Definition: output-json-dnp3.c:46
tm-threads.h
DNP3Transaction_::response_lh
DNP3LinkHeader response_lh
Definition: app-layer-dnp3.h:230
DNP3Transaction_::response_done
uint8_t response_done
Definition: app-layer-dnp3.h:229
LOGGER_JSON_DNP3_TS
@ LOGGER_JSON_DNP3_TS
Definition: suricata-common.h:451
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
DNP3Transaction_::request_done
uint8_t request_done
Definition: app-layer-dnp3.h:215
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
DNP3Object_
Struct to hold the list of decoded objects.
Definition: app-layer-dnp3.h:188
DNP3Transaction_::has_request
uint8_t has_request
Definition: app-layer-dnp3.h:214
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:353
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:960
JsonDNP3LogRegister
void JsonDNP3LogRegister(void)
Definition: output-json-dnp3.c:340
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
DNP3Object_::stop
uint32_t stop
Definition: app-layer-dnp3.h:195
LogDNP3FileCtx_
Definition: output-json-dnp3.c:45
DNP3PrefixIsSize
int DNP3PrefixIsSize(uint8_t prefix_code)
Check if the prefix code is a size prefix.
Definition: app-layer-dnp3.c:1551
detect-dnp3.h
DNP3_APP_FC_UNSOLICITED_RESP
#define DNP3_APP_FC_UNSOLICITED_RESP
Definition: app-layer-dnp3.h:66
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
DNP3Transaction_::response_complete
uint8_t response_complete
Definition: app-layer-dnp3.h:237
util-unittest.h
DNP3Transaction_::has_response
uint8_t has_response
Definition: app-layer-dnp3.h:228
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
DNP3_SWAP16
#define DNP3_SWAP16(x)
Definition: app-layer-dnp3.h:91
OutputJsonThreadCtx_
Definition: output-json.h:89
output-json-dnp3.h
DNP3Object_::points
DNP3PointList * points
Definition: app-layer-dnp3.h:197
LogDNP3LogThread_::dnp3log_ctx
LogDNP3FileCtx * dnp3log_ctx
Definition: output-json-dnp3.c:52
DNP3IndicatorsMap
DNP3Mapping DNP3IndicatorsMap[]
Definition: detect-dnp3.c:60
LogDNP3FileCtx_::include_object_data
uint8_t include_object_data
Definition: output-json-dnp3.c:47
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:37
DNP3_LINK_FCV
#define DNP3_LINK_FCV(control)
Definition: app-layer-dnp3.h:73
DNP3Transaction_::response_iin
DNP3InternalInd response_iin
Definition: app-layer-dnp3.h:233
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:45
DNP3Point_::size
uint32_t size
Definition: app-layer-dnp3.h:177
ALPROTO_DNP3
@ ALPROTO_DNP3
Definition: app-layer-protos.h:44
app-layer-dnp3.h
output-json.h
OutputRegisterTxSubModuleWithProgress
void OutputRegisterTxSubModuleWithProgress(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, int tc_log_progress, int ts_log_progress, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:381
DNP3_APP_FIR
#define DNP3_APP_FIR(x)
Definition: app-layer-dnp3.h:82
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
util-print.h
DNP3Transaction_::request_complete
uint8_t request_complete
Definition: app-layer-dnp3.h:222
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:56
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:46
JsonDNP3LogResponse
void JsonDNP3LogResponse(JsonBuilder *js, DNP3Transaction *dnp3tx)
Definition: output-json-dnp3.c:175
app-layer-parser.h
Packet_
Definition: decode.h:433
DNP3Object_::group
uint8_t group
Definition: app-layer-dnp3.h:189
DNP3_LINK_FCB
#define DNP3_LINK_FCB(control)
Definition: app-layer-dnp3.h:72
conf.h
DNP3_LINK_DIR
#define DNP3_LINK_DIR(control)
Definition: app-layer-dnp3.h:70
TmEcode
TmEcode
Definition: tm-threads-common.h:81
DNP3Object_::start
uint32_t start
Definition: app-layer-dnp3.h:194
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:215
DNP3Transaction_::request_ah
DNP3ApplicationHeader request_ah
Definition: app-layer-dnp3.h:218
DNP3Transaction_::response_ah
DNP3ApplicationHeader response_ah
Definition: app-layer-dnp3.h:232
DNP3Object_::count
uint32_t count
Definition: app-layer-dnp3.h:196
LogDNP3FileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-dnp3.c:48
LogDNP3LogThread
struct LogDNP3LogThread_ LogDNP3LogThread
OutputInitResult_
Definition: output.h:44
LogDNP3LogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-dnp3.c:53
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
DNP3Object_::range_code
uint8_t range_code
Definition: app-layer-dnp3.h:193
DNP3_LINK_PRI
#define DNP3_LINK_PRI(control)
Definition: app-layer-dnp3.h:71
DNP3_APP_UNS
#define DNP3_APP_UNS(x)
Definition: app-layer-dnp3.h:85
DNP3_APP_CON
#define DNP3_APP_CON(x)
Definition: app-layer-dnp3.h:84
DNP3_APP_FIN
#define DNP3_APP_FIN(x)
Definition: app-layer-dnp3.h:83
DNP3Object_::prefix_code
uint8_t prefix_code
Definition: app-layer-dnp3.h:192
output-json-dnp3-objects.h
DNP3Transaction_::request_lh
DNP3LinkHeader request_lh
Definition: app-layer-dnp3.h:216
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:31
OutputJsonDNP3SetItem
void OutputJsonDNP3SetItem(JsonBuilder *js, DNP3Object *object, DNP3Point *point)
Definition: output-json-dnp3-objects.c:33
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
LOGGER_JSON_DNP3_TC
@ LOGGER_JSON_DNP3_TC
Definition: suricata-common.h:452
DNP3Transaction_::request_objects
DNP3ObjectList request_objects
Definition: app-layer-dnp3.h:226
LogDNP3FileCtx
struct LogDNP3FileCtx_ LogDNP3FileCtx
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
DNP3Object_::variation
uint8_t variation
Definition: app-layer-dnp3.h:190
util-buffer.h
DNP3_APP_SEQ
#define DNP3_APP_SEQ(x)
Definition: app-layer-dnp3.h:86
DNP3Object_::qualifier
uint8_t qualifier
Definition: app-layer-dnp3.h:191
JsonDNP3LogRequest
void JsonDNP3LogRequest(JsonBuilder *js, DNP3Transaction *dnp3tx)
Definition: output-json-dnp3.c:144
DNP3Point_::index
uint32_t index
Definition: app-layer-dnp3.h:173
DNP3_LINK_FC
#define DNP3_LINK_FC(control)
Definition: app-layer-dnp3.h:74
app-layer-dnp3-objects.h
DNP3Point_::prefix
uint32_t prefix
Definition: app-layer-dnp3.h:172
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:302
DNP3Point_
DNP3 object point.
Definition: app-layer-dnp3.h:171
debug.h
output.h
DNP3Transaction_
DNP3 transaction.
Definition: app-layer-dnp3.h:207
DNP3Transaction_::response_objects
DNP3ObjectList response_objects
Definition: app-layer-dnp3.h:241
app-layer.h