• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

TCP/IP网络程序编程——端口扫描程序UDP

武飞扬头像
木子南的翻斗花园
帮助1

程序代码

  1.  
    #include<cstdio>
  2.  
    #include<cstdlib>
  3.  
    #include<sys/socket.h>
  4.  
    #include<sys/types.h>
  5.  
    #include<unistd.h>
  6.  
    #include<arpa/inet.h>
  7.  
    #include<netinet/in.h>
  8.  
    #include<netinet/ip_icmp.h>
  9.  
    #include<netinet/ip.h>
  10.  
    #include<netinet/in_systm.h>
  11.  
    #include<netdb.h>
  12.  
    #include<cstring>
  13.  
    #include<sys/time.h>
  14.  
     
  15.  
     
  16.  
    #define CHKADDRESS(_saddr_)\
  17.  
    {\
  18.  
    u_char *p=(u_char*)&(_saddr_);\
  19.  
    if((p[0]==10)||(p[0]==168&&16<=p[1]&&p[1]<=31)||(p[0]==192&&p[1]==168))\
  20.  
    ;\
  21.  
    else{\
  22.  
    fprintf(stderr,"ip address error!\n");\
  23.  
    exit(EXIT_FAILURE);\
  24.  
    }\
  25.  
    }
  26.  
     
  27.  
     
  28.  
    enum{ CMD_NAME,DST_IP,START_PORT,LAST_IP};
  29.  
     
  30.  
    #define MAXBUFF 8192
  31.  
     
  32.  
    int main(int argc,char** argv)
  33.  
    {
  34.  
    struct icmp* icmp;
  35.  
    fd_set select_fd;
  36.  
    struct sockaddr_in send_sa;
  37.  
    int recv_sd;
  38.  
    int send_sd;
  39.  
    char buff[MAXBUFF]; //接受包使用的缓存区
  40.  
     
  41.  
    int end_port;
  42.  
    int start_port;
  43.  
    int dstport;
  44.  
    struct timeval tv;
  45.  
     
  46.  
    if(argc!=4)
  47.  
    {
  48.  
    fprintf(stderr," usage:%s dst_ip start_port end_port\n",argv[CMD_NAME]);
  49.  
    exit(EXIT_FAILURE);
  50.  
    }
  51.  
     
  52.  
    send_sa.sin_addr.s_addr=inet_addr(argv[DST_IP]);
  53.  
    send_sa.sin_family=AF_INET;
  54.  
     
  55.  
    start_port=atoi(argv[START_PORT]);
  56.  
    end_port=atoi(argv[LAST_IP]);
  57.  
     
  58.  
    CHKADDRESS(send_sa.sin_addr.s_addr);
  59.  
     
  60.  
    if((send_sd=socket(AF_INET,SOCK_DGRAM,0))<0)
  61.  
    {
  62.  
    perror("socket(sock_raw)");
  63.  
    exit(EXIT_FAILURE);
  64.  
    }
  65.  
     
  66.  
    if((recv_sd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0)
  67.  
    {
  68.  
    perror("scok_raw");
  69.  
    exit(EXIT_FAILURE);
  70.  
    }
  71.  
     
  72.  
     
  73.  
    for(dstport=start_port;dstport<=end_port;dstport )
  74.  
    {
  75.  
    printf("scan port %d\r",dstport);
  76.  
    fflush(stdout);
  77.  
     
  78.  
    send_sa.sin_port=htons(dstport);
  79.  
    sendto(send_sd,NULL,0,0,(struct sockaddr*)&send_sa,sizeof(send_sa));
  80.  
     
  81.  
    tv.tv_sec=1;
  82.  
    tv.tv_usec=0;
  83.  
     
  84.  
    while(1)
  85.  
    {
  86.  
    FD_ZERO(&select_fd);
  87.  
    FD_SET(recv_sd,&select_fd);
  88.  
     
  89.  
    if(select(recv_sd 1,&select_fd,NULL,NULL,&tv)>0)
  90.  
    {
  91.  
    struct ip* ip;
  92.  
    int hlen;
  93.  
    int port;
  94.  
     
  95.  
     
  96.  
    if(recvfrom(recv_sd,buff,MAXBUFF,0,NULL,NULL)!=56)
  97.  
    {
  98.  
    continue;
  99.  
    }
  100.  
     
  101.  
    ip=(struct ip*)buff;
  102.  
    hlen=ip->ip_hl<<2;
  103.  
    icmp=(struct icmp*)(buff hlen);
  104.  
     
  105.  
    //取出接收到信息的端口号
  106.  
    port=ntohs(*(u_short*)(buff 20 8 20 2));
  107.  
     
  108.  
    //icmp不能到达的包类型为3,检查IP地址以及初始端口号是否一致
  109.  
    if((ip->ip_src.s_addr!=send_sa.sin_addr.s_addr)||(icmp->icmp_type!=ICMP_UNREACH)||(icmp->icmp_code!=ICMP_UNREACH_PORT)||(port!=dstport))
  110.  
    continue;
  111.  
    }
  112.  
    else{
  113.  
    struct servent *se;
  114.  
    se=getservbyport(htons(dstport),"udp");
  115.  
    printf("] %-20s \n",dstport,(se==NULL)?"unknown":se->s_name);
  116.  
    }
  117.  
    break;
  118.  
    }
  119.  
    }
  120.  
    return EXIT_SUCCESS;
  121.  
    }
学新通

运行结果

学新通

部分说明

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfchfc
系列文章
更多 icon
同类精品
更多 icon
继续加载