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