|
|
第1行: |
第1行: |
| ==iptables简介==
| |
| iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表。
| |
|
| |
|
| filter负责过滤数据包,包括的规则链有,input,output和forward;
| | 而相比于普通的壁纸自动换应用,「Windows 聚焦」有它独有的优势:高质量壁纸、随着使用过程而不断提高的壁纸筛选能力,前者主观上来说也很大程度上依赖于后者。 |
|
| |
|
| nat则涉及到网络地址转换,包括的规则链有,prerouting,postrouting和output;
| |
|
| |
|
| mangle表则主要应用在修改数据包内容上,用来做流量整形的,默认的规则链有:INPUT,OUTPUT,NAT,POSTROUTING,PREROUTING;
| | 1.「Windows 聚焦」的壁纸都保存在一个叫做 Assets 的隐藏文件夹里,打开资源管理器,在地址栏键入下方路径后按回车键,即可快速跳转至这个隐藏的文件夹。为了方便日后访问,建议大家通过左上角功能区将这个文件夹目录固定到资源管理器的「快速访问」区域。 |
| | %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets |
|
| |
|
| input匹配目的IP是本机的数据包,forward匹配流经本机的数据包,prerouting用来修改目的地址用来做DNAT,postrouting用来修改源地址用来做SNAT。
| |
|
| |
|
| ==iptables主要参数==
| | [https://zhuanlan.zhihu.com/p/30914809 如何保存 Windows 10「聚焦」功能的精美壁纸] |
| <pre>-A 向规则链中添加一条规则,默认被添加到末尾
| | [[category:desktop]] |
| -T指定要操作的表,默认是filter
| |
| -D从规则链中删除规则,可以指定序号或者匹配的规则来删除
| |
| -R进行规则替换
| |
| -I插入一条规则,默认被插入到首部
| |
| -F清空所选的链,重启后恢复
| |
| -N新建用户自定义的规则链
| |
| -X删除用户自定义的规则链
| |
| -p用来指定协议可以是tcp,udp,icmp等也可以是数字的协议号,
| |
| -s指定源地址
| |
| -d指定目的地址
| |
| -i进入接口
| |
| -o流出接口
| |
| -j采取的动作,accept,drop,snat,dnat,masquerade
| |
| –sport源端口
| |
| –dport目的端口,端口必须和协议一起来配合使用
| |
| | |
| -m:extend matches,这个选项用于提供更多的匹配参数,如:
| |
| -m state –state ESTABLISHED,RELATED
| |
| -m tcp –dport 22
| |
| -m multiport –dports 80,8080
| |
| -m icmp –icmp-type 8
| |
| | |
| 注意:所有链名必须大写,表明必须小写,动作必须大写,匹配必须小写
| |
| | |
| | |
| 三个表 filter nat and mangle
| |
| | |
| filter 是默认的表,有三个链 INPUT OUTPUT AND FORWARD
| |
| | |
| 在处理各种数据包时,根据防火墙规则的不同介入时机,iptables供涉及5种默认规则链,从应用时间点的角度理解这些链:
| |
| | |
| INPUT链:当接收到防火墙本机地址的数据包(入站)时,应用此链中的规则。
| |
| OUTPUT链:当防火墙本机向外发送数据包(出站)时,应用此链中的规则。
| |
| FORWARD链:当接收到需要通过防火墙发送给其他地址的数据包(转发)时,应用此链中的规则。
| |
| PREROUTING链:在对数据包作路由选择之前,应用此链中的规则,如DNAT。
| |
| POSTROUTING链:在对数据包作路由选择之后,应用此链中的规则,如SNAT。
| |
| | |
| | |
| filter表(默认使用的表):用于过滤数据包的进出,其中又包含了3个链:
| |
| INPUT:负责过滤进入主机的数据包
| |
| OUTPUT:负责过滤主机发出的数据包
| |
| FORWARD:负责转发该主机的数据包到其他目标地址
| |
| nat表(网络地址转换):多用于内外网地址转换,包含了3个链:
| |
| PREROUTING: 实现DNAT功能,改变数据包的目的地址,多用于局域网接收公网数据时将目标的地址转换为局域网的一个地址
| |
| POSTROUTING:实现SNAT功能,改变数据包的源地址,多用于局域网向公网发送数据时将地址转换为公网地址
| |
| OUTPUT:同filter
| |
| mangle表与raw表很少使用,这里略过 </pre>
| |
| | |
| ==详细语法:==
| |
| <pre>
| |
| | |
| 查看规则
| |
| iptables -n -L
| |
| | |
| iptables [-t 表名] <-A| I| D| R>[链名][规则编号][-i|o 网卡名称] [-p 协议类型] [-s 源ip地址|源子网][–sport 源端口号] [-d 目标ip地址|目标子网] [–dport 目标端口号][-j 匹配到以后的动作]
| |
| | |
| iptables [-t 要操作的表]<操作命令>[要操作的链][规则号码][匹配条件][-j匹配到以后的动作]
| |
| | |
| 举例说明
| |
| -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
| |
| | |
| -i 输入接口
| |
| -o 输出接口
| |
| | |
| 显式扩展(-m)
| |
| 扩展各种模块
| |
| -m multiport:表示启用多端口扩展
| |
| | |
| #所有的扩展匹配表示要使用-m来指定扩展的名称来引用,而每个扩展模块一般都会有自己特有的专用选项,在这些选项中,有些是必备的:
| |
| | |
| 使用-m来指定其状态并赋予匹配规则,语法如下
| |
| | |
| -mstate –state 状态
| |
| NEW,ESTABLISHED 已建立的 ,RELATED,INVALID
| |
| RELATED(关连)
| |
| | |
| | |
| | |
| INVALID
| |
| meaning that the packet is associated with no known connection
| |
| | |
| NEW meaning that the packet has started a new connection, or oth-
| |
| erwise associated with a connection which has not seen packets
| |
| in both directions, and
| |
| | |
| ESTABLISHED
| |
| meaning that the packet is associated with a connection which
| |
| has seen packets in both directions,
| |
| | |
| RELATED
| |
| meaning that the packet is starting a new connection, but is
| |
| associated with an existing connection, such as an FTP data
| |
| transfer, or an ICMP error.
| |
| </pre>
| |
| | |
| == centos7.x ==
| |
| [[Iptables on centos7]]
| |
| <pre>
| |
| | |
| </pre>
| |
| | |
| == centos6.x 是初始的iptables ==
| |
| <pre>*filter
| |
| :INPUT ACCEPT [0:0]
| |
| :FORWARD ACCEPT [0:0]
| |
| :OUTPUT ACCEPT [0:0]
| |
| -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
| |
| -A INPUT -p icmp -j ACCEPT
| |
| -A INPUT -i lo -j ACCEPT
| |
| -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
| |
| -A INPUT -j REJECT --reject-with icmp-host-prohibited
| |
| -A FORWARD -j REJECT --reject-with icmp-host-prohibited
| |
| COMMIT
| |
| | |
| -A INPUT -j REJECT --reject-with icmp-host-prohibited
| |
| -A FORWARD -j REJECT --reject-with icmp-host-prohibited
| |
| # 这两条的意思是在INPUT表和FORWARD表中拒绝所有其他不符合上述任何一条规则的数据包。并且发送一条host prohibited的消息给被拒绝的主机。</pre>
| |
| | |
| ==my iptables.sh==
| |
| <pre>
| |
| #!/bin/bash
| |
| PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH
| |
| #my iptables.sh for web server
| |
| # 1. 清除规则
| |
| iptables -Z
| |
| iptables -F
| |
| #数据包计数器和流量计数器归0
| |
| iptables -X
| |
| | |
| modprobe ip_tables
| |
| #这个默认要开才行
| |
| #modprobe ip_tables_nat
| |
| modprobe ip_nat_ftp
| |
| modprobe ip_conntrack_ftp
| |
| #
| |
| #modprobe ipt_MASQUERADE
| |
| | |
| # 2. 设定政策
| |
| iptables -P INPUT DROP
| |
| iptables -P OUTPUT ACCEPT
| |
| iptables -P FORWARD DROP
| |
| | |
| #3.制订各项规则
| |
| iptables -A INPUT -i lo -j ACCEPT
| |
| iptables -A INPUT -s 127.0.0.1/32 -j ACCEPT
| |
| #iptables -A INPUT -o lo -j ACCEPT
| |
| iptables -A INPUT -s 127.0.0.1/32 -j ACCEPT
| |
| | |
| iptables -A INPUT -p tcp -m multiport --dports 22,80 -j ACCEPT
| |
| iptables -A INPUT -m state --state new -m tcp -p tcp --dport 4505 -j ACCEPT
| |
| iptables -A INPUT -m state --state new -m tcp -p tcp --dport 4506 -j ACCEPT
| |
| iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
| |
| | |
| iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
| |
| #-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
| |
| | |
| iptables -I INPUT -s 1113.108.187.232/255.255.255.248 -p tcp -m tcp --dport 22 -m comment --comment "sshd from dkm" -j ACCEPT
| |
| #- INPUT -s 114.215.165.29 -p tcp -m tcp --dport 22 -m comment --comment "sshd from dkm" -j ACCEPT
| |
| iptables -I INPUT -s 114.215.165.29/32 -p tcp -m tcp --dport 22 -m comment --comment "sshd from dkm" -j ACCEPT
| |
| iptables -A INPUT -s 1113.108.187.232/29 -p tcp -m tcp --dport 22 -m comment --comment "sshd from dkm" -j ACCEPT
| |
| #-A INPUT -s 147.88.148.158/32 -p tcp -m multiport --dports 4505,4506 -m comment --comment "salt_agentd listen " -j ACCEPT
| |
| #-A INPUT -s 110.45.245.254/32 -p tcp -m tcp --dport 6379 -m comment --comment "redis listen " -j ACCEPT
| |
| iptables -A INPUT -s 1119.29.183.102 -p tcp -m tcp --dport 10050 -m comment --comment "zabbix_agentd listen " -j ACCEPT
| |
| | |
| iptables -A INPUT -s 10.0.0.0/8 -p tcp -m tcp -j ACCEPT
| |
| iptables -A INPUT -s 192.168.1.0/22 -p tcp -m tcp -j ACCEPT
| |
| iptables -A INPUT -s 147.88.148.158/32 -p tcp -m multiport --dports 4505,4506 -j ACCEPT
| |
| #-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
| |
| #-A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT
| |
| iptables -A INPUT -p tcp --match multiport --dports 80,443 -m comment --comment "web" -j ACCEPT
| |
| #-A INPUT -p tcp -m multiport --dports 80,443 comment --comment "web" -j ACCEPT
| |
| #-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
| |
| #-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
| |
| iptables -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
| |
| iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
| |
| iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
| |
| iptables -A INPUT -j DROP
| |
| #-A INPUT -j REJECT --reject-with icmp-host-prohibited
| |
| iptables -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
| |
| iptables -A syn-flood -j REJECT --reject-with icmp-port-unreachable
| |
| | |
| | |
| # 4. 写入防火墙规则配置文件
| |
| /etc/init.d/iptables save
| |
| </pre>
| |
| ==2018 testapi==
| |
| <pre>
| |
| # product prxnginx and api also is ok 此机器有 nginx tomcat Generated by iptables-save v1.4.7 on Mon Oct 29 16:33:14 2018
| |
| *filter
| |
| #:INPUT ACCEPT [188:22246]
| |
| :INPUT DROP [0:0]
| |
| :FORWARD ACCEPT [0:0]
| |
| :OUTPUT ACCEPT [207:45428]
| |
| :syn-flood - [0:0]
| |
| -A INPUT -i lo -j ACCEPT
| |
| -A INPUT -s 127.0.0.1/32 -j ACCEPT
| |
| -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
| |
| | |
| -I INPUT -p tcp --dport 22 -j ACCEPT
| |
| -I OUTPUT -p tcp --sport 22 -j ACCEPT
| |
| #-A INPUT -s 172.18.140.0/32 -p tcp -m tcp --dport 22 -m comment --comment "intra ssh" -j ACCEPT
| |
| -I INPUT -s 172.18.140.0/22 -p tcp -m tcp -m comment --comment "intra" -j ACCEPT
| |
| #-A INPUT -s 10.0.0.0/8 -p tcp -m tcp -j ACCEPT
| |
| ##-A INPUT -s 192.168.1.0/22 -p tcp -m tcp -j ACCEPT
| |
| -A INPUT -p tcp --match multiport --dports 80,443 -m comment --comment "web" -j ACCEPT
| |
| -I INPUT -s 47.106.65.97 -p tcp -m tcp --dport 5495 -m comment --comment "redis" -j ACCEPT
| |
| | |
| -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
| |
| -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
| |
| -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
| |
| -A INPUT -j DROP
| |
| #-A INPUT -j REJECT --reject-with icmp-host-prohibited
| |
| -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
| |
| -A syn-flood -j REJECT --reject-with icmp-port-unreachable
| |
| COMMIT
| |
| | |
| </pre>
| |
| | |
| == 其它==
| |
| <pre>保存方法一
| |
| 对于RHEL和ceontos系统可以使用service iptables save将当前内存中的规则保存到/etc/sysconfig/iptables文件中
| |
| service iptables save
| |
| | |
| 保存方法二
| |
| 修改/etc/sysconfig/iptables-config 将里面的IPTABLES_SAVE_ON_STOP=”no”, 这一句的”no”改为”yes”这样每次服务在停止之前会自动将现有的规则保存在 /etc/sysconfig/iptables 这个文件中去。
| |
| | |
| 折中的方案,也是打多少硬件防火墙厂商所采用的方案,他们都是采用INPUT默认禁用所有,OUTPUT默认允许所有,你只要关注INPUT规则即可。
| |
| -P INPUT DROP
| |
| -P FORWARD ACCEPT
| |
| -P OUTPUT ACCEPT
| |
| *****
| |
| | |
| 规则表之间的优先顺序:
| |
| | |
| Raw——mangle——nat——filter
| |
| | |
| 规则链之间的优先顺序(分三种情况):
| |
| -m –match 匹配的模块 指定数据包规则所使用的过滤模块
| |
| | |
| 对于RHEL和ceontos系统可以使用service iptables save将当前内存中的规则保存到/etc/sysconfig/iptables文件中
| |
| service iptables save
| |
| | |
| 11、参数 -m multiport –destination-port
| |
| 范例 :iptables -A INPUT -p tcp -m multiport –destination-port 22,53,80,110 -j ACCEPT
| |
| 说明:用来比对不连续的多个目的地端口号,设定方式同上。
| |
| | |
| 12、参数 -m multiport –port
| |
| 范例:iptables -A INPUT -p tcp -m multiport –port 22,53,80,110 -j ACCEPT
| |
| 说明:这个参数比较特殊,用来比对来源端口号和目的端口号相同的数据包,设定方式同上。注意:在本范例中,如果来源端口号为 80,目的地端口号为 110,这种数据包并不算符合条件。
| |
| | |
| Iptables详解
| |
| http://blog.csdn.net/reyleon/article/details/12976341
| |
| | |
| iptable基本操作
| |
| ptables -L -n 列出iptables规则
| |
| iptables -F 清除iptables内置规则
| |
| iptables -X 清除iptables自定义规则
| |
| | |
| 设定默认规则
| |
| 在iptables规则中没有匹配到规则则使用默认规则进行处理
| |
| | |
| iptables -P INPUT DROP
| |
| iptables -P OUTPUT ACCEPT
| |
| iptables -P FORWARD DROP
| |
| | |
| 用以一次匹配多个端口
| |
| iptables -A INPUT -p tcp -m muliport --dports 21,22,25,80,110 -j ACCEPT
| |
| | |
| 存储于恢复iptables规则
| |
| iptables-save > somefile
| |
| iptables-restore < somefile eg 设置仅22、80端口可访问 iptables -A INPUT -p tcp –dport 22 -j ACCEPT
| |
| iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT
| |
| iptables -A INPUT -p tcp –dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
| |
| iptables -A OUTPUT -p tcp –sport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
| |
| | |
| 取消其他端口的访问规则
| |
| iptables -P INPUT DROP
| |
| iptables -P FORWARD DROP
| |
| iptables -P OUTPUT DROP
| |
| | |
| ##保持已有连接不中断 以redis 作例子
| |
| iptables -A INPUT -p tcp --dport 6379 -m state --state ESTABLISHED -j ACCEPT
| |
| iptables -A INPUT -s 192.168.1.12 -p TCP --dport 6379 -j ACCEPT
| |
| iptables -A INPUT -p TCP --dport 6379 -j DROP
| |
| | |
| #on zabbix client add the firewall rule , zabbix server 192.168.0.72
| |
| #iptables -I INPUT -s 192.168.0.72 -p tcp --dport 10050 -j ACCEPT
| |
| iptables -I INPUT -s 192.168.0.72 -p tcp -m tcp --dport 10050 -m comment --comment "zabbix agentd listen" -j ACCEPT
| |
| | |
| /etc/init.d/iptables save
| |
| | |
| iptables -A INPUT -p tcp -m tcp –dport 21 -j ACCEPT //开放21端口
| |
| A默认是插入到尾部的,可以-I来插入到指定位置
| |
| | |
| #这个平时一般为了删除而查看 哈哈
| |
| iptables -L -n --line-number |grep 21 # //--line-number可以显示规则序号,在删除的时候比较方
| |
| iptables -D INPUT 3 # //删除input的第3条规则
| |
| | |
| iptables -R INPUT 3 -j DROP # //将规则3改成DROP
| |
| | |
| | |
| | |
| iptables中数据包和4种被跟踪连接的4种不同状态:
| |
| | |
| NEW:该包想要开始一个连接(重新连接或将连接重定向)
| |
| RELATED:该包是属于某个已经建立的连接所建立的新连接。例如:FTP的数据传输连接就是控制连接所 RELATED出来的连接。--icmp-type 0 ( ping 应答) 就是--icmp-type 8 (ping 请求)所RELATED出来的。
| |
| ESTABLISHED :只要发送并接到应答,一个数据连接从NEW变为ESTABLISHED,而且该状态会继续匹配这个连接的后续数据包。
| |
| INVALID:数据包不能被识别属于哪个连接或没有任何状态比如内存溢出,收到不知属于哪个连接的ICMP错误信息,一般应该DROP这个状态的任何数据。
| |
| | |
| 数据包关卡
| |
| 到来自服务器外的其他用户访问这台服务器时,防火墙软件对数据包设置了多个关卡,根据数据处理的逻辑,在逻辑顺序的某些关键节点,防火墙对数据进行监控,根据规则对数据包进行放行、丢弃、拒绝、修改。而这些关卡就是被人称为“链”的逻辑位置。
| |
| | |
| | |
| | |
| 最后用DROP或REJECT把大门一关就可以了。
| |
| | |
| | |
| 实际上save那个命令的结果是把刚才新增的规则写入/etc/sysconfig/iptables这个文件
| |
| | |
| | |
| | |
| | |
| | |
| iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
| |
| | |
| 余下的任何TCP协议的数据包都将被拒绝,发送方会收到一条TCP重置消息(RST)。这是连接到一个没有listening服务的端口时,TCP协议的正常反应。
| |
| | |
| 这样之后,剩余的所有包都由策略来处理以及丢弃。我还应当添加一条规则来以类似的标准方法处理UDP:返回一个ICMP端口无法到达的消息。
| |
| | |
|
| |
| | |
| 以下是一个拦截特定IP地址的主机的规则:
| |
| | |
| iptables -A INPUT -i $IF_PUB -s 10.220.231.236 -j REJECT --reject-with icmp-host-prohibited
| |
| | |
| 这条规则拒绝那些到达公网接口的、来自IP地址10.220.231.236的传输。我是通过回应说主机被封锁了来拒绝这些传输,你也可以只是丢弃这些数据包,对问题主机不做回应。
| |
|
| |
| http://blog.163.com/fan_yishan/blog/static/4769221320117232225688
| |
|
| |
|
| |
| http://blog.163.com/fan_yishan/blog/static/47692213201172315746306/
| |
|
| |
| DROP:悄悄丢弃
| |
| 一般我们多用DROP来隐藏我们的身份,以及隐藏我们的链表
| |
|
| |
|
| |
| | |
| #REJECT
| |
| | |
| [root@dkm-server mon]# nmap 192.168.1.105
| |
| | |
| Starting Nmap 5.51 ( http://nmap.org ) at 2017-03-16 11:44 CST
| |
| Nmap scan report for 192.168.1.105
| |
| Host is up (0.00024s latency).
| |
| Not shown: 998 filtered ports
| |
| PORT STATE SERVICE
| |
| 80/tcp open http
| |
| 443/tcp closed https
| |
| MAC Address: 62:35:30:31:36:66 (Unknown)
| |
| | |
| Nmap done: 1 IP address (1 host up) scanned in 5.20 seconds
| |
| </pre>
| |
| | |
| ==参考==
| |
| | |
| [https://www.cnblogs.com/minseo/p/13588258.html 运维安全之Linux网络安全(iptables) ] | |
| | |
| http://blog.linuxchina.net/?p=2305
| |
| | |
| http://blog.51yip.com/linux/1404.html
| |
| | |
| iptables命令详解
| |
| | |
| http://www.liusuping.com/ubuntu-linux/iptables-firewall-setting.html
| |
| http://lym6520.iteye.com/blog/1931239
| |
| | |
| iptables入门:规则及路由基础
| |
| http://www.tangshuang.net/1806.html
| |
| | |
| [https://www.linuxprobe.com/chapter-08.html 第8章 Iptables与Firewalld防火墙。]
| |
| | |
| iptables防火墙原理详解
| |
| http://seanlook.com/2014/02/23/iptables-understand/
| |
| | |
| iptables入门:规则及路由基础
| |
| http://www.tangshuang.net/1806.html
| |
| | |
| [http://blog.chinaunix.net/uid-22780578-id-3346350.html iptables详解 ]
| |
| | |
| http://www.netfilter.org/documentation/index.html
| |
| | |
| iptables
| |
| https://zh.wikipedia.org/wiki/Iptables
| |
| | |
| linux 防火墙 iptables
| |
| http://blog.163.com/fan_yishan/blog/static/47692213201172315746306/
| |
| | |
| 防火墙
| |
| http://cn.linux.vbird.org/linux_server/0250simple_firewall_3.php
| |
| | |
| [https://www.lshell.com/2015/02/iptables_10.html 常用iptables配置 ]
| |
| | |
| [https://blog.csdn.net/Bilise/article/details/105372667 iptables和firewalld的介绍与区别]
| |
| | |
| [[category:Security]] | |