Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / compiled_rules / generic_samples.c
1 /* @(#) $Id: ./src/analysisd/compiled_rules/generic_samples.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "shared.h"
18 #include "eventinfo.h"
19 #include "config.h"
20
21
22
23 /** Note: If the rule fails to match it should return NULL.
24  * If you want processing to continue, return lf (the eventinfo structure).
25  */
26
27
28
29 /* Example 1:
30  * Comparing if the srcuser and dstuser are the same. If they are the same,
31  * return true.
32  * If any of them is not set, return true too.
33  */
34 void *comp_srcuser_dstuser(Eventinfo *lf)
35 {
36     if(!lf->srcuser || !lf->dstuser)
37     {
38         return(lf);
39     }
40
41     if(strcmp(lf->srcuser, lf->dstuser) == 0)
42     {
43         return(lf);
44     }
45
46
47     /* In here, srcuser and dstuser are present and are different. */
48     return(NULL);
49 }
50
51
52
53 /* Example 2:
54  * Checking if the size of the id field is larger than 10.
55  */
56 void *check_id_size(Eventinfo *lf)
57 {
58     if(!lf->id)
59     {
60         return(NULL);
61     }
62
63     if(strlen(lf->id) >= 10)
64     {
65         return(lf);
66     }
67
68     return(NULL);
69 }
70
71
72
73 /* Example 3:
74  * Comparing the Target Account Name and Caller User Name
75  * on Windows logs.
76  * It will return NULL (not match) if any of these values
77  * are not present or if they are the same.
78  * This function will return TRUE if they are NOT the same.
79  */
80 void *comp_mswin_targetuser_calleruser_diff(Eventinfo *lf)
81 {
82     char *target_user;
83     char *caller_user;
84
85
86     target_user = strstr(lf->log, "Target Account Name");
87     caller_user = strstr(lf->log, "Caller User Name");
88
89     if(!target_user || !caller_user)
90     {
91         return(NULL);
92     }
93
94
95     /* We need to clear each user type and finish the string.
96      * It looks like:
97      * Target Account Name: account\t
98      * Caller User Name: account\t
99      */
100     target_user = strchr(target_user, ':');
101     caller_user = strchr(caller_user, ':');
102
103     if(!target_user || !caller_user)
104     {
105         return(NULL);
106     }
107
108
109     target_user++;
110     caller_user++;
111
112
113     while(*target_user != '\0')
114     {
115         if(*target_user != *caller_user)
116             return(lf);
117
118         if(*target_user == '\t' ||
119            (*target_user == ' '  && target_user[1] == ' '))
120             break;
121
122         target_user++;caller_user++;
123     }
124
125
126     /* If we got in here, the accounts are the same.
127      * So, we return NULL since we only want to alert if they are different.
128      */
129     return(NULL);
130 }
131
132
133 /* Example 4:
134  * Checks if a HTTP request is a simple GET/POST without a query.
135  * This avoid that we call the attack rules for no reason.
136  */
137 void *is_simple_http_request(Eventinfo *lf)
138 {
139
140     /* Simple GET / request. */
141     if(strcmp(lf->url,"/") == 0)
142     {
143         return(lf);
144     }
145
146
147     /* Simple request, no query. */
148     if(!strchr(lf->url,'?'))
149     {
150         return(lf);
151     }
152
153
154     /* In here, we have an additional query to be checked. */
155     return(NULL);
156 }
157
158
159 /* Example 5:
160  * Checks if the source ip is from a valid bot.
161  */
162 void *is_valid_crawler(Eventinfo *lf)
163 {
164     if((strncmp(lf->log, "66.249.",7) == 0)|| /* Google bot */
165        (strncmp(lf->log, "72.14.",6) == 0)||  /* Feedfetcher-Google */
166        (strncmp(lf->log, "209.85.",7) == 0)||  /* Feedfetcher-Google */
167        (strncmp(lf->log, "65.55.",6) == 0)||  /* MSN/Bing */
168        (strncmp(lf->log, "207.46.",7) == 0)||  /* MSN/Bing */
169        (strncmp(lf->log, "74.6.",5) == 0)||  /* Yahoo */
170        (strncmp(lf->log, "72.30.",6) == 0)||  /* Yahoo */
171        (strncmp(lf->log, "67.195.",7) == 0)  /* Yahoo */
172       )
173     {
174         return(lf);
175     }
176
177     return(NULL);
178 }
179
180
181
182 /* END generic samples. */
183