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

相关推荐

  • 第十八周博客作业

    1、为LNMP架构添加memcached支持,并完成对缓存效果的测试报告; 操作系统: CentOS 7.210.0.0.51 nginx+php+mysql10.0.0.52 memcached 一. 环境准备: 搭建LNMP编译安装环境 1. 配置163的yum源和阿里云的epel源 [root@localhost ~]# mv /etc/yum.rep…

    2017-07-12
  • CentOS7 Local yum的一次报错信息

    说明:今天在火车上测试一个CentOS7下的一个服务,要用到yum配置,但是本机没有联网,所以考虑到配置本地yum,按照之前CentOS6下的常规方法,居然发现有报错。 操作如下: 1)虚拟机下将CentOS7光盘加载到系统里面,然后挂载到本地的/localyum上 [root@localhost yum.repos.d]# mount&…

    Linux干货 2016-07-16
  • 用户管理命令之NBA版

    用户管理命令之NBA版 本文纯属扯淡,如有不严谨之处,还请海涵。 1 groupadd 作用:创建一个属组 格式:groupadd [选项] 属组名 选项: -:选项为空 创建新属组 -g:指定组GID,默认是GID+1 实例 1.1 添加属组team,指定组ID为1111      groupadd -g …

    Linux干货 2017-03-27
  • 配置LAMP实现WordPress

    配置LAMP实现WordPress 在同一台主机上实现LAMP(Linux + Apache + MariaDB + PHP) CentOS 7.3、Apache 2.4.6、MariaDB 5.5.52、PHP 5.4.16 1 安装LAMP 采用yum方式进行安装httpd、MariaDB、php、php-mysql,php-mysql用来进行php和M…

    2017-06-06
  • 系统自动化安装、selinux

    系统自动化安装 系统启动流程:bootloader–>kernel(initramfs)–>rootfs–>anaconda–>/sbin/init anaconda: 系统安装程序    tui: 基于图形库curses的文本配置窗口 &nbsp…

    Linux干货 2016-09-22
  • 搭建缓存功能的WEB服务集群

    搭建缓存功能的WEB服务集群 实验简介 本文主要介绍双主模型的nginx proxy高可用集群的搭建方式。实验环境: 使用nfs服务器提供页面数据共享 使用单独的mariadb服务器提供关系型数据库 使用两台httpd服务器处理动态的php和静态页面资源 使用两台nginx服务器处理图片资源 使用两台varnish服务器作缓存处理 使用两台nginx作代理 …

    Linux干货 2017-07-15