mirror of https://github.com/OISF/suricata
Changed printf's to logging API functions
parent
e26833be3f
commit
991d421394
@ -0,0 +1,479 @@
|
||||
/**
|
||||
* Copyright (c) 2009 Open Information Security Foundation
|
||||
*
|
||||
* \file detect-icode.c
|
||||
* \author Gerardo Iglesias <iglesiasg@gmail.com>
|
||||
*
|
||||
* "icode" keyword support
|
||||
*/
|
||||
|
||||
#include "eidps-common.h"
|
||||
#include "debug.h"
|
||||
#include "decode.h"
|
||||
#include "detect.h"
|
||||
|
||||
#include "detect-icode.h"
|
||||
|
||||
#include "util-byte.h"
|
||||
#include "util-unittest.h"
|
||||
#include "util-debug.h"
|
||||
|
||||
/**
|
||||
*\brief Regex for parsing our icode options
|
||||
*/
|
||||
#define PARSE_REGEX "^\\s*(<|>)?\\s*([0-9]+)\\s*(?:<>\\s*([0-9]+))?\\s*$"
|
||||
|
||||
static pcre *parse_regex;
|
||||
static pcre_extra *parse_regex_study;
|
||||
|
||||
int DetectICodeMatch(ThreadVars *, DetectEngineThreadCtx *, Packet *, Signature *, SigMatch *);
|
||||
int DetectICodeSetup(DetectEngineCtx *, Signature *, SigMatch *, char *);
|
||||
void DetectICodeRegisterTests(void);
|
||||
void DetectICodeFree(void *);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Registration function for icode: keyword
|
||||
*/
|
||||
void DetectICodeRegister (void) {
|
||||
sigmatch_table[DETECT_ICODE].name = "icode";
|
||||
sigmatch_table[DETECT_ICODE].Match = DetectICodeMatch;
|
||||
sigmatch_table[DETECT_ICODE].Setup = DetectICodeSetup;
|
||||
sigmatch_table[DETECT_ICODE].Free = DetectICodeFree;
|
||||
sigmatch_table[DETECT_ICODE].RegisterTests = DetectICodeRegisterTests;
|
||||
|
||||
const char *eb;
|
||||
int eo;
|
||||
int opts = 0;
|
||||
|
||||
parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
|
||||
if(parse_regex == NULL)
|
||||
{
|
||||
SCLogError(SC_PCRE_COMPILE_FAILED, "pcre compile of \"%s\" failed at offset %" PRId32 ": %s", PARSE_REGEX, eo, eb);
|
||||
goto error;
|
||||
}
|
||||
|
||||
parse_regex_study = pcre_study(parse_regex, 0, &eb);
|
||||
if(eb != NULL)
|
||||
{
|
||||
SCLogError(SC_PCRE_COMPILE_FAILED, "pcre study failed: %s", eb);
|
||||
goto error;
|
||||
}
|
||||
return;
|
||||
|
||||
error:
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function is used to match icode rule option set on a packet with those passed via icode:
|
||||
*
|
||||
* \param t pointer to thread vars
|
||||
* \param det_ctx pointer to the pattern matcher thread
|
||||
* \param p pointer to the current packet
|
||||
* \param m pointer to the sigmatch that we will cast into DetectICodeData
|
||||
*
|
||||
* \retval 0 no match
|
||||
* \retval 1 match
|
||||
*/
|
||||
int DetectICodeMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p, Signature *s, SigMatch *m) {
|
||||
int ret = 0;
|
||||
uint8_t picode;
|
||||
DetectICodeData *icd = (DetectICodeData *)m->ctx;
|
||||
|
||||
if (PKT_IS_ICMPV4(p)) {
|
||||
picode = ICMPV4_GET_CODE(p);
|
||||
} else if (PKT_IS_ICMPV6(p)) {
|
||||
picode = ICMPV6_GET_CODE(p);
|
||||
} else {
|
||||
/* Packet not ICMPv4 nor ICMPv6 */
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch(icd->mode) {
|
||||
case DETECT_ICODE_EQ:
|
||||
ret = (picode == icd->code1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ICODE_LT:
|
||||
ret = (picode < icd->code1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ICODE_GT:
|
||||
ret = (picode > icd->code1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ICODE_RN:
|
||||
ret = (picode >= icd->code1 && picode <= icd->code2) ? 1 : 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function is used to parse icode options passed via icode: keyword
|
||||
*
|
||||
* \param icodestr Pointer to the user provided icode options
|
||||
*
|
||||
* \retval icd pointer to DetectICodeData on success
|
||||
* \retval NULL on failure
|
||||
*/
|
||||
DetectICodeData *DetectICodeParse(char *icodestr) {
|
||||
DetectICodeData *icd = NULL;
|
||||
char *args[3] = {NULL, NULL, NULL};
|
||||
#define MAX_SUBSTRINGS 30
|
||||
int ret = 0, res = 0;
|
||||
int ov[MAX_SUBSTRINGS];
|
||||
|
||||
ret = pcre_exec(parse_regex, parse_regex_study, icodestr, strlen(icodestr), 0, 0, ov, MAX_SUBSTRINGS);
|
||||
if (ret < 1 || ret > 4) {
|
||||
SCLogError(SC_PCRE_MATCH_FAILED, "DetectICodeParse: parse error");
|
||||
goto error;
|
||||
}
|
||||
|
||||
int i;
|
||||
const char *str_ptr;
|
||||
for (i = 1; i < ret; i++) {
|
||||
res = pcre_get_substring((char *)icodestr, ov, MAX_SUBSTRINGS, i, &str_ptr);
|
||||
if (res < 0) {
|
||||
SCLogError(SC_PCRE_GET_SUBSTRING_FAILED, "DetectICodeParse: pcre_get_substring failed");
|
||||
goto error;
|
||||
}
|
||||
args[i-1] = (char *)str_ptr;
|
||||
}
|
||||
|
||||
icd = malloc(sizeof(DetectICodeData));
|
||||
if (icd == NULL) {
|
||||
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
|
||||
goto error;
|
||||
}
|
||||
icd->code1 = 0;
|
||||
icd->code2 = 0;
|
||||
icd->mode = 0;
|
||||
|
||||
/* we have either "<" or ">" */
|
||||
if (strlen(args[0]) != 0) {
|
||||
/* we have a third part ("<> y"), therefore it's invalid */
|
||||
if (args[2] != NULL) {
|
||||
SCLogInfo("icode: invalid value");
|
||||
goto error;
|
||||
}
|
||||
/* we have only a comparison ("<", ">") */
|
||||
ByteExtractStringUint8(&icd->code1, 10, 0, args[1]);
|
||||
if ((strcmp(args[0], ">")) == 0) icd->mode = DETECT_ICODE_GT;
|
||||
else icd->mode = DETECT_ICODE_LT;
|
||||
} else { /* no "<", ">" */
|
||||
/* we have a range ("<>") */
|
||||
if (args[2] != NULL) {
|
||||
icd->mode = (uint8_t) DETECT_ICODE_RN;
|
||||
ByteExtractStringUint8(&icd->code1, 10, 0, args[1]);
|
||||
ByteExtractStringUint8(&icd->code2, 10, 0, args[2]);
|
||||
/* we check that the first given value in the range is less than
|
||||
the second, otherwise we swap them */
|
||||
if (icd->code1 > icd->code2) {
|
||||
uint8_t temp = icd->code1;
|
||||
icd->code1 = icd->code2;
|
||||
icd->code2 = temp;
|
||||
}
|
||||
} else { /* we have an equality */
|
||||
icd->mode = DETECT_ICODE_EQ;
|
||||
ByteExtractStringUint8(&icd->code1, 10, 0, args[1]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < (ret-1); i++) {
|
||||
if (args[i] != NULL) free(args[i]);
|
||||
}
|
||||
return icd;
|
||||
|
||||
error:
|
||||
for (i = 0; i < (ret-1); i++) {
|
||||
if (args[i] != NULL) free(args[i]);
|
||||
}
|
||||
if (icd != NULL) DetectICodeFree(icd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief this function is used to add the parsed icode data into the current signature
|
||||
*
|
||||
* \param de_ctx pointer to the Detection Engine Context
|
||||
* \param s pointer to the Current Signature
|
||||
* \param m pointer to the Current SigMatch
|
||||
* \param icodestr pointer to the user provided icode options
|
||||
*
|
||||
* \retval 0 on Success
|
||||
* \retval -1 on Failure
|
||||
*/
|
||||
int DetectICodeSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *icodestr) {
|
||||
|
||||
DetectICodeData *icd = NULL;
|
||||
SigMatch *sm = NULL;
|
||||
|
||||
icd = DetectICodeParse(icodestr);
|
||||
if (icd == NULL) goto error;
|
||||
|
||||
sm = SigMatchAlloc();
|
||||
if (sm == NULL) goto error;
|
||||
|
||||
sm->type = DETECT_ICODE;
|
||||
sm->ctx = (void *)icd;
|
||||
|
||||
SigMatchAppend(s, m, sm);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
if (icd != NULL) DetectICodeFree(icd);
|
||||
if (sm != NULL) free(sm);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief this function will free memory associated with DetectICodeData
|
||||
*
|
||||
* \param ptr pointer to DetectICodeData
|
||||
*/
|
||||
void DetectICodeFree(void *ptr) {
|
||||
DetectICodeData *icd = (DetectICodeData *)ptr;
|
||||
free(icd);
|
||||
}
|
||||
|
||||
#ifdef UNITTESTS
|
||||
|
||||
#include "detect-parse.h"
|
||||
#include "detect-engine.h"
|
||||
#include "detect-engine-mpm.h"
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest01 is a test for setting a valid icode value
|
||||
*/
|
||||
int DetectICodeParseTest01(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse("8");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_EQ)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest02 is a test for setting a valid icode value
|
||||
* with ">" operator
|
||||
*/
|
||||
int DetectICodeParseTest02(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse(">8");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_GT)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest03 is a test for setting a valid icode value
|
||||
* with "<" operator
|
||||
*/
|
||||
int DetectICodeParseTest03(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse("<8");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_LT)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest04 is a test for setting a valid icode value
|
||||
* with "<>" operator
|
||||
*/
|
||||
int DetectICodeParseTest04(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse("8<>20");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->code2 == 20 && icd->mode == DETECT_ICODE_RN)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest05 is a test for setting a valid icode value
|
||||
* with spaces all around
|
||||
*/
|
||||
int DetectICodeParseTest05(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse(" 8 ");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_EQ)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest06 is a test for setting a valid icode value
|
||||
* with ">" operator and spaces all around
|
||||
*/
|
||||
int DetectICodeParseTest06(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse(" > 8 ");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_GT)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest07 is a test for setting a valid icode value
|
||||
* with "<>" operator and spaces all around
|
||||
*/
|
||||
int DetectICodeParseTest07(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
int result = 0;
|
||||
icd = DetectICodeParse(" 8 <> 20 ");
|
||||
if (icd != NULL) {
|
||||
if (icd->code1 == 8 && icd->code2 == 20 && icd->mode == DETECT_ICODE_RN)
|
||||
result = 1;
|
||||
DetectICodeFree(icd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeParseTest08 is a test for setting an invalid icode value
|
||||
*/
|
||||
int DetectICodeParseTest08(void) {
|
||||
DetectICodeData *icd = NULL;
|
||||
icd = DetectICodeParse("> 8 <> 20");
|
||||
if (icd == NULL)
|
||||
return 1;
|
||||
DetectICodeFree(icd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectICodeMatchTest01 is a test for checking the working of icode
|
||||
* keyword by creating 5 rules and matching a crafted packet against
|
||||
* them. 4 out of 5 rules shall trigger.
|
||||
*/
|
||||
int DetectICodeMatchTest01(void) {
|
||||
|
||||
Packet p;
|
||||
Signature *s = NULL;
|
||||
ThreadVars th_v;
|
||||
DetectEngineThreadCtx *det_ctx;
|
||||
int result = 0;
|
||||
IPV4Hdr ip4h;
|
||||
ICMPV4Hdr icmpv4h;
|
||||
|
||||
memset(&th_v, 0, sizeof(th_v));
|
||||
memset(&p, 0, sizeof(p));
|
||||
memset(&ip4h, 0, sizeof(ip4h));
|
||||
memset(&icmpv4h, 0, sizeof(icmpv4h));
|
||||
|
||||
p.src.family = AF_INET;
|
||||
p.dst.family = AF_INET;
|
||||
p.proto = IPPROTO_ICMP;
|
||||
p.ip4h = &ip4h;
|
||||
icmpv4h.code = 10;
|
||||
p.icmpv4h = &icmpv4h;
|
||||
|
||||
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||||
if (de_ctx == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
de_ctx->flags |= DE_QUIET;
|
||||
|
||||
s = de_ctx->sig_list = SigInit(de_ctx,"alert icmp any any -> any any (icode:10; sid:1;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:<15; sid:2;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:>20; sid:3;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:8<>20; sid:4;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:20<>8; sid:5;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
SigGroupBuild(de_ctx);
|
||||
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||||
|
||||
SigMatchSignatures(&th_v, de_ctx, det_ctx, &p);
|
||||
if (PacketAlertCheck(&p, 1) == 0) {
|
||||
SCLogDebug("sid 1 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 2) == 0) {
|
||||
SCLogDebug("sid 2 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 3)) {
|
||||
SCLogDebug("sid 3 alerted, but should not have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 4) == 0) {
|
||||
SCLogDebug("sid 4 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 5) == 0) {
|
||||
SCLogDebug("sid 5 did not alert, but should have");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
result = 1;
|
||||
|
||||
cleanup:
|
||||
SigGroupCleanup(de_ctx);
|
||||
SigCleanSignatures(de_ctx);
|
||||
|
||||
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
|
||||
DetectEngineCtxFree(de_ctx);
|
||||
|
||||
end:
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* UNITTESTS */
|
||||
|
||||
/**
|
||||
* \brief this function registers unit tests for DetectICode
|
||||
*/
|
||||
void DetectICodeRegisterTests(void) {
|
||||
#ifdef UNITTESTS
|
||||
UtRegisterTest("DetectICodeParseTest01", DetectICodeParseTest01, 1);
|
||||
UtRegisterTest("DetectICodeParseTest02", DetectICodeParseTest02, 1);
|
||||
UtRegisterTest("DetectICodeParseTest03", DetectICodeParseTest03, 1);
|
||||
UtRegisterTest("DetectICodeParseTest04", DetectICodeParseTest04, 1);
|
||||
UtRegisterTest("DetectICodeParseTest05", DetectICodeParseTest05, 1);
|
||||
UtRegisterTest("DetectICodeParseTest06", DetectICodeParseTest06, 1);
|
||||
UtRegisterTest("DetectICodeParseTest07", DetectICodeParseTest07, 1);
|
||||
UtRegisterTest("DetectICodeParseTest08", DetectICodeParseTest08, 1);
|
||||
UtRegisterTest("DetectICodeMatchTest01", DetectICodeMatchTest01, 1);
|
||||
#endif /* UNITTESTS */
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) 2009 Open Information Security Foundation
|
||||
*/
|
||||
|
||||
#ifndef __DETECT_ICODE_H__
|
||||
#define __DETECT_ICODE_H__
|
||||
|
||||
#define DETECT_ICODE_EQ 0 /**< "equal" operator */
|
||||
#define DETECT_ICODE_LT 1 /**< "less than" operator */
|
||||
#define DETECT_ICODE_GT 2 /**< "greater than" operator */
|
||||
#define DETECT_ICODE_RN 3 /**< "range" operator */
|
||||
|
||||
typedef struct DetectICodeData_ {
|
||||
uint8_t code1;
|
||||
uint8_t code2;
|
||||
|
||||
uint8_t mode;
|
||||
}DetectICodeData;
|
||||
|
||||
/* prototypes */
|
||||
void DetectICodeRegister(void);
|
||||
|
||||
#endif /* __DETECT_ICODE_H__ */
|
||||
@ -0,0 +1,481 @@
|
||||
/**
|
||||
* Copyright (c) 2009 Open Information Security Foundation
|
||||
*
|
||||
* \file detect-itype.c
|
||||
* \author Gerardo Iglesias <iglesiasg@gmail.com>
|
||||
*
|
||||
* "itype" keyword support
|
||||
*/
|
||||
|
||||
#include "eidps-common.h"
|
||||
#include "debug.h"
|
||||
#include "decode.h"
|
||||
#include "detect.h"
|
||||
|
||||
#include "detect-itype.h"
|
||||
|
||||
#include "util-byte.h"
|
||||
#include "util-unittest.h"
|
||||
#include "util-debug.h"
|
||||
|
||||
/**
|
||||
*\brief Regex for parsing our itype options
|
||||
*/
|
||||
#define PARSE_REGEX "^\\s*(<|>)?\\s*([0-9]+)\\s*(?:<>\\s*([0-9]+))?\\s*$"
|
||||
|
||||
static pcre *parse_regex;
|
||||
static pcre_extra *parse_regex_study;
|
||||
|
||||
int DetectITypeMatch(ThreadVars *, DetectEngineThreadCtx *, Packet *, Signature *, SigMatch *);
|
||||
int DetectITypeSetup(DetectEngineCtx *, Signature *, SigMatch *, char *);
|
||||
void DetectITypeRegisterTests(void);
|
||||
void DetectITypeFree(void *);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Registration function for itype: keyword
|
||||
*/
|
||||
void DetectITypeRegister (void) {
|
||||
sigmatch_table[DETECT_ITYPE].name = "itype";
|
||||
sigmatch_table[DETECT_ITYPE].Match = DetectITypeMatch;
|
||||
sigmatch_table[DETECT_ITYPE].Setup = DetectITypeSetup;
|
||||
sigmatch_table[DETECT_ITYPE].Free = DetectITypeFree;
|
||||
sigmatch_table[DETECT_ITYPE].RegisterTests = DetectITypeRegisterTests;
|
||||
|
||||
const char *eb;
|
||||
int eo;
|
||||
int opts = 0;
|
||||
|
||||
parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
|
||||
if(parse_regex == NULL)
|
||||
{
|
||||
SCLogError(SC_PCRE_COMPILE_FAILED, "pcre compile of \"%s\" failed at offset %" PRId32 ": %s", PARSE_REGEX, eo, eb);
|
||||
goto error;
|
||||
}
|
||||
|
||||
parse_regex_study = pcre_study(parse_regex, 0, &eb);
|
||||
if(eb != NULL)
|
||||
{
|
||||
SCLogError(SC_PCRE_COMPILE_FAILED, "pcre study failed: %s", eb);
|
||||
goto error;
|
||||
}
|
||||
return;
|
||||
|
||||
error:
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function is used to match itype rule option set on a packet with those passed via itype:
|
||||
*
|
||||
* \param t pointer to thread vars
|
||||
* \param det_ctx pointer to the pattern matcher thread
|
||||
* \param p pointer to the current packet
|
||||
* \param m pointer to the sigmatch that we will cast into DetectITypeData
|
||||
*
|
||||
* \retval 0 no match
|
||||
* \retval 1 match
|
||||
*/
|
||||
int DetectITypeMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p, Signature *s, SigMatch *m) {
|
||||
int ret = 0;
|
||||
uint8_t pitype;
|
||||
DetectITypeData *itd = (DetectITypeData *)m->ctx;
|
||||
|
||||
if (PKT_IS_ICMPV4(p)) {
|
||||
pitype = ICMPV4_GET_TYPE(p);
|
||||
} else if (PKT_IS_ICMPV6(p)) {
|
||||
pitype = ICMPV6_GET_TYPE(p);
|
||||
} else {
|
||||
/* Packet not ICMPv4 nor ICMPv6 */
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch(itd->mode) {
|
||||
case DETECT_ITYPE_EQ:
|
||||
ret = (pitype == itd->type1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ITYPE_LT:
|
||||
ret = (pitype < itd->type1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ITYPE_GT:
|
||||
ret = (pitype > itd->type1) ? 1 : 0;
|
||||
break;
|
||||
case DETECT_ITYPE_RN:
|
||||
ret = (pitype > itd->type1 && pitype < itd->type2) ? 1 : 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief This function is used to parse itype options passed via itype: keyword
|
||||
*
|
||||
* \param itypestr Pointer to the user provided itype options
|
||||
*
|
||||
* \retval itd pointer to DetectITypeData on success
|
||||
* \retval NULL on failure
|
||||
*/
|
||||
DetectITypeData *DetectITypeParse(char *itypestr) {
|
||||
DetectITypeData *itd = NULL;
|
||||
char *args[3] = {NULL, NULL, NULL};
|
||||
#define MAX_SUBSTRINGS 30
|
||||
int ret = 0, res = 0;
|
||||
int ov[MAX_SUBSTRINGS];
|
||||
|
||||
ret = pcre_exec(parse_regex, parse_regex_study, itypestr, strlen(itypestr), 0, 0, ov, MAX_SUBSTRINGS);
|
||||
if (ret < 1 || ret > 4) {
|
||||
SCLogError(SC_PCRE_MATCH_FAILED, "DetectITypeParse: parse error");
|
||||
goto error;
|
||||
}
|
||||
|
||||
int i;
|
||||
const char *str_ptr;
|
||||
for (i = 1; i < ret; i++) {
|
||||
res = pcre_get_substring((char *)itypestr, ov, MAX_SUBSTRINGS, i, &str_ptr);
|
||||
if (res < 0) {
|
||||
SCLogError(SC_PCRE_GET_SUBSTRING_FAILED, "DetectITypeParse: pcre_get_substring failed");
|
||||
goto error;
|
||||
}
|
||||
args[i-1] = (char *)str_ptr;
|
||||
}
|
||||
|
||||
itd = malloc(sizeof(DetectITypeData));
|
||||
if (itd == NULL) {
|
||||
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
|
||||
goto error;
|
||||
}
|
||||
itd->type1 = 0;
|
||||
itd->type2 = 0;
|
||||
itd->mode = 0;
|
||||
|
||||
/* we have either "<" or ">" */
|
||||
if (strlen(args[0]) != 0) {
|
||||
/* we have a third part ("<> y"), therefore it's invalid */
|
||||
if (args[2] != NULL) {
|
||||
SCLogInfo("itype: invalid value");
|
||||
goto error;
|
||||
}
|
||||
/* we have only a comparison ("<", ">") */
|
||||
ByteExtractStringUint8(&itd->type1, 10, 0, args[1]);
|
||||
if ((strcmp(args[0], ">")) == 0) itd->mode = DETECT_ITYPE_GT;
|
||||
else itd->mode = DETECT_ITYPE_LT;
|
||||
} else { /* no "<", ">" */
|
||||
/* we have a range ("<>") */
|
||||
if (args[2] != NULL) {
|
||||
itd->mode = (uint8_t) DETECT_ITYPE_RN;
|
||||
ByteExtractStringUint8(&itd->type1, 10, 0, args[1]);
|
||||
ByteExtractStringUint8(&itd->type2, 10, 0, args[2]);
|
||||
/* we check that the first given value in the range is less than
|
||||
the second, otherwise we swap them */
|
||||
if (itd->type1 > itd->type2) {
|
||||
uint8_t temp = itd->type1;
|
||||
itd->type1 = itd->type2;
|
||||
itd->type2 = temp;
|
||||
}
|
||||
} else { /* we have an equality */
|
||||
itd->mode = DETECT_ITYPE_EQ;
|
||||
ByteExtractStringUint8(&itd->type1, 10, 0, args[1]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < (ret-1); i++) {
|
||||
if (args[i] != NULL) free(args[i]);
|
||||
}
|
||||
return itd;
|
||||
|
||||
error:
|
||||
for (i = 0; i < (ret-1); i++) {
|
||||
if (args[i] != NULL) free(args[i]);
|
||||
}
|
||||
if (itd != NULL) DetectITypeFree(itd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief this function is used to add the parsed itype data into the current signature
|
||||
*
|
||||
* \param de_ctx pointer to the Detection Engine Context
|
||||
* \param s pointer to the Current Signature
|
||||
* \param m pointer to the Current SigMatch
|
||||
* \param itypestr pointer to the user provided itype options
|
||||
*
|
||||
* \retval 0 on Success
|
||||
* \retval -1 on Failure
|
||||
*/
|
||||
int DetectITypeSetup(DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *itypestr) {
|
||||
|
||||
DetectITypeData *itd = NULL;
|
||||
SigMatch *sm = NULL;
|
||||
|
||||
itd = DetectITypeParse(itypestr);
|
||||
if (itd == NULL) goto error;
|
||||
|
||||
sm = SigMatchAlloc();
|
||||
if (sm == NULL) goto error;
|
||||
|
||||
sm->type = DETECT_ITYPE;
|
||||
sm->ctx = (void *)itd;
|
||||
|
||||
SigMatchAppend(s,m,sm);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
if (itd != NULL) DetectITypeFree(itd);
|
||||
if (sm != NULL) free(sm);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief this function will free memory associated with DetectITypeData
|
||||
*
|
||||
* \param ptr pointer to DetectITypeData
|
||||
*/
|
||||
void DetectITypeFree(void *ptr) {
|
||||
DetectITypeData *itd = (DetectITypeData *)ptr;
|
||||
free(itd);
|
||||
}
|
||||
|
||||
#ifdef UNITTESTS
|
||||
|
||||
#include "detect-parse.h"
|
||||
#include "detect-engine.h"
|
||||
#include "detect-engine-mpm.h"
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest01 is a test for setting a valid itype value
|
||||
*/
|
||||
int DetectITypeParseTest01(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse("8");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->mode == DETECT_ITYPE_EQ)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest02 is a test for setting a valid itype value
|
||||
* with ">" operator
|
||||
*/
|
||||
int DetectITypeParseTest02(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse(">8");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->mode == DETECT_ITYPE_GT)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest03 is a test for setting a valid itype value
|
||||
* with "<" operator
|
||||
*/
|
||||
int DetectITypeParseTest03(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse("<8");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->mode == DETECT_ITYPE_LT)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest04 is a test for setting a valid itype value
|
||||
* with "<>" operator
|
||||
*/
|
||||
int DetectITypeParseTest04(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse("8<>20");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->type2 == 20 && itd->mode == DETECT_ITYPE_RN)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest05 is a test for setting a valid itype value
|
||||
* with spaces all around
|
||||
*/
|
||||
int DetectITypeParseTest05(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse(" 8 ");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->mode == DETECT_ITYPE_EQ)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest06 is a test for setting a valid itype value
|
||||
* with ">" operator and spaces all around
|
||||
*/
|
||||
int DetectITypeParseTest06(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse(" > 8 ");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->mode == DETECT_ITYPE_GT)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest07 is a test for setting a valid itype value
|
||||
* with "<>" operator and spaces all around
|
||||
*/
|
||||
int DetectITypeParseTest07(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
int result = 0;
|
||||
itd = DetectITypeParse(" 8 <> 20 ");
|
||||
if (itd != NULL) {
|
||||
if (itd->type1 == 8 && itd->type2 == 20 && itd->mode == DETECT_ITYPE_RN)
|
||||
result = 1;
|
||||
DetectITypeFree(itd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeParseTest08 is a test for setting an invalid itype value
|
||||
*/
|
||||
int DetectITypeParseTest08(void) {
|
||||
DetectITypeData *itd = NULL;
|
||||
itd = DetectITypeParse("> 8 <> 20");
|
||||
if (itd == NULL)
|
||||
return 1;
|
||||
DetectITypeFree(itd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \test DetectITypeMatchTest01 is a test for checking the working of itype
|
||||
* keyword by creating 5 rules and matching a crafted packet against
|
||||
* them. 4 out of 5 rules shall trigger.
|
||||
*/
|
||||
int DetectITypeMatchTest01(void) {
|
||||
|
||||
Packet p;
|
||||
Signature *s = NULL;
|
||||
ThreadVars th_v;
|
||||
DetectEngineThreadCtx *det_ctx;
|
||||
int result = 0;
|
||||
IPV4Hdr ip4h;
|
||||
ICMPV4Hdr icmpv4h;
|
||||
|
||||
memset(&th_v, 0, sizeof(th_v));
|
||||
memset(&p, 0, sizeof(p));
|
||||
memset(&ip4h, 0, sizeof(ip4h));
|
||||
memset(&icmpv4h, 0, sizeof(icmpv4h));
|
||||
|
||||
p.src.family = AF_INET;
|
||||
p.dst.family = AF_INET;
|
||||
p.proto = IPPROTO_ICMP;
|
||||
p.ip4h = &ip4h;
|
||||
icmpv4h.type = 10;
|
||||
p.icmpv4h = &icmpv4h;
|
||||
|
||||
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||||
if (de_ctx == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
de_ctx->flags |= DE_QUIET;
|
||||
|
||||
s = de_ctx->sig_list = SigInit(de_ctx,"alert icmp any any -> any any (itype:10; sid:1;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (itype:<15; sid:2;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (itype:>20; sid:3;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (itype:8<>20; sid:4;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (itype:20<>8; sid:5;)");
|
||||
if (s == NULL) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
SigGroupBuild(de_ctx);
|
||||
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||||
|
||||
SigMatchSignatures(&th_v, de_ctx, det_ctx, &p);
|
||||
if (PacketAlertCheck(&p, 1) == 0) {
|
||||
SCLogDebug("sid 1 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 2) == 0) {
|
||||
SCLogDebug("sid 2 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 3)) {
|
||||
SCLogDebug("sid 3 alerted, but should not have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 4) == 0) {
|
||||
SCLogDebug("sid 4 did not alert, but should have");
|
||||
goto cleanup;
|
||||
} else if (PacketAlertCheck(&p, 5) == 0) {
|
||||
SCLogDebug("sid 5 did not alert, but should have");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
result = 1;
|
||||
|
||||
cleanup:
|
||||
SigGroupCleanup(de_ctx);
|
||||
SigCleanSignatures(de_ctx);
|
||||
|
||||
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
|
||||
DetectEngineCtxFree(de_ctx);
|
||||
|
||||
end:
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endif /* UNITTESTS */
|
||||
|
||||
/**
|
||||
* \brief this function registers unit tests for DetectIType
|
||||
*/
|
||||
void DetectITypeRegisterTests(void) {
|
||||
#ifdef UNITTESTS
|
||||
UtRegisterTest("DetectITypeParseTest01", DetectITypeParseTest01, 1);
|
||||
UtRegisterTest("DetectITypeParseTest02", DetectITypeParseTest02, 1);
|
||||
UtRegisterTest("DetectITypeParseTest03", DetectITypeParseTest03, 1);
|
||||
UtRegisterTest("DetectITypeParseTest04", DetectITypeParseTest04, 1);
|
||||
UtRegisterTest("DetectITypeParseTest05", DetectITypeParseTest05, 1);
|
||||
UtRegisterTest("DetectITypeParseTest06", DetectITypeParseTest06, 1);
|
||||
UtRegisterTest("DetectITypeParseTest07", DetectITypeParseTest07, 1);
|
||||
UtRegisterTest("DetectITypeParseTest08", DetectITypeParseTest08, 1);
|
||||
UtRegisterTest("DetectITypeMatchTest01", DetectITypeMatchTest01, 1);
|
||||
#endif /* UNITTESTS */
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) 2009 Open Information Security Foundation
|
||||
*/
|
||||
|
||||
#ifndef __DETECT_ITYPE_H__
|
||||
#define __DETECT_ITYPE_H__
|
||||
|
||||
#define DETECT_ITYPE_EQ 0 /**< "equal" operator */
|
||||
#define DETECT_ITYPE_LT 1 /**< "less than" operator */
|
||||
#define DETECT_ITYPE_GT 2 /**< "greater than" operator */
|
||||
#define DETECT_ITYPE_RN 3 /**< "range" operator */
|
||||
|
||||
typedef struct DetectITypeData_ {
|
||||
uint8_t type1;
|
||||
uint8_t type2;
|
||||
|
||||
uint8_t mode;
|
||||
} DetectITypeData;
|
||||
|
||||
/* prototypes */
|
||||
void DetectITypeRegister(void);
|
||||
|
||||
#endif /* __DETECT_ITYPE_H__ */
|
||||
Loading…
Reference in New Issue