正在阅读:从源程序看原理:突破防火墙入侵内网从源程序看原理:突破防火墙入侵内网

2005-09-12 10:16 出处: 作者:eyas 责任编辑:moningfeng

/******
Module:TCPDataRedird.c
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的缓冲区
memcpy(szSendToClientBuff+iSTCBCS,szRecvFromTargetBuff,iRet);
//清空接收target返回数据缓冲区
memset(szRecvFromTargetBuff,0,BuffSize);
//刷新发送到client的数据缓冲区当前大小
iSTCBCS+=iRet;
}
//client可写,发送target返回数据到client
if(FD_ISSET(sock[0],&fdwrite))
{
//发送target返回数据到client
iLeft=iSTCBCS;
idx=0;
while(iLeft>0)
{
iRet=send(sock[0],&szSendToClientBuff[idx],iLeft,0);
if(iRet==SOCKET_ERROR)
{
printf("\nsend() to Client failed:%d",GetLastError());
break;
}
printf("\nsend %d bytes to Client",iRet);
iLeft-=iRet;
idx+=iRet;
}
//清空缓冲区
memset(szSendToClientBuff,0,BuffSize);
iSTCBCS=0;
}
}//end of select ret
Sleep(1);
}//end of while
return 0;
}

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品