コード例 #1
0
ファイル: signature.c プロジェクト: DeepnessLab/moly
void SoRuleOtnLookupAdd(SFGHASH *so_rule_otn_map, OptTreeNode *otn)
{
    if ((so_rule_otn_map == NULL) || (otn == NULL))
        return;

    if (otn->sigInfo.otnKey.gid == 0)
    {
         otn->sigInfo.otnKey.gid = otn->sigInfo.generator;
         otn->sigInfo.otnKey.sid = otn->sigInfo.id;
    }

    if (sfghash_add(so_rule_otn_map, &(otn->sigInfo.otnKey), otn) == SFGHASH_INTABLE)
    {
         OptTreeNode *otn_original = so_rule_otn_map->cnode->data;

         if (!otn_original)
         {
             /* */
             FatalError("Missing Duplicate\n");
         }
         while (otn_original->nextSoid)
         {
             otn_original = otn_original->nextSoid;
         }

         otn_original->nextSoid = otn;
    }
}
コード例 #2
0
ファイル: hostPortAppCache.c プロジェクト: GumpChan/blackcat
int hostPortAppCacheAdd(const struct in6_addr *ip, uint16_t port, uint16_t proto, unsigned type, tAppId appId)
{
    tHostPortKey hk;
    memcpy(&hk.ip, ip, sizeof(hk.ip));
    hk.port = port;
    hk.proto = proto;
    if (sfghash_add(hostPortCache, &hk, (void*)appId))
    {
        return 0;
    }

    return 1;
}
コード例 #3
0
/* Initialize a byteExtract structure. */
int ByteExtractInitialize(Rule *rule, ByteExtract *extractData)
{
    int ret = 0;
    void *memoryLocation;
    if (rule->ruleData == NULL)
    {
        /* Initialize the hash table */

        /* XXX: 3 rows ought to suffice for now... */
        /* 3 rows,
         * 0 bytes key size (ie, its a string),
         * user provided keys,
         * free func -- data is pointer to int
         */
        rule->ruleData = (void *)sfghash_new(3, 0, 1, free);
    }

    memoryLocation = sfghash_find((SFGHASH*)rule->ruleData, extractData->refId);

    if (memoryLocation)
    {
        /* Cannot re-use refId */
        DynamicEngineFatalMessage("Cannot re-use ByteExtract location '%s' for rule [%d:%d]\n",
                                  extractData->refId, rule->info.genID, rule->info.sigID);
        //return -1;
    }

    memoryLocation = calloc(sizeof(u_int32_t), 1);
    if (memoryLocation == NULL)
    {
        DynamicEngineFatalMessage("Failed to allocate memory\n");
    }

    ret = sfghash_add((SFGHASH*)rule->ruleData, extractData->refId, memoryLocation);
    if (ret != SFGHASH_OK)
    {
        free(memoryLocation);

        /* Some error, couldn't allocate hash entry */
        return -2;
    }

    extractData->memoryLocation = memoryLocation;

    return 0;
}
コード例 #4
0
ファイル: signature.c プロジェクト: DeepnessLab/moly
void OtnLookupAdd(SFGHASH *otn_map, OptTreeNode *otn)
{
    int status;
    OtnKey key;

    if (otn_map == NULL)
        return;

    key.gid = otn->sigInfo.generator;
    key.sid = otn->sigInfo.id;

    status = sfghash_add(otn_map, &key, otn);
    switch (status)
    {
        case SFGHASH_OK:
            /* otn was inserted successfully */
            break;

        case SFGHASH_INTABLE:
            /* Assume it's a rule without an sid */
            if (key.sid == 0)
            {
                ParseError("Duplicate rule with same gid (%u) and no sid.  To "
                           "avoid this, make sure all of your rules define an "
                           "sid.\n", key.gid);
            }
            else
            {
                ParseError("Duplicate rule with same gid (%u) and sid (%u)\n",
                           key.gid, key.sid);
            }

            break;

        case SFGHASH_NOMEM:
            FatalError("Failed to allocate memory for rule.\n");
            break;

        default:
            FatalError("%s(%d): OtnLookupAdd() - unexpected return value "
                       "from sfghash_add().\n", __FILE__, __LINE__);
            break;
    }
}
コード例 #5
0
ファイル: sfghash.c プロジェクト: bailehang/snort_2.2.0
int sfatom_add(char * str, void * data)
{
   if( atom_first )
   { 
      if( sfatom_init() )
      {
         return SFGHASH_ERR;
      }
    }

    if( !g_atom ) 
    {
        return SFGHASH_ERR;
    }

    sfghash_add( g_atom, strdup(str), data );

    return SFGHASH_OK;
}
コード例 #6
0
ファイル: sfrf.c プロジェクト: DeepnessLab/moly
/*  Add a permanent threshold object to the threshold table. Multiple
 * objects may be defined for each gid and sid pair. Internally
 * a unique threshold id is generated for each pair.
 *
 * Threshold objects track the number of events seen during the time
 * interval specified by seconds. Depending on the type of threshold
 * object and the count value, the thresholding object determines if
 * the current event should be logged or dropped.
 *
 * @param pContext Threshold object from SFRF_ContextNew()
 * @param cfgNode Permanent Thresholding Object
 *
 * @return @retval  0 successfully added the thresholding object, !0 otherwise
*/
int SFRF_ConfigAdd(SnortConfig *sc, RateFilterConfig *rf_config, tSFRFConfigNode *cfgNode)
{
    SFGHASH* genHash;
    int nrows;
    int hstatus;
    tSFRFSidNode* pSidNode;
    tSFRFConfigNode* pNewConfigNode;
    tSFRFGenHashKey key = {0,0};
    tSfPolicyId policy_id = getParserPolicy(sc);

    // Auto init - memcap must be set 1st, which is not really a problem
    if ( rf_hash == NULL )
    {
        SFRF_New(rf_config->memcap);

        if ( rf_hash == NULL )
            return -1;
    }

    if ((rf_config == NULL) || (cfgNode == NULL))
        return -1;

    if ( (cfgNode->sid == 0 ) || (cfgNode->gid == 0) )
        return -1;

    if ( cfgNode->gid >= SFRF_MAX_GENID )
        return -1;

    if ( cfgNode->count < 1 )
        return -1;

    if ( cfgNode->timeout == 0 )
    {
        if ( rf_config->noRevertCount >= SFRF_NO_REVERT_LIMIT )
            return -1;

        rf_config->noRevertCount++;
    }

    /* Check for an existing 'gid' entry, if none found then create one. */
    /* Get the hash table for this gid */
    genHash = rf_config->genHash[cfgNode->gid];

    if ( !genHash )
    {
        if ( cfgNode->gid == 1 )/* patmatch rules gid, many rules */
        {
            nrows= SFRF_GEN_ID_1_ROWS;
        }
        else  /* other gid's */
        {
            nrows= SFRF_GEN_ID_ROWS;
        }

        /* Create the hash table for this gid */
        genHash = sfghash_new( nrows, sizeof(tSFRFGenHashKey), 0, SFRF_SidNodeFree );
        if ( !genHash )
            return -2;

        rf_config->genHash[cfgNode->gid] = genHash;
    }

    key.sid = cfgNode->sid;
    key.policyId = policy_id;

    /* Check if sid is already in the table - if not allocate and add it */
    pSidNode = (tSFRFSidNode*)sfghash_find( genHash, (void*)&key );
    if ( !pSidNode )
    {
        /* Create the pSidNode hash node data */
        pSidNode = (tSFRFSidNode*)calloc(1,sizeof(tSFRFSidNode));
        if ( !pSidNode )
            return -3;

        pSidNode->gid = cfgNode->gid;
        pSidNode->sid = cfgNode->sid;
        pSidNode->configNodeList = sflist_new();

        if ( !pSidNode->configNodeList )
        {
            free(pSidNode);
            return -4;
        }

        /* Add the pSidNode to the hash table */
        hstatus = sfghash_add( genHash, (void*)&key, pSidNode );
        if ( hstatus )
        {
            sflist_free(pSidNode->configNodeList);
            free(pSidNode);
            return -5;
        }
    }

    /* Create a tSFRFConfigNode for this tSFRFSidNode (Object) */
    pNewConfigNode = (tSFRFConfigNode*)calloc(1,sizeof(tSFRFConfigNode));
    if ( !pNewConfigNode )
    {
        sflist_free(pSidNode->configNodeList);
        free(pSidNode);
        return -6;
    }

    *pNewConfigNode = *cfgNode;

    rf_config->count++;

    /* Copy the node parameters, with unique internally assigned tid */
    pNewConfigNode->tid = rf_config->count;
    if ( pNewConfigNode->tid == 0 )
    {
        // tid overflow. rare but possible
        free(pNewConfigNode);
        sflist_free(pSidNode->configNodeList);
        free(pSidNode);
        return -6;
    }


#ifdef SFRF_DEBUG
    printf("--%d-%d-%d: Threshold node added to tail of list\n",
            pNewConfigNode->tid,
            pNewConfigNode->gid,
            pNewConfigNode->sid);
    fflush(stdout);
#endif
    sflist_add_tail(pSidNode->configNodeList,pNewConfigNode);

    return 0;
}
コード例 #7
0
ファイル: sfthd.c プロジェクト: lynnkitch/openSourceTesting
/*!
Add a permanent threshold object to the threshold table. Multiple
objects may be defined for each gen_id and sig_id pair. Internally
a unique threshold id is generated for each pair.

Threshold objects track the number of events seen during the time
interval specified by seconds. Depending on the type of threshold
object and the count value, the thresholding object determines if
the 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

*/
static int sfthd_create_threshold_local(SnortConfig *sc, ThresholdObjects *thd_objs,
                                        THD_NODE* config)
{
    SFGHASH  * sfthd_hash;
    THD_ITEM * sfthd_item;
    THD_NODE * sfthd_node;
    tThdItemKey key;
    int nrows;
    int hstatus;
    tSfPolicyId policy_id = getParserPolicy(sc);

    if (thd_objs == NULL )
        return -1;

    if( config->gen_id >= THD_MAX_GENID )
        return -1;

#ifdef CRIPPLE
    return 0;
#endif

    /* Check for an existing 'gen_id' entry, if none found create one. */
    if (thd_objs->sfthd_array[config->gen_id] == NULL)
    {
        if( config->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(tThdItemKey), 0, sfthd_item_free );
        if( !sfthd_hash )
        {
            return -2;
        }

        thd_objs->sfthd_array[config->gen_id] = sfthd_hash;
    }
    else
    {
        /* Get the hash table for this gen_id */
        sfthd_hash = thd_objs->sfthd_array[config->gen_id];
    }

    if (sfthd_hash == NULL)
         return -2;

    key.sig_id = config->sig_id;
    key.policyId = policy_id;

    /* Check if sig_id is already in the table - if not allocate and add it */
    sfthd_item = (THD_ITEM*)sfghash_find( sfthd_hash, (void*)&key );
    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 = config->gen_id;
        sfthd_item->sig_id = config->sig_id;
        sfthd_item->policyId = policy_id;
        sfthd_item->sfthd_node_list = sflist_new();

        if(!sfthd_item->sfthd_node_list)
        {
            free(sfthd_item);
            return -4;
        }

        /* Add the sfthd_item to the hash table */
        hstatus = sfghash_add( sfthd_hash, (void*)&key, sfthd_item );
        if( hstatus )
        {
            sflist_free(sfthd_item->sfthd_node_list);
            free(sfthd_item);
            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)
        {
            /* can you say paranoid- if there is a count, there should be a tail */
            return -10;
        }
        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 && config->type != THD_TYPE_SUPPRESS )
            {
#ifdef THD_DEBUG
                printf("THD_DEBUG: Could not add a 2nd Threshold object, "
                       "you can only have 1 per sid: gid=%u, sid=%u\n",
                       config->gen_id, config->sig_id);
#endif
                /* cannot add more than one threshold per sid in
                   version 3.0, wait for 3.2 and CIDR blocks */
                return THD_TOO_MANY_THDOBJ;
            }
        }
    }

    /* 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( config->priority >= THD_PRIORITY_SUPPRESS )
    {
        config->priority  = THD_PRIORITY_SUPPRESS - 1;
    }

    /* Copy the node parameters */
    sfthd_node->thd_id    = config->thd_id;
    sfthd_node->gen_id    = config->gen_id;
    sfthd_node->sig_id    = config->sig_id;
    sfthd_node->tracking  = config->tracking; /* by_src, by_dst */
    sfthd_node->type      = config->type;
    sfthd_node->priority  = config->priority;
    sfthd_node->count     = config->count;
    sfthd_node->seconds   = config->seconds;
    sfthd_node->ip_address= config->ip_address;

    if( config->type == THD_TYPE_SUPPRESS )
    {
        sfthd_node->priority = THD_PRIORITY_SUPPRESS;
    }

    /*
      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
    {
        SF_LNODE* lnode;

        /* 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) )
        {
            THD_NODE* 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;
}
コード例 #8
0
ファイル: sfghash.c プロジェクト: sunzhuo1987/aegis--shield
/*
*       Hash test program  
*/
int main ( int argc, char ** argv )
{
   int         i;
   SFGHASH      * t;
   SFGHASH_NODE * n, *m;
   char str[256],*p;
   int  num=100;

   if( argc > 1 )
       num = atoi(argv[1]);

   sfatom_init();

   /* Create a Hash Table */
   t = sfghash_new( 1000, 0 , GH_COPYKEYS , myfree  );

   /* Add Nodes to the Hash Table */
   for(i=0;i<num;i++) 
   {
       snprintf(str, sizeof(str), "KeyWord%d",i+1);
       str[sizeof(str) - 1] = '\0';
       sfghash_add( t, str,  strupr(strdup(str)) );

       sfatom_add( str,  strupr(strdup(str)) );
   }  

   /* Find and Display Nodes in the Hash Table */
   printf("\n** FIND KEY TEST\n");

   for(i=0;i<num;i++) 
   {
      snprintf(str, sizeof(str), "KeyWord%d",i+1);
      str[sizeof(str) - 1] = '\0';

      p = (char*) sfghash_find( t, str );

      printf("Hash-key=%*s, data=%*s\n", strlen(str),str, strlen(str), p );

      p = (char*) sfatom_find( str );

      printf("Atom-key=%*s, data=%*s\n", strlen(str),str, strlen(str), p );
   }  

   /* Display All Nodes in the Hash Table */
   printf("\n** FINDFIRST / FINDNEXT TEST\n");

   for( n = sfghash_findfirst(t); n; n = sfghash_findnext(t) )
   {
      printf("hash-findfirst/next: key=%s, data=%s\n", n->key, n->data );

      // hashing code frees user data using 'myfree' above ....
      if( sfghash_remove(t,n->key) ) 
            printf("Could not remove the key node\n");
      else  
            printf("key node removed\n");
   }

   for( n = sfatom_findfirst(); n; n = sfatom_findnext() )
   {
      printf("atom-findfirst/next: key=%s, data=%s\n", n->key, n->data );

      free( n->data );  //since atom data is not freed automatically
   }

   /* Free the table and it's user data */
   printf("****sfghash_delete\n");
   sfghash_delete( t );

   printf("****sfatom_reset\n");
   sfatom_reset();

   printf("\nnormal pgm finish\n\n");

   return 0;
}
コード例 #9
0
ファイル: sfghash.c プロジェクト: bailehang/snort_2.2.0
int sfdict_add( SFGHASH * t, char * key, void * data )
{
   return sfghash_add( t, key, data );
}