防火墙详细讲解

发布时间:2020-08-03 21:51:38 作者:天涯悠客
来源:网络 阅读:2130

防火墙详细讲解

一、防火墙的简介

所谓防火墙指的是一个由软件和硬件设备组合而成、在内部网和外部网之间、专用网与公共网之间的界面上构造的保护屏障.是一种获取安全性方法的形象说法,它是一种计算机硬件和软件的结合,使Internet与Intranet之间建立起一个安全网关,从而保护内部网免受非法用户的侵入,防火墙主要由服务访问规则、验证工具、包过滤和应用网关4个部分组成,防火墙就是一个位于计算机和它所连接的网络之间的软件或硬件。该计算机流入流出的所有网络通信和数据包均要经过此防火墙。

在网络中,所谓“防火墙”,是指一种将内部网和公众访问网(如Internet)分开的方法,它实际上是一种隔离技术。防火墙是在两个网络通讯时执行的一种访问控制尺度,它能允许你“同意”的人和数据进入你的网络,同时将你“不同意”的人和数据拒之门外,最大限度地阻止网络中的***来访问你的网络。换句话说,如果不通过防火墙,公司内部的人就无法访问Internet,Internet上的人也无法和公司内部的人进行通信。

二、防火墙的分类

1、根据表现形式分类,可以分为硬件防火墙和软件防火墙

(1)硬件防火墙

硬件防火墙主要有:ciscopix---》asa)、天融信网络安全卫士)、H3Csecpath)、神州数码锐捷等,这些都是工作在网络层,还有深信服防火墙,它可以工作在应用层

(2)软件防火墙

软件防火墙的实现主要是在主机上实现的,如windowsISAinternet security acceleration)和linuxiptables

2、根据工作层次分类,可以分为应用层防火墙和网络层防火墙

(1)网络层防火墙

网络层防火墙可视为一种IP封装包过滤器,运作在底层的TCP/IP协议堆栈上。我们可以以枚举的方式,只允许符合特定规则的封包通过,其余的一概禁止穿越防火墙(病毒除外,防火墙不能防止病毒侵入)。这些规则通常可以经由管理员定义或修改,不过某些防火墙设备可能只能套用内置的规则。

(2)应用层防火墙

应用层防火墙是在TCP/IP堆栈的“应用层”上运作,您使用浏览器时所产生的数据流或是使用FTP时的数据流都是属于这一层。应用层防火墙可以拦截进出某应用程序的所有封包,并且封锁其他的封包(通常是直接将封包丢弃)。理论上,这一类的防火墙可以完全阻绝外部的数据流进到受保护的机器里。

三、iptables的主要模块介绍

1. string(字符串匹配,可以用做内容过滤

# cat string/info 

iptables -I FORWARD -m string --string "腾讯" -j DROP 

iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP 

iptables -I FORWARD -d 192.168.3.0/24 -m string --string "宽频影院" -j DROP 

iptables -I FORWARD -d 192.168.3.0/24 -m string --string "***" -j DROP 

iptables -I FORWARD -d 192.168.3.0/24 -p tcp --sport 80 -m string --string "广告" -j DROP 

至于怎么灵活运用就要看自己的需要了


2. comment (备注匹配,可以支持最多256个字符

Supported options: 

--comment COMMENT 

翻译:这个选项增加CONFIG_IP_NF_MATCH_COMMENT,补充一个注释匹配模块.这个匹允许你增加一个备注都任何规则,这个备注最多支持256个字符,例如 

Example:(例子:) 

-A INPUT -s 192.168.0.0/16 -m comment --comment "A privatized IP block" 

译者:我是这样测试使用这个comment 

iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -j DROP -m comment --comment "the bad guy can not online" 

iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP -m comment --comment "denny go to qq.com" 

这样在iptables -L,就看到每条规则后面出现备注的内容.可以提高可读和理解该条规则的作用

这个comment 2.6.x中已经被正式收录


3. connlimit(同时连接个数限制匹配

# cat connlimit/info 

这个增加一个iptables匹配允许你限制每个客户ip地址的并发tcp连接,即同时连接到一个服务器个数

Examples: 例子

# allow 2 telnet connections per client host (允许每个客户机同时两个telnet连接

iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT 

# you can also match the other way around:(你也可以匹配其他的方法:) 

iptables -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT 

# limit the nr of parallel http requests to 16 per class C sized (这下面例子限制80端口最多同时16个连接请求

# network (24 bit netmask) 

iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT 

模块 connlimit 作用:连接限制 

--connlimit-above n 限制为多少个 

--connlimit-mask n 这组主机的掩码,默认是connlimit-mask 32 ,即每ip. 

这个主要可以限制内网用户的网络使用,对服务器而言则可以限制每个ip发起的连接数...比较实用 

例如:只允许每个ip同时580端口转发,超过的丢弃

iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP 

例如:只允许每组Cip同时1080端口转发

iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP 

例如:为了防止DOS太多连接进来,那么可以允许最多15个初始连接,超过的丢弃

/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP 

/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT 


4. time(时间匹配

Match only if today is one of the given days. (format: Mon,Tue,Wed,Thu,Fri,Sat,Sun ; default everyday) 

(只是匹配已经给出的天,格式Mon,Tue,Wed,Thu,Fri,Sat,Sun ;默认每天

(开始日期 日期

Match only if it is after `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 1970) 

(只是匹配这个开始日期值之后的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默认是1970): 

Match only if it is before `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 2037) 

(只是匹配这个开始日期值之前的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默认是2037): 

Example: (例子:) 

-A INPUT -m time --timestart 8:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri 

will match packets that have an arrival timestamp in the range 8:00->18:00 from Monday to Friday. 

(上面将匹配从到达日期是星期一至星期五时间从8:0018:00的包

-A OUTPUT -m time --timestart 8:00 --timestop 18:00 --Days Mon --date-stop 2010 

will match the packets (locally generated) that have a departure timestamp in the range 8:00->18:00 on Monday only, until 2010 

(上面将匹配本地产生的时间范围直到2010年为止的每个星期一8:0018:00的包

NOTE: the time match does not track changes in daylight savings time

 

5. iprange (ip范围匹配)   

This patch makes possible to match source/destination IP addresses against inclusive IP address ranges. 

翻译这个补丁令匹配源/目标 IP地址可以倚着给出的地址范围进行匹配 

Examples:(例子) 

iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT 

这个例子是允许源ip地址范围192.168.1.5-192.168.1.124的包通过 

iptables -A FORWARD -m iprange --dst-range 10.0.0.0-10.255.255.255 -j ACCEPT 

这个例子是允许目标ip地址范围10.0.0.0-10.255.255.255的包通过    192.168.2.10-192.168.2.20


6. geoip(根据地理位置匹配

This patch makes possible to match a packet by its source or destination country. 

翻译:这个补丁令一个包能够根据源或目的国家(地区)匹配 

GeoIP options: (选项:) 

--src-cc, --source-country country 

Match packet coming from (one of) the specified country(ies) 

根据包的来源(或非来源)地区匹配 

--dst-cc, --destination-country country 

Match packet going to (one of) the specified country(ies) 

根据包的去向(或非去向)地区匹配 

NOTE: The country is inputed by its ISO3166 code. 

注意:这个国家地区列表放在ISO3166编码里 

The only extra files you need is a binary db (geoipdb.bin) & its index file (geoipdb.idx).Both files are generated from a countries & subnets database with the csv2bin tool,available at www.cookinglinux.org/geoip/. Both files MUST also be moved in /var/geoip/ as the shared library is statically looking for that pathname (ex.: /var/geoip/geoipdb.bin). 

这个你需要额外的二进位文件geoipdb.bin 和它的索引文件geoipdb.idx.这两个文件是国家地区网络数据库,是用csv2bin 工具生成的,可以在www.cookinglinux.org/geoip/得到.这些文件必须放在/var/geoip/,作为一个共享库查找路径名字如/var/geoip/geoipdb.bin 


7. Nth(n个包匹配

# cat nth/info 

Title: iptables nth match (标题: iptablesN个匹配

Author: Fabrice MARIE (作者:名字,email地址

Status: Works (状况:运作

Repository: base (贮仓库基础的

# cat nth/help 

This option adds an iptables `Nth' match, which allows you to match every Nth packet encountered. By default there are 16 different counters that can be used. 

翻译这个选项增加一个第N个匹配,允许你匹配每隔N个包,默认有16不同的计算方法可以使用 

This match functions in one of two ways 

1) Match ever Nth packet, and only the Nth packet. 

example:(例子

iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP 

This rule will drop every 10th packet. 

这个规则将丢弃每隔10个包 

2) Unique rule for every packet. This is an easy and quick method to produce load-balancing for both inbound and outbound. 

为每一个包应用一个独特的规则,这样是一个容易和快速的负载均衡方法 

example: (例如

iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 0 -j SNAT --to-source 10.0.0.5 

iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 1 -j SNAT --to-source 10.0.0.6 

iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 2 -j SNAT --to-source 10.0.0.7 

This example evenly splits connections between the three SNAT addresses. 

上面例子由三个源ip地址平滑分割连接 

By using the mangle table and iproute2, you can setup complex load-balanced routing. There's lot of other uses. Be creative! 

配合iptables mangle表和高级路由iproute2,你能设置一个复合的负载平衡路由.还有其他的用途,具有创造性的设置 

Suppported options are: (支持选项有:) 

--every Nth Match every Nth packet (匹配每个包

num Use counter 0-15 (default:0) (用计算器(默认是0)) 

num Initialize the counter at the number 'num' instead of 0. Must be between 0 and Nth-1 

(初始化一个计算器,用这个num值而不是0. 必需是在0N1之间的数 

num Match on 'num' packet. Must be between 0 and Nth-1. If --packet is used for a counter than 

there must be Nth number of --packet rules, covering all values between 0 and Nth-1 inclusively. 

匹配'num' ,必需是在0N1之间的数,如果该包被用一个计算器,必需是第Nth数字包规则,并覆盖0Nth-1的所有值(这个翻译得不好,请指正


8. ipp2p(点对点匹配) 

# cat ipp2p/info 

Title: Detects some P2P packets (标题侦查P2P

Author: Eicke Friedrich (作者:名字,email地址

Status: Stable (状况:稳定的

Repository: extra (贮仓库额外的

Recompile: netfilter, iptables (重新编译:netfilter|iptables) 

# cat ipp2p/help 

This option makes possible to match some P2P packets therefore helps controlling such traffic. 

Dropping all matches prohibits P2P networks. 

Combined with conntrack,CONNMARK and a packet scheduler it can be used for accounting or shaping of P2P traffic. 

这个选项能够匹配某些P2P,帮助控制流量.丢弃所有匹配的P2P.结合连接跟踪,和一个包的调度器,它能被用作计算和×××一个P2流量 

Examples: (例如:) 

iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP 

iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP 

iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP 

上例可以封杀很多bt等的P2P软件

更多参数更详细的参考还有ipp2p/iptables/extensions/libipt_ipp2p.manhttp://www.ipp2p.org 


9. quota(配额匹配

# cat quota/info Title: iptables quota match (标题: iptables配额匹配

Author: Sam Johnston (作者:名字,email地址

Status: worksforme (状况:运作的

Repository: base (贮仓库基础的

# cat quota/help 

This option adds CONFIG_IP_NF_MATCH_QUOTA, which implements network quotas by decrementing a byte counter with each packet. 

这个选项增加一个模块匹配包实现网络包通过的配额 

Supported options are: (支持选项有:) 

--quota 

The quota in bytes. (bytes为单位

iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP 

上例是这样的,ip192.168.3.159的目标端口为80匹配的500个字节内,将被丢弃.直到匹配完这500字节后,才不会丢弃,就是说,才可以打开网页

KNOWN BUGS: this does not work on SMP systems. 

知道的bug:这个不能工作在SMP系统上,(SMP, symmetric multiprocessing 多处理技术


10state模块

-m  state  --state   NEW   ESTABLISHED   RELATED

 


推荐阅读:
  1. MVCC详细讲解
  2. 详细讲解Apache服务配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

防火墙 iptables iptables模块

上一篇:我所理解的Cocos2d-x

下一篇:Redis数据库备份与恢复

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》