Advertisement
bildramer

Untitled

Dec 24th, 2011
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. unsigned short in_cksum(unsigned short *addr, int len)
  2. {
  3. int nleft = len;
  4. int sum = 0;
  5. unsigned short *w = addr;
  6. unsigned short answer = 0;
  7.  
  8. while (nleft > 1) {
  9. sum += *w++;
  10. nleft -= 2;
  11. }
  12.  
  13. if (nleft == 1) {
  14. *(unsigned char *) (&answer) = *(unsigned char *) w;
  15. sum += answer;
  16. }
  17.  
  18. sum = (sum >> 16) + (sum & 0xFFFF);
  19. sum += (sum >> 16);
  20. answer = ~sum;
  21. return (answer);
  22. }
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <netdb.h>
  30.  
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <sys/socket.h>
  34.  
  35. #include <netinet/in_systm.h>
  36. #include <netinet/in.h>
  37. #include <netinet/ip.h>
  38. #include <netinet/udp.h>
  39. #include <netinet/ip_icmp.h>
  40. #include <netinet/tcp.h>
  41.  
  42. #include <arpa/inet.h>
  43.  
  44. int
  45. main(int argc, char **argv)
  46. {
  47. struct ip ip;
  48. struct udphdr udp;
  49. struct icmp icmp;
  50. int sd;
  51. const int on = 1;
  52. struct sockaddr_in sin;
  53. u_char *packet;
  54.  
  55. packet = (u_char *)malloc(60);
  56.  
  57. ip.ip_hl = 0x5;
  58.  
  59. ip.ip_v = 0x4;
  60.  
  61. ip.ip_tos = 0x0;
  62.  
  63. ip.ip_len = htons(60);
  64.  
  65. ip.ip_id = htons(12830);
  66.  
  67. ip.ip_off = 0x0;
  68.  
  69. ip.ip_ttl = 64;
  70.  
  71. ip.ip_p = IPPROTO_ICMP;
  72.  
  73. ip.ip_sum = 0x0;
  74.  
  75. ip.ip_src.s_addr = inet_addr("172.17.14.174");
  76.  
  77. ip.ip_dst.s_addr = inet_addr("172.17.14.169");
  78.  
  79. ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip));
  80.  
  81. memcpy(packet, &ip, sizeof(ip));
  82.  
  83. icmp.icmp_type = ICMP_ECHO;
  84.  
  85. icmp.icmp_code = 0;
  86.  
  87. icmp.icmp_id = 1000;
  88.  
  89. icmp.icmp_seq = 0;
  90.  
  91. icmp.icmp_cksum = 0;
  92. icmp.icmp_cksum = in_cksum((unsigned short *)&icmp, 8);
  93.  
  94. memcpy(packet + 20, &icmp, 8);
  95.  
  96. if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  97. perror("raw socket");
  98. exit(1);
  99. }
  100.  
  101.  
  102. if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
  103. perror("setsockopt");
  104. exit(1);
  105. }
  106.  
  107.  
  108. memset(&sin, 0, sizeof(sin));
  109. sin.sin_family = AF_INET;
  110. sin.sin_addr.s_addr = ip.ip_dst.s_addr;
  111.  
  112.  
  113. if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) {
  114. perror("sendto");
  115. exit(1);
  116. }
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement