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

相关推荐

  • cut,sort,head,tail,uniq基本使用

    1.列出当前系统上所有已经登录的用户的用户名,同一个用户登录多次,则显示一次[root@localhost ~]# who|cut -d” ” -f1|uniqgordenroot2.取出最后登录到当前系统的用户的相关信息。[root@localhost ~]# last|head -1|cut -d’ ‘ -…

    Linux干货 2017-09-04
  • Redis基于Sentinel哨兵高可用方案

    下载最新redis版本,当前最新版本为  2.8.19  2014-12-30 安装redis 首页地址:http://redis.io/ 最新稳定版下载地址: http://download.redis.io/releases/redis-2.8.19.tar.gz # tar -xvf redis-2.8.19.tar.gz # cd…

    2015-03-03
  • vim编辑器

    vim编辑器: 文本编辑器:文本,纯文本,ASCII text;Unicode; 文本编辑种类:                 行编辑器:sed                全屏编辑器:…

    2017-04-17
  • 网络分层模型(OSI,TCP/IP)

    目前存在的两种网络分层模型:OSI模型和TCP/IP模型。 OSI模型一共分为七层 TCP/IP模型和OSI模型类似,但是只分为四层。 OSI模型 OSI的全程是Open Systems Interconncection,即开放系统互联,它由ISO(International Organization for Standardization)制定。 OSI是…

    2017-11-27
  • Linux基础知识之文本查找和正则表达式扩展正则表达式

    1.什么是正则表达式?      正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的复制,让用户可以轻易达到查找、删除、替换某些特定字符串的处理程序。      正则表达式基本上是一种“表示法”,只要工具程序支持这种表示法,那么该工作程序就可以用来作为…

    Linux干货 2016-08-10
  • grep,egrep及相应的正则表达式用法详解

    目录 1、grep简介     1.1、grep是什么     1.2、grep使用格式     1.3、grep常用选项 2、正则表达式与grep     2.1、正则表达式简介  &nb…

    系统运维 2016-08-22