source: trunk/src/samhain_hide.c@ 3

Last change on this file since 3 was 1, checked in by katerina, 19 years ago

Initial import

File size: 17.2 KB
Line 
1/***************************************************************************
2 *
3 * Purpose:
4 * -------
5 * (1) Hide files with the string MAGIC_HIDE in filename,
6 * where MAGIC_HIDE is defined below.
7 * By default, MAGIC_HIDE is defined as "samhain".
8 *
9 * (2) Hide all processes, if the executable has the string MAGIC_HIDE
10 * in its name.
11 *
12 *
13 * Configuration:
14 * -------------
15 * If not building within the samhain system, you may remove the
16 * line '#include "config.h"' and in the line
17 * '#define MAGIC_HIDE SH_MAGIC_HIDE', replace SH_MAGIC_HIDE with
18 * "someString" (in quotes !).
19 */
20
21/* #define _(string) string */
22#include "config.h"
23
24#undef _
25#define _(string) string
26
27/* define if this is a 2.6 kernel */
28/* #define LINUX26 */
29
30#define MAGIC_HIDE SH_MAGIC_HIDE
31
32/* #define MAGIC_HIDE "someString" */
33
34/* define this if you have a modversioned kernel */
35/* #define MODVERSIONS */
36
37/* the address of the sys_call_table (not exported in 2.5 kernels) */
38#define MAGIC_ADDRESS SH_SYSCALLTABLE
39
40/*
41 * Install:
42 * -------
43 * gcc -Wall -O2 -c samhain_hide.c
44 * mv samhain_hide.o /lib/modules/KERNEL_VERSION/misc/
45 *
46 * (Replace KERNEL_VERSION with your kernel's version.)
47 *
48 * Usage:
49 * -----
50 * To load the module:
51 * insmod samhain_hide (for improved safety: 'sync && insmod samhain_hide')
52 *
53 * To unload the module
54 * rmmod samhain_hide (for improved safety: 'sync && rmmod samhain_hide')
55 *
56 *
57 * Details:
58 * -------
59 * The following kernel syscalls are replaced:
60 * sys_getdents [hide files/directories/processes (/proc/PID)]
61 *
62 * Tested on:
63 * ---------
64 * Linux 2.2, 2.4, 2.6
65 *
66 * Copyright:
67 * ---------
68 * Copyright (C) 2001, 2002 Rainer Wichmann (http://la-samhna.de)
69 *
70 * License:
71 * -------
72 * This program is free software; you can redistribute it and/or modify
73 * it under the terms of the GNU General Public License, version 2, as
74 * published by the Free Software Foundation.
75 *
76 * This program is distributed in the hope that it will be useful,
77 * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 * GNU General Public License for more details.
80 *
81 * You should have received a copy of the GNU General Public License
82 * along with this program; if not, write to the Free Software
83 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
84 *
85 ***************************************************************************/
86
87
88
89/*****************************************************
90 *
91 * The defines:
92 *
93 *****************************************************/
94
95/* This is a Linux Loadable Kernel Module.
96 */
97
98#ifndef LINUX26
99#define __KERNEL__
100#define MODULE
101#endif
102#define LINUX
103
104/* Define for debugging.
105 */
106/* #define HIDE_DEBUG */ /* query_module */
107/* #define FILE_DEBUG */ /* getdents */
108/* #define READ_DEBUG */ /* read */
109/* #define PROC_DEBUG */ /* procfs */
110
111
112/*****************************************************
113 *
114 * The include files:
115 *
116 *****************************************************/
117
118
119/* The configure options (#defines) for the Kernel
120 */
121#include <linux/config.h>
122
123#ifndef LINUX26
124#ifdef CONFIG_MODVERSIONS
125#include <linux/modversions.h>
126#endif
127#endif
128
129
130#ifdef LINUX26
131#include <linux/init.h>
132#endif
133
134#include <linux/module.h>
135
136/* File tables structures. If directory caching is used,
137 * <linux/dcache.h> will be included here, and __LINUX_DCACHE_H
138 * will thus be defined.
139 */
140#include <linux/fs.h>
141#include <linux/proc_fs.h>
142
143/* Include the SYS_syscall defines.
144 */
145#ifndef LINUX26
146#include <sys/syscall.h>
147#else
148#define SYS_getdents 141
149#define SYS_getdents64 220
150#endif
151
152
153/* Includes for 'getdents' per the manpage.
154 */
155#include <linux/types.h>
156#include <linux/dirent.h>
157#include <linux/unistd.h>
158
159/* To access userspace memory.
160 */
161#include <asm/uaccess.h>
162
163/* Include for lock_kernel().
164 */
165#include <linux/smp_lock.h>
166
167/* Include for fget().
168 */
169#include <linux/file.h>
170
171/*****************************************************
172 *
173 * The global variables:
174 *
175 *****************************************************/
176
177/* The kernel syscall table. Not exported anymore in 2.5 ff., and also
178 * not in the RedHat 2.4 kernel.
179 */
180
181#if 0
182extern void * sys_call_table[];
183#define sh_sys_call_table sys_call_table
184#endif
185
186unsigned long * sh_sys_call_table = (unsigned long *) MAGIC_ADDRESS;
187
188/* The old address of the sys_getdents syscall.
189 */
190int (*old_getdents)(unsigned int, struct dirent *, unsigned int);
191#ifdef __NR_getdents64
192long (*old_getdents64)(unsigned int, struct dirent64 *, unsigned int);
193#endif
194
195char hidden[] = MAGIC_HIDE;
196
197
198/*****************************************************
199 *
200 * The functions:
201 *
202 *****************************************************/
203
204
205MODULE_AUTHOR("Rainer Wichmann");
206MODULE_DESCRIPTION("Hide files/processes/modules with MAGIC_HIDE in name.");
207#if defined(MODULE_LICENSE) || defined(LINUX26)
208MODULE_LICENSE("GPL");
209#endif
210
211#ifdef LINUX26
212/* Default is to hide ourselves.
213 */
214static int removeme = 1;
215
216MODULE_PARM (removeme, "i");
217#endif
218
219
220/*
221 * struct task_struct is defined in linux/sched.h
222 *
223 * as of 2.4.20, the vanilla kernel holds (among others):
224 * struct task_struct *next_task, *prev_task;
225 *
226 * Redhat kernel seems to have a different scheduler.
227 * use:
228 * struct task_struct * find_task_by_pid (int pid);
229 */
230
231#if defined(SH_VANILLA_KERNEL) && !defined(LINUX26)
232/*
233 * Fetch the task struct for a given PID.
234 */
235struct task_struct * fetch_task_struct (int pid)
236{
237 struct task_struct * task_ptr;
238
239#ifdef PROC_DEBUG
240 printk("FETCH TASK %d\n", pid);
241#endif
242
243 task_ptr = current;
244
245 do
246 {
247 if (task_ptr->pid == (pid_t) pid )
248 return (task_ptr);
249 task_ptr = task_ptr->next_task;
250 }
251 while (task_ptr != current);
252
253#ifdef PROC_DEBUG
254 printk("FETCH TASK: NOT FOUND !!!\n");
255#endif
256
257 return (NULL);
258}
259
260#else
261/*
262 * RedHat 2.4.20 kernel
263 */
264struct task_struct * fetch_task_struct (int pid)
265{
266 struct task_struct * task_ptr = NULL;
267 task_ptr = find_task_by_pid (pid);
268 return (task_ptr);
269}
270#endif
271
272/* Convert a string to an int.
273 * Does not recognize integers with a sign (+/-) in front.
274 */
275int my_atoi(char * in_str)
276{
277 int i = 0;
278 int retval = 0;
279 int conv = 0;
280
281 if (in_str == NULL)
282 return (-1);
283
284 while(in_str[i] != '\0')
285 {
286 /* Break if not numeric.
287 */
288 if (in_str[i] < '0' || in_str[i] > '9')
289 break;
290
291 ++conv;
292
293 /* Leading zeroes (should not happen in /proc)
294 */
295 if (retval == 0 && in_str[i] == '0')
296 retval = retval;
297 else
298 retval = retval * 10;
299
300 retval = retval + (in_str[i] - '0');
301
302 i++;
303 }
304
305 if (conv == 0)
306 return (-1);
307 else
308 return (retval);
309}
310
311/* Purpose:
312 *
313 * Hide all files/dirs that include the string MAGIC_HIDE in their
314 * name.
315 */
316int new_getdents (unsigned int fd, struct dirent *dirp, unsigned int count)
317{
318 int status = 0; /* Return value from original getdents */
319 struct inode * dir_inode;
320 int dir_is_proc = 0;
321
322 struct dirent * dirp_prev;
323 struct dirent * dirp_new;
324 struct dirent * dirp_current;
325
326 int dir_table_bytes;
327 int forward_bytes;
328 struct task_struct * task_ptr;
329 int hide_it = 0;
330 long dirp_offset;
331
332 lock_kernel();
333
334 status = (*old_getdents)(fd, dirp, count);
335
336#ifdef FILE_DEBUG
337 printk("STATUS %d\n", status);
338#endif
339
340 /* 0: end of directory.
341 * -1: some error
342 */
343 if (status <= 0)
344 {
345 unlock_kernel();
346 return (status);
347 }
348
349 /* Handle directory caching. dir_inode is the inode of the directory.
350 */
351#if defined(__LINUX_DCACHE_H)
352 dir_inode = current->files->fd[fd]->f_dentry->d_inode;
353#else
354 dir_inode = current->files->fd[fd]->f_inode;
355#endif
356
357 /* Check for the /proc directory
358 */
359 if (dir_inode->i_ino == PROC_ROOT_INO
360#ifndef LINUX26
361 && !MAJOR(dir_inode->i_dev) &&
362 MINOR(dir_inode->i_dev) == 1
363#endif
364 )
365 dir_is_proc = 1;
366
367 /* Allocate space for new dirent table. Can't use GFP_KERNEL
368 * (kernel oops)
369 */
370 dirp_new = (struct dirent *) kmalloc (status, GFP_ATOMIC);
371
372 if (dirp_new == NULL)
373 {
374 unlock_kernel();
375 return (status);
376 }
377
378 /* Copy the dirp table to kernel space.
379 */
380 copy_from_user(dirp_new, dirp, status);
381
382#ifdef FILE_DEBUG
383 printk("COPY to kernel\n");
384#endif
385
386 /* Loop over the dirp table to find entries to hide.
387 */
388 dir_table_bytes = status;
389 dirp_current = dirp_new;
390 dirp_prev = NULL;
391
392 while (dir_table_bytes > 0)
393 {
394 hide_it = 0;
395
396 if (dirp_current->d_reclen == 0)
397 break;
398
399 dirp_offset = dirp_current->d_off;
400
401#ifdef FILE_DEBUG
402 printk("DIRENT %d %d %ld\n",
403 dir_table_bytes,
404 dirp_current->d_reclen,
405 dirp_current->d_off);
406#endif
407
408 dir_table_bytes -= dirp_current->d_reclen;
409 forward_bytes = dirp_current->d_reclen;
410
411#ifdef FILE_DEBUG
412 printk("ENTRY %s\n", dirp_current->d_name);
413#endif
414
415 /* If /proc is scanned (e.g. by 'ps'), hide the entry for
416 * any process where the executable has MAGIC_HIDE in its name.
417 */
418 if (dir_is_proc == 1)
419 {
420 task_ptr = fetch_task_struct(my_atoi(dirp_current->d_name));
421 if (task_ptr != NULL)
422 {
423 if (strstr(task_ptr->comm, hidden) != NULL)
424 hide_it = 1;
425 }
426 }
427 /* If it is a regular directory, hide any entry with
428 * MAGIC_HIDE in its name.
429 */
430 else
431 {
432 if (strstr (dirp_current->d_name, hidden) != NULL)
433 hide_it = 1;
434 }
435
436 if (hide_it == 1)
437 {
438#ifdef FILE_DEBUG
439 printk(" -->HIDDEN %s\n", dirp_current->d_name);
440#endif
441 if (dir_table_bytes > 0)
442 {
443 status -= dirp_current->d_reclen;
444 memmove (dirp_current,
445 (char *) dirp_current + dirp_current->d_reclen,
446 dir_table_bytes);
447
448 /* Set forward_bytes to 0, because now dirp_current is the
449 * (previously) next entry in the dirp table.
450 */
451 forward_bytes = 0;
452 dirp_prev = dirp_current;
453 }
454 else
455 {
456 status -= dirp_current->d_reclen;
457 if (dirp_prev != NULL)
458 dirp_prev->d_off = dirp_offset;
459 }
460
461 }
462 else
463 {
464 dirp_prev = dirp_current;
465 if (dir_table_bytes == 0 && dirp_prev != NULL)
466 dirp_prev->d_off = dirp_offset;
467 }
468
469 /* Next entry in dirp table.
470 */
471 if (dir_table_bytes > 0)
472 dirp_current = (struct dirent *) ( (char *) dirp_current +
473 forward_bytes);
474 }
475
476 /* Copy our modified dirp table back to user space.
477 */
478 copy_to_user(dirp, dirp_new, status);
479#ifdef FILE_DEBUG
480 printk("COPY to user\n");
481#endif
482
483 kfree (dirp_new);
484#ifdef FILE_DEBUG
485 printk("KFREE\n");
486#endif
487
488 unlock_kernel();
489 return (status);
490}
491
492
493/* For 2.4 kernel
494 */
495#ifdef __NR_getdents64
496long new_getdents64 (unsigned int fd, struct dirent64 *dirp,
497 unsigned int count)
498{
499 long status = 0; /* Return value from original getdents */
500 struct inode * dir_inode;
501 int dir_is_proc = 0;
502
503 struct dirent64 * dirp_prev;
504 struct dirent64 * dirp_new;
505 struct dirent64 * dirp_current;
506
507 int dir_table_bytes;
508 int forward_bytes;
509 struct task_struct * task_ptr;
510 int hide_it = 0;
511 __s64 dirp_offset;
512
513 lock_kernel();
514
515 status = (*old_getdents64)(fd, dirp, count);
516
517#ifdef FILE_DEBUG
518 printk("STATUS64 %ld\n", status);
519#endif
520
521 /* 0: end of directory.
522 * -1: some error
523 */
524 if (status <= 0)
525 {
526 unlock_kernel();
527 return (status);
528 }
529
530 /* Handle directory caching. dir_inode is the inode of the directory.
531 */
532#if defined(__LINUX_DCACHE_H)
533 dir_inode = current->files->fd[fd]->f_dentry->d_inode;
534#else
535 dir_inode = current->files->fd[fd]->f_inode;
536#endif
537
538#ifdef FILE_DEBUG
539 printk("INODE64\n");
540#endif
541
542 /* Check for the /proc directory
543 */
544 if (dir_inode->i_ino == PROC_ROOT_INO
545#ifndef LINUX26
546 && !MAJOR(dir_inode->i_dev) /* &&
547 MINOR(dir_inode->i_dev) == 1 */
548 /* MINOR commented out because of problems with 2.4.17 */
549#endif
550 )
551 {
552 dir_is_proc = 1;
553
554#ifdef PROC_DEBUG
555 printk("PROC_CHECK64\n");
556#endif
557 }
558
559 /* Allocate space for new dirent table. Can't use GFP_KERNEL
560 * (kernel oops)
561 */
562 dirp_new = kmalloc ((size_t)status, GFP_ATOMIC);
563
564#ifdef FILE_DEBUG
565 printk("KMALLOC64_0\n");
566#endif
567
568 if (dirp_new == NULL)
569 {
570 unlock_kernel();
571 return (status);
572 }
573
574#ifdef FILE_DEBUG
575 printk("KMALLOC64\n");
576#endif
577
578 /* Copy the dirp table to kernel space.
579 */
580 copy_from_user(dirp_new, dirp, status);
581
582#ifdef FILE_DEBUG
583 printk("COPY64 to kernel\n");
584#endif
585
586 /* Loop over the dirp table to find entries to hide.
587 */
588 dir_table_bytes = status;
589 dirp_current = dirp_new;
590 dirp_prev = NULL;
591
592 while (dir_table_bytes > 0)
593 {
594 hide_it = 0;
595
596 if (dirp_current->d_reclen == 0)
597 break;
598
599 dirp_offset = dirp_current->d_off;
600
601#ifdef FILE_DEBUG
602 printk("DIRENT %d %d %lld\n",
603 dir_table_bytes,
604 dirp_current->d_reclen,
605 dirp_current->d_off);
606#endif
607
608 dir_table_bytes -= dirp_current->d_reclen;
609 forward_bytes = dirp_current->d_reclen;
610
611#ifdef FILE_DEBUG
612 printk("ENTRY %s\n", dirp_current->d_name);
613#endif
614
615 /* If /proc is scanned (e.g. by 'ps'), hide the entry for
616 * any process where the executable has MAGIC_HIDE in its name.
617 */
618 if (dir_is_proc == 1)
619 {
620#ifdef PROC_DEBUG
621 printk("PROC %s\n", dirp_current->d_name);
622#endif
623 task_ptr = fetch_task_struct(my_atoi(dirp_current->d_name));
624 if (task_ptr != NULL)
625 {
626#ifdef PROC_DEBUG
627 printk("PROC %s <> %s\n", task_ptr->comm, hidden);
628#endif
629 if (strstr(task_ptr->comm, hidden) != NULL)
630 hide_it = 1;
631 }
632 }
633 /* If it is a regular directory, hide any entry with
634 * MAGIC_HIDE in its name.
635 */
636 else
637 {
638 if (strstr (dirp_current->d_name, hidden) != NULL)
639 hide_it = 1;
640 }
641
642 if (hide_it == 1)
643 {
644#ifdef FILE_DEBUG
645 printk(" -->HIDDEN %s\n", dirp_current->d_name);
646#endif
647 if (dir_table_bytes > 0)
648 {
649 status -= dirp_current->d_reclen;
650 memmove (dirp_current,
651 (char *) dirp_current + dirp_current->d_reclen,
652 dir_table_bytes);
653
654 /* Set forward_bytes to 0, because now dirp_current is the
655 * (previously) next entry in the dirp table.
656 */
657 forward_bytes = 0;
658 dirp_prev = dirp_current;
659 }
660 else
661 {
662 status -= dirp_current->d_reclen;
663 if (dirp_prev != NULL)
664 dirp_prev->d_off = dirp_offset;
665 }
666
667 }
668 else
669 {
670 dirp_prev = dirp_current;
671 if (dir_table_bytes == 0 && dirp_prev != NULL)
672 dirp_prev->d_off = dirp_offset;
673 }
674
675 /* Next entry in dirp table.
676 */
677 if (dir_table_bytes > 0)
678 dirp_current = (struct dirent64 *) ( (char *) dirp_current +
679 forward_bytes);
680 }
681
682 /* Copy our modified dirp table back to user space.
683 */
684 copy_to_user(dirp, dirp_new, status);
685 kfree (dirp_new);
686 unlock_kernel();
687 return (status);
688}
689#endif
690
691#ifdef LINUX26
692static struct module *find_module(const char *name)
693{
694 struct module *mod;
695 struct list_head * modules = (struct list_head *) SH_LIST_MODULES;
696
697 list_for_each_entry(mod, modules, list) {
698 if (strcmp(mod->name, name) == 0)
699 return mod;
700 }
701 return NULL;
702}
703#endif
704
705/* The initialisation function. Automatically called when module is inserted
706 * via the 'insmod' command.
707 */
708#ifdef LINUX26
709static int __init samhain_hide_init(void)
710#else
711int init_module(void)
712#endif
713{
714
715 lock_kernel();
716
717 /* Unfortunately this does not fully prevent the module from appearing
718 * in /proc/ksyms.
719 */
720#ifndef LINUX26
721 EXPORT_NO_SYMBOLS;
722#endif
723
724 /* Replace the 'sys_getdents' syscall with the new version.
725 */
726 old_getdents = (void*) sh_sys_call_table[SYS_getdents];
727 sh_sys_call_table[SYS_getdents] = (unsigned long) new_getdents;
728
729#ifdef __NR_getdents64
730 old_getdents64 = (void*) sh_sys_call_table[SYS_getdents64];
731 sh_sys_call_table[SYS_getdents64] = (unsigned long) new_getdents64;
732#endif
733
734#ifdef LINUX26
735 {
736 spinlock_t * modlist_lock = (spinlock_t * ) SH_MODLIST_LOCK;
737 struct module *mod = find_module(SH_INSTALL_NAME"_hide");
738 if (mod) {
739 /* Delete from various lists */
740 spin_lock_irq(modlist_lock);
741 if (removeme == 1)
742 {
743 list_del(&mod->list);
744 }
745 spin_unlock_irq(modlist_lock);
746 }
747 }
748#endif
749
750 unlock_kernel();
751 return (0);
752}
753
754/* The cleanup function. Automatically called when module is removed
755 * via the 'rmmod' command.
756 */
757#ifdef LINUX26
758static void __exit samhain_hide_cleanup(void)
759#else
760void cleanup_module(void)
761#endif
762{
763 lock_kernel();
764
765 /* Restore the new syscalls to the original version.
766 */
767 sh_sys_call_table[SYS_getdents] = (unsigned long) old_getdents;
768#ifdef __NR_getdents64
769 sh_sys_call_table[SYS_getdents64] = (unsigned long) old_getdents64;
770#endif
771
772 unlock_kernel();
773}
774
775#ifdef LINUX26
776module_init(samhain_hide_init);
777module_exit(samhain_hide_cleanup);
778#endif
779
780
Note: See TracBrowser for help on using the repository browser.