1.什么是防火墙,防火墙的种类有哪些

防火墙: 古人用来隔绝失火后的高墙,阻挡外来的火势,起到了隔离的手段。
在互联网上,我们会采用类似于防火墙的方法,来保护我们的网络不受外来攻击,为此我们需要设定一些防火墙的规则,确定哪些数据允许通过,哪些不能通过,具有这种功能的设备或软件,我们将其称为防火墙

防火墙的种类:
  逻辑层面:
    主机防火墙:针对单个主机进行防护,例如Windows防火墙   一般是软件
    网络防火墙:处于网路入口的边缘,针对网络入口进行防护  一般是硬件,也可以用软件
  物理层面:
    硬件防火墙:一个实体的硬件,嵌入软件,实现防火墙的功能,性能高,成本高
    软件防火墙:通过软件的方式,模拟防火墙的功能,运行在操作系统上面,性能低,成本低

2.iptables介绍

1.什么是iptables?
  iptables可以理解为是一个代理程序,用户通过代理程序,将安全规则添加到安全框架中 net filter(内核空间)
  netfilter/iptables 是Linux中的包过滤型防火墙,是免费的,可以代替昂贵的商业防火墙解决方案,可以实现数据包的过滤,实现数据包的转换。

2.什么是包过滤防火墙?
  包过滤型防火墙在我们网络层拦截网络数据包的包头(header),可以针对数据包的包头,与我们事先准备好的防火墙规则进行比对,与规则相匹配的包放行,不匹配的包丢弃或者执行一些复杂的动作,包过滤型防火墙一般作用在我们的网络层,故也称为网络型防火墙。 
  通常header中带有 客户端来源IP,源端口,目标IP,目标端口,协议类型:tcp udp icmp,根据这些状态信息来判断,是否符合我们的IP tables的过滤规则,符合通过,不然拒绝。

3.包过滤型防火墙是怎么实现的?
  是基于net filter安全框架实现的,是内核的一部分,如果我们想要让防火墙达到防火的目的,需要我们在内核中,设定一些关卡,所有进出的报文,根据设定的这些关卡进行检查,将符合条件的放行,不符合的阻止,
  在net filter中,这些关卡就是链

3.iptables链的概念

1.什么是链?
  防火墙的作用就是在于将经过的报文与设定的规则进行比对,然后执行响应的动作,报文经过链时,必须匹配链的规则,在链上不止一条规则,存在着很多规则,当我们将这些规则穿在一起的时候就形成了链 
  链的匹配规则,如下图所示,当有报文经过时,会以此往下匹配规则,如果匹配成功,则执行相对于的动作,如果匹配不成功,就以此往下匹配,直到最后一条规则还是没有匹配成功,会执行链的默认规则。

2. iptables 有哪些链?
    我们从A点到达B点时,途中会经过很多的高速路,不同的高速路有不通的关卡设定,比如说,过路费,查酒驾,查超载,查超速等等,这些都可以理解为一些关卡
    在iptables中,数据流入本机,经过防火墙,会经过五个关卡,也就是五个链
    PREROUTING: 
    input:
    output:
    forward:
    postrouting:

    数据流入本机:当数据经过prerouting,会判断是否是请求本机,请求本机,送往input链,经过input 发送到我们用户空间的process进程
    数据流出本机:process进程发送给output,由我们的output发送给postrouting,由postrouting选择最优路线,回传给用户
    数据经过本机:当用户请求防火墙时,数据报文要传给后端的服务器,这时数据报文是经过本机,而不是到达本机,首先会经过prerouting链,由于不是请求本机,所以不会送往input,会送往forward,通过postrouting选择接口,转给后端主机。

4.什么是表?

  对每个链上面会放很多规则比如:
    A:IP地址或端口过滤
    B:修改数据报文
  我们把具有相同功能规则的集合叫做表,不通功能的规则,可以放置在不同的表中进行管理
iptables中的表:
  filter:   负责做过滤功能         input、output、forward
  nat:      网络地址转换           PREROUTING、input、output、postrouting
  mangle:   负责修改数据包的内容    PREROUTING: input、output、forward、postrouting
  raw:  关闭nat表中的追踪机制(用的较少)

操作不同表中的不同的链实现的功能不一样,

4.1.数据包的优先级

1.当用户A请求的是我本机的用户进程,首先数据包进来会经过prerouting链,由于raw,mangle,nat表都可以管理prerouting链,他会依次看raw,mangle,nat 表中的链有什么规则,依次执行,执行完之后在进行路由选择,
2.由于是访问我们本机的应用进程,会送往input链,会先执行mangle表中的数据规则,看有没有需要改写的,在进入filter表中的数据规则,看有没有需要过滤的,在送往我们的用户空间进程

数据包的优先级
  raw--->mangle--->nat--->filter
问题1:来自于10.0.0.1的地址,访问本机的web服务请求都不允许,应该在那个表的那个链上设定规则?
  在filter表中的input链
  prerouting链是由raw,mangle,nat表管理的,他们没有数据包过滤的功能
  input 是由 filter nat mangle 管理的,filter 支持过滤,所以是由filter表中的input链

问题2:所有本机发往10.0.0.0/24网段的tcp服务都不允许
  在filter表中的output链
  output属于filter表,有数据改写的功能,postrouting属于nat,mangle没有数据包过滤的功能

问题3:所有来自本机内部网络的主机,像互联网发送web请求,都不允许
  防火墙在内部主机之前,内部主机要上网的化需要经过防火墙,当内部主机需要经过防火墙时,第一个经过的时prerouting,不是请求本机,只是从本机经过,所有不会转给input,会转给forward,forward转给postrouting选择接口,转给web服务器。
  prerouting ---> froward ---> postrouting 
  prerouting 和postrouting都不属于 filter表,所以没有过滤功能,要在forward的上面做

PS: 要明确的了解需要做的是 过滤、地址转换、数据包的改写,这样我们才知道是操作的那个表,然后我们需要知道数据包流经的路径,这样我们才知道应该添加到那个链上
  实现的功能是什么:  操作那个表
  报文经过的路径:  判断规则添加到那个链上

5.iptables 的规则

1.什么是规则?规则是如何组成的
  我们的数据包的过滤,就是基于规则,而规则,是基于条件+动作完成的,想实现防火墙达到防火的目的,无外乎是为其添加相应的规则,即,我们匹配的条件和执行的动作

2.规则的增删查改
  iptables -t 表名 选项(增删查改)链名称 条件 动作
       -t                指定操作的表名
       -A, --append      追加一条规则到链中
       -D, --delete      删除链中的规则
       -I, --insert      插入一条规则,插入到顶部
       -R, --replace     替换,修改
       -L, --list        列出当前的规则
       -S, --list-rules  列出所有的规则
       -F, --flush       清空  
       -Z, --zero        清空计数器(每条规则都有匹配的包数量、包大小)
       -N, --new-chain   创建一个自定义链
       -X, --delete-chain 删除一个自定义链
       -P, --policy      指定链的默认规则
  

iptables -t 表名 选项(增删查改)链名称 条件 动作

hdss7-11 bin]# iptables -t filter -L -n -v #查看规则

hdss7-11 bin]# iptables -t filter -I INPUT -p icmp -j REJECT #添加一条禁止icmp的规则(禁ping)

hdss7-11 bin]# iptables -t filter -R INPUT 1 -p icmp -j DROP #修改第一条规则,改为DROP(丢弃)

[root@hdss7-11 bin]# iptables -t filter -Z #清除计数器

hdss7-11 bin]# iptables -t filter -D INPUT 1 #删除指定编号规则

6.iptables 基本的条件匹配

1. -p      #指定协议
2. -s -d    #源地址,目标地址
3. --sport --dport  #源端口 目标端口
4. -i -o     #进来的接口,出去的接口
5. -m -j     # 

实例1:仅允许10.0.0.1访问10.0.0.5的80端口,其他拒绝
先允许:
  1.过滤,添加到filter 表,10.0.0.1请求本机的报文,会经过prerouting,会到达input,到达用户进程,prerouting不支持过滤,所以要在input链中做
  2.指定来源的ip地址    10.0.0.1
  3.指定目标的IP地址    10.0.0.5
  4.指定具体的协议    tcp
  5.指定具体的端口    80
  6.指定匹配后的动作  accept
后拒绝:
  1.添加到filter表
  2.明确指定拒绝所有

具体的配置:
  iptables -t filter -I INPUT -s 10.0.0.1 -d 10.0.0.5 -p tcp --dport 80 -j ACCEPT    #允许来源IP为10.0.0.1目的IP为10.0.0.5 协议为tcp 端口为80 的进行访问
  iptables -t filter -A INPUT -p tcp -j DROP        #拒绝所有的tcp链接,注意ssh属于tcp连接,-A 追加
  iptables -t filter -F         #清空所有的配置
实例2:凡是由本机发出的TCP协议报文,都允许出去,其他的协议不允许

先允许:
    1.过滤,filter表,要从本机出去,所以是output链
  
再拒绝:
    拒绝所有的

具体配置:
  iptables -t filter -I OUTPUT -p tcp -j ACCEPT      #在顶部添加一条允许tcp出去的条件
  iptables -t filter -A OUTPUT -j DROP               #追加一条条件为任何协议都不允许出去
  iptables -t filter -F OUTPUT                        #清空OUTPUT 链上的所有规则

实例3:禁止其他主机 从eth0 发来的ping请求
  1.过滤,filter表,请求的是本机,在input链上
  2.指定从哪个网络接口过来的数据包  ech0  -i  指定接口
  3.指定具体的协议 icmp
  4.指定具体的动作 DROP

  iptables -t filter -I INPUT -p icmp -i eth0 -j DROP

       [!] -i, --in-interface name
              Name of an interface via which a packet was received (only for packets entering the INPUT, FORWARD and PREROUT‐
              ING chains).  When the "!" argument is used before the interface name, the sense is inverted.  If the interface
              name  ends in a "+", then any interface which begins with this name will match.  If this option is omitted, any
              interface name will match.

  1. iptables 扩展的条件匹配
1. multiport模块      #可以添加多个不连续的端口,最多可以添加十五个
  实例:仅允许10.0.0.1访问本机:80、443、20、21、22
    使用--dport 20:22  会添加20 21 22 这几个端口,但是,是连续的
  iptables -t filter -I INPUT -s 10.0.0.1 -d 10.0.0.5 -p tcp -m multiport --dport 20,21,22,80,443 -j ACCEPT    #允许tcp 20,21,22,80,443 ,且来源IP是10.0.0.1,目的IP是10.0.0.5 的请求通过
  iptables -t filter -A INPUT -p tcp -j DROP    #拒绝所有的tcp请求通过

2.iprange模块
  指定一段连续的ip地址范围:用于匹配报文的源地址或者目的地址
  
  [!] --src-range from[-to]    源地址范围  感叹号表示非
  [!] --dst-range from[-to]    目标地址范围

实例1 : 10.0.0.1-10.0.0.7 都不允许ping10.0.0.5这台主机

  1.过滤,filter ,流入本机需要input链
  2.协议 icmp
  3.执行的动作,丢弃
    iptables -t filter -I INPUT -p icmp -m iprange --src-range 10.0.0.1-10.0.0.7 -j DROP

   iprange
       This matches on a given arbitrary range of IP addresses.

       [!] --src-range from[-to]
              Match source IP in the specified range.

       [!] --dst-range from[-to]
              Match destination IP in the specified range.
3.string 模块      
  匹配指定的字符串,数据报文中包含匹配的字符串,则满足要求
       --algo {bm|kmp}          #匹配的查询算法
              Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)
 
       [!] --string pattern      #指定需要匹配的字符串
              Matches the given pattern.

实例:当应用返回的数据报文包含hello,则丢弃,其他正常通过
echo "iptables-tt" > /var/www/html/index.html
echo "iptables-hello" > /var/www/html/test.html

iptables -t filter -I OUTPUT -p tcp -m string --string "hello" --algo bm -j DROP        #使用场景,防火墙,当作路由器,屏蔽一些出去的网站,比如淘宝之类的

curl 10.0.0.5/index.html
iptables-tt
curl 10.0.0.5/test.html
返回值为空

   string
       This modules matches a given string by using some pattern matching strategy. It requires a linux kernel >= 2.6.14.

       --algo {bm|kmp}
              Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)

       --from offset
              Set the offset from which it starts looking for any matching. If not passed, default is 0.

       --to offset
              Set  the offset up to which should be scanned. That is, byte offset-1 (counting from 0) is the last one that is
              scanned.  If not passed, default is the packet size.

       [!] --string pattern
              Matches the given pattern.

       [!] --hex-string pattern
              Matches the given pattern in hex notation.

       Examples:

              # The string pattern can be used for simple text characters.
              iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string 'GET /index.html' -j LOG

              # The hex string pattern can be used for non-printable characters, like |0D 0A| or |0D0A|.
              iptables -p udp --dport 53 -m string --algo bm --from 40 --to 57 --hex-string '|03|www|09|netfilter|03|org|00|'

  
4.time 模块


      --timestart hh:mm[:ss]      #开始时间
  
       --timestop hh:mm[:ss]      #结束时间
              Only match during the given daytime. The possible time range  is  00:00:00  to  23:59:59.  Leading  zeroes  are
              allowed (e.g. "06:03") and correctly interpreted as base-10.

       [!] --monthdays day[,day...]    #指定每个月的某天
              Only  match on the given days of the month. Possible values are 1 to 31. Note that specifying 31 will of course
              not match on months which do not have a 31st day; the same goes for 28- or 29-day February.

       [!] --weekdays day[,day...]    #指定每周的某天
              Only match on the given weekdays. Possible values are Mon, Tue, Wed, Thu, Fri, Sat, Sun, or values from 1 to 7,
              respectively. You may also use two-character variants (Mo, Tu, etc.).


      --kerneltz       #使用内核时区,而不是UTC时间


实例:拒绝每天8:30 -- 18:00 任何主机发送icmp协议
 iptables -t filter -I INPUT -p icmp -m time --timeout 00:30 --timestop 10:30 -j DROP
5.icmp 模块
  当我们iptables -t filter -I INPUT -p icmp -j DROP 时,我们无法和对端主机发送ping 请求,对端也无法ping同我
  可以使用--icmp-type 模块,使对方无法ping我,而我可以ping同对端
  iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j DROP  

echo-request (8)(状态码) 请求
echo-reply (0)  回应

   icmp (IPv4-specific)
       This extension can be used if `--protocol icmp' is specified. It provides the following option:

       [!] --icmp-type {type[/code]|typename}
              This allows specification of the ICMP type, which can be a numeric ICMP type, type/code pair,  or  one  of  the
              ICMP type names shown by the command
               iptables -p icmp -h
6.connlimit 模块
限制每个客户端ip地址,到服务器的并发连接数

iptables -t filter -I INPUT -p tcp --dport 23 -m connlimit --connlimit-above 2 -j REJECT        #一个ip超过两个连接拒绝


   connlimit
       Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).

       --connlimit-upto n  如果现有连接数小于或等于多少时,则进行匹配
              Match if the number of existing connections is below or equal n.

       --connlimit-above n  如果现有连接数大于,则进行匹配
              Match if the number of existing connections is above n.

       --connlimit-mask prefix_length
              Group hosts using the prefix length. For IPv4, this must be a number between (including) 0 and  32.  For  IPv6,
              between 0 and 128. If not specified, the maximum prefix length for the applicable protocol is used.

       --connlimit-saddr
              Apply the limit onto the source group. This is the default if --connlimit-daddr is not specified.

       --connlimit-daddr
              Apply the limit onto the destination group.
7.limit 
  针对报文速率进行限制 秒 分 时 天


   limit
       This module matches at a limited rate using a token bucket filter.  A rule using this extension will match until  this
       limit is reached.  It can be used in combination with the LOG target to give limited logging, for example.

       xt_limit  has no negation support - you will have to use -m hashlimit !  --hashlimit rate in this case whilst omitting
       --hashlimit-mode.

       --limit rate[/second|/minute|/hour|/day]
              Maximum average matching rate: specified as a number, with an optional `/second', `/minute', `/hour', or `/day'
              suffix; the default is 3/hour.

       --limit-burst number   当我们限制一秒钟接受一个报文,默认是5个报文,也就是第一秒是5个报文,然后一秒一个
              Maximum  initial  number  of packets to match: this number gets recharged by one every time the limit specified
              above is not reached, up to this number; the default is 5.

实例1:限制每分钟接收10个报文
  iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT      每分钟允许通过十个报文
  iptables -t filter -A INPUT -p icmp -j DROP 拒绝通过icmp报文

实例2:允许10个数据报文快速通过,超过的数据报文 1/m
  iptables -t filter -I INPUT -p icmp -m limit --limit 1/m --limit-burst 10 -j ACCEPT  默认十个
  iptables -t filter -A INPUT -p icmp -j DROP

实例3:限制传输的带宽不可以超过500k
    500k * 1024 = 500000 b /1500 = 300 左右
    iptables -t filter -I OUTPUT -p tcp -m limit --limit 300/s -j ACCEPT
    iptables -t filter -I OUTPUT -p tcp -j DROP

如若转载,请注明出处:https://www.cnblogs.com/yangxiaoni

posted on 2022-01-11 20:57  杨港澳  阅读(1734)  评论(1编辑  收藏  举报