📄 sfthd.c
字号:
/*! * * \file sfthd.c * * An Abstracted Event Thresholding System * * Copyright (C) 2003 Sourcefire,Inc. * Marc Norton * */#include <stdlib.h>#include <stdio.h>#include <string.h>#include "sflsq.h"#include "sfghash.h"#include "sfxhash.h"#include "sfthd.h" static int s_id = 1; /* thd_id generator for both local and global thresholds *//* * Debug Printing *//* #define THD_DEBUG *//* * This disables adding and testing of Threshold objects *//*#define CRIPPLE*//*! Create a threshold table, initialize the threshold system, and optionally limit it's memory usage. @param nbytes maximum memory to use for thresholding objects, in bytes. @return THD_STRUCT* @retval 0 error @retval !0 valid THD_STRUCT*/THD_STRUCT * sfthd_new( unsigned nbytes ){ THD_STRUCT * thd; int nrows; /* Create the THD struct */ thd = (THD_STRUCT*) calloc(1,sizeof(THD_STRUCT)); if( !thd ) { return 0; } /* Calc max ip nodes for this memory */ nrows = nbytes /( sizeof(THD_IP_NODE)+sizeof(THD_IP_NODE_KEY) );#ifndef CRIPPLE /* Create global hash table for all of the IP Nodes */ thd->ip_nodes = sfxhash_new( nrows, /* try one node per row - for speed */ sizeof(THD_IP_NODE_KEY), /* keys size */ sizeof(THD_IP_NODE), /* data size */ nbytes, /* memcap **/ 1, /* ANR flag - true ?- Automatic Node Recovery=ANR */ 0, /* ANR callback - none */ 0, /* user freemem callback - none */ 1 ) ; /* Recycle nodes ?*/ if( !thd->ip_nodes ) {#ifdef THD_DEBUG printf("Could not allocate the sfxhash table\n");#endif free(thd); return 0; } /* Calc max ip nodes for global thresholding memory */ nrows = nbytes /( sizeof(THD_IP_GNODE)+sizeof(THD_IP_GNODE_KEY) ); /* Create global hash table for all of the Global-Thresholding IP Nodes */ thd->ip_gnodes = sfxhash_new( nrows, /* try one node per row - for speed */ sizeof(THD_IP_GNODE_KEY), /* keys size */ sizeof(THD_IP_GNODE), /* data size */ nbytes, /* memcap **/ 1, /* ANR flag - true ?- Automatic Node Recovery=ANR */ 0, /* ANR callback - none */ 0, /* user freemem callback - none */ 1 ) ; /* Recycle nodes ?*/ if( !thd->ip_gnodes ) {#ifdef THD_DEBUG printf("Could not allocate the sfxhash table\n");#endif free(thd); return 0; }#endif return thd;}/*!Add a permanent threshold object to the threshold table. Multipleobjects may be defined for each gen_id and sig_id pair. Internallya unique threshold id is generated for each pair.Threshold objects track the number of events seen during the timeinterval specified by seconds. Depending on the type of thresholdobject and the count value, the thresholding object determines ifthe current event should be logged or dropped.@param thd Threshold object from sfthd_new()@param gen_id Generator id@param sig_id Signauture id@param tracking Selects tracking by src ip or by dst ip@param type Thresholding type: Limit, Threshold, or Limt+Threshold, Suppress @param priority Assigns a relative priority to this object, higher numbers imply higher priority@param count Number of events@param seconds Time duration over which this threshold object acts.@param ip IP address, for supression@param ip-mask IP mask, applied with ip_mask, for supression@return integer@retval 0 successfully added the thresholding object@retval !0 failed */staticint sfthd_create_threshold_local( THD_STRUCT * thd, unsigned gen_id, unsigned sig_id, int tracking, int type, int priority, int count, int seconds, unsigned ip_address, unsigned ip_mask, unsigned not_flag){ SFGHASH * sfthd_hash; int nrows; int hstatus; THD_ITEM * sfthd_item; THD_NODE * sfthd_node; THD_NODE * sfthd_n; SF_LNODE * lnode; if( !thd ) return -1; if( gen_id >= THD_MAX_GENID ) return -1;#ifdef CRIPPLE return 0;#endif /* Check for an existing 'gen_id' entry, if none found than create one. */ if( !thd->sfthd_array[ gen_id ] ) { if( gen_id == 1 )/* patmatch rules gen_id, many rules */ { nrows= THD_GEN_ID_1_ROWS; } else /* other gen_id's */ { nrows= THD_GEN_ID_ROWS; } /* Create the hash table for this gen_id */ sfthd_hash = sfghash_new( nrows, sizeof(sig_id), 0, 0 ); if( !sfthd_hash ) { return -2; } thd->sfthd_array[gen_id] = sfthd_hash; } else { /* Get the hash table for this gen_id */ sfthd_hash = thd->sfthd_array[gen_id]; } if( !sfthd_hash ) { return -2; } /* Check if sig_id is already in the table - if not allocate it and add it */ sfthd_item = (THD_ITEM*)sfghash_find( sfthd_hash, (void*)&sig_id ); if( !sfthd_item ) { /* Create the sfthd_item hash node data */ sfthd_item = (THD_ITEM*)calloc(1,sizeof(THD_ITEM)); if( !sfthd_item ) { return -3; } sfthd_item->gen_id = gen_id; sfthd_item->sig_id = sig_id; sfthd_item->sfthd_node_list = sflist_new(); if(!sfthd_item->sfthd_node_list) return -4; /* Add the sfthd_item to the hash table */ hstatus = sfghash_add( sfthd_hash, (void*)&sig_id, sfthd_item ); if( hstatus ) { return -5; } } /* * Test that we only have one Limit/Threshold/Both Object at the tail, * we can have multiple suppression nodes at the head */ if( sfthd_item->sfthd_node_list->count > 0 ) { THD_NODE * p; if( !sfthd_item->sfthd_node_list->tail) { return -10; /* can you say paranoid- if there is a count, there should be a tail */ } p = (THD_NODE*)sfthd_item->sfthd_node_list->tail->ndata; if(p) /* just to be safe- if thers a tail, there is is node data */ { if( p->type != THD_TYPE_SUPPRESS && type != THD_TYPE_SUPPRESS ) {#ifdef THD_DEBUG printf("THD_DEBUG: Could not add a 2nd Threshold object, you can onlky have 1 per sid: gid=%u, sid=%u\n",gen_id,sig_id);#endif return THD_TOO_MANY_THDOBJ;/* cannot add more than one threshold per sid in version 3.0, wait for 3.2 and CIDR blocks */ } } } /* Create a THD_NODE for this THD_ITEM (Object) */ sfthd_node = (THD_NODE*)calloc(1,sizeof(THD_NODE)); if( !sfthd_node ) { return -6; } /* Limit priorities to force supression nodes to highest priority */ if( priority >= THD_PRIORITY_SUPPRESS ) { priority = THD_PRIORITY_SUPPRESS - 1; } /* Copy the node parameters */ sfthd_node->thd_id = s_id++; /* produce a unique thd_id for this node */ sfthd_node->gen_id = gen_id; sfthd_node->sig_id = sig_id; sfthd_node->tracking = tracking; /* by_src, by_dst */ sfthd_node->type = type; sfthd_node->priority = priority; sfthd_node->count = count; sfthd_node->seconds = seconds; sfthd_node->ip_address= ip_address; sfthd_node->ip_mask = ip_mask; sfthd_node->not_flag = not_flag; if( type == THD_TYPE_SUPPRESS ) { sfthd_node->priority = THD_PRIORITY_SUPPRESS; if( sfthd_node->ip_mask == 0 && sfthd_node->ip_address != 0 ) { sfthd_node->ip_mask = 0xffffffff; } } thd->count++; /* If sfthd_node list is empty - add as head node */ if( !sfthd_item->sfthd_node_list->count ) {#ifdef THD_DEBUG printf("Threshold node added to head of list\n");fflush(stdout);#endif sflist_add_head(sfthd_item->sfthd_node_list,sfthd_node); } /* else add the sfthd_node using priority to determine where in the list it belongs 3.0 we can have only 1 threshold object but several suppression objects plus a single threshold object is ok. Blocking multiple threshold objects is done above. Suppressions have the highest priority and are at the front of the list, the tail node is either a supprssion node or the only pure thresholding node. */ else { /* Walk the list and insert based on priorities if suppress */ /* */ for( lnode = sflist_first_node(sfthd_item->sfthd_node_list); lnode; lnode = sflist_next_node(sfthd_item->sfthd_node_list) ) { sfthd_n = (THD_NODE*)lnode->ndata; /* check if the new node is higher priority */ if( sfthd_node->priority > sfthd_n->priority ) { /* insert before current node */#ifdef THD_DEBUG printf("Threshold node added after based on priority\n");fflush(stdout);#endif sflist_add_before(sfthd_item->sfthd_node_list,lnode,sfthd_node); return 0; } /* last node, just insert it here */ if( !lnode->next ) { /* if last node, insert at end of list */#ifdef THD_DEBUG printf("Threshold node added to tail\n");fflush(stdout);#endif sflist_add_tail(sfthd_item->sfthd_node_list,sfthd_node); return 0; } } } return 0;}/* * * */staticint sfthd_create_threshold_global( THD_STRUCT * thd, unsigned gen_id, unsigned sig_id, int tracking, int type, int priority, int count, int seconds, unsigned ip_address, unsigned ip_mask ){ THD_NODE * sfthd_node; sfthd_node = (THD_NODE*)calloc(1,sizeof(THD_NODE)); if( !sfthd_node ) { return -2; } /* Copy the node parameters */ sfthd_node->thd_id = s_id++; /* produce a unique thd_id for this node */ sfthd_node->gen_id = gen_id; sfthd_node->sig_id = sig_id; /* -1 for global thresholds */ sfthd_node->tracking = tracking; /* by_src, by_dst */ sfthd_node->type = type; sfthd_node->priority = priority; sfthd_node->count = count; sfthd_node->seconds = seconds;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -