iptables的
一、安装iptables

二、启动iptables

三、关闭iptables

四、iptables 的格式
1 格式: iptables -t 表名 选项 链接名称 条件 动作
| 参数意义 |
|
|
动作作用
|
|
|
|
|
|||
| -t | 指定操作的表 | TCP(http) | ACCEPT | 将数据包放行,进行完此处理动作后,将不再比对其它规则,直接跳往下一个规则链。 |
源地址
|
发送请求的地址 | 源端口 :发送请求的端 | 目标端口:访问的端口 |
||
| -L | 列出当前的规则 | UDP | REJECT | 拦阻该数据包,并传送数据包通知对方。 | 目标地址 | 访问的地址 | ||||
| -v | 显示数据包和数据包大小 | ICMP(ping) | DROP | 丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。 | ||||||
| -n | 不反解地址 |
ALL
|
REDIRECT
|
将包重新导向到另一个端口,进行完此处理动作后,将会继续比对其它规则。 | ||||||
| -A, --append | 追加一条规则到链中 |
|
||||||||
| -I, --insert | 插入一条规则,插入到顶部 | -i | 进来的网卡 | |||||||
| -F, --flush | 清空 |
-o
|
出去的网卡 | |||||||
| -Z, --zero | 清空计数器 |
-m
|
指定模块 | |||||||
| -D, --delete | 删除链中的规则 | -j | 转发动作 | |||||||
-S, --list-rules
|
列出所有的规则 | -p | 指定协议 | |||||||
| -N, --new-chain | 创建一个自定义的链 | |||||||||
| -X, --delete-chain | 删除一个自定义的链 | |||||||||
| mo-P, --policy | 指定链的默认策略 |
|
案例
拓展iptables的功能的。 -m : 指定模块 1、连续匹配多个端口(multiport) --dports : 指定多个-端口(不同端口之间以逗号分割,连续的端口使用冒号分割)。 2、指定一段连续的ip地址范围(iprange) --src-range from[-to]: 源地址范围 --dst-range from[-to] 目标地址范围 3、匹配指定字符串(string) --string pattern # 指定要匹配的字符串 --algo {bm|kmp} # 匹配的查询算法 4、根据时间段匹配报文(time) --timestart hh:mm[:ss] # 开始时间 --timestop hh:mm[:ss] # 结束时间 --monthdays day[,day...] # 指定一个月的某一天 --weekdays day[,day...] # 指定周 还是 周天 5、禁ping, 默认本机无法ping别人 、别人无法ping自己 --icmp-type {type[/code]|typename} echo-request (8) 请求 echo-reply (0) 回应 6、限制链接数,并发连接数(connlimit) --connlimit-upto n # 如果现有连接数小于或等于 n 则 匹配 --connlimit-above n # 如果现有连接数大于n 则匹配 7、针对 报文速率 进行限制。 秒、分钟、小时、天。 --limit rate[/second|/minute|/hour|/day] # 报文数量 --limit-burst number # 报文数量(默认:5)
五、模块
拓展iptablesde 拓展功能
1 拓展iptables的功能的。 2 3 -m : 指定模块 4 5 1、连续匹配多个端口(multiport) 6 7 --dports : 指定多个-端口(不同端口之间以逗号分割,连续的端口使用冒号分割)。 8 9 2、指定一段连续的ip地址范围(iprange) 10 --src-range from[-to]: 源地址范围 11 --dst-range from[-to] 目标地址范围 12 13 3、匹配指定字符串(string) 14 --string pattern # 指定要匹配的字符串 15 --algo {bm|kmp} # 匹配的查询算法 16 17 4、根据时间段匹配报文(time) 18 --timestart hh:mm[:ss] # 开始时间 19 --timestop hh:mm[:ss] # 结束时间 20 --monthdays day[,day...] # 指定一个月的某一天 21 --weekdays day[,day...] # 指定周 还是 周天 22 23 5、禁ping, 默认本机无法ping别人 、别人无法ping自己 24 --icmp-type {type[/code]|typename} 25 echo-request (8) 请求 26 echo-reply (0) 回应 27 28 6、限制链接数,并发连接数(connlimit) 29 --connlimit-upto n # 如果现有连接数小于或等于 n 则 匹配 30 --connlimit-above n # 如果现有连接数大于n 则匹配 31 32 7、针对 报文速率 进行限制。 秒、分钟、小时、天。 33 34 --limit rate[/second|/minute|/hour|/day] # 报文数量 35 --limit-burst number # 报文数量(默认:5)
案例
1、要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝 iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT iptables -f filter -A INPUT -p TCP -j DROP 2、要求访问数据包中包含HelloWorld的数据不允许通过。 iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP 3、要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝 iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT iptables -f filter -A INPUT -p TCP -j DROP 4、要求每天的12到13之间,不允许访问 iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DROP 必须使用UTC时间 5、要求别人不能ping本机,但是本机可以ping别人 iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP 6、要求主机连接最多有2个 iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP 7、要求限制速率在500k/s左右 iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT iptables -t filter -A INPUT -p TCP -j DROP

浙公网安备 33010602011771号