设计一个判断端口是否开启的扫描器程序的源代码怎么写?
for(int i=0;i6;i++)
{
for(int j=0;j10;j++)
{
theport[j].rmt_host=rmt_host;
theport[j].p=port[i*10+j];
theport[j].n=j;
Thread[j]=AfxBeginThread(pScan,(LPVOID)theport[j]);
//启动端口扫描线程
hThread[j]=Thread[j]-m_hThread;
Sleep(1);
}
WaitForMultipleObjects(10,hThread,TRUE,120000);
}
//扫描模块代码
SOCKET sockfd;
SOCKADDR_IN addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd 0)
{
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(rmt_host);
int r = connect(sockfd,(struct sockaddr *) addr, sizeof(addr));
//尝试连接端口进行检测
closesocket(sockfd);
//连接返回值处理
if (r!=-1)
{
::PostMessage(hWnd,WM_DISPLAY,port,0);
}
//显示端口扫描结果
LONG CScanDlg::OnDisplay(LONG lParam, UINT wParam)
{
LPSERVENT bar;
CString open;
int p=lParam;
bar = getservbyport(htons(p),"tcp");
open.Format("\t%d号端口(%s)开放!\r\n",p,(bar == NULL) ? "未知" :
bar-s_name);
m_HistoryEdit.AppendString (open);
return 0;
}
用C++编写端口扫描程序
#include stdio.h
#include winsock.h
#define NETWORK_ERROR -1
#define NETWORK_OK 0
#define PORT_MIN 1
#define PORT_MAX 65535
HANDLE hThread;
DWORD hID;
char hostname[30];
int starting_port = 0;
int ending_port = 0;
int nopen = 0;
DWORD portscan();
int main(){
int ret;
WSADATA dat;
DWORD version;
version = MAKEWORD(2,2);
ret = WSAStartup(version,dat);
if(ret != 0){
printf("Error initializing Winsock.\n");
WSACleanup();
return NETWORK_ERROR;
}
if(ret == 0){
printf("Enter hostname:");
scanf("%s",hostname);
printf("Enter starting port:");
scanf("%d",starting_port);
if(starting_port PORT_MIN){
printf("Invalid port number.\n");
WSACleanup();
return NETWORK_ERROR;
}
printf("Enter ending port:");
scanf("%d",ending_port);
if(ending_port PORT_MAX){
printf("Invalid port number.\n");
WSACleanup();
return NETWORK_ERROR;
}
printf("\nScanning [%s]...\n",hostname);
hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)portscan,0,0,hID);
if(hThread == 0){
printf("Failed to create thread.\n");
WSACleanup();
return NETWORK_ERROR;
}
sleep(-1);
}
WSACleanup();
return NETWORK_OK;
}
DWORD portscan(){
int i,nret;
SOCKET thesocket;
LPHOSTENT hostent;
thesocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
hostent = gethostbyname((hostname));
for(i = starting_port;i ending_port+1;++i){
SOCKADDR_IN hostinfo;
hostinfo.sin_family = AF_INET;
hostinfo.sin_addr=*((LPIN_ADDR)*hostent-h_addr_list);
hostinfo.sin_port = htons(i);
nret = connect(thesocket,(LPSOCKADDR)hostinfo,sizeof(hostinfo));
if(nret == 0){
printf("\n\t%d\n",i);
++nopen;
}
}
printf("\nScan complete.\n\n");
printf("Number of ports opened = %d\n",nopen);
closesocket(thesocket);
}
100分求linux下C语言端口扫描代码
linux tcp udp 端口扫描源程序
#include sys/socket.h
#include netinet/in.h
#include arpa/inet.h
#include unistd.h
#include errno.h
#include netdb.h
#include stdio.h
#include string.h
#include netinet/ip_icmp.h
#include stdlib.h
#include signal.h
#include libxml/parser.h
#include libxml/tree.h
#define TRUE 1
#define FALSE 0
#define UDP "UDP"
#define TCP "TCP"
#define tcp "tcp"
#define udp "udp"
typedef struct _GsSockStru{
int fd;
int len;
struct sockaddr_in addr;
}GsSockStru;
static int tcptest( char ip[32], char port[20]);
static int udptest( char ip[32], char port[20]);
void sig_alrm( int signo );
static GsSockStru test_sock;
int
main( int argc, char** argv)
{
char string[64];
char port[20];
char pro[20];
char ip[32];
int res;
int i = 0;
int k = 0;
if( argc2 || argc2 )
{
printf("鍙傛暟涓嶆纭?-1\n");
return ( -1 );
}
strcpy( string, argv[1]);
while( *string )
{
if( string[i] == ':' )
break;
pro[k] = string[i];
k++;
i++;
}
pro[k] = '\0';
i++;
k = 0;
while( *string )
{
if( string[i] == ':')
break;
ip[k] = string[i];
k++;
i++;
}
ip[k] = '\0';
i++;
k=0;
while( *string )
{
if( string[i] == '\0')
break;
port[k] = string[i];
k++;
i++;
}
port[k] = '\0';
i++;
memset( test_sock, 0, sizeof( test_sock ) );
if ( ( strcmp( TCP, pro) != 0 ) ( strcmp( UDP, pro) != 0 ) ( strcmp( tcp, pro) != 0 ) ( strcmp( udp, pro) != 0 ))
{
printf ( "鍙傛暟涓嶆纭?锛?\n" );
return (-1);
}
if ( strcmp( TCP, pro) == 0 || strcmp( tcp, pro) == 0 )
res = tcptest( ip, port );
if ( strcmp( UDP, pro) == 0 || strcmp( udp, pro) == 0 )
res = udptest( ip, port );printf("%d\n",res);
return ( res );
}
int
tcptest( char ip[32], char port[20])
{
int res;
struct timeval tv;
test_sock.fd = socket( AF_INET, SOCK_STREAM, 0 );
if ( test_sock.fd 0 )
{
printf( "create socket failed -3 \n" );
return ( -3 );
}
memset( ( test_sock.addr ), 0, sizeof( test_sock.addr ) );
test_sock.addr.sin_family = AF_INET;
test_sock.addr.sin_port = htons( atoi( port ) );
inet_pton( AF_INET, ip, test_sock.addr.sin_addr );
test_sock.len = sizeof( struct sockaddr );
tv.tv_sec = 10;
tv.tv_usec = 0;
setsockopt( test_sock.fd, SOL_SOCKET, SO_RCVTIMEO,
(const char *)tv, sizeof( tv ) );
res = connect( test_sock.fd,
( struct sockaddr * )( ( test_sock.addr ) ),
test_sock.len );
if ( res 0 )
{
fprintf( stderr, "connect failed 0\n" );
close( test_sock.fd );
return FALSE;
}
close( test_sock.fd );
return TRUE;
}
int udptest( char ip[32], char port[20])
{
struct icmphdr *icmp_header;
struct sockaddr_in target_info;
int target_info_len;
fd_set read_fd;
int scan_port;
char recvbuf[5000];
struct sockaddr_in target_addr;
int icmp_socket;
int udp_socket;
struct timeval tv;
icmp_header = (struct icmphdr *)(recvbuf+sizeof(struct iphdr));
scan_port = atoi( port );
target_addr.sin_family = AF_INET;
inet_pton( AF_INET, ip, target_addr.sin_addr );
target_addr.sin_port = htons(scan_port);
if ((udp_socket=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("create socket failed -3\n");
return -3;
}
if ((icmp_socket=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1)
{
printf("Create raw socket failed -3\n");
return -3;
}
sendto(udp_socket,NULL,0,0,(void *)target_addr,sizeof(target_addr));
FD_ZERO(read_fd);
FD_SET(icmp_socket,read_fd);
tv.tv_sec = 1;
tv.tv_usec = 0;
select(FD_SETSIZE,read_fd,NULL,NULL,tv);
for (;;){
if (FD_ISSET(icmp_socket,read_fd))
{
target_info_len = sizeof(target_info);
recvfrom(icmp_socket,recvbuf,5000,0,
(struct sockaddr *)target_info,target_info_len);
if (target_info.sin_addr.s_addr == target_addr.sin_addr.s_addr
icmp_header-type == 3 icmp_header-code=12)
{
printf("Port %d : Close\n",scan_port);
return (0);
}
}
return (1) ;
}
}
0条大神的评论