No Internet Connection
Ok. So that's my super mega power iptables script I'm testing for my internet server.
I already put the DHCP rules (my ISP doesn't have a fixed IP address for the DHCP server. They are always changing it... don't ask me why) but I didn't test it yet.
I developed this script using the very good classic
idea: block everything and allow just what I want.
So, this internet server will be a firewall (as soon as this script works), making NAT (with MASQUERADING). I also have the SQUID for proxing and caching.
Some users in my LAN have free pass for everything while the rest of my users just can connect to the mail servers (POP and SMTP) and a few users can connect to ICQ, Messenger, etc (I really don't like this idea, but I have to do it).
I don't use any authentication method based on user and password. Instead, I use rules based on the MAC address for the computers with free pass. (Ok. I know it isn't a very safe aproach).
So, here goes my script:
INET_IP=ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 2
INET_IFACE="eth0"
LAN_IP="192.168.0.41"
LAN_IP_RANGE="192.168.0.0/24"
LAN_IFACE="eth1"
LO_IFACE="lo"
LO_IP="127.0.0.1"
Mac1="00:e0:18:3b:af:78"
Mac2="00:50:04:9c:42:23"
Mac3="00:0e:a6:bd:e7:7f"
Mac4="00:c0:df:a5:0c:a5"
IPTABLES="/usr/sbin/iptables"
/sbin/depmod -a/sbin/modprobe ip_tables/sbin/modprobe ip_conntrack/sbin/modprobe iptable_filter/sbin/modprobe iptable_nat/sbin/modprobe ipt_LOG/sbin/modprobe ipt_limit/sbin/modprobe ipt_state/sbin/modprobe ipt_REJECT/sbin/modprobe ip_conntrack_ftp/sbin/modprobe ip_nat_ftp
echo "1" > /proc/sys/net/ipv4/ip_forward
$IPTABLES -F
$IPTABLES -X
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -N tcp_invalidos
$IPTABLES -A tcp_invalidos -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset $IPTABLES -A tcp_invalidos -p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "Novo nao SYN:"
$IPTABLES -A tcp_invalidos -p tcp ! --syn -m state --state NEW -j DROP
$IPTABLES -A OUTPUT -p tcp -j tcp_invalidos
$IPTABLES -A OUTPUT -p udp --sport 68 --dport 67 -j ACCEPT
$IPTABLES -A OUTPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 53 -j ACCEPT $IPTABLES -A OUTPUT -p udp --dport 53 -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 80 -s $INET_IP -o $INET_IFACE -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 443 -s $INET_IP -o $INET_IFACE -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 21 -s $INET_IP -o $INET_IFACE -j ACCEPT
# ICQ:
$IPTABLES -A OUTPUT -p tcp -d 64.12.163.197 -o $INET_IFACE -j ACCEPT
$IPTABLES -A INPUT -p tcp -j tcp_invalidos
$IPTABLES -A INPUT -p udp --sport 67 --dport 68 -j ACCEPT
$IPTABLES -A INPUT -p ALL -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 53 -j ACCEPT $IPTABLES -A INPUT -p udp --dport 53 -j ACCEPT
# SQUID:
$IPTABLES -A INPUT -i $LAN_IFACE -p tcp --dport 80 -j ACCEPT
$IPTABLES -A FORWARD -p tcp -j tcp_invalidos
$IPTABLES -A FORWARD -m state --state
ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac1 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac2 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac3 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac4 -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 53 -j ACCEPT $IPTABLES -A FORWARD -p udp --dport 53 -j ACCEPT
$IPTABLES -A FORWARD -d <my pop server> -p tcp --dport 110 \
-i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -d <my smtp server> -p tcp --dport 25 \
-i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 21 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 80 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 443 -i $LAN_IFACE -j ACCEPT
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
That's it. Another question: When I configure this script to run automatically after rebooting the server, I receive this error message (3 times): "Bad argument eth0" so the script doesn't work, neither my Internet access from my LAN and I can't find where is the error.
However, after rebooting the server and loggin in as root, I can run the script from command line. It works and my LAN can access the Internet during that short time (about 20 minutes. However, I didn't test the DHCP rules to ckeck if it continues to happen).
Regards
Giancarlo
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Quoted 4 lines · Giancarlo Boaron
So, here goes my script:
INET_IP=
ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 2
maybe this is an ifconfig quirk, but that does not produce an IP address on my machine here (FC1). it actually produces nothing. the working version here is:
ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1
however, might i suggest:
ip -4 -o addr sh eth0 | awk 'NR==1 {print $4}' | cut -d/ -f1
Quoted 10 lines
$IPTABLES -N
tcp_invalidos$IPTABLES -A
tcp_invalidos-p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset $IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "Novo nao SYN:"
$IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j DROP
there's a state called INVALID ("-m state --state INVALID") that could probably do most of the work for you here...
Quoted 20 lines
$IPTABLES -A OUTPUT -p tcp -j
tcp_invalidos$IPTABLES -A OUTPUT -p udp --sport 68 --dport 67 -j ACCEPT
$IPTABLES -A OUTPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 53 -j ACCEPT $IPTABLES -A OUTPUT -p udp --dport 53 -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 80 -s $INET_IP -o $INET_IFACE -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 443 -s $INET_IP -o $INET_IFACE -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --dport 21 -s $INET_IP -o $INET_IFACE -j ACCEPT
Quoted 3 lines
# ICQ:
$IPTABLES -A OUTPUT -p tcp -d 64.12.163.197 -o $INET_IFACE -j ACCEPT
are you running an ICQ client on your firewall? the above rule implies that you are. i can't say i recommend this at all. if your intention is to allow ICQ *through* the firewall--this rule belongs in FORWARD.
Quoted 53 lines
$IPTABLES -A INPUT -p tcp -j
tcp_invalidos$IPTABLES -A INPUT -p udp --sport 67 --dport 68 -j ACCEPT
$IPTABLES -A INPUT -p ALL -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 53 -j ACCEPT $IPTABLES -A INPUT -p udp --dport 53 -j ACCEPT
# SQUID:
$IPTABLES -A INPUT -i $LAN_IFACE -p tcp --dport 80 -j ACCEPT$IPTABLES -A FORWARD -p tcp -j
tcp_invalidos$IPTABLES -A FORWARD -m state --state
ESTABLISHED,RELATED -j ACCEPT$IPTABLES -A FORWARD -m mac --mac-source $Mac1 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac2 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac3 -j ACCEPT
$IPTABLES -A FORWARD -m mac --mac-source $Mac4 -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 53 -j ACCEPT $IPTABLES -A FORWARD -p udp --dport 53 -j ACCEPT
$IPTABLES -A FORWARD -d <my pop server> -p tcp --dport 110 \
-i $LAN_IFACE -j ACCEPT$IPTABLES -A FORWARD -d <my smtp server> -p tcp --dport 25 \
-i $LAN_IFACE -j ACCEPT$IPTABLES -A FORWARD -p tcp --dport 21 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 80 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 443 -i $LAN_IFACE -j ACCEPT$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
Quoted 6 lines
That's it. Another question: When I configure this script to run automatically after rebooting the server, I receive this error message (3 times): "Bad argument eth0" so the script doesn't work, neither my Internet access from my LAN and I can't find where is the error.
it's probably related to the fact that your $INET_IP variable expands to a text string, not an IP address.
Quoted 5 lines
However, after rebooting the server and loggin in as root, I can run the script from command line. It works and my LAN can access the Internet during that short time (about 20 minutes. However, I didn't test the DHCP rules to ckeck if it continues to happen).
are you running your firewall script before your external interface has an IP address? if so--this would explain it. your DHCP client is most likely being spawned by your /etc/init.d/network script, which usually kicks off pretty early in rc2.
i cannot explain the "20 minute" phenomenon you refer to.
-j
Signature · 1 line
Jason Opperisano <[email protected]>
Quoted 2 lines · Giancarlo Boaron
INET_IP=
ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 2
This might not work. For example, on my home Linux box (FC2) this gives the first 2 bytes of link local IPv6 address. Also, with this approach, you must:
a) Make sure this scripts runs *after* dhcpclient configures eth0.
b) Rerun it every time your IP address changes. There are ISPs out there that will let you have same IP address for years, but there are some that will force the change of address every day, and there are some really bad ones that will force the change every hour.
Anyhow, you don't really need to know this address. You can use something like this on external interface to make sure no spoofing can take place (these are private/reserved ranges that should never appear on Internet):
-A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
-A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
-A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
-A INPUT -i eth0 -s 169.254.0.0/16 -j DROP
-A INPUT -i eth0 -s 192.0.2.0/24 -j DROP
-A INPUT -i eth0 -s 204.152.64.0/23 -j DROP
-A INPUT -i eth0 -s 224.0.0.0/3 -j DROP
-A INPUT -i eth0 -s 127.0.0.0/8 -j DROP
-A INPUT -i eth0 -d 192.168.0.0/16 -j DROP
-A INPUT -i eth0 -d 10.0.0.0/8 -j DROP
-A INPUT -i eth0 -d 172.16.0.0/12 -j DROP
-A INPUT -i eth0 -d 169.254.0.0/16 -j DROP
-A INPUT -i eth0 -d 192.0.2.0/24 -j DROP
-A INPUT -i eth0 -d 204.152.64.0/23 -j DROP
-A INPUT -i eth0 -d 224.0.0.0/3 -j DROP
-A INPUT -i eth0 -d 127.0.0.0/8 -j DROP
Repeat this for FORWARD chain.
Now if the packet ends up in INPUT (or FORWARD) chains, and is not dropped by these rules, it means it is a valid packet.
Quoted 11 lines
/sbin/depmod-a/sbin/modprobeip_tables/sbin/modprobeip_conntrack/sbin/modprobeiptable_filter/sbin/modprobeiptable_nat/sbin/modprobeipt_LOG/sbin/modprobeipt_limit/sbin/modprobeipt_state/sbin/modprobeipt_REJECT/sbin/modprobeip_conntrack_ftp/sbin/modprobeip_nat_ftp
Most of these will get automatically loaded. Leave ip_nat_ftp line, and delete all the rest.
Quoted 10 lines
$IPTABLES -N
tcp_invalidos$IPTABLES -A
tcp_invalidos-p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset $IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "Novo nao SYN:"
$IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j DROP
Here's the place where you made an error. When you reach end of "tcp_invalidos" chain, default policy for INPUT/OUTPUT/FORWARD chains will be applied (which is DROP). Which means, all packets will be dropped by the firewall.
You need to place this line at the end of "tcp_invalidos":
-A tcp_invalidos -j RETURN
Anyhow, better way of doing this is using (for example):
-A INPUT -i eth1 -p tcp --dport some_port --tcp-flags SYN,ACK,FIN,RST SYN -m state --state NEW -j ACCEPT
This ensures that the first packet is really SYN packet with no other funny flags set. This way you don't need tcp_invalidos.
Quoted 2 lines
$IPTABLES -A OUTPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
I'd place this one as the very first rule (for all three chains). Vast majority of packets is going to match it, and it doesn't make sense in forcing 99% of packets to go through any other rules.
I haven't looked at the rest of you rules...
Quoted 6 lines
That's it. Another question: When I configure this script to run automatically after rebooting the server, I receive this error message (3 times): "Bad argument eth0" so the script doesn't work, neither my Internet access from my LAN and I can't find where is the error.
Because the ifconfig, grep, cut thingie hasn't returned IP address of eth0. I guess in your case it returned eth0.
Quoted 5 lines
However, after rebooting the server and loggin in as root, I can run the script from command line. It works and my LAN can access the Internet during that short time (about 20 minutes. However, I didn't test the DHCP rules to ckeck if it continues to happen).
Strange. It shouldn't work at all...
Signature · 3 lines
Aleksandar Milivojevic <[email protected]> Pollard Banknote Limited Systems Administrator 1499 Buffalo Place Tel: (204) 474-2323 ext 276 Winnipeg, MB R3T 1L7
Quoted 11 lines · Aleksandar Milivojevic
Quoted 10 lines
$IPTABLES -N
tcp_invalidos$IPTABLES -A
tcp_invalidos-p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset $IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "Novo nao SYN:"
$IPTABLES -Atcp_invalidos-p tcp ! --syn -m state --state NEW -j DROP
Quoted 4 lines
Here's the place where you made an error. When you reach end of "
tcp_invalidos" chain, default policy for INPUT/OUTPUT/FORWARD chains will be applied (which is DROP). Which means, all packets will be dropped by the firewall.
i don't know that i agree with this statement. when a packet reaches the end of custom chain "tcp_invalidos" and has not matched any rules in that chain--it should return to the calling chain where it left off; i.e.
iptables -P INPUT DROP
iptables -A INPUT -j badstuff
iptables -A INPUT -j goodstuff
iptables -A INPUT -j LOG
a packet not matching any rule in "badstuff" will return to INPUT, and then jump to "goodstuff"... if the packet doesn't match any rule in "goodstuff" it will return to INPUT, get logged, and *then* get dropped, as it has hit the end of the INPUT chain and not matched any rules. only then does the POLICY of the chain get enforced.
-j
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
We don't smoke and we don't chew, and we don't go with girls that do. -- Walter Summers
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Quoted 10 lines · Jason Opperisano
i don't know that i agree with this statement. when a packet reaches the end of custom chain "
tcp_invalidos" and has not matched any rules in that chain--it should return to the calling chain where it left off; i.e.iptables -P INPUT DROP
iptables -A INPUT -j badstuff
iptables -A INPUT -j goodstuff
iptables -A INPUT -j LOG
Quoted 4 lines
a packet not matching any rule in "badstuff" will return to INPUT, and
then jump to "goodstuff"... if the packet doesn't match any rule in
"goodstuff" it will return to INPUT, get logged, and *then* get dropped,
as it has hit the end of the INPUT chain and not matched any rules. only then does the POLICY of the chain get enforced.
Hm, interesting... I vaugly remember (I might be wrong) testing something similar a while ago, and I got different results. Anyhow, this seems to be undocumented ("man iptables" doesn't say what happens when end of user defined chain is reached). Could it be that it changed from one version to another?
Signature · 3 lines
Aleksandar Milivojevic <[email protected]> Pollard Banknote Limited Systems Administrator 1499 Buffalo Place Tel: (204) 474-2323 ext 276 Winnipeg, MB R3T 1L7
Ok Jason. I agree with you. When any packet matches a specific rule, it returns to the top level rule that called a down level rule (in this situation, that's the INPUT chain). And if we have no matches in the INPUT chain, iptables uses the default policy (DROP).
I tested that DHCP rules and it seems that it's working. I'm running my script for about 2 hours and it didn't crash yet.
So, I think that strange behavior was resolved.
Im going to test that nice ideas when rebooting the server and another ones suggested in this mail list.
Thank you.
Giancarlo
--- Jason Opperisano <[email protected]> escreveu:
Quoted 48 lines
Quoted 3 lines · Aleksandar Milivojevic
Quoted 3 lines
$IPTABLES -N
tcp_invalidos$IPTABLES -A
tcp_invalidos-p tcp --tcp-flagsSYN,ACK
Quoted 2 lines
Quoted 2 lines
SYN,ACK \
-m state --state NEW -j REJECT --reject-withtcp-reset
Quoted 1 line
Quoted 1 line
$IPTABLES -A
tcp_invalidos-p tcp ! --syn -mstate
Quoted 3 lines
Quoted 3 lines
--state NEW -j LOG \
--log-prefix "Novo nao SYN:"
$IPTABLES -Atcp_invalidos-p tcp ! --syn -mstate
Quoted 3 lines
Quoted 1 line
--state NEW -j DROP
Here's the place where you made an error. When
you reach end of
Quoted 1 line
"
tcp_invalidos" chain, default policy forINPUT/OUTPUT/FORWARD chains
Quoted 1 line
will be applied (which is DROP). Which means, all
packets will be
Quoted 1 line
dropped by the firewall.
i don't know that i agree with this statement. when a packet reaches
the end of custom chain "tcp_invalidos" and has not matched any rules in
that chain--it should return to the calling chain where it left off;
i.e.iptables -P INPUT DROP
iptables -A INPUT -j badstuff
iptables -A INPUT -j goodstuff
iptables -A INPUT -j LOGa packet not matching any rule in "badstuff" will return to INPUT, and
then jump to "goodstuff"... if the packet doesn't match any rule in
"goodstuff" it will return to INPUT, get logged, and *then* get dropped,
as it has hit the end of the INPUT chain and not matched any rules.
only then does the POLICY of the chain get enforced.
Quoted 3 lines
-j
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Quoted 4 lines
We don't smoke and we don't chew, and we don't go with girls that do. --
Walter Summers
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Quoted 3 lines
_______________________________________________________
Yahoo! Messenger 6.0 - jogos, emoticons sonoros e muita diversão. Instale agora!
http://br.download.yahoo.com/messenger/