设置静态防火墙

来源:互联网 发布:java中的动态代理模式 编辑:程序博客网 时间:2024/06/07 06:21
原贴:http://blog.chinaunix.net/u/13329/showart.php?id=84158

设置静态防火墙
   
介绍:
这篇文章是本人原创,向读者展示了如何一步一步建立静态防火墙来保护您的计算机,同时在每一步中,我力图向读者讲述清楚原理。在这篇教程之后,你将能理解到防火墙内在过滤机制,同时也能自己动手创建符合自己要求的防火墙。

来自www.linuxsir.org 作者liweioop


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上看过很多教程,它们往往多而全,反而让人望而生畏,希望我的这个教程,能够指导你入门。加油!

最后,我把完整的脚本写出来如下,你只要修改常量定义部分,就能表现出较大的伸缩性



1)#!/bin/bash
#chkconfig: 345 85 15
#description: my iptables rules, which can auto run when system start

# This is a script
# Edit by liwei, cnscn
# establish a static firewall

#网络接口
interdevice="eth0"

#端口
#21   ftp
#22   sshd
#25   smtp
#53   named
#80   http
#110  pop3

#外界可以访问的端口
Open_ports="21  22  80  25  110"

#可以外出的端口
Allow_ports="20 21 22 53 80 2401  8080 20121 20122 23621 23622 "

#清除所有以前设置的规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

#接受所有的,来源不是网络接口$interdevice的数据
# iptables -P INPUT DROP
for eths in $interdevice ; do
   iptables -A INPUT -i ! $eths -j ACCEPT
done

#定义外界可以访问的端口规则
for eths in $interdevice ; do
  for Port in $Open_ports ; do
    iptables -A INPUT -i $eths -p tcp --dport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp --dport $Port -j ACCEPT
  done
done

#定义可以外出的端口规则
for eths in $interdevice ; do
  for Port in $Allow_ports ; do
    iptables -A INPUT -i $eths -p tcp  --sport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp  --sport $Port -j ACCEPT
  done
done

#给不应该进入我们机器的数据,一个欺骗性的回答

for eths in $interdevice ; do
   iptables -A INPUT -i $eths -p tcp -j REJECT --reject-with tcp-reset
   iptables -A INPUT -i $eths -p udp -j REJECT --reject-with icmp-port-unreachable
done




================================================================


2)#!/bin/bash
#chkconfig: 345 85 15
#description: my iptables rules, which can auto run when system start

# This is a script
# Edit by liwei, cnscn
# establish a static firewall

#网络接口
interdevice="eth0 vmnet1 vmnet8"

#端口
#21   ftp
#22   sshd
#25   smtp
#53   named
#80   http
#110  pop3

#外界可以访问的端口
Open_ports="21  22  80  25  110"

#不可以外出的端口,其它端口都可以外出
Allow_ports="106 2999"

#清除所有以前设置的规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

#接受所有的,来源不是网络接口$interdevice的数据
# iptables -P INPUT DROP
for eths in $interdevice ; do
  iptables -A INPUT -i ! $eths -j ACCEPT
done


#定义外界可以访问的端口规则
for eths in $interdevice ; do
  for Port in $Open_ports ; do
    iptables -A INPUT -i $eths -p tcp --dport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp --dport $Port -j ACCEPT
  done
done

#定义不可以外出的端口规则
for eths in $interdevice ; do
  for Port in $Allow_ports ; do
    iptables -A INPUT -i $eths -p tcp ! --sport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp ! --sport $Port -j ACCEPT
  done
done

#给不应该进入我们机器的数据,一个欺骗性的回答

for eths in $interdevice ; do
   iptables -A INPUT -i $eths -p tcp -j REJECT --reject-with tcp-reset
   iptables -A INPUT -i $eths -p udp -j REJECT --reject-with icmp-port-unreachable
done
#End of Script

------------------------------------------------------------


3)#!/bin/bash
#chkconfig: 345 85 15
#description: my iptables rules, which can auto run when system start

# This is a script
# Edit by liwei, cnscn
# establish a static firewall

#网络接口
interdevice="eth0 vmnet1 vmnet8"

#端口
#21   ftp
#22   sshd
#25   smtp
#53   named
#80   http
#110  pop3

#外界可以访问的端口
Open_ports="21  22  80  25  110"

#可以外出的端口
Allow_ports="20 21 22 53 80 2401  8080 20121 20122 23621 23622 "

#清除所有以前设置的规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

#定义每一个网络接口规则
for eths in $interdevice ; do

  #接受所有的,来源不是网络接口$interdevice的数据
  #iptables -P INPUT DROP
  iptables -A INPUT -i ! $eths -j ACCEPT


  #定义外界可以访问的端口规则

  for Port in $Open_ports ; do
    iptables -A INPUT -i $eths -p tcp --dport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp --dport $Port -j ACCEPT
  done


  #定义可以外出的端口规则
  for Port in $Allow_ports ; do
    iptables -A INPUT -i $eths -p tcp  --sport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp  --sport $Port -j ACCEPT
  done


  #给不应该进入我们机器的数据,一个欺骗性的回答
  iptables -A INPUT -i $eths -p tcp -j REJECT --reject-with tcp-reset
  iptables -A INPUT -i $eths -p udp -j REJECT --reject-with icmp-port-unreachable
done
#End of Script


------------------------------------------------------------
4)#!/bin/bash
#chkconfig: 345 85 15
#description: my iptables rules, which can auto run when system start

# This is a script
# Edit by liwei, cnscn
# establish a static firewall

#网络接口
interdevice="eth0 vmnet1 vmnet8"

#端口
#21   ftp
#22   sshd
#25   smtp
#53   named
#80   http
#110  pop3

#外界可以访问的端口
Open_ports="21  22  80  25  110"

#不可以外出的端口,其它端口都可以外出
Allow_ports="106 2999"

#清除所有以前设置的规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

#定义每一个网络接口规则
for eths in $interdevice ; do

  #接受所有的,来源不是网络接口$interdevice的数据
  #iptables -P INPUT DROP
  iptables -A INPUT -i ! $eths -j ACCEPT


  #定义外界可以访问的端口规则
  for Port in $Open_ports ; do
    iptables -A INPUT -i $eths -p tcp --dport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp --dport $Port -j ACCEPT
  done

  #定义不可以外出的端口规则
  for Port in $Allow_ports ; do
    iptables -A INPUT -i $eths -p tcp ! --sport $Port -j ACCEPT
    iptables -A INPUT -i $eths -p udp ! --sport $Port -j ACCEPT
  done

  #给不应该进入我们机器的数据,一个欺骗性的回答
  iptables -A INPUT -i $eths -p tcp -j REJECT --reject-with tcp-reset
  iptables -A INPUT -i $eths -p udp -j REJECT --reject-with icmp-port-unreachable
done

#End of Script
创建于: 2006-03-12 16:45:53,修改于: 2006-08-15 16:00:13,已浏览753次,有评论3条    
  网友评论
  网友:cnscn 时间:2006-03-12 18:15:54 IP地址:221.221.218.★      
when I  use:
# 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

on my computer, I connected to my computer through 21, but when I carry on the command ls, I got the error:

ftp> ls
227 Entering Passive Mode (192,168,0,103,226,70)
ftp: connect: No route to host


when I remove
# 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

I got the right result:
ftp> ls
227 Entering Passive Mode (192,168,0,103,78,7)
150 Here comes the directory listing.
drwxrwxr-x 2 500 500 4096 Mar 02 08:10 bin
-rw-r--r-- 1 500 500 291 Mar 07 07:07 config.inc.php
-rw-r--r-- 1 500 500 16385595 Mar 02 09:14 setup.exe
drwxrwxr-x 4 500 500 4096 Mar 02 08:10 winetools
-rw-r--r-- 1 500 500 21074 Mar 01 09:55 winxp_decor_config.tgz
226 Directory send OK.
ftp> ls

I want your help. What can I do?

=========================================
Here is my iptable:

[root@localhost desktop-softs]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
...
ACCEPT tcp -- anywhere anywhere tcp dpt:ftp
ACCEPT udp -- anywhere anywhere udp dpt:ftp
...
ACCEPT tcp -- anywhere anywhere tcp spt:ftp
ACCEPT udp -- anywhere anywhere udp spt:ftp

REJECT tcp -- anywhere anywhere reject-with tcp-reset
REJECT udp -- anywhere anywhere reject-with icmp-port-unreachable
   
  网友:cnscn 时间:2006-03-12 18:33:09 IP地址:221.221.212.★      
Very Glad:

I have found the right answer:



建置一个防火墙下的ftp server

1)port: 20 and 21

listen_port=21
ftp_data_port=20


启动VSFTPD 之后执行以下两行指令,只允许port 21 以及port 20 开放,其它关闭。

iptables -A INPUT -p tcp -m multiport --dport 21,20 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset



2)port: 2020 and 2121

a)修改/etc/vsftpd/vsftpd.conf 新增底下两行
listen_port=2121
ftp_data_port=2020


b)执行以下两行指令,只允许port 2121 以及port 2020 开放,其它关闭。

iptables -A INPUT -p tcp -m multiport --dport 2121,2020 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset


c)重新启动vsftpd
[root@home vsftpd]# /sbin/service vsftpd restart
Shutting down vsftpd: OK
Starting vsftpd for vsftpd: OK

d)注意: ftp client(如cuteftp)的联机方式不能够选择passive mode,否则无法建立数据的联机。
也就是读者可以连上ftp server,但是执行ls、get 等等的指令时,无法运作。



3)建置一个防火墙下的ftp server,使用PASS FTP mode:

ftp port:2121 以及ftp data port 从9981 到9986。

a)执行以下两行指令,只允许port 2121 以及port 9981-9990 开放,其它关闭。

iptables -A INPUT -p tcp -m multiport --dport 2121,9981,9982,9983,9984,9985,9986,9987,9988,9989,9990 -j ACCEPT
iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset

a)修改/etc/vsftpd/vsftpd.conf
新增底下四行

listen_port=2121
pasv_enable=YES
pasv_min_port=9981
pasv_max_port=9986

c)重新启动vsftpd
[root@home vsftpd]# /sbin/service vsftpd restart
Shutting down vsftpd: OK ]
Starting vsftpd for vsftpd: OK ]

d)注意: ftp client(如cuteftp)的联机方式必须选择passive mode,否则无法建立数据的联机。
也就是读者可以连上ftp server,但是执行ls,get 等等的指令时,便无法运作。
   
  网友:cnscn 时间:2006-03-12 18:36:03 IP地址:221.221.212.★      
Here you must allow the port 20 input, and carray on ftp> passive

.... [ok]

==============================================
[root@192.168.0.89 ~]$ ftp 192.168.0.103 2121
Connected to 192.168.0.103.
..
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

ftp> ls
227 Entering Passive Mode (192,168,0,103,145,115)
ftp: connect: Connection refused

ftp> passive
Passive mode off.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxrwxr-x 2 500 500 4096 Mar 02 08:10 bin
-rw-r--r-- 1 500 500 291 Mar 07 07:07 config.inc.php
-rw-r--r-- 1 500 500 16385595 Mar 02 09:14 setup.exe
drwxrwxr-x 4 500 500 4096 Mar 02 08:10 winetools
-rw-r--r-- 1 500 500 21074 Mar 01 09:55 winxp_decor_config.tgz
226 Directory send OK.
ftp>



 
原创粉丝点击