From: Bill Hendrickson <wjhendrickson@gmail.com>
To: netfilter list <netfilter@vger.kernel.org>
Subject: SSH Port Forwarding with iptables
Date: Tue, 29 Sep 2009 11:16:45 -0400 [thread overview]
Message-ID: <a0017e7e0909290816i35c5e55dw77ec8517232ef276@mail.gmail.com> (raw)
Hey list,
I'm having trouble setting up port forwarding using iptables. This is
my first attempt so I'm not surprised. I want to have a dual-NIC
linux server set up to take incoming SSH requests on a non-standard
port, and forward them to an internal server running SSH on the
standard port. I actually need to have two internal servers set up
this way, and handling HTTP redirects as well, but I'll settle for one
working SSH server.
This is my setup:
EXTERNAL_PC<===>SWITCH<===>GATEWAY_SVR<== x/o ==>INTERNAL_SVR
EXTERNAL_PC
eth0:172.17.17.9
SWITCH:
Linksys SD2008 (no ipaddr)
GATEWAY_SVR
eth0:172.17.17.165
eth1:172.16.0.165
INTERNAL_SVR
eth0:172.16.0.101
I poured thru tutorials and other googled bounty, and this is what I
came up with for my iptables script (hand-typed, not cut-and-paste):
<<<<<<<<<<<<<<>>>>>>>>>>>>
#!/bin/bash
modprobe iptable_nat
echo 1 > /proc/sys/net/ipv4/ip_forward
EXT_IFACE="eth0"
EXT_IP="172.17.17.165"
INT_IFACE="eth1"
SSH_IP="172.16.0.101"
SSH_PORT="2203"
IPT="/sbin/iptables"
$IPT -F
$IPT -F -t nat
$IPT -X
$IPT -A OUTPUT -o $EXT_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -o $INT_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT
# wide open, for testing...
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
$IPT -t nat -A PREROUTING -p tcp -i $EXT_IFACE -d $EXT_IP --dport
$SSH_PORT --sport 1024:65535 -j DNAT --to $SSH_HOST:22
$IPT -A FORWARD -p tcp -i $EXT_IFACE -o $INT_IFACE -d $SSH_HOST
--dport $SSH_PORT --sport 1024:65535 -m state --state NEW -j ACCEPT
$IPT -A FORWARD -j LOG --log-prefix "meh "
<<<<<<<<<<<<<<>>>>>>>>>>>>
All 3 boxes are running Fedora 8. All three boxes are set up for
static IP: there is no default gateway defined for any box, and the
netmask for all three is 255.255.255.0. There is no DNS/internet
involved (at least /etc/resolv.conf is empty). TCP_wrappers are
disabled. I can successfully SSH from GATEWAY_SVR to INTERNAL_SVR no
problem. I can also SSH from EXTERNAL_PC to GATEWAY_SVR no problem.
But when I try to SSH from EXTERNAL_PC directly to INTERNAL_SVR, via
the redirected port, e.g.:
root@EXTERNAL_PC # ssh -p 2203 172.17.17.165
it just hangs, until SSH times out. I have Wireshark running on
INTERNAL_SVR, so I know the packets are getting there. They just seem
to get stuck on the return path, at the GATEWAY_SVR box.
This is what is getting logged on GATEWAY_SVR, in /var/log/messages:
meh IN=eth0 OUT=eth1 SRC=172.17.17.9 DST=172.16.0.101 LEN=60 TOS=0x00
PREC=0x00 TTL=63 ID=53921 DF PROTO=TCP SPT=34718 DPT=22 WINDOW=5840
RES=0x00 SYN URGP=0
meh IN=eth0 OUT=eth1 SRC=172.17.17.9 DST=172.16.0.101 LEN=44 TOS=0x00
PREC=0x00 TTL=45 ID=24728 PROTO=TCP SPT=58967 DPT=22 WINDOW=3072
RES=0x00 SYN URGP=0
meh IN=eth0 OUT=eth1 SRC=172.17.17.9 DST=172.16.0.101 LEN=44 TOS=0x00
PREC=0x00 TTL=46 ID=20175 PROTO=TCP SPT=58968 DPT=22 WINDOW=4096
RES=0x00 SYN URGP=0
If I nmap port 2203 on GATEWAY_SVR from EXTERNAL_PC, it reports the
state as "filtered".
The infuriating thing is, I had it working briefly. Then I tried to
add two more internal SSH servers, but those dogs did not hunt - they
were "filtered", although the first one still worked. Then I dorked
things all up, and can get nothing to work now.
Can anyone help?
-bill
next reply other threads:[~2009-09-29 15:16 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-29 15:16 Bill Hendrickson [this message]
2009-09-29 16:14 ` SSH Port Forwarding with iptables Bill Hendrickson
2009-10-01 10:37 ` Pascal Hambourg
2009-10-01 15:24 ` Bill Hendrickson
2009-10-01 22:07 ` Pascal Hambourg
2009-10-01 16:26 ` Gáspár Lajos
2009-09-29 16:16 ` Gáspár Lajos
2009-09-29 16:41 ` Bill Hendrickson
2009-09-29 17:00 ` Gáspár Lajos
2009-09-29 17:12 ` Bill Hendrickson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a0017e7e0909290816i35c5e55dw77ec8517232ef276@mail.gmail.com \
--to=wjhendrickson@gmail.com \
--cc=netfilter@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox