Linux Netfilter discussions
 help / color / mirror / Atom feed
From: Josh Cepek <josh.cepek@usa.net>
To: "Irmãos Bocchi & CIA Ltda" <challado@ibocchi.com.br>,
	netfilter@vger.kernel.org
Subject: Re: DNAT iptables bug or connection tracking issue?
Date: Fri, 23 May 2008 17:44:44 -0500	[thread overview]
Message-ID: <483748DC.3090508@usa.net> (raw)
In-Reply-To: <4836D208.9040808@ibocchi.com.br>

[-- Attachment #1: Type: text/plain, Size: 3769 bytes --]

Irmãos Bocchi & CIA Ltda wrote:
> 1) I have two routers in two different networks: one is a FreeBSD 7.0 
> router, here called "Router A" and another is a Debian 4.0 router, 
> here called "Router B"
> 2) The Router A uses pf to make the firewall rules, with standard 
> installation. The Router B have the kernel 2.6.25.4 and iptables 1.4.0
> 3) In the first router, I have a rule to access my vnc server in a 
> windows machine. To make these, I need to create this rule
> rdr on sk0 proto tcp from any to <my external addr> port 5900 -> <my 
> internal addr> port 5900
> nat on sk0 proto tcp from <my internal addr> port 5900 to any -> sk0
>
> In resume: I need to create a rule to make the redirection and, after 
> these, I need to insert a rule to make the nat
>
> 4) In the second router, only adding this rule
> iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 5900 -j DNAT 
> --to-destination <my internal addr> port 5900
>
> THE RULES WORK PERFECTLY!
>
> It's a bug? Because, in my vision, I need to create the two rules, the 
> DNAT rule and the MASQUERADE rule to these work.

Typically when you have a LAN behind NAT you use either MASQUERADE or 
SNAT on all packets going out the public interface.  It doesn't make 
sense to do so only for certain packets; if you intend to filter traffic 
to prevent it from going out to the Internet you should do so in the 
filter table, not by preventing NAT from occurring (which won't 
necessarily stop the packet from being forwarded.)

> Another point of view: If I need to permit only the machines A, B and 
> C to access the VNC, in BSD, I only need to create these rules
>
> my_servers="{ server_a_addr, server_b_addr, server_c_addr }"
> rdr on sk0 proto tcp from any to <my external addr> port 5900 -> <my 
> internal addr> port 5900
> nat on sk0 proto tcp from <my internal addr> port 5900 to $my_servers 
> -> sk0
>
> or
>
> rdr on sk0 proto tcp from $my_servers to <my external addr> port 5900 
> -> <my internal addr> port 5900
> nat on sk0 proto tcp from <my internal addr> port 5900 to any -> sk0
>
> How I can make these in iptables?

The easiest way is probably to create a user-defined chain containing 
tests for the IP's you want to allow to connect to this port.  If you 
include a rule to allow ESTABLISHED traffic prior to the jump to 
vnc_access only the first packet in the connection will need to traverse 
the list of IP's to allow.  Something like this should accomplish the 
task (along with the NAT rule you listed above):

iptables -N vnc_access
iptables -A vnc_access -s server_a_addr -j ACCEPT
iptables -A vnc_access -s server_b_addr -j ACCEPT
iptables -A vnc_access -s server_c_addr -j ACCEPT
iptables -A vnc_access -j DROP

[... other rules may go here ...]

iptables -A FORWARD -d <internal addr> -p tcp --dport 5900 -j vnc_access

If you have many IP's you want to add to the vnc_access chain, another 
way to write that first portion by using a bash for-loop might be as 
follows:

my_servers="server_a_addr server_b_addr server_c_addr"
iptables -N vnc_access
for ip in $my_servers; do
    iptables -A vnc_access -s $ip -j ACCEPT
done
iptables -A vnc_access -j DROP

For many hundreds or thousands of IP's you probably want to look at 
ipsets for a performance increase when processing that many rules.

Finally, note that this will only cause packets to be dropped that are 
sourced from IP's not in your list.  The standard VNC protocol has 
inherently weak security and would still be vulnerable to a 
man-in-the-middle attack potentially exposing your VNC password.  An 
encrypted tunnel such as a VPN or ssh port-forwarding would secure the 
VNC data cryptographically.

-- 
Josh



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

  reply	other threads:[~2008-05-23 22:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-23 14:17 DNAT iptables bug or connection tracking issue? Irmãos Bocchi & CIA Ltda
2008-05-23 22:44 ` Josh Cepek [this message]
  -- strict thread matches above, loose matches on Subject: below --
2008-05-24 14:52 challado

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=483748DC.3090508@usa.net \
    --to=josh.cepek@usa.net \
    --cc=challado@ibocchi.com.br \
    --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