本站推荐

如何突破TCP-IP过滤防火墙进入内网(2)

作 者:未知 时间:2005-07-01 点击:
loading...

if(sock[0]==INVALID_SOCKET)
{
printf("\naccept() failed:%d",GetLastError());
break;
}
printf("\nAccept client==>%s:%d",inet_ntoa(Client.sin_addr),
ntohs(Client.sin_port));
//create socket sTarget
sock[1]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[1]==INVALID_SOCKET)
{
printf("\nsocket() failed:%d",GetLastError());
__leave;
}
//connect to target port
if(connect(sock[1],(struct sockaddr
*)&Target,sizeof(Target))==SOCKET_ERROR)
{
printf("\nconnect() failed:%d",GetLastError());
__leave;
}
printf("\nconnect to target 3389 success!");
//创建两个线程进行数据转发
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待两个线程结束
WaitForSingleObject(hThreadC2T,INFINITE);
WaitForSingleObject(hThreadT2C,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[1]);
closesocket(sock[0]);
printf("\n\n*****************Connection
Close*******************\n\n");
}//end of sock外循环
}//end of try
__finally
{
if(sListen!=INVALID_SOCKET) closesocket(sListen);
if(sock[0]!=INVALID_SOCKET) closesocket(sock[0]);
if(sock[1]!=INVALID_SOCKET) closesocket(sock[1]);
if(hThreadC2T!=NULL) CloseHandle(hThreadC2T);
if(hThreadT2C!=NULL) CloseHandle(hThreadT2C);
WSACleanup();
}
return 0;
}
/**************************************************************
Module:TCPDataRedird.c
Date:2001/4/16
CopyRight(c) eyas
HomePage:www.patching.net
Thanks to shotgun
说明:TCP socket数据转发,sock[0]==>sClient sock[1]==>sTarget
******************************************************************/
#define BuffSize 20*1024 //缓冲区大小20k
//此函数负责从Client读取数据,然后转发给Target
DWORD WINAPI TCPDataC2T(SOCKET* sock)
{
int iRet,
ret=-1,//select 返回值
iLeft,
idx,
iSTTBCS=0;//STTBCS=SendToTargetBuffCurrentSize
char szSendToTargetBuff[BuffSize]=,
szRecvFromClientBuff[BuffSize]=;
fd_set fdread,fdwrite;
printf("\n\n*****************Connection
Active*******************\n\n");
while(1)
{
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_SET(sock[0],&fdread);
FD_SET(sock[1],&fdwrite);
if((ret=select(0,&fdread,&fdwrite,NULL,NULL))==SOCKET_ERROR)
{
printf("\nselect() failed:%d",GetLastError());
break;
}
//printf("\nselect() return value ret=%d",ret);
if(ret>0)
{
//sClinet可读,client有数据要发送过来
if(FD_ISSET(sock[0],&fdread))
{
//接收sock[0]发送来的数据
iRet=recv(sock[0],szRecvFromClientBuff,BuffSize,0);
if(iRet==SOCKET_ERROR)
{
printf("\nrecv() from sock[0] failed:%d",GetLastError());
break;
}
else if(iRet==0)
break;
printf("\nrecv %d bytes from sClinet.",iRet);
//把从client接收到的数据存添加到发往target的缓冲区
memcpy(szSendToTargetBuff iSTTBCS,szRecvFromClientBuff,iRet);
//刷新发往target的数据缓冲区当前buff大小
iSTTBCS =iRet;
//清空接收client数据的缓冲区
memset(szRecvFromClientBuff,0,BuffSize);
}
//sTarget可写,把从client接收到的数据发送到target
if(FD_ISSET(sock[1],&fdwrite))
{
//转发数据到target的3389端口
iLeft=iSTTBCS;
idx=0;
while(iLeft>0)
{
iRet=send(sock[1],&szSendToTargetBuff[idx],iLeft,0);
if(iRet==SOCKET_ERROR)
{
printf("\nsend() to target failed:%d",GetLastError());
break;
}
printf("\nsend %d bytes to target",iRet);
iLeft-=iRet;
idx =iRet;
}
//清空缓冲区
memset(szSendToTargetBuff,0,BuffSize);
//重置发往target的数据缓冲区当前buff大小
iSTTBCS=0;
}
}//end of select ret
Sleep(1);
}//end of data send & recv循环
return 0;
}
//此函数负责从target读取数据,然后发送给client
DWORD WINAPI TCPDataT2C(SOCKET* sock)
{
int iRet,
ret=-1,//select 返回值
iLeft,
idx,
iSTCBCS=0;//STCBCS=SendToClientBuffCurrentSize
char szRecvFromTargetBuff[BuffSize]=,
szSendToClientBuff[BuffSize]=;
fd_set fdread,fdwrite;

while(1)
{
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_SET(sock[0],&fdwrite);
FD_SET(sock[1],&fdread);
if((ret=select(0,&fdread,&fdwrite,NULL,NULL))==SOCKET_ERROR)
{
printf("\nselect() failed:%d",GetLastError());
break;
}
if(ret>0)
{
//sTarget可读,从target接收数据
if(FD_ISSET(sock[1],&fdread))
{
//接收target返回数据
iRet=recv(sock[1],szRecvFromTargetBuff,BuffSize,0);
if(iRet==SOCKET_ERROR)
{
printf("\nrecv() from target failed:%d",GetLastError());
break;
}
else if(iRet==0)
break;
printf("\nrecv %d bytes from target",iRet);
//把从target接收到的数据添加到发送到client的缓冲区

来源:http://www.2ky.cn/h/54/3437.htm
转载要求注明来源,请尊重作者版权!

关键字: 

会员登陆close X