端口扫描程序的设计与实现_tcp端口扫描程序代码

hacker|
219

tcp connect 远程端口扫描

你贴的代码不够全面,检查你设置IP处是否正确,以及你扫描的计算机是否开了防火墙.

补充:关掉防火墙,开防火墙的话,防火墙将阻止一些端口的TCPConnect

求教高手,编写一个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++ TCP端口扫描

差不多就是这样的吧

#include stdio.h

#include winsock.h

#pragma comment(lib,"wsock32.lib")

int main(int argc, char **argv)

{

SOCKET sd_client;

u_short iPortStart, iPortEnd, port;

struct sockaddr_in addr_srv;

char *pszHost;

WSADATA wsaData;

WORD wVersionRequested;

int err;

switch(argc)

{

case 2:

iPortStart = 0;

iPortEnd = 65535;

pszHost =argv[1];

break;

case 3:

iPortStart = iPortEnd = atoi(argv[2]);

pszHost =argv[1];

break;

case 4:

iPortStart = atoi(argv[2]);

iPortEnd = atoi(argv[3]);

pszHost =argv[1];

break;

default:

printf("正确的命令行参数:\n");

printf("[IP] 扫描所有端口\n");

printf("[IP] [端口]:扫描单个端口\n");

printf("[IP] [端口1] [端口2]:扫描端口1到端口2\n");

return 1;

}

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, wsaData );

if ( err != 0 )

{

printf("Error %d: Winsock not available\n", err);

return 1;

}

for(port=iPortStart; port=iPortEnd; port++)

{

sd_client = socket(PF_INET, SOCK_STREAM, 0);

if (sd_client == INVALID_SOCKET)

{

printf("no more socket resources\n");

return 1;

}

addr_srv.sin_family = PF_INET;

addr_srv.sin_addr.s_addr=inet_addr(pszHost);

addr_srv.sin_port = htons(port);

err = connect(sd_client, (struct sockaddr *) addr_srv, sizeof(addr_srv));

if (err == INVALID_SOCKET)

{

printf("不能连接此端口:%d\n", port);

closesocket(sd_client);

continue;

}

printf("扫描此端口成功:%d\n", port);

closesocket(sd_client);

}

WSACleanup();

return 0;

}

假设要判断某台机器打开了哪些tcp端口 该如何设计端口扫描程序

直接在命令提示符里面输入 netstat -a 就可以了。

netstat -a 查看开启了哪些端口,常用netstat -a

netstat -n 查看端口的网络连接情况,常用netstat -an

0条大神的评论

发表评论