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