udp端口扫描程序代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//udp.cpp:定义控制台应用程序的入口点。//#includestdafx.h#includestdio.h#includewinsock.h#pragmacomment(lib,ws2_32.lib)#defineMAXBUFF8192#defineICMP_UNREACH_PORT3#defineICMP_UNREACH4enum{CMD_NAME,DST_IP,START_PORT,LAST_PORT};structip{unsignedintip_v:4;//版本unsignedintip_hl:4;//头部长unsignedcharip_tos;//服务类型unsignedshortip_len;//总长unsignedshortip_id;//鉴定域unsignedshortip_flags;//IP标志unsignedshortip_off;//片偏移unsignedcharip_ttl;//TTLunsignedcharip_protocol;//协议unsignedshortip_sum;//头部效验和structin_addrip_src,ip_dest;//源ip和目的IP#defineIP_RF0x8000//标记收到数据#defineIP_DF0x4000#defineIP_MF0x2000#defineIP_OFFMASK0x1fff};structicmp{unsignedshortinticmp_type;unsignedshortinticmp_code;unsignedlonginticmp_cksum;unsignedlonginticmp_id;unsignedlonginticmp_seq;unsignedshortinticmp_data[1];};intmain(intargc,char*argv[]){structtimevaltv;fd_setselect_fd;charbuff[MAXBUFF];intdstport,startport,endport;if(argc!=4){fprintf(stdout,usage:%s目的ip起始端口目的端口\n,argv[CMD_NAME]);exit(EXIT_FAILURE);}WSADatawsaData;WSAStartup(MAKEWORD(2,2),&wsaData);structsockaddr_insend_sa;//发送端地址信息memset(&send_sa,0,sizeof(send_sa));send_sa.sin_family=AF_INET;send_sa.sin_addr.s_addr=inet_addr(argv[DST_IP]);startport=atoi(argv[START_PORT]);endport=atoi(argv[LAST_PORT]);SOCKETsock_send=socket(AF_INET,SOCK_DGRAM,0);//发送用的描述符if(sock_send0){perror(sock(SOCK_DGRAM));closesocket(sock_send);exit(EXIT_FAILURE);}SOCKETsock_recv=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);//接收用的描述符if(sock_recv0){perror(socket(SOCK_RAW));closesocket(sock_recv);exit(EXIT_FAILURE);}for(dstport=startport;dstport=endport;dstport++){printf(扫描端口%d\r,dstport);fflush(stdout);send_sa.sin_port=htons(dstport);intres=sendto(sock_send,NULL,0,0,(structsockaddr*)&send_sa,sizeof(send_sa));tv.tv_sec=1;tv.tv_usec=0;while(1){FD_ZERO(&select_fd);FD_SET(sock_recv,&select_fd);if(select(sock_recv+1,&select_fd,NULL,NULL,&tv)0){structip*ip;structicmp*icmp;inthlen;intport;memset(&ip,0,sizeof(ip));if(recvfrom(sock_recv,buff,MAXBUFF,0,NULL,NULL)!=56)continue;ip=(structip*)buff;hlen=ip-ip_hl2;icmp=(structicmp*)(buff+hlen);port=ntohs(*(u_short*)(buff+20+8+20+2));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))continue;}else{structservent*se;se=getservbyport(htons(dstport),udp);if(se!=NULL)printf(%5d%-20s\n,dstport,(se==NULL)?unknown:se-s_name);}break;}}closesocket(sock_send);closesocket(sock_recv);intWSACleanup();return(0);}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功