QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 890|回复: 6

网络编程--扫描端口出问题

[复制链接]
发表于 2004-7-5 11:13:38 | 显示全部楼层 |阅读模式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

int main(int argc , char *argv[])
{
        int dst_ip;
        int dst_port;
        int start,last;
        int sockfd;
        struct sockaddr_in dst;

        if(argc != 4)
        {
                fprintf(stderr , "usage : %s dst_ip start_port last_port\n" , argv[0]);
                exit(1);
        }

        dst_ip = inet_addr(argv[1]);
        start = atoi(argv[2]);
        last = atoi(argv[3]);

        sockfd = socket(AF_INET , SOCK_STREAM , 0);
       
        if(sockfd < 0)
        {
                fprintf(stderr , "socket error");
            exit(1);
        }       

        for(dst_port = start ; dst_port <= last ; dst_port++)
        {
                memset(&dst , 0 , sizeof(dst));
                dst.sin_family = AF_INET;
                dst.sin_port = htons(dst_port);
                dst.sin_addr.s_addr = dst_ip;

                if(connect(sockfd , (struct sockaddr *)&dst , sizeof(dst)) < 0)
                {
                        fprintf(stderr , "port %d close\n" ,dst_port);
                }
                else
                {
                        fprintf(stderr , "port %d open\n" ,dst_port);
                }
        }

        close(sockfd);
        exit(0);
}

这个程序当扫描开放的端口时能显示port XX open
可当某个端口关闭时,就无法执行下去了
好比我用scan 202.23.36.56 79 81
假设202.23.36.56是个http服务,80开放
可是程序没什么反映
如果用scan 202.23.36.56 80 80
则会显示port 80 open
怎么回事啊,帮帮我啊
发表于 2004-7-5 20:04:00 | 显示全部楼层
等了多久?
回复

使用道具 举报

 楼主| 发表于 2004-7-5 20:27:16 | 显示全部楼层
[quote:2b8365b3f3="sagaeon"]等了多久?[/quote]
如果scan xx.xx.xx.xx 80 80(80端口开放)
没一秒就能显示port 80 open
如果scan xx.xx.xx.xx 79 79(79端口关闭)
一分多钟都没任何反应,只好ctrl+c关闭了
回复

使用道具 举报

发表于 2004-7-5 21:33:21 | 显示全部楼层
会返回的,只是你没耐心罢了,看我的
网易
$ ./a.out 202.106.168.103 79 81
port 79 close
port 80 open
port 81 close
另一个不知哪的
$ ./a.out 202.114.30.139  79 80
port 79 close
port 80 close
大约用了5分钟。
如果你scan的ip是局域网内中间无防火墙,那就很快,你试。
回复

使用道具 举报

 楼主| 发表于 2004-7-5 22:29:48 | 显示全部楼层
[quote:c3c1683429="sagaeon"]会返回的,只是你没耐心罢了,看我的
网易
$ ./a.out 202.106.168.103 79 81
port 79 close
port 80 open
port 81 close
另一个不知哪的
$ ./a.out 202.114.30.139  79 80
port 79 close
port 80 close
大约用了5分钟。
如果你scan的ip是局域网内中间无防火墙,那就很快,你试。[/quote]
可我用过人家的扫描程序很快的啊,能告诉我怎么才能实现吗?不要具体做法,只需简要方法就好了,谢谢!
回复

使用道具 举报

发表于 2004-7-6 09:01:49 | 显示全部楼层
将连接设为非阻塞方式不知是否可行?
回复

使用道具 举报

发表于 2004-7-14 14:46:22 | 显示全部楼层
这种扫描最好用原始套接字+多线程,模仿TCP的三次握手.
很多网站(xfocus.net,等等)都有介绍高级扫描技术的文章的.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-6-3 04:44 , Processed in 0.169339 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表