UDP端口扫描程序问题,C

时间:2017-07-06 19:15:36

标签: c sockets udp port

所以我正在尝试创建一个功能性的udp端口扫描程序。我到目前为止的代码似乎缺少一些东西,如切换,使用iwreq或只是扫描端口的链表,不确定。我是C编程的新手,所以非常感谢任何帮助。

#include <erno.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <iwlib.h>
#include <wireless.h>

#define MAXSIZE 0

int main(){

struct timeval timesec;
int sockfd;
int ipleng;

int port= 1020;
struct hostent hostent;
struct sockaddr my_addre;
struct imcmphdr IC;
struct sockaddr_in my_addr;
struct ip ipp;
struct iw_range rang;
struct iwreq iwq;
char message="  ";
h_addr_list[0];



scan();
if(scan==NULL){
perror("scan not done");
exit(1);

}
int scan(){
ioctl(socket,SIOCSIWSCAN,&iwq);
iw_get_range_info(sockfd,wlan0,&rang);
  if(iw_get_range_info(sockfd, wlan0, &rang)>0){
    rang=1;
  }
   r=iw_get_range_info(sockfd,wlan0,&rang);
char serve [50];
int hlen;
int slen;
for(int i=0; port<r; i++){
if(hostt=getnameinfo(struct hostent  *host, MAXSIZE, host, hlen, serve, 
slen, NULL)==NULL){

if(haddrinfo=getaddrinfo(struct hostent *host, MAXSIZE, host, hlen, serve, 
slen, NULL)==NULL){
exit(1);
}
else{
printf("IP address");
addrlist=h_addr_list;
for(i=0; addrlist[i]>0; i++){
printf("host: %s\n", arrdlist[i],  hostt);
printf("server: %s\n", haddrinfo);

if(sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0 {
   perror("socket not created");
   return -1;

if(rsock=sockfd(AF_INET, SOCK_RAW, IPPROTO_ICMP))<0){
    perror("socket fail"); 
    exit(1);
    else{


memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(1020);
my_addr.sin_addr.s_addr=inet_addr();
my_addr.sin_addr(struct in_addr*)hostt->h_addr;



if(sendto(sockfd,MAXSIZE,0,0,(struct sockaddr*)&my_addre,sizeof(my_addre))
<0){
    perror("can't send");
    return -1;
   }
  }
 }
}
if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&host,sizeof(host))== -1){
    perror("setsockopt(SOL_BROADCAST)");
    return -1;
    else
        timesec.tv_sec=3;
     while(1);
    {
    FD_ISSET(&my_addr);
    FD_ZERO(&readfds);
    FD_SET(&rsock,&readfds);
    ipleng=ip->ip_hl<<2;
    if(select(rsock+1,&readfds,NULL,NULL,&timsec);
    for(;;){
        if(select(rsock+1,&readfds,NULL,NULL,&timesec){
        if(FD_ISSET(rsock,&readfds0{
            siz=read(rsock,(struct icmphdr*)&IC,sizeof(IC));
            if(siz=0){
                printf("can't read");
            }

         }


        }
  } 
        ipp=(struct ip*)MAXSIZE;
        message=(struct icmp*)(MAXSIZE+ipleng);
        if(recvfrom(rsock,MAXSIZE,sizeof(MAXSIZE),NULL,my_addre,hlen)<0){
            perror("ICMP error");
            printf("port closed");
            if(message->icmp_type==ICMP_UNREACH)&&message->icmp_code==ICMP_UNREACH_PORT{
                exit(1);
            }
            else{
                if(message==0){
                    printf("p;ort open");
                    port=getservbyport(htons(addrlist[i], "port");
                    printf("udp port %d %s is open\n", addrlist[i], port->s_addr);
                    if(port==NULL){
                        printf("unknown");
                    }




                }





            }


          }


        }
       }

      }

    }



  }




  }
 }


fflush(sockfd);
return 0;



}

1 个答案:

答案 0 :(得分:1)

像'UDP端口扫描程序'这样的东西在技术上并不存在,因为UDP是无状态的,因此没有连接。

相反,“UDP端口扫描程序”所做的是发送“有效负载”以尝试获得响应。

基本上,测试UDP 53的正确有效负载是DNS请求。

因此,您实现了有效负载,并测试了端口上的所有有效负载。然后你有一种UDP扫描器。

嗯,我想这不能回答你的问题,但我希望它可以帮助你做更好的扫描仪。