forked from terrylao/WinCat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinCat.cc
405 lines (369 loc) · 13.8 KB
/
WinCat.cc
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
/*
publish with BSD Licence.
Copyright (c) Terry Lao
*/
#include "wincat.h"
#include "pipes.h"
#include "options.h"
#include "otp.h"
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#define DEBUG 1
WinCat::WinCat(char *filename) {
this->filename = NULL;
if (filename != NULL) {
this->filename = (char *) malloc((strlen(filename) + 1) * sizeof(char));
strcpy(this->filename, filename);
}
this->launched = 0;
udpsocket = 0;
}
void WinCat::setUdp(char * restrictip) {
udpsocket =1;
myrestrictip=restrictip;
}
WinCat::~WinCat() {
if (pipes.process_spawned) close_pipes(pipes);
free(filename);
if (input) delete input;
_setmode(fileno(stdout), _O_TEXT);
}
int WinCat::SyncProcess(SOCKET ClientSocket) {
STARTUPINFO sinfo;
PROCESS_INFORMATION pinfo;
SECURITY_ATTRIBUTES sattr;
HANDLE readfh, writefh;
// Initialize the STARTUPINFO struct
ZeroMemory(&sinfo, sizeof(STARTUPINFO));
sinfo.cb = sizeof(STARTUPINFO);
sinfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
// Uncomment this if you want to hide the other app's
// DOS window while it runs
// sinfo.wShowWindow = SW_HIDE;
sinfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
sinfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
sinfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
// Initialize security attributes to allow the launched app to
// inherit the caller's STDOUT, STDIN, and STDERR
DWORD dwRead, dwWritten,dwAvail;
char chBuf[DEFAULT_BUFLEN];
char cmd[DEFAULT_BUFLEN];
int iResult;
sattr.nLength = sizeof(SECURITY_ATTRIBUTES);
sattr.lpSecurityDescriptor = 0;
sattr.bInheritHandle = TRUE;
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
if (DEBUG) fprintf(stderr, "Wincat: enter sync processing.\n");
if (udpsocket==1){
iResult = recvfrom(ClientSocket, chBuf, DEFAULT_BUFLEN,0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
}else{
iResult = recv(ClientSocket, chBuf, DEFAULT_BUFLEN, 0);
}
if (DEBUG) fprintf(stderr, "Wincat: recv %s.\n",chBuf);
if ( iResult <= 0 ) {
printverbose();
if (DEBUG) fprintf(stderr, "Wincat: Connection closed on other end.\n");
return -1;
}
if (udpsocket==1){
getpeername(ClientSocket, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
char *ip =inet_ntoa(SenderAddr.sin_addr);
if (myrestrictip!=NULL){
if (strcmp(myrestrictip,ip)==0){
}else{
if (verbose) {
printverbose();
fprintf(stderr, "ban connect from: %s\n", ip);
}
closesocket(ClientSocket);
return -3;
}
}else{
if (verbose) {
printverbose();
fprintf(stderr, "connect from: %s\n", ip);
}
}
}
if (memchr(chBuf,'\n',iResult)==NULL){
return -2;
}
//One Time Pass verification
//char ipt1[41],ipt2[41],ipt3[41];
char userinput[7];
//memset(&ipt1[0],0,41);
//memset(&ipt2[0],0,41);
//memset(&ipt3[0],0,41);
memset(&userinput,0,7);
//genOTP(&ipt1[0],&ipt2[0],&ipt3[0]);
//memcpy(userinput,ipt1,6);
//printf("OTP:%s\n",userinput);
//printf("checkOTP:%d\n",checkOTP(userinput));
memcpy(userinput,chBuf,6);
if (checkOTP(userinput)==0){
fprintf(stderr, "Wincat: checkOTP Error");
closesocket(ClientSocket);
return -3;
}
memset(cmd,0,sizeof(cmd));
memcpy(cmd,this->filename,strlen(this->filename));
//if (DEBUG) fprintf(stderr, "Wincat: show %s.\n",&chBuf[6]);
//if (DEBUG) fprintf(stderr, "Wincat: cmd %s.\n",cmd);
//if (DEBUG) fprintf(stderr, "Wincat: iResult: %d.\n",iResult);
//chBuf[7] because of one time pass is 6 bytes, the 7th is empty space
memcpy(&cmd[strlen(this->filename)],&chBuf[6],iResult-7);
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: execute:%s\n",cmd);
}
// Get a pipe from which we read
// output from the launched app
if (!CreatePipe(&readfh, &sinfo.hStdOutput, &sattr, 0))
{
// Error opening the pipe
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: Error opening the pipe\n");
}
closesocket(ClientSocket);
return -1;
}
HANDLE CallerToken= NULL;
if ( !OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS , &CallerToken) ) {
printverbose();
fprintf(stderr, "OpenProcessToken failed - 0x%08x\n", GetLastError());
closesocket(ClientSocket);
return -1;
}
// Launch the app. We should return immediately (while the app is running)
//if (!CreateProcess(0, cmd, 0, 0, TRUE, 0, 0, 0, &sinfo, &pinfo))
if (!CreateProcessAsUser(CallerToken,0, cmd, 0, 0, TRUE, 0, 0, 0, &sinfo, &pinfo))
{
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: Error CreateProcess\n");
}
CloseHandle(sinfo.hStdInput);
//CloseHandle(writefh);
CloseHandle(readfh);
CloseHandle(sinfo.hStdOutput);
closesocket(ClientSocket);
return -1;
}
// Don't need the read access to these pipes
CloseHandle(sinfo.hStdInput);
CloseHandle(sinfo.hStdOutput);
// We haven't yet read app's output
sinfo.dwFlags = 0;
// Input and/or output still needs to be done?
while (readfh)
{
// Capture more output of the app?
// Read in upto DEFAULT_BUFLEN bytes
if (!ReadFile(readfh, chBuf, DEFAULT_BUFLEN, &dwRead, 0))
{
// If we aborted for any reason other than that the
// app has closed that pipe, it's an
// error. Otherwise, the program has finished its
// output apparently
if (GetLastError() != ERROR_BROKEN_PIPE && dwRead)
{
// An error reading the pipe
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: An error reading the pipe: %d\n",GetLastError());
}
}
break;
}
if (udpsocket==1){
iResult = sendto(ClientSocket, chBuf, dwRead, 0, (struct sockaddr*) &SenderAddr, SenderAddrSize);
}else{
iResult = send(ClientSocket, chBuf, dwRead, 0);
}
if ( iResult == 0 ) {
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: Connection closed on other end.\n");
}
break;
} else if (iResult < 0 ) {
if (DEBUG) {
printverbose();
fprintf(stderr, "Wincat: Send() failed with error: %d\n", WSAGetLastError());
}
break;
}
}
// Close input pipe if it's still open
//if (writefh) CloseHandle(writefh);
// Close output pipe
if (readfh) CloseHandle(readfh);
closesocket(ClientSocket);
// Return the output
return 0;
}
int WinCat::Launch() {
int reader = TEXT_READER;
if (this->filename == NULL) {
if (!_isatty(fileno(stdin)) ) {
reader = BINARY_READER;
}
/* Create thread for non-blocking stdin reader */
AsyncStreamReader *input = new AsyncStreamReader(stdin, reader);
this->input = input;
/* Enable VT100 and similar control character sequences
that control cursor movement, color/font mode,
and other operations */
//SetConsoleMode( GetStdHandle(STD_OUTPUT_HANDLE),
// ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
fprintf(stderr,"lauch no filename");
this->output = stdout;
} else {
/* Create a new process */
this->pipes = get_pipes(this->filename);
if (!pipes.process_spawned) {
return 1;
}
int in_fd = _open_osfhandle((intptr_t)this->pipes.Child_Std_OUT_Rd, _O_BINARY | _O_RDONLY);
int out_fd = _open_osfhandle((intptr_t)this->pipes.Child_Std_IN_Wr, _O_BINARY | _O_APPEND);
FILE *in = _fdopen(in_fd, "rb");
FILE *out = _fdopen(out_fd, "wb");
/* Create thread for non-blocking stream reader */
AsyncStreamReader *input = new AsyncStreamReader(in, reader);
this->input = input;
this->output = out;
}
_setmode(fileno(stdout), _O_BINARY);
this->launched = 1;
return 0;
}
int WinCat::Process(SOCKET ClientSocket) {
if (!this->launched) {
if(Launch()) return 1;
}
int iResult;
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
int err;
char msgbuf [256]; // for a message up to 255 bytes.
msgbuf [0] = '\0'; // Microsoft doesn't guarantee this on man page.
char sendbuf[DEFAULT_BUFLEN];
char recvbuf[DEFAULT_BUFLEN];
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
memset(recvbuf, '\0', DEFAULT_BUFLEN);
memset(sendbuf, '\0', DEFAULT_BUFLEN);
/* Main run-forever loop: */
for(;;) {
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(ClientSocket, &readfds);
FD_SET(ClientSocket, &writefds);
FD_SET(ClientSocket, &exceptfds);
int rs = select(1, &readfds, &writefds, &exceptfds, NULL);
if (rs == SOCKET_ERROR) {
err = WSAGetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (DEBUG) {
fprintf(stderr, "Wincat: Select() failed with error: %d,%s\n", err,msgbuf);
}
break;
}
if (FD_ISSET(ClientSocket, &readfds)) {
/* SOCKET IS READY FOR READING */
if (udpsocket==1){
iResult = recvfrom(ClientSocket, recvbuf, DEFAULT_BUFLEN,0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
}else{
iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0);
}
if ( iResult > 0 ) {
if (udpsocket==1){
getpeername(ClientSocket, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
char *ip =inet_ntoa(SenderAddr.sin_addr);
if (myrestrictip!=NULL){
if (strcmp(myrestrictip,ip)==0){
}else{
if (verbose) {
printverbose();
fprintf(stderr, "ban connect from: %s\n", ip);
}
closesocket(ClientSocket);
return -3;
}
}else{
if (verbose) {
printverbose();
fprintf(stderr, "connect from: %s\n", ip);
}
}
}
if (verbose) {
printverbose();
fprintf(stderr, "recv: %s\n",recvbuf);
}
fwrite(recvbuf, 1, iResult, output);
fflush(output);
} else if ( iResult == 0 ) {
if (DEBUG) fprintf(stderr, "Wincat: Connection closed on other end.\n");
break;
} else {
err = WSAGetLastError();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (DEBUG) {
fprintf(stderr, "Wincat: Recv() failed with error: %d.%s\n", err,msgbuf);
}
break;
}
memset(recvbuf, '\0', DEFAULT_BUFLEN);
} else if (FD_ISSET(ClientSocket, &writefds)) {
/* SOCKET IS READY FOR WRITING */
int bytes_read = this->input->Read(sendbuf);
if (bytes_read > 0) {
if (udpsocket==1){
iResult = sendto(ClientSocket, sendbuf, bytes_read, 0, (struct sockaddr*) &SenderAddr, SenderAddrSize);
}else{
iResult = send(ClientSocket, sendbuf, bytes_read, 0);
}
if ( iResult == 0 ) {
if (DEBUG) fprintf(stderr, "Wincat: Connection closed on other end.\n");
break;
} else if (iResult < 0 ) {
if (DEBUG) fprintf(stderr, "Wincat: Send() failed with error: %d\n", WSAGetLastError());
break;
}
if (verbose) {
printverbose();
fprintf(stderr, "Wincat: Send(): %s", sendbuf);
}
} else if (bytes_read < 0) {
if (DEBUG) fprintf(stderr, "Wincat: Input reading complete.\n");
break;
}
memset(sendbuf, '\0', DEFAULT_BUFLEN);
} else if (FD_ISSET(ClientSocket, &exceptfds)) {
/* SOCKET EXCEPTION */
if (DEBUG) fprintf(stderr, "Wincat: Socket exception.\n");
break;
}
/* Free up some CPU time for the OS */
Sleep(1);
}
return 0;
}