首頁 iptables教程linux-iptable防火墻- 基本認識

iptables實戰練習

運維派隸屬馬哥教育旗下專業運維社區,是國內成立最早的IT運維技術社區,歡迎關注公眾號:yunweipai
領取學習更多免費Linux云計算、Python、Docker、K8s教程關注公眾號:馬哥linux運維

Linux 防火墻的基本認識

2.1 Netfilter

Linux防火墻是由Netfilter組件提供的,Netfilter工作在內核空間,集成在linux內核中

Netfilter 是Linux2.4.x之后新一代的Linux防火墻機制,是linux內核的一個子系統。Netfilter采用模塊化設計,具有良好的可擴充性,提供擴展各種網絡服務的結構化底層框架。Netfilter與IP協議棧是無縫契合,并允許對數據報進行過濾、地址轉換、處理等操作

Netfilter官網文檔:https://netfilter.org/documentation/

2.2 防火墻工具介紹

2.2.1 iptables

由軟件包iptables提供的命令行工具,工作在用戶空間,用來編寫規則,寫好的規則被送往netfilter,告訴內核如何去處理信息包

[root@centos8 ~]#rpm -qi iptables
Name ? ? ?  : iptables
Version ? ? : 1.8.2
Release ? ? : 9.el8
Architecture: x86_64
Install Date: Wed 25 Sep 2019 09:29:06 PM CST
Group ? ? ? : Unspecified
Size ? ? ?  : 2050086
License ? ? : GPLv2 and Artistic 2.0 and ISC
Signature ? : RSA/SHA256, Tue 02 Jul 2019 06:50:00 AM CST, Key ID 05b555b38483c65d
Source RPM  : iptables-1.8.2-9.el8.src.rpm
Build Date  : Sat 11 May 2019 10:21:57 PM CST
Build Host  : x86-01.mbox.centos.org
Relocations : (not relocatable)
Packager ?  : CentOS Buildsys <bugs@centos.org>
Vendor ? ?  : CentOS
URL ? ? ? ? : http://www.netfilter.org/
Summary ? ? : Tools for managing Linux kernel packet filtering capabilities
Description :
The iptables utility controls the network packet filtering code in the
Linux kernel. If you need to set up firewalls and/or IP masquerading,
you should either install nftables or this package.
?
Note: This package contains the nftables-based variants of iptables and
ip6tables, which are drop-in replacements of the legacy tools.
?
?
[root@centos8 ~]# iptables --version
iptables v1.8.2 (nf_tables)
[root@centos8 ~]#ll /usr/sbin/iptables
lrwxrwxrwx. 1 root root 17 May 11  2019 /usr/sbin/iptables -> xtables-nft-multi
?
[root@centos7 ~]#ll /usr/sbin/iptables
lrwxrwxrwx. 1 root root 13 Dec  9  2018 /usr/sbin/iptables -> xtables-multi
[root@centos7 ~]# iptables --version
iptables v1.4.21
?
[root@centos6 ~]#iptables --version
iptables v1.4.7
[root@centos6 ~]#ll /sbin/iptables
lrwxrwxrwx. 1 root root 33 Dec 12  2018 /sbin/iptables -> /etc/alternatives/iptables.x86_64
[root@centos6 ~]#ll /etc/alternatives/iptables.x86_64
lrwxrwxrwx. 1 root root 20 Dec 12  2018 /etc/alternatives/iptables.x86_64 -> /sbin/iptables-1.4.7
[root@centos6 ~]#ll /sbin/iptables
lrwxrwxrwx. 1 root root 33 Dec 12  2018 /sbin/iptables -> /etc/alternatives/iptables.x86_64

范例:安裝iptables的service包

[root@centos8 ~]#dnf -y install iptables-services
[root@centos8 ~]#rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
/usr/libexec/initscripts/legacy-actions/ip6tables
/usr/libexec/initscripts/legacy-actions/ip6tables/panic
/usr/libexec/initscripts/legacy-actions/ip6tables/save
/usr/libexec/initscripts/legacy-actions/iptables
/usr/libexec/initscripts/legacy-actions/iptables/panic
/usr/libexec/initscripts/legacy-actions/iptables/save
/usr/libexec/iptables
/usr/libexec/iptables/ip6tables.init
/usr/libexec/iptables/iptables.init
2.2.2 firewalld

從CentOS 7 版開始引入了新的前端管理工具

軟件包:

  • firewalld

  • firewalld-config

管理工具:

  • firewall-cmd 命令行工具

  • firewall-config 圖形工作

2.2.3 nftables

此軟件是CentOS 8 新特性,Nftables最初在法國巴黎的Netfilter Workshop 2008上發表,然后由長期的netfilter核心團隊成員和項目負責人Patrick McHardy于2009年3月發布。它在2013年末合并到Linux內核中,自2014年以來已在內核3.13中可用。

它重用了netfilter框架的許多部分,例如連接跟蹤和NAT功能。它還保留了命名法和基本iptables設計的幾個部分,例如表,鏈和規則。就像iptables一樣,表充當鏈的容器,并且鏈包含單獨的規則,這些規則可以執行操作,例如丟棄數據包,移至下一個規則或跳至新鏈。

從用戶的角度來看,nftables添加了一個名為nft的新工具,該工具替代了iptables,arptables和ebtables中的所有其他工具。從體系結構的角度來看,它還替換了內核中處理數據包過濾規則集運行時評估的那些部分。

范例:查看軟件包

[root@centos8 ~]#rpm -qi nftables
Name ? ? ?  : nftables
Epoch ? ? ? : 1
Version ? ? : 0.9.0
Release ? ? : 8.el8
Architecture: x86_64
Install Date: Wed 25 Sep 2019 09:29:06 PM CST
Group ? ? ? : Unspecified
Size ? ? ?  : 758622
License ? ? : GPLv2
Signature ? : RSA/SHA256, Tue 02 Jul 2019 08:19:09 AM CST, Key ID 05b555b38483c65d
Source RPM  : nftables-0.9.0-8.el8.src.rpm
Build Date  : Sat 11 May 2019 11:06:46 PM CST
Build Host  : x86-01.mbox.centos.org
Relocations : (not relocatable)
Packager ?  : CentOS Buildsys <bugs@centos.org>
Vendor ? ?  : CentOS
URL ? ? ? ? : http://netfilter.org/projects/nftables/
Summary ? ? : Netfilter Tables userspace utillites
Description :
Netfilter Tables userspace utilities.

范例:CentOS 8 支持三種防火墻工具

[root@centos8 ~]#systemctl status iptables.service 
● iptables.service - IPv4 firewall with iptables
 ? Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
 ? Active: inactive (dead)
[root@centos8 ~]#systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
 ? Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
 ? Active: inactive (dead)
 ? ? Docs: man:firewalld(1)
[root@centos8 ~]#systemctl status nftables.service 
● nftables.service - Netfilter Tables
 ? Loaded: loaded (/usr/lib/systemd/system/nftables.service; disabled; vendor preset: disabled)
 ? Active: inactive (dead)
 ? ? Docs: man:nft(8)

2.3 netfilter 中五個勾子函數和報文流向

Netfilter在內核中選取五個位置放了五個hook(勾子) function(INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING),而這五個hook function向用戶開放,用戶可以通過一個命令工具(iptables)向其寫入規則 由信息過濾表(table)組成,包含控制IP包處理的規則集(rules),規則被分組放在鏈(chain)上

image-20191112223543391

提示:從 Linux kernel 4.2 版以后,Netfilter 在prerouting 前加了一個 ingress 勾子函數??梢允褂眠@個新的入口掛鉤來過濾來自第2層的流量,這個新掛鉤比預路由要早,基本上是 tc 命令(流量控制工具)的替代品

三種報文流向

  • 流入本機:PREROUTING –> INPUT–>用戶空間進程

  • 流出本機:用戶空間進程 –>OUTPUT–> POSTROUTING

  • 轉發:PREROUTING –> FORWARD –> POSTROUTING

2.4 iptables的組成

iptables由五個表table和五個鏈chain以及一些規則組成

五個表table:filter、nat、mangle、raw、security

filter表:過濾規則表,根據預定義的規則過濾符合條件的數據包 nat表:network address translation 地址轉換規則表 mangle:修改數據標記位規則表 raw:關閉啟用的連接跟蹤機制,加快封包穿越防火墻速度 security:用于強制訪問控制(MAC)網絡規則,由Linux安全模塊(如SELinux)實現

優先級由高到低的順序為:

security -->raw-->mangle-->nat-->filter

五個內置鏈chain:

INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING

鏈 chain:

  • 內置鏈:每個內置鏈對應于一個鉤子函數

  • 自定義鏈:用于對內置鏈進行擴展或補充,可實現更靈活的規則組織管理機制;只有Hook鉤子調用自定義鏈時,才生效

表和鏈對應關系

image-20191114100719569

數據包過濾匹配流程

image-20191114100913851

內核中數據包的傳輸過程

  • 當一個數據包進入網卡時,數據包首先進入PREROUTING鏈,內核根據數據包目的IP判斷是否需要轉送出去

  • 如果數據包是進入本機的,數據包就會沿著圖向下移動,到達INPUT鏈。數據包到達INPUT鏈后,任何進程都會收到它。本機上運行的程序可以發送數據包,這些數據包經過OUTPUT鏈,然后到達POSTROUTING鏈輸出

  • 如果數據包是要轉發出去的,且內核允許轉發,數據包就會向右移動,經過FORWARD鏈,然后到達POSTROUTING鏈輸出

范例:

root@centos8 ~]#iptables -vnL -t filter
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
 ? ? ?
[root@centos8 ~]#iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ?

[root@centos8 ~]#iptables -vnL -t mangle
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ?

[root@centos8 ~]#iptables -vnL -t raw
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? 

[root@centos8 ~]#iptables -vnL -t security
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
[root@centos8 ~]#
?
?
#CentOS 6 nat表不支持INPUT鏈
[root@centos6 ~]#iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination 

2.5 netfilter 完整流程

iptables

本文鏈接:http://www.royaladd.com/34204.html

iptables實戰練習

網友評論comments

發表回復

您的電子郵箱地址不會被公開。

暫無評論

Copyright ? 2012-2022 YUNWEIPAI.COM - 運維派 京ICP備16064699號-6
掃二維碼
掃二維碼
返回頂部
欧美激情视频一区二区|国产精品毛片va一区二区|999国内精品永久免费|国产无码sm视频在线观看