suricata
detect-metadata.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Implements metadata keyword support
24  *
25  * \todo Do we need to do anything more this is used in snort host attribute table
26  * It is also used for rule management.
27  */
28 
29 #include "suricata-common.h"
30 #include "detect.h"
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-metadata.h"
34 #include "util-hash-string.h"
35 #include "util-unittest.h"
36 #include "rust.h"
37 #include "util-validate.h"
38 
39 static int DetectMetadataSetup (DetectEngineCtx *, Signature *, const char *);
40 #ifdef UNITTESTS
41 static void DetectMetadataRegisterTests(void);
42 #endif
43 
45 {
46  sigmatch_table[DETECT_METADATA].name = "metadata";
47  sigmatch_table[DETECT_METADATA].desc = "used for logging";
48  sigmatch_table[DETECT_METADATA].url = "/rules/meta.html#metadata";
50  sigmatch_table[DETECT_METADATA].Setup = DetectMetadataSetup;
52 #ifdef UNITTESTS
53  sigmatch_table[DETECT_METADATA].RegisterTests = DetectMetadataRegisterTests;
54 #endif
55 }
56 
57 /**
58  * \brief Free a Metadata object
59  */
61 {
62  SCEnter();
63 
64  SCFree(mdata);
65 
66  SCReturn;
67 }
68 
70 {
72  return 0;
73 
75  if (de_ctx->metadata_table == NULL)
76  return -1;
77  return 0;
78 }
79 
81 {
84 }
85 
86 static const char *DetectMetadataHashAdd(DetectEngineCtx *de_ctx, const char *string)
87 {
88  const char *hstring = (char *)HashTableLookup(
89  de_ctx->metadata_table, (void *)string, (uint16_t)strlen(string));
90  if (hstring) {
91  return hstring;
92  }
93 
94  const char *astring = SCStrdup(string);
95  if (astring == NULL) {
96  return NULL;
97  }
98 
99  if (HashTableAdd(de_ctx->metadata_table, (void *)astring, (uint16_t)strlen(astring)) == 0) {
100  return (char *)HashTableLookup(
101  de_ctx->metadata_table, (void *)astring, (uint16_t)strlen(astring));
102  } else {
103  SCFree((void *)astring);
104  }
105  return NULL;
106 }
107 
108 static int SortHelper(const void *a, const void *b)
109 {
110  const DetectMetadata *ma = *(const DetectMetadata **)a;
111  const DetectMetadata *mb = *(const DetectMetadata **)b;
112  return strcasecmp(ma->key, mb->key);
113 }
114 
115 static char *CraftPreformattedJSON(const DetectMetadata *head)
116 {
117  int cnt = 0;
118  for (const DetectMetadata *m = head; m != NULL; m = m->next) {
119  cnt++;
120  }
121  if (cnt == 0)
122  return NULL;
123 
124  const DetectMetadata *array[cnt];
125  int i = 0;
126  for (const DetectMetadata *m = head; m != NULL; m = m->next) {
127  array[i++] = m;
128  }
129  BUG_ON(i != cnt);
130  qsort(array, cnt, sizeof(DetectMetadata *), SortHelper);
131 
132  JsonBuilder *js = jb_new_object();
133  if (js == NULL)
134  return NULL;
135 
136  /* array is sorted by key, so we can create a jsonbuilder object
137  * with each key appearing just once with one or more values */
138  bool array_open = false;
139  for (int j = 0; j < cnt; j++) {
140  const DetectMetadata *m = array[j];
141  const DetectMetadata *nm = j + 1 < cnt ? array[j + 1] : NULL;
142  DEBUG_VALIDATE_BUG_ON(m == NULL); // for scan-build
143 
144  if (nm && strcasecmp(m->key, nm->key) == 0) {
145  if (!array_open) {
146  jb_open_array(js, m->key);
147  array_open = true;
148  }
149  jb_append_string(js, m->value);
150  } else {
151  if (!array_open) {
152  jb_open_array(js, m->key);
153  }
154  jb_append_string(js, m->value);
155  jb_close(js);
156  array_open = false;
157  }
158  }
159  jb_close(js);
160  /* we have a complete json builder. Now store it as a C string */
161  const size_t len = jb_len(js);
162 #define MD_STR "\"metadata\":"
163 #define MD_STR_LEN (sizeof(MD_STR) - 1)
164  char *str = SCMalloc(len + MD_STR_LEN + 1);
165  if (str == NULL) {
166  jb_free(js);
167  return NULL;
168  }
169  char *ptr = str;
170  memcpy(ptr, MD_STR, MD_STR_LEN);
171  ptr += MD_STR_LEN;
172  memcpy(ptr, jb_ptr(js), len);
173  ptr += len;
174  *ptr = '\0';
175 #undef MD_STR
176 #undef MD_STR_LEN
177  jb_free(js);
178  return str;
179 }
180 
181 static int DetectMetadataParse(DetectEngineCtx *de_ctx, Signature *s, const char *metadatastr)
182 {
183  DetectMetadata *head = s->metadata ? s->metadata->list : NULL;
184  char copy[strlen(metadatastr)+1];
185  strlcpy(copy, metadatastr, sizeof(copy));
186  char *xsaveptr = NULL;
187  char *key = strtok_r(copy, ",", &xsaveptr);
188  while (key != NULL) {
189  while (*key != '\0' && isblank(*key)) {
190  key++;
191  }
192  char *val = strchr(key, ' ');
193  if (val != NULL) {
194  *val++ = '\0';
195  while (*val != '\0' && isblank(*val)) {
196  val++;
197  }
198  } else {
199  /* Skip metadata without a value. */
200  goto next;
201  }
202 
203  /* Also skip metadata if the key or value is empty. */
204  if (strlen(key) == 0 || strlen(val) == 0) {
205  goto next;
206  }
207 
208  const char *hkey = DetectMetadataHashAdd(de_ctx, key);
209  if (hkey == NULL) {
210  SCLogError("can't create metadata key");
211  continue;
212  }
213 
214  const char *hval = DetectMetadataHashAdd(de_ctx, val);
215  if (hval == NULL) {
216  SCLogError("can't create metadata value");
217  goto next;
218  }
219 
220  SCLogDebug("key: %s, value: %s", hkey, hval);
221 
222  DetectMetadata *dkv = SCMalloc(sizeof(DetectMetadata));
223  if (dkv == NULL) {
224  goto next;
225  }
226  dkv->key = hkey;
227  dkv->value = hval;
228  dkv->next = head;
229  head = dkv;
230 
231  next:
232  key = strtok_r(NULL, ",", &xsaveptr);
233  }
234  if (head != NULL) {
235  if (s->metadata == NULL) {
236  s->metadata = SCCalloc(1, sizeof(*s->metadata));
237  if (s->metadata == NULL) {
238  for (DetectMetadata *m = head; m != NULL; ) {
239  DetectMetadata *next_m = m->next;
241  m = next_m;
242  }
243  return -1;
244  }
245  }
246  s->metadata->list = head;
247  SCFree(s->metadata->json_str);
248  s->metadata->json_str = CraftPreformattedJSON(head);
249  }
250  return 0;
251 }
252 
253 static int DetectMetadataSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
254 {
256  DetectMetadataParse(de_ctx, s, rawstr);
257  }
258 
259  return 0;
260 }
261 
262 #ifdef UNITTESTS
263 
264 static int DetectMetadataParseTest01(void)
265 {
269 
271  "alert tcp any any -> any any "
272  "(metadata: toto 1; sid:1; rev:1;)");
273  FAIL_IF_NULL(sig);
274  FAIL_IF(sig->metadata);
275 
277  PASS;
278 }
279 
280 static int DetectMetadataParseTest02(void)
281 {
286  "alert tcp any any -> any any "
287  "(metadata: toto 1; "
288  "metadata: titi 2, jaivu gros_minet;"
289  "sid:1; rev:1;)");
290  FAIL_IF_NULL(sig);
291  FAIL_IF_NULL(sig->metadata);
292  FAIL_IF_NULL(sig->metadata->list);
293  FAIL_IF_NULL(sig->metadata->list->key);
294  FAIL_IF(strcmp("jaivu", sig->metadata->list->key));
295  FAIL_IF(strcmp("gros_minet", sig->metadata->list->value));
296  FAIL_IF_NULL(sig->metadata->list->next);
297  DetectMetadata *dm = sig->metadata->list->next;
298  FAIL_IF(strcmp("titi", dm->key));
299  dm = dm->next;
300  FAIL_IF_NULL(dm);
301  FAIL_IF(strcmp("toto", dm->key));
302  FAIL_IF_NOT(strcmp(sig->metadata->json_str,
303  "\"metadata\":{\"jaivu\":[\"gros_minet\"],\"titi\":[\"2\"],\"toto\":[\"1\"]}") == 0);
305  PASS;
306 }
307 
308 /**
309  * \brief this function registers unit tests for DetectCipService
310  */
311 static void DetectMetadataRegisterTests(void)
312 {
313  UtRegisterTest("DetectMetadataParseTest01", DetectMetadataParseTest01);
314  UtRegisterTest("DetectMetadataParseTest02", DetectMetadataParseTest02);
315 }
316 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1299
util-hash-string.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectMetadataHead::json_str
char * json_str
Definition: detect-metadata.h:40
DetectMetadataFree
void DetectMetadataFree(DetectMetadata *mdata)
Free a Metadata object.
Definition: detect-metadata.c:60
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
DetectMetadata_::value
const char * value
Definition: detect-metadata.h:34
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
DetectMetadata_::key
const char * key
Definition: detect-metadata.h:32
DetectEngineMustParseMetadata
int DetectEngineMustParseMetadata(void)
Definition: detect-engine.c:4816
SigTableElmt_::name
const char * name
Definition: detect.h:1296
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
DetectMetadataHashInit
int DetectMetadataHashInit(DetectEngineCtx *de_ctx)
Definition: detect-metadata.c:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2533
rust.h
m
SCMutex m
Definition: flow-hash.h:6
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2620
DetectMetadataRegister
void DetectMetadataRegister(void)
Definition: detect-metadata.c:44
StringHashCompareFunc
char StringHashCompareFunc(void *data1, uint16_t datalen1, void *data2, uint16_t datalen2)
Definition: util-hash-string.c:38
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
StringHashFunc
uint32_t StringHashFunc(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash-string.c:33
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
HashTableFree
void HashTableFree(HashTable *ht)
Definition: util-hash.c:78
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:183
DetectMetadataHashFree
void DetectMetadataHashFree(DetectEngineCtx *de_ctx)
Definition: detect-metadata.c:80
DETECT_METADATA
@ DETECT_METADATA
Definition: detect-engine-register.h:58
detect.h
HashTableAdd
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:104
StringHashFreeFunc
void StringHashFreeFunc(void *data)
Definition: util-hash-string.c:51
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
DetectMetadataHead::list
DetectMetadata * list
Definition: detect-metadata.h:41
SCReturn
#define SCReturn
Definition: util-debug.h:273
MD_STR_LEN
#define MD_STR_LEN
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
DetectMetadata_
Signature metadata list.
Definition: detect-metadata.h:30
MD_STR
#define MD_STR
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
detect-metadata.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
util-validate.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:291
Signature_::metadata
DetectMetadataHead * metadata
Definition: detect.h:661
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
head
Flow * head
Definition: flow-hash.h:1
DetectEngineUnsetParseMetadata
void DetectEngineUnsetParseMetadata(void)
Definition: detect-engine.c:4811
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectMetadata_::next
struct DetectMetadata_ * next
Definition: detect-metadata.h:36
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
HashTableInit
HashTable * HashTableInit(uint32_t size, uint32_t(*Hash)(struct HashTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hash.c:35
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2494
DetectEngineSetParseMetadata
void DetectEngineSetParseMetadata(void)
Definition: detect-engine.c:4806
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::metadata_table
HashTable * metadata_table
Definition: detect.h:986
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:103
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288