Outils personnels

Reverse ssh : Accéder à un serveur derrière un NAT - Firewall : Différence entre versions

De wikiGite

(Reverse ssh)
Ligne 39 : Ligne 39 :
  
 
<source lang="bash">
 
<source lang="bash">
#! /bin/bash
+
#!/bin/bash
### BEGIN INIT INFO
 
# Provides:          autosshd
 
# Required-Start:    $local_fs $remote_fs $network $syslog
 
# Required-Stop:    $local_fs $remote_fs $network $syslog
 
# Default-Start:    2 3 4 5
 
# Default-Stop:      0 1 6
 
# Short-Description: starts the autossh daemon
 
# Description:      starts autossh
 
### END INIT INFO
 
  
DAEMON_OPTS="-i /root/.ssh/id_dsa -NR 22222:localhost:22 userssh@serveurB &"
+
 
NAME=autossh
+
PATH=/sbin:/bin:/usr/sbin:/usr/bin
 +
DAEMON="/usr/bin/autossh"
 
DESC="Autossh daemon"
 
DESC="Autossh daemon"
PID=/tmp/autossh.pid
+
PIDFOLDER="/var/run/autossh"
 +
PIDFOLDERSSH="$PIDFOLDER/ssh"
 +
REMOTE_USER="userssh"
 +
REMOTE_ADDR="SERVEURB"
 +
LOGFILE="/var/log/autossh.log"
 +
 
 +
if [ ! -d $PIDFOLDER ] ; then
 +
    mkdir -p $PIDFOLDER
 +
fi
 +
 
 +
if [ ! -d $PIDFOLDERSSH ] ; then
 +
    mkdir -p $PIDFOLDERSSH
 +
fi
 +
 
 +
test -f $DAEMON || exit 0
 +
 
 +
. /lib/lsb/init-functions
 +
 
 +
PIDFILE="$PIDFOLDER/$REMOTE_USER-$REMOTE_ADDR.pid"
 +
PIDFILESSH="$PIDFOLDERSSH/$REMOTE_USER-$REMOTE_ADDR.pid"
 +
 
 +
is_running() {
 +
    if [ -f $PIDFILE ]; then
 +
        PID=`cat $PIDFILE`
 +
        if [ -n "$PID" ]; then
 +
            return 0
 +
        else
 +
            return 1
 +
        fi
 +
    else
 +
        return 1
 +
    fi
 +
}
 +
 
 +
start_autossh() {
 +
    if ! is_running; then
 +
        echo "Starting $DESC"
 +
        export AUTOSSH_FIRST_POLL=10
 +
        export AUTOSSH_POLL=60
 +
        export AUTOSSH_PIDFILE=$PIDFILESSH
 +
        start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- -M 29000 -i /root/.ssh/id_dsa -X -C -R 22222:localhost:22 $REMOTE_USER@$REMOTE_ADDR >> $LOGFILE 2>&1 &
 +
        sleep 1;
 +
        if ! is_running; then
 +
            echo "$DESC: running @ pid $PID"
 +
        else
 +
            echo 'Something went wrong';
 +
        fi
 +
    else
 +
        echo "$DESC: already running (pid $PID)"
 +
    fi
 +
}
 +
 
 +
stop_autossh() {
 +
    if is_running; then
 +
        echo "Stopping $DESC"
 +
        start-stop-daemon --stop --pidfile $PIDFILE --signal 15
 +
        if [ -f $PIDSSHFILE ]; then
 +
            PIDSSH=`cat $PIDFILESSH`
 +
            kill $PIDSSH
 +
            rm -f $PIDFILESSH
 +
        fi
 +
    else
 +
        echo "$DESC: not running"
 +
    fi
 +
    [ -f $PIDFILE ] && rm -f $PIDFILE
 +
}
  
 
case "$1" in
 
case "$1" in
  start)
+
    start)
         CD_TO_APP_DIR="cd /home/gitlab/gitlab"
+
         start_autossh
         START_DAEMON_PROCESS="/var/lib/gems/1.9.1/bin/bundle exec unicorn_rails $DAEMON_OPTS"
+
    ;;
         START_RESQUE_PROCESS="./resque.sh"
+
    stop)
 
+
        stop_autossh
         echo -n "Starting $DESC: "
+
    ;;
         if [ `whoami` = root ]; then
+
    force-reload|restart)
          sudo -u gitlab sh -c "$CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS"
+
        stop_autossh
 +
         start_autossh
 +
    ;;
 +
    status)
 +
         if is_running; then
 +
            echo "$DESC: running (pid $PID)"
 +
            exit 0
 +
         else
 +
            echo "$DESC: not running"
 +
            [ -f $PIDFILE ] && exit 1 || exit 3
 +
        fi
 +
    ;;
 +
    log)
 +
         if [ -f $LOGIFLE ]; then
 +
            tail $LOGFILE
 
         else
 
         else
          $CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS
+
            echo "log file '$LOGFILE' does't exist"
 
         fi
 
         fi
        echo "$NAME."
+
    ;;
        ;;
+
    *)
  stop)
+
         echo "Usage: $0 {start|stop|restart|force-reload|status|log}"
        echo -n "Stopping $DESC: "
+
         exit 3
        kill -QUIT `cat $PID`
+
    ;;
        kill -QUIT `cat $RESQUE_PID`
 
        echo "$NAME."
 
        ;;
 
  restart)
 
        echo -n "Restarting $DESC: "
 
        kill -USR2 `cat $PID`
 
        kill -USR2 `cat $RESQUE_PID`
 
        echo "$NAME."
 
        ;;
 
  reload)
 
        echo -n "Reloading $DESC configuration: "
 
        kill -HUP `cat $PID`
 
        kill -HUP `cat $RESQUE_PID`
 
        echo "$NAME."
 
        ;;
 
  *)
 
         echo "Usage: $NAME {start|stop|restart|reload}" >&2
 
         exit 1
 
        ;;
 
 
esac
 
esac
  
 
exit 0
 
exit 0
 +
 
</source>
 
</source>
  

Version du 25 avril 2012 à 15:11

Fonctionnement

Reverse.png


Le serveur A se trouvant derriere le par-feu créé un tunnel vers le serveur B. Depuis B on se connecte au serveur A au travers du tunnel ssh.

Prérequis

Ajouter cette ligne dans /etc/ssh/sshd_config :

AllowTcpForwarding yes

Par sécurité créer un utilisateur dédié au tunnel sur B :

adduser userssh


Reverse ssh

Le port 22222 de l'exemple suivant doit se trouver entre 1024 et 65535. Il faut evidement tenir une liste des ports accociés aux machines.

Créez le tunnel sur le serveur A :

ssh -NR 22222:localhost:22 userssh@serveurB

Se connecter au tunnel depuis le serveur B

ssh -p 22222 rootA@127.0.0.1

Service au démarrage de A

aptitude install autossh

Générer une paire de clef avec root

ssh-keygen -t dsa

Faire un echange de clef avec le serveur B:

ssh-copy-id -i /root/.ssh/id_dsa.pub userssh@serveurB

ajouter dans /etc/rc.local :

autossh -i /root/.ssh/id_dsa -NR 22222:localhost:22 userssh@serveurB &


Créer un script dans /etc/init.d/autosshd

#!/bin/bash


PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/bin/autossh"
DESC="Autossh daemon"
PIDFOLDER="/var/run/autossh"
PIDFOLDERSSH="$PIDFOLDER/ssh"
REMOTE_USER="userssh"
REMOTE_ADDR="SERVEURB"
LOGFILE="/var/log/autossh.log"

if [ ! -d $PIDFOLDER ] ; then
    mkdir -p $PIDFOLDER
fi

if [ ! -d $PIDFOLDERSSH ] ; then
    mkdir -p $PIDFOLDERSSH
fi

test -f $DAEMON || exit 0

. /lib/lsb/init-functions

PIDFILE="$PIDFOLDER/$REMOTE_USER-$REMOTE_ADDR.pid"
PIDFILESSH="$PIDFOLDERSSH/$REMOTE_USER-$REMOTE_ADDR.pid"

is_running() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -n "$PID" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

start_autossh() {
    if ! is_running; then
        echo "Starting $DESC"
        export AUTOSSH_FIRST_POLL=10
        export AUTOSSH_POLL=60
        export AUTOSSH_PIDFILE=$PIDFILESSH
        start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- -M 29000 -i /root/.ssh/id_dsa -X -C -R 22222:localhost:22 $REMOTE_USER@$REMOTE_ADDR >> $LOGFILE 2>&1 &
        sleep 1;
        if ! is_running; then
            echo "$DESC: running @ pid $PID"
        else
            echo 'Something went wrong';
        fi
    else
        echo "$DESC: already running (pid $PID)"
    fi
}

stop_autossh() {
    if is_running; then
        echo "Stopping $DESC"
        start-stop-daemon --stop --pidfile $PIDFILE --signal 15
        if [ -f $PIDSSHFILE ]; then
            PIDSSH=`cat $PIDFILESSH`
            kill $PIDSSH
            rm -f $PIDFILESSH
        fi
    else
        echo "$DESC: not running"
    fi
    [ -f $PIDFILE ] && rm -f $PIDFILE
}

case "$1" in
    start)
        start_autossh
    ;;
    stop)
        stop_autossh
    ;;
    force-reload|restart)
        stop_autossh
        start_autossh
    ;;
    status)
        if is_running; then
            echo "$DESC: running (pid $PID)"
            exit 0
        else
            echo "$DESC: not running"
            [ -f $PIDFILE ] && exit 1 || exit 3
        fi
    ;;
    log)
        if [ -f $LOGIFLE ]; then
            tail $LOGFILE
        else
            echo "log file '$LOGFILE' does't exist"
        fi
    ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload|status|log}"
        exit 3
    ;;
esac

exit 0

Autoriser l'execution du script :

chmod +x /etc/init.d/autosshd

Ajouter le script au démarage :

update-rc.d autosshd defaults


Se connecter à d'autres port

On souhaite par exemple se connecter à un serveur web se trouvant sur A.

Sur A :

ssh -NR 22280:localhost:80 userssh@serveurB

Sur B :

firefox "http://127.0.0.1:22280"