iptables练习

iptables练习


一、COMMAND

1、列出所有链的规则:iptables -L ,显示某条链的规则就是iptables -L INPUT

iptables练习

详细信息:iptables -vnL

iptables练习

2、清楚所有链的规则 :iptables -F

3、设置默认规则策略:iptables -P INPUT DROP,iptables -P OUTPUT DROP , iptables -P FORWARD DROP(拒绝所有数据包)

iptables练习

在虚拟机上改成:iptables -P INPUT ACCEPT ,远程连接才可用。

4、添加规则,在INPUT链上添加规则,协议是tcp,目标端口号是21:iptables -A INPUT -p tcp –dport 21

iptables练习

5、插入规则,在INPUT链上插入规则,协议是tcp,目标端口号是23,规则号是1:iptables -I INPUT 1 -p tcp –dport 23

iptables练习

6、替换规则,在INPUT链上替换规则号1的iptables规则,将目标端口号更改为24:iptables -R INPUT 1 -p tcp –dport 24

iptables练习

7、删除规则,在INPUT 链上删除规则号是1的iptables规则

iptables练习

二、match:基本规则匹配器

1、指定协议:iptables -A INPUT -p tcp -j ACCEPT

iptables练习

2、指定ICMP类型:iptables -A INPUT -p icmp –icmp-type echo-request -j ACCEPT

iptables练习

3、指定ip地址:iptables -A INPUT -s 192.168.1.109 -j ACCEPT

iptables练习

4、指定接口:iptables -A FORWARD -o eno16777736 -j ACCEPT

iptables练习

5、指定端口号:iptables -A INPUT -p tcp –sport 80 -j ACCEPT

三、match:扩展规则匹配器

1、limit :限制速率。iptables -I INPUT -d 192.168.1.109 -p icmp –icmp-type 8 -m limit –limit 3/minute –limit-burst 5 -j ACCEPT

iptables练习

2、iprange :一整段连续的ip都可以:iptables -A INPUT -d 172.16.100.67 -p tcp –dport 80 -m iprange –src-range 172.16.100.5-172.16.100.10 -j DROP

iptables练习

3、time :指定某个时间范围内可以。

iptables练习

4、multiport :多个端口

iptables练习

5、string :对报文中的字符串做匹配检查,一些敏感词汇。

iptables练习

6、state:根据”连接追踪机制“去检查连接的状态。

~]# iptables -A INPUT -d 172.16.100.67 -p tcp -m multiport --dports 22,80 -m state --state NEW,ESTABLISHED -j ACCEPT
~]# iptables -A OUTPUT -s 172.16.100.67 -p tcp -m multiport --sports 22,80 -m state --state ESTABLISHED -j ACCEPT

练习:INPUT 和 OUTPUT 默认策略为 DROP;

iptables -P INPUT DROP
iptables -P OUTPUT DROP

1、限制本地主机的 web 服务器在周一不允许访问;新请求的速率不能超过 100 个每秒;web 服务器包含了 admin 字符串的页面不允许访问;web 服务器仅允许响应报文离开本机;

周一不允许访问

#iptables -A INPUT -p tcp --dport 80 -m time ! --weekdays Mon -j ACCEPT
#iptables -A OUTPUT -p tcp --dport 80 -m state --state ESTABLISHED -j ACCEPT

新请求速率不能超过100个每秒

# iptables -A INPUT -p tcp --dport 80 -m limit --limit 100/s

web包含admin字符串的页面不允许访问,源端口:dport

# iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string 'admin' -j REJECT

web服务器仅允许响应报文离开主机,放行端口(目标端口):sport

# iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

2、在工作时间,即周一到周五的 8:30-18:00,开放本机的 ftp 服务给 172.16.0.0 网络中的主机访问;数据下载请求的次数每分钟不得超过 5 个;

# iptables -A INPUT -p tcp --dport 21 -s 172.16.0.0 -m time ! --weekdays 6,7  -m time --timestart 8:30 --timestop 18:00  -m connlimit --connlimit-above 5 -j ACCEPT

3、开放本机的 ssh 服务给 172.16.x.1-172.16.x.100 中的主机,x 为你的学号,新请求建立的速率一分钟不得超过 2 个;仅允许响应报文通过其服务端口离开本机;

# iptables -A INPUT -p tcp --dport 22 -m iprange --src-range 172.16.0.1-172.16.0.100 -m limit --limit 2/m
# iptables -A OUTPUT -p tcp --sport 22 -m iprange --dst-range 172.16.0.1-172.16.0.100 -m state --state ESTABLISHED -j ACCEPT

4、拒绝 TCP 标志位全部为 1 及全部为 0 的报文访问本机;

# iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

5、允许本机 ping 别的主机;但不开放别的主机 ping 本机;

# iptables -A INPUT  -p icmp --icmp-type 0 -j ACCEPT
# iptables -A OUTPUT  -p icmp --icmp-type 8 -j ACCEPT

原创文章,作者:N24_yezi,如若转载,请注明出处:http://www.178linux.com/64323

(1)
N24_yeziN24_yezi
上一篇 2016-12-20 20:35
下一篇 2016-12-20 22:15

相关推荐

  • Tomcat详解

    Tomcat详解 1、tomcat安装 tomcat的组件:<Server> <Service> <connector/> <connector/> <Engine> <Host /> <Host> <Context/> … </Host> &lt…

    2017-04-27
  • sed&awk

    Evernote Export 1、总结sed和awk的详细用法; awk Linux文本处理工具三剑客:grep、sed和awk。其中grep是一种文本过滤工具,sed是文本编辑器,而awk是一种报表生成器,就是对文件进行格式化处理的,这里的格式化不是文件系统的格式化,而是对文件内容进行各种“排版”,进而格式化显示。 在Linux之上我们使用的是GNU a…

    Linux干货 2017-01-05
  • linux基础学习第十二天(shell函数、数组以及yum使用及编译安装)

    2016-08-18 授课内容: shell编程基础(select 循环与菜单、函数) yum管理、定制yum仓库 select 循环与菜单         select variable in list       &…

    Linux干货 2016-08-21
  • LVM逻辑卷管理

    LVM逻辑卷管理 LVM全称为Logical VolumeManager逻辑卷管理器,LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性。 其中 PV:Physical Volume 物理卷 VG:volume Group 卷组 LV:Logincal Volume 逻辑卷 实验…

    2017-08-14
  • N25第八周学习总结_sed命令详解

    sed使用详解 大纲: 1、    sed是什么 2、    为什么要使用sed 3、    sed使用方法 4、    sed详解   正题: 1、    sed是什么 sed全名Stream Ed…

    2017-02-26