#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
u_long gipbeg,gipend,gipcur;
HFILE ghf;
CRITICAL_SECTION gcse;
CRITICAL_SECTION gcseip;
int gtimeout;
unsigned short gptmd,gptbeg,gptsz;
unsigned short gprot[10];
HANDLE ghsdtout;
int mystrlen(char *str)
{
int len = 0;
while(*str++!='\0'){len++;}
return len;
}
int myatoi(char *sin)
{
int nb = 0;
while(*sin<='9'&&*sin>='0')
{
nb*=10;
nb+=(*sin - '0');
sin++;
}
return nb;
}
char *myitoa(int nb)
{
static char sret[11];
int n=0;
int i=9;
do
{
n=nb%10;
sret[i]='0'+n;
i--;
}
while(nb/=10);
return &sret[i+1];
}
void myprintf(char *sout)
{
DWORD dd;
WriteFile(ghsdtout,sout,mystrlen(sout),&dd,NULL);
}
void myfilewrite(char* sout)
{
DWORD dd;
WriteFile((HANDLE)ghf,sout,mystrlen(sout),&dd,NULL);
}
void getprotfromcl(char* str)
{
char *stmp = str;
while(*str++!='\0')
{
if(*str=='-')
{
gptmd = 1;
gptbeg=myatoi(stmp);
while(*stmp++!='-'){};
gptsz=myatoi(stmp)-gptbeg;
return;
}
}
int i=0;
gptmd = 2;
while(*stmp!='\0')
{
gprot[i]=myatoi(stmp);
i++;
if(i>9)
break;
while(*stmp++!=','&&*stmp!='\0'){};
}
}
int getprot(unsigned short &prot)
{
static unsigned short pos = 0;
int ret=0;
if(gptmd==1)
{
if(pos<=gptsz)
{
ret = 0;
}
else
{
pos = 0;
ret = 1;
}
prot = gptbeg+pos++;
return ret;
}
else if(gptmd == 2)
{
if(gprot[pos]!=0)
{
ret = 0;
}
else
{
pos = 0;
ret = 1;
}
prot = gprot[pos++];
return ret;
}
return 0;
}
int getip(u_long &ip,unsigned short &prot)
{
int ret=1;
EnterCriticalSection(&gcseip);
if(gipcur>gipend)
ret = 0;
else
{
gipcur += getprot(prot);
if(!((gipcur+1)&0xff))
gipcur+=2;
if(gipcur>gipend)
ret = 0;
ip=gipcur;
}
LeaveCriticalSection(&gcseip);
return ret;
}
void saveip(u_long ip,unsigned short prot)
{
char *sip,*sprot;
in_addr addr;
addr.S_un.S_addr = htonl(ip);
EnterCriticalSection(&gcse);
sip = inet_ntoa(addr);
sprot=myitoa(prot);
myfilewrite(sip);
myfilewrite(" ");
myfilewrite(sprot);
myfilewrite("\r\n");
myprintf(sip);
myprintf(" ");
myprintf(sprot);
myprintf("\r\n");
LeaveCriticalSection(&gcse);
}
int checkipprot(u_long ip,int prot,int timeout)
{
int ret;
int len = sizeof(int);
int error=-1;
SOCKET sc;
sockaddr_in addr;
u_long ul;
timeval time;
fd_set r;
sc = socket(AF_INET,SOCK_STREAM,0);
if(sc==SOCKET_ERROR)
return sc;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(0);
ret = bind(sc,(sockaddr *)&addr,sizeof(addr));
if(ret <0)
goto CleanUp;
addr.sin_addr.s_addr = htonl(ip);
addr.sin_port = htons(prot);
ret = ioctlsocket(sc, FIONBIO, &ul);
if(ret <0)
goto CleanUp;
connect(sc,(sockaddr *)&addr,sizeof(addr));
FD_ZERO(&r);
FD_SET(sc, &r);
time.tv_sec=0;
time.tv_usec=timeout*1000;
ret = select(sc, 0, &r, 0, &time);
if(ret>0)
{
getsockopt(sc, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
ret = (error==0)?1:0;
}
CleanUp:
closesocket(sc);
return ret;
}
DWORD WINAPI thdfun(LPVOID pParam)
{
u_long ip;
unsigned short prot;
while(getip(ip,prot))
{
if(checkipprot(ip,prot,gtimeout)>0)
{
saveip(ip,prot);
}
}
return 0;
}
char *gCmdline;
char *GetArgv(int i)
{
static char argv[80];
int n=0;
int ft=0;
char *sCmd=gCmdline;
while(*sCmd++==' '){}
sCmd--;
while(i!=0&&*sCmd!='\0')
{
ft=0;
while(*sCmd++==' '){ft++;}
if(ft)
i--;
}
sCmd--;
while(*sCmd!=' '&&*sCmd!='\0')
{
argv[n++]=*sCmd++;
}
argv[n]='\0';
return argv;
}
int GetArgc()
{
int argc=1;
int ft=0;
char *sCmd=gCmdline;
while(*sCmd++==' '){}
sCmd--;
while(*sCmd++!='\0')
{
ft=0;
while(*sCmd++==' '){ft++;}
sCmd--;
if(ft)
argc++;
}
return argc;
}
int main()
{
//得到命令行参数个数
gCmdline = GetCommandLineA();
int argc = GetArgc();
//初始化myprintf
ghsdtout = GetStdHandle(STD_OUTPUT_HANDLE);
//初始化全局变量
int nthd =100;
gtimeout = 2000;
gipend = 0;
char *spath=NULL;
ghf = (HFILE)INVALID_HANDLE_VALUE;
if(argc<2)
{
useage:
myprintf("Useage:\r\nmmscan.exe -s ((IP Host)|(IP begin)-(IP end)) -p (prot:80,33,44|135-1433) -T (thread:100) -t (timeout:2000 millisecond) -s (save result file:res.txt)\r\n");
myprintf("For example:\r\n mmscan.exe -a 192.168.0.0-192.169.0.0 -p 135 -T 250 -s c:\\result.txt \r\n\r\n");
myprintf(" mmscan.exe -a 127.0.0.1 -p 1-65535 -T 100 -t 1000\r\n\r\n");
myprintf(" mmscan.exe -a 172.0.0.0-173.0.0.0 -p 135,445,80,139 -T 250 -t 2000 -s res.txt\r\n\r\n");
return 0;
}
int cc = 0;
char *stmp,*stmp2;
while(cc<argc-1)
{
cc++;
stmp = GetArgv(cc);
if(stmp[0]!='-')
{
continue;
}
switch(stmp[1])
{
case 'a':
cc++;
if(cc+1>argc)
goto useage;
stmp = GetArgv(cc);
stmp2=stmp;
while(*stmp++!='-'&&*stmp!='\0'){}
if(*stmp)
{
*(stmp-1)='\0';
gipbeg = inet_addr(stmp2);
gipend = inet_addr(stmp);
}
else
{
gipbeg = inet_addr(GetArgv(cc));
gipend = gipbeg;
}
gipbeg = htonl(gipbeg);
gipend = htonl(gipend);
if(gipbeg > gipend)
goto useage;
gipcur = gipbeg;
break;
case 't':
cc++;
if(cc>argc-1)
goto useage;
gtimeout = myatoi(GetArgv(cc));
if(gtimeout > 75000 || gtimeout < 50)
gtimeout = 2000;
break;
case 'T':
cc++;
if(cc>argc-1)
goto useage;
nthd = myatoi(GetArgv(cc));
if(nthd > 500 || nthd < 1)
nthd = 100;
break;
case 'p':
cc++;
if(cc>argc-1)
goto useage;
getprotfromcl(GetArgv(cc));
break;
case 's':
cc++;
if(cc>argc-1)
goto useage;
spath = GetArgv(cc);
break;
default:
goto useage;
break;
}
}
if(gipend == 0)
goto useage;
OFSTRUCT fbuf;
ghf = OpenFile(spath,&fbuf,OF_WRITE|OF_CREATE);
if((HANDLE)ghf==INVALID_HANDLE_VALUE)
ghf = OpenFile("res.txt",&fbuf,OF_WRITE|OF_CREATE);
if((HANDLE)ghf==INVALID_HANDLE_VALUE)
{
myprintf("error! \r\n");
return 0;
}
InitializeCriticalSection(&gcse);
InitializeCriticalSection(&gcseip);
//myprintf("************Mini Prot Scan!*************\r\n\r\n");
//myprintf("************By Redne\***************r\n\r\n");
myprintf("\r\n\r\n************http://www.cfxy.net//*******\r\n\r\n");
myprintf("Scaning!\r\n");
WSADATA wsd;
WSAStartup(MAKEWORD(2,2),&wsd);
DWORD thdid;
HANDLE phd[512];
for(int i=0;i<nthd;i++)
{
phd[i]=CreateThread(NULL,0,thdfun,0,0,&thdid);
//Sleep(50);
}
for(int j=0;j<nthd;j++)
{
WaitForSingleObject(phd[j],INFINITE);
}
myprintf("Scan Over! \r\n");
return 0;
}