home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / internet / bounce.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  7.5 KB  |  243 lines

  1. #define    MY_PASSWORD    "w00p"
  2. #define    SERV_TCP_PORT  31337
  3. /*"-= To enter. Telnet to address and port #. Hit 1x [ENTER] and then"*/
  4. /*"the password. Then Host and port 23 for normal telnet connect. =-"*/ 
  5.  
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/time.h>
  10. #include <sys/resource.h>
  11. #include <sys/wait.h>
  12. #include <fcntl.h>
  13. #include <errno.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <arpa/inet.h>
  17. #include <sys/ioctl.h>
  18.  
  19. #define    QLEN           5
  20.  
  21. char sbuf[2048], cbuf[2048];
  22. extern int errno;
  23. extern char *sys_errlist[];
  24. void reaper();
  25. int main();
  26. void telcli();
  27.  
  28. int main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     int srv_fd, rem_fd, rem_len, opt = 1;
  33.     struct sockaddr_in rem_addr, srv_addr;
  34.  
  35.     bzero((char *) &rem_addr, sizeof(rem_addr));
  36.     bzero((char *) &srv_addr, sizeof(srv_addr));
  37.     srv_addr.sin_family = AF_INET;
  38.     srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  39.     srv_addr.sin_port = htons(SERV_TCP_PORT);
  40.     srv_fd = socket(PF_INET, SOCK_STREAM, 0);
  41.     if (bind(srv_fd, (struct sockaddr *) &srv_addr, sizeof(srv_addr)) == -1) {
  42.         perror("bind");
  43.         exit(-1);
  44.     }
  45.     listen(srv_fd, QLEN);
  46.     close(0); close(1); close(2);
  47. #ifdef TIOCNOTTY
  48.     if ((rem_fd = open("/dev/tty", O_RDWR)) >= 0) {
  49.         ioctl(rem_fd, TIOCNOTTY, (char *)0);
  50.         close(rem_fd);
  51.     }
  52. #endif
  53.     if (fork()) exit(0);
  54.     while (1) {
  55.     rem_len = sizeof(rem_addr);
  56.         rem_fd=accept(srv_fd, (struct sockaddr *) &rem_addr, &rem_len);
  57.         if (rem_fd < 0) {
  58.             if (errno == EINTR) continue;
  59.             exit(-1);
  60.         }
  61.         switch(fork()) {
  62.         case 0:                             /* child process */
  63.             close(srv_fd);                  /* close original socket */
  64.             telcli(rem_fd);                 /* process the request */
  65.             close(rem_fd);
  66.             exit(0);
  67.             break;
  68.         default: 
  69.             close(rem_fd);                  /* parent process */
  70.             if (fork()) exit(0);            /* let init worry about children */
  71.             break;
  72.         case -1:
  73.             fprintf(stderr, "\n\rfork: %s\n\r", sys_errlist[errno]);
  74.             break;
  75.         }
  76.     }
  77. }
  78.  
  79. void telcli(source)
  80. int source;
  81. {
  82.     int dest;
  83.     int found;
  84.     struct sockaddr_in sa;
  85.     struct hostent *hp;
  86.     struct servent *sp;
  87.     char gethost[100];
  88.     char getport[100];
  89.     char string[100];
  90.  
  91.     bzero(gethost, 100);
  92.     read(source, gethost, 100);
  93.     sprintf(string, "");
  94.     write(source, string, strlen(string));
  95.     read(source, gethost, 100);
  96.     gethost[(strlen(gethost)-2)] = '\0'; /* kludge alert - kill the \r\n */
  97.     if (strcmp(gethost, MY_PASSWORD) != 0) {
  98.         close(source);
  99.         exit(0);
  100.     }
  101.     do {
  102.         found = 0;
  103.         bzero(gethost,100);
  104.         sprintf(string, "telnet bouncer ready.\n");
  105.         write(source, string, strlen(string));
  106.         sprintf(string, "Host: ");
  107.         write(source, string, strlen(string));
  108.         read(source, gethost, 100);
  109.         gethost[(strlen(gethost)-2)] = '\0';
  110.         hp = gethostbyname(gethost);
  111.         if (hp) {
  112.             found++;
  113. #if !defined(h_addr)        /* In 4.3, this is a #define */
  114. #if defined(hpux) || defined(NeXT) || defined(ultrix) || defined(POSIX)
  115.             memcpy((caddr_t)&sa.sin_addr, hp->h_addr_list[0], hp->h_length);
  116. #else
  117.             bcopy(hp->h_addr_list[0], &sa.sin_addr, hp->h_length);
  118. #endif
  119. #else /* defined(h_addr) */
  120. #if defined(hpux) || defined(NeXT) || defined(ultrix) || defined(POSIX)
  121.             memcpy((caddr_t)&sa.sin_addr, hp->h_addr, hp->h_length);
  122. #else
  123.             bcopy(hp->h_addr, &sa.sin_addr, hp->h_length);
  124. #endif
  125. #endif /* defined(h_addr) */
  126.             sprintf(string, "Found address for %s\n", hp->h_name);
  127.             write(source, string, strlen(string));
  128.         } else {
  129.             if (inet_addr(gethost) == -1) {
  130.                 found = 0;
  131.                 sprintf(string, "Didnt find address for %s\n", gethost);
  132.                 write(source, string, strlen(string));
  133.             } else {
  134.                 found++;
  135.                 sa.sin_addr.s_addr = inet_addr(gethost);
  136.             }
  137.         }
  138.     } while (!found);
  139.     sa.sin_family = AF_INET;
  140.     sprintf(string, "Port: ");
  141.     write(source, string, strlen(string));
  142.     read(source, getport, 100);
  143.     gethost[(strlen(getport)-2)] = '\0';
  144.     sa.sin_port = htons((unsigned) atoi(getport));
  145.     if (sa.sin_port == 0) {
  146.         sp = getservbyname(getport, "tcp");
  147.         if (sp)
  148.             sa.sin_port = sp->s_port;
  149.         else {
  150.             sprintf(string, "%s: bad port number\n", getport);
  151.             write(source, string, strlen(string));
  152.             return;
  153.         }
  154.     }
  155.     sprintf(string, "Trying %s...\n", (char *) inet_ntoa(sa.sin_addr));
  156.     write(source, string, strlen(string));
  157.     if ((dest = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  158.         perror("telcli: socket");
  159.         exit(1);
  160.     }
  161.     connect(dest, (struct sockaddr *) &sa, sizeof(sa));
  162.     sprintf(string, "Connected to %s port %d...\n", inet_ntoa(sa.sin_addr),
  163.                                                           ntohs(sa.sin_port));
  164.     write(source, string, strlen(string));
  165. #ifdef FNDELAY
  166.     fcntl(source,F_SETFL,fcntl(source,F_GETFL,0)|FNDELAY);
  167.     fcntl(dest,F_SETFL,fcntl(dest,F_GETFL,0)|FNDELAY);
  168. #else
  169.     fcntl(source,F_SETFL,O_NDELAY);
  170.     fcntl(dest,F_SETFL,O_NDELAY);
  171. #endif
  172.     communicate(dest,source);
  173.     close(dest);
  174.     exit(0);
  175. }
  176.  
  177. communicate(sfd,cfd)    {
  178.     char *chead, *ctail, *shead, *stail;
  179.     int num, nfd, spos, cpos;
  180.     extern int errno;
  181.     fd_set rd, wr;
  182.  
  183.     chead = ctail = cbuf;
  184.     cpos = 0;
  185.     shead = stail = sbuf;
  186.     spos = 0;
  187.     while (1) {
  188.         FD_ZERO(&rd);
  189.         FD_ZERO(&wr);
  190.         if (spos < sizeof(sbuf)-1) FD_SET(sfd, &rd);
  191.         if (ctail > chead) FD_SET(sfd, &wr);
  192.         if (cpos < sizeof(cbuf)-1) FD_SET(cfd, &rd);
  193.         if (stail > shead) FD_SET(cfd, &wr);
  194.         nfd = select(256, &rd, &wr, 0, 0);
  195.         if (nfd <= 0) continue;
  196.         if (FD_ISSET(sfd, &rd)) {
  197.             num=read(sfd,stail,sizeof(sbuf)-spos);
  198.             if ((num==-1) && (errno != EWOULDBLOCK)) return;
  199.             if (num==0) return;
  200.             if (num>0) {
  201.                 spos += num;
  202.                 stail += num;
  203.                 if (!--nfd) continue;
  204.             }
  205.         }
  206.         if (FD_ISSET(cfd, &rd)) {
  207.             num=read(cfd,ctail,sizeof(cbuf)-cpos);
  208.             if ((num==-1) && (errno != EWOULDBLOCK)) return;
  209.             if (num==0) return;
  210.             if (num>0) {
  211.                 cpos += num;
  212.                 ctail += num;
  213.                 if (!--nfd) continue;
  214.             }
  215.         }
  216.         if (FD_ISSET(sfd, &wr)) {
  217.             num=write(sfd,chead,ctail-chead);
  218.             if ((num==-1) && (errno != EWOULDBLOCK)) return;
  219.             if (num>0) {
  220.                 chead += num;
  221.                 if (chead == ctail) {
  222.                     chead = ctail = cbuf;
  223.                     cpos = 0;
  224.                 }
  225.                 if (!--nfd) continue;
  226.             }
  227.         }
  228.         if (FD_ISSET(cfd, &wr)) {
  229.             num=write(cfd,shead,stail-shead);
  230.             if ((num==-1) && (errno != EWOULDBLOCK)) return;
  231.             if (num>0) {
  232.                 shead += num;
  233.                 if (shead == stail) {
  234.                     shead = stail = sbuf;
  235.                     spos = 0;
  236.                 }
  237.                 if (!--nfd) continue;
  238.             }
  239.         }
  240.     }
  241. }
  242.  
  243.