suricata
detect-modbus.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 ANSSI
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/**
29 * \file
30 *
31 * \author David DIALLO <diallo@et.esiea.fr>
32 *
33 * Implements the Modbus function and access keywords
34 * You can specify a:
35 * - concrete function like Modbus:
36 * function 8, subfunction 4 (diagnostic: Force Listen Only Mode)
37 * - data (in primary table) register access (r/w) like Modbus:
38 * access read coils, address 1000 (.i.e Read coils: at address 1000)
39 * - write data value at specific address Modbus:
40 * access write, address 1500<>2000, value >2000 (Write multiple coils/register:
41 * at address between 1500 and 2000 value greater than 2000)
42 */
43
44#include "suricata-common.h"
45
46#include "detect.h"
47#include "detect-parse.h"
48#include "detect-engine.h"
49
50#include "detect-modbus.h"
51
52#include "util-debug.h"
53#include "util-byte.h"
54
55#include "stream-tcp.h"
56#include "rust.h"
57
58static int g_modbus_buffer_id = 0;
59
60/** \internal
61 *
62 * \brief this function will free memory associated with DetectModbus
63 *
64 * \param ptr pointer to DetectModbus
65 */
66static void DetectModbusFree(DetectEngineCtx *de_ctx, void *ptr) {
67 SCEnter();
68 if (ptr != NULL) {
69 SCModbusFree(ptr);
70 }
72}
73
74/** \internal
75 *
76 * \brief this function is used to add the parsed "id" option into the current signature
77 *
78 * \param de_ctx Pointer to the Detection Engine Context
79 * \param s Pointer to the Current Signature
80 * \param str Pointer to the user provided "id" option
81 *
82 * \retval 0 on Success or -1 on Failure
83 */
84static int DetectModbusSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
85{
86 SCEnter();
87 DetectModbusRust *modbus = NULL;
88
90 return -1;
91
92 if ((modbus = SCModbusParse(str)) == NULL) {
93 SCLogError("invalid modbus option");
94 goto error;
95 }
96
97 /* Okay so far so good, lets get this into a SigMatch and put it in the Signature. */
99 de_ctx, s, DETECT_MODBUS, (SigMatchCtx *)modbus, g_modbus_buffer_id) == NULL) {
100 goto error;
101 }
102
103 SCReturnInt(0);
104
105error:
106 if (modbus != NULL)
107 DetectModbusFree(de_ctx, modbus);
108 SCReturnInt(-1);
109}
110
111static int DetectModbusMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, void *state,
112 void *txv, const Signature *s, const SigMatchCtx *ctx)
113{
114 return SCModbusInspect(txv, (void *)ctx);
115}
116
117/**
118 * \brief Registration function for Modbus keyword
119 */
121{
123 sigmatch_table[DETECT_MODBUS].desc = "match on various properties of Modbus requests";
124 sigmatch_table[DETECT_MODBUS].url = "/rules/modbus-keyword.html#modbus-keyword";
126 sigmatch_table[DETECT_MODBUS].Setup = DetectModbusSetup;
127 sigmatch_table[DETECT_MODBUS].Free = DetectModbusFree;
128 sigmatch_table[DETECT_MODBUS].AppLayerTxMatch = DetectModbusMatch;
129
132
133 g_modbus_buffer_id = DetectBufferTypeGetByName("modbus");
134}
@ ALPROTO_MODBUS
uint8_t flags
Definition decode-gre.h:0
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
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.
void DetectModbusRegister(void)
Registration function for Modbus keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition detect.h:1424
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCReturn
Definition util-debug.h:279