Linux之防火墙与端口

一、关闭全部的 INPUT FORWARD OUTPUT 只对某些端口开放。
下面是命令实现:

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

再用命令 iptables -L -n 查看 是否设置好, 好看到所有 DROP 了
这样的设置好了,咱们只是临时的, 重启服务器仍是会恢复原来没有设置的状态
还要使用 service iptables save 进行保存
看到信息 firewall rules 防火墙的规则 其实就是保存在 /etc/sysconfig/iptables
能够打开文件查看 vi /etc/sysconfig/iptables
二、
下面我只打开22端口,看我是如何操做的,就是下面2个语句

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

再查看下 iptables -L -n 是否添加上去, 看到添加了

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22

如今Linux服务器只打开了22端口,用putty.exe测试一下是否能够连接上去。
能够连接上去了,说明没有问题。

最后别忘记了保存 对防火墙的设置
经过命令:service iptables save 进行保存
重启iptables 
service iptables save && service iptables restart
关闭防火墙
chkconfig iptables off && service iptables stop


iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
针对这2条命令进行一些讲解吧
-A 参数就当作是添加一条 INPUT 的规则
-p 指定是什么协议 咱们经常使用的tcp 协议,固然也有udp 例如53端口的DNS
到时咱们要配置DNS用到53端口 你们就会发现使用udp协议的

而 --dport 就是目标端口 当数据从外部进入服务器为目标端口
反之 数据从服务器出去 则为数据源端口 使用 --sport

-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
三、禁止某个IP访问
1台Linux服务器,2台windows xp 操做系统进行访问
Linux服务器ip 192.168.1.99
xp1 ip: 192.168.1.2
xp2 ip: 192.168.1.8

下面看看我2台xp 均可以访问的

192.168.1.2 这是 xp1 能够访问的,
192.168.1.8 xp2 也是能够正常访问的。

那么如今我要禁止 192.168.1.2 xp1 访问, xp2 正常访问, 
下面看看演示

经过命令 iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
这里意思就是 -A 就是添加新的规则, 怎样的规则呢? 因为咱们访问网站使用tcp的,
咱们就用 -p tcp , 若是是 udp 就写udp,这里就用tcp了, -s就是 来源的意思,
ip来源于 192.168.1.2 ,-j 怎么作 咱们拒绝它 这里应该是 DROP

好,看看效果。好添加成功。下面进行验证 一下是否生效

一直出现等待状态 最后 该页没法显示 ,这是 192.168.1.2 xp1 的访问被拒绝了。

再看看另一台 xp 是否能够访问, 是能够正常访问的 192.168.1.8 是能够正常访问的
四、如何删除规则
首先咱们要知道 这条规则的编号,每条规则都有一个编号

经过 iptables -L -n --line-number 能够显示规则和相对应的编号
num target     prot opt source               destination
1    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:3306
2    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:21
3    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
多了 num 这一列, 这样咱们就能够 看到刚才的规则对应的是 编号2

那么咱们就能够进行删除了
iptables -D INPUT 2
删除INPUT链编号为2的规则。

再 iptables -L -n 查看一下 已经被清除了。
五、过滤无效的数据包
假设有人进入了服务器,或者有病毒木马程序,它能够经过22,80端口像服务器外传送数据。
它的这种方式就和咱们正常访问22,80端口区别。它发向外发的数据不是咱们经过访问网页请求
而回应的数据包。

下面咱们要禁止这些没有经过请求回应的数据包,通通把它们堵住掉。

iptables 提供了一个参数 是检查状态的,下面咱们来配置下 22 和 80 端口,防止无效的数据包。

iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

能够看到和咱们之前使用的:
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
多了一个状态判断。

一样80端口也同样, 如今删掉原来的2条规则,
iptables -L -n --line-number    这个是查看规则并且带上编号。咱们看到编号就能够
删除对应的规则了。

iptables -D OUTPUT 1     这里的1表示第一条规则。

当你删除了前面的规则, 编号也会随之改变。看到了吧。

好,咱们删除了前面2个规则,22端口还能够正常使用,说明没问题了

下面进行保存,别忘记了,否则的话重启就会还原到原来的样子。

service iptables save    进行保存。

Saving firewall rules to /etc/sysconfig/iptables:          [ OK ]
其实就是把刚才设置的规则写入到 /etc/sysconfig/iptables 文件中。
六、DNS端口53设置
下面咱们来看看如何设置iptables来打开DNS端口,DNS端口对应的是53

你们看到我如今的状况了吧,只开放22和80端口, 我如今看看能不能解析域名。

host www.google.com    输入这个命令后,一直等待,说明DNS不通

出现下面提示 :
;; connection timed out; no servers could be reached

ping 一下域名也是不通
[root@localhost ~ping www.google.com
ping: unknown host www.google.com

我这里的缘由就是 iptables 限制了53端口。

有些服务器,特别是Web服务器减慢,DNS其实也有关系的,没法发送包到DNS服务器致使的。

下面演示下如何使用 iptables 来设置DNS 53这个端口,若是你不知道 域名服务端口号,你

能够用命令 : grep domain /etc/services

[root@localhost ~grep domain /etc/services
domain          53/tcp                          # name-domain server
domain          53/udp
domaintime      9909/tcp                        # domaintime
domaintime      9909/udp                        # domaintime

看到了吧, 咱们通常使用 udp 协议。

好了, 开始设置。。。

iptables -A OUTPUT -p udp --dport 53 -j ACCEPT 
这是咱们 ping 一个域名,数据就是从本机出去,因此咱们先设置 OUTPUT,
咱们按照ping这个流程来设置。

而后 DNS 服务器收到咱们发出去的包,就回应一个回来
iptables -A INPUT -p udp --sport 53 -j ACCEPT

同时还要设置 
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT

好了, 下面开始测试下, 能够用 iptables -L -n 查看设置状况,肯定没有问题就能够测试了

[root@localhost ~iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22 state ESTABLISHED
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:80 state ESTABLISHED
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53

能够测试一下 是否 DNS 能够经过iptables 了。

[root@localhost ~host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com is an alias for www-china.l.google.com.
www-china.l.google.com has address 64.233.189.104
www-china.l.google.com has address 64.233.189.147
www-china.l.google.com has address 64.233.189.99

正常能够解析 google 域名。

ping 方面可能还要设置些东西。

用 nslookup 看看吧

[root@localhost ~nslookup
> www.google.com
Server:         192.168.1.1
Address:        192.168.1.1#53

Non-authoritative answer:
www.google.com canonical name = www.l.google.com.
www.l.google.com        canonical name = www-china.l.google.com.
Name:   www-china.l.google.com
Address: 64.233.189.147
Name:   www-china.l.google.com
Address: 64.233.189.99
Name:   www-china.l.google.com
Address: 64.233.189.104

说明本机DNS正常, iptables 容许53这个端口的访问。
七、iptables对ftp的设置
如今我开始对ftp端口的设置,按照咱们之前的视频,添加须要开放的端口
ftp链接端口有2个 21 和 20 端口,我如今添加对应的规则。

[root@localhost rootiptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@localhost rootiptables -A INPUT -p tcp --dport 20 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 20 -j ACCEPT

好,这样就添加完了,咱们用浏览器访问一下ftp,出现超时。

因此我刚才说 ftp 是比较特殊的端口,它还有一些端口是 数据传输端口,
例如目录列表, 上传 ,下载 文件都要用到这些端口。

而这些端口是 任意 端口。。。 这个 任意 真的比较特殊。

若是不指定什么一个端口范围, iptables 很难对任意端口开放的,
若是iptables容许任意端口访问, 那和不设置防火墙没什么区别,因此不现实的。

那么咱们的解决办法就是 指定这个数据传输端口的一个范围。

下面咱们修改一下ftp配置文件。

我这里使用vsftpd来修改演示,其余ftp我不知道哪里修改,你们能够找找资料。

[root@localhost rootvi /etc/vsftpd.conf

在配置文件的最下面 加入

pasv_min_port=30001
pasv_max_port=31000

而后保存退出。

这两句话的意思告诉vsftpd, 要传输数据的端口范围就在30001到31000 这个范围内传送。

这样咱们使用 iptables 就好办多了,咱们就打开 30001到31000 这些端口。

[root@localhost rootiptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT

[root@localhost rootservice iptables save

最后进行保存, 而后咱们再用浏览器范围下 ftp。能够正常访问

用个帐号登录上去,也没有问题,上传一些文件上去看看。

看到了吧,上传和下载都正常。。 再查看下 iptables 的设置

[root@localhost rootiptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpts:30001:31000

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spts:30001:31000

这是我为了演示ftp特殊端口作的简单规则,你们能够添加一些对数据包的验证
例如 -m state --state ESTABLISHED,RELATED 等等要求更加高的验证windows

相关文章
相关标签/搜索