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