new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / rootcheck / check_rc_trojans.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right 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 "shared.h"
11 #include "rootcheck.h"
12
13
14 /* Read the file pointer specified (rootkit_trojans)
15  * and check if any trojan entry is in the configured files
16  */
17 void check_rc_trojans(const char *basedir, FILE *fp)
18 {
19     int i = 0, _errors = 0, _total = 0;
20     char buf[OS_SIZE_1024 + 1];
21     char file_path[OS_SIZE_1024 + 1];
22     char *file;
23     char *string_to_look;
24
25 #ifndef WIN32
26     const char *(all_paths[]) = {"bin", "sbin", "usr/bin", "usr/sbin", NULL};
27 #else
28     const char *(all_paths[]) = {"C:\\Windows\\", "D:\\Windows\\", NULL};
29 #endif
30
31     debug1("%s: DEBUG: Starting on check_rc_trojans", ARGV0);
32
33     while (fgets(buf, OS_SIZE_1024, fp) != NULL) {
34         char *nbuf;
35         char *message = NULL;
36
37         i = 0;
38         /* Remove end of line */
39         nbuf = strchr(buf, '\n');
40         if (nbuf) {
41             *nbuf = '\0';
42         }
43
44         nbuf = normalize_string(buf);
45
46         if (*nbuf == '\0' || *nbuf == '#') {
47             continue;
48         }
49
50         /* File now may be valid */
51         file = nbuf;
52
53         string_to_look = strchr(file, '!');
54         if (!string_to_look) {
55             continue;
56         }
57
58         *string_to_look = '\0';
59         string_to_look++;
60
61         message = strchr(string_to_look, '!');
62         if (!message) {
63             continue;
64         }
65         *message = '\0';
66         message++;
67
68         string_to_look = normalize_string(string_to_look);
69         file = normalize_string(file);
70         message = normalize_string(message);
71
72         if (*file == '\0' || *string_to_look == '\0') {
73             continue;
74         }
75
76         _total++;
77
78         /* Try with all possible paths */
79         while (all_paths[i] != NULL) {
80             if (*file != '/') {
81                 snprintf(file_path, OS_SIZE_1024, "%s/%s/%s", basedir,
82                          all_paths[i],
83                          file);
84             } else {
85                 strncpy(file_path, file, OS_SIZE_1024);
86                 file_path[OS_SIZE_1024 - 1] = '\0';
87             }
88
89             /* Check if entry is found */
90             if (is_file(file_path) && os_string(file_path, string_to_look)) {
91                 char op_msg[OS_SIZE_1024 + 1];
92                 _errors = 1;
93
94                 snprintf(op_msg, OS_SIZE_1024, "Trojaned version of file "
95                          "'%s' detected. Signature used: '%s' (%s).",
96                          file_path,
97                          string_to_look,
98                          *message == '\0' ?
99                          "Generic" : message);
100
101                 notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
102             }
103
104             if (*file == '/') {
105                 break;
106             }
107             i++;
108         }
109         continue;
110     }
111
112     if (_errors == 0) {
113         char op_msg[OS_SIZE_1024 + 1];
114         snprintf(op_msg, OS_SIZE_1024, "No binaries with any trojan detected. "
115                  "Analyzed %d files.", _total);
116         notify_rk(ALERT_OK, op_msg);
117     }
118 }
119