Firewall four tables pentachain |
|
Four Menu |
|
Network filter table: filter | Filtering, firewall |
Network address translation table: nat | network address translation Network Address Translation |
Traffic shaping table labeled: mangle | Dismantling message, make changes, encapsulated message |
Traffic tracking table: raw | Close links tracking mechanisms enabled on nat table |
Pentachain |
|
PREROUTING chain |
Before entering route data packets |
INPUT chain | Native destination address |
FORWARD chain | Achieve forward |
OUTPUT链 | Native original address, send out |
POSTROUTING chain | Before being sent to the card |
Relationship iptables tables and chains |
|
Correspondence between tables and iptables chain, which is actually a table that contains several chain. | |
Network filter table: filter | INPUT chain, FORWARD chain, OUTPUT chain |
Network address translation table: nat | PREROUTING chain, OUTPUT chain, POSTROUTING chain |
Traffic shaping table labeled: mangle | PREROUTING chain, INPUT chain, FORWARD chain, OUTPUT chain, POSTROUTING chain |
Traffic tracking table: raw | PREROUTING chain, OUTPUT chain |
iptables commands common method |
|
1, the temporary opening and closing firewall | /etc/init.d/iptables start | stop |
2, completely turn off the firewall: | Command: setup enter, locate firewall. Enter in [] bid ✳ (space) after firewall click ok, quit quit. |
3. Verification: | Enter iptables -t filter -nvL |
iptables -t filter -nvL command interpreter: | 1, View firewall to "filter" the contents of Table 2, -n display address, port number and other information in digital form 3, -L list all rule entries 4, -v shown in a more detailed manner 5, - line-numbers when viewing the rule to display the serial number rule |
Displayed information Explanation: | INPUT:入栈链 OUTPUT:出栈链 FORWARD:转发链 是否运输数据经过本机,有本机转发 |
4、开启xinetd | 客户端连接,显示连接失败 |
6、重新启动防火墙: | service iptables restart |
编写防火墙规则 |
|
linux包过滤防火墙概述包过滤的工作层次 |
|
1、主要是网络层,针对ip数据包。 | 2、体现在对包内ip地址、端口等信息的处理 |
iptables基本语法(一) |
|
1、语法结构 |
iptables [ -t 表名 ] 选项 [ 链名 ] [ 条件 ] [ -j 控制类型 ] |
表名 | 1、raw 流量跟踪 2、mangle 流量 标记整形 3、nat 网络地址转换 4、filter 网络过滤 |
选项 | -I 在链的开头(或指定序号)插入一条规则 -D 删除一条规则 -P 指定链的默认规则(默认规则只接受 ACCETR DROP ,不支持REJECT和LOG) -F 清空链的所有规则 |
链名 | INPUT OUTPUT FORWARD |
条件 | 1、all 2、icmp 3、tcp 4、udp |
控制类型 | 1、 REJECT 2、 ACCETPR 3、DROP 丢弃不做任何回应 4、LOG 记录日志信息,传递给下一条规则 |
2、例如: |
iptables -t filter -I INPUT -p icmp -j REJECT (阻止ping测试) |
3、几个注意事项 |
1、不指定表名时,默认是filter 2、不指定链名时,默认是表内所有链 (指的是写在所有的默认规则中) 3、除非设置链的默认策略,否则必须指定匹配条件 4、选项、链名、控制类型使用大写,其余均为小写 |
常见的通用匹配条件 |
|
1、协议匹配: | -p 协议名 |
2、地址匹配: | -s 源地址 -d 目的地址 |
3、接口匹配: | -i入栈网卡 -o 出栈网卡 -j 控制类型 |
例如: | 1、iptables -I INPIUT -p icmp -j DROP 2、iptables -A FORWARD !-p icmp -j REJECT //!取反 3、iptables -A FORWARD -s 192.168.1.11 -j REJECT 4、iptables -I INPIUT -s 10.20.30.0/24 -j REJECT //一个网段 5、iptables -A INPIUT -i eth1 -s 192.168.0.0/16 -j DROP 6、iptables -A INPIUT -i eth1 -s 10.0.0.0/8 -j DROP 7、iptables -A INPIUT -i eth1 -s 172.16.0.0/12 -j DROP |
常用的隐含匹配条件 |
|
1、端口匹配: | --sport 源端口 --dport 目标端口 |
2、TCP标记匹配: | --tcp-flags 检查范围被设置标记 |
3、ICMP类型匹配: | --icmp-type ICMP类型 |
举例说明: | 1、iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCETP 4、iptables -A INPIUT -p icmp --icmp-type 0 -j ACCETP |
实验说明: | 8 ; 3; 0:iptables -I INPIUT -p icmp --icmp-type 8 -j DROP linux这样修改防火墙后,windows不能ping通linux,反之可以(windows防火墙是关闭的 ) |
常用的显示匹配条件 |
|
1、多端口配置: | -m multiport --sport 源端口列表 |
2、目的端口列表 | -m multiport --dport |
3、ip范围匹配: | -m iprange --src-range ip范围 |
4、MAC地址范围: | -m -mac -mac-secure MAC地址 |
5、状态匹配: | -m state --state 连接状态(根据连接状态进行放行) |
举例说明: | 1、iptables -I INPIUT -p tcp -m multiport --dport 20,21,2100:2500 -j ACCETP 2、iptables -A INPIUT -p tcp -m multiport --dport 25,80,110,143 -j ACCETP // 25邮件端口 3、iptables -A FORWARD -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j ACCETP 4、iptables -A INPIUT -m mac --mac-secure 00:0c:29:c0:55:3f -j DROP |
路由规则 |
|
显示内容解释: | PREROUTING //路由器前规则 POSTROUTING //路由后规则 OUTOUT //出栈 |
配置路由后规则 |
|
例: | iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.100 -o eth1 --dport 80 -j SNAT --to-source 12.34.56.78 |
例: | iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.0/ 24 -o eth1 -j MASQUERADE 说明:MASQUERADE ----伪装地址 适用于外网ip非固定的情况 |
注释: | 使用内网访问外网网站,中间要通过网关进行转换,访问外网IP,实则是访问到网关,网管进行转换,转换以后使用网关使用自己ip进行访问的,访问成功。 在网关上设置转换命令,将内网访问来的源IP转换为,另一网关IP进行访问。 |
配置路由前规则 |
|
例: | iptables -t nat -I PREROUTING -i eth1 -d 12.34.56.78 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100 |
注释: | 使用外网访问内网,网关的网站,但是网关没有他给转换到一个有的服务器上去。当你访问网关ip时自动给你转换。 在网关的防火墙上进行命令配置。( 命令后面转换输出口为外网出口eth1 ) |
防火墙永久保存 |
|
1、修改防火墙配置文件 | vim /etc/sysconfig/iptables |
2、命令:etc /init.d/iptables save | 也可以将当前修改的iptables规则永久保存。 |
计划任务 |
|
命令: | at 20:00 回车 |
输入: | service iptables restart 回车 |
任务计划保存: | ctrl+d |