内网穿透

关于公网的 shell 如何返回到内网,我们可以通过端口映射来解决。

C 语言版本的 portmap 源码:

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
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>

#define VERSION "2.2"
#define TIMEOUT 300
#define max(a,b) (a)>(b)?(a):(b)
#define MAXSIZE 10240
#define HOSTLEN 40
#define CONNECT_NUMBER 5

/* define function here */
void usage(char *s);
void transdata(int fd1,int fd2);
void closeallfd(int);
void makelog(char *buffer,int length);
int testifisvalue(char *str);
int bind2conn(int port1,char *host,int port2);
int bind2bind(int port1,int port2);
int conn2conn(char *host1,int port1,char *host2,int port2);
int create_socket();
int create_serv(int sockfd,int port);
int client_connect(int sockfd,char* server,int port);

/* define GLOBAL varible here */
extern int errno;
FILE *fp;

main(int argc,char **argv)
{
char **p;
char host1[HOSTLEN],host2[HOSTLEN];
int port1=0,port2=0,method=0;
int length;
char *logfile=NULL;

p=argv;
memset(host1,0,HOSTLEN);
memset(host2,0,HOSTLEN);
while(*p)
{
if(strcmp(*p,"-v")==0)
{
printf("Socket data transport tool.\r\nVersion:%s\r\n",VERSION);
p++;
continue;
}
if(strcmp(*p,"-h1")==0)
{
if(testifisvalue(*(p+1))==1)
{
length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1));
strncpy(host1,*(++p),length);
}
p++;
continue;
}
if(strcmp(*p,"-h2")==0)
{
if(testifisvalue(*(p+1))==1)
{
length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1));
strncpy(host2,*(++p),length);
}
p++;
continue;
}
if(strcmp(*p,"-p1")==0)
{
if(testifisvalue(*(p+1))==1)
port1=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-p2")==0)
{
if(testifisvalue(*(p+1))==1)
port2=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-m")==0)
{
if(testifisvalue(*(p+1))==1)
method=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-log")==0)
{
if(testifisvalue(*(p+1))==1)
logfile=*(++p);
else
{
printf("[ERROR]:must supply logfile name\r\n");
exit(0);
}
p++;
continue;
}
p++;
}
signal(SIGCLD,SIG_IGN);
signal(SIGINT,&closeallfd);
if(logfile !=NULL)
{
fp=fopen(logfile,"a");
if(fp == NULL )
{
perror("open logfile");
exit(0);
}
}
makelog("######################## start ################\r\n",49);
switch(method)
{
case 0:
usage(argv[0]);
break;
case 1:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
if(strlen(host2)==0)
{
printf("[ERROR]:must supply HOST2.\r\n");
break;
}
bind2conn(port1,host2,port2);
break;
case 2:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
bind2bind(port1,port2);
break;
case 3:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
if(strlen(host1)==0)
{
printf("[ERROR]:must supply HOST1.\r\n");
break;
}
if(strlen(host2)==0)
{
printf("[ERROR]:must supply HOST2.\r\n");
break;
}
conn2conn(host1,port1,host2,port2);
break;
default:
usage(argv[0]);
}
closeallfd(0);
}

int testifisvalue(char *str)
{
if(str == NULL ) return(0);
if(str[0]=='-') return(0);
return(1);
}

void usage(char *s)
{
printf("Socket data transport tool\r\n");
printf("by bkbll(bkbll@cnhonker.net)\r\n\r\n");
printf("Usage:%s -m method [-h1 host1] -p1 port1 [-h2 host2] -p2 port2 [-v] [-log filename]\r\n",s);
printf(" -v: version\r\n");
printf(" -h1: host1\r\n");
printf(" -h2: host2\r\n");
printf(" -p1: port1\r\n");
printf(" -p2: port2\r\n");
printf(" -log: log the data\r\n");
printf(" -m: the action method for this tool\r\n");
printf(" 1: listen on PORT1 and connect to HOST2:PORT2\r\n");
printf(" 2: listen on PORT1 and PORT2\r\n");
printf(" 3: connect to HOST1:PORT1 and HOST2:PORT2\r\n");

closeallfd(0);
}

int bind2conn(int port1,char *host,int port2)
{
int sockfd,sockfd1,sockfd2;
struct sockaddr_in remote;
int size;
int pid;
char buffer[1024];

memset(buffer,0,1024);
if((sockfd=create_socket())==0) exit(0);
if(create_serv(sockfd,port1)==0)
{
close(sockfd1);
exit(0);
}
size=sizeof(struct sockaddr);
while(1)
{
printf("waiting for response.........\n");
if((sockfd1=accept(sockfd,(struct sockaddr *)&remote,(socklen_t *)&size))<0)
{
perror("accept error\n");
continue;
}
printf("accept a client from %s:%d\n",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port));
if((sockfd2=create_socket())==0)
{
close(sockfd1);
continue;
}
printf("make a connection to %s:%d....",host,port2);
fflush(stdout);
if(client_connect(sockfd2,host,port2)==0)
{
close(sockfd2);
sprintf(buffer,"[SERVER]connection to %s:%d error\r\n",host,port2);
write(sockfd1,buffer,strlen(buffer));
memset(buffer,0,1024);
close(sockfd1);
continue;
}
printf("ok\r\n");
pid=fork();
if(pid==0) transdata(sockfd1,sockfd2);
// sleep(2);
close(sockfd1);
close(sockfd2);
}

}

int bind2bind(int port1,int port2)
{
int fd1,fd2,sockfd1,sockfd2;
struct sockaddr_in client1,client2;
int size1,size2;
int pid;

if((fd1=create_socket())==0) exit(0);
if((fd2=create_socket())==0) exit(0);
printf("binding port %d......",port1);
fflush(stdout);
if(create_serv(fd1,port1)==0)
{
close(fd1);
exit(0);
}
printf("ok\r\n");
printf("binding port %d......",port2);
fflush(stdout);
if(create_serv(fd2,port2)==0)
{
close(fd2);
exit(0);
}
printf("ok\r\n");
size1=size2=sizeof(struct sockaddr);
while(1)
{
printf("waiting for response on port %d.........\n",port1);
if((sockfd1=accept(fd1,(struct sockaddr *)&client1,(socklen_t *)&size1))<0)
{
perror("accept1 error");
continue;
}
printf("accept a client on port %d from %s,waiting another on port %d....\n",port1,inet_ntoa(client1.sin_addr),port2);
if((sockfd2=accept(fd2,(struct sockaddr *)&client2,(socklen_t *)&size2))<0)
{
perror("accept2 error");
close(sockfd1);
continue;
}
printf("accept a client on port %d from %s\n",port2,inet_ntoa(client2.sin_addr));
pid=fork();
if(pid==0) transdata(sockfd1,sockfd2);
//sleep(2);
close(sockfd1);
close(sockfd2);
}
}

int conn2conn(char *host1,int port1,char *host2,int port2)
{
int sockfd1,sockfd2;
int pid;
fd_set fds;
int l;
int result;
char buffer[MAXSIZE];

while(1)
{
if((sockfd1=create_socket())==0) exit(0);
if((sockfd2=create_socket())==0) exit(0);
printf("[+] make a connection to %s:%d....\r\n",host1,port1);
fflush(stdout);
if(client_connect(sockfd1,host1,port1)==0)
{
printf("[-] connect to host1 failed\r\n");
close(sockfd1);
close(sockfd2);
continue;
}
printf("[+] host1 connected\r\n");
l=0;
memset(buffer,0,MAXSIZE);
//fixed by Twi1ight 2012.09.12
while(1)
{
FD_ZERO(&fds);
FD_SET(sockfd1, &fds);
if (select(sockfd1+1, &fds, NULL, NULL, NULL) < 0 )
{
if (errno == EINTR) continue;
break;
}

if (FD_ISSET(sockfd1, &fds))
{
l=read(sockfd1, buffer, MAXSIZE);
break;
}
sleep(5);
}
if(l<=0)
{
printf("[-] there is a error...Create a new connection.\r\n");
continue;
}
while(1)
{
printf("[+] make a connection to %s:%d....\r\n",host2,port2);
fflush(stdout);
if(client_connect(sockfd2,host2,port2)==0)
{
printf("[-] connect to host2 failed\r\n");
close(sockfd1);
close(sockfd2);
continue;
}
if(write(sockfd2,buffer,l) < 0)
{
printf("[-] send failed.\r\n");
continue;
}
l=0;
memset(buffer,0,MAXSIZE);
break;
}
printf("[+] all hosts connected!\r\n");
pid=fork();
if(pid==0) transdata(sockfd1,sockfd2);
//sleep(2);
close(sockfd1);
close(sockfd2);
}
}

void transdata(int fd1,int fd2)
{
struct timeval timeset;
fd_set readfd,writefd;
int result,i=0;
char read_in1[MAXSIZE],send_out1[MAXSIZE];
char read_in2[MAXSIZE],send_out2[MAXSIZE];
int read1=0,totalread1=0,send1=0;
int read2=0,totalread2=0,send2=0;
int sendcount1,sendcount2;
int maxfd;
struct sockaddr_in client1,client2;
int structsize1,structsize2;
char host1[20],host2[20];
int port1=0,port2=0;
char tmpbuf1[100],tmpbuf2[100];

memset(host1,0,20);
memset(host2,0,20);
memset(tmpbuf1,0,100);
memset(tmpbuf2,0,100);
if(fp!=NULL)
{
structsize1=sizeof(struct sockaddr);
structsize2=sizeof(struct sockaddr);
if(getpeername(fd1,(struct sockaddr *)&client1,(socklen_t *)&structsize1)<0)
{
strcpy(host1,"fd1");
}
else
{
printf("got,ip:%s,port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port));
strcpy(host1,inet_ntoa(client1.sin_addr));
port1=ntohs(client1.sin_port);
}
if(getpeername(fd2,(struct sockaddr *)&client2,(socklen_t *)&structsize2)<0)
{
strcpy(host2,"fd2");
}
else
{
printf("got,ip:%s,port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port));
strcpy(host2,inet_ntoa(client2.sin_addr));
port2=ntohs(client2.sin_port);
}
sprintf(tmpbuf1,"\r\n########### read from %s:%d ####################\r\n",host1,port1);
sprintf(tmpbuf2,"\r\n########### reply from %s:%d ####################\r\n",host2,port2);
}

maxfd=max(fd1,fd2)+1;
memset(read_in1,0,MAXSIZE);
memset(read_in2,0,MAXSIZE);
memset(send_out1,0,MAXSIZE);
memset(send_out2,0,MAXSIZE);

timeset.tv_sec=TIMEOUT;
timeset.tv_usec=0;
while(1)
{
FD_ZERO(&readfd);
FD_ZERO(&writefd);

FD_SET(fd1,&readfd);
FD_SET(fd1,&writefd);
FD_SET(fd2,&writefd);
FD_SET(fd2,&readfd);

result=select(maxfd,&readfd,&writefd,NULL,&timeset);
if((result<0) && (errno!=EINTR))
{
perror("select error");
break;
}
else if(result==0)
{
printf("time out\n");
break;
}
if(FD_ISSET(fd1,&readfd))
{
/* 不能超过MAXSIZE-totalread1,不然send_out1会溢出 */
if(totalread1<MAXSIZE)
{
read1=read(fd1,read_in1,MAXSIZE-totalread1);
if(read1==0) break;
if((read1<0) && (errno!=EINTR))
{
perror("read data error");
break;
}
memcpy(send_out1+totalread1,read_in1,read1);
makelog(tmpbuf1,strlen(tmpbuf1));
makelog(read_in1,read1);
totalread1+=read1;
memset(read_in1,0,MAXSIZE);
}
}
if(FD_ISSET(fd2,&writefd))
{
int err=0;
sendcount1=0;
while(totalread1>0)
{
send1=write(fd2,send_out1+sendcount1,totalread1);
if(send1==0)break;
if((send1<0) && (errno!=EINTR))
{
perror("unknow error");
err=1;
break;
}
if((send1<0) && (errno==ENOSPC)) break;
sendcount1+=send1;
totalread1-=send1;
}
if(err==1) break;
if((totalread1>0) && (sendcount1>0))
{
/* 移动未发送完的数据到开始 */
memcpy(send_out1,send_out1+sendcount1,totalread1);
memset(send_out1+totalread1,0,MAXSIZE-totalread1);
}
else
memset(send_out1,0,MAXSIZE);
}
if(FD_ISSET(fd2,&readfd))
{

if(totalread2<MAXSIZE)
{
read2=read(fd2,read_in2,MAXSIZE-totalread2);
if(read2==0)break;
if((read2<0) && (errno!=EINTR))
{
perror("read data error");
break;
}
memcpy(send_out2+totalread2,read_in2,read2);
makelog(tmpbuf2,strlen(tmpbuf2));
makelog(read_in2,read2);
totalread2+=read2;
memset(read_in2,0,MAXSIZE);
}
}
if(FD_ISSET(fd1,&writefd))
{
int err2=0;
sendcount2=0;
while(totalread2>0)
{
send2=write(fd1,send_out2+sendcount2,totalread2);
if(send2==0)break;
if((send2<0) && (errno!=EINTR))
{
perror("unknow error");
err2=1;
break;
}
if((send2<0) && (errno==ENOSPC)) break;
sendcount2+=send2;
totalread2-=send2;
}
if(err2==1) break;
if((totalread2>0) && (sendcount2 > 0))
{
/* 移动未发送完的数据到开始 */
memcpy(send_out2,send_out2+sendcount2,totalread2);
memset(send_out2+totalread2,0,MAXSIZE-totalread2);
}
else
memset(send_out2,0,MAXSIZE);
}
}

close(fd1);
close(fd2);
printf("ok,I closed the two fd\r\n");
//exit(0);
}

void closeallfd(int n)
{
int i;
printf("Let me exit...");
fflush(stdout);
for(i=3;i<256;i++)
{
close(i);
}
if(fp != NULL)
{
fprintf(fp,"exited\r\n");
fclose(fp);
}
printf("all overd\r\n");
exit(0);
}
void makelog(char *buffer,int length)
{
if(fp !=NULL)
{
//fprintf(fp,"%s",buffer);
write(fileno(fp),buffer,length);
fflush(fp);
}
}

int create_socket()
{
int sockfd;

sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
perror("create socket error");
return(0);
}
return(sockfd);
}

int create_serv(int sockfd,int port)
{
struct sockaddr_in srvaddr;
int on=1;

bzero(&srvaddr,sizeof(struct sockaddr));
srvaddr.sin_port=htons(port);
srvaddr.sin_family=AF_INET;
srvaddr.sin_addr.s_addr=htonl(INADDR_ANY);

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //so I can rebind the port
if(bind(sockfd,(struct sockaddr *)&srvaddr,sizeof(struct sockaddr))<0)
{
perror("error");
return(0);
}
if(listen(sockfd,CONNECT_NUMBER)<0)
{
perror("listen error\n");
return(0);
}
return(1);
}

int client_connect(int sockfd,char* server,int port)
{
struct sockaddr_in cliaddr;
struct hostent *host;

if(!(host=gethostbyname(server)))
{
printf("gethostbyname(%s) error:%s\n",server,strerror(errno));
return(0);
}

bzero(&cliaddr,sizeof(struct sockaddr));
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(port);
cliaddr.sin_addr=*((struct in_addr *)host->h_addr);

if(connect(sockfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr))<0)
{
perror("[-] error");
return(0);
}
return(1);
}
0%