suricata
detect-dnp3.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2022 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 
20 #include "stream.h"
21 
22 #include "detect.h"
23 #include "detect-parse.h"
24 #include "detect-dnp3.h"
25 #include "detect-engine.h"
26 #include "detect-engine-mpm.h"
29 
30 #include "app-layer-dnp3.h"
31 #include "util-byte.h"
32 
33 static int g_dnp3_match_buffer_id = 0;
34 static int g_dnp3_data_buffer_id = 0;
35 
36 /**
37  * The detection struct.
38  */
39 typedef struct DetectDNP3_ {
40  union {
41  struct {
42  /* Function code for function code detection. */
43  uint8_t function_code;
44  };
45  struct {
46  /* Internal indicator flags for IIN detection. */
47  uint16_t ind_flags;
48  };
49  struct {
50  /* Object info for object detection. */
51  uint8_t obj_group;
52  uint8_t obj_variation;
53  };
54  };
56 
57 /**
58  * Indicator names to value mappings (Snort compatible).
59  */
61  {"device_restart", 0x8000},
62  {"device_trouble", 0x4000},
63  {"local_control", 0x2000},
64  {"need_time", 0x1000},
65  {"class_3_events", 0x0800},
66  {"class_2_events", 0x0400},
67  {"class_1_events", 0x0200},
68  {"all_stations", 0x0100},
69 
70  {"reserved_1", 0x0080},
71  {"reserved_2", 0x0040},
72  {"config_corrupt", 0x0020},
73  {"already_executing", 0x0010},
74  {"event_buffer_overflow", 0x0008},
75  {"parameter_error", 0x0004},
76  {"object_unknown", 0x0002},
77  {"no_func_code_support", 0x0001},
78 
79  {NULL, 0},
80 };
81 
82 /**
83  * Application function code name to code mappings (Snort compatible).
84  */
86  {"confirm", 0},
87  {"read", 1},
88  {"write", 2},
89  {"select", 3},
90  {"operate", 4},
91  {"direct_operate", 5},
92  {"direct_operate_nr", 6},
93  {"immed_freeze", 7},
94  {"immed_freeze_nr", 8},
95  {"freeze_clear", 9},
96  {"freeze_clear_nr", 10},
97  {"freeze_at_time", 11},
98  {"freeze_at_time_nr", 12},
99  {"cold_restart", 13},
100  {"warm_restart", 14},
101  {"initialize_data", 15},
102  {"initialize_appl", 16},
103  {"start_appl", 17},
104  {"stop_appl", 18},
105  {"save_config", 19},
106  {"enable_unsolicited", 20},
107  {"disable_unsolicited", 21},
108  {"assign_class", 22},
109  {"delay_measure", 23},
110  {"record_current_time", 24},
111  {"open_file", 25},
112  {"close_file", 26},
113  {"delete_file", 27},
114  {"get_file_info", 28},
115  {"authenticate_file", 29},
116  {"abort_file", 30},
117  {"activate_config", 31},
118  {"authenticate_req", 32},
119  {"authenticate_err", 33},
120  {"response", 129},
121  {"unsolicited_response", 130},
122  {"authenticate_resp", 131}
123 };
124 
125 #ifdef UNITTESTS
126 static void DetectDNP3FuncRegisterTests(void);
127 static void DetectDNP3IndRegisterTests(void);
128 static void DetectDNP3ObjRegisterTests(void);
129 #endif
130 
131 /**
132  * \brief Utility function to trim leading and trailing whitespace
133  * from a string.
134  */
135 static char *TrimString(char *str)
136 {
137  char *end = str + strlen(str) - 1;
138  while (isspace(*str)) {
139  str++;
140  }
141  while (end > str && isspace(*end)) {
142  end--;
143  }
144  *(end + 1) = '\0';
145  return str;
146 }
147 
148 static InspectionBuffer *GetDNP3Data(DetectEngineThreadCtx *det_ctx,
149  const DetectEngineTransforms *transforms,
150  Flow *_f, const uint8_t flow_flags,
151  void *txv, const int list_id)
152 {
153  SCLogDebug("list_id %d", list_id);
154  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
155  if (buffer->inspect == NULL) {
156  DNP3Transaction *tx = (DNP3Transaction *)txv;
157  SCLogDebug("tx %p", tx);
158 
159  if ((flow_flags & STREAM_TOSERVER && !tx->is_request) ||
160  (flow_flags & STREAM_TOCLIENT && tx->is_request)) {
161  return NULL;
162  }
163 
164  if (tx->buffer == NULL || tx->buffer_len == 0) {
165  return NULL;
166  }
167 
168  SCLogDebug("tx %p data %p data_len %u", tx, tx->buffer, tx->buffer_len);
169  InspectionBufferSetup(det_ctx, list_id, buffer, tx->buffer, tx->buffer_len);
170  InspectionBufferApplyTransforms(buffer, transforms);
171  }
172  return buffer;
173 }
174 
175 /**
176  * \brief Parse the provided function name or code to its integer
177  * value.
178  *
179  * If the value passed is a number, it will be checked that it falls
180  * within the range of valid function codes. If function name is
181  * passed it will be resolved to its function code.
182  *
183  * \retval The function code as an integer if successful, -1 on
184  * failure.
185  */
186 static int DetectDNP3FuncParseFunctionCode(const char *str, uint8_t *fc)
187 {
188  if (StringParseUint8(fc, 10, (uint16_t)strlen(str), str) >= 0) {
189  return 1;
190  }
191 
192  /* Lookup by name. */
193  for (size_t i = 0;
194  i < sizeof(DNP3FunctionNameMap) / sizeof(DNP3Mapping); i++) {
195  if (strcasecmp(str, DNP3FunctionNameMap[i].name) == 0) {
196  *fc = (uint8_t)(DNP3FunctionNameMap[i].value);
197  return 1;
198  }
199  }
200 
201  return 0;
202 }
203 
204 static int DetectDNP3FuncSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
205 {
206  SCEnter();
207  DetectDNP3 *dnp3 = NULL;
208  SigMatch *sm = NULL;
209  uint8_t function_code;
210 
212  return -1;
213 
214  if (!DetectDNP3FuncParseFunctionCode(str, &function_code)) {
215  SCLogError("Invalid argument \"%s\" supplied to dnp3_func keyword.", str);
216  return -1;
217  }
218 
219  dnp3 = SCCalloc(1, sizeof(DetectDNP3));
220  if (unlikely(dnp3 == NULL)) {
221  goto error;
222  }
224 
225  sm = SigMatchAlloc();
226  if (sm == NULL) {
227  goto error;
228  }
229  sm->type = DETECT_AL_DNP3FUNC;
230  sm->ctx = (void *)dnp3;
231 
232  SigMatchAppendSMToList(s, sm, g_dnp3_match_buffer_id);
233 
234  SCReturnInt(0);
235 error:
236  if (dnp3 != NULL) {
237  SCFree(dnp3);
238  }
239  if (sm != NULL) {
240  SCFree(sm);
241  }
242  SCReturnInt(-1);
243 }
244 
245 static int DetectDNP3IndParseByName(const char *str, uint16_t *flags)
246 {
247  char tmp[strlen(str) + 1];
248  char *p, *last = NULL;
249 
250  strlcpy(tmp, str, sizeof(tmp));
251 
252  for ((p = strtok_r(tmp, ",", &last)); p; (p = strtok_r(NULL, ",", &last))) {
253  p = TrimString(p);
254  int found = 0;
255  int i = 0;
256  while (DNP3IndicatorsMap[i].name != NULL) {
257  if (strcasecmp(p, DNP3IndicatorsMap[i].name) == 0) {
259  found = 1;
260  break;
261  }
262  i++;
263  }
264 
265  if (!found) {
266  SCLogError("Bad argument \"%s\" supplied to dnp3.ind keyword.", p);
267  return 0;
268  }
269  }
270 
271  return 1;
272 }
273 
274 static int DetectDNP3IndParse(const char *str, uint16_t *flags)
275 {
276  *flags = 0;
277 
278  if (StringParseUint16(flags, 0, (uint16_t)strlen(str), str) > 0) {
279  return 1;
280  }
281 
282  /* Parse by name - will log a more specific error message on error. */
283  if (DetectDNP3IndParseByName(str, flags)) {
284  return 1;
285  }
286 
287  return 0;
288 }
289 
290 static int DetectDNP3IndSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
291 {
292  SCEnter();
293  DetectDNP3 *detect = NULL;
294  SigMatch *sm = NULL;
295  uint16_t flags;
296 
298  return -1;
299 
300  if (!DetectDNP3IndParse(str, &flags)) {
301  SCLogError("Invalid argument \"%s\" supplied to dnp3.ind keyword.", str);
302  return -1;
303  }
304 
305  detect = SCCalloc(1, sizeof(DetectDNP3));
306  if (unlikely(detect == NULL)) {
307  goto error;
308  }
309  detect->ind_flags = flags;
310 
311  sm = SigMatchAlloc();
312  if (sm == NULL) {
313  goto error;
314  }
315  sm->type = DETECT_AL_DNP3IND;
316  sm->ctx = (void *)detect;
317  SigMatchAppendSMToList(s, sm, g_dnp3_match_buffer_id);
318 
319  SCReturnInt(0);
320 error:
321  if (detect != NULL) {
322  SCFree(detect);
323  }
324  if (sm != NULL) {
325  SCFree(sm);
326  }
327  SCReturnInt(-1);
328 }
329 
330 /**
331  * \brief Parse the value of string of the dnp3_obj keyword.
332  *
333  * \param str the input string
334  * \param gout pointer to variable to store the parsed group integer
335  * \param vout pointer to variable to store the parsed variation integer
336  *
337  * \retval 1 if parsing successful otherwise 0.
338  */
339 static int DetectDNP3ObjParse(const char *str, uint8_t *group, uint8_t *var)
340 {
341  size_t size = strlen(str) + 1;
342  char groupstr[size], *varstr, *sep;
343  strlcpy(groupstr, str, size);
344 
345  sep = strchr(groupstr, ',');
346  if (sep == NULL) {
347  return 0;
348  }
349  *sep = '\0';
350  varstr = sep + 1;
351 
352  if (StringParseUint8(group, 0, (uint16_t)strlen(groupstr), groupstr) < 0) {
353  return 0;
354  }
355 
356  if (StringParseUint8(var, 0, (uint16_t)strlen(varstr), varstr) < 0) {
357  return 0;
358  }
359 
360  return 1;
361 }
362 
363 static int DetectDNP3ObjSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
364 {
365  SCEnter();
366  uint8_t group;
367  uint8_t variation;
368  DetectDNP3 *detect = NULL;
369  SigMatch *sm = NULL;
370 
372  return -1;
373 
374  if (!DetectDNP3ObjParse(str, &group, &variation)) {
375  goto fail;
376  }
377 
378  detect = SCCalloc(1, sizeof(*detect));
379  if (unlikely(detect == NULL)) {
380  goto fail;
381  }
382  detect->obj_group = group;
383  detect->obj_variation = variation;
384 
385  sm = SigMatchAlloc();
386  if (unlikely(sm == NULL)) {
387  goto fail;
388  }
389  sm->type = DETECT_AL_DNP3OBJ;
390  sm->ctx = (void *)detect;
391  SigMatchAppendSMToList(s, sm, g_dnp3_match_buffer_id);
392 
393  SCReturnInt(1);
394 fail:
395  if (detect != NULL) {
396  SCFree(detect);
397  }
398  if (sm != NULL) {
399  SCFree(sm);
400  }
401  SCReturnInt(0);
402 }
403 
404 static void DetectDNP3Free(DetectEngineCtx *de_ctx, void *ptr)
405 {
406  SCEnter();
407  if (ptr != NULL) {
408  SCFree(ptr);
409  }
410  SCReturn;
411 }
412 
413 static int DetectDNP3FuncMatch(DetectEngineThreadCtx *det_ctx,
414  Flow *f, uint8_t flags, void *state, void *txv, const Signature *s,
415  const SigMatchCtx *ctx)
416 {
417  DNP3Transaction *tx = (DNP3Transaction *)txv;
418  DetectDNP3 *detect = (DetectDNP3 *)ctx;
419  int match = 0;
420 
421  if (flags & STREAM_TOSERVER && tx->is_request) {
422  match = detect->function_code == tx->ah.function_code;
423  } else if (flags & STREAM_TOCLIENT && !tx->is_request) {
424  match = detect->function_code == tx->ah.function_code;
425  }
426 
427  return match;
428 }
429 
430 static int DetectDNP3ObjMatch(DetectEngineThreadCtx *det_ctx,
431  Flow *f, uint8_t flags, void *state, void *txv, const Signature *s,
432  const SigMatchCtx *ctx)
433 {
434  DNP3Transaction *tx = (DNP3Transaction *)txv;
435  DetectDNP3 *detect = (DetectDNP3 *)ctx;
436  DNP3ObjectList *objects = NULL;
437 
438  if (flags & STREAM_TOSERVER && tx->is_request) {
439  objects = &tx->objects;
440  } else if (flags & STREAM_TOCLIENT && !tx->is_request) {
441  objects = &tx->objects;
442  }
443 
444  if (objects != NULL) {
445  DNP3Object *object;
446  TAILQ_FOREACH(object, objects, next) {
447  if (object->group == detect->obj_group &&
448  object->variation == detect->obj_variation) {
449  return 1;
450  }
451  }
452  }
453 
454  return 0;
455 }
456 
457 static int DetectDNP3IndMatch(DetectEngineThreadCtx *det_ctx,
458  Flow *f, uint8_t flags, void *state, void *txv, const Signature *s,
459  const SigMatchCtx *ctx)
460 {
461  DNP3Transaction *tx = (DNP3Transaction *)txv;
462  DetectDNP3 *detect = (DetectDNP3 *)ctx;
463 
464  if (flags & STREAM_TOCLIENT) {
465  if ((tx->iin.iin1 & (detect->ind_flags >> 8)) ||
466  (tx->iin.iin2 & (detect->ind_flags & 0xf))) {
467  return 1;
468  }
469  }
470 
471  return 0;
472 }
473 
474 static void DetectDNP3FuncRegister(void)
475 {
476  SCEnter();
477 
478  sigmatch_table[DETECT_AL_DNP3FUNC].name = "dnp3_func";
479  sigmatch_table[DETECT_AL_DNP3FUNC].alias = "dnp3.func";
480  sigmatch_table[DETECT_AL_DNP3FUNC].desc = "match on the application function code found in DNP3 request and responses";
481  sigmatch_table[DETECT_AL_DNP3FUNC].url = "/rules/dnp3-keywords.html#dnp3-func";
483  sigmatch_table[DETECT_AL_DNP3FUNC].AppLayerTxMatch = DetectDNP3FuncMatch;
484  sigmatch_table[DETECT_AL_DNP3FUNC].Setup = DetectDNP3FuncSetup;
485  sigmatch_table[DETECT_AL_DNP3FUNC].Free = DetectDNP3Free;
486 #ifdef UNITTESTS
488  DetectDNP3FuncRegisterTests;
489 #endif
490  SCReturn;
491 }
492 
493 static void DetectDNP3IndRegister(void)
494 {
495  SCEnter();
496 
497  sigmatch_table[DETECT_AL_DNP3IND].name = "dnp3_ind";
498  sigmatch_table[DETECT_AL_DNP3IND].alias = "dnp3.ind";
499  sigmatch_table[DETECT_AL_DNP3IND].desc = "match on the DNP3 internal indicator flags in the response application header";
500  sigmatch_table[DETECT_AL_DNP3IND].url = "/rules/dnp3-keywords.html#dnp3-ind";
502  sigmatch_table[DETECT_AL_DNP3IND].AppLayerTxMatch = DetectDNP3IndMatch;
503  sigmatch_table[DETECT_AL_DNP3IND].Setup = DetectDNP3IndSetup;
504  sigmatch_table[DETECT_AL_DNP3IND].Free = DetectDNP3Free;
505 #ifdef UNITTESTS
507  DetectDNP3IndRegisterTests;
508 #endif
509  SCReturn;
510 }
511 
512 static void DetectDNP3ObjRegister(void)
513 {
514  SCEnter();
515 
516  sigmatch_table[DETECT_AL_DNP3OBJ].name = "dnp3_obj";
517  sigmatch_table[DETECT_AL_DNP3OBJ].alias = "dnp3.obj";
518  sigmatch_table[DETECT_AL_DNP3OBJ].desc = "match on the DNP3 application data objects";
519  sigmatch_table[DETECT_AL_DNP3OBJ].url = "/rules/dnp3-keywords.html#dnp3-obj";
521  sigmatch_table[DETECT_AL_DNP3OBJ].AppLayerTxMatch = DetectDNP3ObjMatch;
522  sigmatch_table[DETECT_AL_DNP3OBJ].Setup = DetectDNP3ObjSetup;
523  sigmatch_table[DETECT_AL_DNP3OBJ].Free = DetectDNP3Free;
524 #ifdef UNITTESTS
526  DetectDNP3ObjRegisterTests;
527 #endif
528  SCReturn;
529 }
530 
531 static int DetectDNP3DataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
532 {
533  SCEnter();
535  return -1;
536 
537  if (DetectBufferSetActiveList(de_ctx, s, g_dnp3_data_buffer_id) != 0)
538  return -1;
539 
540  SCReturnInt(0);
541 }
542 
543 static void DetectDNP3DataRegister(void)
544 {
545  SCEnter();
546 
547  sigmatch_table[DETECT_AL_DNP3DATA].name = "dnp3.data";
548  sigmatch_table[DETECT_AL_DNP3DATA].alias = "dnp3_data";
549  sigmatch_table[DETECT_AL_DNP3DATA].desc = "make the following content options to match on the re-assembled application buffer";
550  sigmatch_table[DETECT_AL_DNP3DATA].url = "/rules/dnp3-keywords.html#dnp3-data";
551  sigmatch_table[DETECT_AL_DNP3DATA].Setup = DetectDNP3DataSetup;
553 
557  GetDNP3Data);
559  PrefilterGenericMpmRegister, GetDNP3Data,
560  ALPROTO_DNP3, 0);
561 
565  GetDNP3Data);
567  PrefilterGenericMpmRegister, GetDNP3Data,
568  ALPROTO_DNP3, 0);
569 
570  g_dnp3_data_buffer_id = DetectBufferTypeGetByName("dnp3_data");
571  SCReturn;
572 }
573 
575 {
576  DetectDNP3DataRegister();
577 
578  DetectDNP3FuncRegister();
579  DetectDNP3IndRegister();
580  DetectDNP3ObjRegister();
581 
582  /* Register the list of func, ind and obj. */
587 
588  g_dnp3_match_buffer_id = DetectBufferTypeRegister("dnp3");
589 
590 }
591 
592 #ifdef UNITTESTS
593 
594 #include "util-unittest.h"
595 #include "util-unittest-helper.h"
596 #include "app-layer-parser.h"
597 #include "flow-util.h"
598 #include "stream-tcp.h"
599 
600 static int DetectDNP3FuncParseFunctionCodeTest(void)
601 {
602  uint8_t fc;
603 
604  /* Valid. */
605  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("0", &fc));
606  FAIL_IF(fc != 0);
607 
608  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("1", &fc));
609  FAIL_IF(fc != 1);
610 
611  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("254", &fc));
612  FAIL_IF(fc != 254);
613 
614  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("255", &fc));
615  FAIL_IF(fc != 255);
616 
617  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("confirm", &fc));
618  FAIL_IF(fc != 0);
619 
620  FAIL_IF_NOT(DetectDNP3FuncParseFunctionCode("CONFIRM", &fc));
621  FAIL_IF(fc != 0);
622 
623  /* Invalid. */
624  FAIL_IF(DetectDNP3FuncParseFunctionCode("", &fc));
625  FAIL_IF(DetectDNP3FuncParseFunctionCode("-1", &fc));
626  FAIL_IF(DetectDNP3FuncParseFunctionCode("-2", &fc));
627  FAIL_IF(DetectDNP3FuncParseFunctionCode("256", &fc));
628  FAIL_IF(DetectDNP3FuncParseFunctionCode("unknown_function_code", &fc));
629 
630  PASS;
631 }
632 
633 static int DetectDNP3FuncTest01(void)
634 {
637 
638  Signature *s = DetectEngineAppendSig(de_ctx, "alert dnp3 any any -> any any "
639  "(msg:\"SURICATA DNP3 Write request\"; "
640  "dnp3_func:2; sid:5000009; rev:1;)");
642 
643  SigMatch *sm = DetectBufferGetFirstSigMatch(s, g_dnp3_match_buffer_id);
644  FAIL_IF_NULL(sm);
645  FAIL_IF_NULL(sm->ctx);
646 
647  DetectDNP3 *dnp3func = (DetectDNP3 *)sm->ctx;
648  FAIL_IF(dnp3func->function_code != 2);
649 
651  PASS;
652 }
653 
654 static int DetectDNP3IndTestParseAsInteger(void)
655 {
656  uint16_t flags = 0;
657 
658  FAIL_IF(!DetectDNP3IndParse("0", &flags));
659  FAIL_IF(flags != 0);
660  FAIL_IF(!DetectDNP3IndParse("1", &flags));
661  FAIL_IF(flags != 0x0001);
662 
663  FAIL_IF(!DetectDNP3IndParse("0x0", &flags));
664  FAIL_IF(flags != 0);
665  FAIL_IF(!DetectDNP3IndParse("0x0000", &flags));
666  FAIL_IF(flags != 0);
667  FAIL_IF(!DetectDNP3IndParse("0x0001", &flags));
668  FAIL_IF(flags != 0x0001);
669 
670  FAIL_IF(!DetectDNP3IndParse("0x8421", &flags));
671  FAIL_IF(flags != 0x8421);
672 
673  FAIL_IF(DetectDNP3IndParse("a", &flags));
674 
675  PASS;
676 }
677 
678 static int DetectDNP3IndTestParseByName(void)
679 {
680  uint16_t flags = 0;
681 
682  FAIL_IF(!DetectDNP3IndParse("all_stations", &flags));
683  FAIL_IF(!(flags & 0x0100));
684  FAIL_IF(!DetectDNP3IndParse("class_1_events , class_2_events", &flags));
685  FAIL_IF(!(flags & 0x0200));
686  FAIL_IF(!(flags & 0x0400));
687  FAIL_IF((flags & 0xf9ff));
688 
689  FAIL_IF(DetectDNP3IndParse("something", &flags));
690 
691  PASS;
692 }
693 
694 static int DetectDNP3ObjSetupTest(void)
695 {
697  FAIL_IF(de_ctx == NULL);
698 
699  Signature *s = DetectEngineAppendSig(de_ctx, "alert dnp3 any any -> any any "
700  "(msg:\"SURICATA DNP3 Object Test\"; "
701  "dnp3_obj:99,99; sid:1; rev:1;)");
702  FAIL_IF(de_ctx->sig_list == NULL);
703 
704  SigMatch *sm = DetectBufferGetFirstSigMatch(s, g_dnp3_match_buffer_id);
705  FAIL_IF_NULL(sm);
706  FAIL_IF_NULL(sm->ctx);
707 
708  DetectDNP3 *detect = (DetectDNP3 *)sm->ctx;
709  FAIL_IF(detect->obj_group != 99);
710  FAIL_IF(detect->obj_variation != 99);
711 
713  PASS;
714 }
715 
716 static int DetectDNP3ObjParseTest(void)
717 {
718  uint8_t group, var;
719 
720  FAIL_IF(!DetectDNP3ObjParse("0,0", &group, &var));
721  FAIL_IF(group != 0 || var != 0);
722 
723  FAIL_IF(!DetectDNP3ObjParse("255,255", &group, &var));
724  FAIL_IF(group != 255 || var != 255);
725 
726  FAIL_IF(DetectDNP3ObjParse("-1,-1", &group, &var));
727  FAIL_IF(DetectDNP3ObjParse("256,256", &group, &var));
728  FAIL_IF(DetectDNP3ObjParse("a,1", &group, &var));
729  FAIL_IF(DetectDNP3ObjParse("1,a", &group, &var));
730 
731  PASS;
732 }
733 
734 static void DetectDNP3FuncRegisterTests(void)
735 {
736  UtRegisterTest("DetectDNP3FuncParseFunctionCodeTest",
737  DetectDNP3FuncParseFunctionCodeTest);
738  UtRegisterTest("DetectDNP3FuncTest01", DetectDNP3FuncTest01);
739 }
740 
741 static void DetectDNP3IndRegisterTests(void)
742 {
743  UtRegisterTest("DetectDNP3IndTestParseAsInteger",
744  DetectDNP3IndTestParseAsInteger);
745  UtRegisterTest("DetectDNP3IndTestParseByName",
746  DetectDNP3IndTestParseByName);
747 }
748 
749 static void DetectDNP3ObjRegisterTests(void)
750 {
751  UtRegisterTest("DetectDNP3ObjParseTest", DetectDNP3ObjParseTest);
752  UtRegisterTest("DetectDNP3ObjSetupTest", DetectDNP3ObjSetupTest);
753 }
754 #endif
util-byte.h
DETECT_AL_DNP3IND
@ DETECT_AL_DNP3IND
Definition: detect-engine-register.h:246
SigTableElmt_::url
const char * url
Definition: detect.h:1245
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1627
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1451
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:382
SigTableElmt_::desc
const char * desc
Definition: detect.h:1244
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2165
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1232
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1242
stream-tcp.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:375
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectDNP3Register
void DetectDNP3Register(void)
Definition: detect-dnp3.c:574
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DNP3Object_
Struct to hold the list of decoded objects.
Definition: app-layer-dnp3.h:186
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1386
InspectionBuffer
Definition: detect.h:340
Flow_
Flow data structure.
Definition: flow.h:343
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1236
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:801
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:337
DETECT_AL_DNP3FUNC
@ DETECT_AL_DNP3FUNC
Definition: detect-engine-register.h:245
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2588
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1213
DetectDNP3_
Definition: detect-dnp3.c:39
DNP3Transaction_::objects
DNP3ObjectList objects
Definition: app-layer-dnp3.h:215
detect-dnp3.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2493
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
DNP3Transaction_::buffer_len
uint32_t buffer_len
Definition: app-layer-dnp3.h:214
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1227
DetectDNP3_::function_code
uint8_t function_code
Definition: detect-dnp3.c:43
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1525
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1132
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
DNP3IndicatorsMap
DNP3Mapping DNP3IndicatorsMap[]
Definition: detect-dnp3.c:60
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:361
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
DNP3Transaction_::ah
DNP3ApplicationHeader ah
Definition: app-layer-dnp3.h:218
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectDNP3_::ind_flags
uint16_t ind_flags
Definition: detect-dnp3.c:47
DetectEngineThreadCtx_
Definition: detect.h:1032
DNP3Transaction_::iin
DNP3InternalInd iin
Definition: app-layer-dnp3.h:219
ALPROTO_DNP3
@ ALPROTO_DNP3
Definition: app-layer-protos.h:44
app-layer-dnp3.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:319
SCReturn
#define SCReturn
Definition: util-debug.h:273
stream.h
DNP3Object_::group
uint8_t group
Definition: app-layer-dnp3.h:187
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:224
variation
uint8_t variation
Definition: app-layer-dnp3.h:1
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1210
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:267
DetectDNP3_::obj_variation
uint8_t obj_variation
Definition: detect-dnp3.c:52
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:311
DNP3Transaction_::is_request
bool is_request
Definition: app-layer-dnp3.h:209
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1068
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1243
group
uint8_t group
Definition: app-layer-dnp3.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:317
DetectDNP3_::obj_group
uint8_t obj_group
Definition: detect-dnp3.c:51
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1724
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:77
DetectBufferGetFirstSigMatch
SigMatch * DetectBufferGetFirstSigMatch(const Signature *s, const uint32_t buf_id)
Definition: detect-engine.c:1355
DNP3Mapping_::value
uint16_t value
Definition: detect-dnp3.h:26
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:806
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2122
DNP3Mapping_
Definition: detect-dnp3.h:24
DETECT_AL_DNP3DATA
@ DETECT_AL_DNP3DATA
Definition: detect-engine-register.h:244
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:341
str
#define str(s)
Definition: suricata-common.h:286
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1620
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DNP3Object_::variation
uint8_t variation
Definition: app-layer-dnp3.h:188
detect-parse.h
Signature_
Signature container.
Definition: detect.h:557
SigMatch_
a single match condition for a signature
Definition: detect.h:316
function_code
uint8_t function_code
Definition: app-layer-dnp3.h:1
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2549
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1427
DetectDNP3
struct DetectDNP3_ DetectDNP3
DNP3FunctionNameMap
DNP3Mapping DNP3FunctionNameMap[]
Definition: detect-dnp3.c:85
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DNP3Transaction_
DNP3 transaction.
Definition: app-layer-dnp3.h:205
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1234
DETECT_AL_DNP3OBJ
@ DETECT_AL_DNP3OBJ
Definition: detect-engine-register.h:247
DNP3Transaction_::buffer
uint8_t * buffer
Definition: app-layer-dnp3.h:213