iptables

一、甜点

关闭两项功能:
1、selinux(生产中也是关闭的),ids.
2、iptables(生产中看情况,内网关闭,外网打开)
大并发的情况,不能开iptables,影响性能,硬件防火墙。

安全优化:
1、尽可能不给服务器配置外网ip,可以通过代理转发。
2、并发不是特别大情况在外网ip的环境,开启防火墙。

 

二、iptables防火墙

基于包过滤的防火墙.
支持7层控制(squid代理+iptables).

Netfilter/iptables是表的容器,iptables包含各个表
(filter,NAT,MANGLE,RAW)

iptables的表又是链的容器
链:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING

链是规则容器:

规则:一条条过滤的语句。。。


提示,所有的链名都要大写

三、查看当前的规则等信息

[root@aliyun ~]# iptables -L -n

删除链,删除自定义的链,将所有的链清0
 --flush   -F [chain]						Delete all rules in  chain or all chains
 --delete-chain    -X [chain]				Delete a user-defined chain
 --zero    -Z [chain [rulenum]]				Zero counters in chain or all chains

[root@aliyun ~]# iptables -F      
[root@aliyun ~]# iptables -X
[root@aliyun ~]# iptables -Z

四、简单操作

[root@aliyun ~]# iptables -A INPUT -p tcp -d 12345 -j DROP
[root@aliyun ~]# iptables -L -n

给链编号,方便删除
[root@ipt ~]# iptables -L -n --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@ipt ~]#  iptables -t filter -D INPUT 1              


-A是添加规则到指定链的结尾,最后一条
-I是添加规则到指定的开头,第一天 

-I INPUT 2 插入到第二行

五、一些匹配的例子

匹配指定协议外的所有协议 
iptables -A INPUT -p ! tcp
匹配主机源IP
iptables -A INPUT -s 10.0.0.14
iptables -A INPUT -s ! 10.0.0.14

匹配网段
iptables -A INPUT -s 10.0.0.0/24
iptables -A INPUT -s ! 10.0.0.0/24
匹配单一端口
iptables -A INPUT -p tcp --sport 53
iptables -A INPUT -p udp --dport 53

匹配指定端口之外的端口
iptables -A INPUT -p tcp --dport ! 22 
iptables -I INPUT -p tcp ! --dport 22  -s 10.0.0.123 -j DROP

iptables -I INPUT -p tcp --dport 52000:53000 -j DROP
匹配端口范围:
iptables -A INPUT -p tcp --sport 22:80
iptables -I INPUT -p tcp --dport 21,22,23,24 -j ACCEPT===》错误语法
iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT
iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT
iptables -I INPUT -p tcp --dport 18:80 -j DROP


匹配ICMP类型
iptables -A INPUT -p icmp --icmp-type 8 
例:iptables -A INPUT -p icmp --icmp-type 8 -j DROP
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
匹配指定的网络接口
iptables -A INPUT -i eth0
iptables -A FORWARD -o eth0


匹配网络状态
-m state --state
    NEW:已经或将启动新的连接
    ESTABLISHED:已建立的连接
    RELATED:正在启动新连接
INVALID:非法或无法识别的
FTP服务是特殊的,需要配状态连接。
允许关联的状态包通过(web服务不要使用FTP服务)
#others RELATED ftp协议
#允许关联的状态包
iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#___________________________ ipconfig conf end _______________________________
比喻:看电影出去WC或者接个电话,回来也得允许进去。

-m limit
--limit n/{second/minute/hour}:指定时间内的请求速率"n"为速率,后面为时间分别为:秒、分、时    
--limit-burst [n]:在同一时间内允许通过的请求"n"为数字,不指定默认为5
fg:本机地址:172.16.14.1,允许172.16.0.0/16网络ping本机,但限制每分钟请求不能超过20,每次并发不能超过6个
iptables -A INPUT -s 172.16.0.0/16 -d 172.16.14.1 -p icmp --icmp-type 8 -m limit --limit 20/min --limit-burst 6 -j ACCEPT
    iptables -A OUTPUT -s 172.16.14.1 -d 172.16.0.0/16 -p icmp --icmp-type 0 -j ACCEPT

六、生产环境配置主机防火墙的两站模式

逛公园及看电影两种方式
逛公园:默认随便进出,对非法的分子进行拒绝,企业应用:企业配置上网网关路由
看电影:默认没票进不去,花钱买票才能看电影,企业应用:服务器主机防火墙
第二种更严格,更安全。

配置一个企业防火墙:用第二种方法

[root@ipt ~]# iptables -F
[root@ipt ~]# iptables -X
[root@ipt ~]# iptables -Z
一、允许本地访问
[root@ipt ~]# iptables -A INPUT -p tcp  --dport 52113 -s 10.0.0.0/24  -j ACCEPT
[root@ipt ~]# iptables -A INPUT -i lo -j ACCEPT
[root@ipt ~]# iptables -A INPUT -o lo -j ACCEPT  ///有问题的
[root@ipt ~]# iptables -A OUTPUT -o lo -j ACCEPT  
二、设置默认的策略
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
三、开启信任的ip网段
iptables -A INPUT -s 124.43.62.96/27 -p all -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT 
iptables -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT    
iptables -A INPUT -s 203.83.24.0/24 -p all -j ACCEPT 
iptables -A INPUT -s 201.82.34.0/24 -p all -j ACCEPT 

四、允许业务服务端口对外访问(允许http服务无条件通过)
iptables -A INPUT -p tcp  --dport 80 -j ACCEPT
 
五 、允许icmp类型协议通过
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
提示:如果不想开,就不执行此行命令。
如果对内开,对外不开就用下面的方式
iptables -A INPUT -p icmp -s 10.0.0.0/24 -m icmp --icmp-type any -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8  -j ACCEPT

六、允许关联的状态包通过(web服务不要使用FTP服务)
iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


七、保存配置文件
/etc/init.d/iptables save


企业iptables面试题:自定义链处理syn攻击
iptables -N syn-flood
iptables -A INPUT -i eth0 -syn -j syn-flood
iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
iptables -A syn-flood -j DROP


/etc/sysconfig/iptables

七、生产环境如何维护

1、需要改配置文件的时候可以直接进去修改
vi /etc/sysconfig/iptables
直接修改
/etc/init.d/iptables reload

2、10.0.0.116这个机器攻击我们服务器或者在BBS里发垃圾帖子。
手工封IP:

写脚本

八、配置网关服务器

服务器网关需具备如下条件:
1)物理条件是具备双网卡,内网卡不配GW

2)确保服务器网关B要可以上网(B上网才能代理别的),可以通过ping baidu.com测试

3)内核文件/etc/sysctl.conf里开启转发功能。
在服务器网关B 上开启路由转发功能。编辑/etc/sysctl.conf修改内容为
net.ipv4.ip_forward = 1,,,然后执行sysctl -p使修改生效。

4)iptables的filter表的FORWARD链允许转发
iptables -P FORWARD ACCEPT

5)查看iptables内核模块
配置网关需要iptables的nat表,PREROUTING,POSTROUTING
[root@aliyun ~]# lsmod | egrep ^ip
iptable_filter          2793  0 
ip_tables              17895  1 iptable_filter
ipv6                  336368  2 ib_ipoib,ib_addr

载入内核模块
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state


[root@aliyun ~]# lsmod | egrep ^ip
iptable_nat             5923  0 
iptable_filter          2793  0 
ip_tables              17895  2 iptable_nat,iptable_filter
ipv6                  336368  2 ib_ipoib,ib_addr


6)处理的局域网的两条命令方法:
方法1:适合于有固定外网地址的:
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.1
1)-s 192.168.1.0/24办公室或IDC内网网段
2)-o eth0 为网关的外网卡地址(ADSL)
3)-j SNAT --to-source 10.0.0.1是网关外网卡ip地址

方法2:适合变化外网地址(ADSL),,==1将内部的网络转发出去
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE 伪装。

==2把外部ip地址及端口映射到内部服务器地址及端口(在10段的主机可以通过访问B 
10.0.0.1:80 ,及可访问到192.168.1.1:9000,c提供的web服务)
iptables -t nat -A PREROUTING -d 10.0.0.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.1:9000

九、iptables常用企业案例:
1、linux主机防火墙(表:FILTER)
2、局域网机器共享上网(表:NAT 链:POSTROUTING)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.1
3、外部地址和端口,映射为内部地址和端口(表:NAT 链:PREROUTING)

iptables -t nat -A PREROUTING -d 10.0.0.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.1:9000

posted @ 2017-09-23 21:56  腐汝  阅读(190)  评论(0编辑  收藏  举报