?? code for port scanning in lan(局域網(wǎng)搜索端口掃描源碼).txt
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
#define ScanSpeed 20 //主線程等待時間
#define RetryCount 3 //創(chuàng)建線程失敗重試次數(shù)
/*-------------- 函數(shù)原型---------------*/
void usage(char * path);
DWORD WINAPI scan(LPVOID lp);
typedef struct
{
unsigned long ip;
USHORT port;
}infor;
int threadcount;
int findcount;
// 主函數(shù)入口
int main(int argc,char *argv[])
{
int ret;
WSADATA wsa;
infor infor1;
int MaxThread;
int nowAddr, port;
int min,max;
HANDLE h;
DWORD dwThreadID;
unsigned long startAddr, endAddr;
int reTryTime = 0;//創(chuàng)建線程失敗已經(jīng)重試次數(shù)
DWORD scantime;
if( argc != 6 )
{
usage( argv[0] );
exit(-1);
}
MaxThread = atoi(argv[5]);
ret = WSAStartup( 0x0202, &wsa ); // 初始化winsock.dll
if( ret )
{
printf( "WSAStartup failed! %d\n", WSAGetLastError() );
exit(0);
}
// 定義掃描開始結(jié)束的端口
min = atoi( argv[3] );
max = atoi( argv[4] );
startAddr = ntohl( inet_addr(argv[1]) );
endAddr = ntohl( inet_addr(argv[2]) );
// 判斷端口是否在0-65535之間
if( (min<0) || (max>65535) )
{
printf("\n");
printf("Ports must between 0-65535!\n");
usage( argv[0] );
exit(-1);
}
threadcount=0; //線程計數(shù)器
findcount=0;
scantime = GetTickCount();
for( nowAddr = startAddr; nowAddr <= endAddr; nowAddr ++ )
{
infor1.ip = nowAddr;
for( port = min; port <= max; port ++ )
{
infor1.port = port;
//應(yīng)該是while而不是if
while( threadcount >= MaxThread )
{
Sleep( ScanSpeed );
}
h = CreateThread( NULL, 0, scan, &infor1, 0, &dwThreadID );
//創(chuàng)建線程失敗且重試次數(shù)未達(dá)到最大,則重新創(chuàng)建,否則漏報
while( (h == NULL) && reTryTime < RetryCount )
{
printf("\nCreateThread error!\n");
h = CreateThread( NULL, 0, scan, &infor1, 0, &dwThreadID );
}
if( h != NULL )
{
threadcount ++;
dwThreadID ++;
CloseHandle( h );
}
Sleep( ScanSpeed );
// printf( "Current thread is %d\n", threadcount );
}
}
while( TRUE )
{
if( threadcount > 0 )
{
Sleep( ScanSpeed );
}
else
{
break;
}
}
printf("\n\nScan End! Find PortCount:%d",findcount);
printf("\nTotal %d s\n", (GetTickCount()-scantime)/1000 );
WSACleanup();
return 0;
}
/*--------------------幫助函數(shù)----------------------*/
void usage( char * path )
{
printf("\n----------------------------------------");
printf("\n Code By zhouzhen ");
printf("\n USAGE:%s <ip1> <ip2> <port1> <port2> <MaxThread>",path);
printf("\n----------------------------------------\n\n");
}
DWORD WINAPI scan( LPVOID lp )
{
SOCKET sock;
int ret;
struct sockaddr_in sin;
int ntime = 1000;
infor *lpinfor = (infor*)lp;
sock = socket( AF_INET, SOCK_STREAM, 0 ); //創(chuàng)建socket
if( sock == INVALID_SOCKET )
{
printf( "socket error: %d\n", WSAGetLastError() );
threadcount --;//失敗要減少當(dāng)前線程數(shù)量
return 0;
}
ret = setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&ntime, sizeof(ntime) ); //設(shè)置超時1s
if( ret != 0 )
{
printf( "setsockopt failed: %d\n", WSAGetLastError() );
}
memset( &sin, 0, sizeof(sin) );
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(lpinfor->ip);
sin.sin_port = htons(lpinfor->port);
ret = connect( sock, (struct sockaddr*)&sin, sizeof(sin) );//connect連接
if(!ret)
{
findcount ++;
printf("\nHost %s -> %d\n", (char*)inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) );
}
closesocket(sock);
threadcount--;
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -