Imported Upstream version 2.7
[ossec-hids.git] / src / rootcheck / run_rk_check.c
1 /* @(#) $Id: ./src/rootcheck/run_rk_check.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right 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
13
14 #include "shared.h"
15 #include "rootcheck.h"
16
17
18 /* notify_rk
19  * Report a problem.
20  */
21 int notify_rk(int rk_type, char *msg)
22 {
23     /* Non-queue notification */
24     if(rootcheck.notify != QUEUE)
25     {
26         if(rk_type == ALERT_OK)
27             printf("[OK]: %s\n", msg);
28         else if(rk_type == ALERT_SYSTEM_ERROR)
29             printf("[ERR]: %s\n", msg);
30         else if(rk_type == ALERT_POLICY_VIOLATION)
31             printf("[INFO]: %s\n", msg);
32         else
33         {
34             printf("[FAILED]: %s\n", msg);
35         }
36
37         printf("\n");
38         return(0);
39     }
40
41     /* No need to alert on that to the server */
42     if(rk_type <= ALERT_SYSTEM_ERROR)
43         return(0);
44
45     #ifdef OSSECHIDS
46     if(SendMSG(rootcheck.queue, msg, ROOTCHECK, ROOTCHECK_MQ) < 0)
47     {
48         merror(QUEUE_SEND, ARGV0);
49
50         if((rootcheck.queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
51         {
52             ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
53         }
54
55         if(SendMSG(rootcheck.queue,msg,ROOTCHECK,ROOTCHECK_MQ) < 0)
56         {
57             ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
58         }
59     }
60     #endif
61
62     return(0);
63 }
64
65
66 /* start_rk_daemon
67  * Start the rootkit daemon variables
68  */
69 void start_rk_daemon()
70 {
71     return;
72
73     if(rootcheck.notify == QUEUE)
74     {
75     }
76 }
77
78
79 /* run_rk_check: v0.1
80  * Execute the rootkit checks
81  */
82 void run_rk_check()
83 {
84     time_t time1;
85     time_t time2;
86
87     FILE *fp;
88     OSList *plist;
89
90     #ifndef WIN32
91     /* Hard coding basedir */
92     int i;
93     char basedir[] = "/";
94
95     /* Removing the last / from basedir */
96     i = strlen(basedir);
97     if(i > 0)
98     {
99         if(basedir[i-1] == '/')
100         {
101             basedir[i-1] = '\0';
102         }
103     }
104     #else
105
106     /* Basedir for Windows */
107     char basedir[] = "C:\\";
108
109     #endif
110
111
112     /* Setting basedir */
113     if(rootcheck.basedir == NULL)
114     {
115         rootcheck.basedir = basedir;
116     }
117
118
119     time1 = time(0);
120
121     /*** Initial message ***/
122     if(rootcheck.notify != QUEUE)
123     {
124         printf("\n");
125         printf("** Starting Rootcheck v0.9 by Daniel B. Cid        **\n");
126         printf("** http://www.ossec.net/en/about.html#dev-team     **\n");
127         printf("** http://www.ossec.net/rootcheck/                 **\n\n");
128         printf("Be patient, it may take a few minutes to complete...\n");
129         printf("\n");
130     }
131
132
133     /* Cleaning the global variables */
134     rk_sys_count = 0;
135     rk_sys_file[rk_sys_count] = NULL;
136     rk_sys_name[rk_sys_count] = NULL;
137
138
139
140     /* Sending scan start message */
141     notify_rk(ALERT_POLICY_VIOLATION, "Starting rootcheck scan.");
142     if(rootcheck.notify == QUEUE)
143     {
144         merror("%s: INFO: Starting rootcheck scan.", ARGV0);
145     }
146
147
148
149     /***  First check, look for rootkits ***/
150     /* Open rootkit_files and pass the pointer to check_rc_files */
151     if (rootcheck.checks.rc_files)
152     {
153         if(!rootcheck.rootkit_files)
154         {
155             #ifndef WIN32
156             merror("%s: No rootcheck_files file configured.", ARGV0);
157             #endif
158         }
159
160         else
161         {
162             fp = fopen(rootcheck.rootkit_files, "r");
163             if(!fp)
164             {
165                 merror("%s: No rootcheck_files file: '%s'",ARGV0,
166                         rootcheck.rootkit_files);
167             }
168
169             else
170             {
171                 check_rc_files(rootcheck.basedir, fp);
172
173                 fclose(fp);
174             }
175         }
176     }
177
178
179
180     /*** Second check. look for trojan entries in common binaries ***/
181     if (rootcheck.checks.rc_trojans)
182     {
183         if(!rootcheck.rootkit_trojans)
184         {
185             #ifndef WIN32
186             merror("%s: No rootcheck_trojans file configured.", ARGV0);
187             #endif
188         }
189
190         else
191         {
192             fp = fopen(rootcheck.rootkit_trojans, "r");
193             if(!fp)
194             {
195                 merror("%s: No rootcheck_trojans file: '%s'",ARGV0,
196                                             rootcheck.rootkit_trojans);
197             }
198
199             else
200             {
201                 #ifndef HPUX
202                 check_rc_trojans(rootcheck.basedir, fp);
203                 #endif
204
205                 fclose(fp);
206             }
207         }
208     }
209
210
211
212     #ifdef WIN32
213
214     /*** Getting process list ***/
215     plist = os_get_process_list();
216
217
218     /*** Windows audit check ***/
219     if (rootcheck.checks.rc_winaudit)
220     {
221         if(!rootcheck.winaudit)
222         {
223             merror("%s: No winaudit file configured.", ARGV0);
224         }
225         else
226         {
227             fp = fopen(rootcheck.winaudit, "r");
228             if(!fp)
229             {
230                 merror("%s: No winaudit file: '%s'",ARGV0,
231                                     rootcheck.winaudit);
232             }
233             else
234             {
235                 check_rc_winaudit(fp, plist);
236                 fclose(fp);
237             }
238         }
239     }
240
241     /* Windows malware */
242     if (rootcheck.checks.rc_winmalware)
243     {
244         if(!rootcheck.winmalware)
245         {
246             merror("%s: No winmalware file configured.", ARGV0);
247         }
248         else
249         {
250             fp = fopen(rootcheck.winmalware, "r");
251             if(!fp)
252             {
253                 merror("%s: No winmalware file: '%s'",ARGV0,
254                                                     rootcheck.winmalware);
255             }
256             else
257             {
258                 check_rc_winmalware(fp, plist);
259                 fclose(fp);
260             }
261         }
262     }
263
264     /* Windows Apps */
265     if (rootcheck.checks.rc_winapps)
266     {
267         if(!rootcheck.winapps)
268         {
269             merror("%s: No winapps file configured.", ARGV0);
270         }
271         else
272         {
273             fp = fopen(rootcheck.winapps, "r");
274             if(!fp)
275             {
276                 merror("%s: No winapps file: '%s'",ARGV0,
277                                                 rootcheck.winapps);
278             }
279             else
280             {
281                 check_rc_winapps(fp, plist);
282                 fclose(fp);
283             }
284         }
285     }
286
287
288     /* Freeing process list */
289     del_plist((void *)plist);
290
291
292
293     /** Checks for other non Windows. **/
294     #else
295
296
297
298     /*** Unix audit check ***/
299     if (rootcheck.checks.rc_unixaudit)
300     {
301         if(rootcheck.unixaudit)
302         {
303             /* Getting process list. */
304             plist = os_get_process_list();
305
306
307             i = 0;
308             while(rootcheck.unixaudit[i])
309             {
310                 fp = fopen(rootcheck.unixaudit[i], "r");
311                 if(!fp)
312                 {
313                     merror("%s: No unixaudit file: '%s'",ARGV0,
314                             rootcheck.unixaudit[i]);
315                 }
316                 else
317                 {
318                     /* Running unix audit. */
319                     check_rc_unixaudit(fp, plist);
320
321                     fclose(fp);
322                 }
323
324                 i++;
325             }
326
327
328             /* Freeing list */
329             del_plist((void *)plist);
330         }
331     }
332
333
334     #endif
335
336
337     /*** Third check, looking for files on the /dev ***/
338     if (rootcheck.checks.rc_dev)
339     {
340         debug1("%s: DEBUG: Going into check_rc_dev", ARGV0);
341         check_rc_dev(rootcheck.basedir);
342     }
343
344     /*** Fourth check,  scan the whole system looking for additional issues */
345     if (rootcheck.checks.rc_sys)
346     {
347         debug1("%s: DEBUG: Going into check_rc_sys", ARGV0);
348         check_rc_sys(rootcheck.basedir);
349     }
350
351     /*** Process checking ***/
352     if (rootcheck.checks.rc_pids)
353     {
354         debug1("%s: DEBUG: Going into check_rc_pids", ARGV0);
355         check_rc_pids();
356     }
357
358     /*** Check all the ports ***/
359     if (rootcheck.checks.rc_ports)
360     {
361         debug1("%s: DEBUG: Going into check_rc_ports", ARGV0);
362         check_rc_ports();
363
364         /*** Check open ports ***/
365         debug1("%s: DEBUG: Going into check_open_ports", ARGV0);
366         check_open_ports();
367     }
368
369     /*** Check interfaces ***/
370     if (rootcheck.checks.rc_if)
371     {
372         debug1("%s: DEBUG: Going into check_rc_if", ARGV0);
373         check_rc_if();
374     }
375
376
377     debug1("%s: DEBUG: Completed with all checks.", ARGV0);
378
379
380     /* Cleaning the global memory */
381     {
382         int li;
383         for(li = 0;li <= rk_sys_count; li++)
384         {
385             if(!rk_sys_file[li] ||
386                !rk_sys_name[li])
387                 break;
388
389             free(rk_sys_file[li]);
390             free(rk_sys_name[li]);
391         }
392     }
393
394     /*** Final message ***/
395     time2 = time(0);
396
397     if(rootcheck.notify != QUEUE)
398     {
399         printf("\n");
400         printf("- Scan completed in %d seconds.\n\n", (int)(time2 - time1));
401     }
402     else
403     {
404         sleep(5);
405     }
406
407
408     /* Sending scan ending message */
409     notify_rk(ALERT_POLICY_VIOLATION, "Ending rootcheck scan.");
410     if(rootcheck.notify == QUEUE)
411     {
412         merror("%s: INFO: Ending rootcheck scan.", ARGV0);
413     }
414
415
416     debug1("%s: DEBUG: Leaving run_rk_check",ARGV0);
417     return;
418 }
419
420
421 /* EOF */