iptables

1.策略与规则链

1.1策略读取

防火墙会从上至下来读取配置的策略规则,在找到匹配项后就立即结束匹配工作并去执行匹配项中定义的行为(放行/阻止/记录/...)。

如果在读取完所有的策略规则之后没有匹配项,就执行默认策略。

所以设置默认策略为拒绝时,必须要设置允许规则;设置默认策略为允许时,必须要设置拒绝规则。

1.2规则链

iptables把策略条目称为规则,多条规则组成一个规则链

规则链依据数据处理包处理时机不同进行分类:

       进行路由选择前处理数据包(PREROUTING)

       处理流入的数据包(INPUT)

       处理流出的数据包(OUTPUT)

       处理转发的数据包(FORWARD)

       进行路由选择后处理数据包(POSTROUTING)

1.3数据包的处理

       允许流量通过(ACCEPT)

       拒绝流量通过(REJECT) - 流量发送方会看到端口不可达提示(Destination Port Unreachable)

       记录日志信息(LOG)

       丢弃该流量(DROP) - 流量发送方会看到相应超时(无法判断是主机不在线,还是被丢弃)

1.4四表五链

把具有相同功能的规则的集合叫做"表",filter、nat、mangle、raw,称为“四表”

五个处理时机称为“五链”,PREROUTING、INPUT、OUTPUT、FORWARD、POSTROUTING。

PREROUTING      的规则可以存在于:raw表,mangle表,nat表。

INPUT          的规则可以存在于:mangle表,filter表,(centos7中还有nat表,centos6中没有)。

FORWARD         的规则可以存在于:mangle表,filter表。

OUTPUT         的规则可以存在于:raw表mangle表,nat表,filter表。

POSTROUTING      的规则可以存在于:mangle表,nat表。

2.iptables命令

2.1常见参数

-P    设置默认策略 # 注:默认策略中,拒绝只能是DROP

-F    清空规则链

-L    查看规则链

-A    在规则链末尾加入新规则

-I num    在规则链第num行插入新规则,默认1

-D num    删除某一条规则

-s    匹配来源地址IP/MASK,加感叹号!表示除这个IP外

-d    匹配目标地址

-i 网卡名称    匹配从这块网卡流入的数据

-o 网卡名称    匹配从这块网卡流出的数据

-p    匹配协议,如TCP/UDP/ICMP(注:ping就用的ICMP协议)

--dport num    匹配目标端口号

--sport num    匹配来源端口号

-j    指定所匹配到的数据包,进行如何处理(默认策略可以不用)

注:iptables命令配置的防火墙策略,会在重启时失效,使用如下命令可永久生效

       service iptables save

2.2基础应用举例

# 查看所有规则

iptables -L

复制代码
Chain INPUT (policy ACCEPT)

target     prot opt source               destination

ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED

ACCEPT     icmp --  anywhere             anywhere

ACCEPT     all  --  anywhere             anywhere

ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh

REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

 

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

 

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination
View Code
复制代码

# 删除所有规则

iptables -F

iptables -L

复制代码
Chain INPUT (policy ACCEPT)

target     prot opt source               destination

 

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

 

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination
View Code
复制代码

# 更改INPUT链默认策略:不允许所有人访问本机

iptables -P INPUT DROP # Chain INPUT (policy DROP)

远程ssh工具断开连接,且无法ping通!!!!

复制代码
[Administrator.yc] ➤ ping 192.168.0.102

 

正在 Ping 192.168.0.102 具有 32 字节的数据:

请求超时。

请求超时。
View Code
复制代码

# 允许icmp访问

iptables -I INPUT -p icmp -j ACCEPT

复制代码
[Administrator.yc] ➤ ping 192.168.0.102

 

正在 Ping 192.168.0.102 具有 32 字节的数据:

来自 192.168.0.102 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.0.102 的回复: 字节=32 时间<1ms TTL=64
View Code
复制代码

# 去掉icmp访问本机限制

iptables -D INPUT 1

# 更改INPUT链默认策略:允许所有人访问本机

iptables -P INPUT ACCEPT # Chain INPUT (policy ACCEPT)

# 远程ssh工具恢复连接!!!

# 只允许192.168.10.0/24访问本机22端口

iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j REJECT

# 不在192.168.10.0/24网段内的机器无法远程ssh工具连接本机!!!!

iptables -I INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT

# 不允许所有人访问本机12345端口

iptables -I INPUT -p tcp --dport 12345 -j REJECT

iptables -I INPUT -p udp --dport 12345 -j REJECT

# 不允许192.168.10.5访问本机80端口(默认策略是允许,所以其他人可以访问)

iptables -I INPUT -p tcp -s 192.168.10.5 --dport 80 -j REJECT

# 不允许所有人访问本机1000~1024端口

iptables -I INPUT -p tcp --dport 1000:1024 -j REJECT

iptables -I INPUT -p udp --dport 1000:1024 -j REJECT

iptables -L

复制代码
Chain INPUT (policy ACCEPT)

target     prot opt source               destination

REJECT     udp  --  anywhere             anywhere            udp dpts:cadlock2:1024 reject-with icmp-port-unreachable

REJECT     tcp  --  anywhere             anywhere            tcp dpts:cadlock2:1024 reject-with icmp-port-unreachable

REJECT     tcp  --  192.168.0.5          anywhere            tcp dpt:http reject-with icmp-port-unreachable

REJECT     udp  --  anywhere             anywhere            udp dpt:italk reject-with icmp-port-unreachable

REJECT     tcp  --  anywhere             anywhere            tcp dpt:italk reject-with icmp-port-unreachable

ACCEPT     tcp  --  192.168.0.0/24       anywhere            tcp dpt:ssh

ACCEPT     tcp  --  192.168.10.0/24      anywhere            tcp dpt:ssh

REJECT     tcp  --  anywhere             anywhere            tcp dpt:ssh reject-with icmp-port-unreachable

 

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

 

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination
View Code
复制代码

3.扩展资料

iptables概览

http://www.cnblogs.com/metoy/p/4320813.html

iptables实战

http://www.cnblogs.com/JemBai/archive/2009/03/19/1416364.html

linuxのNat

http://www.cnblogs.com/JemBai/archive/2012/04/27/2474003.html

iptables系列博客

http://www.zsythink.net/archives/category/%E8%BF%90%E7%BB%B4%E7%9B%B8%E5%85%B3/%E9%98%B2%E7%81%AB%E5%A2%99/page/2/

posted @   yc紫日  阅读(166)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示