计算机网络 - iptables 防火墙

目录

1.  iptables 的四表五链

1.1  规则链

1.1.1  默认的5种规则链

1.1.2  规则链间的匹配顺序

1.1.3  规则链内的匹配顺序

1.2  规则表

1.2.1  默认的4个规则表

1.2.2  规则表之间的优先顺序

1.3  数据包过滤匹配流程

2.  iptables命令的语法格式

2.1  设置规则内容:

2.2  列表查看规则

2.3  清除规则

2.4  策略:

2.5  定时清除iptables规则

2.6  自定义规则链

2.6.1  -N:创建一条新的规则链

2.6.2  怎么使用自定义链?

2.6.3  -X:删除自定义的规则链

3.  linux 日志级别

3.1  记日志功能

3.2  修改默认日志文件:

3.2.1  1.vim /etc/rsyslog.conf

3.2.2  2.修改默认日志文件

3.2.3  3.刷新rsyslog 服务

4.  设置匹配数据包的条件

4.1  通用条件匹配

4.1.1  协议匹配

4.1.2  地址匹配

4.1.3   接口匹配

4.2  隐含条件匹配

4.2.1  端口匹配

4.2.2  TCP标记匹配

4.2.3  ICMP类型匹配

4.3  显式条件匹配

4.3.1  MAC地址匹配

4.3.2  多端口匹配

4.3.3  IP地址范围匹配

5.  导入,导出防火墙规则

5.1  导出规则

5.2  导出规则

5.3  如何开机自动加载我们的自定义防火墙规则?

5.4  练习

5.5  开启路由转发功能


防火墙 : firewall

防火墙:一个安全工具,防止别人攻击

防火墙只能防攻击,不能防病毒

防火墙:保安

杀毒软件:医生

iptables 和 firewalld 是两个不同的防火墙传参工具

iptables 是旧的

firewalld是新式,firewalld 在itpables 的基础上做了封装,底层依然使用的是iptables

关闭防火墙

service firewalld stop

systemctl disable firewalld

linux防火墙概述:

linux防火墙功能是由内核实现的

包过滤机制:linux内核来实现,内核有个模块 netfilter

查看系统版本

[root@localhost ~]# cat /etc/centos-release

CentOS Linux release 7.9.2009 (Core)

查看内核版本

[root@localhost ~]# uname -r

3.10.0-1160.el7.x86_64

git 和 github gitee gitlab 有什么区别?

版本控制软件

代码托管平台

github --》全球 微软

gitee --》码云 国内

gitlab --》局域网内的github

netfilter

位于linux内核中的包过滤防火墙功能体系

称为linux防火墙的内核态

iptables

位于sbin/iptables,是用来管理防火墙的命令工具

为防火墙体系提供过滤规则/策略,决定如何过滤或处理达到防火墙主机的数据包

称为linux防火墙的用户态

习惯上,把以上两种称呼都可以代表linux防火墙

 ################################

1.  iptables 的四表五链

1.1  规则链

规则的作用在于对数据包进行过滤或处理,根据处理时机的不同,各种规则被组织在不同的链中

规则链是防火墙规则/策略的集合

 ################################

1.1.1  默认的5种规则链

INPUT:处理入站数据包

OUTPUT:处理出站数据包

FORWARD:处理转发数据包

POSTROUTING:在进行路由选择后处理数据包

PREROUTING:在进行路由选择前处理数据包

 ################################

1.1.2  规则链间的匹配顺序

入站数据:PREROUTING,INPUT

出站数据:OUTPUT,POSTROUTING

转发数据: PREROUTING,FORWARD,POSTROUTING

 ################################

1.1.3  规则链内的匹配顺序

按顺序依次进行检查,找到相匹配的规则即停止(LOG策略会有例外)

若在该链内找不到相匹配的规则,则按该链的默认策略处理

 ################################

1.2  规则表

具有某一相似用途的防火墙规则,按照不同处理时机区分到不同的规则链以后,被归置到不同的表中

规则表是规则链的集合

 ################################

1.2.1  默认的4个规则表

raw表:确定是否对该数据包进行状态跟踪

mangle表:为数据包设置标记

nat表:修改数据包中的源,目标ip地址或端口

filter表:确定是否放行该数据包(过滤)

 ################################

1.2.2  规则表之间的优先顺序

优先级:raw>mangle>nat>filter

 ################################

1.3  数据包过滤匹配流程

入站数据流向: 

 数据从网络过来以后,首先会经过PREROUTING链,经过raw,mangle,nat表的PREROUTING链检查,然后经过路由选择,如果是送往本机的往INPUT链送。如果是转发的就往FORWARD链送(必须开启路由功能)

出站数据流向:

出站数据有两种,一种是本机的应用进程的数据,一种是转发数据流向

 ################################ 

2.  iptables命令的语法格式

iptables[-t表名]管理选项[链名][条件匹配][-j目标动作或跳转]

几个注意事项

不指定表名时,默认表示filter表

不指定链名时,默认该表示该表内所有链

除非设置规则链的缺省策略,否则需要指定匹配条件

[root@localhost lianxi]# iptables -A INPUT -p icmp  --icmp-type 8 -j DROP
[root@localhost lianxi]# iptables -A INPUT -p icmp  --icmp-type 0 -j DROP
[root@localhost lianxi]# iptables -L -t filter -v -n --line
Chain INPUT (policy ACCEPT 74 packets, 4292 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8
2        0     0 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 38 packets, 2952 bytes)
num   pkts bytes target     prot opt in     out     source               destination
[root@localhost lianxi]# iptables -F -t filter

 ################################ 

2.1  设置规则内容:

-A:在链尾追加一条新的规则 append

-I : 在指定位置(或链首)插入一条新的规则 insert

-R:修改,替换指定位置或内容的规则 replace

-P:设置指定链的默认策略 policy

-p:设置协议 protocol

[root@localhost lianxi]# iptables -t filter -A INPUT -p tcp -j ACCEPT
[root@localhost lianxi]# iptables -I INPUT -p udp -j ACCEPT
[root@localhost lianxi]# iptables -I INPUT 2 -p icmp -j ACCEPT
[root@localhost lianxi]# iptables -L INPUT --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     tcp  --  anywhere             anywhere
[root@localhost lianxi]# iptables -P INPUT DROP
[root@localhost lianxi]# iptables -L INPUT --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     tcp  --  anywhere             anywhere
[root@localhost lianxi]# iptables -I INPUT 2 -p tcp -j ACCEPT
[root@localhost lianxi]# iptables -L INPUT --line
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     tcp  --  anywhere             anywhere
3    ACCEPT     icmp --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere
[root@localhost lianxi]# iptables -L INPUT --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     tcp  --  anywhere             anywhere
3    ACCEPT     icmp --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere

 ################################ 

2.2  列表查看规则

-L:列表查看规则 list

--line-numbers:查看规则信息显示规则的行号

-n:以数字形式显示ip地址,端口等信息verbose

-v:显示数据包个数,字节数,等详细信息

[root@localhost 7-15]# iptables -v -n  -L
Chain INPUT (policy ACCEPT 4 packets, 240 bytes)
pkts bytes target     prot opt in     out     source               destination
8   797 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0
385 25542 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 49 packets, 5736 bytes)
pkts bytes target     prot opt in     out     source               destination

 ################################ 

2.3  清除规则

-D:删除指定位置或内容的规则

-F:清空规则链内的所有规则

清除规则并不会将默认策略改为ACCEPT

[root@localhost 7-15]# iptables -L --line
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     tcp  --  anywhere             anywhere
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
[root@localhost 7-15]# iptables -D INPUT 2
[root@localhost 7-15]# iptables -L --line
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere
2    ACCEPT     tcp  --  anywhere             anywhere
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
[root@localhost 7-15]# iptables -F
[root@localhost 7-15]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 ################################ 

2.4  策略:

ACCEPT:接受数据

DROP:不给任何回复,直接丢弃

REJECT:给不能到达的回复,拒绝了,但是给回复

 ################################

2.5  定时清除iptables规则

可以使用计划任务,定时来清除iptabies规则

但是计划任务在这里有一个坑:调用的脚本里,如果有些命令不适用绝对路径会导致命令查找不到,从而不能执行

我们可以用一个变量来代替命令的绝对路径

或者每条命令都使用绝对路径

[root@localhost 7-15]# crontab -l
*/1 * * * * bash /lianxi/7-15/clear_iptables.sh
[root@localhost 7-15]# cat clear_iptables.sh
#! /bin/bash
iptables='/usr/sbin/iptables'
清除filter表和nat表里面的规则
$iptables -t filter -F
$iptables -t nat -F
将filter表里的INPUT链的默认规则恢复为ACCEPT
$iptables -t filter -P INPUT ACCEPT

如果在计划任务调用脚本时,脚本里面的一些命令没有使用绝对路径,计划任务会给用户发邮件,提示错误信息。

您在 /var/spool/mail/root 中有新邮件
[root@localhost 7-15]# cat /var/spool/mail/root
/lianxi/7-15/clear_iptables.sh:行4: iptables: 未找到命令
/lianxi/7-15/clear_iptables.sh:行5: iptables: 未找到命令
/lianxi/7-15/clear_iptables.sh:行7: iptables: 未找到命令

 ################################ 

2.6  自定义规则链

2.6.1  -N:创建一条新的规则链

[root@localhost 7-15]# iptables -t filter -N sanchuang
[root@localhost 7-15]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain sanchuang (0 references)
target     prot opt source               destination

 ################################ 

2.6.2  怎么使用自定义链?

在五个默认链做一次跳转,符合某种规则的就往自定义链送

  ################################

2.6.3  -X:删除自定义的规则链

直接删除自定义链会报错,因为自定义链里面有我们刚刚添加的规则

[root@localhost ~]# iptables -X sanchuang -t filter
iptables: Too many links.

先删除INPUT链第3条sanchuang引用

然后清除自定义链规则

其他:

-h:查看iptables命令的使用帮助

 ################################

3.  linux 日志级别

日志分为九个级别,严重级别从小到大为:

none:不记录日志

debug:调试信息,系统进行调试时产生的日志,不属于错误日志,不需要过多关注。

info:一般的通知信息,用来反馈系统的当前状态给当前用户。

notice:提醒信息,需要检查一下程序了,不理会可能会出现错误。

warning:警告信息,当出现警告时,你的程序可能已经出现了问题,但不影响程序正常运行,尽快进行处理,以免导致服务宕掉。

error:错误信息,出现这一项时,已经挑明服务出现了问题,服务都无法确认是否能正常运行。

critical:比较严重的错误信息,服务已经宕了,可能已经无法修复。

alert:警报信息,需要立即采取行动,不仅是服务宕了,还会影响系统的正常启动。

emerg:紧急信息,系统可能已经不能使用了,如果不能解决,就重新装机吧。

日志的设备类型

内核 kernel

计划任务 cron

authpriv 认证 --》ssh

mail

 ################################

3.1  记日志功能

语法:

iptables -t filter -I INPUT -p icmp --icmp-type 8 -j LOG --log-prefix sanchuang-wangsh --log-level 4

--log-prefix 前缀名

--log-level 日志登记

默认记录的文件:

/var/log/messages

运行这条命令,我们可以看到iptables规则就多了这样一条规则

[root@localhost 7-15]# bash set_iptables.sh
[root@localhost 7-15]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
LOG        icmp --  anywhere             anywhere             icmp echo-request LOG level warning prefix sanchuang-wangsh
ACCEPT     udp  --  anywhere             anywhere
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

然后ping一下自己的ip地址

使用 tail -f 动态监控日志文件的尾部

看到有日志记录,就成功了。

 ################################ 

3.2  修改默认日志文件:

示例:将kern.* 的日志文件放到 /var/log/sc_iptables.txt

3.2.1  1.vim /etc/rsyslog.conf

3.2.2  2.修改默认日志文件

3.2.3  3.刷新rsyslog 服务

service rsyslog restart

 ################################

4.  设置匹配数据包的条件

4.1  通用条件匹配

可直接使用,不依赖于其他的条件或扩展模块

包括网络协议,IP地址,网络接口等匹配方式

 ################################

4.1.1  协议匹配

使用 -p+ 协议名的形式

协议名可以使用 /etc/protocols 文件中定义的名称

常用的协议包括tcp,udp,icmp等

 ################################

4.1.2  地址匹配

使用 -s + 源地址 ,-d + 目标地址的形式

地址可以是单个IP地址,网络地址(带掩码长度)

 ################################

4.1.3   接口匹配

使用 -i + 网络接口名, -o + 网络接口的形式

分别对应接受,发送数据包的网络接口

 ################################

4.2  隐含条件匹配

一般需要以特定的协议匹配作为前提

包括端口,TCP标记,ICMP类型等匹配方式

 ################################

4.2.1  端口匹配

使用 --sport 源端口 , --dport 目标端口的方式

 ################################

4.2.2  TCP标记匹配

使用--tcp-flags 检查范围,被设置的标记的形式

如 --tcp-flags SYN,RST,ACK, SYN表示检查这三个标记,只有SYN为1时满足条件

 ################################

4.2.3  ICMP类型匹配

使用 --icmp-type ICMP 类型的形式

ICMP 类型可以使用类型字符串或者对应的数值

例如 Echo-Request(对应数值:8),Echo-Reply(对应数值 :0)

 ################################

4.3  显式条件匹配

需要使用“-m 扩展模块” 的形式明确指定匹配方式

包括多端口,MAC地址,IP地址范围,数据包状态等匹配方式

 ################################

4.3.1  MAC地址匹配

使用 -m mac 结合 --mac-source MAC地址的形式

 ################################

4.3.2  多端口匹配

使用 -m multiport 结合 --sports 源端口列表或者

--dports 目标端口列表的形式

多个端口之间使用逗号 , 分割,连续的端口使用 :分割

 ################################

4.3.3  IP地址范围匹配

使用 -m iprange 结合 --src-range 源IP范围或者

--dst-range 目标IP范围的形式

以 - 符号连接起始IP地址,结束IP地址

 ################################

5.  导入,导出防火墙规则

5.1  导出规则

iptables-save

结合重定向输出>符号保存规则

示例:将防火墙规则导出到/lianxi/180_iptables.txt

iptables-save>/lianxi/180_iptables.txt

 ################################

5.2  导出规则

iptables-restore

结合重定向输入

iptables-restore

 ################################

5.3  如何开机自动加载我们的自定义防火墙规则?

1.在/etc/rc.local文件里写上执行

bash /lianxi/7-15/set_iptables.sh

2.iptables-restore

 ################################

5.4  练习

iptables='/usr/sbin/iptables'
每次执行脚本之前先清除iptables规则
$iptables -F
$iptables -t nat -F
1.允许192.168.0.0/24网段的主句访问本机的web,ftp,ssh,mysql服务
$iptables -A INPUT -s 192.168.0.0/24 -m multiport -p tcp --dport 80,21,22,3306 -j ACCEPT
也可以一次只添加一条
#$iptables -A INPUT -s 192.168.0.0/24  -p tcp --dport 80 -j ACCEPT
#$iptables -A INPUT -s 192.168.0.0/24  -p tcp --dport 21 -j ACCEPT
#$iptables -A INPUT -s 192.168.0.0/24  -p tcp --dport 22 -j ACCEPT
#$iptables -A INPUT -s 192.168.0.0/24  -p tcp --dport 3306 -j ACCEPT
2.允许任何人都可以访问本机的dns服务
$iptables -A INPUT -p udp --dport 53  -j ACCEPT
3.禁止其他电脑ping本机,但是本机可以ping其他电脑
$iptables -A INPUT -p icmp --icmp-type 8 -j DROP
如果不加这条规则reply包是回不来的,因为没有规则符合,
他就会走默认规则,下面我们设置了默认规则DROP
$iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
5.将访问本机的80端口和icmp的数据包都记录到一个文件/var/log/icmp_http.sh文件里面
$iptables -A INPUT -p tcp --dport 80 -j LOG --log-prefix=wangshenghu --log-level 4
$iptables -A INPUT -p icmp -j LOG --log-prefix=xiaowang --log-level 4
6.所有的规则生效后要保存,同时如果重启机器,希望也是生效的。
4.将INPUT链规则设置为DROP
$iptables -P INPUT DROP
$iptables -P INPUT ACCEPT

 ################################ 

5.5  开启路由转发功能

方法1:/sbin/sysctl -w net.ipv4.ip_forward=1

方法2:echo 1>/proc/sys/net/ipv4/ip_forward 临时

方法3:修改/etc/sysctl.conf,设置net.ipv4.ip_forward=1 永久

然后让内核重新加载参数

sysctl -p

查看模块

使用 lsmod 命令查看内核模块

加载模块

使用 modprobe加载模块

 ################################

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值