new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / rules.h
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right 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 #ifndef _OS_RULES
11 #define _OS_RULES
12
13 #define MAX_LAST_EVENTS 11
14
15 #define MAX_TIMEFRAME 604800
16
17 #include "shared.h"
18 #include "active-response.h"
19 #include "lists.h"
20
21 /* Event context  - stored on a uint8 */
22 #define SAME_USER       0x001 /* 1   */
23 #define SAME_SRCIP      0x002 /* 2   */
24 #define SAME_ID         0x004 /* 4   */
25 #define SAME_LOCATION   0x008 /* 8   */
26 #define DIFFERENT_URL   0x010 /* */
27 #define DIFFERENT_SRCIP 0x200 
28 #define DIFFERENT_SRCGEOIP 0x400 
29 #define SAME_SRCPORT    0x020
30 #define SAME_DSTPORT    0x040
31 #define SAME_DODIFF     0x100
32 #define NOT_SAME_USER   0xffe /* 0xfff - 0x001  */
33 #define NOT_SAME_SRCIP  0xffd /* 0xfff - 0x002  */
34 #define NOT_SAME_ID     0xffb /* 0xfff - 0x004  */
35 #define NOT_SAME_AGENT  0xff7 /* 0xfff - 0x008 */
36
37 /* Alert options  - store on a uint8 */
38 #define DO_FTS          0x001
39 #define DO_MAILALERT    0x002
40 #define DO_LOGALERT     0x004
41 #define NO_AR           0x008
42 #define NO_ALERT        0x010
43 #define DO_OVERWRITE    0x020
44 #define DO_PACKETINFO   0x040
45 #define DO_EXTRAINFO    0x100
46 #define SAME_EXTRAINFO  0x200
47
48 #define RULE_MASTER     1
49 #define RULE_SRCIP      2
50 #define RULE_SRCPORT    4
51 #define RULE_DSTIP      8
52 #define RULE_DSTPORT    16
53 #define RULE_USER       32
54 #define RULE_URL        64
55 #define RULE_ID         128
56 #define RULE_HOSTNAME   256
57 #define RULE_PROGRAM_NAME 512
58 #define RULE_STATUS     1024
59 #define RULE_ACTION     2048
60
61 #define RULEINFODETAIL_TEXT     0
62 #define RULEINFODETAIL_LINK     1
63 #define RULEINFODETAIL_CVE      2
64 #define RULEINFODETAIL_OSVDB    3
65 #define RULEINFODETAIL_BUGTRACK 4
66
67 #define MAX_RULEINFODETAIL  32
68
69 typedef struct _FieldInfo {
70     char *name;
71     OSRegex *regex;
72 } FieldInfo;
73
74
75 typedef struct _RuleInfoDetail {
76     int type;
77     char *data;
78     struct _RuleInfoDetail *next;
79 } RuleInfoDetail;
80
81 typedef struct _RuleInfo {
82     int sigid;  /* id attribute -- required*/
83     int level;  /* level attribute --required */
84     size_t maxsize;
85     int frequency;
86     int timeframe;
87
88     u_int8_t context; /* Not an user option */
89
90     int firedtimes;  /* Not an user option */
91     time_t time_ignored; /* Not an user option */
92     int ignore_time;
93     int ignore;
94     int ckignore;
95     unsigned int group_prev_matched_sz;
96
97     int __frequency;
98     char **last_events;
99
100     /* Not an option in the rule */
101     u_int16_t alert_opts;
102
103     /* Context options */
104     u_int16_t context_opts;
105
106     /* Category */
107     u_int8_t category;
108
109     /* Decoded as */
110     u_int16_t decoded_as;
111
112     /* List of previously matched events */
113     OSList *sid_prev_matched;
114
115     /* Pointer to a list (points to sid_prev_matched of if_matched_sid */
116     OSList *sid_search;
117
118     /* List of previously matched events in this group.
119      * Every rule that has if_matched_group will have this
120      * list. Every rule that matches this group, it going to
121      * have a pointer to it (group_search).
122      */
123     OSList **group_prev_matched;
124
125     /* Pointer to group_prev_matched */
126     OSList *group_search;
127
128     /* Function pointer to the event_search */
129     void *(*event_search)(void *lf, void *rule);
130
131     char *group;
132     OSMatch *match;
133     OSPcre2 *match_pcre2;
134     OSRegex *regex;
135     OSPcre2 *pcre2;
136
137     /* Policy-based rules */
138     char *day_time;
139     char *week_day;
140
141     os_ip **srcip;
142     os_ip **dstip;
143     OSMatch *srcgeoip;
144     OSMatch *dstgeoip;
145     OSMatch *srcport;
146     OSMatch *dstport;
147     OSMatch *user;
148     OSMatch *url;
149     OSMatch *id;
150     OSMatch *status;
151     OSMatch *hostname;
152     OSMatch *program_name;
153     OSMatch *extra_data;
154     FieldInfo **fields;
155
156
157     OSPcre2 *srcgeoip_pcre2;
158     OSPcre2 *dstgeoip_pcre2;
159     OSPcre2 *srcport_pcre2;
160     OSPcre2 *dstport_pcre2;
161     OSPcre2 *user_pcre2;
162     OSPcre2 *url_pcre2;
163     OSPcre2 *id_pcre2;
164     OSPcre2 *status_pcre2;
165     OSPcre2 *hostname_pcre2;
166     OSPcre2 *program_name_pcre2;
167     OSPcre2 *extra_data_pcre2;
168     char *action;
169
170     char *comment; /* description in the xml */
171     char *info;
172     char *cve;
173     RuleInfoDetail *info_details;
174     ListRule *lists;
175
176     char *if_sid;
177     char *if_level;
178     char *if_group;
179
180     OSRegex *if_matched_regex;
181     OSMatch *if_matched_group;
182     int if_matched_sid;
183
184     void *(*compiled_rule)(void *lf);
185     active_response **ar;
186
187 } RuleInfo;
188
189
190 typedef struct _RuleNode {
191     RuleInfo *ruleinfo;
192     struct _RuleNode *next;
193     struct _RuleNode *child;
194 } RuleNode;
195
196
197 extern RuleInfo *currently_rule;
198
199 RuleInfoDetail *zeroinfodetails(int type, const char *data);
200 int get_info_attributes(char **attributes, char **values);
201
202 /* RuleInfo functions */
203 RuleInfo *zerorulemember(int id,
204                          int level,
205                          int maxsize,
206                          int frequency,
207                          int timeframe,
208                          int noalert,
209                          int ignore_time,
210                          int overwrite);
211
212
213 /** Rule_list Functions **/
214
215 /* create the rule list */
216 void OS_CreateRuleList(void);
217
218 /* Add rule information to the list */
219 int OS_AddRule(RuleInfo *read_rule);
220
221 /* Add rule information as a child */
222 int OS_AddChild(RuleInfo *read_rule);
223
224 /* Add an overwrite rule */
225 int OS_AddRuleInfo(RuleNode *r_node, RuleInfo *newrule, int sid);
226
227 /* Mark groups (if_matched_group) */
228 int OS_MarkGroup(RuleNode *r_node, RuleInfo *orig_rule);
229
230 /* Mark IDs (if_matched_sid) */
231 int OS_MarkID(RuleNode *r_node, RuleInfo *orig_rule);
232
233 /* Get first rule */
234 RuleNode *OS_GetFirstRule(void);
235
236 void Rules_OP_CreateRules(void);
237
238 int Rules_OP_ReadRules(const char *rulefile);
239
240 int AddHash_Rule(RuleNode *node);
241
242 int _setlevels(RuleNode *node, int nnode);
243
244 /** Definition of the internal rule IDS **
245  ** These SIGIDs cannot be used         **
246  **                                     **/
247
248 #define STATS_MODULE        11
249 #define FTS_MODULE          12
250 #define SYSCHECK_MODULE     13
251 #define HOSTINFO_MODULE     15
252
253 #define ROOTCHECK_MOD   "rootcheck"
254 #define HOSTINFO_NEW    "hostinfo_new"
255 #define HOSTINFO_MOD    "hostinfo_modified"
256 #define SYSCHECK_MOD    "syscheck_integrity_changed"
257 #define SYSCHECK_MOD2   "syscheck_integrity_changed_2nd"
258 #define SYSCHECK_MOD3   "syscheck_integrity_changed_3rd"
259 #define SYSCHECK_NEW    "syscheck_new_entry"
260 #define SYSCHECK_DEL    "syscheck_deleted"
261
262 /* Global variables */
263 extern int _max_freq;
264
265 #endif /* _OS_RULES */
266