1 /* @(#) $Id: eventinfo.c,v 1.41 2009/06/24 17:06:22 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
17 * Available at http://www.ossec.net
23 #include "analysisd.h"
24 #include "eventinfo.h"
25 #include "os_regex/os_regex.h"
28 /* Search last times a signature fired
29 * Will look for only that specific signature.
31 Eventinfo *Search_LastSids(Eventinfo *my_lf, RuleInfo *currently_rule)
38 /* Setting frequency to 0 */
39 currently_rule->__frequency = 0;
42 /* checking sid search is valid */
43 if(!currently_rule->sid_search)
45 merror("%s: No sid search!! XXX", ARGV0);
48 /* Getting last node */
49 lf_node = OSList_GetLastNode(currently_rule->sid_search);
54 first_lf = (Eventinfo *)lf_node->data;
59 lf = (Eventinfo *)lf_node->data;
61 /* If time is outside the timeframe, return */
62 if((c_time - lf->time) > currently_rule->timeframe)
67 /* We avoid multiple triggers for the same rule
68 * or rules with a lower level.
70 else if(lf->matched >= currently_rule->level)
77 /* Checking for same id */
78 if(currently_rule->context_opts & SAME_ID)
80 if((!lf->id) || (!my_lf->id))
83 if(strcmp(lf->id,my_lf->id) != 0)
87 /* Checking for repetitions from same src_ip */
88 if(currently_rule->context_opts & SAME_SRCIP)
90 if((!lf->srcip)||(!my_lf->srcip))
93 if(strcmp(lf->srcip,my_lf->srcip) != 0)
98 /* Grouping of additional data */
99 if(currently_rule->alert_opts & SAME_EXTRAINFO)
101 /* Checking for same source port */
102 if(currently_rule->context_opts & SAME_SRCPORT)
104 if((!lf->srcport)||(!my_lf->srcport))
107 if(strcmp(lf->srcport, my_lf->srcport) != 0)
111 /* Checking for same dst port */
112 if(currently_rule->context_opts & SAME_DSTPORT)
114 if((!lf->dstport)||(!my_lf->dstport))
117 if(strcmp(lf->dstport, my_lf->dstport) != 0)
121 /* Checking for repetitions on user error */
122 if(currently_rule->context_opts & SAME_USER)
124 if((!lf->dstuser)||(!my_lf->dstuser))
127 if(strcmp(lf->dstuser,my_lf->dstuser) != 0)
131 /* Checking for same location */
132 if(currently_rule->context_opts & SAME_LOCATION)
134 if(strcmp(lf->hostname, my_lf->hostname) != 0)
139 /* Checking for different urls */
140 if(currently_rule->context_opts & DIFFERENT_URL)
142 if((!lf->url)||(!my_lf->url))
147 if(strcmp(lf->url, my_lf->url) == 0)
156 /* Checking if the number of matches worked */
157 if(currently_rule->__frequency < currently_rule->frequency)
159 if(currently_rule->__frequency <= 10)
161 currently_rule->last_events[currently_rule->__frequency]
163 currently_rule->last_events[currently_rule->__frequency+1]
167 currently_rule->__frequency++;
172 /* If reached here, we matched */
173 my_lf->matched = currently_rule->level;
174 lf->matched = currently_rule->level;
175 first_lf->matched = currently_rule->level;
180 }while((lf_node = lf_node->prev) != NULL);
187 /* Search last times a group fired
188 * Will look for only that specific group on that rule.
190 Eventinfo *Search_LastGroups(Eventinfo *my_lf, RuleInfo *currently_rule)
197 /* Setting frequency to 0 */
198 currently_rule->__frequency = 0;
201 /* checking sid search is valid */
202 if(!currently_rule->group_search)
204 merror("%s: No group search!! XXX", ARGV0);
207 /* Getting last node */
208 lf_node = OSList_GetLastNode(currently_rule->group_search);
213 first_lf = (Eventinfo *)lf_node->data;
218 lf = (Eventinfo *)lf_node->data;
220 /* If time is outside the timeframe, return */
221 if((c_time - lf->time) > currently_rule->timeframe)
226 /* We avoid multiple triggers for the same rule
227 * or rules with a lower level.
229 else if(lf->matched >= currently_rule->level)
236 /* Checking for same id */
237 if(currently_rule->context_opts & SAME_ID)
239 if((!lf->id) || (!my_lf->id))
242 if(strcmp(lf->id,my_lf->id) != 0)
246 /* Checking for repetitions from same src_ip */
247 if(currently_rule->context_opts & SAME_SRCIP)
249 if((!lf->srcip)||(!my_lf->srcip))
252 if(strcmp(lf->srcip,my_lf->srcip) != 0)
257 /* Grouping of additional data */
258 if(currently_rule->alert_opts & SAME_EXTRAINFO)
260 /* Checking for same source port */
261 if(currently_rule->context_opts & SAME_SRCPORT)
263 if((!lf->srcport)||(!my_lf->srcport))
266 if(strcmp(lf->srcport, my_lf->srcport) != 0)
270 /* Checking for same dst port */
271 if(currently_rule->context_opts & SAME_DSTPORT)
273 if((!lf->dstport)||(!my_lf->dstport))
276 if(strcmp(lf->dstport, my_lf->dstport) != 0)
280 /* Checking for repetitions on user error */
281 if(currently_rule->context_opts & SAME_USER)
283 if((!lf->dstuser)||(!my_lf->dstuser))
286 if(strcmp(lf->dstuser,my_lf->dstuser) != 0)
290 /* Checking for same location */
291 if(currently_rule->context_opts & SAME_LOCATION)
293 if(strcmp(lf->hostname, my_lf->hostname) != 0)
298 /* Checking for different urls */
299 if(currently_rule->context_opts & DIFFERENT_URL)
301 if((!lf->url)||(!my_lf->url))
306 if(strcmp(lf->url, my_lf->url) == 0)
315 /* Checking if the number of matches worked */
316 if(currently_rule->__frequency < currently_rule->frequency)
318 if(currently_rule->__frequency <= 10)
320 currently_rule->last_events[currently_rule->__frequency]
322 currently_rule->last_events[currently_rule->__frequency+1]
326 currently_rule->__frequency++;
331 /* If reached here, we matched */
332 my_lf->matched = currently_rule->level;
333 lf->matched = currently_rule->level;
334 first_lf->matched = currently_rule->level;
339 }while((lf_node = lf_node->prev) != NULL);
345 /* Search LastEvents.
346 * Will look if any of the last events (inside the timeframe)
347 * match the specified rule.
349 Eventinfo *Search_LastEvents(Eventinfo *my_lf, RuleInfo *currently_rule)
351 EventNode *eventnode_pt;
356 merror("XXXX : remove me!");
360 eventnode_pt = OS_GetLastEvent();
367 /* Setting frequency to 0 */
368 currently_rule->__frequency = 0;
369 first_lf = (Eventinfo *)eventnode_pt->event;
372 /* Searching all previous events */
375 lf = eventnode_pt->event;
377 /* If time is outside the timeframe, return */
378 if((c_time - lf->time) > currently_rule->timeframe)
384 /* We avoid multiple triggers for the same rule
385 * or rules with a lower level.
387 else if(lf->matched >= currently_rule->level)
393 /* The category must be the same */
394 else if(lf->decoder_info->type != my_lf->decoder_info->type)
400 /* If regex does not match, go to next */
401 if(currently_rule->if_matched_regex)
403 if(!OSRegex_Execute(lf->log, currently_rule->if_matched_regex))
410 /* Checking for repetitions on user error */
411 if(currently_rule->context_opts & SAME_USER)
413 if((!lf->dstuser)||(!my_lf->dstuser))
416 if(strcmp(lf->dstuser,my_lf->dstuser) != 0)
420 /* Checking for same id */
421 if(currently_rule->context_opts & SAME_ID)
423 if((!lf->id) || (!my_lf->id))
426 if(strcmp(lf->id,my_lf->id) != 0)
430 /* Checking for repetitions from same src_ip */
431 if(currently_rule->context_opts & SAME_SRCIP)
433 if((!lf->srcip)||(!my_lf->srcip))
436 if(strcmp(lf->srcip,my_lf->srcip) != 0)
440 /* Checking for different urls */
441 if(currently_rule->context_opts & DIFFERENT_URL)
443 if((!lf->url)||(!my_lf->url))
448 if(strcmp(lf->url, my_lf->url) == 0)
455 /* Checking if the number of matches worked */
456 if(currently_rule->__frequency < currently_rule->frequency)
458 if(currently_rule->__frequency <= 10)
460 currently_rule->last_events[currently_rule->__frequency]
462 currently_rule->last_events[currently_rule->__frequency+1]
466 currently_rule->__frequency++;
471 /* If reached here, we matched */
472 my_lf->matched = currently_rule->level;
473 lf->matched = currently_rule->level;
474 first_lf->matched = currently_rule->level;
478 }while((eventnode_pt = eventnode_pt->next) != NULL);
485 /* Zero the loginfo structure */
486 void Zero_Eventinfo(Eventinfo *lf)
491 lf->program_name = NULL;
507 lf->systemname = NULL;
517 lf->generated_rule = NULL;
518 lf->sid_node_to_delete = NULL;
519 lf->decoder_info = NULL_Decoder;
524 /* Free the loginfo structure */
525 void Free_Eventinfo(Eventinfo *lf)
529 merror("%s: Trying to free NULL event. Inconsistent..",ARGV0);
566 free(lf->systemname);
569 /* Freeing node to delete */
570 if(lf->sid_node_to_delete)
572 OSList_DeleteThisNode(lf->generated_rule->sid_prev_matched,
573 lf->sid_node_to_delete);
575 else if(lf->generated_rule && lf->generated_rule->group_prev_matched)
579 while(i < lf->generated_rule->group_prev_matched_sz)
581 OSList_DeleteOldestNode(lf->generated_rule->group_prev_matched[i]);
586 /* We dont need to free: