linux 防火墙
介绍
【千锋】Linux视频教程-4小时弄懂什么是防火墙
https://www.bilibili.com/video/BV197411b7Ve?p=1
共4个小时,15集
20210228 study
第一集:iptables & firewald
控制服务器与网络流量的
iptables : centOS6 中
firewald: centOS6 , centOS7 中都有 新一代的防火墙
网络防火墙: 思科ASA5550/ 华为USG6650 (报价40K)
应用安全防火墙:WAF(WebApplicationFirewall)
防毒墙:趋势科技、国信冠群YORTON-2000
上网行为管理器: 精细的区别流量来源,区分游戏与工作,
入侵检测: IDS,IPS (供应商有 绿盟、华为、天融信、启明星辰)
传输安全: VPN 虚拟专用网 (使用到了 隧道技术----通信被加密了)
举例:
公司老板要请保镖,此老板到中南海开会还需要保镖吗?不需要!!
计算机内部不要开启防火墙,因为网络设备会有更高级的防护,不需要自己设置!!
如果服务器出于外网,需要开启防火墙!
iptables:主机型、网络型
主机型: 此机器为服务器,开启防火墙是为了保护我自己
网络型:允许和放行经过它的数据。
防火墙有netfilter / iptables ,可以有效控制进出服务器的流量,和流经服务器的流量。
/sbin/iptables 可以配置iptables防火墙。
第三集
应表会传网链物
7 6 5 4 3 2 1
四表五列:

四表,五列,多条策略



表像安保小队, 列像安保队员。
一个表有多个链条,一个链条有多个策略。
四表:filter (过滤) / nat (装换) / mangle(碾压) / raw (生的)
主机型用filter , 网络型 用 nat ,其他的很少用
五列:input, output, forward , preRouting , postRouting
链条: 链安防员
策略:
----192.168.0.100 drop
----192.168.0.100 accept
第四章
表应用顺序:
raw–> mangle --> nat --> filter
链表的顺序:
PREROUTING --> INPUT --> OUTPUT --> POSTROUTING
PREROUTING --> FORWARD --> POSTROUTING
iptables
语法:

表 – 链 – 策略
表是小队,链式队员,队员还要有策略的处理事情方式 。
iptables -t 表名 管理选项 [链名] [匹配条件] [-j 控制类型]
-t 表名:四个表(raw mangle nat filter) 中的一个,不写 默认是filter表。
管理选项:操作方式 如 插入(I)、删除(D)、查看等。
链名: INPUT / FORWARD / OUTPUT 等
匹配条件:数据包特征 ip , port ,p (协议) 等
控制类型:数据包的处理方式 , ACCEPT允许,REJECT拒绝,DROP丢弃,等(拒绝会说一声,有返回值, 丢弃就是不理,没有返回值)
示例:
iptables -t 表名 管理选项 [链名] [匹配条件] [-j 控制类型]
本机: IPv4 地址 . . . . . . . . . . . . : 192.168.1.16
虚拟主机: inet 192.168.121.31
示例 查看
iptables -L 查阅当前iptables所有表 ,默认filter 表
iptables -L == iptables -L -t filter
iptables -L -t nat
示例 : 安装 http网站服务 ftp 文件传输服务
--安装 sftp
[root@firewall01 ~]# yum install -y vsftpd
---启动 ftp http
[root@firewall01 ~]# systemctl status httpd
[root@firewall01 ~]# systemctl status vsftpd
--- 安装 http
root@firewall01 opt]# yum install -y httpd
--- 启动 http
[root@firewall01 opt]# systemctl status httpd
[root@firewall01 opt]# systemctl start httpd
[root@firewall01 opt]# systemctl status httpd
--- 测试 http
[root@firewall01 opt]# curl http://192.168.121.31
基础示例: 清空
[root@firewall01 ~]# iptables -X
[root@firewall01 ~]# iptables -F
[root@firewall01 ~]# iptables -Z
[root@firewall01 ~]# iptables -L -n
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:配置不被ping
禁止自己被别人PING
《 icmp协议定义了ping这个命令 》
root@firewall01 opt]# iptables -t filter -I INPUT -p icmp -j REJECT
root@firewall01 opt]# iptables -L -t filter
再用 其他机器ping 看能否ping通
示例3:删除规则
禁止自己被别人PING
《 icmp协议定义了ping这个命令 》
root@firewall01 opt]# iptables -t filter -D INPUT -p icmp -j REJECT
root@firewall01 opt]# iptables -L -t filter
再用 其他机器ping 看能否ping通
第七章
细化匹配条件: ip port protocol
原因是,腾讯有 学习,有娱乐,要区分,不能只区分ip port ,一禁都禁不好。

示例4:开启一个网站,指定某台机器不可以访问

配置机器 192.168.121.31 (firewall01)
测试机器 192.168.121.32 (firewall02)
root@firewall01 opt]# yum install -y httpd
[root@firewall01 opt]# systemctl status httpd
[root@firewall01 opt]# systemctl start httpd
[root@firewall01 opt]# systemctl status httpd
root@firewall01 opt]# iptables -L -t filter
[root@firewall01 opt]# iptables -I INPUT -s 192.168.121.32 -j REJECT
root@firewall01 opt]# iptables -L -t filter
yum install -y httpd ---- 安装
systemctl start httpd – 开启
windows 平台测试
192.168.1.16 访问 192.168.121.31

linux 平台测试
192.168.121.32 访问 192.168.121.31
[root@firewall02 ~]# curl 192.168.121.31
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Bootstrap -->
<link href="/noindex/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="noindex/css/open-sans.css" type="text/css" />
<style type="text/css">
...
...
...
禁用 32 访问 31 后
[root@firewall01 opt]# iptables -I INPUT -s 192.168.121.32 -j REJECT
[root@firewall01 opt]#
192.168.121.32 访问 192.168.121.31
[root@firewall02 ~]# curl 192.168.121.31
curl: (7) Failed connect to 192.168.121.31:80; Connection refused
执行如下指令,通过xshell 都不能访问 192.168.121.31 这台主机了。
因为所有121网段的访问都被reject 了,只能登陆 31 删除规则
[root@firewall01 opt]# iptables -I INPUT -s 192.168.121.0/24 -j REJECT
[root@firewall01 opt]#
使用VMware
iptables -L
iptables -L -n 查看配置情况

iptables -x
iptables -F
iptables -Z 清空配置 ,
[root@firewall01 ~]# iptables -X
[root@firewall01 ~]# iptables -F
[root@firewall01 ~]# iptables -Z
[root@firewall01 ~]# iptables -L -n
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

示例5: 谁也不能访问
执行后xshell 就不能执行了 ,因为把自己也禁用了
[root@firewall01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:1e:b6:f1 brd ff:ff:ff:ff:ff:ff
inet 192.168.121.31/24 brd 192.168.121.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::a2cd:d26a:151d:8896/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:1d:c0:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:1d:c0:15 brd ff:ff:ff:ff:ff:ff
[root@firewall01 ~]#
[root@firewall01 ~]#
[root@firewall01 ~]# iptables -I INPUT -i ens33 -j DROP
示例6:配置不让 A 访问 B tcp 协议的 80 端口

可以访问网站,不可以访问FTP
--安装 sftp
[root@firewall01 ~]# yum install -y vsftpd
---启动 ftp http
[root@firewall01 ~]# systemctl status httpd
[root@firewall01 ~]# systemctl status vsftpd
[root@firewall01 ~]# 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
---- 不让 192.168.121.32 访问 192.168.121.31 tcp 协议的 80 端口
[root@firewall01 ~]# iptables -I INPUT -s 192.168.121.32 -p tcp --dport 80 -j DROP
[root@firewall01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 192.168.121.32 anywhere tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@firewall01 ~]#

20210228 – 第七个视频完结
8 todu
https://www.bilibili.com/video/BV197411b7Ve?p=8
示例7: 自己可以ping 别人 ,反过来不可以
121.31 上面设置 及 测试
--- icmp 8 发送
--- icmp 0 响应
---- 121.31 上面 配置 icmp 8 的 DROP
[root@firewall01 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@firewall01 ~]# ping 192.168.121.32 ---可以PING通
121.32 上面测试
[root@firewall02 ~]# ping 192.168.121.31 ---无法PING通
第9集
安全界原则: 没有明确的允许皆为拒绝
第10集
iptables 网络型配置 学习
nat: network address transaction 网络地址转换



第11集
-------------firewall02 的配置 -------------
/etc/sysconfig/network-scripts/ifcfg-ens33
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.100.10
NETMASK=255.255.255.0
GATEWAY=192.168.100.20
注意此处配置的是 20 作为gateway
-------------firewallGateWay 的配置 -------------
/etc/sysconfig/network-scripts/ifcfg-ens33
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.100.20
NETMASK=255.255.255.0
注意此处没有GATEWAY 的配置
/etc/sysconfig/network-scripts/ifcfg-ens37
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens37"
DEVICE="ens37"
ONBOOT="yes"
IPADDR=192.168.200.10
NETMASK=255.255.255.0
-------------firewall03 的配置 -------------
/etc/sysconfig/network-scripts/ifcfg-ens33
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.200.20
NETMASK=255.255.255.0
GATEWAY=192.168.100.10
1. 配置完成后 100 及 200 网段 机器相互ping 一下
systemctl restart network
ip a
- 启动路由转发功能 在 gateway 那台双网卡机器上面 执行

默认ip_forward 没有开启
[root@localhost sysctl.d]# cat /usr/lib/sysctl.d/50-default.conf
[root@localhost sysctl.d]# grep ip_forward /usr/lib/sysctl.d/50-default.conf
[root@localhost sysctl.d]# sysctl -a | grep ip_forward
更改方式1:永久更改内核
[root@localhost sysctl.d]# echo "net.ipv4.ip_forward = 1" >> /usr/lib/sysctl.d/50-default.conf
---重启后查看
reboot
[root@localhost sysctl.d]# sysctl -a | grep ip_forward
更改方式2:临时更改内核
[root@localhost sysctl.d]# echo 1 > /proc/sys/net/ipv4/ip_forward
-- 查看
[root@localhost sysctl.d]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1 表名此机器就是路由器了
[root@localhost sysctl.d]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.ens37.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"
[root@localhost sysctl.d]#

在 gateway 表配置如下内容
--- 当100.0 的数据 经过我ens37网卡 出去时候 ,将源地址装换为 192.168.200.10
[root@localhost sysctl.d]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens37 -j SNAT --to-source 192.168.200.10
firewall
第13集
前12章讲 iptables ,13,14,15 讲 firewalld
firewalld 讲zone (zone 等同于 iptables 的四表五列 )
记住原则 : ip > 接口(网卡) > 默认规则

区域zong有
trusted
home/internal
work
public ------ 默认使用public
external
dmz
block
drop
常用端口
http 80
https 443
ftp 20 21
dhcp 67 68
mail 25 110 142
mysql 3306
看到 第13章第5分钟,后面会做这个测试,暂时没有做 20210302
https://www.bilibili.com/video/BV197411b7Ve?p=13





1.准备服务器用于测试,观察默认区域
2.观察所有区域
3.观察 10.0.107.230 用户访问服务器,防火墙是否会阻拦
4.将public 默认区域,更改为trusted区域,用户立刻可以访问所有服务
5.添加Http规则进入默认区域public
6.
----
---- [root@firewalld ~]# systemctl start firewalld 启动firewalld
---- [root@firewalld ~]# firewall-cmd --get-default-zone 显示默认区域
---- public
---- [root@firewalld ~]# firewall-cmd --list-all-zone 显示所有区域
---- [root@firewalld ~]# firewall-cmd --list-all-zone | wc -l
---- 135
---- [root@firewalld ~]# firewall-cmd --list-all-zone |more
----
---- [root@firewalld ~]# firewall-cmd --set tab 键模糊查询
---- [root@firewalld ~]# firewall-cmd --get tab 键模糊查询
---- [root@firewalld ~]# firewall-cmd --list tab 键模糊查询
---- [root@firewalld ~]# firewall-cmd --get-default-zone
---- [root@firewalld ~]# firewall-cmd --get-active-zones
---- [root@firewalld ~]# firewall-cmd --set-default-zone=trusted
---- [root@firewalld ~]#
---- [root@firewalld ~]# firewall-cmd --reload 立刻生效
---- [root@firewalld ~]# systemctl start httpd 启动 http
---- [root@firewalld ~]# systemctl status httpd 查看 http
---- [root@firewalld ~]#
---- [root@firewalld ~]# 在主机访问 Http://192.168.121.35 可以访问
---- [root@firewalld ~]# 永久的在public 区域添加一个http服务
---- [root@firewalld ~]# firewall-cmd --permanent --add-service=http --zone=public
---- [root@firewalld ~]# success
---- [root@firewalld ~]# [root@firewalld ~]# firewall-cmd --reload
---- [root@firewalld ~]# success
---- [root@firewalld ~]# firewall-cmd --list-all-zone
---- [root@firewalld ~]#
---- [root@firewalld ~]# firewall-cmd --permanent --remove-service=http --zone=public
---- [root@firewalld ~]# firewall-cmd --reload
---- [root@firewalld ~]# 只看 public 区域的状态
---- [root@firewalld ~]# firewall-cmd --list-all --zone=public
---- [root@firewalld ~]# 查看DMZ区域
---- [root@firewalld ~]# firewall-cmd --list-all --zone=dmz
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]# ip 优先级 大于 接口优先级
---- [root@firewalld ~]# ip 优先级 大于默认规则
---- [root@firewalld ~]# ip > 接口(网卡) > 默认规则
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]# ens33 网卡上的数据 走 dmz 区域
---- [root@firewalld ~]# firewall-cmd --permanent --add-interface=ens33 --zone=dmz
---- [root@firewalld ~]#
---- [root@firewalld ~]# 122 这台机器自动进入(192.168.121.35)dmz区域
---- [root@firewalld ~]# firewall-cmd --permanent --add-source=192.168.121.122/24 --zone=dmz
---- [root@firewalld ~]# success
---- [root@firewalld ~]# firewall-cmd --reload
---- [root@firewalld ~]# success
---- [root@firewalld ~]# firewall-cmd --list-all --zone=dmz
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]# firewall-cmd --permanent --add-service=http --zone=public
---- [root@firewalld ~]# firewall-cmd --reload
---- [root@firewalld ~]# firewall-cmd --list-all --zone=public
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]# tailf /var/log/messages 查看访问网站日志信息
----
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]# yum install -y elinks
---- [root@zookeeper ~]# elinks http://192.168.121.35
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]#
---- [root@firewalld ~]#

1万+

被折叠的 条评论
为什么被折叠?



