前言python
管理防火墙工具mysql
1.图形化管理工具 firewall-configlinux
2.命令管理工具 iptables(操做复杂) firewall-cmdweb
iptables 只是linux防火墙管理工具而已,真正实现防火墙功能的是netfilter,咱们配置了iptables规则后netfilter经过这些规则来进行防火墙过滤等操做sql
netfilter模块:docker
它是主要的工做模块,位于内核中,在网络层的五个位置(也就是防火墙四表五链中的五链)注册了一些函数,用来抓取数据包;把数据包的信息拿出来匹配各个链位置在对应表中的规则:匹配以后,进行相应的处理accept、drop等等。shell
下面这张图很明了的说明了netfilter和iptables之间的关系apache
只要有操做系统,就有防火墙centos
accept 赞成 drop 拒绝api
网络接口层从某种意义上是二层的mac
链就是执行的步骤:共有五个
进路由(prerouting)、
进系统(input)、
转发(forward)、
出系统(output)、
出路由(postrouting);
表就是存储的规则:数据包到了该链处,会去对应表中查询设置的规则,而后决定是否放行、丢弃、转发仍是修改等等操做。
表——————链————规则
一个表——一个表当中包含多个链——一个链当中包含多个规则
进路由、出路由 地址转换时用
硬件防火墙效果大于软件防火墙,软件防火墙只是多一道防御
五链 | raw表(打标记) | mangle表(修改数据包类型) | nat表(地址转换) | filter表(过滤数据包) |
---|---|---|---|---|
PREROUTING链(进路由) | - | - | - | |
INPUT链(进系统) | - | - | ||
FORWARD链(数据包转发) | - | - | ||
OUTPUT链(出系统) | - | - | - | - |
POSTROUTING链(出路由) | - | - |
iptables [ -t 表名 ] 选项 [ 链名 ] [ 条件 ] [ -j 控制类型 ]
iptables 经常使用参数
-P 设置默认策略:iptables
-P INPUT (DROP | ACCEPT)
-F 清空规则链
-L 查看规则链
-A 在规则连的末尾插入新的规则
-I num 在规则连的头部加入新规则 //大写的i
-D num 删除某一条规则
-s 匹配来源地址IP/MASK,加叹号“!”表示除这个IP外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议,如tcp,udp,icmp
--dport num 匹配目标端口号
--sport num 匹配来源端口号
-I 规则链 -i网卡 -p 协议 --dport 目标端口 -s 源地址 -j 操做
iptables -F 清空默认的filters表
想要清空指定的表,须要-t指定表
一般防火墙的接口在两个或者两个以上,针对用途的不一样能够将接口划分为不一样的区域,针对区域去进行管理
linux6与linux7 的网卡名称不一样————linux6 的网卡名称 eth0
区别 | linux6 | linux7 |
---|---|---|
网卡名称 | eth0 | ens33 |
防火墙 | iptables | firewalld,iptables |
在防火墙中,发挥做用的是netfilter(内核态),不能够直接管理,只能间接管理,使用firewalld或者iptabvles
daemon 指的是进程
firewall-config 是图形化工具
firewall-cmd 是字符命令
使用工具或者操做去管理进程和服务
进程服务去控制封装的iptables命令,间接的去管理内核中的netfiler
真正能跟netfilter交互的是iptables,firewall去管理iptables
四表五链也就在iptables(command)中
工做原理体系图
区别 | firewalld | iptables |
---|---|---|
配置文件 | /usr/lib/firewalld/ /etc/firewalld | etc/firewalld/etc/sysconfig/iptables |
对规则的修改 | 不须要所有刷新策略,不丢失现行链接 | 须要所有刷新策略,丢失链接 |
防火墙类型 | 动态防火墙 | 静态防火墙 |
区域 | 描述 |
---|---|
drop(丢弃) | 任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络链接 |
block(限制) | 人和接受的网络链接都被IPv4的imcp-host-prohibited 信息和IPv6的icmp6-adm-prohibited 信息所拒绝 |
public(公共) | 在公共区域内使用,不能详细网络内的其余计算机不会对您的计算机形成危害,只能接收通过选取的内容 |
external(外部) | 特别是为路由器启用了假装功能的外部网。您不能信任来自网络的其余计算机,不能相信他们不会对您的计算机形成危害,只能接收通过选择的连接 |
dmz(非军事区) | 用于您的非军事区内的电脑,此区域内可公开访问,能够有限的进入您的内部网络,仅仅接收通过选择的连接 |
work(工做) | 用于工做区。您能够基本详细网络内的其余电脑不会危害您的电脑。仅仅接收通过选择的连接 |
home(家庭) | 用于家庭网络。您能够基本信任网络内的其余计算机不会危害您的计算机。仅仅接收通过选择的连接 |
internal(内部) | 用于内部网络。您能够基本上信任网络内的其余计算机不会威胁您的计算机。仅仅接收通过选择的链接 |
trusted(信任) | 可接受全部的网络链接 |
public 公共区域,默认全部端口放在这个区域
默认规则,高安全级别能够访问低安全级别,若是让外网访问内网,须要专门修改规则
好比内网的安全级别为100,外网的安全级别为0,则
内网能够访问外网,外网不能够访问内网
把提供对外服务的服务器放到内网,若是想要让外网能够访问内网,须要设置对应的配置,可是安全性方面存在隐患
因而就出现了dmz(非军事区),将dmz安全级别设置为50,将提供对外服务的服务器划分到dmz,这样安全性方面就获得了提升
可是这样也有以dmz为跳板,去访问内网的状况,为了更安全,背靠背堡垒主机的模式出现,即再加入一台硬件防火墙,将新的一台防火墙放到内网与当前硬件防火墙之间,起到隔绝的做用
即,第一道防火墙挡在外面,后面分两道线,一道是对外服务器,一道是内网,在内网的位置再添加第二道防火墙,以保护内网主机安全
能够在第一道防火墙启用流量监控清晰,实时分析数据,针对性去过滤可疑数据,
还能够在第一道防火墙放置一个蜜罐
经常使用的区域为:
public
external
dmz
internal
block 是阻塞模式
工做和家庭是自定义区域
低安全通向高安全须要设置入站规则
默认状况下,ens33绑定在公共区域(public)中
iptables没有区域的概念
iptables是针对四表五链设置规则
备注:
在业务比较繁忙时,不要设置永久配置,先设置运行时配置,不会中断原有业务,等到夜晚不繁忙的时候,再设置永久配置
被激活的区域(区域内有网卡存在)名会被黑色加粗;先指定要修改的区域,而后去修改服务等子选项,这些配置只对在该区域内的网卡生效
[root@localhost ~]# rpm -q httpd httpd-2.4.6-90.el7.centos.x86_64 [root@localhost ~]# firewall-config
C:\Users\GSY>ping 192.168.247.142 正在 Ping 192.168.247.142 具备 32 字节的数据: 来自 192.168.247.142 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.247.142 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.247.142 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.247.142 的回复: 字节=32 时间<1ms TTL=64 192.168.247.142 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms
关闭icmp的请求选项
C:\Users\GSY>ping 192.168.247.142 正在 Ping 192.168.247.142 具备 32 字节的数据: 来自 192.168.247.142 的回复: 没法访问目标主机。 来自 192.168.247.142 的回复: 没法访问目标主机。 来自 192.168.247.142 的回复: 没法访问目标主机。 来自 192.168.247.142 的回复: 没法访问目标主机。 192.168.247.142 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
[root@localhost ~]# ssh root@192.168.247.142 The authenticity of host '192.168.247.142 (192.168.247.142)' can't be established. ECDSA key fingerprint is SHA256:dXWxtS2ShXQgfb7R672V7+l3i7rGqHBbIB5MTcFnAws. ECDSA key fingerprint is MD5:59:fb:20:f0:28:96:5e:14:90:82:63:c9:ae:67:d6:e9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.247.142' (ECDSA) to the list of known hosts. root@192.168.247.142's password: Last login: Mon Dec 9 13:42:31 2019 from 192.168.247.1 [root@localhost ~]# exit 登出 Connection to 192.168.247.142 closed. [root@localhost ~]# ssh root@192.168.247.142 ssh: connect to host 192.168.247.142 port 22: No route to host [root@localhost ~]# [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.247.143 netmask 255.255.255.0 broadcast 192.168.247.255 inet6 fe80::e2c1:c26d:afa1:a4ad prefixlen 64 scopeid 0x20<link> ether 00:0c:29:52:4d:89 txqueuelen 1000 (Ethernet) RX packets 602 bytes 49107 (47.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 227 bytes 24670 (24.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ssh root@192.168.247.142 root@192.168.247.142's password: Last login: Mon Dec 9 14:05:12 2019 from 192.168.247.1 [root@localhost ~]#
[root@localhost ~]# systemctl start httpd [root@localhost ~]# netstat -natp | grep 'httpd' tcp6 0 0 :::80 :::* LISTEN 119597/httpd [root@localhost ~]#
[root@localhost ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 一 2019-12-09 09:26:39 CST; 5h 14min ago Docs: man:firewalld(1) Main PID: 803 (firewalld) CGroup: /system.slice/firewalld.service └─803 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... 12月 09 14:10:14 localhost.localdomain firewalld[803]: WARNING: COMMAND_FAILED: '/usr/sb.... Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# firewall-cmd --state running [root@localhost ~]#
[root@localhost ~]# firewall-cmd --reload success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --complete-reload success [root@localhost ~]#
[root@localhost ~]# systemctl restart firewalld
[root@localhost ~]# firewall-cmd --get-zones block dmz drop external home internal public trusted work [root@localhost ~]#
[root@localhost ~]# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp open*** ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server [root@localhost ~]#
[root@localhost ~]# firewall-cmd --get-icmptypes address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option [root@localhost ~]#
[root@localhost ~]# firewall-cmd --get-default-zone public [root@localhost ~]#
[root@localhost ~]# firewall-cmd --set-default-zone=internal success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --get-active-zones public interfaces: ens33 [root@localhost ~]#
激活的条件 : 区域至少关联一个接口或一个源地址/网段
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 public [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --add-interface=ens33 The interface is under control of NetworkManager, setting zone to 'work'. success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --change-interface=ens33 The interface is under control of NetworkManager and already bound to 'work' The interface is under control of NetworkManager, setting zone to 'work'. success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --remove-interface=ens33 The interface is under control of NetworkManager, setting zone to default. success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --query-interface=ens33 no [root@localhost ~]#
[root@localhost ~]# firewall-cmd --list-all-zones block target: %%REJECT%% icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: dmz target: default icmp-block-inversion: no interfaces: sources: services: ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: drop target: DROP icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: external target: default icmp-block-inversion: no interfaces: sources: services: ssh ports: protocols: masquerade: yes forward-ports: source-ports: icmp-blocks: rich rules: home target: default icmp-block-inversion: no interfaces: sources: services: ssh mdns samba-client dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: internal (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh mdns samba-client dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client http ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: trusted target: ACCEPT icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: work target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --list-all internal (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh mdns samba-client dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~]#
[root@localhost ~]# firewall-cmd --list-all internal (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh mdns samba-client dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --list-services ssh mdns samba-client dhcpv6-client
[root@localhost ~]# firewall-cmd --zone=public --add-service=smtp success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --remove-service=ssh success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --query-service=ssh no [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --list-ports
[root@localhost ~]# firewall-cmd --zone=internal --add-port=22/tcp --timeout=5m success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --remove-port=22/tcp success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=internal --query-port=22/tcp no [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --list-icmp-blocks [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --add-icmp-block=echo-request success [root@localhost ~]# [root@localhost ~]# firewall-cmd --zone=work --list-icmp-blocks echo-request [root@localhost ~]# firewall-cmd --zone=work --add-icmp-block=echo-reply success [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --remove-icmp-block=echo-reply success [root@localhost ~]# firewall-cmd --zone=work --list-icmp-blocks echo-request [root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=work --query-icmp-block=echo-request yes [root@localhost ~]#
备注: 阻塞,即不让通行的意思