iptables基础实战练习

目录:

一、基本规则练习

二、SNAT源地址转移

三、DNAT目标地址转移

 


一、基础规则练习


 

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

1 iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
2 iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

 

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

1 iptables -P INPUT DROP
2 iptables -P OUTPUT DROP
3 iptables -P FORWARD DROP

 

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

1 iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT
2 iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT

 

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

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

 

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

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

 

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

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

 

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

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
2 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) 源地址,目的地址范围匹配

1 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
2 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”字符的页面出来

1 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端口

1 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端口

1 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个并发连接(禁止)

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

 

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

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

 

(15)目标地址和端口转换示例(对22端口的转换)

1 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 22022 -j DNAT --to-destination 192.168.2.4:22

 

 


 二、SNAT源地址转移



 iptables基础实战练习

SNAT:源地址转换。内网主机在访问互联网的时候所有源地址都转换为防火墙的外网地址,起到隐藏内网客户机的目的。同时,也解决了IPV4公网地址不够用的需求。

1 iptables -t nat -A POSTROUTING -s 10.1.249.158 -j SNAT --to-source 192.168.2.3 

 


 三、DNAT目标地址转移


 

 iptables基础实战练习

 

DNAT:目的地址转换。当外网主机访问内网的某台服务器的时候,如果直接暴露服务器的IP于公网,可能会遭受各种各样的攻击,而DNAT的主要作用就是在服务器前面添加一台防火墙。将防火墙的地址公布出去,让外网客户端通过访问防火墙的地址就可以访问到本地服务器。这样就起到了保护服务器的目的;

1 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4

 

 

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

(0)
z longz long
上一篇 2017-08-04 11:08
下一篇 2017-08-04 11:42

相关推荐

  • LVS入门

    随着互联网进入寻常百姓家,网络流量愈来愈大,大规模的网路访问如果都使用一个服务器提供服务,那么网络通讯注定会拥堵不堪。为了解决这些问题,达到使网络流量均衡地分散到各个服务器上的目的,一些技术大牛发明了LVS负载均衡技术。   LVS简介   LVS(Linux Virtual Server)即为Linux虚拟服务器,使由章文嵩博士主导开发…

    Linux干货 2017-05-09
  • shell脚本基础练习2

    1.编写一个脚本/root/bin/createuser.sh,脚本的执行语法必须是:createuser.sh -username -m password,选项与参数间可支持多空格,但不能顺序颠倒。当未指定正确的选项或参数时,以错误输出方式提示“createuser.sh -u username -m password ”后退出脚本。用…

    2017-08-12
  • 堡垒机-麒麟开源堡垒机苹果 Mac支持版本发布

      近日,麒麟开源堡垒机团队开发测试了支持Mac OS苹果操作系统的Web插件,苹果系统用户可以直接和Windows用户一样,登录到Web平台,使用点击的方式调动运维工具并且登录到目标系统进行操作运维。 Mac OS插件支持ssh、telnet、rdp、vnc、x11、sftp、ftp、应用发布等所有协议。   注:麒…

    Linux干货 2016-05-19
  • linux 文本处理工具 grep cut sort等

    linux day 7 间歇性回忆 自动属于这个组 是  SGID 的功能 chmod g+s /data/testdir setfacl —m g:g2:rwx /data/testdir setfacl -m b:g:g2:rwx /data/testdir setfacl -m d:g:g3:r testdir chmod o= testdi…

    Linux干货 2016-08-08
  • 1017作业

    1 生产环境发现一台服务器系统时间产生偏差,造成服务异常,请帮忙校正 ##先分析硬件时间不对还是系统时间不对,如果是系统时间不对: [root@localhost ~]# hwclock -w [root@localhost ~]#  ##如果是硬件时间不对: [root@localhost ~]#…

    Linux干货 2016-10-18
  • N26第五周博客作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;grep “^[[:space:]]+” /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;grep “^#[[:space:…

    Linux干货 2017-03-06