From: "mpboden" Subject: RE: my ftp problem sovled with response from Joel Newkirk... Date: Thu, 6 Feb 2003 18:02:24 -0800 Sender: netfilter-admin@lists.netfilter.org Message-ID: <00b501c2ce4c$f5869730$483918ac@C1006818> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00B2_01C2CE09.E7465950" Return-path: Errors-To: netfilter-admin@lists.netfilter.org List-Help: List-Post: List-Subscribe: , List-Id: List-Unsubscribe: , List-Archive: To: netfilter@lists.netfilter.org This is a multi-part message in MIME format. ------=_NextPart_000_00B2_01C2CE09.E7465950 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thanks to Joel Newkirk, here is the longwinded response he sent me: Heavily snipped and re-commented script: IPTABLES=3D/sbin/iptables LSMOD=3D/sbin/lsmod DEPMOD=3D/sbin/depmod INSMOD=3D/sbin/insmod GREP=3D/bin/grep AWK=3D/bin/awk SED=3D/bin/sed IFCONFIG=3D/sbin/ifconfig EXTIF=3D"eth0" INTIF=3D"eth1" EXTIP=3D"xxx.xxx.xxx.xxx" INTNET=3D"192.168.1.0/24" INTIP=3D"192.168.1.1/24" # This is redundant, since they are both the same number in actual = usage. # It is likely that the intention for INTIP is to hold the IP of $INTIF=20 # interface on this machine, which should not have "/24" at the end. UNIVERSE=3D"0.0.0.0/0" # for what it is worth, this could be "0/0" and would work, but if you # leave it out of rules entirely then this is the default anyway. $DEPMOD -a if [ -z "` $LSMOD | $GREP ip_tables | $AWK {'print $1'} `" ]; then $INSMOD ip_tables fi if [ -z "` $LSMOD | $GREP ip_conntrack | $AWK {'print $1'} `" ]; then $INSMOD ip_conntrack fi if [ -z "` $LSMOD | $GREP ip_conntrack_ftp | $AWK {'print $1'} `" ]; = then $INSMOD ip_conntrack_ftp fi if [ -z "` $LSMOD | $GREP ip_conntrack_irc | $AWK {'print $1'} `" ]; = then $INSMOD ip_conntrack_irc fi if [ -z "` $LSMOD | $GREP iptable_nat | $AWK {'print $1'} `" ]; then $INSMOD iptable_nat fi if [ -z "` $LSMOD | $GREP ip_nat_ftp | $AWK {'print $1'} `" ]; then $INSMOD ip_nat_ftp fi # You would probably find it faster, and much simpler to read, if you # just use "INSMOD=3D/sbin/modprobe" and then $INSMOD each # module without the if-fi construct. modprobe checks if the module # is loaded, and if not will load it with insmod, and does it silently. echo "1" > /proc/sys/net/ipv4/ip_forward # barring catastrophe it is not really an issue, but you shouldn't = enable # forwarding until AFTER your rules are in place, or at least until = after # you have set the DROP policy for FORWARD. $IPTABLES -P INPUT DROP $IPTABLES -F INPUT $IPTABLES -P OUTPUT DROP $IPTABLES -F OUTPUT $IPTABLES -P FORWARD DROP $IPTABLES -F FORWARD $IPTABLES -F -t nat if [ -n "`$IPTABLES -L | $GREP drop-and-log-it`" ]; then $IPTABLES -F drop-and-log-it fi # again, you could dispense with the if-fi construct here. The worst # that will happen is that you see "no chain/target/match by that name"=20 # either on the console or in the logfile. Having it the way it is is a = # more 'proper' approach if the script is re-entrant, but in such a case = # the "-N drop-and-log-it" below will give you a warning instead if the=20 # "-F drop-and-log-it"... :^) $IPTABLES -X $IPTABLES -Z $IPTABLES -N drop-and-log-it $IPTABLES -A drop-and-log-it -j LOG --log-level info $IPTABLES -A drop-and-log-it -j DROP $IPTABLES -A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT $IPTABLES -A INPUT -i $INTIF -s $INTNET -d $UNIVERSE -j ACCEPT ### OK, anything in from the LAN is accepted, regardless of what it is. # Kind of loose, but if you are in control of all the machines it's safe = # enough. $IPTABLES -A INPUT -i $EXTIF -s $INTNET -d $UNIVERSE -j drop-and-log-it # This is unlikely ever to match, because in the unlikely event that you = # DO get a packet from the internet that is addressed back to the=20 # internet, it will end up in the FORWARD chain, since it isn't bound = for=20 # this box's IP... $IPTABLES -A INPUT -i $EXTIF -p ICMP -s $UNIVERSE -d $EXTIP -j ACCEPT $IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m state --state \ ESTABLISHED,RELATED -j ACCEPT ### This rule, together with the ip_conntrack_ftp module you loaded=20 ### above, will allow both passive and active FTP connections through,=20 ### once they are allowed to initiate, IE with the port 20 connection. $IPTABLES -A INPUT -i $INTIF -p tcp --sport 68 --dport 67 -j ACCEPT $IPTABLES -A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j ACCEPT $IPTABLES -A INPUT -i $EXTIF -p tcp --sport 21 -m state --state NEW,ESTABLISHED -j ACCEPT ### Ahhh. You accept "new" connections with SOURCEport 21. ### However, incoming FTP connections will have DESTport 21. $IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT # This is redundant, since you allow ANY connection out from the local # box below. However, if you ever tighten up the rules to only ACCEPT # specific traffic, simply match "--dport 21" and skip state match. # 1) Active ftp. $IPTABLES -A INPUT -i $EXTIF -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT $IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 20 -m state --state NEW,ESTABLISHED -j ACCEPT # 2) Passive ftp. $IPTABLES -A INPUT -i $EXTIF -p tcp --sport 1024:65535 --dport=20 1024:65535 \ -m state --state ESTABLISHED -j ACCEPT $IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport 1024:65535 --dport=20 1024:65535 \ -m state --state ESTABLISHED,RELATED -j ACCEPT ### These (and the two for active, above) are also redundant. You allow ### anything at all outbound anyway, and already have ESTABLISHED ### and RELATED state accepted for inbound. $IPTABLES -A INPUT -s $UNIVERSE -d $UNIVERSE -j drop-and-log-it $IPTABLES -A OUTPUT -o lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT $IPTABLES -A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j ACCEPT $IPTABLES -A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j ACCEPT $IPTABLES -A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j drop-and-log-it $IPTABLES -A OUTPUT -o $EXTIF -s $EXTIP -d $UNIVERSE -j ACCEPT $IPTABLES -A OUTPUT -o $INTIF -p tcp -s $INTIP --sport 67 \ -d 255.255.255.255 --dport 68 -j ACCEPT $IPTABLES -A OUTPUT -o $INTIF -p udp -s $INTIP --sport 67 \ -d 255.255.255.255 --dport 68 -j ACCEPT $IPTABLES -A OUTPUT -s $UNIVERSE -d $UNIVERSE -j drop-and-log-it # This rule should be 'safe', since you have the "-s $EXTIP" accept = above # However to be precise you'd be better off with: # $IPTABLES -A OUTPUT -s ! $EXTIP -d $UNIVERSE -j drop-and-log-it # which says "any source IP that is NOT $EXTIP" $IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED \ -j ACCEPT $IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT $IPTABLES -A FORWARD -j drop-and-log-it $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j SNAT --to $EXTIP # just as an aside, if you're not aware, you need to use # "iptables -t nat -L" to list this rule. the iptables command only=20 # deals with one table at a time, and the default is "-t filter". ------=_NextPart_000_00B2_01C2CE09.E7465950 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Thanks to Joel Newkirk, here is the = longwinded=20 response he sent me:
 
 

Heavily snipped and re-commented=20 script:


IPTABLES=3D/sbin/iptables

LSMOD=3D/sbin/lsmodDEPMOD=3D/sbin/depmod
INSMOD=3D/sbin/insmod
GREP=3D/bin/grep
A= WK=3D/bin/awk
SED=3D/bin/sed
IFCONFIG=3D/sbin/ifconfig

EXTIF= =3D"eth0"
INTIF=3D"eth1"

EXTIP=3D"xxx.xxx.xxx.xxx"

INTNE= T=3D"192.168.1.0/24"
INTIP=3D"192.168.1.1/24"

#=20 This is redundant, since they are both the same number in actual = usage.
# It=20 is likely that the intention for INTIP is to hold the IP of $INTIF
# = interface on this machine, which should not have "/24" at the=20 end.

UNIVERSE=3D"0.0.0.0/0"

# for what it is worth, this = could be=20 "0/0" and would work, but if you
# leave it out of rules entirely = then this=20 is the default anyway.

$DEPMOD -a

if [ -z "` $LSMOD | = $GREP=20 ip_tables | $AWK {'print $1'} `" ]; then
   $INSMOD=20 ip_tables
fi

if [ -z "` $LSMOD | $GREP ip_conntrack | $AWK = {'print=20 $1'} `" ]; then
   $INSMOD ip_conntrack
fi

if [ = -z "`=20 $LSMOD | $GREP ip_conntrack_ftp | $AWK {'print $1'} `" ]; = then
  =20 $INSMOD ip_conntrack_ftp
fi

if [ -z "` $LSMOD | $GREP = ip_conntrack_irc=20 | $AWK {'print $1'} `" ]; then
   $INSMOD=20 ip_conntrack_irc
fi

if [ -z "` $LSMOD | $GREP iptable_nat | = $AWK=20 {'print $1'} `" ]; then
   $INSMOD = iptable_nat
fi

if [ -z=20 "` $LSMOD | $GREP ip_nat_ftp | $AWK {'print $1'} `" ]; = then
  =20 $INSMOD ip_nat_ftp
fi

# You would probably find it faster, and = much=20 simpler to read, if you
# just use "INSMOD=3D/sbin/modprobe" and then = $INSMOD=20 each
# module without the if-fi construct.  modprobe checks if = the=20 module
# is loaded, and if not will load it with insmod, and does it=20 silently.

echo "1" > /proc/sys/net/ipv4/ip_forward

# = barring=20 catastrophe it is not really an issue, but you shouldn't enable
# = forwarding=20 until AFTER your rules are in place, or at least until after
# you = have set=20 the DROP policy for FORWARD.

$IPTABLES -P INPUT DROP
$IPTABLES = -F=20 INPUT
$IPTABLES -P OUTPUT DROP
$IPTABLES -F OUTPUT
$IPTABLES -P = FORWARD=20 DROP
$IPTABLES -F FORWARD
$IPTABLES -F -t nat

if [ -n = "`$IPTABLES=20 -L | $GREP drop-and-log-it`" ]; then
   $IPTABLES -F=20 drop-and-log-it
fi

# again, you could dispense with the if-fi=20 construct here.  The worst
# that will happen is that you see = "no=20 chain/target/match by that name"
# either on the console or in the=20 logfile.  Having it the way it is is a
# more 'proper' approach = if the=20 script is re-entrant, but in such a case
# the "-N drop-and-log-it" = below=20 will give you a warning instead if the
# "-F drop-and-log-it"...=20 :^)

$IPTABLES -X

$IPTABLES -Z

$IPTABLES -N=20 drop-and-log-it
$IPTABLES -A drop-and-log-it -j LOG --log-level=20 info
$IPTABLES -A drop-and-log-it -j DROP

$IPTABLES -A INPUT = -i lo -s=20 $UNIVERSE -d $UNIVERSE -j ACCEPT

$IPTABLES -A INPUT -i $INTIF -s = $INTNET=20 -d $UNIVERSE -j ACCEPT

### OK, anything in from the LAN is = accepted,=20 regardless of what it is.
# Kind of loose, but if you are in control = of all=20 the machines it's safe
# enough.

$IPTABLES -A INPUT -i $EXTIF = -s=20 $INTNET -d $UNIVERSE -j drop-and-log-it

# This is unlikely ever = to match,=20 because in the unlikely event that you
# DO get a packet from the = internet=20 that is addressed back to the
# internet, it will end up in the = FORWARD=20 chain, since it isn't bound for
# this box's IP...

$IPTABLES = -A INPUT=20 -i $EXTIF -p ICMP -s $UNIVERSE -d $EXTIP -j ACCEPT

$IPTABLES -A = INPUT -i=20 $EXTIF -s $UNIVERSE -d $EXTIP -m state --state = \
 ESTABLISHED,RELATED -j=20 ACCEPT

### This rule, together with the ip_conntrack_ftp module = you=20 loaded
### above, will allow both passive and active FTP connections = through,
### once they are allowed to initiate, IE with the port 20=20 connection.

$IPTABLES -A INPUT -i $INTIF -p tcp --sport 68 = --dport 67 -j=20 ACCEPT
$IPTABLES -A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j=20 ACCEPT

$IPTABLES -A INPUT  -i $EXTIF -p tcp --sport 21 -m = state=20 --state
NEW,ESTABLISHED -j ACCEPT

### Ahhh.  You accept = "new"=20 connections with SOURCEport 21.
### However, incoming FTP connections = will=20 have DESTport 21.

$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 21 = -m=20 state --state
NEW,ESTABLISHED -j ACCEPT

# This is redundant, = since you=20 allow ANY connection out from the local
#  box below.  = However, if=20 you ever tighten up the rules to only ACCEPT
# specific traffic, = simply match=20 "--dport 21" and skip state match.

# 1) Active ftp.
$IPTABLES = -A=20 INPUT  -i $EXTIF -p tcp --sport 20 -m state = --state
ESTABLISHED,RELATED=20 -j ACCEPT
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 20 -m state=20 --state
NEW,ESTABLISHED -j ACCEPT

# 2) Passive = ftp.
$IPTABLES -A=20 INPUT  -i $EXTIF -p tcp --sport 1024:65535 --dport
1024:65535=20 \
  -m state --state ESTABLISHED -j ACCEPT
$IPTABLES -A = OUTPUT -o=20 $EXTIF -p tcp --sport 1024:65535 --dport
1024:65535 \
  -m = state=20 --state ESTABLISHED,RELATED -j ACCEPT

### These (and the two for = active,=20 above) are also redundant.  You allow
### anything at all = outbound=20 anyway, and already have ESTABLISHED
### and RELATED state accepted = for=20 inbound.

$IPTABLES -A INPUT -s $UNIVERSE -d $UNIVERSE -j=20 drop-and-log-it


$IPTABLES -A OUTPUT -o lo -s $UNIVERSE -d = $UNIVERSE=20 -j ACCEPT

$IPTABLES -A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j=20 ACCEPT

$IPTABLES -A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j=20 ACCEPT

$IPTABLES -A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j=20 drop-and-log-it

$IPTABLES -A OUTPUT -o $EXTIF -s $EXTIP -d = $UNIVERSE -j=20 ACCEPT

$IPTABLES -A OUTPUT -o $INTIF -p tcp -s $INTIP --sport 67=20 \
 -d 255.255.255.255 --dport 68 -j ACCEPT
$IPTABLES -A = OUTPUT -o=20 $INTIF -p udp -s $INTIP --sport 67 \
 -d 255.255.255.255 --dport = 68 -j=20 ACCEPT

$IPTABLES -A OUTPUT -s $UNIVERSE -d $UNIVERSE -j=20 drop-and-log-it

# This rule should be 'safe', since you have the = "-s=20 $EXTIP" accept above
# However to be precise you'd be better off = with:
#=20 $IPTABLES -A OUTPUT -s ! $EXTIP -d $UNIVERSE -j drop-and-log-it
# = which says=20 "any source IP that is NOT $EXTIP"

$IPTABLES -A FORWARD -i $EXTIF = -o=20 $INTIF -m state --state
ESTABLISHED,RELATED \
 -j = ACCEPT
$IPTABLES=20 -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

$IPTABLES -A FORWARD -j=20 drop-and-log-it

$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j SNAT = --to=20 $EXTIP

# just as an aside, if you're not aware, you need to = use
#=20 "iptables -t nat -L" to list this rule.  the iptables command only =
#=20 deals with one table at a time, and the default is "-t=20 filter".
------=_NextPart_000_00B2_01C2CE09.E7465950--