Advertisement
KhaosBringer

nat-pmp Amp Scanner.c

Nov 24th, 2018
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.17 KB | None | 0 0
  1. /* NAT-PMP SCANNER SCRIPT */
  2. #include <pcap.h>
  3. #include <stdio.h>
  4. #include <stdlib.h> // for exit()
  5. #include <string.h> //for memset
  6. #include <sys/ioctl.h>
  7. #include <net/if.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h> // for inet_ntoa()
  10. #include <net/ethernet.h>
  11. #include <netinet/udp.h> //Provides declarations for udp header
  12. #include <netinet/ip.h>  //Provides declarations for ip header
  13. #include <pthread.h>
  14. #include <semaphore.h>
  15. #include <signal.h>
  16. #include <sys/resource.h>
  17. #include <unistd.h>
  18.  
  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) == 5351)
  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.         strcpy(buf, "\x00\x00");
  88.         int sizeofpayload = 2;
  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(5351);
  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.         sleep(1);
  139.         printf("Starting Scan...\n");
  140.         char *temp = (char *)malloc(17);
  141.         memset(temp, 0, 17);
  142.         sprintf(temp, "Found");
  143.         printf("%-16s", temp);
  144.         memset(temp, 0, 17);
  145.         sprintf(temp, "Host/s");
  146.         printf("%-16s", temp);
  147.         memset(temp, 0, 17);
  148.         sprintf(temp, "B/s");
  149.         printf("%-16s", temp);
  150.         memset(temp, 0, 17);
  151.         sprintf(temp, "Running Thrds");
  152.         printf("%-16s", temp);
  153.         memset(temp, 0, 17);
  154.         sprintf(temp, "Done");
  155.         printf("%s", temp);
  156.         printf("\n");
  157.  
  158.         char *new;
  159.         new = (char *)malloc(16*6);
  160.         while (running_threads > 0)
  161.         {
  162.                 printf("\r");
  163.                 memset(new, '\0', 16*6);
  164.                 sprintf(new, "%s|%-15lu", new, found_srvs);
  165.                 sprintf(new, "%s|%-15d", new, scanned);
  166.                 sprintf(new, "%s|%-15d", new, bytes_sent);
  167.                 sprintf(new, "%s|%-15d", new, running_threads);
  168.                 memset(temp, 0, 17);
  169.                 int percent_done=((double)(hosts_done)/(double)(toscan))*100;
  170.                 sprintf(temp, "%d%%", percent_done);
  171.                 sprintf(new, "%s|%s", new, temp);
  172.                 printf("%s", new);
  173.                 fflush(stdout);
  174.                 bytes_sent=0;
  175.                 scanned = 0;
  176.                 sleep(1);
  177.         }
  178.         printf("\n");
  179.         fclose(fd);
  180.         exit(0);
  181. }
  182.  
  183. int main(int argc, char *argv[ ])
  184. {
  185.         if(argc < 6){
  186.                 fprintf(stderr, "Invalid parameters!\n");
  187.                 fprintf(stdout, "Usage: %s <ip range start (192.168.0.0)> <ip range end (192.168.255.255)> <outfile> <threads> <scan delay in ms>\n", argv[0]);
  188.                 exit(-1);
  189.         }
  190.         fd = fopen(argv[3], "a");
  191.         sleep_between = atoi(argv[5]);
  192.  
  193.         int num_threads = atoi(argv[4]);
  194.  
  195.         const rlim_t kOpenFD = 1024 + (num_threads * 2);
  196.         struct rlimit rl;
  197.         int result;
  198.         rl.rlim_cur = kOpenFD;
  199.         rl.rlim_max = kOpenFD;
  200.         result = setrlimit(RLIMIT_NOFILE, &rl);
  201.         if (result != 0)
  202.         {
  203.                 perror("setrlimit_nofile");
  204.                 fprintf(stderr, "setrlimit_nofile returned result = %d\n", result);
  205.         }
  206.         bzero(&rl, sizeof(struct rlimit));
  207.         rl.rlim_cur = 256 * 1024;
  208.         rl.rlim_max = 4096 * 1024;
  209.         result = setrlimit(RLIMIT_STACK, &rl);
  210.         if (result != 0)
  211.         {
  212.                 perror("setrlimit_stack");
  213.                 fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
  214.         }
  215.  
  216.         signal(SIGINT, &sighandler);
  217.  
  218.         pcap_if_t *alldevsp;
  219.         pcap_t *handle; //Handle of the device that shall be sniffed
  220.  
  221.         char errbuf[100] , *devname , devs[100][100];
  222.         int count = 1 , n;
  223.  
  224.         if( pcap_findalldevs( &alldevsp , errbuf) )
  225.         {
  226.                 exit(1);
  227.         }
  228.  
  229.         devname = alldevsp->name;
  230.         ipv4 = malloc(16);
  231.         bzero(ipv4, 16);
  232.         struct ifreq ifc;
  233.     int res;
  234.     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  235.  
  236.         if(sockfd < 0) exit(-1);
  237.     strcpy(ifc.ifr_name, devname);
  238.     res = ioctl(sockfd, SIOCGIFADDR, &ifc);
  239.     close(sockfd);
  240.         if(res < 0) exit(-1);
  241.     strcpy(ipv4, inet_ntoa(((struct sockaddr_in*)&ifc.ifr_addr)->sin_addr));
  242.         printf("Opening device %s for sniffing ... " , devname);
  243.         handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
  244.  
  245.         if (handle == NULL)
  246.         {
  247.                 fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
  248.                 exit(1);
  249.         }
  250.         printf("Done\n");
  251.  
  252.         sem_init(&loop_sem, 0, -1);
  253.         i = 1024*1000;
  254.         while(i--)
  255.         {
  256.                 if(head == NULL)
  257.                 {
  258.                         head = (struct buffer *)malloc(sizeof(struct buffer));
  259.                         bzero(head, sizeof(struct buffer));
  260.                         head->data = NULL;
  261.                         head->size = 0;
  262.                         head->next = head;
  263.                         head->prev = head;
  264.                 } else {
  265.                         struct buffer *new_node = (struct buffer *)malloc(sizeof(struct buffer));
  266.                         bzero(new_node, sizeof(struct buffer));
  267.                         new_node->data = NULL;
  268.                         new_node->size = 0;
  269.                         new_node->prev = head;
  270.                         new_node->next = head->next;
  271.                         head->next = new_node;
  272.                 }
  273.         }
  274.  
  275.         pthread_t prnthread;
  276.         pthread_create( &prnthread, NULL, &printthread, (void *)argv);
  277.         pthread_t redthread;
  278.         pthread_create( &redthread, NULL, &readthread, NULL);
  279.  
  280.         pcap_loop(handle , -1 , process_packet , NULL);
  281.  
  282.         return 0;
  283. }
  284.  
  285. void process_packet(void *args, struct pcap_pkthdr *header, void *buffer)
  286. {
  287.         int size = header->len;
  288.  
  289.         //Get the IP Header part of this packet , excluding the ethernet header
  290.         struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
  291.         memset(&dest, 0, sizeof(dest));
  292.         dest.sin_addr.s_addr = iph->daddr;
  293.  
  294.         if(iph->protocol == 17 && strcmp(inet_ntoa(dest.sin_addr), ipv4) == 0)
  295.         {
  296.                 //toss into buffer
  297.                 if(head->data != NULL) over++;
  298.                 pthread_mutex_lock(&buf_mutex);
  299.                 void *temp = malloc(size);
  300.                 memcpy(temp, buffer, size);
  301.                 head->data = temp;
  302.                 head->size = size;
  303.                 head = head->next;
  304.                 pthread_mutex_unlock(&buf_mutex);
  305.                 sem_post(&loop_sem);
  306.                 total++;
  307.         }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement