The use of enterprise-class firewall Detailed iptalbes

1.IPtables Introduction

Iptables (hereinafter referred to as Iptables) is unix / linux firewall that comes with an excellent and completely free open-source tool based packet filtering (for four or four layers of the OSI model the following filter), its function is very powerful, very flexible and can be very granular control of packet flow into and out of the server.

iptables其实并不是真正的防火墙,我们可以把他理解为一个客户端的代理,用户是通过iptables这个代理,将用户的安全设定执行到对应的“安全框架”中,这个“安全框架”才是真正的防火墙。这个框架叫做“netfilter”。

netfilter:内核空间,是真正实现防火墙的功能。
iptables:用户空间,在/sbin/iptables存在的防火墙,通过iptables提供管理,修改,删除或者插入规则。
用户和内核交互的一个工具就是iptables。

iptables workflow

1、防火墙是一层层过滤的。实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
2、如果匹配上了规则,即明确表明是阻止还是通过,此时数据包就不在向下匹配新规则了。
3、如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
4、防火墙的默认规则是对应链的所有的规则执行完以后才会执行的(最后执行的规则)。

2. four tables pentachain

By default, iptables contains three tables, filter, nat, mangle function definition and division tables, each table in turn contains different operating chains (chains). The actual iptables contains four tables and five chains, the main filter can remember.
Four tables

必须是小写
raw   ------------追踪数据包, ----此表用处较少,可以忽略不计
mangle   -------- 给数据包打标记,做标记
nat   ---------网络地址转换即来源与目的的IP地址和port的转换。
filter   --------做过滤的,防火墙里面用的最多的表。
#表的应用顺序:raw-》mangle-》nat-》filter

Five chain

五链:(必须是大写)链里面写的是规则。
PREROUTING  ---------------------进路由之前数据包
INPUT    -----------------就是过滤进来的数据包(输入)
FORWARD -----------------转发
OUTPUT  ---------------发出去的数据包
POSTROUTING    --------------路由之后数据包
#所有的访问都是按顺序:
入站:比如访问自身的web服务流量。先PREROUTING(是否改地址,只能改目标地址),经路由再INPUT(是否允许)到达程序。
转发:经过linux网关的流量.先PREROUTING(是否改地址),然后路由。转发给FORWARD(转发或者丢弃),最后经过POSTROUTING(看看改不改地址,原地址和目标地址。)
出站:源自linux自身的流量.先OUTPUT,再给POSTROUTING(是否改IP)。
#规则顺序:逐条匹配,匹配即停止。

Four tables pentachain

raw表里面:
PREROUTING
OUTPUT
总结:数据包跟踪  内核模块iptables_raw
===============================================
mangel表里面有5个链:
PREROUTING  
INPUT    
FORWARD 
OUTPUT 
POSTROUTING
路由标记用的表。内核模块iptables_mangle
=====================================================
nat表里面的链:
PREROUTING
INPUT
OUTPUT
POSTROUTING
转换地址的表(改IP,改端口。当网关使用的linux。保护内外网流量。内核模块叫iptable_nat)
==========================================
filter表有三个链:重点
INPUT    #负责过滤所有目标是本机地址的数据包通俗来说:就是过滤进入主机的数据包
FORWARD  #负责转发经过主机的数据包。起到转发的作用
OUTPUT   #处理所有源地址是本机地址的数据包通俗的讲:就是处理从主机发出的数据包
总结:根据规则来处理数据包,如转或者丢。就是实现主机型防火墙的主要表。
内核模块 iptable_filter

3.iptables arguments detailed

1. Install iptanles
centos7
[root@iptables-server ~]# yum install -y iptables iptables-services  #安装iptables
[root@iptables-server ~]# systemctl stop firewalld    #关闭防火墙
[root@iptables-server ~]# systemctl  disable firewalld   #永久关闭
[root@iptables-server ~]# systemctl start iptables     #启动iptables
查看版本:
[root@iptables-server ~]# iptables -V 
iptables v1.4.21
如果你是centos(5/6) ,iptanles的启动方式用这个命令
#/etc/init.d/iptables start
2. Common parameters
-L:列出一个链或所有链中的规则信息
-n:以数字形式显示地址、端口等信息
-v:以更详细的方式显示规则信息
--line-numbers:查看规则时,显示规则的序号(方便之处,通过需要删除规则-D INPUT 1
-F:清空所有的规则(-X是清理自定义的链,用的少;-Z清零规则序号)
-D:删除链内指定序号(或内容)的一条规则
-R:修改规则
-P:为指定的链设置默认规则
-A:在链的末尾追加一条规则
-I:在链的开头(或指定序号)插入一条规则
-t: 指定表名
.... 更多参数可通过--help查看
3. Examples of particular use
-L:列出一个链或所有链中的规则信息
[root@iptables-server ~]# iptables -L  默认查看所有链规则
-t: 指定表名  但是如果不加-t参数,默认使用的是filter表
[root@iptables-server ~]# iptables -t nat -L   #列出nat表中的规则
[root@iptables-server ~]# iptables -nL  #以数字的形式显示ip和端口与协议
[root@iptables-server ~]# iptables -nL --line  #显示规则行号
清空规则:
#iptables  -F 
清空单独的某一个链里面的规则
#iptables  -F  链名
清空单独的某一个表里的,某一个链里面的规则
# iptables -t nat -F 链名
保存规则:
[root@iptables-server ~]# service iptables save
或者
[root@iptables-server ~]# iptables-save > /etc/sysconfig/iptables
不保存的话只是临时起作用,重启会失效

The general syntax of iptables rules

iptables -t 表名 动作  [链名] [-p 匹配条件] [-j 控制类型]
-j:控制类型, 通过前面匹配到之后是丢弃还是保留数据包的处理方式: 
ACCEPT允许,
REJECT拒绝,
DROP丢弃。 不会给用户返回任何的拒绝消息,不推荐使用。
LOG写日志(log不适用匹配,只是记录一下)
======================================================
动作:添规则还是删除规则
-p:匹配条件:数据包特征ip,端口等
如果不写-t 默认使用filter表
=======================================================
动作
修改默认规则: -P (大p)
删除规则:-D
修改规则:-R
追加规则: -A  默认追加到链的末尾
插入规则:-I (大i),在链的开头(或指定序号)插入一条规则

For example

iptables -t filter -A INPUT -p tcp -j ACCEPT    #在filter表INPUT链的最后一行插入允许tcp的规则
iptables -I INPUT -p udp -j ACCEPT   #在filter表INPUT链的第一行插入允许udp的规则
iptables -I INPUT 4 -p icmp -j ACCEPT   #在filter表INPUT链的第四行插入允许icmp的规则
iptables -D INPUT 3  #删除filter表INPUT链的第三行规则

Case
protocol :-p (smaller the p-)
tcp - the most widely used
udp
ICMP -ping when using the protocol
# using the protocol when you can not specify the port and port must be specified when the protocol.

1.禁止自己被别人ping
[root@iptables-server ~]# iptables -A INPUT -p icmp -j REJECT
我们去另一台上面测试
[root@iptables-test ~]# ping 192.168.13.139
PING 192.168.13.139 (192.168.13.139) 56(84) bytes of data.
From 192.168.13.139 icmp_seq=1 Destination Port Unreachable
2.拒绝test(192.168.13.140)这台机器通过ssh连接到本服务器
端口:
--sport    ---源端口
--dport    --目标端口
-s : 指定ip地址
[root@iptables-server ~]# iptables -I INPUT -s 192.168.13.140 -p tcp --dport 22 -j REJECT
验证:
[root@iptables-test ~]# ssh [email protected]
ssh: connect to host 192.168.13.139 port 22: Connection refused
[root@iptables-server ~]# iptables -I INPUT -s 192.168.13.140 -p tcp --dport 22:80 -j REJECT 
#端口的范围: 拒绝192.168.13.140这台机器通过22端口到80端口的访问,包括22和80端口在内
验证:
[root@iptables-test ~]# curl -I http://192.168.13.139
curl: (7) Failed connect to 192.168.13.139:80; Connection refused
-s后面可以跟多个ip地址,比如-s 192.168.13.140,192.168.13.141  中间用逗号隔开
如果不指定-s ip地址 ,那就是对所有的机器都生效
3.禁止ping策略原则
iptables服务器是ping命令发起者或是接受者
-i --in-interface:在INPUT链配置规则中,指定从哪一个网卡接口进入的流量(只能配置在INPUT链上)
-o --out-interface:在OUTPUT链配置规则中,指定从哪一个网卡接口出去的流量(只能配置在OUTPUT链上)
====================================================
icmp的类型:
0: Echo Reply——回显应答(Ping应答)ping的结果返回。
8: Echo request——回显请求(Ping请求),发出去的请求。
=====================================================
iptables服务器-----发起者:ping 别的机器
1.自己不能ping别人,但是别人可以ping自己:
[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 8 -j REJECT  #ping发出的请求禁止掉了
验证:
[root@iptables-server ~]# ping 192.168.13.140  #将ping请求给禁止掉了。
PING 192.168.13.140 (192.168.13.140) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
[root@jenkins-server ~]# ping 192.168.13.139   #可以ping通
PING 192.168.13.139 (192.168.13.139) 56(84) bytes of data.
64 bytes from 192.168.13.139: icmp_seq=1 ttl=64 time=0.280 ms
=========================================================================================
iptables服务器作为接受者。也就是别人ping自己:
本机可以ping其他机器。其他机器不能ping通本机:
第一种方法:
[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 8 -j ACCEPT #允许自己ping别人
[root@iptables-server ~]# iptables -A INPUT -i ens33 -p icmp  --icmp-type 8 -j REJECT  #将进来的ping请求给丢弃了。
换一种方法:
[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 0 -j REJECT #不给回应icmp包
验证:
[root@iptables-server ~]# ping 192.168.13.140   #ping其他机器通
PING 192.168.13.140 (192.168.13.140) 56(84) bytes of data.
64 bytes from 192.168.13.140: icmp_seq=1 ttl=64 time=0.491 ms
[root@iptables-test ~]# ping 192.168.13.139    #其他机器ping不同
PING 192.168.13.139 (192.168.13.139) 56(84) bytes of data.
=========================================================================================
拒绝任何ping的协议:
[root@iptables-server ~]# iptables -A INPUT -p icmp -j DROP
4. Extended Match

Display Match: The port match, IP range, MAC address, and other special matching

#iptables -m iprange   --help
1.指定ip范围:
语法: -m iprange --src-range
# iptables -I INPUT -p tcp --dport 80 -m iprange --src-range 192.168.13.120-192.168.13.150 -j REJECT
2.指定多端口范围:一次拒绝多个指定端口
语法:
-m multiport --sports   #源端口
-m multiport --dports   #目的端口
# iptables -A INPUT -p tcp -m  multiport --dports 22,80 -s 192.168.13.140 -j REJECT
验证:在13.140机器上
# ssh [email protected]  #不通
ssh: connect to host 192.168.13.139 port 22: Connection refused
3.MAC地址匹配
拒绝MAC地址的匹配:只能匹配源MAC地址
语法: -m mac --mac-source
# iptables -I INPUT -p icmp -m mac --mac-source 0:0c:29:cd:26:77 -j REJECT #拒绝指定的MAC地址服务通过icmp协议请求到本地
# iptables -I INPUT -m mac --mac-source 00:0C:29:64:E3:8D -j REJECT  #将指定的MAC地址服务请求全部禁止了
# iptables -I INPUT -i ens33 -j DROP  #通过网卡接口,谁也连不上了.
Published 44 original articles · won 127 Like · views 20000 +

Guess you like

Origin blog.csdn.net/baidu_38803985/article/details/105315893