• iptables 教程,iptables 详解,iptables 常见使用实例


    iptables的详细教程,

    可以参考朱双印的个人博客,http://www.zsythink.net/archives/1199

    iptables由4张表 5条链组成

    表像一个容器,来存储不同类型的规则,
    链是类似一个关卡,大门上写好了不同的表(表里有规则)

    表: filter表,nat表,mangle表,raw表
    链: input链,output链,prerouting链,postrouting链,forward链

    虽然表有4张,其实运维几乎大部分时候用到了 filter表

    虽然链有5条,其实运维几乎大部分的时候用到了input链(output 链偶尔用到,不过数量忽略不计,其他的链更是很少很少用)

    其实运维方面大部分是围绕 filter表,input链展开的

    如 iptables -t filter -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

    关键字 filter(表), INPUT(链),

    iptables 的设置需要依照顺序,来添加规则,顺序不好,影响性能,虽然也许影响的性能只是微乎其微

    最好的规则规划,可以参考iptables 的默认规则

    iptables 规则可以修改配置/etc/sysconfig/iptables文件,之后service iptables restart来生效

    或者直接用类似"iptables -t filter -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT" 这样的方式在命令行添加,

    但是命令行添加后,重启服务器,设置会丢失,需要保存一下,命令是 service iptables save,运行该命令,会把设置

    写入到/etc/sysconfig/iptables中

    centos 7 默认使用firewalld 没有使用iptables
    可以通过如下命令禁用firewalld 并启用iptables,(清空iptables 规则的命令 iptables -F)

    yum install iptables-services -y
    systemctl stop firewalld
    systemctl disable firewalld
    systemctl enable iptables
    service iptables start
    iptables -F
    service iptables save

    以上操作后,启用iptables 并且清空了iptables的规则

    (一)查询规则

    iptables -nL

    n参数是禁止把ip转换成域名的形式,因为很多ip会反解析成域名,不方便查看
    L是列表的意思
    查看现有的filter表的规则,默认省却了 -t filter, 其实全写是iptables -t filter -nL
    如果要查询nat表的规则,那就iptables -t nat -nL

    iptables -nvL

    比上面的事例多了一个v, v参数可以显示不同规则,产生的流量包数量和流量大小

    iptables -nvxL

    比上面的事例多了一个x, 这个在shell编程中可能会用到,不加x ,类似 ls -h, 流量是为了人类可读的方式,根据流量
    大小,转换成Kb或者Mb
    加了x,那么流量的单位就变成了小b, byte,方便shell脚本计算

    iptables -t filter -nvxL INPUT
    iptables -t filter -nvxL OUTPUT
    iptables -t filter -nvxL self_define

    可以指定查看不同链,在行尾放不同的链名称,或者自定义的链名称

    iptables -t filter -nvxL INPUT --line

    比上一个多了一个--line,两个短杠加line ,双短杠不可以省略,原本全写是--line-numbers,已经是省略过的了
    可以查看不同规则的行号

    清空统计

    iptables -Z

    或者指定清零、清空某个链的某个规则

    -Z参数支持指定链名称和规则索引号
     iptables -Z INPUT 1

    (二)修改规则

    iptables -t filter -A INPUT -s 192.168.1.146 -j DROP
    iptables -t filter -A INPUT -s 192.168.1.146 -j ACCEPT

    命令语法: iptabels -t 表名 -A 链名 匹配条件 -j 动作
    -A 是append的意思,这里如果写 -I 就是insert的意思
    append把新增规则放到最后一行
    insert把新增规则放到第一行
    第一个命令是把 s (source的意思) 来自IP 192.168.1.146 的数据包 drop 掉这个命令加到行尾
    第二个命令是把 s (source的意思) 来自IP 192.168.1.146 的数据包 accept 这个命令加到行尾

    iptables -t filter -D INPUT -s 192.168.1.146 -j DROP

    命令语法: iptabels -t 表名 -D 链名 匹配条件 -j 动作
    -D 是delete 删除的意思
    是把 s (source的意思) 来自IP 192.168.1.146 的数据包 drop掉这个命令删除

    iptables -t filter -A INPUT 2 -s 192.168.1.146 -j DROP

    比上面的事例多了一个2(在INPUT后面),这个2的意思是把规则添加到第2行

    iptables -t filter -D INPUT 2

    表示删除filter表中关于INPUT链的2号规则(--line-numbers 中的序列号为2)

    iptabels -t filter -P INPUT ACCEPT
    iptabels -t filter -P FORWARD ACCEPT

    命令的意思是给某个链设定默认的规则,
    第一个命令把INPUT链设置为默认ACCEPT
    第二个命令把FORWARD链设置为默认ACCEPT

    iptables -t filter -F INPUT
    iptables -t filter -F OUTPUT

    第一个命令表示清空filter表中的关于INPUT链的所有规则
    第二个命令表示清空filter表中关于OUTPUT链的所有规则

    iptables -t filter -F
    iptables -t nat -F

    第一个命令表示清空filter表中的所有规则,-f filter 可以省却,写作iptables -F
    第二个命令表示清空nat表中的所有规则

    service iptables save
    /usr/sbin/iptables-save > /etc/sysconfig/iptables
    /usr/sbin/iptables-restore < /etc/sysconfig/iptables-new

    第一行的命令和第二行的命令,功能相同,都是把现有规则写入到系统中 /etc/sysconfig/iptables中
    并且即使重启服务器,iptables规则也不会改变
    第三行命令是说把文件/etc/sysconfig/iptables-new中的规则应用到系统中,需要service iptabels save
    才可以真正写入到系统文件/etc/sysconfig/iptables 中,保证开启重启后有效

    (三)基本匹配条件

    iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP
    iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT
    iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT

    参数-s 用于匹配源地址,如有多个源地址,每个IP之间用逗号隔开,
    支持网段

    iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP
    iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT
    iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT

    参数-d 用于匹配目标地址,如有多个源地址,每个IP之间用逗号隔开,
    支持网段

    iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT
    iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

    参数-p 用于匹配tcp协议,类似的匹配协议类型有 udp,icmp,udplite,esp,ah,sctp等
    centos7 还支持,icmpv6,mh

    iptables -t filter -I INPUT -p icmp -i eth4 -j DROP
    iptables -t filter -I INPUT -p icmp ! -i eth4 -j DROP

    参数-i ,通常和INPUT 配合,针对某个网卡,使用规则

    iptables -t filter -I OUTPUT -p icmp -o eth4 -j DROP
    iptables -t filter -I OUTPUT -p icmp ! -o eth4 -j DROP

    参数-o, 通常和OUTPUT配合,针对某个网卡,使用规则

    (四)扩展匹配条件

    参数-m 后面接模块名称,如tcp模块,udp模块,multiport模块

    iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp --sport 22 -j REJECT
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 22:25 -j REJECT
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport :22 -j REJECT
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 80: -j REJECT
    iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp ! --sport 22 -j REJECT

    参数-p tcp -m tcp 用于匹配tcp协议,
    --sport 源端口
    --dport 目标端口
    可以使用冒号,指定一个连续的端口范围

    iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j DROP
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j DROP
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j DROP
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j DROP
    iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j DROP
    iptables -t filter -A INPUT -p tcp -m multiport --dports 8088,8000:10000 -j DROP

    模块multiport 代替tcp 模块,可以指定多个离散的端口,或者连续的端口

    iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
    iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT
    iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags ALL SYN -j REJECT
    iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags ALL SYN,ACK -j REJECT

    参数 --tcp-flags,判断tcp 头中的标志位

    iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT

    --syn参数,等价于"--tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN"
    专门匹配第一次握手

    有个state模块,-m state --state NEW 该state NEW可以是udp,tcp,icmp等,比--syn适用范围更广

    (五)更多扩展匹配条件

    iprange 模块 (-m iprange --src-range 192.168.1.127-192.168.1.146)
    string 模块 (-m string --algo bm --string "XXOOstring")
    time 模块
    connlimit 模块
    limit 模块

    state模块

    set模块

    iptabels -t filter -I INPUT -m iprange --src-range 192.168.1.127-192.168.1.146 -j DROP
    iptables -t filter -I OUTPUT -m iprange --dst-range 192.168.1.127-192.168.1.146 -j DROP
    iptabels -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.146 -j DROP

    使用iprange模块,指定不同的src或者dst IP range

    iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j DROP
    iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo kmp --string "OOXX" -j DROP

    使用string模块,--algo 指定算法,算法可以为 bm,kmp,这是必需的选项
    --string 指定需要匹配的字符串

    使用ipset来批量添加IP

    例如:

    #创建一个名为myipset的集合,timeout 0允许新增IP带时间参数,最大存储IP数量是1000000
    ipset create -exist myipset hash:net family inet hashsize 1024 maxelem 1000000 timeout 0
    
    #添加一个关于ipset myipset的 iptables规则,
    iptables -I INPUT -m set --match-set myipset src -j DROP
    
    #添加IP 1.2.3.4到myset中,并且timeout是3600秒
    ipset -exist add myipset 1.2.3.4 timeout 3600
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 19:00:00 -J REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 6,7 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --monthdays 22,23 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time ! --monthdays 22,23 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 5 --monthdays 22,23,24,25,26,27,28 -j REJECT
    iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --datestart 2020-05-01 --datestop 2020-05-07 -j REJECT

    使用time模块
    --timestart 指定时间范围开始时间,不可取反
    --timestop 指定时间范围结束时间,不可取反
    --weekdays 用于指定星期几,可取反
    --monthdays 用于指定几号,可取反
    --datestart 用于指定日期范围的开始日期,不可取反
    --datestop 用于指定日期范围的结束日期,不可取反

    iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
    iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT 
    iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 27 -j REJECT

    使用connlimit模块,对连接数量做限制
    --connlimit-above 单独使用此选项时,表示限制每个IP的链接数量
    --connlimit-mask 此选项不能单独使用,使用 --connlimit-above选项时,配合此选项,表示针对某IP段内的IP,限制接数量

    iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
    iptables -t filter -A INPUT -p icmp -j REJECT

    以上事例是两条命令的组合,一起组合使用,表示每分钟允许10个包,令牌桶数量为3,多余的包被reject掉
    使用limit模块,对报文的到达速率做限制,如果要限制单位时间内流入包的数量,就能用limit模块
    --limit-burst 此选项用于指定令牌筒的最大数量,令牌筒要装满,才可以出去
    --limit 此选项用于指定令牌桶中生成新令牌的频率,
    可用的时间单位有second,minute,hour,day

    iptables -t filter -I INPUT -p udp -m udp --dport 137 -j ACCEPT
    iptables -t filter -I INPUT -p udp -m udp --dport 137:157 -j ACCEPT
    iptables -t filter -I INPUT -p udp -m multiport --dport 53,137:157 -j DROP

    udp扩展,可以配合--sport,--dport匹配地址,
    连续端口可以使用冒号,离散的端口可以配合multiport模块指定多个端口

    iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j DROP
    iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8 -j DROP
    iptables -t filter -I INPUT -p icmp -m icmp --icmp-type "echo-request" -j DROP
    iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0/0 -j REJECT
    iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0 -j REJECT

    以上五行效果相同,表示 可以ping 通别人,但是别人不可以ping 通我,
    谦三行针对INPUT设置规则,后两行针对OUTPUT设置规则
    --icmp-type 匹配icmp报文的具体类型
    在有-p 情况下,可以省却-m icmp ,类似的情况有 tcp 和 udp等

    iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    iptables -t filter -A INPUT -j REJECT

    以上三行,需要一起配合使用,才有效果,表示只有回应我们的抱文或者已经建立好链接的请求才可以通过防火墙
    第二行的意思是允许某个端口的第一次tcp连接请求

    iptables -t filter -N IN_WEB

    表示在filter表中创建名为IN_WEB的自定义链

    iptables -t filter -I IN_WEB -p tcp -m multiport --dports 80,8000:10000 -j DROP

    给自定义链IN_WEB设置规则

    iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB

    表示在INPUT链中引用刚才创建的自定义链

    iptables -E IN_WEB WEB

    表示将IN_WEB自定义链重命名为WEB

    iptables -X WEB

    表示删除自定义链WEB
    删除需要满足两个条件
    1. 自定义链没有被引用
    2. 自定义链中没有任何规则

    最好的iptables 规则就是 iptables默认的规则

    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [60:7140]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m multiport --dports 22,80,8181,3000,5000,8000:10000 -j ACCEPT

    -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT

    请根据具体的情况修改iptables 规则

    iptables 中的中括号[]内的数字,例如如下

    INPUT ACCEPT [26:2613]
    FORWARD DROP [0:0]
    OUTPUT ACCEPT [12:844]

    [26:2613] 26是指 26个数据包 2613是指 2613 bytes
    [12:844] 12
    是指 12个数据包 844是指 844 bytes
    有些是否你会注意到 INPUT ACCEPT policy 为 [0:0] 因为这里统计的数据包和大小是指没有被INPUT 规则匹配
    没匹配的数据包会走到默认policy,进而产生数据,也就是在设置了INPUT 规则的情况下,你会看到 INPUT ACCEPT [0:0]
    可以通过命令iptables -nvL 查看当前某个链的数据包数量和流量大小
    service iptables save 之后把当前的数据包数量和流量大小保存到/etc/sysconfig/iptables







  • 相关阅读:
    .Net Core主机配置
    .NET Core 初识
    控制反转IOC,依赖注入DI理解
    依赖倒置原则解析,理解面向抽象编程
    工厂模式
    IOC 概念
    利用Comparator排序
    使用Integer类实现二叉树排序
    先按成绩由高到低,相等则按年龄由低到高
    对象销毁之前进行某些操作
  • 原文地址:https://www.cnblogs.com/faberbeta/p/iptables001.html
Copyright © 2020-2023  润新知