前言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 ~]#
備註: 阻塞,即不讓通行的意思