1: iptables -A INPUT -p tcp -syn -j DROP
This is a desktop-centric rule that will do two things: First it will allow you to actually work normally on your desktop. All network traffic going out of your machine will be allowed out, but all TCP/IP traffic coming into your machine will simply be dropped. This makes for a solid Linux desktop that does not need any incoming traffic. What if you want to allow specific networking traffic in — for example, ssh for remote management? To do this, you’ll need to add an iptables rule for the service and make sure that service rule is run before rule to drop all incoming traffic.
2: iptables -A INPUT -p tcp –syn –destination-port 22 -j ACCEPT
Let’s build on our first command. To allow traffic to reach port 22 (secure shell), you will add this line. Understand that this line will allow any incoming traffic into port 22. This is not the most secure setup alone. To make it more secure, you’ll want to limit which machines can actually connect to port 22 on the machine. Fortunately, you can do this with iptables as well. If you know the IP address of the source machine, you can add the -s SOURCE_ADDRESS option (Where SOURCE_ADDRESS is the actual address of the source machine) before the –destination-port portion of the line.
3: /sbin/iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
This will allow all previously initiated and accepted exchanges to bypass rule checking. The ESTABLISHED and RELATED arguments belong to the –state switch. The ESTABLISHED argument says, “Any packet that belongs to an existing connection,” and the RELATED argument says, “Any packet that does not belong to an already existing connection but is related to an existing connection.” The “state machine” of iptables is a means for iptables to track connections with the help of the kernel level “conntrack” module. By tracking connections, iptables knows what connections can be allowed and what can’t. This reduces the amount of work the administrator has to do.
Here’s how state works. If the local user initiates a connection, that packet (to that connection) is set as NEW in the prerouting chain. When the local user gets a return packet, the state is changed to ESTABLISHED in the prerouting chain. So when a state is set as ESTABLISHED, it can be allowed with the right iptables rule.
4: iptables -N LOGDROP
With this handy chain, iptables will log all dropped packets. Of course, this is only part of the chain. To complete it, you need to add the follow two rules: iptables -A logdrop -J LOG and iptables -A logdrop -J DROP. Now all matching packets (in this case, anything that has been dropped) will be added to the logdrop chain which will log them and then drop them.
5: iptables -t nat -A PREROUTING -i WLAN_INTERFACE -p tcp –dportPORTNUMBERS -j DNAT –to-destination DESTINATION_IP
When you need to route packets from external sources to specific ports on specific internal machines, this is what you want to do. This rule takes advantage of network address translation to route packets properly. To suit your needs, the WLAN_INTERFACE must be changed to the WLAN interface that bridges the external network to the internal network, the PORTNUMBERS must be changed, and DESTINATION_IP must be changed to match the IP address of the destination machine.
6: iptables -A INPUT -p tcp –syn –dport 25 -j ACCEPT
This is the beginning of a SYN flood protection rule. This portion of the rule blocks DoS attacks on a mail server port. (You can change this to suit your mail server needs.) There are three more portions of this rule set. The first is to add the same rule but modify the port to whatever is being served up by whatever ports you have open. The next portion is iptables -A INPUT -p tcp –syn -m limit –limit 1/s –limit-burst 4 -j ACCEPT, which is the actual SYN flood protection. Finally, iptables -A INPUT -p tcp –syn -j DROP will drop all SYN flood packets.
7: iptables -A INPUT -p tcp -m tcp -s MALICIOUS_ADDRESS -j DROP
This is where you can take care of malicious source IP addresses. For this to work properly, you must make sure you know the offending source IP address and that, in fact, it’s one you want to block. The biggest problem with this occurs when the offending address has been spoofed. If that’s the case, you can wind up blocking legitimate traffic from reaching your network. Do your research on this address.
8: iptables -N port-scan
This is the beginning of a rule to block furtive port scanning. A furtive port scan is a scan that detects closed ports to deduce open ports. Two more lines are needed to complete this rule:
iptables -A port-scan -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j RETURN
iptables -A port-scan -j DROP
Notice that the above rule set is adding a new chain called “port-scan”. You don’t have to name it such; it’s just easier to keep things organized. You can also add timeouts to the above rule set like so:
iptables -A specific-rule-set -p tcp --syn -j syn-flood
iptables -A specific-rule-set -p tcp --tcp-flags SYN,ACK,FIN,RST RST -j port-scan
9: iptables -A INPUT -i eth0 -p tcp -m state –state NEW -m multiport –dports ssh,smtp,http,https -j ACCEPT
What you see here is a chain making use of the multiport argument, which will allow you to set up multiple ports. Using the multiport argument lets you write one chain instead of multiple chains. This single rule saves you from writing out four separate rules, one each for ssh, smtp, http, and https. Naturally, you can apply this to ACCEPT, DENY, REJECT.
10: iptables -A PREROUTING -i eth0 -p tcp –dport 80 -m state –state NEW -m nth –counter 0 –every 4 –packet 0 -j DNAT –to-destination 192.168.1.10:80
If you’re looking to load balance between multiple mirrored servers (in the example case, load balancing a Web server at 192.168.1.10), this rule is what you want. At the heart of this rule is the nth extension, which tells iptables to act on every “nth” packet. In the example, iptables uses counter 0 and acts upon every 4th packet. You can extend this to balance out your mirrored sites this way. Say you have four mirrored servers up and you want to balance the load between them. You could have one line for each server like so:
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 0 -j DNAT --to-destination 192.168.1.10:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 1 -j DNAT --to-destination 192.168.1.20:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 2 -j DNAT --to-destination 192.168.1.30:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 3 -j DNAT --to-destination 192.168.1.40:80
As you can see the server on .10 will be routed every 0 packet, the server on .20 will be routed every 1st packet, the server on .30 will be routed every 2nd packet, and the server on .40 will be routed every 3rd packet.
================================================
iptables--静态防火墙实例教程 follow me
介绍:
这篇文章是本人原创,向读者展示了如何一步一步建立静态防火墙来保护您的计算机,同时在每一步中,我力图向读者讲述清楚原理。在这篇教程之后,你将能理解到防火墙内在过滤机制,同时也能自己动手创建符合自己要求的防火墙。
版权所有,转载请注明来自www.linuxsir.org 并写明作者
1、iptables介绍
iptables是复杂的,它集成到linux内核中。用户通过iptables,可以对进出你的计算机的数据包进行过滤。通过iptables命令设置 你的规则,来把守你的计算机网络──哪些数据允许通过,哪些不能通过,哪些通过的数据进行记录(log)。接下来,我将告诉你如何设置自己的规则,从现在 就开始吧。
2、初始化工作
在shell提示符 # 下打入
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
以上每一个命令都有它确切的含义。一般设置你的iptables之前,首先要清除所有以前设置的规则,我们就把它叫做初始化好了。虽然很多情况下它什么也不做,但是保险起见,不妨小心一点吧! 如果你用的是redhat 或fedora,那么你有更简单的办法
service iptables stop
3、开始设置规则:
接下下开始设置你的规则了
iptables -P INPUT DROP
这一条命令将会为你构建一个非常“安全”的防火墙,我很难想象有哪个hacker能攻破这样的机器,因为它将所有从网络进入你机器的数据丢弃(drop) 了。这当然是安全过头了,此时你的机器将相当于没有网络。如果你ping localhost,你就会发现屏幕一直停在那里,因为ping收不到任何回应。
4 、添加规则
接着上文继续输入命令:
iptables -A INPUT -i ! ppp0 -j ACCEPT
这条规则的意思是:接受所有的,来源不是网络接口ppp0的数据。
我们假设你有两个网络接口,eth0连接局域网,loop是回环网(localhost)。ppp0是一般的adsl上网的internet网络接口,如果你不是这种上网方式,那则有可能是eth1。在此我假设你是adsl上网,你的internet接口是ppp0
此时你即允许了局域网的访问,你也可以访问localhost
此时再输入命令 ping localhost,结果还会和刚才一样吗?
到此我们还不能访问www,也不能mail,接着看吧。
5、我想访问www
iptables -A INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT
允许来自网络接口ppp0(internet接口),并且来源端口是80的数据进入你的计算机。
80端口正是www服务所使用的端口。
好了,现在可以看网页了。但是,你能看到吗?
如果你在浏览器的地址中输入www.baidu.com,能看到网页吗?
你得到的结果一定是:找不到主机www.baidu.com
但是,如果你再输入220.181.27.5,你仍然能够访问baidu的网页。
为什么?如果你了解dns的话就一定知道原因了。
因为如果你打入www.baidu.com,你的电脑无法取得www.baidu.com这个名称所能应的ip地址220.181.27.5。如果你确实 记得这个ip,那么你仍然能够访问www,你当然可以只用ip来访问www,如果你想挑战你的记忆的话^ _ ^,当然,我们要打开DNS。
6、打开dns端口
打开你的dns端口,输入如下命令:
iptables -A INPUT -i ppp0 -p udp -sport 53 -j ACCEPT
这条命令的含义是,接受所有来自网络接口ppp0,upd协议的53端口的数据。53也就是著名的dns端口。
此时测试一下,你能通过主机名称访问www吗?你能通过ip访问www吗?
当然,都可以!
7、查看防火墙
此时可以查看你的防火墙了
iptables -L
如果你只想访问www,那么就可以到此为止,你将只能访问www了。 不过先别急,将上面讲的内容总结一下,写成一个脚本。
#!/bin/bash
# This is a script
# Edit by liwei
# establish static firewall
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -P INPUT DROP
iptables -A INPUT -i ! ppp0 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport 53 -j ACCEPT
8、复杂吗?到此iptables可以按你的要求进行包过滤了。你可以再设定一些端口,允许你的机器访问这些端口。这样有可能,你不能访问QQ,也可能不 能打网络游戏,是好是坏,还是要看你自己而定了。顺便说一下,QQ这个东西还真是不好控制,用户与服务器连接使用的好像是8888端口,而QQ上好友互发 消息使用的又是udp的4444端口(具体是不是4444还不太清楚)。而且QQ还可以使用www的80端口进行登录并发消息,看来学无止境,你真的想把 这个家伙控制住还不容易呢?还是进入我们的正题吧。
如果你的机器是服务器,怎么办?
9、如果不巧你的机器是服务器,并且要提供www服务。显然,以上的脚本就不能符合我们的要求了。但只要你撑握了规则,稍作修改同样也能很好的工作。在最后面加上一句
iptables -A INPUT -i ppp0 -p tcp --dport 80 -j ACCEPT
这一句也就是将自己机器上的80端口对外开放了,这样internet上的其他人就能访问你的www了。当然,你的www服务器得工作才行。如果你的机器 同时是smtp和pop3服务器,同样的再加上两条语句,将--dport后面的80改成25和110就行了。如果你还有一个ftp服务器,呵呵,如果你 要打开100个端口呢……
我们的工作好像是重复性的打入类似的语句,你可能自己也想到了,我可以用一个循环语句来完成,对,此处可以有效的利用shell脚本的功能,也让你体验到了shell脚本语言的威力。看下文:
10、用脚本简化你的工作,阅读下面的脚本
#!/bin/bash
# This is a script
# Edit by liwei
# establish a static firewall
# define const here
Open_ports="80 25 110 10" # 自己机器对外开放的端口
Allow_ports="53 80 20 21" # internet的数据可以进入自己机器的端口
#init
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -P INPUT DROP #we can use another method to instead it
iptables -A INPUT -i ! ppp0 -j ACCEPT
# define ruler so that some data can come in.
for Port in "$Allow_ports" ; do
iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done
for Port in "$Open_ports" ; do
iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT
done
这个脚本有三个部分(最前面的一段是注释,不算在这三部分中)
第一部分是定义一些端口:访问你的机器"Open_ports"端口的数据,允许进入;来源是"Allow_ports"端口的数据,也能够进入。
第二部分是iptables的初始化,第三部分是对定义的端口具体的操作。
如果以后我们的要求发生了一些变化,比如,你给自己的机器加上了一个ftp服务器,那么只要在第一部分"Open_ports"的定义中,将ftp对应的20与21端口加上去就行了。呵呵,到此你也一定体会到了脚本功能的强大的伸缩性,但脚本的能力还远不止这些呢!
11、使你的防火墙更加完善
看上面的脚本init部分的倒数第二句
iptables -P INPUT DROP
这是给防火墙设置默认规则。当进入我们计算机的数据,不匹配我们的任何一个条件时,那么就由默认规则来处理这个数据----drop掉,不给发送方任何应答。
也就是说,如果你从internet另外的一台计算机上ping你的主机的话,ping会一直停在那里,没有回应。
如果黑客用namp工具对你的电脑进行端口扫描,那么它会提示黑客,你的计算机处于防火墙的保护之中。我可不想让黑客对我的计算机了解太多,怎么办,如果我们把drop改成其他的动作,或许能够骗过这位刚出道的黑客呢。
怎么改呢?将刚才的那一句( iptables -P INPUT DROP )去掉,在脚本的最后面加上
iptables -A INPUT -i ppp0 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i ppp0 -p udp -j REJECT --reject-with icmp-port-unreachable
这样就好多了,黑客虽然能扫描出我们所开放的端口,但是他却很难知道,我们的机器处在防火墙的保护之中。如果你只运行了ftp并且仅仅对局域网内部访问, 他很难知道你是否运行了ftp。在此我们给不应该进入我们机器的数据,一个欺骗性的回答,而不是丢弃(drop)后就不再理会。这一个功能,在我们设计有 状态的防火墙中(我这里讲的是静态的防火墙)特别有用。
你可以亲自操作一下,看一看修改前后用namp扫描得到的结果会有什么不同?
12、这个教程我想到此就结束了,其中有很多东西在这里没有提到,如ip伪装,端口转发,对数据包的记录功能。还有一个很重要的东西就 是:iptables处理数据包的流程.在这里我想告诉你,你设置的过滤规则的顺序很重要,在此不宜详细介绍,因为这样一来,这个教程就会拘泥于细节。
iptables是复杂的,我在linuxsir上看过很多教程,它们往往多而全,反而让人望而生畏,希望我的这个教程,能够指导你入门。加油!
最后,我把完整的脚本写出来如下,你只要修改常量定义部分,就能表现出较大的伸缩性^_^
#!/bin/bash
# This is a script
# Edit by liwei
# establish a static firewall
# define const here
Open_ports="80 25 110 10" # 自己机器对外开放的端口
Allow_ports="53 80 20 21" # internet的数据可以进入自己机器的端口
#init
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
# The follow is comment , for make it better
# iptables -P INPUT DROP
iptables -A INPUT -i ! ppp0 -j ACCEPT
# define ruler so that some data can come in.
for Port in "$Allow_ports" ; do
ptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done
for Port in "$Open_ports" ; do
iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT
done
# This is the last ruler , it can make you firewall better
iptables -A INPUT -i ppp0 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i ppp0 -p udp -j REJECT --reject-with icmp-port-unreachable

