防火墙是一道关卡,阻止******的一种手段,防火墙是安全门,过滤网络的数据包,接下来的主要讲的iptables是一个重要的服务,现在我们隆重推出。

一、iptables基础

1.包的处理方式:DROP,ACCEPT,REJECT,LOG

[root@master ~]# iptables -A INPUT -p tcp  -j DROP 

2.iptables常见参数

[root@master ~]#iptables -[A R D I] INPUT[OUTPUT]  -p tcp[udp icmp] -i interface -m state --state ESTABFISHED -j DROP[ACCEPT REJECT LOG]  

注释:

-A 添加

-R 替换  

-D 删除  

-I 插入  

-p 协议类型tcp udp icmp  

-i 接口参数  -

m 匹配  -

j 目标参数 target有 ACCEPT 接收,DROP 丢弃,REJECT弹回,LOG加入日志

3.为本机提供一人安全的防护,将防火墙默认规则设置为DROP,在应用中input chian and

forward chain 任选其一,它们不可能同时生效。INPUT与output是对本地包的过滤forward链是网络包的保护。

二.防火墙的应用实例

1.设置默认规则

[root@master ~]# iptables -P INPUT DROP                   [root@master ~]# iptables -P OUTPUT DROP               

2.允许dns通信

[root@master ~]# iptables -A OUTPUT -p udp --dport 53 -j ACCEPT  

[root@master ~]# iptables -A INPUT  -p udp --sport 53 -j ACCEPT

[root@master ~]#  nslookup                                            >  www.tvmining.com                                                 Server: 192.168.91.2                                                 Address:192.168.91.2#53                                           Non-authoritative answer:                                           Name: www.tvmining.com                                             Address: 58.215.50.172                                              

本机为dns服务器,dns设置为本机ip,设置防火墙如下

 [root@master ~]# iptables -A INPUT  -p udp --dport 53 -j ACCEPT  [root@master ~]# iptables -A OUTPUT -p udp --sport 53 -j ACCEPT

3.防火墙对ssh服务设置

(1)作为ssh服务器时的设置

 [root@master ~]# iptables -A INPUT  -p tcp --dport 22 -j ACCEPT  [root@master ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

(2)作为客户机连接ssh服务

 [root@master ~]# iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT  [root@master ~]# iptables -A INPUT  -p tcp --sport 22 -j ACCEPT

 注:ssh连接本机ip是服务器作为客户机连接ssh服务的一特例。

上面对dns服务与ssh服务设置的规则有漏洞,如果***利用网络包欺骗,便可***服务;只有当服务器iptables对发出包的进行验证,如果是已经建立的连接的数据包(ESTABFILISHED),则允许通过,否则DROP。

(3)作为ssh,dns服务器时

[root@master ~]# iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

[root@master ~]# iptables -A OUTPUT -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT

(4)作为客户机iptables对ssh,dns设置

 

[root@master ~]# iptables -A INPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

[root@master ~]# iptables -A INPUT -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT

三.iptables对nat应用的实例

1.设置企业共享上网的方案

(1)开启linux服务器的路由

将/etc/sysctl.conf中的net.ipv4.ip_forward = 0更改为net.ipv4.ip_forward = 1.

(2)设置nat,进行地址转换

[root@master ~]# iptables -t nat -A POSTROUTING -s 192.168.120.0/24 -j MASQUERADE

静态转换

[root@master ~]# iptables -t nat -A POSTROUTING -s 192.168.120.0/24 -j SNAT --to-source 192.168.4.70  

 2.透明代理应用案例

(1)更改配置vim /etc/squid/squid.conf中http_port选项如下

http_port  192.168.100.120:3128  transparent

(2)设置地址转换

[root@master ~]# iptables -t nat -A   PREROUTING -i eth0 -s      192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
 
3.将内网的应用共享到公网
[root@master ~]# iptables -t nat -A PREROUTING -d 192.168.4.70 -p tcp --dport 80 -j DNAT --to-destination 192.168.6.70
[root@master ~]# iptables -t nat -A PREROUTING -d 192.168.4.70 -p tcp --dport 21 -j DNAT --to-destination 192.168.6.70