Imported Upstream version 2.3
[ossec-hids.git] / src / analysisd / rules.h
1 /* @(#) $Id: rules.h,v 1.41 2009/06/24 17:06:22 dcid Exp $ */
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 3) 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
22 /* Event context  - stored on a uint8 */
23 #define SAME_USER       0x001 /* 1   */
24 #define SAME_SRCIP      0x002 /* 2   */
25 #define SAME_ID         0x004 /* 4   */
26 #define SAME_LOCATION   0x008 /* 8   */
27 #define DIFFERENT_URL   0x010 /* */
28 #define SAME_SRCPORT    0x020
29 #define SAME_DSTPORT    0x040
30 #define NOT_SAME_USER   0xffe /* 0xfff - 0x001  */
31 #define NOT_SAME_SRCIP  0xffd /* 0xfff - 0x002  */
32 #define NOT_SAME_ID     0xffb /* 0xfff - 0x004  */
33 #define NOT_SAME_AGENT  0xff7 /* 0xfff - 0x008 */
34
35 /* Alert options  - store on a uint8 */
36 #define DO_FTS          0x001
37 #define DO_MAILALERT    0x002
38 #define DO_LOGALERT     0x004
39 #define NO_ALERT        0x010
40 #define DO_OVERWRITE    0x020
41 #define DO_PACKETINFO   0x040
42 #define DO_EXTRAINFO    0x100
43 #define SAME_EXTRAINFO  0x200
44
45
46 typedef struct _RuleInfo
47 {
48     int sigid;  /* id attribute -- required*/
49     int level;  /* level attribute --required */
50     int maxsize;
51     int frequency;
52     int timeframe;
53
54     u_int8_t context; /* Not an user option */
55
56     int firedtimes;  /* Not an user option */
57     int time_ignored; /* Not an user option */
58     int ignore_time;
59     int ignore;
60     int ckignore;
61     int group_prev_matched_sz;
62
63     int __frequency;
64     char **last_events;
65     
66
67     /* Not an option in the rule */
68     u_int16_t alert_opts;
69
70     /* Context options */
71     u_int16_t context_opts;
72
73     /* category */
74     u_int8_t category;
75    
76     /* Decoded as */
77     u_int16_t decoded_as;
78
79     /* List of previously matched events */
80     OSList *sid_prev_matched;
81
82     /* Pointer to a list (points to sid_prev_matched of if_matched_sid */
83     OSList *sid_search;
84
85     /* List of previously matched events in this group.
86      * Every rule that has if_matched_group will have this
87      * list. Every rule that matches this group, it going to
88      * have a pointer to it (group_search).
89      */
90     OSList **group_prev_matched;
91
92     /* Pointer to group_prev_matched */
93     OSList *group_search;
94
95     /* Function pointer to the event_search. */
96     void *(*event_search)(void *lf, void *rule);
97     
98
99     char *group;
100     OSMatch *match;
101     OSRegex *regex;
102
103     /* Policy-based rules */
104     char *day_time;
105     char *week_day;
106
107     os_ip **srcip;
108     os_ip **dstip;
109     OSMatch *srcport;
110     OSMatch *dstport;
111     OSMatch *user;
112     OSMatch *url;
113     OSMatch *id;
114     OSMatch *status;
115     OSMatch *hostname;
116     OSMatch *program_name;
117     OSMatch *extra_data;
118     char *action;
119     
120     char *comment; /* description in the xml */
121     char *info;
122     char *cve;
123     
124     char *if_sid;
125     char *if_level;
126     char *if_group;
127
128     OSRegex *if_matched_regex;
129     OSMatch *if_matched_group;
130     int if_matched_sid;
131     
132     void *(*compiled_rule)(void *lf);
133     active_response **ar;
134
135 }RuleInfo;
136
137
138 typedef struct _RuleNode
139 {
140     RuleInfo *ruleinfo;
141     struct _RuleNode *next;
142     struct _RuleNode *child;
143 }RuleNode;
144
145
146 RuleInfo *currently_rule; /* */
147
148 /* RuleInfo functions */
149 RuleInfo *zerorulemember(int id, 
150                          int level,
151                          int maxsize, 
152                          int frequency,
153                          int timeframe, 
154                          int noalert,
155                          int ignore_time,
156                          int overwrite);
157
158
159 /** Rule_list Functions **/
160
161 /* create the rule list */
162 void OS_CreateRuleList();
163
164 /* Add rule information to the list */
165 int OS_AddRule(RuleInfo *read_rule);
166
167 /* Add rule information as a child */
168 int OS_AddChild(RuleInfo *read_rule);
169
170 /* Add an overwrite rule */
171 int OS_AddRuleInfo(RuleNode *r_node, RuleInfo *newrule, int sid);
172
173 /* Mark groups (if_matched_group) */
174 int OS_MarkGroup(RuleNode *r_node, RuleInfo *orig_rule);
175
176 /* Mark IDs (if_matched_sid) */
177 int OS_MarkID(RuleNode *r_node, RuleInfo *orig_rule);
178
179
180 /* Get first rule */
181 RuleNode *OS_GetFirstRule();
182
183
184 /** Defition of the internal rule IDS **
185  ** These SIGIDs cannot be used       **
186  **                                   **/
187    
188 #define STATS_MODULE        11
189 #define FTS_MODULE          12
190 #define SYSCHECK_MODULE     13   
191 #define HOSTINFO_MODULE     15
192
193
194 #define ROOTCHECK_MOD   "rootcheck"
195 #define HOSTINFO_NEW    "hostinfo_new"
196 #define HOSTINFO_MOD    "hostinfo_modified"
197 #define SYSCHECK_MOD    "syscheck_integrity_changed"
198 #define SYSCHECK_MOD2   "syscheck_integrity_changed_2nd"
199 #define SYSCHECK_MOD3   "syscheck_integrity_changed_3rd"
200 #define SYSCHECK_NEW    "syscheck_new_entry"
201 #define SYSCHECK_DEL    "syscheck_deleted"
202
203
204 #endif /* _OS_RULES */