1 /* @(#) $Id: fts.c,v 1.34 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
16 /* First time seen functions
21 #include "eventinfo.h"
23 int fts_minsize_for_str = 0;
25 OSList *fts_list = NULL;
26 OSStore *fts_store = NULL;
29 FILE *fp_ignore = NULL;
33 * Starts the FTS module.
38 char _line[OS_FLSIZE + 1];
40 _line[OS_FLSIZE] = '\0';
43 fts_list = OSList_Create();
46 merror(LIST_ERROR, ARGV0);
50 /* Creating store data */
51 fts_store = OSStore_Create();
54 merror(LIST_ERROR, ARGV0);
58 /* Getting default list size */
59 fts_list_size = getDefine_Int("analysisd",
63 /* Getting minimum string size */
64 fts_minsize_for_str = getDefine_Int("analysisd",
65 "fts_min_size_for_str",
68 if(!OSList_SetMaxSize(fts_list, fts_list_size))
70 merror(LIST_SIZE_ERROR, ARGV0);
75 /* creating fts list */
76 fp_list = fopen(FTS_QUEUE, "r+");
79 /* Create the file if we cant open it */
80 fp_list = fopen(FTS_QUEUE, "w+");
84 fp_list = fopen(FTS_QUEUE, "r+");
87 merror(FOPEN_ERROR, ARGV0, FTS_QUEUE);
93 /* Adding content from the files to memory */
94 fseek(fp_list, 0, SEEK_SET);
95 while(fgets(_line, OS_FLSIZE , fp_list) != NULL)
99 /* Removing new lines */
100 tmp_s = strchr(_line, '\n');
107 os_strdup(_line, tmp_s);
108 if(!OSStore_Put(fts_store, tmp_s, NULL))
110 merror(LIST_ADD_ERROR, ARGV0);
115 /* Creating ignore list */
116 fp_ignore = fopen(IG_QUEUE, "r+");
119 /* Create the file if we cant open it */
120 fp_ignore = fopen(IG_QUEUE, "w+");
124 fp_ignore = fopen(IG_QUEUE, "r+");
127 merror(FOPEN_ERROR, ARGV0, IG_QUEUE);
132 debug1("%s: DEBUG: FTSInit completed.", ARGV0);
137 /* AddtoIGnore -- adds a pattern to be ignored.
139 void AddtoIGnore(Eventinfo *lf)
141 fseek(fp_ignore, 0, SEEK_END);
147 /* Assigning the values to the FTS */
148 fprintf(fp_ignore, "%s %s %s %s %s %s %s %s\n",
149 (lf->decoder_info->name && (lf->generated_rule->ignore & FTS_NAME))?
150 lf->decoder_info->name:"",
151 (lf->id && (lf->generated_rule->ignore & FTS_ID))?lf->id:"",
152 (lf->dstuser&&(lf->generated_rule->ignore & FTS_DSTUSER))?
154 (lf->srcip && (lf->generated_rule->ignore & FTS_SRCIP))?
156 (lf->dstip && (lf->generated_rule->ignore & FTS_DSTIP))?
158 (lf->data && (lf->generated_rule->ignore & FTS_DATA))?
160 (lf->systemname && (lf->generated_rule->ignore & FTS_SYSTEMNAME))?
162 (lf->generated_rule->ignore & FTS_LOCATION)?lf->location:"");
171 * Check if the event is to be ignored.
172 * Only after an event is matched (generated_rule must be set).
174 int IGnore(Eventinfo *lf)
176 char _line[OS_FLSIZE + 1];
177 char _fline[OS_FLSIZE +1];
179 _line[OS_FLSIZE] = '\0';
182 /* Assigning the values to the FTS */
183 snprintf(_line,OS_FLSIZE, "%s %s %s %s %s %s %s %s\n",
184 (lf->decoder_info->name && (lf->generated_rule->ckignore & FTS_NAME))?
185 lf->decoder_info->name:"",
186 (lf->id && (lf->generated_rule->ckignore & FTS_ID))?lf->id:"",
187 (lf->dstuser && (lf->generated_rule->ckignore & FTS_DSTUSER))?
189 (lf->srcip && (lf->generated_rule->ckignore & FTS_SRCIP))?
191 (lf->dstip && (lf->generated_rule->ckignore & FTS_DSTIP))?
193 (lf->data && (lf->generated_rule->ignore & FTS_DATA))?
195 (lf->systemname && (lf->generated_rule->ignore & FTS_SYSTEMNAME))?
197 (lf->generated_rule->ckignore & FTS_LOCATION)?lf->location:"");
199 _fline[OS_FLSIZE] = '\0';
202 /** Checking if the ignore is present **/
203 /* Pointing to the beginning of the file */
204 fseek(fp_ignore, 0, SEEK_SET);
205 while(fgets(_fline, OS_FLSIZE , fp_ignore) != NULL)
207 if(strcmp(_fline, _line) != 0)
210 /* If we match, we can return 1 */
219 * Check if the word "msg" is present on the "queue".
220 * If it is not, write it there.
222 int FTS(Eventinfo *lf)
224 int number_of_matches = 0;
226 char _line[OS_FLSIZE + 1];
228 char *line_for_list = NULL;
230 OSListNode *fts_node;
232 _line[OS_FLSIZE] = '\0';
235 /* Assigning the values to the FTS */
236 snprintf(_line, OS_FLSIZE, "%s %s %s %s %s %s %s %s %s",
237 lf->decoder_info->name,
238 (lf->id && (lf->decoder_info->fts & FTS_ID))?lf->id:"",
239 (lf->dstuser && (lf->decoder_info->fts & FTS_DSTUSER))?lf->dstuser:"",
240 (lf->srcuser && (lf->decoder_info->fts & FTS_SRCUSER))?lf->srcuser:"",
241 (lf->srcip && (lf->decoder_info->fts & FTS_SRCIP))?lf->srcip:"",
242 (lf->dstip && (lf->decoder_info->fts & FTS_DSTIP))?lf->dstip:"",
243 (lf->data && (lf->decoder_info->fts & FTS_DATA))?lf->data:"",
244 (lf->systemname && (lf->decoder_info->fts & FTS_SYSTEMNAME))?lf->systemname:"",
245 (lf->decoder_info->fts & FTS_LOCATION)?lf->location:"");
248 /** Checking if FTS is already present **/
249 if(lf->decoder_info->type == WINDOWS)
251 /* Windows is case insensitive */
252 if(OSStore_NCaseCheck(fts_store, _line))
257 else if(OSStore_NCheck(fts_store, _line))
263 /* Checking if from the last FTS events, we had
264 * at least 3 "similars" before. If yes, we just
267 if(lf->decoder_info->type == IDS)
269 fts_node = OSList_GetLastNode(fts_list);
272 if(OS_StrHowClosedMatch((char *)fts_node->data, _line) >
277 /* We go and add this new entry to the list */
278 if(number_of_matches > 2)
280 _line[fts_minsize_for_str] = '\0';
285 fts_node = OSList_GetPrevNode(fts_list);
288 os_strdup(_line, line_for_list);
289 OSList_AddData(fts_list, line_for_list);
293 /* Storing new entry */
294 if(line_for_list == NULL)
296 os_strdup(_line, line_for_list);
299 if(!OSStore_Put(fts_store, line_for_list, NULL))
301 merror(LIST_ADD_ERROR, ARGV0);
310 /* Saving to fts fp */
311 fseek(fp_list, 0, SEEK_END);
312 fprintf(fp_list,"%s\n", _line);