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