Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / rules.h
1 /* @(#) $Id: ./src/analysisd/rules.h, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right 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
13
14 #ifndef _OS_RULES
15
16 #define _OS_RULES
17
18 #define MAX_LAST_EVENTS 11
19
20 #include "shared.h"
21 #include "active-response.h"
22 #include "lists.h"
23
24
25 /* Event context  - stored on a uint8 */
26 #define SAME_USER       0x001 /* 1   */
27 #define SAME_SRCIP      0x002 /* 2   */
28 #define SAME_ID         0x004 /* 4   */
29 #define SAME_LOCATION   0x008 /* 8   */
30 #define DIFFERENT_URL   0x010 /* */
31 #define SAME_SRCPORT    0x020
32 #define SAME_DSTPORT    0x040
33 #define SAME_DODIFF     0x100
34 #define NOT_SAME_USER   0xffe /* 0xfff - 0x001  */
35 #define NOT_SAME_SRCIP  0xffd /* 0xfff - 0x002  */
36 #define NOT_SAME_ID     0xffb /* 0xfff - 0x004  */
37 #define NOT_SAME_AGENT  0xff7 /* 0xfff - 0x008 */
38
39 /* Alert options  - store on a uint8 */
40 #define DO_FTS          0x001
41 #define DO_MAILALERT    0x002
42 #define DO_LOGALERT     0x004
43 #define NO_AR           0x008
44 #define NO_ALERT        0x010
45 #define DO_OVERWRITE    0x020
46 #define DO_PACKETINFO   0x040
47 #define DO_EXTRAINFO    0x100
48 #define SAME_EXTRAINFO  0x200
49
50 #define RULE_MASTER     1
51 #define RULE_SRCIP      2
52 #define RULE_SRCPORT    4
53 #define RULE_DSTIP      8
54 #define RULE_DSTPORT    16
55 #define RULE_USER       32
56 #define RULE_URL        64
57 #define RULE_ID         128
58 #define RULE_HOSTNAME   256
59 #define RULE_PROGRAM_NAME 512
60 #define RULE_STATUS     1024
61 #define RULE_ACTION     2048
62
63
64 #define RULEINFODETAIL_TEXT     0
65 #define RULEINFODETAIL_LINK     1
66 #define RULEINFODETAIL_CVE      2
67 #define RULEINFODETAIL_OSVDB    3
68 #define RULEINFODETAIL_BUGTRACK 4
69
70 #define MAX_RULEINFODETAIL  32
71
72 typedef struct _RuleInfoDetail
73 {
74     int type;
75     char *data;
76     struct _RuleInfoDetail *next;
77 }RuleInfoDetail;
78
79 typedef struct _RuleInfo
80 {
81     int sigid;  /* id attribute -- required*/
82     int level;  /* level attribute --required */
83     int maxsize;
84     int frequency;
85     int timeframe;
86
87     u_int8_t context; /* Not an user option */
88
89     int firedtimes;  /* Not an user option */
90     int time_ignored; /* Not an user option */
91     int ignore_time;
92     int ignore;
93     int ckignore;
94     int group_prev_matched_sz;
95
96     int __frequency;
97     char **last_events;
98
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
132     char *group;
133     OSMatch *match;
134     OSRegex *regex;
135
136     /* Policy-based rules */
137     char *day_time;
138     char *week_day;
139
140     os_ip **srcip;
141     os_ip **dstip;
142     OSMatch *srcport;
143     OSMatch *dstport;
144     OSMatch *user;
145     OSMatch *url;
146     OSMatch *id;
147     OSMatch *status;
148     OSMatch *hostname;
149     OSMatch *program_name;
150     OSMatch *extra_data;
151     char *action;
152
153     char *comment; /* description in the xml */
154     char *info;
155     char *cve;
156     RuleInfoDetail *info_details;
157     ListRule *lists;
158
159     char *if_sid;
160     char *if_level;
161     char *if_group;
162
163     OSRegex *if_matched_regex;
164     OSMatch *if_matched_group;
165     int if_matched_sid;
166
167     void *(*compiled_rule)(void *lf);
168     active_response **ar;
169
170 }RuleInfo;
171
172
173 typedef struct _RuleNode
174 {
175     RuleInfo *ruleinfo;
176     struct _RuleNode *next;
177     struct _RuleNode *child;
178 }RuleNode;
179
180
181 RuleInfo *currently_rule; /* */
182
183 RuleInfoDetail *zeroinfodetails(int type, char *data);
184 int get_info_attributes(char **attributes, char **values);
185
186 /* RuleInfo functions */
187 RuleInfo *zerorulemember(int id,
188                          int level,
189                          int maxsize,
190                          int frequency,
191                          int timeframe,
192                          int noalert,
193                          int ignore_time,
194                          int overwrite);
195
196
197 /** Rule_list Functions **/
198
199 /* create the rule list */
200 void OS_CreateRuleList();
201
202 /* Add rule information to the list */
203 int OS_AddRule(RuleInfo *read_rule);
204
205 /* Add rule information as a child */
206 int OS_AddChild(RuleInfo *read_rule);
207
208 /* Add an overwrite rule */
209 int OS_AddRuleInfo(RuleNode *r_node, RuleInfo *newrule, int sid);
210
211 /* Mark groups (if_matched_group) */
212 int OS_MarkGroup(RuleNode *r_node, RuleInfo *orig_rule);
213
214 /* Mark IDs (if_matched_sid) */
215 int OS_MarkID(RuleNode *r_node, RuleInfo *orig_rule);
216
217
218 /* Get first rule */
219 RuleNode *OS_GetFirstRule();
220
221
222 /** Defition of the internal rule IDS **
223  ** These SIGIDs cannot be used       **
224  **                                   **/
225
226 #define STATS_MODULE        11
227 #define FTS_MODULE          12
228 #define SYSCHECK_MODULE     13
229 #define HOSTINFO_MODULE     15
230
231
232 #define ROOTCHECK_MOD   "rootcheck"
233 #define HOSTINFO_NEW    "hostinfo_new"
234 #define HOSTINFO_MOD    "hostinfo_modified"
235 #define SYSCHECK_MOD    "syscheck_integrity_changed"
236 #define SYSCHECK_MOD2   "syscheck_integrity_changed_2nd"
237 #define SYSCHECK_MOD3   "syscheck_integrity_changed_3rd"
238 #define SYSCHECK_NEW    "syscheck_new_entry"
239 #define SYSCHECK_DEL    "syscheck_deleted"
240
241
242 #endif /* _OS_RULES */