码迷,mamicode.com
首页 > 其他好文 > 详细

LCX端口转发源码分析

时间:2020-07-20 15:16:44      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:nal   void   ase   adf   must   ems   code   之间   rmi   

 lcx.exe算是端口转发工具中普及率特别高的吧。当然,最近也出了好些Web端口转发工具,这个待后续再分析一下。我从网上下载了一个lcx的源码片段,结合源码谈谈自己的认识。

      lcx工作原理是使不同端口之间形成一个回路,这样就可以进行端口转发。常用于外网连接内网3389端口。具体使用方法网上一堆。

      这个程序主要采用Socket来实现,有几个比较重要的函数:bind2bind、bind2conn、conn2conn及transmitdata。transmitdata函数是核心,这个函数作用是使得不同端口之间的数据形成一个回路。

      注意源代码中的setsocket函数,其中设置了SO_REUSEADDR。设置SO_REUSEADDR之后,可以应用的一种场景就是同一个端口可以绑定多个socket。查看相关资料可以得知,其实设置SO_REUSEADDR之后,还有其他几种应用场景。我试了一下,你把setsocket函数注释掉,在运行这个端口转发程序时,本地listen的时候设置的两个端口不一样程序还是可以正常运行的。启用setsocket函数之后,本地listen的时候,可以设置两个端口为同一个值,但是这样设置的时候,我在虚拟机中无法远程连接,一直就是尝试连接也不报错。原因不因,未深入分析。

      网上很多的源码其实是有问题的,下面我分享一个稍作修改的源代码,能够在windows xp +vc6.0下编译通过,理论上是可以在windows系列所有32位版本下运行的。对于64位系统,我也修改了一个版本,在windows 7 x64+vs2013编译通过,主要用于64位windows下运行。可执行文件我就不放出来了,自己编译运行差不多就OK了。

      Windows XP +VC6.0下编译通过的源码:

技术图片
/*
************************************************************************************
* 
* HTran.cpp - HUC Packet Transmit Tool.
*
* Copyright (C) 2000-2004 HUC All Rights Reserved.
*
* Author : lion
* : lion#cnhonker.net
* : <a href="http://www.cnhonker.com" target="_blank">http://www.cnhonker.com</a>
* :
* Notice : Thx to bkbll (bkbll#cnhonker.net)
* :
* Date : 2003-10-20
* :
* Complie : cl HTran.cpp
* :
* Usage : E:\>HTran
* : ======================== HUC Packet Transmit Tool V1.00 =======================
* : =========== Code by lion & bkbll, Welcome to <a href="http://www.cnhonker.com" target="_blank">http://www.cnhonker.com</a> ==========
* :
* : [Usage of Packet Transmit:]
* : HTran - [-log logfile]
* :
* : [option:]
* : -listen 
* : -tran 
* : -slave 
*
************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <signal.h>
#include <errno.h>
#include <io.h> 
#pragma comment(lib, "ws2_32.lib")
 
#define VERSION "1.00"
#define TIMEOUT 300
#define MAXSIZE 20480
#define HOSTLEN 40
#define CONNECTNUM 5
 
// define 2 socket struct
struct transocket 
{
    SOCKET fd1;
    SOCKET fd2;
};
 
// define function 
void ver();
void usage(char *prog);
void transmitdata(LPVOID data);
void getctrlc(int j);
void closeallfd();
void makelog(char *buffer, int length);
void proxy(int port);
void bind2bind(int port1, int port2);
void bind2conn(int port1, char *host, int port2);
void conn2conn(char *host1, int port1, char *host2, int port2);
int testifisvalue(char *str);
int create_socket();
int create_server(int sockfd, int port);
int client_connect(int sockfd, char* server, int port);
 
// define GLOBAL variable here
extern int errno;
FILE *fp;
int method=0;
//int connectnum=0;
 
//************************************************************************************
// 
// function main 主要是处理用户参数输入的问题
//
//************************************************************************************
VOID main(int argc, char* argv[])
{
    char **p;
    char sConnectHost[HOSTLEN], sTransmitHost[HOSTLEN];
    int iConnectPort=0, iTransmitPort=0;
    char *logfile=NULL;
     
    ver();
    memset(sConnectHost, 0, HOSTLEN);
    memset(sTransmitHost, 0, HOSTLEN);
     
    p=argv;
    while(*p)
    {
        if(stricmp(*p, "-log") == 0)
        {
            if(testifisvalue(*(p+1)))
            {
                logfile = *(++p);
            } 
            else
            {
                printf("[-] ERROR: Must supply logfile name.\r\n");
                return;
            }
            p++;
            continue;
        }
        p++;
    }
     
    if(logfile !=NULL)
    {
        fp=fopen(logfile,"a");
        if(fp == NULL ) 
        {
            printf("[-] ERROR: open logfile");
            return;
        }
     
        makelog("====== Start ======\r\n", 22);
    }
     
     
    // Win Start Winsock.
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
     
    signal(SIGINT, &getctrlc);
     
    if(argc > 2)
    {
        if(stricmp(argv[1], "-listen") == 0 && argc >= 4)
        {
            iConnectPort = atoi(argv[2]);
            iTransmitPort = atoi(argv[3]);
            method = 1;
        }
        else
        if(stricmp(argv[1], "-tran") == 0 && argc >= 5)
        {
            iConnectPort = atoi(argv[2]);
            strncpy(sTransmitHost, argv[3], HOSTLEN);
            iTransmitPort = atoi(argv[4]);
            method = 2;
        }
        else
        if(stricmp(argv[1], "-slave") == 0 && argc >= 6)
        {
            strncpy(sConnectHost, argv[2], HOSTLEN);
            iConnectPort = atoi(argv[3]);
            strncpy(sTransmitHost, argv[4], HOSTLEN);
            iTransmitPort = atoi(argv[5]);
            method = 3;
        }
    }
     
    switch(method)
    {
        case 1:
        bind2bind(iConnectPort, iTransmitPort);
        break;
        case 2:
        bind2conn(iConnectPort, sTransmitHost, iTransmitPort);
        break;
        case 3:
        conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort);
        break;
        default:
        usage(argv[0]);
        break;
    }
     
    if(method)
    {
        closeallfd();
    }
     
    WSACleanup();
     
    return;
}
 
 
//************************************************************************************
// 
// print version message
//
//************************************************************************************
VOID ver() 
{ 
    printf("======================== HUC Packet Transmit Tool V%s =======================\r\n", VERSION);
    printf("=========== Code by lion & bkbll, Welcome to http://www.cnhonker.com==========\r\n\n");
}
 
//************************************************************************************
// 
// print usage message
//
//************************************************************************************
VOID usage(char* prog) 
{ 
    printf("[Usage of Packet Transmit:]\r\n");
    printf(" %s - [-log logfile]\n\n", prog);
    printf("[option:]\n");
    printf(" -listen \n");
    printf(" -tran \n");
    printf(" -slave \n\n");
    return;
}
 
//************************************************************************************
// 
// test if is value 
//
//************************************************************************************
int testifisvalue(char *str)
{
    if(str == NULL ) return(0);
    if(str[0]==‘-‘) return(0);
    return(1);
}
 
//************************************************************************************
// 
// LocalHost:ConnectPort transmit to LocalHost:TransmitPort
//
//************************************************************************************
void bind2bind(int port1, int port2)
{
    SOCKET fd1,fd2, sockfd1, sockfd2;
    struct sockaddr_in client1,client2;
    int size1,size2;
     
    HANDLE hThread=NULL;
    transocket sock;
    DWORD dwThreadID;
     
    if((fd1=create_socket())==0) return;
    if((fd2=create_socket())==0) return;
     
    printf("[+] Listening port %d ......\r\n",port1);
    fflush(stdout);
     
    if(create_server(fd1, port1)==0)
    {
        closesocket(fd1);
        return;
    }
     
    printf("[+] Listen OK!\r\n");
    printf("[+] Listening port %d ......\r\n",port2);
    fflush(stdout);
    if(create_server(fd2, port2)==0)
    {
        closesocket(fd2);
        return;
    }
     
    printf("[+] Listen OK!\r\n");
    size1=size2=sizeof(struct sockaddr);
    while(1)
    {
        printf("[+] Waiting for Client on port:%d ......\r\n",port1);
        if((sockfd1 = accept(fd1,(struct sockaddr *)&client1,&size1))<0)
        {
            printf("[-] Accept1 error.\r\n");
            continue;
        }
     
    printf("[+] Accept a Client on port %d from %s ......\r\n", port1, inet_ntoa(client1.sin_addr));
    printf("[+] Waiting another Client on port:%d....\r\n", port2);
    if((sockfd2 = accept(fd2, (struct sockaddr *)&client2, &size2))<0)
    {
        printf("[-] Accept2 error.\r\n");
        closesocket(sockfd1);
        continue;
    }
     
    printf("[+] Accept a Client on port %d from %s\r\n",port2, inet_ntoa(client2.sin_addr));
    printf("[+] Accept Connect OK!\r\n");
     
    sock.fd1 = sockfd1;
    sock.fd2 = sockfd2;
     
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID); 
    if(hThread == NULL) 
    {
        TerminateThread(hThread, 0);
        return;
    }
     
    Sleep(1000);
    printf("[+] CreateThread OK!\r\n\n");
    }
}
 
//************************************************************************************
// 
// LocalHost:ConnectPort transmit to TransmitHost:TransmitPort
//
//************************************************************************************
void bind2conn(int port1, char *host, int port2)
{
    SOCKET sockfd,sockfd1,sockfd2;
    struct sockaddr_in remote;
    int size;
    char buffer[1024];
     
    HANDLE hThread=NULL;
    transocket sock;
    DWORD dwThreadID;
     
    if (port1 > 65535 || port1 < 1)
    {
        printf("[-] ConnectPort invalid.\r\n");
        return;
    }
     
    if (port2 > 65535 || port2 < 1)
    {
        printf("[-] TransmitPort invalid.\r\n");
        return;
    }
     
    memset(buffer,0,1024);
    if((sockfd=create_socket()) == INVALID_SOCKET) return;
     
    if(create_server(sockfd, port1) == 0) 
    {
        closesocket(sockfd);
        return;
    }
     
    size=sizeof(struct sockaddr);
    while(1)
    {
        printf("[+] Waiting for Client ......\r\n"); 
        if((sockfd1=accept(sockfd,(struct sockaddr *)&remote,&size))<0)
        {
        printf("[-] Accept error.\r\n");
        continue;
    }
     
    printf("[+] Accept a Client from %s:%d ......\r\n",
    inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
    if((sockfd2=create_socket())==0) 
    {
        closesocket(sockfd1);
        continue; 
    }
    printf("[+] Make a Connection to %s:%d ......\r\n",host,port2);
    fflush(stdout);
     
    if(client_connect(sockfd2,host,port2)==0)
    {
        closesocket(sockfd2);
        sprintf(buffer,"[SERVER]connection to %s:%d error\r\n", host, port2);
        send(sockfd1,buffer,strlen(buffer),0);
        memset(buffer, 0, 1024);
        closesocket(sockfd1);
        continue;
    }
     
    printf("[+] Connect OK!\r\n");
     
    sock.fd1 = sockfd1;
    sock.fd2 = sockfd2;
     
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID); 
    if(hThread == NULL) 
    {
        TerminateThread(hThread, 0);
        return;
    }
     
    Sleep(1000);
    printf("[+] CreateThread OK!\r\n\n");
    }
}
 
//************************************************************************************
// 
// ConnectHost:ConnectPort transmit to TransmitHost:TransmitPort
//
//************************************************************************************
void conn2conn(char *host1,int port1,char *host2,int port2)
{
    SOCKET sockfd1,sockfd2;
     
    HANDLE hThread=NULL;
    transocket sock;
    DWORD dwThreadID;
    fd_set fds;
    int l;
    char buffer[MAXSIZE];
     
    while(1)
    {
     
        if((sockfd1=create_socket())==0) return;
        if((sockfd2=create_socket())==0) return;
         
        printf("[+] Make a Connection to %s:%d....\r\n",host1,port1);
        fflush(stdout);
        if(client_connect(sockfd1,host1,port1)==0) 
        {
            closesocket(sockfd1);
            closesocket(sockfd2);
            continue;
        }
         
        // fix by bkbll 
        // if host1:port1 recved data, than connect to host2,port2
        l=0;
        memset(buffer,0,MAXSIZE);
        while(1)
        {
            FD_ZERO(&fds);
            FD_SET(sockfd1, &fds);
            if (select(sockfd1+1, &fds, NULL, NULL, NULL) == SOCKET_ERROR) 
            {
                if (errno == WSAEINTR) continue;
                break;
            }
            if (FD_ISSET(sockfd1, &fds)) 
            {
                l=recv(sockfd1, buffer, MAXSIZE, 0);
                break;
            }
            Sleep(5);
        }
         
        if(l<=0) 
        { 
            printf("[-] There is a error...Create a new connection.\r\n");
            continue;
        }
        while(1)
        {
            printf("[+] Connect OK!\r\n");
            printf("[+] Make a Connection to %s:%d....\r\n", host2,port2);
            fflush(stdout);
            if(client_connect(sockfd2,host2,port2)==0) 
            {
                closesocket(sockfd1);
                closesocket(sockfd2);
                continue;
            }
             
            if(send(sockfd2,buffer,l,0)==SOCKET_ERROR)
            { 
                printf("[-] Send failed.\r\n");
                continue;
            }
             
            l=0;
            memset(buffer,0,MAXSIZE);
            break;
        }
         
        printf("[+] All Connect OK!\r\n");
         
        sock.fd1 = sockfd1;
        sock.fd2 = sockfd2;
             
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID); 
        if(hThread == NULL) 
        {
            TerminateThread(hThread, 0);
            return;
        }
         
        // connectnum++;
         
        Sleep(1000);
        printf("[+] CreateThread OK!\r\n\n");
    }
}
 
//************************************************************************************
// 
// Socket Transmit to Socket
//
//************************************************************************************
void transmitdata(LPVOID data)
{
SOCKET fd1, fd2;
transocket *sock;
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 tmpbuf[100];
 
sock = (transocket *)data;
fd1 = sock->fd1;
fd2 = sock->fd2;
 
memset(host1,0,20);
memset(host2,0,20);
memset(tmpbuf,0,100);
 
structsize1=sizeof(struct sockaddr);
structsize2=sizeof(struct sockaddr);
 
if(getpeername(fd1,(struct sockaddr *)&client1,&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,&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);
}
 
printf("[+] Start Transmit (%s:%d <-> %s:%d) ......\r\n\n", host1, port1, 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((UINT)fd1, &readfd);
FD_SET((UINT)fd1, &writefd);
FD_SET((UINT)fd2, &writefd);
FD_SET((UINT)fd2, &readfd);
 
result=select(maxfd,&readfd,&writefd,NULL,&timeset);
if((result<0) && (errno!=EINTR))
{
printf("[-] Select error.\r\n");
break;
}
else if(result==0)
{
printf("[-] Socket time out.\r\n");
break;
}
 
if(FD_ISSET(fd1, &readfd))
{
/* must < MAXSIZE-totalread1, otherwise send_out1 will flow */
if(totalread1 < MAXSIZE

) {
read1=recv(fd1, read_in1, MAXSIZE-totalread1, 0); 
if((read1==SOCKET_ERROR) || (read1==0))
{
printf("[-] Read fd1 data error,maybe close?\r\n");
break;
}
 
memcpy(send_out1+totalread1,read_in1,read1);
sprintf(tmpbuf,"\r\nRecv %5d bytes from %s:%d\r\n", read1, host1, port1);
printf(" Recv %5d bytes %16s:%d\r\n", read1, host1, port1);
makelog(tmpbuf,strlen(tmpbuf));
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=send(fd2, send_out1+sendcount1, totalread1, 0);
if(send1==0)break;
if((send1<0) && (errno!=EINTR))
{
printf("[-] Send to fd2 unknow error.\r\n");
err=1;
break;
}
 
if((send1<0) && (errno==ENOSPC)) break;
sendcount1+=send1;
totalread1-=send1;
 
printf(" Send %5d bytes %16s:%d\r\n", send1, host2, port2);
}
 
if(err==1) break;
if((totalread1>0) && (sendcount1>0))
{
/* move not sended data to start addr */
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=recv(fd2,read_in2,MAXSIZE-totalread2, 0); 
if(read2==0)break;
if((read2<0) && (errno!=EINTR))
{
printf("[-] Read fd2 data error,maybe close?\r\n\r\n");
break;
}
 
memcpy(send_out2+totalread2,read_in2,read2);
sprintf(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read2, host2, port2);
printf(" Recv %5d bytes %16s:%d\r\n", read2, host2, port2);
makelog(tmpbuf,strlen(tmpbuf));
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=send(fd1, send_out2+sendcount2, totalread2, 0);
if(send2==0)break;
if((send2<0) && (errno!=EINTR))
{
printf("[-] Send to fd1 unknow error.\r\n");
err2=1;
break;
}
if((send2<0) && (errno==ENOSPC)) break;
sendcount2+=send2;
totalread2-=send2;
 
printf(" Send %5d bytes %16s:%d\r\n", send2, host1, port1);
}
if(err2==1) break;
if((totalread2>0) && (sendcount2 > 0))
{
/* move not sended data to start addr */
memcpy(send_out2, send_out2+sendcount2, totalread2);
memset(send_out2+totalread2, 0, MAXSIZE-totalread2);
}
else
memset(send_out2,0,MAXSIZE);
}
 
Sleep(5);
}
 
closesocket(fd1);
closesocket(fd2);
// if(method == 3)
// connectnum --;
 
printf("\r\n[+] OK! I Closed The Two Socket.\r\n"); 
}
 
void getctrlc(int j)
{
    printf("\r\n[-] Received Ctrl+C\r\n");
    closeallfd();
    exit(0);
}
 
void closeallfd()
{
    int i;
     
    printf("[+] Let me exit ......\r\n");
    fflush(stdout);
     
    for(i=3; i<256; i++)
    {
        closesocket(i); 
    }
     
    if(fp != NULL) 
    {
        fprintf(fp,"\r\n====== Exit ======\r\n");
        fclose(fp);
    }
     
    printf("[+] All Right!\r\n");
}
 
int create_socket()
{ 
    int sockfd;
 
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        printf("[-] Create socket error.\r\n");
        return(0);
    }
 
    return(sockfd); 
}
 
int create_server(int sockfd,int port)
{
    struct sockaddr_in srvaddr;
    int on=1;
 
    memset(&srvaddr, 0, 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, (char*)&on,sizeof(on)); //so I can rebind the port
 
    if(bind(sockfd,(struct sockaddr *)&srvaddr,sizeof(struct sockaddr))<0)
    {
        printf("[-] Socket bind error.\r\n");
        return(0);
    }
 
    if(listen(sockfd,CONNECTNUM)<0)
    {
        printf("[-] Socket Listen error.\r\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);
    }
 
    memset(&cliaddr, 0, 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)
    {
        printf("[-] Connect error.\r\n");
        return(0);
    }
    return(1);
}
 
void makelog(char *buffer,int length)
{
    if(fp !=NULL)
    {
        // fprintf(fp, "%s", buffer);
        // printf("%s",buffer);
        write(fileno(fp),buffer,length);
        // fflush(fp);
    }
}
技术图片

      附上效果图一张:

 技术图片

     Windows7 64+VS2013编译通过的源码:

技术图片
/*
************************************************************************************
*
* PortTrans.cpp - Port Forwarding.
*
* Copyright (C) 2015 HUC All Rights Reserved.
*
* Author : pandas
* : 
* :
* Date : 2015-4-9
* :
* Complie : cl PortTrans.cpp
* :
* Usage : D:\>PortTrans
* : ======================== Port Forwarding Tool V1.00 =======================
* :    ========================== Improved by pandas ===========================
* :
* : [Usage of Port Forwarding:]
* : PortTrans - [-log logfile]
* :
* : [option:]
* : -listen
* : -tran
* : -slave
*
************************************************************************************
*/
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <signal.h>
#include <errno.h>
#include <io.h> 
#pragma comment(lib, "ws2_32.lib")
#define VERSION "1.00"
#define TIMEOUT 300
#define MAXSIZE 20480
#define HOSTLEN 40
#define CONNECTNUM 5
// define 2 socket struct
struct transocket
{
    SOCKET fd1;
    SOCKET fd2;
};
// define function 
void ver();
void usage(char *prog);
void transmitdata(LPVOID data);
void getctrlc(int j);
void closeallfd();
void makelog(char *buffer, int length);
void proxy(int port);
void bind2bind(int port1, int port2);
void bind2conn(int port1, char *host, int port2);
void conn2conn(char *host1, int port1, char *host2, int port2);
int testifisvalue(char *str);
int create_socket();
int create_server(int sockfd, int port);
int client_connect(int sockfd, char* server, int port);
// define GLOBAL variable here
//int error;
FILE *fp;
int method = 0;
//int connectnum=0;
//************************************************************************************
// 
// function main 主要是处理用户参数输入的问题
//
//************************************************************************************
VOID main(int argc, char* argv[])
{
    char **p;
    char sConnectHost[HOSTLEN], sTransmitHost[HOSTLEN];
    int iConnectPort = 0, iTransmitPort = 0;
    char *logfile = NULL;
    errno_t err;
    ver();
    memset(sConnectHost, 0, HOSTLEN);
    memset(sTransmitHost, 0, HOSTLEN);
    p = argv;
    while (*p)
    {
        if (_stricmp(*p, "-log") == 0)
        {
            if (testifisvalue(*(p + 1)))
            {
                logfile = *(++p);
            }
            else
            {
                printf("[-] ERROR: Must supply logfile name.\r\n");
                return;
            }
            //p++;
            continue;
        }
        p++;
    }
    if (logfile != NULL)
    {
        //fp = fopen(logfile, "a");
        err = fopen_s(&fp, logfile, "a");
        if (fp == NULL)
        {
            printf("[-] ERROR: open logfile");
            return;
        }
        makelog("====== Start ======\r\n", 22);
    }
    // Win Start Winsock.
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    signal(SIGINT, &getctrlc);
    if (argc > 2)
    {
        if (_stricmp(argv[1], "-listen") == 0 && argc >= 4)
        {
            iConnectPort = atoi(argv[2]);
            iTransmitPort = atoi(argv[3]);
            method = 1;
        }
        else
            if (_stricmp(argv[1], "-tran") == 0 && argc >= 5)
            {
                iConnectPort = atoi(argv[2]);
                strncpy_s(sTransmitHost, argv[3], HOSTLEN);
                iTransmitPort = atoi(argv[4]);
                method = 2;
            }
            else
                if (_stricmp(argv[1], "-slave") == 0 && argc >= 6)
                {
                    strncpy_s(sConnectHost, argv[2], HOSTLEN);
                    iConnectPort = atoi(argv[3]);
                    strncpy_s(sTransmitHost, argv[4], HOSTLEN);
                    iTransmitPort = atoi(argv[5]);
                    method = 3;
                }
    }
    switch (method)
    {
    case 1:
        bind2bind(iConnectPort, iTransmitPort);
        break;
    case 2:
        bind2conn(iConnectPort, sTransmitHost, iTransmitPort);
        break;
    case 3:
        conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort);
        break;
    default:
        usage(argv[0]);
        break;
    }
    if (method)
    {
        closeallfd();
    }
    WSACleanup();
    return;
}
//************************************************************************************
// 
// print version message
//
//************************************************************************************
VOID ver()
{
    printf("======================== Port Forwarding Tool V%s =======================\r\n", VERSION);
    printf("========================== Improved by pandas ===========================\r\n\n");
}
//************************************************************************************
// 
// print usage message
//
//************************************************************************************
VOID usage(char* prog)
{
    printf("[Usage of Packet Transmit:]\r\n");
    printf(" %s - [-log logfile]\n\n", prog);
    printf("[option:]\n");
    printf(" -listen \n");
    printf(" -tran \n");
    printf(" -slave \n\n");
    return;
}
//************************************************************************************
// 
// test if is value 
//
//************************************************************************************
int testifisvalue(char *str)
{
    if (str == NULL) return(0);
    if (str[0] == ‘-‘) return(0);
    return(1);
}
//************************************************************************************
// 
// LocalHost:ConnectPort transmit to LocalHost:TransmitPort
//
//************************************************************************************
void bind2bind(int port1, int port2)
{
    SOCKET fd1, fd2, sockfd1, sockfd2;
    struct sockaddr_in client1, client2;
    int size1, size2;
    HANDLE hThread = NULL;
    transocket sock;
    DWORD dwThreadID;
    if ((fd1 = create_socket()) == 0) return;
    if ((fd2 = create_socket()) == 0) return;
    printf("[+] Listening port %d ......\r\n", port1);
    fflush(stdout);
    if (create_server(fd1, port1) == 0)
    {
        closesocket(fd1);
        return;
    }
    printf("[+] Listen OK!\r\n");
    printf("[+] Listening port %d ......\r\n", port2);
    fflush(stdout);
    if (create_server(fd2, port2) == 0)
    {
        closesocket(fd2);
        return;
    }
    printf("[+] Listen OK!\r\n");
    size1 = size2 = sizeof(struct sockaddr);
    while (1)
    {
        printf("[+] Waiting for Client on port:%d ......\r\n", port1);
        if ((sockfd1 = accept(fd1, (struct sockaddr *)&client1, &size1))<0)
        {
            printf("[-] Accept1 error.\r\n");
            continue;
        }
        printf("[+] Accept a Client on port %d from %s ......\r\n", port1, inet_ntoa(client1.sin_addr));
        printf("[+] Waiting another Client on port:%d....\r\n", port2);
        if ((sockfd2 = accept(fd2, (struct sockaddr *)&client2, &size2))<0)
        {
            printf("[-] Accept2 error.\r\n");
            closesocket(sockfd1);
            continue;
        }
        printf("[+] Accept a Client on port %d from %s\r\n", port2, inet_ntoa(client2.sin_addr));
        printf("[+] Accept Connect OK!\r\n");
        sock.fd1 = sockfd1;
        sock.fd2 = sockfd2;
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID);
        if (hThread == NULL)
        {
            TerminateThread(hThread, 0);
            return;
        }
        Sleep(1000);
        printf("[+] CreateThread OK!\r\n\n");
    }
}
//************************************************************************************
// 
// LocalHost:ConnectPort transmit to TransmitHost:TransmitPort
//
//************************************************************************************
void bind2conn(int port1, char *host, int port2)
{
    SOCKET sockfd, sockfd1, sockfd2;
    struct sockaddr_in remote;
    int size;
    char buffer[1024];
    HANDLE hThread = NULL;
    transocket sock;
    DWORD dwThreadID;
    if (port1 > 65535 || port1 < 1)
    {
        printf("[-] ConnectPort invalid.\r\n");
        return;
    }
    if (port2 > 65535 || port2 < 1)
    {
        printf("[-] TransmitPort invalid.\r\n");
        return;
    }
    memset(buffer, 0, 1024);
    if ((sockfd = create_socket()) == INVALID_SOCKET) return;
    if (create_server(sockfd, port1) == 0)
    {
        closesocket(sockfd);
        return;
    }
    size = sizeof(struct sockaddr);
    while (1)
    {
        printf("[+] Waiting for Client ......\r\n");
        if ((sockfd1 = accept(sockfd, (struct sockaddr *)&remote, &size))<0)
        {
            printf("[-] Accept error.\r\n");
            continue;
        }
        printf("[+] Accept a Client from %s:%d ......\r\n",
            inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
        if ((sockfd2 = create_socket()) == 0)
        {
            closesocket(sockfd1);
            continue;
        }
        printf("[+] Make a Connection to %s:%d ......\r\n", host, port2);
        fflush(stdout);
        if (client_connect(sockfd2, host, port2) == 0)
        {
            closesocket(sockfd2);
            sprintf_s(buffer, "[SERVER]connection to %s:%d error\r\n", host, port2);
            send(sockfd1, buffer, strlen(buffer), 0);
            memset(buffer, 0, 1024);
            closesocket(sockfd1);
            continue;
        }
        printf("[+] Connect OK!\r\n");
        sock.fd1 = sockfd1;
        sock.fd2 = sockfd2;
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID);
        if (hThread == NULL)
        {
            TerminateThread(hThread, 0);
            return;
        }
        Sleep(1000);
        printf("[+] CreateThread OK!\r\n\n");
    }
}
//************************************************************************************
// 
// ConnectHost:ConnectPort transmit to TransmitHost:TransmitPort
//
//************************************************************************************
void conn2conn(char *host1, int port1, char *host2, int port2)
{
    SOCKET sockfd1, sockfd2;
    HANDLE hThread = NULL;
    transocket sock;
    DWORD dwThreadID;
    fd_set fds;
    int l;
    char buffer[MAXSIZE];
    while (1)
    {
        /*
        while(connectnum)
        {
        if(connectnum < CONNECTNUM)
        {
        Sleep(10000);
        break;
        }
        else
        {
        Sleep(TIMEOUT*1000);
        continue;
        }
        }
        */
        if ((sockfd1 = create_socket()) == 0) return;
        if ((sockfd2 = create_socket()) == 0) return;
        printf("[+] Make a Connection to %s:%d....\r\n", host1, port1);
        fflush(stdout);
        if (client_connect(sockfd1, host1, port1) == 0)
        {
            closesocket(sockfd1);
            closesocket(sockfd2);
            continue;
        }
        // fix by bkbll 
        // if host1:port1 recved data, than connect to host2,port2
        l = 0;
        memset(buffer, 0, MAXSIZE);
        while (1)
        {
            FD_ZERO(&fds);
            FD_SET(sockfd1, &fds);
            if (select(sockfd1 + 1, &fds, NULL, NULL, NULL) == SOCKET_ERROR)
            {
                if (errno== WSAEINTR) continue;
                break;
            }
            if (FD_ISSET(sockfd1, &fds))
            {
                l = recv(sockfd1, buffer, MAXSIZE, 0);
                break;
            }
            Sleep(5);
        }
        if (l <= 0)
        {
            printf("[-] There is a error...Create a new connection.\r\n");
            continue;
        }
        while (1)
        {
            printf("[+] Connect OK!\r\n");
            printf("[+] Make a Connection to %s:%d....\r\n", host2, port2);
            fflush(stdout);
            if (client_connect(sockfd2, host2, port2) == 0)
            {
                closesocket(sockfd1);
                closesocket(sockfd2);
                continue;
            }
            if (send(sockfd2, buffer, l, 0) == SOCKET_ERROR)
            {
                printf("[-] Send failed.\r\n");
                continue;
            }
            l = 0;
            memset(buffer, 0, MAXSIZE);
            break;
        }
        printf("[+] All Connect OK!\r\n");
        sock.fd1 = sockfd1;
        sock.fd2 = sockfd2;
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)&sock, 0, &dwThreadID);
        if (hThread == NULL)
        {
            TerminateThread(hThread, 0);
            return;
        }
        // connectnum++;
        Sleep(1000);
        printf("[+] CreateThread OK!\r\n\n");
    }
}
//************************************************************************************
// 
// Socket Transmit to Socket
//
//************************************************************************************
void transmitdata(LPVOID data)
{
    SOCKET fd1, fd2;
    transocket *sock;
    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 tmpbuf[100];
    sock = (transocket *)data;
    fd1 = sock->fd1;
    fd2 = sock->fd2;
    memset(host1, 0, 20);
    memset(host2, 0, 20);
    memset(tmpbuf, 0, 100);
    structsize1 = sizeof(struct sockaddr);
    structsize2 = sizeof(struct sockaddr);
    if (getpeername(fd1, (struct sockaddr *)&client1, &structsize1)<0)
    {
        strcpy_s(host1, "fd1");
    }
    else
    {
        // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port));
        strcpy_s(host1, inet_ntoa(client1.sin_addr));
        port1 = ntohs(client1.sin_port);
    }
    if (getpeername(fd2, (struct sockaddr *)&client2, &structsize2)<0)
    {
        strcpy_s(host2, "fd2");
    }
    else
    {
        // printf("[+]got, ip:%s, port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port));
        strcpy_s(host2, inet_ntoa(client2.sin_addr));
        port2 = ntohs(client2.sin_port);
    }
    printf("[+] Start Transmit (%s:%d <-> %s:%d) ......\r\n\n", host1, port1, 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((UINT)fd1, &readfd);
        FD_SET((UINT)fd1, &writefd);
        FD_SET((UINT)fd2, &writefd);
        FD_SET((UINT)fd2, &readfd);
        result = select(maxfd, &readfd, &writefd, NULL, &timeset);
        if ((result<0) && (errno!= EINTR))
        {
            printf("[-] Select error.\r\n");
            break;
        }
        else if (result == 0)
        {
            printf("[-] Socket time out.\r\n");
            break;
        }
        if (FD_ISSET(fd1, &readfd))
        {
            /* must < MAXSIZE-totalread1, otherwise send_out1 will flow */
            if (totalread1 < MAXSIZE) {
                read1 = recv(fd1, read_in1, MAXSIZE - totalread1, 0);
                if ((read1 == SOCKET_ERROR) || (read1 == 0))
                {
                    printf("[-] Read fd1 data error,maybe close?\r\n");
                    break;
                }
                memcpy(send_out1 + totalread1, read_in1, read1);
                sprintf_s(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read1, host1, port1);
                printf(" Recv %5d bytes %16s:%d\r\n", read1, host1, port1);
                makelog(tmpbuf, strlen(tmpbuf));
                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 = send(fd2, send_out1 + sendcount1, totalread1, 0);
                if (send1 == 0)break;
                if ((send1<0) && (errno != EINTR))
                {
                    printf("[-] Send to fd2 unknow error.\r\n");
                    err = 1;
                    break;
                }
                if ((send1<0) && (errno == ENOSPC)) break;
                sendcount1 += send1;
                totalread1 -= send1;
                printf(" Send %5d bytes %16s:%d\r\n", send1, host2, port2);
            }
            if (err == 1) break;
            if ((totalread1>0) && (sendcount1>0))
            {
                /* move not sended data to start addr */
                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 = recv(fd2, read_in2, MAXSIZE - totalread2, 0);
                if (read2 == 0)break;
                if ((read2<0) && (errno != EINTR))
                {
                    printf("[-] Read fd2 data error,maybe close?\r\n\r\n");
                    break;
                }
                memcpy(send_out2 + totalread2, read_in2, read2);
                sprintf_s(tmpbuf, "\r\nRecv %5d bytes from %s:%d\r\n", read2, host2, port2);
                printf(" Recv %5d bytes %16s:%d\r\n", read2, host2, port2);
                makelog(tmpbuf, strlen(tmpbuf));
                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 = send(fd1, send_out2 + sendcount2, totalread2, 0);
                if (send2 == 0)break;
                if ((send2<0) && (errno != EINTR))
                {
                    printf("[-] Send to fd1 unknow error.\r\n");
                    err2 = 1;
                    break;
                }
                if ((send2<0) && (errno == ENOSPC)) break;
                sendcount2 += send2;
                totalread2 -= send2;
                printf(" Send %5d bytes %16s:%d\r\n", send2, host1, port1);
            }
            if (err2 == 1) break;
            if ((totalread2>0) && (sendcount2 > 0))
            {
                /* move not sended data to start addr */
                memcpy(send_out2, send_out2 + sendcount2, totalread2);
                memset(send_out2 + totalread2, 0, MAXSIZE - totalread2);
            }
            else
                memset(send_out2, 0, MAXSIZE);
        }
        Sleep(5);
    }
    closesocket(fd1);
    closesocket(fd2);
    // if(method == 3)
    // connectnum --;
    printf("\r\n[+] OK! I Closed The Two Socket.\r\n");
}
void getctrlc(int j)
{
    printf("\r\n[-] Received Ctrl+C\r\n");
    closeallfd();
    exit(0);
}
void closeallfd()
{
    int i;
    printf("[+] Let me exit ......\r\n");
    fflush(stdout);
    for (i = 3; i<256; i++)
    {
        closesocket(i);
    }
    if (fp != NULL)
    {
        fprintf(fp, "\r\n====== Exit ======\r\n");
        fclose(fp);
    }
    printf("[+] All Right!\r\n");
}
int create_socket()
{
    int sockfd;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd<0)
    {
        printf("[-] Create socket error.\r\n");
        return(0);
    }
    return(sockfd);
}
int create_server(int sockfd, int port)
{
    struct sockaddr_in srvaddr;
    int on = 1;
    memset(&srvaddr, 0, 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, (char*)&on, sizeof(on)); //so I can rebind the port
    if (bind(sockfd, (struct sockaddr *)&srvaddr, sizeof(struct sockaddr))<0)
    {
        printf("[-] Socket bind error.\r\n");
        return(0);
    }
    if (listen(sockfd, CONNECTNUM)<0)
    {
        printf("[-] Socket Listen error.\r\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);
    }
    memset(&cliaddr, 0, 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)
    {
        printf("[-] Connect error.\r\n");
        return(0);
    }
    return(1);
}
void makelog(char *buffer, int length)
{
    if (fp != NULL)
    {
        _write(_fileno(fp), buffer, length);
    }
}
技术图片

      注意,VS2013编译出来的程序在其他计算机上运行时,可能出现如下问题:

 技术图片

      这是由于需要MSVCR120.dll这个动态链接库来支持C函数库和C++的STL标准库。对于这个问题的解决方法是:可以把C/C++运行时库MSVCR120编译到你的exe里,这样你的程序复制到任意windows上就能直接运行了。项目 - 属性 - 配置属性 - C/C++ - 代码生成 - 运行库 - 多线程(/MT)。这样生成的EXE会增大一些,取决于你用到了多少库函数。具体设置如下图所示:

 技术图片

      最后,附上效果图一张:

 技术图片

      采用这种方式进行端口转发,其实速度有些慢,有时候会比较卡。对于Web端口转发工具及HTTP版本的LCX,后续我再分析一下。

      以上源码仅供学习之用,请不要用于非法途径。

 

LCX端口转发源码分析

标签:nal   void   ase   adf   must   ems   code   之间   rmi   

原文地址:https://www.cnblogs.com/ruingking/p/13344307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!