new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / compiled_rules / generic_samples.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation.
8  */
9
10 #include "eventinfo.h"
11 #include "shared.h"
12 #include "config.h"
13
14
15 /* Note: If the rule fails to match it should return NULL.
16  * If you want processing to continue, return lf (the eventinfo structure).
17  */
18
19 /* Example 1: Comparing if the srcuser and dstuser are the same
20  * If they are the same, return true
21  * If any of them is not set, return true too
22  */
23 void *comp_srcuser_dstuser(Eventinfo *lf)
24 {
25     if (!lf->srcuser || !lf->dstuser) {
26         return (lf);
27     }
28
29     if (strcmp(lf->srcuser, lf->dstuser) == 0) {
30         return (lf);
31     }
32
33     /* In here, srcuser and dstuser are present and are different */
34     return (NULL);
35 }
36
37 /* Example 2: Checking if the size of the id field is larger than 10 */
38 void *check_id_size(Eventinfo *lf)
39 {
40     if (!lf->id) {
41         return (NULL);
42     }
43
44     if (strlen(lf->id) >= 10) {
45         return (lf);
46     }
47
48     return (NULL);
49 }
50
51 /* Example 3: Comparing the Target Account Name and Caller User Name on Windows logs
52  * It will return NULL (not match) if any of these values
53  * are not present or if they are the same.
54  * This function will return TRUE if they are NOT the same.
55  */
56 void *comp_mswin_targetuser_calleruser_diff(Eventinfo *lf)
57 {
58     char *target_user;
59     char *caller_user;
60
61     target_user = strstr(lf->log, "Target Account Name");
62     caller_user = strstr(lf->log, "Caller User Name");
63
64     if (!target_user || !caller_user) {
65         return (NULL);
66     }
67
68     /* We need to clear each user type and finish the string.
69      * It looks like:
70      * Target Account Name: account\t
71      * Caller User Name: account\t
72      */
73     target_user = strchr(target_user, ':');
74     caller_user = strchr(caller_user, ':');
75
76     if (!target_user || !caller_user) {
77         return (NULL);
78     }
79
80     target_user++;
81     caller_user++;
82
83     while (*target_user != '\0') {
84         if (*target_user != *caller_user) {
85             return (lf);
86         }
87
88         if (*target_user == '\t' ||
89                 (*target_user == ' '  && target_user[1] == ' ')) {
90             break;
91         }
92
93         target_user++;
94         caller_user++;
95     }
96
97     /* If we got in here, the accounts are the same.
98      * So, we return NULL since we only want to alert if they are different.
99      */
100     return (NULL);
101 }
102
103 /* Example 4: Checking if a HTTP request is a simple GET/POST without a query
104  * This avoid that we call the attack rules for no reason.
105  */
106 void *is_simple_http_request(Eventinfo *lf)
107 {
108
109     if (!lf->url) {
110         return (NULL);
111     }
112
113     /* Simple GET / request */
114     if (strcmp(lf->url, "/") == 0) {
115         return (lf);
116     }
117
118     /* Simple request, no query */
119     if (!strchr(lf->url, '?')) {
120         return (lf);
121     }
122
123     /* In here, we have an additional query to be checked */
124     return (NULL);
125 }
126
127 /* Example 5: Checking if the source IP is from a valid bot */
128 void *is_valid_crawler(Eventinfo *lf)
129 {
130     if ((strncmp(lf->log, "66.249.", 7) == 0) || /* Google bot */
131             (strncmp(lf->log, "72.14.", 6) == 0) || /* Feedfetcher-Google */
132             (strncmp(lf->log, "209.85.", 7) == 0) || /* Feedfetcher-Google */
133             (strncmp(lf->log, "65.55.", 6) == 0) || /* MSN/Bing */
134             (strncmp(lf->log, "207.46.", 7) == 0) || /* MSN/Bing */
135             (strncmp(lf->log, "74.6.", 5) == 0) || /* Yahoo */
136             (strncmp(lf->log, "72.30.", 6) == 0) || /* Yahoo */
137             (strncmp(lf->log, "67.195.", 7) == 0) /* Yahoo */
138        ) {
139         return (lf);
140     }
141
142     return (NULL);
143 }
144