但我有几个问题不明白:
1、如何收集端口数据?
2、如何确定端口号?
3、如何动态监听?
哪位大侠能帮小弟一把,我不胜感激,拜谢!
13 个解决方案
#1
网上源代码多的是。
#2
先学学socket
#3
这个问题要在底层监听才行。
可以用libpcap库
可以用libpcap库
#4
socket
#5
能详细点吗?大虾们
#6
使用套接字socket,首先创建一个socket类的对象,然后分别使用socket类的成员函数create,bind,listen,recieve,send,close,当然这是服务器端的,如果是客户端的只要create,connect就可以了,接下来你可以做其它想做的动作。
#7
能否给点source code?
#8
gz
#9
服务器端程序.
Linux or Solaris下运行,运行后在telnet主机地址,端口号5000.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#define PORT 5000
#define LINK_NUM 5
int main()
{
int sock_fd;
int new_fd;
int sin_size;
struct sockaddr_in host_addr;
struct sockaddr_in client_addr;
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(host_addr.sin_zero),8);
if(bind(sock_fd,(struct sockaddr*)&host_addr,sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
if(listen(sock_fd,LINK_NUM) == -1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size)) == -1)
{
perror("accept");
continue;
}
printf("server:got connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"hi, man !\n",10,0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
while(waitpid(-1,NULL,WNOHANG)>0);
return 0;
}
Linux or Solaris下运行,运行后在telnet主机地址,端口号5000.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#define PORT 5000
#define LINK_NUM 5
int main()
{
int sock_fd;
int new_fd;
int sin_size;
struct sockaddr_in host_addr;
struct sockaddr_in client_addr;
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(host_addr.sin_zero),8);
if(bind(sock_fd,(struct sockaddr*)&host_addr,sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
if(listen(sock_fd,LINK_NUM) == -1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size)) == -1)
{
perror("accept");
continue;
}
printf("server:got connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"hi, man !\n",10,0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
while(waitpid(-1,NULL,WNOHANG)>0);
return 0;
}
#10
To 楼上的:
楼主是要监听已经打开的端口,而不是写一个服务程序。
To 楼主:
我原来写过一个,不过要找找。
楼主是要监听已经打开的端口,而不是写一个服务程序。
To 楼主:
我原来写过一个,不过要找找。
#11
太长,只好分开贴
/////////////////////////////////////////////////////////////////
//
// Programme Name: IP & Port Scanner
// Functions: used to scan whether a port is opened on a IP addr
//
/////////////////////////////////////////////////////////////////
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
int TCount = 0; //MAX Thread numbers
int ThreadNumber = 0; //Realtime thread numbers
WORD PORT = 139; //PORT to scan
HANDLE ThreadEvent;
HANDLE FinishEvent;
BOOL FastMode = FALSE;
char startaddr[ 32 ];
char endaddr[ 32 ];
//-----------------------------------------------------------------------------
// Name: SaveRecord
// Desc: Log the useful informations
//-----------------------------------------------------------------------------
void SaveRecord( LPSTR fmt, ... )
{
char buff[ 256 ];
wvsprintf( buff, fmt, (LPSTR)(&fmt+1) );
FILE *fp = fopen( "c:\\iplog.txt","a+" );
if( fp != NULL )
{
fprintf( fp, "%s", buff );
fclose( fp );
}
}
//-----------------------------------------------------------------------------
// Name: CheckPort
// Desc: A thread function to check whether the given PORT is opened on the IP
//-----------------------------------------------------------------------------
DWORD WINAPI CheckPort( LPVOID lpParam )
{
//Save the IP address passed from main thread
char ipaddr[32];
strcpy( ipaddr, ( char* )lpParam );
printf( "Scanning %s\n", ipaddr );
//After saving the IP address, we can allow the main thread to modify the pointer
//of the passed-in IP address
if( FastMode )
SetEvent( FinishEvent );
//Address for connecting...
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr( ipaddr );
server.sin_port = htons( PORT );
//Create a TCP/IP socket
SOCKET s = socket( AF_INET, SOCK_STREAM, 0 );
//Get the start time of connection
DWORD dwTickCount = timeGetTime();
//If we can connect the IP address on the certain PORT,
//it means that the PORT is opened on this IP address...
if ( connect( s, (struct sockaddr *)&server, sizeof( server ) ) != SOCKET_ERROR )
{
//Special for trying to check the PORT 139, we will attempt
//to link the server to check out if it need no PASSWORD.
if( PORT == 139 )
{
//resource enumeration handle
HANDLE nethandle;
NETRESOURCE NetResource;
ZeroMemory( &NetResource, sizeof( NETRESOURCE ) );
char ippaddr2[32] ;
//we are trying to connect this IP, good...
sprintf( ippaddr2, "\\\\%s", ipaddr );
NetResource.lpRemoteName = ippaddr2;
//Try if we can enumerate the resource of it,
//if we can: This COMPUTER doesn't need any password for us to log on ^O^
//else: It need a PASSWORD, so boring~
DWORD result = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE, &NetResource, &nethandle);
if( result == NO_ERROR )
{
printf("The IP: %s opened PORT %d and need no Password!! ( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
SaveRecord("The IP: %s opened PORT %d and need no Password!!( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
WNetCloseEnum( nethandle ) ;
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
}
//decrease the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedDecrement( ( long* )&ThreadNumber );
//Tell the main thread that we finished!
SetEvent( ThreadEvent );
//Let the tiring socket go to sleep...
closesocket( s );
return 0;
}
/////////////////////////////////////////////////////////////////
//
// Programme Name: IP & Port Scanner
// Functions: used to scan whether a port is opened on a IP addr
//
/////////////////////////////////////////////////////////////////
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
int TCount = 0; //MAX Thread numbers
int ThreadNumber = 0; //Realtime thread numbers
WORD PORT = 139; //PORT to scan
HANDLE ThreadEvent;
HANDLE FinishEvent;
BOOL FastMode = FALSE;
char startaddr[ 32 ];
char endaddr[ 32 ];
//-----------------------------------------------------------------------------
// Name: SaveRecord
// Desc: Log the useful informations
//-----------------------------------------------------------------------------
void SaveRecord( LPSTR fmt, ... )
{
char buff[ 256 ];
wvsprintf( buff, fmt, (LPSTR)(&fmt+1) );
FILE *fp = fopen( "c:\\iplog.txt","a+" );
if( fp != NULL )
{
fprintf( fp, "%s", buff );
fclose( fp );
}
}
//-----------------------------------------------------------------------------
// Name: CheckPort
// Desc: A thread function to check whether the given PORT is opened on the IP
//-----------------------------------------------------------------------------
DWORD WINAPI CheckPort( LPVOID lpParam )
{
//Save the IP address passed from main thread
char ipaddr[32];
strcpy( ipaddr, ( char* )lpParam );
printf( "Scanning %s\n", ipaddr );
//After saving the IP address, we can allow the main thread to modify the pointer
//of the passed-in IP address
if( FastMode )
SetEvent( FinishEvent );
//Address for connecting...
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr( ipaddr );
server.sin_port = htons( PORT );
//Create a TCP/IP socket
SOCKET s = socket( AF_INET, SOCK_STREAM, 0 );
//Get the start time of connection
DWORD dwTickCount = timeGetTime();
//If we can connect the IP address on the certain PORT,
//it means that the PORT is opened on this IP address...
if ( connect( s, (struct sockaddr *)&server, sizeof( server ) ) != SOCKET_ERROR )
{
//Special for trying to check the PORT 139, we will attempt
//to link the server to check out if it need no PASSWORD.
if( PORT == 139 )
{
//resource enumeration handle
HANDLE nethandle;
NETRESOURCE NetResource;
ZeroMemory( &NetResource, sizeof( NETRESOURCE ) );
char ippaddr2[32] ;
//we are trying to connect this IP, good...
sprintf( ippaddr2, "\\\\%s", ipaddr );
NetResource.lpRemoteName = ippaddr2;
//Try if we can enumerate the resource of it,
//if we can: This COMPUTER doesn't need any password for us to log on ^O^
//else: It need a PASSWORD, so boring~
DWORD result = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE, &NetResource, &nethandle);
if( result == NO_ERROR )
{
printf("The IP: %s opened PORT %d and need no Password!! ( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
SaveRecord("The IP: %s opened PORT %d and need no Password!!( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
WNetCloseEnum( nethandle ) ;
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
}
//decrease the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedDecrement( ( long* )&ThreadNumber );
//Tell the main thread that we finished!
SetEvent( ThreadEvent );
//Let the tiring socket go to sleep...
closesocket( s );
return 0;
}
#12
//-----------------------------------------------------------------------------
// Name: main
// Desc: What do you think it will do ? ^O^
//-----------------------------------------------------------------------------
int main( void )
{
WSADATA wsd;
DWORD dwThreadId;
//printf the stupid notice...
printf(" IP port scanner v1.00.\n");
printf(" Brought to you by Strlee. 2001-8-23.\n\n");
printf("This programme will make a memory leak error when running.\n");
printf("Although it's not a big problem,you should save your works before starting.\n");
printf("The log will be written in the file c:\\iplog.txt.\n\n");
//Get the infors for scanning.
again:
printf("Please input the Start IP Address:\n");
scanf("%s", startaddr );
printf("Please input the End IP Address:\n");
scanf("%s", endaddr );
IN_ADDR sIP;
IN_ADDR eIP;
sIP.s_addr = inet_addr( startaddr );
eIP.s_addr = inet_addr( endaddr );
if( INADDR_NONE == sIP.s_addr || INADDR_NONE == eIP.s_addr )
{
printf( "Wrong IP number!!!\n\n");
goto again;
}
printf("Please input the PORT:\n");
scanf("%d", &PORT );
printf("Are you using a Wide-Band network and a High performance PC?(Y/N)\n");
printf("Exp: PIII 800 up and 256MB ram and IMPORTANT: a 128KBs/S network or faster.\n");
printf("A certain choice will make the scanning faster.\n");
getchar();
char choice;
scanf("%c", &choice );
if( _toupper( choice ) == 'Y' )
{
FastMode = TRUE;
printf("How many threads do you want to use( 1 ~ 512 )?\n");
scanf("%d", &TCount );
if( TCount > 512 )
TCount = 512;
}
else
FastMode = FALSE;
//begin the scanning
printf("Scanning now begins!\n");
//Store each BYTE of start and end IP address
int s1 = sIP.S_un.S_un_b.s_b1;
int s2 = sIP.S_un.S_un_b.s_b2;
int s3 = sIP.S_un.S_un_b.s_b3;
int s4 = sIP.S_un.S_un_b.s_b4;
int e1 = eIP.S_un.S_un_b.s_b1;
int e2 = eIP.S_un.S_un_b.s_b2;
int e3 = eIP.S_un.S_un_b.s_b3;
int e4 = eIP.S_un.S_un_b.s_b4;
if( e4 == 255 )
e4 = 254;
//create 2 events for Synchronizaing
ThreadEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
FinishEvent = CreateEvent( NULL, FALSE, TRUE, NULL );
//Start winsock 1.1
if ( WSAStartup( 0x101, &wsd ) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
char ipaddr[ 32 ];
//Scanning loop
//count down the IP and scan them with multi threads
while( s1 != e1 || s2 != e2 || s3 != e3 || s4 != e4 )
{
if( FastMode )
{
if( ThreadNumber > TCount )
WaitForSingleObject( ThreadEvent, INFINITE );
WaitForSingleObject( FinishEvent, INFINITE );
}
else
if( ThreadNumber > 256 )
WaitForSingleObject( ThreadEvent, INFINITE );
sprintf( ipaddr, "%d.%d.%d.%d", s1, s2 , s3, s4 );
//increase the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedIncrement( ( long* )&ThreadNumber );
//Create the scanning thread
if( NULL == CreateThread( NULL, 0, CheckPort, (LPVOID)ipaddr, 0, &dwThreadId ) )
{
printf("Not enough memory to creat any more threads.\n");
while( ThreadNumber != 0 )
{
printf( "Waiting for %d threads to get end.\n", ThreadNumber );
WaitForSingleObject( ThreadEvent, INFINITE );
}
CreateThread( NULL, 0, CheckPort, (LPVOID)ipaddr, 0, &dwThreadId );
}
//We need to sleep for a while to prevent crashing the computer...
if( !FastMode )
Sleep( 30 );
else
Sleep( 10 );
//Here counts down the IP
if( s4 < 254 )
s4 ++;
else
{
s4 = 1;
if( s3 < 254 )
s3 ++;
else
{
s3 = 0;
if( s2 < 254 )
s2 ++;
else
{
s2 = 0;
if( s1 < 254 )
s1 ++;
}
}
}
}
//We need to wait for all threads to get ended.
while( ThreadNumber != 0 )
{
printf( "Waiting for %d threads to get end.\n", ThreadNumber );
WaitForSingleObject( ThreadEvent, INFINITE );
}
//Well done
printf("Scanning is ended!\n");
printf("The log has be written in the file c:\\iplog.txt.\n");
//clean the tiring winsock
WSACleanup();
return 0;
}
#13
to wwwunix(木易)
能发给我吗谢谢
Sniper0000@etang.com
能发给我吗谢谢
Sniper0000@etang.com
#1
网上源代码多的是。
#2
先学学socket
#3
这个问题要在底层监听才行。
可以用libpcap库
可以用libpcap库
#4
socket
#5
能详细点吗?大虾们
#6
使用套接字socket,首先创建一个socket类的对象,然后分别使用socket类的成员函数create,bind,listen,recieve,send,close,当然这是服务器端的,如果是客户端的只要create,connect就可以了,接下来你可以做其它想做的动作。
#7
能否给点source code?
#8
gz
#9
服务器端程序.
Linux or Solaris下运行,运行后在telnet主机地址,端口号5000.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#define PORT 5000
#define LINK_NUM 5
int main()
{
int sock_fd;
int new_fd;
int sin_size;
struct sockaddr_in host_addr;
struct sockaddr_in client_addr;
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(host_addr.sin_zero),8);
if(bind(sock_fd,(struct sockaddr*)&host_addr,sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
if(listen(sock_fd,LINK_NUM) == -1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size)) == -1)
{
perror("accept");
continue;
}
printf("server:got connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"hi, man !\n",10,0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
while(waitpid(-1,NULL,WNOHANG)>0);
return 0;
}
Linux or Solaris下运行,运行后在telnet主机地址,端口号5000.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#define PORT 5000
#define LINK_NUM 5
int main()
{
int sock_fd;
int new_fd;
int sin_size;
struct sockaddr_in host_addr;
struct sockaddr_in client_addr;
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(host_addr.sin_zero),8);
if(bind(sock_fd,(struct sockaddr*)&host_addr,sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
if(listen(sock_fd,LINK_NUM) == -1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size)) == -1)
{
perror("accept");
continue;
}
printf("server:got connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"hi, man !\n",10,0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
while(waitpid(-1,NULL,WNOHANG)>0);
return 0;
}
#10
To 楼上的:
楼主是要监听已经打开的端口,而不是写一个服务程序。
To 楼主:
我原来写过一个,不过要找找。
楼主是要监听已经打开的端口,而不是写一个服务程序。
To 楼主:
我原来写过一个,不过要找找。
#11
太长,只好分开贴
/////////////////////////////////////////////////////////////////
//
// Programme Name: IP & Port Scanner
// Functions: used to scan whether a port is opened on a IP addr
//
/////////////////////////////////////////////////////////////////
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
int TCount = 0; //MAX Thread numbers
int ThreadNumber = 0; //Realtime thread numbers
WORD PORT = 139; //PORT to scan
HANDLE ThreadEvent;
HANDLE FinishEvent;
BOOL FastMode = FALSE;
char startaddr[ 32 ];
char endaddr[ 32 ];
//-----------------------------------------------------------------------------
// Name: SaveRecord
// Desc: Log the useful informations
//-----------------------------------------------------------------------------
void SaveRecord( LPSTR fmt, ... )
{
char buff[ 256 ];
wvsprintf( buff, fmt, (LPSTR)(&fmt+1) );
FILE *fp = fopen( "c:\\iplog.txt","a+" );
if( fp != NULL )
{
fprintf( fp, "%s", buff );
fclose( fp );
}
}
//-----------------------------------------------------------------------------
// Name: CheckPort
// Desc: A thread function to check whether the given PORT is opened on the IP
//-----------------------------------------------------------------------------
DWORD WINAPI CheckPort( LPVOID lpParam )
{
//Save the IP address passed from main thread
char ipaddr[32];
strcpy( ipaddr, ( char* )lpParam );
printf( "Scanning %s\n", ipaddr );
//After saving the IP address, we can allow the main thread to modify the pointer
//of the passed-in IP address
if( FastMode )
SetEvent( FinishEvent );
//Address for connecting...
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr( ipaddr );
server.sin_port = htons( PORT );
//Create a TCP/IP socket
SOCKET s = socket( AF_INET, SOCK_STREAM, 0 );
//Get the start time of connection
DWORD dwTickCount = timeGetTime();
//If we can connect the IP address on the certain PORT,
//it means that the PORT is opened on this IP address...
if ( connect( s, (struct sockaddr *)&server, sizeof( server ) ) != SOCKET_ERROR )
{
//Special for trying to check the PORT 139, we will attempt
//to link the server to check out if it need no PASSWORD.
if( PORT == 139 )
{
//resource enumeration handle
HANDLE nethandle;
NETRESOURCE NetResource;
ZeroMemory( &NetResource, sizeof( NETRESOURCE ) );
char ippaddr2[32] ;
//we are trying to connect this IP, good...
sprintf( ippaddr2, "\\\\%s", ipaddr );
NetResource.lpRemoteName = ippaddr2;
//Try if we can enumerate the resource of it,
//if we can: This COMPUTER doesn't need any password for us to log on ^O^
//else: It need a PASSWORD, so boring~
DWORD result = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE, &NetResource, &nethandle);
if( result == NO_ERROR )
{
printf("The IP: %s opened PORT %d and need no Password!! ( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
SaveRecord("The IP: %s opened PORT %d and need no Password!!( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
WNetCloseEnum( nethandle ) ;
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
}
//decrease the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedDecrement( ( long* )&ThreadNumber );
//Tell the main thread that we finished!
SetEvent( ThreadEvent );
//Let the tiring socket go to sleep...
closesocket( s );
return 0;
}
/////////////////////////////////////////////////////////////////
//
// Programme Name: IP & Port Scanner
// Functions: used to scan whether a port is opened on a IP addr
//
/////////////////////////////////////////////////////////////////
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
int TCount = 0; //MAX Thread numbers
int ThreadNumber = 0; //Realtime thread numbers
WORD PORT = 139; //PORT to scan
HANDLE ThreadEvent;
HANDLE FinishEvent;
BOOL FastMode = FALSE;
char startaddr[ 32 ];
char endaddr[ 32 ];
//-----------------------------------------------------------------------------
// Name: SaveRecord
// Desc: Log the useful informations
//-----------------------------------------------------------------------------
void SaveRecord( LPSTR fmt, ... )
{
char buff[ 256 ];
wvsprintf( buff, fmt, (LPSTR)(&fmt+1) );
FILE *fp = fopen( "c:\\iplog.txt","a+" );
if( fp != NULL )
{
fprintf( fp, "%s", buff );
fclose( fp );
}
}
//-----------------------------------------------------------------------------
// Name: CheckPort
// Desc: A thread function to check whether the given PORT is opened on the IP
//-----------------------------------------------------------------------------
DWORD WINAPI CheckPort( LPVOID lpParam )
{
//Save the IP address passed from main thread
char ipaddr[32];
strcpy( ipaddr, ( char* )lpParam );
printf( "Scanning %s\n", ipaddr );
//After saving the IP address, we can allow the main thread to modify the pointer
//of the passed-in IP address
if( FastMode )
SetEvent( FinishEvent );
//Address for connecting...
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr( ipaddr );
server.sin_port = htons( PORT );
//Create a TCP/IP socket
SOCKET s = socket( AF_INET, SOCK_STREAM, 0 );
//Get the start time of connection
DWORD dwTickCount = timeGetTime();
//If we can connect the IP address on the certain PORT,
//it means that the PORT is opened on this IP address...
if ( connect( s, (struct sockaddr *)&server, sizeof( server ) ) != SOCKET_ERROR )
{
//Special for trying to check the PORT 139, we will attempt
//to link the server to check out if it need no PASSWORD.
if( PORT == 139 )
{
//resource enumeration handle
HANDLE nethandle;
NETRESOURCE NetResource;
ZeroMemory( &NetResource, sizeof( NETRESOURCE ) );
char ippaddr2[32] ;
//we are trying to connect this IP, good...
sprintf( ippaddr2, "\\\\%s", ipaddr );
NetResource.lpRemoteName = ippaddr2;
//Try if we can enumerate the resource of it,
//if we can: This COMPUTER doesn't need any password for us to log on ^O^
//else: It need a PASSWORD, so boring~
DWORD result = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONNECTABLE, &NetResource, &nethandle);
if( result == NO_ERROR )
{
printf("The IP: %s opened PORT %d and need no Password!! ( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
SaveRecord("The IP: %s opened PORT %d and need no Password!!( %d ms )\n", ipaddr, PORT, timeGetTime() - dwTickCount );
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
WNetCloseEnum( nethandle ) ;
}
else
{
printf( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
SaveRecord( "The IP: %s opened PORT %d. ( %d ms)\n", ipaddr, PORT , timeGetTime() - dwTickCount );
}
}
//decrease the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedDecrement( ( long* )&ThreadNumber );
//Tell the main thread that we finished!
SetEvent( ThreadEvent );
//Let the tiring socket go to sleep...
closesocket( s );
return 0;
}
#12
//-----------------------------------------------------------------------------
// Name: main
// Desc: What do you think it will do ? ^O^
//-----------------------------------------------------------------------------
int main( void )
{
WSADATA wsd;
DWORD dwThreadId;
//printf the stupid notice...
printf(" IP port scanner v1.00.\n");
printf(" Brought to you by Strlee. 2001-8-23.\n\n");
printf("This programme will make a memory leak error when running.\n");
printf("Although it's not a big problem,you should save your works before starting.\n");
printf("The log will be written in the file c:\\iplog.txt.\n\n");
//Get the infors for scanning.
again:
printf("Please input the Start IP Address:\n");
scanf("%s", startaddr );
printf("Please input the End IP Address:\n");
scanf("%s", endaddr );
IN_ADDR sIP;
IN_ADDR eIP;
sIP.s_addr = inet_addr( startaddr );
eIP.s_addr = inet_addr( endaddr );
if( INADDR_NONE == sIP.s_addr || INADDR_NONE == eIP.s_addr )
{
printf( "Wrong IP number!!!\n\n");
goto again;
}
printf("Please input the PORT:\n");
scanf("%d", &PORT );
printf("Are you using a Wide-Band network and a High performance PC?(Y/N)\n");
printf("Exp: PIII 800 up and 256MB ram and IMPORTANT: a 128KBs/S network or faster.\n");
printf("A certain choice will make the scanning faster.\n");
getchar();
char choice;
scanf("%c", &choice );
if( _toupper( choice ) == 'Y' )
{
FastMode = TRUE;
printf("How many threads do you want to use( 1 ~ 512 )?\n");
scanf("%d", &TCount );
if( TCount > 512 )
TCount = 512;
}
else
FastMode = FALSE;
//begin the scanning
printf("Scanning now begins!\n");
//Store each BYTE of start and end IP address
int s1 = sIP.S_un.S_un_b.s_b1;
int s2 = sIP.S_un.S_un_b.s_b2;
int s3 = sIP.S_un.S_un_b.s_b3;
int s4 = sIP.S_un.S_un_b.s_b4;
int e1 = eIP.S_un.S_un_b.s_b1;
int e2 = eIP.S_un.S_un_b.s_b2;
int e3 = eIP.S_un.S_un_b.s_b3;
int e4 = eIP.S_un.S_un_b.s_b4;
if( e4 == 255 )
e4 = 254;
//create 2 events for Synchronizaing
ThreadEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
FinishEvent = CreateEvent( NULL, FALSE, TRUE, NULL );
//Start winsock 1.1
if ( WSAStartup( 0x101, &wsd ) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
char ipaddr[ 32 ];
//Scanning loop
//count down the IP and scan them with multi threads
while( s1 != e1 || s2 != e2 || s3 != e3 || s4 != e4 )
{
if( FastMode )
{
if( ThreadNumber > TCount )
WaitForSingleObject( ThreadEvent, INFINITE );
WaitForSingleObject( FinishEvent, INFINITE );
}
else
if( ThreadNumber > 256 )
WaitForSingleObject( ThreadEvent, INFINITE );
sprintf( ipaddr, "%d.%d.%d.%d", s1, s2 , s3, s4 );
//increase the ThreadNumber, and make sure it's not being acessed by other threads
InterlockedIncrement( ( long* )&ThreadNumber );
//Create the scanning thread
if( NULL == CreateThread( NULL, 0, CheckPort, (LPVOID)ipaddr, 0, &dwThreadId ) )
{
printf("Not enough memory to creat any more threads.\n");
while( ThreadNumber != 0 )
{
printf( "Waiting for %d threads to get end.\n", ThreadNumber );
WaitForSingleObject( ThreadEvent, INFINITE );
}
CreateThread( NULL, 0, CheckPort, (LPVOID)ipaddr, 0, &dwThreadId );
}
//We need to sleep for a while to prevent crashing the computer...
if( !FastMode )
Sleep( 30 );
else
Sleep( 10 );
//Here counts down the IP
if( s4 < 254 )
s4 ++;
else
{
s4 = 1;
if( s3 < 254 )
s3 ++;
else
{
s3 = 0;
if( s2 < 254 )
s2 ++;
else
{
s2 = 0;
if( s1 < 254 )
s1 ++;
}
}
}
}
//We need to wait for all threads to get ended.
while( ThreadNumber != 0 )
{
printf( "Waiting for %d threads to get end.\n", ThreadNumber );
WaitForSingleObject( ThreadEvent, INFINITE );
}
//Well done
printf("Scanning is ended!\n");
printf("The log has be written in the file c:\\iplog.txt.\n");
//clean the tiring winsock
WSACleanup();
return 0;
}
#13
to wwwunix(木易)
能发给我吗谢谢
Sniper0000@etang.com
能发给我吗谢谢
Sniper0000@etang.com