Advertisement
Guest User

Untitled

a guest
May 29th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <stdio.h> // printf/fprintf
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <netinet/ip.h> // struct ip
  6. #include <sys/socket.h> // socket()
  7. #include <netinet/in.h> // struct sockadd
  8.  
  9. #define __FAVOR_BSD
  10. #define _USE_BSD
  11. #include <netinet/udp.h> // struct udp
  12.  
  13.  
  14.  
  15. void udp(char *, int , char * , char * , int );
  16. unsigned short int in_chksum (unsigned short int *, int);
  17. unsigned long hasard(unsigned long, unsigned long);
  18.  
  19.  
  20.  
  21. char * hexToAscii(char *hex_a_convertir){
  22.  
  23. int taille;
  24. int i;
  25. char *data;
  26. char hex[5], *stop;
  27.  
  28. hex[0] = '0';
  29. hex[1] = 'x';
  30. hex[4] = 0;
  31. taille = strlen(hex_a_convertir);
  32.  
  33. data = (char *)malloc(taille);
  34.  
  35.  
  36. for(i=0;i<taille /2;i++){
  37.  
  38. hex[2] = hex_a_convertir[i*2];
  39. hex[3] = hex_a_convertir[(i*2)+1];
  40. *(data+i) = (char) strtol(hex, &stop, 16);
  41.  
  42. }
  43.  
  44.  
  45. return data;
  46.  
  47.  
  48.  
  49. }
  50.  
  51.  
  52. main(int argc , char *arg[]) {
  53.  
  54.  
  55.  
  56.  
  57. printf("initialisation......... \n" );
  58.  
  59.  
  60. /*if( strcmp(arg[1] , "") == 0
  61.  
  62. while((c = getopt(argc,argv,"s:d:p:h")) != -1)
  63. {
  64. switch(c)
  65. {
  66. case 's':
  67. src = optarg;
  68. printf("\\nIP source:\\t\\t%s",src);
  69. break;
  70. case 'd':
  71. dest = optarg;
  72. printf("\\nIP de destination:\\t\\t%s",dest);
  73. break;
  74. case 'p':
  75. port = atoi(optarg);
  76. printf("\\nPort:\\t\\t%d",port);
  77. break;
  78. case 'h':
  79. case '?':
  80. printf(VERSION"Usage:\\n\\t-s\\tAdresse IP source\\n\\t-d\\tAdresse IP de destination\\n\\t-p\\tNuméro de port\\n");
  81. exit(0);
  82. break;
  83. }
  84. }
  85. printf("test......... \n" );*/
  86.  
  87. int port;
  88. sscanf(arg[2],"%i",&port);
  89.  
  90.  
  91.  
  92. udp(arg[1] , port , arg[3] , hexToAscii(arg[4]) , strlen(arg[4]) /2 );
  93.  
  94.  
  95. }
  96.  
  97. void udp(char *cible, int port , char *spoofed , char *donne , int taille_donne ) {
  98.  
  99. int sd;
  100. sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  101. if (sd == -1) {
  102. fprintf(stderr,"socket() error, root ?\n");
  103. }
  104.  
  105. unsigned long ip_src = inet_addr(spoofed);; //->spoof adresse
  106. unsigned long ip_dst = inet_addr(cible);
  107. unsigned short p_src = 10;
  108. unsigned short p_dst = port;
  109. char *data;
  110. struct sockaddr_in sin;
  111.  
  112. sin.sin_family = AF_INET;
  113. sin.sin_port = p_dst;
  114. sin.sin_addr.s_addr = ip_dst; // dst
  115.  
  116. struct ip *ip;
  117. struct udphdr *udp;
  118. char *dgm;
  119. char *interface;
  120.  
  121. int pksize = sizeof(struct ip) + sizeof(struct udphdr) + taille_donne;
  122. dgm = (char *) malloc(pksize);
  123. ip = (struct ip *) dgm;
  124. udp = (struct udphdr *) (dgm + sizeof(struct ip));
  125. data = (char *) (dgm + sizeof(struct ip) + sizeof(struct udphdr));
  126.  
  127. memset(dgm, 0, pksize);
  128. memcpy((char *) data, donne, taille_donne);
  129.  
  130. int un = 1;
  131. if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (char *)&un, sizeof(un)) == -1)
  132. {
  133. fprintf(stderr,"setsockopt()");
  134. exit(-1);
  135. }
  136.  
  137. //entete ip
  138.  
  139. ip->ip_hl = 0x5;
  140. ip->ip_v = 0x4;
  141. ip->ip_tos = 0x0;
  142. ip->ip_len = sizeof(pksize);
  143. ip->ip_ttl = 64;
  144. ip->ip_off = 0x0;
  145. ip->ip_id = htons(12830);
  146. ip->ip_p = IPPROTO_UDP;
  147. ip->ip_sum = 0x0; // a remplir aprés
  148. ip->ip_src.s_addr = ip_src;
  149. ip->ip_dst.s_addr = ip_dst;
  150.  
  151.  
  152.  
  153. //entete udp
  154.  
  155. udp->uh_sport = htons(p_src);
  156. udp->uh_dport = htons(p_dst);
  157. udp->uh_ulen = htons(sizeof(struct udphdr ) + taille_donne);
  158. udp->uh_sum = 0;
  159.  
  160. interface = (char *)malloc(4);
  161. strncpy(interface, "ppp0\0",5);
  162.  
  163. /*if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, interface , strlen(interface)+1 ) == -1)
  164. {
  165. fprintf(stderr,"pb a la specification de l'option\n");
  166. exit(-1);
  167. }*/
  168.  
  169.  
  170. // envoi
  171. printf("Envoie du paquet........... \n");
  172. if (sendto(sd, dgm, pksize, 0, (struct sockaddr *) &sin,
  173. sizeof(struct sockaddr)) == -1) {
  174. fprintf(stderr,"oops, sendto() error\n");
  175. }
  176. printf("Envoie OK\n");
  177. //libere la memoire
  178. free(dgm);
  179. free(donne);
  180. close(sd);
  181. }
  182.  
  183. u_short in_chksum (u_short *addr, int len) // taken from papasmurf.c
  184. {
  185. register int nleft = len;
  186. register u_short *w = addr;
  187. register int sum = 0;
  188. u_short answer = 0;
  189.  
  190. while (nleft > 1)
  191. {
  192. sum += *w++;
  193. nleft -= 2;
  194. }
  195.  
  196. if (nleft == 1)
  197. {
  198. *(u_char *)(&answer) = *(u_char *)w;
  199. sum += answer;
  200. }
  201.  
  202. sum = (sum >> 16) + (sum + 0xffff);
  203. sum += (sum >> 16);
  204. answer = ~sum;
  205. return(answer);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement