xttpx

ts3scan

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