求教高手,编写一个linux下端口扫描程序
这里介绍netcat命令检查开放端口
netcat(或nc)是一种命令行工具,可以使用TCP或UDP协议跨网络连接读取和写入数据。使用netcat可以扫描单个端口或者端口范围。
比如,要扫描IP地址192.168.8.51在范围内远程计算机上打开的TCP端口,4000-4004可以进行以下命令:nc -z -v 192.168.8.51 4000-4004
1-z选项指示nc仅扫描打开的端口,并不发送任何数据,并且-v用于获取更多详细信息。运行结果如下:
sl@Li:~/Works/brochain/corsac$ nc -z -v 192.168.8.51 4000-4004
Connection to 192.168.8.51 4000 port [tcp/*] succeeded!
nc: connect to 192.168.8.51 port 4001 (tcp) failed: Connection refused
nc: connect to 192.168.8.51 port 4002 (tcp) failed: Connection refused
nc: connect to 192.168.8.51 port 4003 (tcp) failed: Connection refused
nc: connect to 192.168.8.51 port 4004 (tcp) failed: Connection refused
用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);
}
假设要判断某台机器打开了哪些tcp端口 该如何设计端口扫描程序
直接在命令提示符里面输入 netstat -a 就可以了。
netstat -a 查看开启了哪些端口,常用netstat -a
netstat -n 查看端口的网络连接情况,常用netstat -an
设计一个判断端口是否开启的扫描器程序的源代码怎么写?
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;
}
0条大神的评论