-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunz.c
621 lines (538 loc) · 15.6 KB
/
funz.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
/*____________________________________________*\
* *
* *
* -= vida =- *
* Visual Interactive Datapipe *
* no (c) 2002/03-2003/05 *
* v.0.8.0 *
* *
* coded by *
* embyte -> embyte@madlab.it *
* lesion -> lesion@autisti.org *
* *
* less README for features and more info *
* *
* -------------------------------------- *
* *
* funz.c: *
* -some functions *
* *
\*____________________________________________*/
#include "include.h"
// print in the left corner the argument
// if sl=1 print, sleep, clean
void info(char sl, const char *s, ...)
{
char complete[128];
u_short space = strlen (VERSION)+10;
va_list ap;
va_start(ap, s);
vsprintf(complete, s, ap);
mvaddstr(0, space, "\n");
mvaddstr(0, space, complete);
refresh();
va_end(ap);
if (sl)
{
sleep(1);
mvaddstr(0, space, "\n");
}
}
// create a new datapipe
// stay always in accept and pass at handler() the new connection
// we can't stop this with pthread_cancel :( (FIX IT!!)
void datapipe()
{
char s[128];
struct hostent *host_addr;
int i = ndp;
struct sockaddr_in sin1;
// struct sockaddr_in sin2;
int sd;
int addlen = sizeof(struct sockaddr_in);
// no connection still
dp[i].cn = 0;
// print this message
sprintf(s, "Creating a datapipe throught port %i and %s:%i",
dp[i].localport, dp[i].host, dp[i].remoteport);
info(0, s);
// try to resolve host
if (!(host_addr = gethostbyname(dp[i].host)))
{
sprintf(s, "Cannot resolve %s", dp[i].host);
info(1, s);
return;
}
// create the listen socket
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
sprintf(s, "Error in socket(): %s", strerror(errno));
info(1, s);
return;
}
memset(&sin1, 0, sizeof(sin1));
sin1.sin_port = htons(dp[i].localport);
sin1.sin_family = AF_INET;
// bind and put in listen mode the socket
if (bind(sd, (struct sockaddr *) &sin1, sizeof(sin1)))
{
sprintf(s, "Error in bind(): %s", strerror(errno));
info(1, s);
return;
}
listen(sd, 20);
// riempio la struttura di uscita con i dati del vettore datapipe
dp[i].sout.sin_port = htons(dp[i].remoteport);
dp[i].sout.sin_family = AF_INET;
memset(&(dp[i].sout.sin_zero), '\0', (size_t) 8);
memcpy(&(dp[i].sout.sin_addr), host_addr->h_addr,
(size_t) sizeof(host_addr->h_length));
// put the new datapipe in datapipe list
mvwprintw(wlist, i + 2, 4, "%d -> %s:%d (0) %c", dp[i].localport,
dp[i].host, dp[i].remoteport, CHECKPWD(i) ? 'P' : ' ');
wrefresh(wlist);
info(1, "Datapipe activate");
// increase datapipe number
ndp++;
dp[i].stat = 1;
memset(&sin1, 0, sizeof(sin1));
// fprintf(stderr,"Metto in accept dp[%d].sd ke vale %d\n",i,dp[i].sd);
// accept cicle (the thread block here)
while ((dp[i].from_sd[dp[i].cn] =
accept(sd, (struct sockaddr *) &sin1, &addlen)) != -1)
{
// fprintf(stderr,"Dentro accept, dp[%d].sd vale %d\n",i,dp[i].sd);
//control that this datapipe wasn't deleted
if (dp[i].stat == 2)
{
shutdown(dp[i].from_sd[dp[i].cn], 2);
pthread_exit(NULL);
}
info(0, "Connection from %s",
inet_ntoa((struct in_addr) sin1.sin_addr));
// add this connection to datapipe
strcpy(s, (char *) inet_ntoa((struct in_addr) sin1.sin_addr));
dp[i].ch[dp[i].cn] = (char *) malloc(strlen(s) + 1);
strcpy(dp[i].ch[dp[i].cn], s);
dp[i].cp[dp[i].cn] = ntohs(sin1.sin_port);
// connection list window is open
if (win == 2)
{
mvwprintw(wlist, dp[i].cn + 3, 6, "%s:%d (0/0)",
dp[i].ch[dp[i].cn], dp[i].cp[dp[i].cn]);
wrefresh(wlist);
}
// datapipe list window is open
else
{
if (dpos == i && win == 1)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, i + 2, 4, "%d -> %s:%d (%d) %c",
dp[i].localport, dp[i].host, dp[i].remoteport,
dp[i].cn + 1, CHECKPWD(i) ? 'P' : ' ');
wattrset(wlist, A_NORMAL);
}
else
mvwprintw(wlist, i + 2, 4, s);
wrefresh(wlist);
}
// increase the number of connection for this datapipe
dp[i].cn++;
// pass the new connection to handler()
pthread_create(&h, NULL, (void *) handler, &i);
}
// fprintf(stderr,"Fuori accept, dp[%d].sd vale %d\n",i,dp[i].sd);
info(1, "Datapipe closed!!");
return;
}
void
// gli passo il numero di datapipe
handler(int *io)
{
// buffer
char recvline[1028];
// password receive form client
char password[32];
// number of received byte
int n;
// required for log
ushort l = 0;
int inf;
// to control the two fd
fd_set rfds;
fd_set efds;
// il valore n della funct select()
int max_sd = 0;
char s[128];
// numero datapipe
int i = *io;
// numero connessione
int c = dp[i].cn - 1;
// azzera il numero di byte trasmessi/ricevuti
dp[i].tb[c] = dp[i].rb[c] = 0;
// azzera lo sniffing mode
dp[i].status[c] = -1;
// no log at now
dp[i].logfile[c] = 0;
// if psw is needed check for it (we must check only here and not in
// datapipe(), if you want how emailme)
if (CHECKPWD(i))
{
sprintf(s, "A password is required : ");
write(dp[i].from_sd[c], s, strlen(s));
n = read(dp[i].from_sd[c], password, 32);
password[n - 2] = '\0';
if (strcmp(password, dp[i].pwd))
{
// non sono uguali -> jump to the end (correct goto :^)
sprintf (s, "Wrong password.\n");
write(dp[i].from_sd[c], s, strlen(s));
shutdown(dp[i].from_sd[c], 2);
goto closing;
}
else
{
sprintf (s, "OK waiting the connection.. :-)\n");
write(dp[i].from_sd[c], s, strlen(s));
}
}
// creo il socket descriptor di destinazione
if ((dp[i].dest_sd[c] = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
shutdown(dp[i].from_sd[c], 2);
info(1, "Error in socket(): %s", strerror(errno));
return;
}
// provo a connettermi
inf =
connect(dp[i].dest_sd[c], (struct sockaddr *) &dp[i].sout,
(socklen_t) sizeof(dp[i].sout));
if (inf == -1)
{
shutdown(dp[i].from_sd[c], 2);
info(1, "Error in connect(): %s", strerror(errno));
return;
}
info(1, "Connection estabilished");
// imposto il max_sd per la select()
if (dp[i].from_sd[c] > max_sd)
max_sd = dp[i].from_sd[c];
if (dp[i].dest_sd[c] > max_sd)
max_sd = dp[i].dest_sd[c];
max_sd++;
// ciclo di trasmissione dati
while (1)
{
// azzero i fd_set
FD_ZERO(&rfds);
FD_ZERO(&efds);
// lego i due fd ai due capi delle connessioni
FD_SET(dp[i].from_sd[c], &rfds);
FD_SET(dp[i].dest_sd[c], &rfds);
FD_SET(dp[i].from_sd[c], &efds);
FD_SET(dp[i].dest_sd[c], &efds);
// è necessario per il logging se no devi fputtare solo n byte
bzero(&recvline, MAXLINE);
// sta chiudendo...ho il tempo di chiudere le connessioni
if (dp[i].status[c] == 8)
break;
// controllo dei socket in polling
if (select(max_sd, &rfds, NULL, &efds, NULL) == -1)
{
info(0, "Error in select(): %s", strerror(errno));
break;
}
if (dp[i].status[c] == 8)
break;
if (FD_ISSET(dp[i].dest_sd[c], &rfds)
|| FD_ISSET(dp[i].dest_sd[c], &efds))
{
// dati in arrivo dal "server"
n = read(dp[i].dest_sd[c], recvline, MAXLINE);
// incremento il valore dei byte ricevuti
dp[i].tb[c] += n;
// se la finestra ke lista le connessioni e' aperta
// aggiorno i tb e rb
if (win == 2)
{
if (cpos == c)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, c + 3, 6, "%s:%d (%d/%d)",
dp[i].ch[c], dp[i].cp[c], dp[i].rb[c],
dp[i].tb[c]);
wattrset(wlist, A_NORMAL);
}
else
mvwprintw(wlist, c + 3, 6, "%s:%d (%d/%d)",
dp[i].ch[c], dp[i].cp[c], dp[i].rb[c],
dp[i].tb[c]);;
wrefresh(wlist);
}
// scrivo sull'altro fd
inf = write(dp[i].from_sd[c], recvline, n);
if (inf < 1)
{
info(0, "Error in write(): %s", strerror(errno));
break;
}
// se e' 1,2 o 3 (qui devo printarli in ogni caso
if (dp[i].status[c] < 4 && dp[i].status[c] > 0)
{
recvline[n] = '\0';
normal(recvline, strlen(recvline));
mvwprintw(wdata, LINES / 2, 0, recvline);
wrefresh(wdata);
}
else if (dp[i].status[c] == 4)
if (strstr(recvline, dp[i].mstring) != NULL)
{
recvline[n] = '\0';
normal(recvline, strlen(recvline));
mvwprintw(wdata, LINES / 2, 0, recvline);
wrefresh(wdata);
}
}
else if (FD_ISSET(dp[i].from_sd[c], &rfds)
|| FD_ISSET(dp[i].from_sd[c], &efds))
{
// dati in arrivo dal "client"
n = read(dp[i].from_sd[c], recvline, MAXLINE);
// incremento il valore dei byte ricevuti
dp[i].rb[c] += n;
// se la finestra ke lista le connessioni e' aperta
// aggiorno i tb e rb
if (win == 2)
{
if (cpos == c)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, c + 3, 6, "%s:%d (%d/%d)",
dp[i].ch[c], dp[i].cp[c], dp[i].rb[c],
dp[i].tb[c]);
wattrset(wlist, A_NORMAL);
}
else
mvwprintw(wlist, c + 3, 6, "%s:%d (%d/%d)",
dp[i].ch[c], dp[i].cp[c], dp[i].rb[c],
dp[i].tb[c]);
wrefresh(wlist);
}
// scrivo sull'altro fd
inf = write(dp[i].dest_sd[c], recvline, n);
if (inf < 1)
{
info(0, "Error in write(): %s", strerror(errno));
break;
}
if (dp[i].status[c] == 0 || dp[i].status[c] == 2)
{
recvline[n] = '\0';
normal(recvline, strlen(recvline));
mvwprintw(wdata, (LINES / 2), 0, recvline);
wrefresh(wdata);
}
else if (dp[i].status[c] == 3)
{
recvline[n] = '\0';
normal(recvline, strlen(recvline));
wattrset(wdata, COLOR_PAIR(1));
mvwprintw(wdata, (LINES / 2), 0, recvline);
wattrset(wdata, A_NORMAL);
wrefresh(wdata);
}
else if (dp[i].status[c] == 4)
if (strstr(recvline, dp[i].mstring) != NULL)
{
recvline[n] = '\0';
normal(recvline, strlen(recvline));
mvwprintw(wdata, LINES / 2, 0, recvline);
wrefresh(wdata);
}
}
if (dp[i].logfile[c] && l == 0)
{
// apro il file descriptor associato alla connessione alla prima
// richiesta di logging
sprintf(s, "%s-localhost:%d-%s:%d", dp[i].ch[c],
dp[i].localport, dp[i].host, dp[i].remoteport);
dp[i].fout = fopen(s, "a");
fprintf(dp[i].fout, "\n------ Logging from ------\n");
fprintf(dp[i].fout, "client requesting: %s:%d\n", dp[i].ch[c],
dp[i].cp[i]);
fprintf(dp[i].fout, "datapipe on: 127.0.0.1:%d\n", dp[i].localport);
fprintf(dp[i].fout, "redirecting to: %s:%d\n", dp[i].host,
dp[i].remoteport);
fprintf(dp[i].fout, "---------- DATA ----------\n");
// incremento il parametro l di check
l++;
}
// se la connessione è da loggare allora lo faccio
if (dp[i].logfile[c])
fputs(recvline, dp[i].fout);
}
// fprintf(stderr, "Closing connection %d of datapipe %d\n", c, i);
//
// connessione interrotta
shutdown(dp[i].from_sd[c], 2);
shutdown(dp[i].dest_sd[c], 2);
close(dp[i].from_sd[c]);
close(dp[i].dest_sd[c]);
// chiudo lo stream
if (dp[i].fout != NULL)
{
fprintf(dp[i].fout, "byte trasmitted : %ld IN, %ld OUT\n", dp[i].rb[c],
dp[i].tb[c]);
fclose(dp[i].fout);
}
// i datapipe corrente
// c connessione corrente
//
// deve cancellare la connessione corrente
// e far shiftare tutto l'array delle connessioni indietro
//
/*
* fprintf(stderr, "Comincio il ciclo del male\n"); for (n = 0, inf = 0; n
* < dp[i].cn; n++) {
*
* // siamo dopo l'elemento eliminato if (inf) { fprintf(stderr, "ok,
* dentro il ciclo del male, n=%d c=%d\n", n, c);
*
* dp[i].status[n - 1] = dp[i].status[n]; dp[i].cp[n - 1] = dp[dpos].cp[n];
*
* fprintf(stderr, "Prima della free\n"); if (dp[i].ch[n - 1] != NULL) {
* fprintf(stderr, "Ok... libero davvero\n"); free(dp[i].ch[n - 1]); }
*
* fprintf(stderr, "la free nn e' il male\n");
*
* dp[i].ch[n - 1] = (char *) malloc(strlen(dp[i].ch[n]) + 1);
* fprintf(stderr, "la malloc nn e' il male, forse strcpy\n");
*
* strcpy(dp[i].ch[n - 1], dp[i].ch[n]); fprintf(stderr, "strcpy nn e' il
* male\n");
*
* dp[i].rb[n - 1] = dp[i].rb[n]; dp[i].tb[n - 1] = dp[i].tb[n];
* dp[i].from_sd[n - 1] = dp[i].from_sd[n]; dp[i].dest_sd[n - 1] =
* dp[i].dest_sd[n]; fprintf(stderr, "bene\n"); }
*
* if (n == c) { fprintf(stderr, "Ok....sono all'elemento eliminato %d\n",
* c); free(dp[i].ch[n]); inf = 1; } }
*
* fprintf(stderr, "Fatto...ho finito il ciclo del male\n");
*
* // decrementa il numero di connessioni x quel datapipe
*/
closing:
dp[i].cn--;
wclear(wlist);
wborder(wlist, 0, 0, 0, 0, ACS_HLINE, 0, ACS_HLINE, 0);
// e' aperta la finestra ke lista le connessioni
if (win == 2)
{
// se era l'unica connessione
// apre la finestra ke lista i datapipe
if (!dp[i].cn)
{
win = 1;
for (n = 0; n < ndp; n++)
{
if (n == dpos)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, n + 2, 4, "%d -> %s:%d (%d) %c",
dp[n].localport, dp[n].host,
dp[n].remoteport, dp[n].cn,
CHECKPWD(n) ? 'P' : ' ');
wattrset(wlist, A_NORMAL);
}
else
mvwaddstr(wlist, n + 2, 4, s);
wrefresh(wlist);
}
}
// nn e' l'unica connessione
// riprinta tuta la lista delle connessioni
else
{
for (n = 0; n < dp[i].cn; n++)
{
if (n == 0)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, n + 3, 6, "%s:%d (%d/%d) %c",
dp[n].ch[c], dp[n].cp[c], dp[n].rb[c],
dp[n].tb[c], CHECKPWD(n) ? 'P' : ' ');
wattrset(wlist, A_NORMAL);
}
else
mvwaddstr(wlist, n + 3, 6, s);
}
}
}
// e' aperta la finestra ke lista i datapipe
else
{
for (n = 0; n < ndp; n++)
{
sprintf(s, "%d -> %s:%d (%d)", dp[n].localport, dp[n].host,
dp[n].remoteport, dp[n].cn);
if (n == dpos)
{
wattrset(wlist, A_REVERSE);
mvwprintw(wlist, n + 2, 4, "%d -> %s:%d (%d) %c",
dp[n].localport, dp[n].host, dp[n].remoteport,
dp[n].cn, CHECKPWD(n) ? 'P' : ' ');
wattrset(wlist, A_NORMAL);
}
else
mvwprintw(wlist, n + 2, 4, s);
}
}
// fprintf(stderr, "qua\n");
wrefresh(wlist);
dp[i].stat = 0;
info(1, "Connection closed");
return;
}
void finish()
{
int i, a;
info(0, "Closing program... ");
// libero la memoria allocata
for (i = 0; i < ndp; i++)
{
free(dp[i].host);
for (a = 0; a < dp[i].cn; a++)
free(dp[i].ch[a]);
if (dp[i].fout!=NULL)
fclose(dp[i].fout);
}
// cancello le finestre create prima
delwin(wmenu);
delwin(wlist);
delwin(wdata);
// pulisco lo schermo
clear();
refresh();
// chiudo le ncurses
endwin();
exit(0);
}
void normal(char *s, int len)
{
char str[len + 1];
int i = 0, i2 = 0;
memset(&str, 0, len + 1);
for (; i < len; i++)
if (s[i] != '\r')
str[i2++] = s[i];
strcpy(s, str);
}
void statzero()
{
int i, y;
for (i = 0; i < ndp; i++)
for (y = 0; y < dp[i].cn; y++)
dp[i].status[y] = -1;
}