sockets - C: UDP packets source Ip faking - can't receive packet -


after hours on google still cant figure out.

what trying is: want send udp packets fake source-ip c programm (see code below).

what not working: when send packet server-1 server-2 , trying receive it, appear in 'iftop', when sourceip of packet not faked. when put fake ip in udp header still sais me packet sent (i see on 'iftop' on server-1), won't receive on server-2. example when send correct sender ip, i'll receive packet , see on 'iftop', when take example '1.2.3.4' source ip cant receive (but 'iftop' on server-1 still says has been send).

i read lot of stuff , says no problem fake source ip of udp packet, i'm wondering doing wrong. tried in python , didn't receive too.

/*     raw udp sockets */ #include<stdio.h> //for printf #include<string.h> //memset #include<sys/socket.h>    //for socket ofcourse #include<stdlib.h> //for exit(0); #include<errno.h> //for errno - error number #include<netinet/udp.h>   //provides declarations udp header #include<netinet/ip.h>    //provides declarations ip header  /*      96 bit (12 bytes) pseudo header needed udp header checksum calculation  */ struct pseudo_header {     u_int32_t source_address;     u_int32_t dest_address;     u_int8_t placeholder;     u_int8_t protocol;     u_int16_t udp_length; };  /*     generic checksum calculation function */ unsigned short csum(unsigned short *ptr,int nbytes)  {     register long sum;     unsigned short oddbyte;     register short answer;      sum=0;     while(nbytes>1) {         sum+=*ptr++;         nbytes-=2;     }     if(nbytes==1) {         oddbyte=0;         *((u_char*)&oddbyte)=*(u_char*)ptr;         sum+=oddbyte;     }      sum = (sum>>16)+(sum & 0xffff);     sum = sum + (sum>>16);     answer=(short)~sum;      return(answer); }  int main (void) {     //create raw socket of type ipproto     int s = socket (af_inet, sock_raw, ipproto_raw);      if(s == -1)     {         //socket creation failed, may because of non-root privileges         perror("failed create raw socket");         exit(1);     }      //datagram represent packet     char datagram[4096] , source_ip[32] , *data , *pseudogram;      //zero out packet buffer     memset (datagram, 0, 4096);      //ip header     struct iphdr *iph = (struct iphdr *) datagram;      //udp header     struct udphdr *udph = (struct udphdr *) (datagram + sizeof (struct ip));      struct sockaddr_in sin;     struct pseudo_header psh;      //data part     data = datagram + sizeof(struct iphdr) + sizeof(struct udphdr);     strcpy(data , "abcdefghijklmnopqrstuvwxyz");      //some address resolution     strcpy(source_ip , "***.***.***.***");    // <- (fake-)source ip      sin.sin_family = af_inet;     sin.sin_port = htons(80);     sin.sin_addr.s_addr = inet_addr ("***.***.***.***");  // <- receiver ip      //fill in ip header     iph->ihl = 5;     iph->version = 4;     iph->tos = 0;     iph->tot_len = sizeof (struct iphdr) + sizeof (struct udphdr) + strlen(data);     iph->id = htonl (54321); //id of packet     iph->frag_off = 0;     iph->ttl = 255;     iph->protocol = ipproto_udp;     iph->check = 0;      //set 0 before calculating checksum     iph->saddr = inet_addr ( source_ip );    //spoof source ip address     iph->daddr = sin.sin_addr.s_addr;      //ip checksum     iph->check = csum ((unsigned short *) datagram, iph->tot_len);      //udp header     udph->source = htons (45242);     udph->dest = htons (12345);     udph->len = htons(8 + strlen(data)); //tcp header size     udph->check = 0; //leave checksum 0 now, filled later pseudo header      //now udp checksum using pseudo header     psh.source_address = inet_addr( source_ip );     psh.dest_address = sin.sin_addr.s_addr;     psh.placeholder = 0;     psh.protocol = ipproto_udp;     psh.udp_length = htons(sizeof(struct udphdr) + strlen(data) );      int psize = sizeof(struct pseudo_header) + sizeof(struct udphdr) + strlen(data);     pseudogram = malloc(psize);      memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));     memcpy(pseudogram + sizeof(struct pseudo_header) , udph , sizeof(struct udphdr) + strlen(data));      udph->check = csum( (unsigned short*) pseudogram , psize);      //loop if want flood :)     //while (1)     {         //send packet         if (sendto (s, datagram, iph->tot_len ,  0, (struct sockaddr *) &sin, sizeof (sin)) < 0)         {             perror("sendto failed");         }         //data send         else         {             printf ("packet send. length : %d \n" , iph->tot_len);         }     }      return 0; } 

thanks time.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -