Advertisement
xttpx

db2scan

Jul 10th, 2017
11,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.21 KB | None | 0 0
  1. #include <pcap.h>
  2. #include <stdio.h>
  3. #include <stdlib.h> // for exit()
  4. #include <string.h> //for memset
  5. #include <sys/ioctl.h>
  6. #include <net/if.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h> // for inet_ntoa()
  9. #include <net/ethernet.h>
  10. #include <netinet/udp.h> //Provides declarations for udp header
  11. #include <netinet/ip.h>  //Provides declarations for ip header
  12. #include <pthread.h>
  13. #include <semaphore.h>
  14. #include <signal.h>
  15. #include <sys/resource.h>
  16. #include <unistd.h>
  17. static unsigned int payloadsize = 20;
  18. static unsigned int xport = 523;
  19. void process_packet(void *args, struct pcap_pkthdr *header, void *buffer);
  20.  
  21. struct buffer
  22. {
  23.         void *data;
  24.         int size;
  25.         struct buffer *next;
  26.         struct buffer *prev;
  27. };
  28. struct buffer *head;
  29.  
  30. char *ipv4;
  31. int processed,over,total,i,j;
  32. struct sockaddr_in dest;
  33. pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
  34. sem_t loop_sem;
  35. int running_threads = 0;
  36. volatile int found_srvs = 0;
  37. volatile unsigned long per_thread = 0;
  38. volatile unsigned long start = 0;
  39. volatile unsigned long scanned = 0;
  40. int sleep_between = 0;
  41. volatile int bytes_sent = 0;
  42. volatile unsigned long hosts_done = 0;
  43. FILE *fd;
  44.  
  45. void *readthread()
  46. {
  47.         struct buffer *ourhead = head;
  48.         struct sockaddr_in saddr;
  49.         while(1)
  50.         {
  51.                 sem_wait(&loop_sem);
  52.                 while(ourhead->data == NULL){ ourhead = ourhead->next; }
  53.                 pthread_mutex_lock(&buf_mutex);
  54.                 void *buf = malloc(ourhead->size);
  55.                 int size = ourhead->size;
  56.                 memcpy(buf, ourhead->data, ourhead->size);
  57.                 free(ourhead->data);
  58.                 ourhead->data = NULL;
  59.                 ourhead->size = 0;
  60.                 pthread_mutex_unlock(&buf_mutex);
  61.                 memset(&saddr, 0, sizeof(saddr));
  62.                 struct iphdr *iph = (struct iphdr*)(buf + sizeof(struct ethhdr));
  63.                 saddr.sin_addr.s_addr = iph->saddr;
  64.                 struct udphdr *udph = (struct udphdr *)(buf + sizeof(struct ethhdr) + sizeof(struct iphdr));
  65.                 if(ntohs(udph->source) == xport)
  66.                 {
  67.                         int body_length = size - sizeof(struct ethhdr) - sizeof(struct iphdr) - sizeof(struct udphdr);
  68.                         fprintf(fd,"%s %d\n",inet_ntoa(saddr.sin_addr),body_length);
  69.                         fflush(fd);
  70.                         found_srvs++;
  71.                 }
  72.                 free(buf);
  73.                 processed++;
  74.                 ourhead = ourhead->next;
  75.         }
  76. }
  77.  
  78. void *flood(void *par1)
  79. {
  80.         running_threads++;
  81.         int thread_id = (int)par1;
  82.         unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
  83.         unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
  84.         unsigned long w;
  85.         int y;
  86.         unsigned char buf[65536];
  87.         memcpy(buf, "\x44\x42\x32\x47\x45\x54\x41\x44\x44\x52\x00\x53\x51\x4c\x30\x35\x30\x30\x30\x00", payloadsize);
  88.         int sizeofpayload = payloadsize;
  89.         int sock;
  90.         if((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) {
  91.                 perror("cant open socket");
  92.                 exit(-1);
  93.         }
  94.         for(w=ntohl(start_ip);w<htonl(end);w++)
  95.         {
  96.                 struct sockaddr_in servaddr;
  97.                 bzero(&servaddr, sizeof(servaddr));
  98.                 servaddr.sin_family = AF_INET;
  99.                 servaddr.sin_addr.s_addr=htonl(w);
  100.                 servaddr.sin_port=htons(xport);
  101.                 sendto(sock,(char *)buf,sizeofpayload,0, (struct sockaddr *)&servaddr,sizeof(servaddr));
  102.                 bytes_sent+=sizeofpayload;
  103.                 scanned++;
  104.                 hosts_done++;
  105.                 usleep(sleep_between*1000);
  106.         }
  107.         close(sock);
  108.         running_threads--;
  109.         return;
  110. }
  111.  
  112. void sighandler(int sig)
  113. {
  114.         fclose(fd);
  115.         printf("\n");
  116.         exit(0);
  117. }
  118.  
  119. void *printthread(void *argvs)
  120. {
  121.         char **argv = (char **)argvs;
  122.         int threads = atoi(argv[4]);
  123.         pthread_t thread;
  124.         sleep(1);
  125.         char *str_start = malloc(18);
  126.         memset(str_start, 0, 18);
  127.         str_start = argv[1];
  128.         char *str_end = malloc(18);
  129.         memset(str_end, 0, 18);
  130.         str_end = argv[2];
  131.         start = inet_addr(str_start);
  132.         per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
  133.         unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
  134.         int i;
  135.         for(i = 0;i<threads;i++){
  136.                 pthread_create( &thread, NULL, &flood, (void *) i);
  137.         }
  138.          char *temp = (char *)malloc(17);
  139.         memset(temp, 0, 17);
  140.         sprintf(temp, "Reflectors");
  141.         printf("%-16s", temp);
  142.         memset(temp, 0, 17);
  143.         sprintf(temp, "IP/s");
  144.         printf("%-16s", temp);
  145.         memset(temp, 0, 17);
  146.         sprintf(temp, "Bytes/s");
  147.         printf("%-16s", temp);
  148.         memset(temp, 0, 17);
  149.         sprintf(temp, "Threads");
  150.         printf("%-16s", temp);
  151.         memset(temp, 0, 17);
  152.         sprintf(temp, "%");
  153.         printf("%s", temp);
  154.         printf("\n");
  155.  
  156.         char *new;
  157.         new = (char *)malloc(16*6);
  158.         while (running_threads > 0)
  159.         {
  160.                 printf("\r");
  161.                 memset(new, '\0', 16*6);
  162.                 sprintf(new, "%s|%-15lu", new, found_srvs);
  163.                 sprintf(new, "%s|%-15d", new, scanned);
  164.                 sprintf(new, "%s|%-15d", new, bytes_sent);
  165.                 sprintf(new, "%s|%-15d", new, running_threads);
  166.                 memset(temp, 0, 17);
  167.                 int percent_done=((double)(hosts_done)/(double)(toscan))*100;
  168.                 sprintf(temp, "%d%%", percent_done);
  169.                 sprintf(new, "%s|%s", new, temp);
  170.                 printf("%s", new);
  171.                 fflush(stdout);
  172.                 bytes_sent=0;
  173.                 scanned = 0;
  174.                 sleep(1);
  175.         }
  176.         printf("\n");
  177.         fclose(fd);
  178.         exit(0);
  179. }
  180.  
  181. int main(int argc, char *argv[ ])
  182.  
  183. {
  184.         if(argc < 6){
  185.                 fprintf(stderr, "Invalid parameters!\n");
  186.                 fprintf(stdout, "Usage: %s <1.0.0.0> <255.255.255.255> <list> <threads> <delay>\n", argv[0]);
  187.                 exit(-1);
  188.         }
  189.         fd = fopen(argv[3], "a");
  190.         sleep_between = atoi(argv[5]);
  191.         int num_threads = atoi(argv[4]);
  192.        
  193.         const rlim_t kOpenFD = 1024 + (num_threads * 2);
  194.         struct rlimit rl;
  195.         int result;
  196.         rl.rlim_cur = kOpenFD;
  197.         rl.rlim_max = kOpenFD;
  198.         result = setrlimit(RLIMIT_NOFILE, &rl);
  199.         if (result != 0)
  200.         {
  201.                 perror("setrlimit_nofile");
  202.                 fprintf(stderr, "setrlimit_nofile returned result = %d\n", result);
  203.         }
  204.         bzero(&rl, sizeof(struct rlimit));
  205.         rl.rlim_cur = 256 * 1024;
  206.         rl.rlim_max = 4096 * 1024;
  207.         result = setrlimit(RLIMIT_STACK, &rl);
  208.         if (result != 0)
  209.         {
  210.                 perror("setrlimit_stack");
  211.                 fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
  212.         }
  213.  
  214.         signal(SIGINT, &sighandler);
  215.  
  216.         pcap_if_t *alldevsp;
  217.         pcap_t *handle; //Handle of the device that shall be sniffed
  218.  
  219.         char errbuf[100] , *devname , devs[100][100];
  220.         int count = 1 , n;
  221.  
  222.         if( pcap_findalldevs( &alldevsp , errbuf) )
  223.         {
  224.                 exit(1);
  225.         }
  226.  
  227.         devname = alldevsp->name;
  228.         ipv4 = malloc(16);
  229.         bzero(ipv4, 16);
  230.         struct ifreq ifc;
  231.     int res;
  232.     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  233.  
  234.         if(sockfd < 0) exit(-1);
  235.     strcpy(ifc.ifr_name, devname);
  236.     res = ioctl(sockfd, SIOCGIFADDR, &ifc);
  237.     close(sockfd);
  238.         if(res < 0) exit(-1);
  239.     strcpy(ipv4, inet_ntoa(((struct sockaddr_in*)&ifc.ifr_addr)->sin_addr));
  240.         printf("Opening device %s for sniffing ... " , devname);
  241.         handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
  242.  
  243.         if (handle == NULL)
  244.         {
  245.                 fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
  246.                 exit(1);
  247.         }
  248.         printf("Done\n");
  249.  
  250.         sem_init(&loop_sem, 0, -1);
  251.         i = 1024*1000;
  252.         while(i--)
  253.         {
  254.                 if(head == NULL)
  255.                 {
  256.                         head = (struct buffer *)malloc(sizeof(struct buffer));
  257.                         bzero(head, sizeof(struct buffer));
  258.                         head->data = NULL;
  259.                         head->size = 0;
  260.                         head->next = head;
  261.                         head->prev = head;
  262.                 } else {
  263.                         struct buffer *new_node = (struct buffer *)malloc(sizeof(struct buffer));
  264.                         bzero(new_node, sizeof(struct buffer));
  265.                         new_node->data = NULL;
  266.                         new_node->size = 0;
  267.                         new_node->prev = head;
  268.                         new_node->next = head->next;
  269.                         head->next = new_node;
  270.                 }
  271.         }
  272.  
  273.         pthread_t prnthread;
  274.         pthread_create( &prnthread, NULL, &printthread, (void *)argv);
  275.         pthread_t redthread;
  276.         pthread_create( &redthread, NULL, &readthread, NULL);
  277.  
  278.         pcap_loop(handle , -1 , process_packet , NULL);
  279.  
  280.         return 0;
  281. }
  282.  
  283. void process_packet(void *args, struct pcap_pkthdr *header, void *buffer)
  284. {
  285.         int size = header->len;
  286.  
  287.         //Get the IP Header part of this packet , excluding the ethernet header
  288.         struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
  289.         memset(&dest, 0, sizeof(dest));
  290.         dest.sin_addr.s_addr = iph->daddr;
  291.  
  292.         if(iph->protocol == 17 && strcmp(inet_ntoa(dest.sin_addr), ipv4) == 0)
  293.         {
  294.                 //toss into buffer
  295.                 if(head->data != NULL) over++;
  296.                 pthread_mutex_lock(&buf_mutex);
  297.                 void *temp = malloc(size);
  298.                 memcpy(temp, buffer, size);
  299.                 head->data = temp;
  300.                 head->size = size;
  301.                 head = head->next;
  302.                 pthread_mutex_unlock(&buf_mutex);
  303.                 sem_post(&loop_sem);
  304.                 total++;
  305.         }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement