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)
sraybansrayban
上一篇 2017-06-12
下一篇 2017-06-13

相关推荐

  • Linux进程与作业1

    Linux进程与作业 操作系统的组成部分:硬件,kernel,lib, 系统调用–>调用kernel lib调用–> 库调用,kernel调用 运行用户代码–>用户模式–>用户空间; 运行kernel–>内核模式–>内核空间; 内核的功用:进程管理,文件系统,网络功能,内存管理,驱动程序,安全功能 C…

    Linux干货 2016-08-02
  • bash中的变量

        对任何一门编程语言来说,最基础的部分就是变量。那什么事变量呢?变量就是把一个已知的可以变动的值,赋给一个固定名字的,用固定的名字代表这个可变动的值。在bash中也不例外,跟大多数编程语言一样,它也有一些自己的语法和规则 bash变量:     1.规则设置:变量和变量的值中间用=连接,=的两…

    Linux干货 2016-08-15
  • shell脚本的一点补充

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

    Linux干货 2016-08-24
  • 位置变量&特殊变量总结

    位置变量 常用的位置变量有 $1, $2, $3 ……,表示命令行传给脚本的第一个参数,第二个参数,第三个参数。。。 $0 表示脚本的文件名,比如a.sh 位置变量在脚本中的主要作用,是让脚本通过他们来获取命令行传递给脚本的参数。 变量位置调整 shift [n] 用于调整变量位置 第n+1个位置变量会被重新命名为$1…

    Linux干货 2016-08-15
  • bash脚本进阶

    case  变量引用  in PAT1)分支1 ;; PAT2)  分支2 ;; … *) 分支n ;; esac case支持glob风格的通配符:   *:任意长度任意字符: ?:任意单个字符: [ ]:指定范围内的任意单个字符: a|b:a或b function:函数   &nbs…

    Linux干货 2017-05-21
  • 文件管理命令及变量基础

    文件管理工具:cp,    mv,     rm       cp命令:copy 源文件;目标文件 单源复制: cp [OPTION]….[-T]  SOURCE  DEST 多源复制:cp [OPTION] ….SOURCE…DIR…

    Linux干货 2016-12-21