iptables实战笔记一

iptables实战

1.开启防火墙

systemctl start firewalld

2.清空所有的默认规则,我们自己定义自己的规则

iptables -F

查看此时的iptables
iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD_IN_ZONES (0 references)
target     prot opt source               destination         

Chain FORWARD_IN_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain FORWARD_OUT_ZONES (0 references)
target     prot opt source               destination         

Chain FORWARD_OUT_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain FORWARD_direct (0 references)
target     prot opt source               destination         

Chain FWDI_public (0 references)
target     prot opt source               destination         

Chain FWDI_public_allow (0 references)
target     prot opt source               destination         

Chain FWDI_public_deny (0 references)
target     prot opt source               destination         

Chain FWDI_public_log (0 references)
target     prot opt source               destination         

Chain FWDO_public (0 references)
target     prot opt source               destination         

Chain FWDO_public_allow (0 references)
target     prot opt source               destination         

Chain FWDO_public_deny (0 references)
target     prot opt source               destination         

Chain FWDO_public_log (0 references)
target     prot opt source               destination         

Chain INPUT_ZONES (0 references)
target     prot opt source               destination         

Chain INPUT_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain INPUT_direct (0 references)
target     prot opt source               destination         

Chain IN_public (0 references)
target     prot opt source               destination         

Chain IN_public_allow (0 references)
target     prot opt source               destination         

Chain IN_public_deny (0 references)
target     prot opt source               destination         

Chain IN_public_log (0 references)
target     prot opt source               destination         

Chain OUTPUT_direct (0 references)
target     prot opt source               destination

3.我们准备建立自己的规则

(1) 放行ssh (端口:22)

iptables -t filter -A INPUT  -s 0/0  -d 192.168.42.153  -p tcp --dport 22 -j ACCEPT 或者
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT  -s 192.168.42.153  -d 0/0   -p tcp   --sport 22  -j ACCEPT 或者
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

(2)修改默认规则链(关闭所有端口)

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

(3)放行web(80)端口 httpd nginx

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 或者
iptables -t filter -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT 或者
iptables -t filter -I OUTPUT -s 192.168.42.153  -d 0/0 -p tcp --sport 80 -j ACCEPT

(4)修改默认规则链后,我们发现ping不通自己,也ping不通别的主机

iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 
iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT

(5)允许自己ping别的主机

iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT

(6)允许任何人来ping本机

iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT

(7)同时开发多个端口(多端口匹配)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT

(8)iptables -vnL –line-numbers #显示数字

iptables  -vnL INPUT  --line-numbers 
Chain INPUT (policy DROP 1 packets, 229 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 8
2       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 0
3       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           
4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:80
5     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22

(9) 源地址,目的地址范围匹配

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT

(10)禁止包含”old”字符的页面出来

iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP

(11)基于时间限定,9点到19点,禁止访问80端口

iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP

(12)周一到周五9点到19点禁止访问80端口

iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP

(13)端口大于2个并发连接(禁止)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP

(14)端口同一个客户端小于3个并发连接

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

4.新建自定义链 ,开放80

iptables -F
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -N webinput
iptables -N weboutput
iptables -I webinput -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 
iptables -I weboutput -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp -j webinput 
iptables -A OUTPUT -p tcp -j weboutput

“`

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

(0)
上一篇 2017-06-12 21:15
下一篇 2017-06-13 11:34

相关推荐

  • Linux三剑客之sed

    sed 简介 sed 工作原理 命令格式 常用选项: 地址定界: 编辑命令: 替换标记: sed元字符集(正则表达式) 高级编辑命令: sed用法实例 作业: 选定行的范围:,(逗号) 删除操作:d命令 显示模式空间内容 追加(行下):a\命令 插入(行上):i\命令 退出:q命令 多点编辑:e命令 从文件读入:r命令 写入文件:w命令 替换操作:s命令 替…

    Linux干货 2016-08-11
  • shell脚本终结篇——数组

    数组定义: 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引: 索引:编号从0开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅仅是数值格式,自定义格式的数组即为关联数组,bash4.0版本之后开始支持 bash的数组支持稀疏格式(索引不连续) 声明数组: declare -a ARRAY_NAM…

    Linux干货 2016-08-24
  • 第十周:Linux系统启动流程及shell脚本练习

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) 一张图足以说明一切: 2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;   (1) 为硬盘新建两个主分区;并为其安装grub;   (2) 为硬盘的第一个主分区提供内核和ramdisk文件;为第二个分区提供rootfs;   (3)…

    Linux干货 2016-11-30
  • Linux基础小模块

    1.基础知识小块:shell 由shell程序的自带命令:即为内置命令(builtin) 独立的可执行程序文件、文件名 :即为外部命令  [root@localhost~]#ls      ~用户当前所在目录  #:命令提示符(管理员帐号root)  $:普通用户   &nbsp…

    Linux干货 2016-08-05
  • lvs初探

    1,lvs是什么 lvs是Linux Virtual Server缩写,linux虚拟服务器,担任负载调度器(load balance)的角色,它不提供任何服务,用户请求到这里的时候,它是将客户需求转发至后端真正提供服务的服务,LVS分为两部分组成,ipvsadm管理集群服务的命令行工具,ipvs,是内核模块。由于ipvs采用基于ip负载均衡技术,所以具有很…

    Linux干货 2016-10-30
  • LVM逻辑卷&RAID管理&磁盘配置详解

    LVM逻辑卷管理 应用场景:想象一下,你通过传统的硬盘分区方式为某些用户提供了一块磁盘,随着用户数据的不断增加,分区的容量告急。这时要增加容量,只能选择用另一块容量更大的硬盘或分区来替换,在这个过程中,你需要将数据先拷贝至新设备,拷贝完成之后,再用新设备替换容量告罄的设备。假设你很有耐心并且整个数据的拷贝过程是顺顺利利的,你也需要考虑靠背后文件的权限变化、替…

    Linux干货 2016-09-02