Advertisement
wtfbbq

ssdpscan.c

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