Advertisement
KhaosBringer

Memcached Amp Scanner scan.c

May 12th, 2018
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. //Memcached Amplification Scanner
  2. //
  3. //compile: gcc scan.c -o scan -pthread
  4.  
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <signal.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <math.h>
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/ip.h>
  20. #include <netinet/udp.h>
  21.  
  22. volatile int running_threads = 0;
  23. volatile int found_srvs = 0;
  24. volatile unsigned long per_thread = 0;
  25. volatile unsigned long start = 0;
  26. volatile unsigned long scanned = 0;
  27. volatile int sleep_between = 0;
  28. volatile int bytes_sent = 0;
  29. volatile unsigned long hosts_done = 0;
  30. FILE *fd;
  31. char payload[] =
  32. "\x00\x01\x00\x00\x00\x01\x00\x00stats\r\n";
  33.  
  34. size = sizeof(payload);
  35.  
  36. void *flood(void *par1)
  37. {
  38.     running_threads++;
  39.     int thread_id = (int)par1;
  40.     unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
  41.     unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
  42.     unsigned long w;
  43.     int y;
  44.     unsigned char buf[65536];
  45.     memset(buf, 0x01, 15);
  46.     int sizeofpayload = 15;
  47.     int sock;
  48.     if((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) {
  49.         perror("cant open socket");
  50.         exit(-1);
  51.     }
  52.     for(w=ntohl(start_ip);w<htonl(end);w++)
  53.     {
  54.         struct sockaddr_in servaddr;
  55.         bzero(&servaddr, sizeof(servaddr));
  56.         servaddr.sin_family = AF_INET;
  57.         servaddr.sin_addr.s_addr=htonl(w);
  58.         servaddr.sin_port=htons(11211);
  59.         sendto(sock,payload,size,0, (struct sockaddr *)&servaddr,sizeof(servaddr));
  60.         bytes_sent+=size;
  61.         scanned++;
  62.         hosts_done++;
  63.     }
  64.     close(sock);
  65.     running_threads--;
  66.     return;
  67. }
  68.  
  69. void sighandler(int sig)
  70. {
  71.     fclose(fd);
  72.     printf("\n");
  73.     exit(0);
  74. }
  75.  
  76. void *recievethread()
  77. {
  78.     printf("\n");
  79.     int saddr_size, data_size, sock_raw;
  80.     struct sockaddr_in saddr;
  81.     struct in_addr in;
  82.  
  83.     unsigned char *buffer = (unsigned char *)malloc(65536);
  84.     sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_UDP);
  85.     if(sock_raw < 0)
  86.     {
  87.         printf("Socket Error\n");
  88.         exit(1);
  89.     }
  90.     while(1)
  91.     {
  92.         saddr_size = sizeof saddr;
  93.         data_size = recvfrom(sock_raw , buffer , 65536 , 0 , (struct sockaddr *)&saddr , &saddr_size);
  94.         if(data_size <0 )
  95.         {
  96.             printf("Recvfrom error , failed to get packets\n");
  97.             exit(1);
  98.         }
  99.         struct iphdr *iph = (struct iphdr*)buffer;
  100.         if(iph->protocol == 17)
  101.         {
  102.             unsigned short iphdrlen = iph->ihl*4;
  103.             struct udphdr *udph = (struct udphdr*)(buffer + iphdrlen);
  104.             unsigned char* payload = buffer + iphdrlen + 15;
  105.             if(ntohs(udph->source) == 11211)
  106.             {
  107.                 int body_length = data_size - iphdrlen - 15;
  108.                 if (body_length > 40)
  109.                 {
  110.                 found_srvs++;
  111.                 fprintf(fd,"%s %d\n",inet_ntoa(saddr.sin_addr),body_length);
  112.                 fflush(fd);
  113.                 }
  114.             }
  115.         }
  116.     }
  117.     close(sock_raw);
  118. }
  119. int main(int argc, char *argv[ ])
  120. {
  121.     if(argc < 6){
  122.     fprintf(stderr, "Invalid parameters!\n");
  123.     fprintf(stdout, "Usage: %s <ip range start (1.0.0.0)> <ip range end (255.255.255.255)> <outfile> <threads> <scan delay in ms>\n", argv[0]);
  124.     exit(-1);
  125.     }
  126.     fd = fopen(argv[3], "a");
  127.     sleep_between = atoi(argv[5]);
  128.     signal(SIGINT, &sighandler);
  129.     int threads = atoi(argv[4]);
  130.     pthread_t thread;
  131.     pthread_t listenthread;
  132.     pthread_create( &listenthread, NULL, &recievethread, NULL);
  133.     char *str_start = malloc(18);
  134.     memset(str_start, 0, 18);
  135.     str_start = argv[1];
  136.     char *str_end = malloc(18);
  137.     memset(str_end, 0, 18);
  138.     str_end = argv[2];
  139.     start = inet_addr(str_start);
  140.     per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
  141.     unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
  142.     int i;
  143.     for(i = 0;i<threads;i++){
  144.         pthread_create( &thread, NULL, &flood, (void *) i);
  145.     }
  146.     sleep(1);
  147.     printf("Scan in Progress \n");
  148.     char *temp = (char *)malloc(17);
  149.     memset(temp, 0, 17);
  150.     sprintf(temp, "Found");
  151.     printf("%-16s", temp);
  152.     memset(temp, 0, 17);
  153.     sprintf(temp, "IP/s");
  154.     printf("%-16s", temp);
  155.     memset(temp, 0, 17);
  156.     sprintf(temp, "Bytes/s");
  157.     printf("%-16s", temp);
  158.     memset(temp, 0, 17);
  159.     sprintf(temp, "Threads");
  160.     printf("%-16s", temp);
  161.     memset(temp, 0, 17);
  162.     sprintf(temp, "Percent Done");
  163.     printf("%s", temp);
  164.     printf("\n");
  165.     char *new;
  166.     new = (char *)malloc(16*6);
  167.     while (running_threads > 0)
  168.     {
  169.         printf("\r");
  170.         memset(new, '\0', 16*6);
  171.         sprintf(new, "%s|%-15lu", new, found_srvs);
  172.         sprintf(new, "%s|%-15d", new, scanned);
  173.         sprintf(new, "%s|%-15d", new, bytes_sent);
  174.         sprintf(new, "%s|%-15d", new, running_threads);
  175.         memset(temp, 0, 17);
  176.         int percent_done=((double)(hosts_done)/(double)(toscan))*100;
  177.         sprintf(temp, "%d%%", percent_done);
  178.         sprintf(new, "%s|%s", new, temp);
  179.         printf("%s", new);
  180.         fflush(stdout);
  181.         bytes_sent=0;
  182.         scanned = 0;
  183.         sleep(1);
  184.     }
  185.     printf("\n");
  186.     fclose(fd);
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement