home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / pipefakeps.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  5.1 KB  |  199 lines

  1.  
  2. /*
  3.  
  4. The following is an interesting snippet of code I wrote recently. It
  5. makes a data pipe between a listen port on the machine it's being run on
  6. and a port on a remote machine. For example, running
  7.   datapipe 2222 23 your.machine.com
  8.  
  9. would create a port 2222 on the local machine that, if telnetted to, would
  10. be the same as telnetting to port 23 on your.machine.com. This can be used
  11. for a variety of purposes: redirect IRC connections so that identd shows
  12. the username of the datapipe process; redirect sendmail direct connections
  13. for the same reason; even use on a firewall machine to give access to an
  14. internal service (ftpd, for instance). Cascaded datapipes make for
  15. interesting traceback dilemmas. Questions and comments accepted.
  16.  
  17. Compile with:
  18.     cc -o datapipe -O datapipe.c
  19. On boxes without strerror() (like SunOS 4.x), compile with:
  20.     cc -o datapipe -O -DSTRERROR datapipe.c
  21.  
  22. Run as:
  23.     datapipe localport remoteport remotehost [fakeps (tested on Linux)]
  24.  
  25. It will fork itself into the background.
  26.  
  27. /*
  28.  * Datapipe - Create a listen socket to pipe connections to another
  29.  * machine/port. 'localport' accepts connections on the machine running
  30.  * datapipe, which will connect to 'remoteport' on 'remotehost'. Fairly
  31.  * standard 500 xxxx extended errors are used if something drastic
  32.  * happens.
  33.  *
  34.  * (c) 1995 Todd Vierling
  35.  * fakeps no(c) 1998 fusys
  36.  * 
  37.  * Define STRERROR while compiling on a SunOS 4.x box
  38.  */
  39.  
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/wait.h>
  43. #include <netinet/in.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <errno.h>
  47. #include <unistd.h>
  48. #include <netdb.h>
  49. #include <time.h>
  50.  
  51. #ifdef STRERROR
  52. extern char *sys_errlist[];
  53. extern int sys_nerr;
  54. char *undef = "Undefined error";
  55.  
  56. char *strerror(error)
  57.   int error;
  58. {
  59.   if (error > sys_nerr)
  60.     return undef;
  61.   return sys_errlist[error];
  62. }
  63. #endif
  64. #define CIAO_PS "bfi_2"
  65.  
  66. main(argc, argv)
  67.   int argc;
  68.   char **argv;
  69. {
  70.   int lsock, csock, osock;
  71.   FILE *cfile;
  72.   char buf[4096];
  73.   struct sockaddr_in laddr, caddr, oaddr;
  74.   int caddrlen = sizeof(caddr);
  75.   fd_set fdsr, fdse;
  76.   struct hostent *h;
  77.   struct servent *s;
  78.   int nbyt;
  79.   unsigned long a;
  80.   unsigned short oport;
  81.   int i, j, argvlen;
  82.   char *bfiargv[argc+1];
  83.   char *fintops = CIAO_PS ;
  84.  
  85.   if (argc < 4) {
  86.     fprintf(stderr,"Usage: %s localport remoteport remotehost fakeps\n",argv[0]);
  87.     return 30;
  88.   }
  89.  
  90.   for(i=0; i < argc; i++) {
  91.     bfiargv[i] = malloc(strlen(argv[i]) + 1);
  92.     strncpy(bfiargv[i], argv[i], strlen(argv[i]) + 1);
  93.    }
  94.   bfiargv[argc] = NULL;
  95.   argvlen = strlen(argv[0]);
  96.   if (argvlen < strlen(CIAO_PS)) {
  97.     printf("Se vuoi fregare davvero ps vedi di lanciarmi almeno come superFunkyDataPipe !\n") ;
  98.     abort();
  99.    }
  100.   if(bfiargv[4]) fintops=bfiargv[4] ;
  101.   strncpy(argv[0], fintops, strlen(fintops));
  102.   for(i = strlen(fintops); i < argvlen; i++) argv[0][i] = '\0';
  103.   for(i=1; i < argc; i++) {
  104.     argvlen = strlen(argv[i]);
  105.     for(j=0; j <= argvlen; j++)
  106.       argv[i][j] = '\0';
  107.    }
  108.  
  109.   a = inet_addr(argv[3]);
  110.   if (!(h = gethostbyname(bfiargv[3])) &&
  111.       !(h = gethostbyaddr(&a, 4, AF_INET))) {
  112.     perror(bfiargv[3]);
  113.     return 25;
  114.   }
  115.   oport = atol(bfiargv[2]);
  116.   laddr.sin_port = htons((unsigned short)(atol(bfiargv[1])));
  117.   if ((lsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  118.     perror("socket");
  119.     return 20;
  120.   }
  121.   laddr.sin_family = htons(AF_INET);
  122.   laddr.sin_addr.s_addr = htonl(0);
  123.   if (bind(lsock, &laddr, sizeof(laddr))) {
  124.     perror("bind");
  125.     return 20;
  126.   }
  127.   if (listen(lsock, 1)) {
  128.     perror("listen");
  129.     return 20;
  130.   }
  131.   if ((nbyt = fork()) == -1) {
  132.     perror("fork");
  133.     return 20;
  134.   }
  135.   if (nbyt > 0) 
  136.     return 0;
  137.   setsid();
  138.   while ((csock = accept(lsock, &caddr, &caddrlen)) != -1) {
  139.     cfile = fdopen(csock,"r+");
  140.     if ((nbyt = fork()) == -1) {
  141.       fprintf(cfile, "500 fork: %s\n", strerror(errno));
  142.       shutdown(csock,2);
  143.       fclose(cfile);
  144.       continue;
  145.     }
  146.     if (nbyt == 0)
  147.       goto gotsock;
  148.     fclose(cfile);
  149.     while (waitpid(-1, NULL, WNOHANG) > 0);
  150.   }
  151.   return 20;
  152.  
  153.  gotsock:
  154.   if ((osock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  155.     fprintf(cfile, "500 socket: %s\n", strerror(errno));
  156.     goto quit1;
  157.   }
  158.   oaddr.sin_family = h->h_addrtype;
  159.   oaddr.sin_port = htons(oport);
  160.   memcpy(&oaddr.sin_addr, h->h_addr, h->h_length);
  161.   if (connect(osock, &oaddr, sizeof(oaddr))) {
  162.     fprintf(cfile, "500 connect: %s\n", strerror(errno));
  163.     goto quit1;
  164.   }
  165.   while (1) {
  166.     FD_ZERO(&fdsr);
  167.     FD_ZERO(&fdse);
  168.     FD_SET(csock,&fdsr);
  169.     FD_SET(csock,&fdse);
  170.     FD_SET(osock,&fdsr);
  171.     FD_SET(osock,&fdse);
  172.     if (select(20, &fdsr, NULL, &fdse, NULL) == -1) {
  173.       fprintf(cfile, "500 select: %s\n", strerror(errno));
  174.       goto quit2;
  175.     }
  176.     if (FD_ISSET(csock,&fdsr) || FD_ISSET(csock,&fdse)) {
  177.       if ((nbyt = read(csock,buf,4096)) <= 0)
  178.         goto quit2;
  179.       if ((write(osock,buf,nbyt)) <= 0)
  180.         goto quit2;
  181.     } else if (FD_ISSET(osock,&fdsr) || FD_ISSET(osock,&fdse)) {
  182.       if ((nbyt = read(osock,buf,4096)) <= 0)
  183.         goto quit2;
  184.       if ((write(csock,buf,nbyt)) <= 0)
  185.         goto quit2;
  186.     }
  187.   }
  188.  
  189.  quit2:
  190.   shutdown(osock,2);
  191.   close(osock);
  192.  quit1:
  193.   fflush(cfile);
  194.   shutdown(csock,2);
  195.  quit0:
  196.   fclose(cfile);
  197.   return 0;
  198. }
  199.