You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/detect-reference.c

384 lines
9.9 KiB
C

/* Copyright (C) 2007-2010 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
15 years ago
/**
* \file
*
* \author Breno Silva <breno.silva@gmail.com>
* \author Anoop Saldanha <anoopsaldanha@gmail.com>
*
* Implements the reference keyword support
15 years ago
*/
#include "suricata-common.h"
15 years ago
#include "suricata.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "decode.h"
#include "flow-var.h"
#include "decode-events.h"
#include "stream-tcp.h"
#include "util-reference-config.h"
15 years ago
#include "detect-reference.h"
#include "util-unittest.h"
#include "util-byte.h"
#include "util-debug.h"
#define PARSE_REGEX "^\\s*([A-Za-z0-9]+)\\s*,\"?\\s*\"?\\s*([a-zA-Z0-9\\-_\\.\\/\\?\\=]+)\"?\\s*\"?"
15 years ago
static pcre *parse_regex;
static pcre_extra *parse_regex_study;
static int DetectReferenceSetup(DetectEngineCtx *, Signature *s, char *str);
15 years ago
/**
* \brief Registration function for the reference: keyword
15 years ago
*/
void DetectReferenceRegister(void)
{
sigmatch_table[DETECT_REFERENCE].name = "reference";
sigmatch_table[DETECT_REFERENCE].Match = NULL;
sigmatch_table[DETECT_REFERENCE].Setup = DetectReferenceSetup;
sigmatch_table[DETECT_REFERENCE].Free = NULL;
15 years ago
sigmatch_table[DETECT_REFERENCE].RegisterTests = ReferenceRegisterTests;
const char *eb;
int opts = 0;
int eo;
opts |= PCRE_CASELESS;
15 years ago
parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
if (parse_regex == NULL) {
SCLogError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at "
"offset %" PRId32 ": %s", PARSE_REGEX, eo, eb);
15 years ago
goto error;
}
parse_regex_study = pcre_study(parse_regex, 0, &eb);
if (eb != NULL) {
15 years ago
SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
goto error;
}
error:
return;
}
/**
* \brief Free a Reference object
*/
void DetectReferenceFree(DetectReference *ref)
{
SCEnter();
if (ref->reference != NULL) {
SCFree(ref->reference);
}
SCFree(ref);
SCReturn;
}
15 years ago
/**
* \internal
* \brief This function is used to parse reference options passed via reference: keyword
*
* \param rawstr Pointer to the user provided reference options.
15 years ago
*
* \retval ref Pointer to signature reference on success.
* \retval NULL On failure.
15 years ago
*/
static DetectReference *DetectReferenceParse(char *rawstr, DetectEngineCtx *de_ctx)
{
SCEnter();
DetectReference *ref = NULL;
15 years ago
#define MAX_SUBSTRINGS 30
int ret = 0, res = 0;
int ov[MAX_SUBSTRINGS];
const char *key = NULL;
const char *content = NULL;
15 years ago
ret = pcre_exec(parse_regex, parse_regex_study, rawstr, strlen(rawstr),
0, 0, ov, MAX_SUBSTRINGS);
15 years ago
if (ret < 2) {
SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, "
"ret %" PRId32 ", string %s", ret, rawstr);
15 years ago
goto error;
}
ref = SCMalloc(sizeof(DetectReference));
15 years ago
if (ref == NULL) {
goto error;
}
memset(ref, 0, sizeof(DetectReference));
15 years ago
res = pcre_get_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 1, &key);
15 years ago
if (res < 0) {
SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
goto error;
}
res = pcre_get_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 2, &content);
15 years ago
if (res < 0) {
SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
goto error;
}
if (key == NULL || content == NULL)
15 years ago
goto error;
SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
if (lookup_ref_conf != NULL) {
ref->key = lookup_ref_conf->url;
} else {
SCLogError(SC_ERR_REFERENCE_UNKNOWN, "unknown reference key \"%s\". "
"Supported keys are defined in reference.config file. Please "
"have a look at the conf param \"reference-config-file\"", key);
goto error;
15 years ago
}
/* make a copy so we can free pcre's substring */
ref->reference = SCStrdup((char *)content);
if (ref->reference == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "strdup failed: %s", strerror(errno));
15 years ago
goto error;
}
/* free the substrings */
pcre_free_substring(key);
pcre_free_substring(content);
SCReturnPtr(ref, "Reference");
15 years ago
error:
if (key != NULL)
pcre_free_substring(key);
if (content != NULL)
pcre_free_substring(content);
if (ref != NULL)
DetectReferenceFree(ref);
15 years ago
SCReturnPtr(NULL, "Reference");
15 years ago
}
/**
* \internal
* \brief Used to add the parsed reference into the current signature.
15 years ago
*
* \param de_ctx Pointer to the Detection Engine Context.
* \param s Pointer to the Current Signature.
* \param m Pointer to the Current SigMatch.
* \param rawstr Pointer to the user provided reference options.
15 years ago
*
* \retval 0 On Success.
* \retval -1 On Failure.
15 years ago
*/
static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
char *rawstr)
15 years ago
{
SCEnter();
DetectReference *ref = NULL;
DetectReference *sig_refs = NULL;
15 years ago
ref = DetectReferenceParse(rawstr, de_ctx);
15 years ago
if (ref == NULL)
goto error;
SCLogDebug("ref %s %s", ref->key, ref->reference);
15 years ago
if (s->references == NULL) {
s->references = ref;
15 years ago
} else {
sig_refs = s->references;
while (sig_refs->next != NULL) {
sig_refs = sig_refs->next;
15 years ago
}
sig_refs->next = ref;
ref->next = NULL;
15 years ago
}
SCReturnInt(0);
15 years ago
error:
SCReturnInt(-1);
15 years ago
}
/***************************************Unittests******************************/
15 years ago
#ifdef UNITTESTS
/**
* \test one valid reference.
15 years ago
*
* \retval 1 on succces.
* \retval 0 on failure.
15 years ago
*/
static int DetectReferenceParseTest01(void)
{
int result = 0;
Signature *s = NULL;
DetectReference *ref = NULL;
15 years ago
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
if (de_ctx == NULL) {
goto cleanup;
15 years ago
}
de_ctx->flags |= DE_QUIET;
SCRConfGenerateValidDummyReferenceConfigFD01();
SCRConfLoadReferenceConfigFile(de_ctx);
SCRConfDeleteDummyReferenceConfigFD();
15 years ago
s = de_ctx->sig_list = SigInit(de_ctx, "alert icmp any any -> any any "
"(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
15 years ago
if (s == NULL) {
goto cleanup;
15 years ago
}
if (s->references == NULL) {
15 years ago
goto cleanup;
}
ref = s->references;
if (strcmp(ref->key, "http://www.one.com") != 0 ||
strcmp(ref->reference, "001-2010") != 0) {
goto cleanup;
15 years ago
}
result = 1;
cleanup:
if (de_ctx != NULL) {
DetectEngineCtxFree(de_ctx);
}
15 years ago
return result;
}
15 years ago
/**
* \test for two valid references.
15 years ago
*
* \retval 1 on succces.
* \retval 0 on failure.
15 years ago
*/
static int DetectReferenceParseTest02(void)
{
int result = 0;
Signature *s = NULL;
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
if (de_ctx == NULL) {
goto cleanup;
15 years ago
}
de_ctx->flags |= DE_QUIET;
SCRConfGenerateValidDummyReferenceConfigFD01();
SCRConfLoadReferenceConfigFile(de_ctx);
SCRConfDeleteDummyReferenceConfigFD();
s = de_ctx->sig_list = SigInit(de_ctx, "alert icmp any any -> any any "
"(msg:\"Two references\"; "
"reference:one,openinfosecdoundation.txt; "
"reference:two,001-2010; sid:2;)");
15 years ago
if (s == NULL) {
printf("sig parse failed: ");
goto cleanup;
15 years ago
}
if (s->references == NULL || s->references->next == NULL) {
printf("no ref or not enough refs: ");
15 years ago
goto cleanup;
}
if (strcmp(s->references->key, "http://www.one.com") != 0 ||
strcmp(s->references->reference, "openinfosecdoundation.txt") != 0) {
printf("first ref failed: ");
goto cleanup;
15 years ago
}
if (strcmp(s->references->next->key, "http://www.two.com") != 0 ||
strcmp(s->references->next->reference, "001-2010") != 0) {
printf("second ref failed: ");
goto cleanup;
15 years ago
}
result = 1;
15 years ago
cleanup:
if (de_ctx != NULL) {
DetectEngineCtxFree(de_ctx);
}
15 years ago
return result;
}
/**
* \test parsing: invalid reference.
15 years ago
*
* \retval 1 on succces.
* \retval 0 on failure.
15 years ago
*/
static int DetectReferenceParseTest03(void)
{
int result = 0;
Signature *s = NULL;
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
if (de_ctx == NULL) {
goto cleanup;
15 years ago
}
de_ctx->flags |= DE_QUIET;
SCRConfGenerateValidDummyReferenceConfigFD01();
SCRConfLoadReferenceConfigFile(de_ctx);
SCRConfDeleteDummyReferenceConfigFD();
s = de_ctx->sig_list = SigInit(de_ctx, "alert icmp any any -> any any "
"(msg:\"invalid ref\"; "
"reference:unknownkey,001-2010; sid:2;)");
if (s != NULL) {
printf("sig parsed even though it's invalid: ");
15 years ago
goto cleanup;
}
result = 1;
15 years ago
cleanup:
if (de_ctx != NULL) {
DetectEngineCtxFree(de_ctx);
}
15 years ago
return result;
}
15 years ago
#endif /* UNITTESTS */
void ReferenceRegisterTests(void)
{
15 years ago
#ifdef UNITTESTS
UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01, 1);
UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02, 1);
UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03, 1);
15 years ago
#endif /* UNITTESTS */
return;
15 years ago
}