iptables的基本使用

1、防火墙:一道保护性安全屏障,起到隔离公网,保护内网
根据保护对象区分防火墙
主机型防火墙:保护本机
网络型防火墙:保护其他主机
2、iptables的组成(4表5链)
(1)表(服务的功能分类):
raw:状态跟踪表
mangle:包标记表
nat:地址转换表
filter:过滤表
(2)链(数据包的传输方向,以防火墙服务器本机作为参照)
INPUT(入站规则):匹配到达防火墙本机的数据包
OUTPUT(出站规则):匹配从防火墙本机出去的数据包
FOEWARD(转发规则):匹配经过防火墙主机的数据包
POSTROUTING(路由后规则):路由后处理
PREROUTE(路由前规则):路由前处理
在这里插入图片描述
3、包过滤匹配流程(链里规则的执行顺序,链里可以有多条规则)
(1)顺序匹配,匹配则停止(log除外)
(2)若无任何匹配,则按该链的默认策略处理
4、iptables的用法解析
iptables [-t 表名] 选项 [链名] [-j 目标操作]
目标操作

  • ACCEPT:允许通过/放行
  • DROP:直接丢弃,不给出任何回应
  • REJECT:拒绝通过,必要时会给出提示
  • LOG:记录日志,然后传给下一条规则(“匹配即停止”规则的唯一例外)
    常用的管理选项
    在这里插入图片描述
    5、iptables的基本使用
[root@host51 ~]# yum -y install iptables-services  //安装iptables
[root@host51 ~]# systemctl start iptables		//启动服务
[root@web50 ~]# ping -c 2 192.168.8.51		//51主机设置防火墙规则前,50主机能ping通51主机
PING 192.168.8.51 (192.168.8.51) 56(84) bytes of data.
64 bytes from 192.168.8.51: icmp_seq=1 ttl=64 time=0.268 ms
64 bytes from 192.168.8.51: icmp_seq=2 ttl=64 time=0.607 ms

--- 192.168.8.51 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.268/0.437/0.607/0.170 ms
[root@host51 ~]# iptables -t filter -I INPUT -p icmp -j REJECT		//51主机设置防火墙规则,禁止其他主机ping本机
[root@web50 ~]# ping -c 2 192.168.8.51		//51主机设置防火墙规则后50主机无法ping通51主机
PING 192.168.8.51 (192.168.8.51) 56(84) bytes of data.
From 192.168.8.51 icmp_seq=1 Destination Port Unreachable
From 192.168.8.51 icmp_seq=2 Destination Port Unreachable

--- 192.168.8.51 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1000ms
[root@host51 ~]# iptables -t filter -L INPUT --line-numbers		//查看filter表的规则
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     icmp --  anywhere             anywhere             reject-with icmp-port-unreachable
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
4    ACCEPT     icmp --  anywhere             anywhere            
5    ACCEPT     all  --  anywhere             anywhere            
6    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
7    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[root@host51 ~]# iptables -t filter -D INPUT 1		//删除filter表中刚创建的第一条规则
[root@host51 ~]# iptables -t filter -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     icmp --  anywhere             anywhere            
2    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
3    ACCEPT     icmp --  anywhere             anywhere            
4    ACCEPT     all  --  anywhere             anywhere            
5    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
6    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[root@host51 ~]# iptables -t filter -I INPUT -p icmp -j DROP		//配置51主机防火墙规则,直接丢弃ping包,不予回应
[root@host51 ~]# iptables -t filter -F INPUT		//清空INPUT链中所有规则
[root@host51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy ACCEPT)		//查看INPUT链规则
num  target     prot opt source               destination
[root@host51 ~]# iptables -t filter -nL FORWARD		//查看FORWARD 链的默认规则为ACCEPT
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
[root@host51 ~]# iptables -t filter -P FORWARD DROP		//修改FORWARD 链的默认规则为DROP
[root@host51 ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy DROP)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

6、防护类型及条件

  • 主机型防火墙
    在这里插入图片描述
    在这里插入图片描述
[root@host51 ~]#  iptables -t filter -F	    //删除filter表的所有规则
[root@host51 ~]#  iptables -t nat -F		//删除filter表的所有规则
[root@host51 ~]#  iptables -t mangle -F		//删除mangle表的所有规则
[root@host51 ~]#  iptables -t raw -F		//删除raw表的所有规则
[root@host51 ~]# iptables -t filter -A INPUT -s 192.168.8.254 -p tcp --dport 22 -j ACCEPT		//只允许254主机通过ssh(22端口)远程连接(此时其他端口默认规则是ACCEPT)
[root@host51 ~]# iptables -t filter -P INPUT DROP	//设置filter表的INPUT链规则均为DROP(只允许254主机可以ssh远程连接)
[root@host51 ~]# iptables -t filter -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@web50 ~]# curl http://192.168.8.51/test.html		//50主机无法访问51主机网页
^C
[root@host51 ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT		//允许其他主机通过80访问本机网页
[root@host51 ~]# iptables -t filter -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@web50 ~]# curl http://192.168.8.51/test.html		//50主机可以正常访问51主页
123
  • 禁止其他主机ping通本机,允许本机ping通其他主机
[root@host51 ~]# iptables -t filter -A INPUT -p icmp --icmp-type echo-request -j DROP		//禁止ping请求进来
[root@host51 ~]# iptables -t filter -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT	//允许ping请求出去
[root@host51 ~]# iptables -t filter -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT		//允许ping回复进来

  • 扩展匹配
    在这里插入图片描述
    在这里插入图片描述
[root@host51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:27051
5    ACCEPT     all  --  192.168.8.254        0.0.0.0/0           
6    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
[root@host51 ~]# iptables -t filter -I INPUT 6 -p icmp -m mac --mac-source 52:54:00:75:c4:fb -j DROP		//在INPUT链的第6条规则前插入一条规则,禁止源mac地址为52:54:00:75:c4:fb的主机(这里指host50主机)ping本机
[root@host51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:27051
5    ACCEPT     all  --  192.168.8.254        0.0.0.0/0           
6    DROP       icmp --  0.0.0.0/0            0.0.0.0/0            MAC 52:54:00:75:C4:FB
7    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
[root@host51 ~]# iptables -t filter -D INPUT 2		//连续执行三次该命令,删除INPUT链第2、3、4条规则
[root@host51 ~]# iptables -t filter -A INPUT -p tcp -m multiport --dport 80,27051,3306 -j ACCEPT		//基于多端口设置默认规则,以便提高规则过滤效率 
[root@host51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22
2    ACCEPT     all  --  192.168.8.254        0.0.0.0/0           
3    DROP       icmp --  0.0.0.0/0            0.0.0.0/0            MAC 52:54:00:75:C4:FB
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 80,27051,3306
[root@host51 ~]# iptables -t filter -I INPUT 4 -p icmp -m iprange --src-range 192.168.8.52-192.168.8.54 -j DROP		//在INPUT链的第4条规则前插入一条规则,禁止ip地址在52-54范围的主机ping本机 
[root@host51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.254        0.0.0.0/0            tcp dpt:22
2    ACCEPT     all  --  192.168.8.254        0.0.0.0/0           
3    DROP       icmp --  0.0.0.0/0            0.0.0.0/0            MAC 52:54:00:75:C4:FB
4    DROP       icmp --  0.0.0.0/0            0.0.0.0/0            source IP range 192.168.8.52-192.168.8.54
5    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 80,27051,3306
  • SNAT地址转换
    1、SNAT(Ssource Network Address Translation)源地址转换
    (1)修改数据包的源地址
    (2)仅用于nat表的POSTROUTING链
    配置如下环境
    在这里插入图片描述
    2、环境配置准备
[root@host51 ~]# systemctl stop iptables		//关闭51主机iptables
[root@host52 ~]# yum -y install iptables-services		//52主机安装iptables
[root@host52 ~]# systemctl start iptables
[root@host52 ~]# iptables -t filter -F		//清空所有表规则
[root@host52 ~]# iptables -t raw -F
[root@host52 ~]# iptables -t nat -F
[root@host52 ~]# iptables -t mangle -F
[root@host53 ~]# echo web53 >/var/www/html/test.html		//53主机创建web主页
[root@web50 ~]# route add default gw 192.168.8.52		//给50主机添加网关192.168.8.52
[root@web50 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.52    0.0.0.0         UG    0      0        0 eth0
192.168.8.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@web51 ~]# route add default gw 192.168.8.52		//给51主机添加网关192.168.8.52
[root@web51 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.52    0.0.0.0         UG    0      0        0 eth0
192.168.8.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@host52 ~]# vim /etc/sysctl.conf		//给52主机配置路由转发功能
末行添加:
net.ipv4.ip_forward=1
[root@host52 ~]# sysctl -p		//刷新配置
net.ipv4.ip_forward = 1

3、配置NAT地址转换,并测试访问

[root@host52 ~]# iptables -t nat -A POSTROUTING -s 192.168.8.0/24 -p tcp --dport 80 -j SNAT --to-source 192.168.2.52		//允许192.168.8.0网段主机通过52主机NAT转换为52主机外网ip(192.168.2.52)访问外网
[root@host52 ~]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       tcp  --  192.168.8.0/24       0.0.0.0/0            tcp dpt:80 to:192.168.2.52
[root@host50 ~]# curl http://192.168.2.53/test.html
web53		//50主机测试访问外网(192.168.2.53),成功
[root@host51 ~]# curl http://192.168.2.53/test.html
web53		//51主机测试访问外网(192.168.2.53),成功
[root@host53 ~]# tail -f /etc/httpd/logs/access_log		//查看53主机网页访问日志,访问请求来自192.168.2.52 
192.168.2.52 - - [01/May/2019:09:32:21 -0400] "GET /test.html HTTP/1.1" 200 6 "-" "curl/7.29.0"
192.168.2.52 - - [01/May/2019:09:41:00 -0400] "GET /test.html HTTP/1.1" 200 6 "-" "curl/7.29.0"
  • 地址伪装策略
    共享动态公网IP地址实现上网
    (1)主要针对外网接口的ip地址不固定的情况
    (2)将SNAT改为MASQUERADE即可
    (3)对于ADSL宽带拨号连接,网络接口可写为ppp+
[root@host52 ~]# iptables -t nat -F		//清空nat表规则
[root@host52 ~]# iptables -t nat -A POSTROUTING -s 192.168.8.0/24 -o eth1 -j MASQUERADE		//允许192.168.8.0网段主机通过eth1接口共享公网ip地址访问外网
[root@host52 ~]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  192.168.8.0/24       0.0.0.0/0
[root@web50 ~]# curl http://192.168.2.53/test.html
web53
  • 网络防护(网络型防火墙)
[root@host53 ~]# systemctl stop NetworkManager
 [root@host53 ~]# route add default 192.168.2.52		//给53主机添加到52主机的默认网关
 [root@host53 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.52    0.0.0.0         UG    0      0        0 eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@host52 ~]# iptables -t nat -F		//清除nat表规则(到此步为止,50、51、52、53已实现全网互通—ping、ssh、访问web)
[root@host52 ~]# iptables -t filter -P FORWARD DROP		//设置52主机路由转发模式为DROP(丢弃),限制内网(50、51)与外网(53)互通
[root@web50 ~]# curl http://192.168.2.53/test.html		//测试访问外网失败
^C
[root@host52 ~]# iptables -t filter -A FORWARD -p tcp --dport 80 -j ACCEPT		//设置放行目标端口为80的tcp请求数据包
[root@host52 ~]# iptables -t filter -A FORWARD -p tcp --sport 80 -j ACCEPT		//设置放行源端口为80的tcp回应数据包
[root@host52 ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:80

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@web50 ~]# curl http://192.168.2.53/test.html		//测试访问外网成功
web53	
[root@host52 ~]# iptables -t filter -A FORWARD -p tcp --dport 22 -j ACCEPT		//设置放行目标端口为22的ssh连接请求
[root@host52 ~]# iptables -t filter -A FORWARD -p tcp --sport 22 -j ACCEPT		//设置放行目标端口为22的ssh连接回应请求
[root@host53 ~]# ssh 192.168.8.51		//测试ssh连接成功
root@192.168.8.51's password:
  • 保存防火墙设置
[root@host52 ~]# iptables-save > /etc/sysconfig/iptables		//保存防火墙设置,保证重启iptables后仍有效

补充:

  • DNAT地址转换
    1、SNAT: 是设置内网的设备经过防火器(路由器等)接入到互联网时,按内网设备的不同IP地址、对应选择不同的外网接口(电信、联通等)。即根据源IP地址在网络出口进行对应设置。
    2、DNAT: 是设置外网(互联网)用户访问–>我们防火墙的外网IP所提供的服务(http ssh ftp等)时,将防火墙外网IP提供的服务映射回防火墙的DMZ服务器或者内网服务器
    3、修改源ip地址的目的一般都是为了让这个包能再回到自己这里,所以在iptables中,SNAT是在出口,也即POSTROUTING链发挥作用。修改目的ip地址的原因一般就是为了改变包发送的目的地,让包走出去,而不是留下来,所以在iptables中,DNAT是在入口,也即PREROUTING链中发挥作用,以便让包进入FORWARD表
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值