cjj20
cjj20

Reputation: 60

UDP port scanner issues, C

So I am trying to create a functional udp port scanner. The code I have so far seems to be missing something such maybe switch, use of iwreq or simply a linked list for the scanned ports, not sure. I am new to C programming so would appreciate any help.

#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;



}

Upvotes: 0

Views: 1210

Answers (1)

Fabien
Fabien

Reputation: 4960

A thing like 'UDP port scanner' does not technically exist because UDP is stateless, so there is no connection.

Instead, what "UDP port scanners" do is send 'payloads' in order to try to get a response.

Basically a correct payload to test UDP 53 is a DNS request.

So you implement payloads, and you test all payloads on a port. Then you have a sort of UDP scanner.

Well I guess this does not answer your question but I hope it helps you perform a better scanner.

Upvotes: 1

Related Questions