Networking Forums

Networking Forums > Computer Networking > Linux Networking > forwarding ssh connections through a gateway

Reply
Thread Tools Display Modes

forwarding ssh connections through a gateway

 
 
bad_knee
Guest
Posts: n/a

 
      05-11-2004, 06:30 PM
Hello All.

I was playing with iptables and the 'ssh -p' command today trying to
forward external ssh client connections to a box on a different
network, behind a dual-homed gateway. Pretty groovy, thought I would
post it here in case anyone else can use it.

One problem is the ssh client side is getting the key back from the
box behind the gateway, and not the same server key as a previous
connect to the gateway itself returns, so it appears as a spoofed
connection. (which it is!) One way around this is to specify the ip
address of the host instead of the hostname itself. This way you get
two keys for the same host in your .ssh/known_hosts file, when you are
actually getting to connect to two different machines using one host,
and two different destination ports. eg:

ssh barney == barney's key
ssh barney:1222 == fred's key (ssh client thinks it should have
barney's key)

after setting this up, an ssh barney -p 1222 will ssh me to fred,
through barney, even though fred is on a completely different network.

cheers

-----------------------------------------------------------------------------

|
|
[Fred:192.168.1.4] | [Wilma:192.168.1.3]
| | |
| | |
[Hub/Switch]
|
|
(Barney:192.168.1.1 INTIF)
[ ]
[ ]
[ BARNEY GW ]
[ ]
[ ]
(Barney:217.212.212.98 EXTIF)
|
|
|
|
[.oO0 217.212.212.0 network 0Oo.]


---8<------8<------8<------8<------8<------8<------8<------8<------8<---
#!/bin/bash

## (watch for linebreaks in posting !!)

FRED="192.168.1.4"
INTIF="eth1"
EXTIF="eth0"
IPTABLES="/usr/sbin/iptables"

# function to forward from barney <=> fred
forward_ssh()
{
echo "ssh forwarding for port 1222"
# ssh ports
$IPTABLES -t nat -A PREROUTING -i $EXTIF -p tcp --dport 1222 -j DNAT
--to-destination $FRED:22

# accept forwarding to external
$IPTABLES -A FORWARD -s $FRED -i $INTIF -o $EXTIF -p tcp --sport 22
-j ACCEPT

# accept forwarding to internal
$IPTABLES -A FORWARD -i $EXTIF -p tcp --dport 22 -d $FRED -j ACCEPT
}


# setup for masq
setup_ip_nat()
{
echo "nat"
$IPTABLES -t nat -F

#/sbin/depmod -a
/sbin/insmod ip_tables 2>&1 | tee > /dev/null
/sbin/insmod ip_conntrack 2>&1 | tee > /dev/null
/sbin/insmod ip_conntrack_ftp 2>&1 | tee > /dev/null
/sbin/insmod ip_conntrack_irc 2>&1 | tee > /dev/null
/sbin/insmod iptable_nat 2>&1 | tee > /dev/null
/sbin/insmod ip_nat_ftp 2>&1 | tee > /dev/null

# forwarding enabled
echo "1" > /proc/sys/net/ipv4/ip_forward

$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE
}




# flush rules, set defaults
$IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT

echo "output"
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT

echo "forward"
$IPTABLES -P FORWARD DROP
#$IPTABLES -P FORWARD ACCEPT
$IPTABLES -F FORWARD


# call ip_masq setup
# be sure to setup nat first or prerouting wont work
setup_ip_nat

# forward cons from gw to fred for ssh connects
forward_ssh
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default gateway and PPTP Connections Scooty Windows Networking 1 03-06-2007 10:15 PM
DNAT forwarding from two Internet connections / uplinks (fwd) S P Arif Sahari Wibowo Linux Networking 2 10-05-2005 08:24 PM
forwarding gateway setup: force routes nirnimesh@gmail.com Linux Networking 2 09-11-2005 09:58 PM
VPN Connections and port forwarding Carolina On-Site Windows Networking 5 06-10-2005 07:39 PM
Gateway/forwarding problem between subnetworks Aymeric Duclert Linux Networking 1 08-16-2003 11:24 PM



1 2 3 4 5 6 7 8 9 10 11