LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 09-26-2006, 11:53 AM   #1
Garak
LQ Newbie
 
Registered: Feb 2003
Location: Seattle, WA
Posts: 16

Rep: Reputation: 1
iptables port forwarding issue


I am trying to port forward all connections to our WAN side IP address using port 81 to an internal web server listening on port 80. The two statements I'm using below do work but I am wondering two things:

1) Is the forwarding statement necessary?
2) If the forwarding statement is necessary, should it be forwarding port 80 or port 81?


iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 81 -j DNAT --to 192.168.0.35:80
iptables -A FORWARD -p tcp -i eth0 -d 192.168.0.35 --dport 80 -j ACCEPT


Sometimes I get confused on how a packet travels through the firewall. Thanks in advance.
 
Old 09-26-2006, 01:10 PM   #2
amitsharma_26
Member
 
Registered: Sep 2005
Location: New delhi
Distribution: RHEL 3.0/4.0
Posts: 777

Rep: Reputation: 31
Quote:
Originally Posted by Garak
I am trying to port forward all connections to our WAN side IP address using port 81 to an internal web server listening on port 80. The two statements I'm using below do work but I am wondering two things:

1) Is the forwarding statement necessary?
2) If the forwarding statement is necessary, should it be forwarding port 80 or port 81?


iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 81 -j DNAT --to 192.168.0.35:80
iptables -A FORWARD -p tcp -i eth0 -d 192.168.0.35 --dport 80 -j ACCEPT


Sometimes I get confused on how a packet travels through the firewall. Thanks in advance.

Ans 1
: Forwarding rules are'nt necesary if your default forward policy isnt DROP or any other FORWARD rule isnt blocking these particular packets. Anyhow if any packet isnt meant for your box.. it goes through PREROUTING -> FORWARD -> POSTROUTING & hence a block at FORWARD could stop these packets to further traverse.

Ans 2: Forwarding port 80 or 81 is a dependant situation on what you require & is also dependant on what is your existing FORWARD policy & other rules. e.g. If i have my FORWARD policy as DROP, then i would need to allow them biredirectionally with two rules. Like in your case, i got to allow DPORT 80 in @ FORWARD & either established/related packets back OUT or sport 80 BACK.

I hope this might clear your some confusion.

You could get good piece of information about NATing, iptables port forwarding here at http://amitsharma.linuxbloggers.com/portforwarding.htm
__________________
With best regards,
Amit..
--
Quote:
Originally Posted by Albert einstein
Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.
--
RSYNC tutorial : http://www.amitsharma.linuxbloggers.com/how_to_rsync.htm
FIND command tutorial : http://amitsharma.linuxbloggers.com/how_to_find.htm
Samba tutorial : http://www.amitsharma.linuxbloggers.com/how_to_samba.htm
Port forwarding tutorial: http://amitsharma.linuxbloggers.com/portforwarding.htm

Last edited by amitsharma_26; 09-26-2006 at 05:04 PM.
 
Old 09-26-2006, 01:31 PM   #3
Capt_Caveman
Senior Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 3,658

Rep: Reputation: 69
Quote:
1) Is the forwarding statement necessary?
If the packet will be getting sent to some other host, then you will need a forwarding rule (or policy) to accept the packet.

Quote:
2) If the forwarding statement is necessary, should it be forwarding port 80 or port 81?
The packet will be handled by the FORWARD chain after it has been processed by the nat table (where PREROUTING happens). So if you are altering the dest port in PREROUTING, then when the packet hits the FORWARD chain it will have the new port number (80)

Quote:
Sometimes I get confused on how a packet travels through the firewall.
http://iptables-tutorial.frozentux.n...ERSINGOFTABLES
 
Old 09-26-2006, 02:00 PM   #4
Garak
LQ Newbie
 
Registered: Feb 2003
Location: Seattle, WA
Posts: 16

Original Poster
Rep: Reputation: 1
Thanks for the info guys. That does clear it up. My default policies are set to DROP for all chains so I'm leaving the forwarding rule in. I also hadn't thought about the destination port being changed before the next chain so that clears up question two for me. Thanks for the help.
 
Old 09-26-2006, 02:18 PM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by Garak
My default policies are set to DROP for all chains
that's a very good policy (pun intended)...

Quote:
so I'm leaving the forwarding rule in
cool, just remember that you'll need a rule for packets which are going back to the client from the server... you can do it like this (using stateful packet filtering):

Code:
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 81 \
-j DNAT --to 192.168.0.35:80

iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A FORWARD -p tcp -i eth0 -o eth1 -d 192.168.0.35 --dport 80 \
-m state --state NEW -j ACCEPT
also notice how i made the FORWARD rule a little bit more specific by specifying the outgoing interface for the packet... being as specific as you can is also a good policy...

just my ...

Last edited by win32sux; 09-26-2006 at 02:26 PM.
 
Old 09-29-2006, 03:20 PM   #6
slimjim
Member
 
Registered: Apr 2004
Location: east midlands, england
Distribution: Ubuntu 9.04
Posts: 56

Rep: Reputation: 15
Useful information chaps, but it's not working for me, trying to do much the same thing.

I'm doing the following:

Code:
iptables -F

iptables -t nat -A PREROUTING -p tcp -i lo --dport 3306 -j DNAT --to 192.168.7.8
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -p tcp -i lo -o eth1 -d 192.168.7.8 --dport 3306 -m state --state NEW -j ACCEPT
.. to forward port 3306 (mysql) to a separate mysql server, but it's not happening sadly. I've tested by doing telnet localhost 3306 but I just get a connection refused message, whereas I'd expect to get the same gibberish response from mysqld that telnet 192.168.7.8 3306 gives.

I've checked that port fowarding is enabled:

[root@www root]# cat /proc/sys/net/ipv4/ip_forward
1
[root@www root]#

.. any clues, anyone?
 
Old 09-29-2006, 03:34 PM   #7
amitsharma_26
Member
 
Registered: Sep 2005
Location: New delhi
Distribution: RHEL 3.0/4.0
Posts: 777

Rep: Reputation: 31
Quote:
Originally Posted by slimjim
Useful information chaps, but it's not working for me, trying to do much the same thing.

I'm doing the following:

Code:
iptables -F

iptables -t nat -A PREROUTING -p tcp -i lo --dport 3306 -j DNAT --to 192.168.7.8
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -p tcp -i lo -o eth1 -d 192.168.7.8 --dport 3306 -m state --state NEW -j ACCEPT
.. to forward port 3306 (mysql) to a separate mysql server, but it's not happening sadly. I've tested by doing telnet localhost 3306 but I just get a connection refused message, whereas I'd expect to get the same gibberish response from mysqld that telnet 192.168.7.8 3306 gives.

I've checked that port fowarding is enabled:

[root@www root]# cat /proc/sys/net/ipv4/ip_forward
1
[root@www root]#

.. any clues, anyone?
use this..
Code:
iptables -t nat -A OUTPUT -p tcp --dport 3306 -j DNAT --to 192.168.7.8

__________________
With best regards,
Amit..
--
Quote:
Originally Posted by Albert einstein
Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.
--
Port forwarding tutorial: http://amitsharma.linuxbloggers.com/portforwarding.htm
 
Old 09-29-2006, 04:31 PM   #8
slimjim
Member
 
Registered: Apr 2004
Location: east midlands, england
Distribution: Ubuntu 9.04
Posts: 56

Rep: Reputation: 15
Hmm, progress! Thanks for that!

I can now telnet to localhost 3306 and go through to the mysql server. Unfortunately the webpage hosted by the server gives an error, where it didn't with a local mysqld running ..

Code:
Warning: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var2/copied/home/httpd/vhosts/propertysalesonline.com/httpdocs/Scripts/PHP/dataconnect.php on line 2

Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var2/copied/home/httpd/vhosts/propertysalesonline.com/httpdocs/Scripts/PHP/dataconnect.php on line 2
Couldn't make connection.
however that's probably for a different topic, unless someone has a quick answer!

Many thanks,
James
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
IPCHAINS port forwarding and IPTABLES port forwarding ediestajr Linux - Networking 26 01-14-2007 07:35 PM
iptables and port forwarding cs.cracker Linux - Networking 5 09-02-2006 01:03 PM
IPTables port forwarding.. NeoTech Linux - Networking 2 01-03-2005 11:27 AM
iptables port forwarding MadTurki Linux - Networking 6 01-05-2004 01:03 PM
port forwarding with iptables David_99 Linux - Security 5 12-09-2003 08:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration