centos6 iptables
centos7 firewalld
目前企业中将firewalld卸载,然后yum下载iptables使用。
补充:阿里云搭建博客过程
- 购买云主机
- lnmp架构
- 进行域名和公网IP相关联
- 申请备案
防火墙
防火墙种类及使用说明
- 硬件
三层路由 深信服 Juniper - 软件
iptables firewalld 云防火墙
阿里云:安全组
必须熟悉的名词
容器: 瓶子 罐子 存放东西
表(table): 存放链的容器
链(chain): 存放规则的容器
规则(policy): 准许或拒绝规则
表----->链----->规则
Netfilter | 表(tables) | 链(chains) | 规则(Policy) |
---|---|---|---|
一栋楼 | 楼里的房子 | 房子里的柜子 | 柜子里衣服,摆放规则 |
iptables执行的过程
- 防火墙是层层过滤的,实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
- 如果匹配上规则,即明确表示是阻止还是通过,数据包就不再向下匹配新的规则。
- 如果规则中没有明确表明是阻止还是通过的,也就是没有匹配规则,向下进行匹配,直到匹配默认 规则得到明确的阻止还是通过。
- 4.防火墙的默认规则是所有规则执行完才执行的。(默认式通过的)
image
iptables表与链
- 4表伍链(链大写)
表 | 功能 |
---|---|
filter | 实现防火墙功能的: 准许或拒绝(端口 ip) |
nat | 网络地址转发 1.共享上网 2.端口/ip映射 |
raw | 了解即可 |
mangle | 了解即可 |
filter表(默认) | 强调:主要和主机自身相关,真正负责主机防火墙功能的(过滤流入流出主机的数据包) filter表示iptables默认使用的表,这个表定义了三个链(chains 企业工作场景:主机防火墙 |
---|---|
INPUT | 负责过滤所有目标地址是本机地址的数据包 通俗来说:就是过滤进入主机的数据包。 |
FORWARD | 负责转发流经主机的数据包。起转发的作用,和NAT关系很大,可实现LVS NAT模式,(需配置net.ipv4.ip_forward=0) |
OUTPUT | 处理所有源地址是本机地址的数据包 通俗的讲:就是处理从主机发出去的数据包 |
image
image
nat表和filter表执行过程
环境准备
[root@m01 ~]# rpm -qa iptables
iptables-1.4.21-28.el7.x86_64 #默认系统是安装的。
[root@m01 ~]# rpm -ql iptables
/usr/sbin/ip6tables
/usr/sbin/ip6tables-restore
/usr/sbin/ip6tables-save
/usr/sbin/iptables #iptables管理命令
/usr/sbin/iptables-restore
/usr/sbin/iptables-save
/usr/sbin/xtables-multi
/usr/share/man/man1/iptables-xml.1.gz
/usr/share/man/man8/ip6tables-restore.8.gz
/usr/share/man/man8/ip6tables-save.8.gz
/usr/share/man/man8/ip6tables.8.gz
/usr/share/man/man8/iptables-extensions.8.gz
/usr/share/man/man8/iptables-restore.8.gz
/usr/share/man/man8/iptables-save.8.gz
/usr/share/man/man8/iptables.8.gz
注意:默认的软件无法systemctl启动。
- 安装软件
yum install -y iptables-services
[root@m01 ~]# rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables #防火墙的配置文件
/usr/lib/systemd/system/iptables.service #防火墙服务配置文件(命令,并可开机自启)
- 防火墙相关模块 加载到内核中
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
- 验证是否加入到内核当中
[root@m01 ~]# lsmod |egrep 'filter|nat|ipt'
xt_nat 12681 2
ipt_REJECT 12541 0
nf_reject_ipv4 13373 1 ipt_REJECT
nf_nat_ftp 12770 0
nf_conntrack_ftp 18638 1 nf_nat_ftp
iptable_nat 12875 1
nf_nat_ipv4 14115 1 iptable_nat
nf_nat 26787 3 nf_nat_ftp,nf_nat_ipv4,xt_nat
nf_conntrack 133095 7 nf_nat_ftp,nf_nat,xt_state,nf_nat_ipv4,xt_conntrack,nf_conntrack_ftp,nf_conntrack_ipv4
iptable_filter 12810 0
ip_tables 27126 2 iptable_filter,iptable_nat
libcrc32c 12644 3 xfs,nf_nat,nf_conntrack
防火墙太成熟了,已经放入内核。
-
查看当前的规则,默认是filter表
image
iptables的应用
- iptables的参数
命令 | 参数 | 作用 |
---|---|---|
iptables | F | 清空防火墙 默认数filter表 |
x | 清空用户自定义的链 | |
z | (数据包计数器与数据包字节计数器) | |
nL | 查看当前规则 | |
iptables-save | 保存规则 并输出屏幕 | |
iptables-restore | 通过< 读取防火墙配置文件 并生效 | |
-L | 显示表中的所有规则 | |
-n | 不要把端口 或ip反向解析为 名字 | |
-t | 指定表 | |
-A | append 追加 规则追加到最后 准许 ACCEPT | |
-D | delete 删除 | |
-I (大写字母I) | insert 拒绝的规则放在最上面 | |
-p | protocal 协议 tcp/udp/icmp/all/ ….. | |
–dport | 目标端口 | |
–sport | 源端口 | |
-d | –destination目标ip | |
-s | -–source 源ip | |
-m | 指定模块 | |
-i | --input 数据通过哪个接口(网卡) 进来 | |
-o | --ouput 数据通过哪个接口(网卡) 出去 | |
-j | jump 满足条件后的动作 : DROP(拒绝)/ACCEPT(准许)/REJECT(拒绝) |
- 禁止所有人访问22端口
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
会将断开自己的远程连接。解决方法:书写定时任务,清空防火墙策略,或者放行22端口。
- 禁止网段连入(禁止10.0.0.0网段访问 8080端口)
- 实现
nc 创建端口,telnet测试端口,并通过安全策略禁止10.0.0.0 8080访问。
[root@m01 ~]# nc -kl 8888
dsad
dsad
sad
asd
ad
可实现实时通讯的功能
[root@web01 ~]# telnet 10.0.0.61 8888
Trying 10.0.0.61...
Connected to 10.0.0.61.
Escape character is '^]'.
dsad
dsad
sad
asd
ad
进行限制
iptables -I INPUT -s 10.0.0.0/24 -p tcp --dport 8080 -j DROP
- 白名单: 只允许指定网段连入(允许10.0.0.0网段)
iptables -I INPUT ! -s 10.0.0.0/24 -jDROP
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- !10.0.0.0/24 0.0.0.0/0 #只允许10.0.0.0的网段访问。
DROP tcp -- 192.168.20.0/24 0.0.0.0/0
tcp dpt:8888
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
- 允许某个网段的多个端口访问
[root@m01 ~]# iptables -I INPUT -p tcp -m multiport ! --dport 80,443 -j DROP
[root@m01 ~]# iptables -I INPUT -p tcp --dport
1024:65535 -j DROP
查看当前防火墙规则表
image
- 通过防火墙规则 控制是否可以ping
iptables -I INPUT -p icmp --icmp-type any -j DROP
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
8代表icmp的一种类型(请求码)
测试:可用treatroute追踪路由或者telnet查看端口,也可以用curl查看七层的内容
- 匹配网络状态(TCP/IP连接状态)
-m state –state
NEW:已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动的新连接
INVALID:非法或无法识别的
iptables -A INPUT -m state -–state ESTABLISHED,RELATED
-j ACCEPT
iptables -A OUTPUT -m state -–state ESTABLISHED,RELATED
-j ACCEPT
- 限制并发(发送数据)及速率
-m limit –limit n/{second/minute/hour}:
解释:指定时间内的请求速率(”n”为速率),后面为时间分别为:秒 分 时
–limit-burst [n]
解释:在同一时间内允许通过的请求(”n”为数字),不指定默认为5
# 每分钟内最多发送6次数据包 最多并发发送5个
-m limit --limit 6/min --limit-burst 5
防火墙规则的保存与恢复
- 防火墙的配置文件
[root@m01 ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-configfirewall
# please do not ask us to add additional
ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j
ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22
-j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-hostprohibited
COMMIT
cp /etc/sysconfig/iptables {,.bak} #先将防火墙配置文件备份
iptables-save > /etc/sysconfig/iptables 将当前的防火墙配置导入到防火墙中
systmectl restart iptables #重启防火墙使之生效
- 恢复防火墙规则
背景:
防火墙配置无意中被清空
恢复
iptables-restore < /etc/sysconfig/iptables
实际生产用法
默认是拒绝 去电影院
准许哪些ip或端口通过
修改默认规则为DROP
- ssh可以连接进来
[root@m01 ~]# iptables -F
[root@m01 ~]# iptables -X
[root@m01 ~]# iptables -Z
[root@m01 ~]#iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
tcp dpt:22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
- 设置允许本机lo通讯规则
允许本机回环lo接口数据流量流出与流入
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
- 配置默认规则及 放行 80 443端口
[root@m01 ~]# iptables -P INPUT DROP
[root@m01 ~]# iptables -P FORWARD DROP
[root@m01 ~]# iptables -P OUTPUT ACCEPT
#将默认规则都拒绝掉
[root@m01 ~]# iptables -A INPUT -m multiport -p tcp
--dport 443,80 -j ACCEPT
[root@m01 ~]#iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
multiport dports 443,80
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
- 接受本网段访问
[root@m01 ~]# iptables -A INPUT -s 10.0.0.0/24 -j
ACCEPT
[root@m01 ~]# iptables -A INPUT -s 172.16.1.0/24 -j
ACCEPT
- 将配置规则导入到配置文件中
[root@m01 ~]# iptables-save
# Generated by iptables-save v1.4.21 on Wed Jul 24
23:42:00 2019
[root@m01 ~]# iptables-save
# Generated by iptables-save v1.4.21 on Wed Jul 24
23:42:00 2019
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [24:3008]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m multiport --dports 443,80 -j ACCEPT
-A INPUT -s 10.0.0.0/24 -j ACCEPT
-A INPUT -s 172.16.1.0/24 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
# Completed on Wed Jul 24 23:42:00 2019
NAT共享上网
共享上网- 访问百度时,将源ip10.0.0.7的IP地址进行了转换成了10.0.0.61
- 因为10.0.0.61可以访问外网,所以访问百度时,百度网站可以给10.0.0.61进行回包。
- 10.0.0.61 通过arp+内核转发从而找到10.0.0.7完成数据回包。
防火墙配置:
- 还原默认的拒绝的策略
[root@m01 ~]# iptables -P INPUT ACCEPT
[root@m01 ~]# iptables -P FORWARD ACCEPT
- 配置内网上网SNAT和内核转发功能
[root@m01 ~]# iptables -t nat -A POSTROUTING -s
172.16.1.0/24 -j SNAT --to-source 10.0.0.61
[root@m01 ~]# echo 'net.ipv4.ip_forward = 1' >>
/etc/sysctl.conf
[root@m01 ~]# sysctl -p
net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.ip_forward = 1
公网ip不固定:
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j
MASQUERADE
MASQUERADE #伪装
web01配置
- 网卡配置
[root@web01 ~]# cat /etc/sysconfig/networkscripts/
ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=no
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=10.0.0.254
DNS1=223.5.5.5
GATEWAY=10.0.0.254
[root@web01 ~]# cat /etc/sysconfig/networkscripts/
ifcfg-eth1
TYPE=Ethernet
IPADDR=172.16.1.7
PREFIX=24
NAME=eth1
DEVICE=eth1
ONBOOT=yes
GATEWAY=172.16.1.61
DNS1=1.2.4.8
重启网络服务 systemctl restart network
- 远程并测试
ssh 172.16.1.7
[root@web01 ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148):
icmp_seq=1 ttl=127 time=8.90 ms
64 bytes from 220.181.38.148 (220.181.38.148):
icmp_seq=2 ttl=127 time=7.52 ms
64 bytes from 220.181.38.148 (220.181.38.148):
icmp_seq=3 ttl=127 time=9.28 ms
64 bytes from 220.181.38.148 (220.181.38.148):
icmp_seq=4 ttl=127 time=9.36 ms
[root@web01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.1.61 0.0.0.0 UG 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
实现端口转发
image访问10.0.0.61:9000 ----->172.16.1.7:22
也需要配置内核转发功能
[root@m01 ~]# iptables -t nat -A PREROUTING -d
10.0.0.61 -p tcp --dport 9000 -j DNAT --todestination 10.0.0.7:22
[root@m01 ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 10.0.0.61 tcp dpt:9000 to:172.16.1.7:22
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 172.16.1.7 0.0.0.0/0 to:10.0.0.61
- 测试与检查
本地shell中
测试
注意:1.开启内核转发 2. 内网的网关为防火墙
- 实现ip映射
ip a add 10.0.0.62/24 dev eth0 label eth0:0 #添加一个ip
必须要有label 否则ifconfig会无法识别。
[root@m01 ~]# iptables -t nat -A PREROUTING -d10.0.0.62 -j DNAT --to-destination 172.16.1.7
用的较少,公网的资源有限。
网友评论