42#define DETECT_CLASSCONFIG_REGEX "^\\s*config\\s*classification\\s*:\\s*([a-zA-Z][a-zA-Z0-9-_]*)\\s*,\\s*(.+)\\s*,\\s*(\\d+)\\s*$"
45#if defined OS_WIN32 || defined __CYGWIN__
46#define SC_CLASS_CONF_DEF_CONF_FILEPATH CONFIG_DIR "\\\\classification.config"
48#define SC_CLASS_CONF_DEF_CONF_FILEPATH CONFIG_DIR "/classification.config"
53 void *data2, uint16_t datalen2);
58 const char *classtype,
const char *classtype_desc,
int priority);
70 PCRE2_UCHAR errbuffer[256];
71 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
128 const char *filename = SCClassConfGetConfFilename(
de_ctx);
129 if ( (fd = fopen(filename,
"r")) == NULL) {
134 SCLogWarning(
"could not open: \"%s\": %s", filename, strerror(errno));
154 const char *log_filename = NULL;
157 char config_value[256];
158 snprintf(config_value,
sizeof(config_value),
163 if (
SCConfGet(config_value, &log_filename) != 1) {
164 if (
SCConfGet(
"classification-file", &log_filename) != 1) {
169 if (
SCConfGet(
"classification-file", &log_filename) != 1) {
180static void SCClassConfDeInitLocalResources(FILE *fd)
203static char *SCClassConfStringToLowercase(
const char *
str)
205 char *new_str = NULL;
206 char *temp_str = NULL;
214 while (*temp_str !=
'\0') {
215 *temp_str =
u8_tolower((
unsigned char)*temp_str);
237 char ct_priority_str[16];
238 uint32_t ct_priority = 0;
239 uint16_t ct_id = index;
250 "classification.config file %s: \"%s\"",
251 SCClassConfGetConfFilename(
de_ctx), rawstr);
255 size_t copylen =
sizeof(ct_name);
257 ret = pcre2_substring_copy_bynumber(
260 SCLogInfo(
"pcre2_substring_copy_bynumber() failed");
265 copylen =
sizeof(ct_desc);
266 ret = pcre2_substring_copy_bynumber(
269 SCLogInfo(
"pcre2_substring_copy_bynumber() failed");
274 copylen =
sizeof(ct_priority_str);
275 ret = pcre2_substring_copy_bynumber(
278 SCLogInfo(
"pcre2_substring_copy_bynumber() failed");
286 ct_new = SCClassConfAllocClasstype(ct_id, ct_name, ct_desc, ct_priority);
293 if (ct_lookup == NULL) {
297 SCLogDebug(
"Duplicate classtype found inside classification.config");
321static int SCClassConfIsLineBlankOrComment(
char *line)
323 while (*line !=
'\0') {
329 if (!isspace((
unsigned char)*line))
351 while (fgets(line,
sizeof(line), fd) != NULL) {
352 if (SCClassConfIsLineBlankOrComment(line))
364 SCLogInfo(
"tenant id %d: Added \"%d\" classification types from the classification file",
367 SCLogInfo(
"Added \"%d\" classification types from the classification file",
387 const char *classtype,
388 const char *classtype_desc,
393 if (classtype == NULL)
399 if ((ct->
classtype = SCClassConfStringToLowercase(classtype)) == NULL) {
400 SCClassConfDeAllocClasstype(ct);
404 if (classtype_desc != NULL &&
408 SCClassConfDeAllocClasstype(ct);
455 for (i = 0; i <
len; i++)
477 void *data2, uint16_t datalen2)
484 if (ct1 == NULL || ct2 == NULL)
494 SCLogDebug(
"Match found inside Classification-Config hash function");
509 SCClassConfDeAllocClasstype(ch);
526 fd = SCClassConfInitContextAndLocalResources(
de_ctx, fd);
533 SCLogError(
"please check the \"classification-file\" "
534 "option in your suricata.yaml file");
539 if (!SCClassConfParseFile(
de_ctx, fd)) {
540 SCLogWarning(
"Error loading classification configuration from %s",
541 SCClassConfGetConfFilename(
de_ctx));
545 SCClassConfDeInitLocalResources(fd);
564 char name[strlen(ct_name) + 1];
566 for (s = 0; s < strlen(ct_name); s++)
573 return lookup_ct_info;
590 "config classification: nothing-wrong,Nothing Wrong With Us,3\n"
591 "config classification: unknown,Unknown are we,3\n"
592 "config classification: bad-unknown,We think it's bad, 2\n";
594 FILE *fd =
SCFmemopen((
void *)buffer, strlen(buffer),
"r");
596 SCLogDebug(
"Error with SCFmemopen() called by Classification Config test code");
610 "config classification: not-suspicious,Not Suspicious Traffic,3\n"
611 "onfig classification: unknown,Unknown Traffic,3\n"
612 "config classification: _badunknown,Potentially Bad Traffic, 2\n"
613 "config classification: bamboola1,Unknown Traffic,3\n"
614 "config classification: misc-activity,Misc activity,-1\n"
615 "config classification: policy-violation,Potential Corporate "
616 "config classification: bamboola,Unknown Traffic,3\n";
618 FILE *fd =
SCFmemopen((
void *)buffer, strlen(buffer),
"r");
620 SCLogDebug(
"Error with SCFmemopen() called by Classification Config test code");
634 "conig classification: not-suspicious,Not Suspicious Traffic,3\n"
635 "onfig classification: unknown,Unknown Traffic,3\n"
636 "config classification: _badunknown,Potentially Bad Traffic, 2\n"
637 "config classification: misc-activity,Misc activity,-1\n";
639 FILE *fd =
SCFmemopen((
void *)buffer, strlen(buffer),
"r");
641 SCLogDebug(
"Error with SCFmemopen() called by Classification Config test code");
650static int SCClassConfTest01(
void)
676static int SCClassConfTest02(
void)
701static int SCClassConfTest03(
void)
719static int SCClassConfTest04(
void)
752static int SCClassConfTest05(
void)
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
main detection engine ctx
HashTable * class_conf_ht
pcre2_code * class_conf_regex
pcre2_match_data * class_conf_regex_match
Container for a Classtype from the Classification.config file.
int RunmodeIsUnittests(void)
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
FILE * SCClassConfGenerateValidDummyClassConfigFD01(void)
Creates a dummy classification file, with all valid Classtypes, for testing purposes.
uint32_t SCClassConfClasstypeHashFunc(HashTable *ht, void *data, uint16_t datalen)
Hashing function to be used to hash the Classtype name. Would be supplied as an argument to the HashT...
void SCClassConfRegisterTests(void)
This function registers unit tests for Classification Config API.
void SCClassSCConfInit(DetectEngineCtx *de_ctx)
FILE * SCClassConfGenerateInvalidDummyClassConfigFD02(void)
Creates a dummy classification file, with some valid Classtypes and a couple of invalid Classtypes,...
void SCClassConfClasstypeHashFree(void *ch)
Used to free the Classification Config Hash Data that was stored in DetectEngineCtx->class_conf_ht Ha...
#define DETECT_CLASSCONFIG_REGEX
void SCClassConfDeInitContext(DetectEngineCtx *de_ctx)
Releases resources used by the Classification Config API.
int SCClassConfAddClasstype(DetectEngineCtx *de_ctx, char *rawstr, uint16_t index)
Parses a line from the classification file and adds it to Classtype hash table in DetectEngineCtx,...
void SCClassConfDeinit(DetectEngineCtx *de_ctx)
SCClassConfClasstype * SCClassConfGetClasstype(const char *ct_name, DetectEngineCtx *de_ctx)
Gets the classtype from the corresponding hash table stored in the Detection Engine Context's class c...
bool SCClassConfLoadClassificationConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Classtype info from the classification.config file.
#define SC_CLASS_CONF_DEF_CONF_FILEPATH
char SCClassConfClasstypeHashCompareFunc(void *data1, uint16_t datalen1, void *data2, uint16_t datalen2)
Used to compare two Classtypes that have been stored in the HashTable. This function is supplied as a...
FILE * SCClassConfGenerateInvalidDummyClassConfigFD03(void)
Creates a dummy classification file, with all invalid Classtypes, for testing purposes.
#define CLASSTYPE_DESC_MAX_LEN
#define CLASSTYPE_NAME_MAX_LEN
#define SCLogWarning(...)
Macro used to log WARNING messages.
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
#define SCLogError(...)
Macro used to log ERROR messages.
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
HashTable * HashTableInit(uint32_t size, uint32_t(*Hash)(struct HashTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
void HashTableFree(HashTable *ht)
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)