182c9a61876b687622c9244eb3fbe65de7bd3408
[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 tmpstr_buffer[4096 + 1];
51     char *tmp_str = NULL;
52     void *rule_pointer;
53
54
55     lf->decoder_info->type = OSSEC_ALERT;
56
57
58     /* Checking the alert level. */
59     if(strncmp("Alert Level: ", lf->log, 12) != 0 &&
60        strncmp("ossec: Alert Level:", lf->log, 18) != 0)
61     {
62         return(NULL);
63     }
64
65
66     /* Going past the level. */
67     oa_strchr(lf->log, ';', tmp_str);
68     tmp_str++;
69
70
71     /* Getting rule id. */
72     oa_strchr(tmp_str, ':', tmp_str);
73     tmp_str++;
74     if(*tmp_str != ' ')
75     {
76         return(NULL);
77     }
78     tmp_str++;
79
80
81     /* Getting id. */
82     oa_id = tmp_str;
83     oa_strchr(tmp_str, ' ', tmp_str);
84     *tmp_str = '\0';
85
86
87     /* Getting rule structure. */
88     rule_pointer = OSHash_Get(Config.g_rules_hash, oa_id);
89     if(!rule_pointer)
90     {
91         merror("%s: WARN: Rule id '%s' not found internally.", ARGV0, oa_id);
92         *tmp_str = ' ';
93         return(NULL);
94     }
95     *tmp_str = ' ';
96     oa_strchr(tmp_str, ';', tmp_str);
97     tmp_str++;
98
99
100     /* Checking location. */
101     if(strncmp(" Location: ", tmp_str, 11) != 0)
102     {
103         return(NULL);
104     }
105     tmp_str+=11;
106
107
108     /* Setting location; */
109     oa_location = tmp_str;
110
111
112     oa_strchr(tmp_str, ';', tmp_str);
113     *tmp_str = '\0';
114
115
116     /* Setting new location. */
117     oa_newlocation[255] = '\0';
118
119     if(lf->hostname == lf->location)
120     {
121         snprintf(oa_newlocation, 255, "%s|%s", lf->location, oa_location);
122         free(lf->location);
123         os_strdup(oa_newlocation, lf->location);
124         lf->hostname = lf->location;
125     }
126     else
127     {
128         snprintf(oa_newlocation, 255, "%s->%s|%s", lf->hostname,
129                  lf->location, oa_location);
130         free(lf->location);
131         os_strdup(oa_newlocation, lf->location);
132         lf->hostname = lf->location;
133     }
134
135     *tmp_str = ';';
136     tmp_str++;
137
138
139     /* Getting additional fields. */
140     while((*tmp_str == ' ') && (tmp_str[1] != ' '))
141     {
142         tmp_str++;
143         oa_val = tmp_str;
144
145         tmp_str = strchr(tmp_str, ';');
146         if(!tmp_str)
147         {
148             return(NULL);
149         }
150         *tmp_str = '\0';
151
152         if(strncmp(oa_val, "srcip: ", 7) == 0)
153         {
154             os_strdup(oa_val + 7, lf->srcip);
155         }
156         if(strncmp(oa_val, "user: ", 6) == 0)
157         {
158             os_strdup(oa_val + 6, lf->dstuser);
159         }
160
161         *tmp_str = ';';
162         tmp_str++;
163     }
164
165
166     /* Removing space. */
167     while(*tmp_str == ' ')
168         tmp_str++;
169
170     /* Create new full log */
171     tmpstr_buffer[0] = '\0';
172     tmpstr_buffer[4095] = '\0';
173     strncpy(tmpstr_buffer, tmp_str, 4094);
174
175
176     free(lf->full_log);
177     lf->full_log = NULL;
178     os_strdup(tmpstr_buffer, lf->full_log);
179
180     lf->log = lf->full_log;
181
182
183     /* Rule that generated. */
184     lf->generated_rule = rule_pointer;
185
186
187     return(NULL);
188 }
189
190 /* END Decoder */