美文网首页
iptables的应用

iptables的应用

作者: 小尛酒窝 | 来源:发表于2018-05-12 18:51 被阅读0次

前言

在上一篇文章我记录了iptables的链表规则的相关描述及解析,那么本篇我们将来共同探讨验证下iptables的应用。

环境准备

在centos7系统上启动iptables:

[root@iptables ~]# systemctl start firewalld

清除所有iptables规则并另行配置默认的iptables规则:

[root@iptables ~]# iptables -F
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 22 -j ACCEPT  #放开电脑主机到虚拟机的ssh连接
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -d 192.168.0.38 -p tcp --sport 22 -j ACCEPT   #放开电脑主机到虚拟机的ssh连接
[root@iptables ~]# iptables -A OUTPUT -j REJECT  #配置OUTPUT最后一条策略的处理动作为REJECT
[root@iptables ~]# iptables -A INPUT -j REJECT  #配置INPUT最后一条策略的处理动作为REJECT
[root@iptables ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1180 91700 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.0.81         tcp dpt:22
    4   240 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  118 15264 ACCEPT     tcp  --  *      *       192.168.0.81         0.0.0.0/0            tcp spt:22
    3   264 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

iptables的应用

  • iptables的多端口匹配
    Linux系统上配置iptables放开相应的80、21、22、23、3306的端口访问:
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp -m multiport --dports 3306,21:23,80 -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp -m multiport --sport 3306,21:23,80 -j ACCEPT
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p udp -m multiport --dports 3306,21:23,80 -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p udp -m multiport --sports 3306,21:23,80 -j ACCEPT
  • 匹配连读的地址访问:
    允许192.168.0.1-192.168.0.50的地址访问到192.168.0.81的80端口:
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 80 -m iprange --src-range 192.168.0.1-192.168.0.50 -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp --sport 80 -m iprange --dst-range 192.168.0.1-192.168.0.50 -j ACCEPT
  • 按照时间标签作访问控制
    只允许在每周一到周五的上午9点到下午5点访问指定IP的80端口:
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp --sport 80 -m time --kerneltz --timestart 9:00 --timestop 17:00 -m time --kerneltz --weekdays 1,2,3,4,5 -j ACCEPT
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 80 -m time --kerneltz --timestart 9:00 --timestop 17:00 -m time  --kerneltz --weekdays 1,2,3,4,5  -j ACCEPT
  • 对报文做字符串模式匹配检查:
    拒绝80端口中带有vpn字符串的报文:
[root@iptables ~]# iptables -F
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp --sport 80 -m string --algo bm --string "vpn" -j REJECT
  • 对匹配的地址做并发连接数限制
    限制指定IP的22端口的并发数为3
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
  • 基于收发报文做速率限制
    限制icmp报文的收发速率:
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p icmp --icmp-type 8 -m limit --limit 5/minute --limit-burst 5 -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p icmp --icmp-type 0 -m limit --limit 5/minute --limit-burst 5 -j ACCEPT
  • 报文状态匹配及连接状态追踪
    仅匹配放开会话状态为ESTABLISHTED且端口为22的连接:
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 22 -m state --state ESTABLISHED -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp -sport 22 -m state --state ESTABLISHED -j ACCEPT
iptables v1.4.21: multiple -s flags not allowed
Try `iptables -h' or 'iptables --help' for more information.
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

对ftp服务作会话追踪:

#装载ftp连接追踪的专用模块
[root@iptables ~]# modprobe  nf_conntrack_ftp  
#放开命令链接
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
#放开数据连接
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -m state --state ESTABLISHED -j ACCEPT
#查看已经追踪并记录下来的链接
root@iptables ~]# cat /proc/net/nf_conntrack
......
ipv4     2 tcp      6 431997 ESTABLISHED src=192.168.0.85 dst=192.168.0.81 sport=52070 dport=21 src=192.168.0.81 dst=192.168.0.85 sport=21 dport=52070 [ASSURED] mark=0 secctx=system_u:object_r:unlabeled_t:s0 zone=0 use=3
ipv4     2 tcp      6 428774 ESTABLISHED src=192.168.0.38 dst=192.168.0.81 sport=51061 dport=22 src=192.168.0.81 dst=192.168.0.38 sport=22 dport=51061 [ASSURED] mark=0 secctx=system_u:object_r:unlabeled_t:s0 zone=0 use=2
  • 端口跳转
    将服务的80端口重定向到8080端口:
[root@iptables ~]# iptables -I INPUT -d 192.168.0.81 -p tcp -m multiport --dports 80,8080 -j ACCEPT
[root@iptables ~]# iptables -I OUTPUT -s 192.168.0.81 -p tcp -m multiport --sports 80,8080 -j ACCEPT
[root@iptables ~]# iptables -A PREROUTING -t nat -d 192.168.0.81 -p tcp --dport 80 -j REDIRECT --to-ports 8080
  • SNAT源地址转换
测试环境逻辑拓扑

在server1启用iptables并设置SNAT地址转换,使得10.10.10.0/24网段能够正常访问互联网:

#首先启用IP转发功能
[root@iptables ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
#重启firewalld服务,把iptables恢复默认配置
[root@iptables ~]# systemctl restart firewalld
#添加SNAT规则
[root@iptables ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eno16777736 -j SNAT --to-source 192.168.0.81

另外还可以使用MASQUERADE处理动作来实现SNAT地址转换,此动作适用于动态IP环境:

[root@iptables ~]# systemctl restart firewalld
[root@iptables ~]# iptables -F
[root@iptables ~]# iptables -t nat -F
[root@iptables ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j MASQUERADE
  • PNAT目的端口映射


    测试环境逻辑拓扑图

    在Server1上通过iptables设置PNAT使得client192.168.0.38能通过192.168.0.81的8080端口访问10.10.10.10的80端口:

[root@iptables ~]# systemctl restart firewalld
[root@iptables ~]# iptables -F
[root@iptables ~]# iptables -t nat -F
[root@iptables ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eno16777736 -j SNAT --to-source 192.168.0.81
[root@iptables ~]# iptables -t nat -A PREROUTING -d 192.168.0.81 -p tcp --dport 8080 -j DNAT --to-destination 10.10.10.10:80

另外也可以通过一对一目的地址映射实现:

[root@iptables ~]# systemctl restart firewalld
[root@iptables ~]# iptables -F 
[root@iptables ~]# iptables -t nat -F
[root@iptables ~]# iptables -t nat -A PREROUTING -d 192.168.0.81 -j DNAT --to-destination 10.10.10.10
[root@iptables ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eno16777736 -j SNAT --to-source 192.168.0.81

相关文章

  • iptables filter表案例、iptables nat表

    目录 一、iptables filter表案例二、iptables nat表应用 扩展 iptables应用在一个...

  • linux日常管理(四)--iptables案例

    10.15 iptables filter表案例10.16/10.17/10.18 iptables nat表应用...

  • iptables应用

    iptables:四表五链: 四表:raw: prerouting output 关闭nat表打开的连接追踪功...

  • iptables的应用

    前言 在上一篇文章我记录了iptables的链表规则的相关描述及解析,那么本篇我们将来共同探讨验证下iptable...

  • iptables系列二

    iptables系列之基本应用及显式扩展 netfilter:Framework,TCP,内核中 iptables...

  • iptables的具体应用

    题目1.PC机只能访问v.secevery.com这个网站,别的都不可以。而且只能通过内网进行链接,不可以通过外网...

  • [原创] 在高通MDM9607设备上配置iptables服务

    1 Iptables简介 Iptables 更确切的说是一个应用程序,工作在linux的用户层,对 Linux 内...

  • iptables学习笔记

    iptables需要处理两个维度的信息: 在什么时候应用配置,这便是iptables中的链(chain),对应于n...

  • 关于iptables

    什么是iptables 常见于Linux系统下应用层防火墙工具 使用场景 场景:模拟使用iptables控制并发h...

  • linux iptables的简单用法

    Iptables 是标准的 Linux防火墙应用程序,在没有硬件防火墙的情况下,使用iptables也是一种简单经...

网友评论

      本文标题:iptables的应用

      本文链接:https://www.haomeiwen.com/subject/lwdtdftx.html