Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / decoders / plugins / ossecalert_decoder.c
1 /* @(#) $Id: ./src/analysisd/decoders/plugins/ossecalert_decoder.c, 2012/03/28 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation.
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "shared.h"
18 #include "eventinfo.h"
19 #include "config.h"
20
21
22
23
24 /* OSSECAlert decoder init */
25 void *OSSECAlert_Decoder_Init()
26 {
27     debug1("%s: Initializing OSSECAlert decoder.", ARGV0);
28
29
30     /* There is nothing else to do over here */
31     return(NULL);
32 }
33
34
35
36 #define oa_strchr(x,y,z) z = strchr(x,y); if(!z){ return(NULL); }
37
38 /* OSSECAlert decoder
39  * Will extract the rule_id and point back to the original rule.
40  * Will also extract srcip and username if available.
41  * Examples:
42  *
43  */
44 void *OSSECAlert_Decoder_Exec(Eventinfo *lf)
45 {
46     char *oa_id = 0;
47     char *oa_location;
48     char *oa_val;
49     char oa_newlocation[256];
50     char *tmp_str = NULL;
51     void *rule_pointer;
52
53
54     lf->decoder_info->type = OSSEC_ALERT;
55
56
57     /* Checking the alert level. */
58     if(strncmp("Alert Level: ", lf->log, 12) != 0 &&
59        strncmp("ossec: Alert Level:", lf->log, 18) != 0)
60     {
61         return(NULL);
62     }
63
64
65     /* Going past the level. */
66     oa_strchr(lf->log, ';', tmp_str);
67     tmp_str++;
68
69
70     /* Getting rule id. */
71     oa_strchr(tmp_str, ':', tmp_str);
72     tmp_str++;
73     if(*tmp_str != ' ')
74     {
75         return(NULL);
76     }
77     tmp_str++;
78
79
80     /* Getting id. */
81     oa_id = tmp_str;
82     oa_strchr(tmp_str, ' ', tmp_str);
83     *tmp_str = '\0';
84
85
86     /* Getting rule structure. */
87     rule_pointer = OSHash_Get(Config.g_rules_hash, oa_id);
88     if(!rule_pointer)
89     {
90         merror("%s: WARN: Rule id '%s' not found internally.", ARGV0, oa_id);
91         *tmp_str = ' ';
92         return(NULL);
93     }
94     *tmp_str = ' ';
95     oa_strchr(tmp_str, ';', tmp_str);
96     tmp_str++;
97
98
99     /* Checking location. */
100     if(strncmp(" Location: ", tmp_str, 11) != 0)
101     {
102         return(NULL);
103     }
104     tmp_str+=11;
105
106
107     /* Setting location; */
108     oa_location = tmp_str;
109
110
111     oa_strchr(tmp_str, ';', tmp_str);
112     *tmp_str = '\0';
113
114
115     /* Setting new location. */
116     oa_newlocation[255] = '\0';
117
118     if(lf->hostname == lf->location)
119     {
120         snprintf(oa_newlocation, 255, "%s|%s", lf->location, oa_location);
121         free(lf->location);
122         os_strdup(oa_newlocation, lf->location);
123         lf->hostname = lf->location;
124     }
125     else
126     {
127         snprintf(oa_newlocation, 255, "%s->%s|%s", lf->hostname,
128                  lf->location, oa_location);
129         free(lf->location);
130         os_strdup(oa_newlocation, lf->location);
131         lf->hostname = lf->location;
132     }
133
134     *tmp_str = ';';
135     tmp_str++;
136
137
138     /* Getting additional fields. */
139     while((*tmp_str == ' ') && (tmp_str[1] != ' '))
140     {
141         tmp_str++;
142         oa_val = tmp_str;
143
144         tmp_str = strchr(tmp_str, ';');
145         if(!tmp_str)
146         {
147             return(NULL);
148         }
149         *tmp_str = '\0';
150
151         if(strncmp(oa_val, "srcip: ", 7) == 0)
152         {
153             os_strdup(oa_val + 7, lf->srcip);
154         }
155         if(strncmp(oa_val, "user: ", 6) == 0)
156         {
157             os_strdup(oa_val + 6, lf->dstuser);
158         }
159
160         *tmp_str = ';';
161         tmp_str++;
162     }
163
164
165     /* Removing space. */
166     while(*tmp_str == ' ')
167         tmp_str++;
168
169
170     /* Creating new full log. */
171     free(lf->full_log);
172     os_strdup(tmp_str, lf->full_log);
173     lf->log = lf->full_log;
174
175
176     /* Rule that generated. */
177     lf->generated_rule = rule_pointer;
178
179
180     return(NULL);
181 }
182
183 /* END Decoder */