new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / decoders / plugins / ossecalert_decoder.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation.
8  *
9  */
10
11
12 #include "shared.h"
13 #include "eventinfo.h"
14 #include "config.h"
15
16
17
18
19 /* OSSECAlert decoder init */
20 void *OSSECAlert_Decoder_Init()
21 {
22     debug1("%s: Initializing OSSECAlert decoder.", ARGV0);
23
24
25     /* There is nothing else to do over here */
26     return(NULL);
27 }
28
29
30
31 #define oa_strchr(x,y,z) z = strchr(x,y); if(!z){ return(NULL); }
32
33 /* OSSECAlert decoder 
34  * Will extract the rule_id and point back to the original rule.
35  * Will also extract srcip and username if available.
36  * Examples:
37  * 
38  */                             
39 void *OSSECAlert_Decoder_Exec(Eventinfo *lf)
40 {
41     char *oa_id = 0;
42     char *oa_location;
43     char *oa_val;
44     char oa_newlocation[256];
45     char agent_file[OS_SIZE_1024 +1];
46     char tmpstr_buffer[4096 +1];
47     char *tmp_str = NULL;
48     void *rule_pointer;
49     FILE *fp;
50
51
52     lf->decoder_info->type = OSSEC_ALERT;
53
54
55     /* Checking the alert level. */
56     if(strncmp("Alert Level: ", lf->log, 12) != 0 &&
57        strncmp("ossec: Alert Level:", lf->log, 18) != 0)
58     {
59         return(NULL);
60     }
61
62     
63     /* Going past the level. */
64     oa_strchr(lf->log, ';', tmp_str);
65     tmp_str++;
66
67
68     /* Getting rule id. */
69     oa_strchr(tmp_str, ':', tmp_str);
70     tmp_str++;
71     if(*tmp_str != ' ')
72     {
73         return(NULL);
74     }    
75     tmp_str++;
76
77     
78     /* Getting id. */
79     oa_id = tmp_str;
80     oa_strchr(tmp_str, ' ', tmp_str);
81     *tmp_str = '\0';
82
83
84     /* Getting rule structure. */
85     rule_pointer = OSHash_Get(Config.g_rules_hash, oa_id);
86     if(!rule_pointer)
87     {
88         *tmp_str = ' ';
89         merror("%s: WARN: Rule id '%s' not found internally: %s", ARGV0, oa_id, lf->log);
90         *tmp_str = ' ';
91         return(NULL);
92     }
93     *tmp_str = ' ';
94     oa_strchr(tmp_str, ';', tmp_str);
95     tmp_str++;
96
97
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
117     /* Setting new location. */
118     oa_newlocation[255] = '\0';
119     agent_file[OS_SIZE_1024] = '\0';
120
121     
122     snprintf(agent_file, OS_SIZE_1024, "%s/%s->%s",
123              AGENTINFO_DIR, lf->hostname, lf->location);
124
125     snprintf(oa_newlocation, 255, "%s|%s", lf->location, oa_location);
126     free(lf->location);
127     os_strdup(oa_newlocation, lf->location);
128     lf->hostname = lf->location;
129
130
131
132     /* Writting to the agent file */
133     fp = fopen(agent_file, "w");
134     if(fp)
135     {
136         fprintf(fp, "%s\n", "Remote Syslog");
137         fclose(fp);
138     }
139
140
141     *tmp_str = ';';
142     tmp_str++;
143
144
145
146
147     /* Getting additional fields. */
148     while((*tmp_str == ' ') && (tmp_str[1] != ' '))
149     {
150         tmp_str++;
151         oa_val = tmp_str;
152
153         tmp_str = strchr(tmp_str, ';');
154         if(!tmp_str)
155         {
156             return(NULL);
157         }
158         *tmp_str = '\0';
159
160         if(strncmp(oa_val, "srcip: ", 7) == 0)
161         {
162             os_strdup(oa_val + 7, lf->srcip);
163         }
164         if(strncmp(oa_val, "user: ", 6) == 0)
165         {
166             os_strdup(oa_val + 6, lf->dstuser);
167         }
168
169         *tmp_str = ';';
170         tmp_str++;
171     }
172     
173
174     /* Removing space. */
175     while(*tmp_str == ' ')
176         tmp_str++;
177     
178
179     /* Creating new full log. */
180     tmpstr_buffer[0] = '\0';
181     tmpstr_buffer[4095] = '\0';
182     strncpy(tmpstr_buffer, tmp_str, 4094);
183
184     free(lf->full_log);
185     lf->full_log = NULL;
186     os_strdup(tmpstr_buffer, lf->full_log);
187     lf->log = lf->full_log;
188     
189
190     /* Rule that generated. */
191     lf->generated_rule = rule_pointer;
192
193
194     return(NULL);
195 }
196
197 /* END Decoder */