nftables技術分享@Debian Jessie (I) - Linux

Table of Contents

本技術分享一共分為四章

第一章 緒言
第二章 環境簡介
第三章 nftables簡易規則介紹
第四章 實作nftables與開機載入規則分享

本文(I)將介紹第一章至第三章,

第四章將於(II)進行介紹。



第一章 緒言

由於netfilter已經宣佈了nftables及將自kernel 3.13版本後,

陸續的取代使用許久的iptables。

手邊剛好有了一台沒有任何用途使用中的電腦,

故便幫各位Debian的使用者們,

對nftables進行了簡單的測試。




第二章 環境簡介

本次的測試硬體與軟體環境如下:

DHCP浮動ip網段

某不知名Intel Core Duo 雙核心單執行緒CPU乙顆

G31晶片組主機板

RTL8168 ethernet晶片網卡乙張

Debian Jessie 64bit boot up with SystemD



由於本次使用的硬體僅為單網卡的電腦,

故本次測試的nftables範圍,

就沒有包含到了使用NAT的規則部份。




第三章 nftables簡易規則介紹

由於近日以來,

部份勇於嘗試新鮮的linux distro,

皆提供了其發行板所使用的套件庫內nftables可安裝packages,

並release了nftables相關的wiki文件[1][2],

很可惜的,

在下必須向各位說聲抱歉,

目前debian官方並未提供詳細的nftables wiki相關文件,

不過由於netfilter的官方文件[3]與諸多先進們曾留下的howto筆記中[4][5],

我們仍可以透過這些資訊,

來進行一個基本的nftables study。



於debian jessie中,

目前我們已經可由apt套件庫安裝上nftables,

# apt-get install nftables


在安裝好nftables後,

我們可以看一下/etc/nftables的目錄,

所有最基本的nftables設定皆放在此目錄中。

#ls -l| /etc/nftables

您會看到類似像是這樣的畫面:

-rwxr-xr-x 1 root root 217 5月 28 15:42 bridge-filter
-rwxr-xr-x 1 root root 207 5月 28 15:42 inet-filter
-rwxr-xr-x 1 root root 202 5月 28 15:42 ipv4-filter
-rwxr-xr-x 1 root root 94 5月 28 15:42 ipv4-mangle
-rwxr-xr-x 1 root root 160 5月 28 15:42 ipv4-nat
-rwxr-xr-x 1 root root 206 5月 28 15:42 ipv6-filter
-rwxr-xr-x 1 root root 98 5月 28 15:42 ipv6-mangle
-rwxr-xr-x 1 root root 164 5月 28 15:42 ipv6-nat

各位其實可以發現,

iptables中我們可以設定的規則,

都可以在nftables上的設定檔中來進行設定。

此次我們僅測試到的是ipv4與ipv6的基本功能,

我們便使用ipv4-filter與ipv6-filter這兩個檔案進行匯入

匯入的方式很簡單,

我們可以下這兩個指令,

將ipv4與ipv6的規則迅速的匯入。

# nft -f /etc/nftables/ipv4-filter

# nft -f /etc/nftables/ipv6-filter

此時您可以使用這兩個指令來去察看ipv4與ipv6的現存設定

# nft list table ip filter

# nft list table ip6 filter

這時您看到的畫面,

與您直接cat /etc/nftables/ipv4-filter,

以及cat /etc/nftables/ipv6-filter這兩個指令看到的畫面,

會看到相同的東西。


先來個簡單又實用的example!

若擋了所有input的連線,

但電腦仍然需要上網,

則勢必要讓主動連外的封包可以回來:

# nft add rule ip filter input ct state established accept

同樣的若要是在ipv6的環境下,

也可以加入在ipv6的規則中:

# nft add rule ip6 filter input ct state established accept



在linux上使用ssh來遠端連線是很基本的需求,

使用nfs進行傳送檔案更不用說了,

故依照我們的需求開port也是很理所當然的事情。

假設本機的ssh port在4444,

那麼以下的這條規則就可以幫各位開好了ssh使用的port:

# nft add rule ip filter input tcp dport 4444 accept

當所需要開啟的port不只一個,

在nftables中可以很簡單的使用multi port的方式,

以一條規則開啟多個port,

假設今天要開啟3333、4444以及5555這3個tcp的port,

那麼只要下這一個指令就可以一氣呵成。

# nft add rule ip filter input tcp dport {3333,4444,5555} accept



阻擋某一個ip也是很基本的需求,

假設今天要阻擋來自於ip 1.2.3.4的電腦的連線:

# nft add rule ip filter input ip saddr 1.2.3.4 drop



阻擋某個mac address的網卡的連線,

當然也不會是太困難的事情!

假設今天要阻擋來自00:11:22:33:44:55這個mac address的網卡的連線:

# nft add rule ip filter input ether saddr 00:11:22:33:44:55 drop



其餘部份於(II)繼續進行介紹!




參考文獻


[1] http://wiki.gentoo.org/wiki/Nftables
[2] https://wiki.archlinux.org/index.php/nftables
[3] http://wiki.nftables.org/wiki-nftables/index.php/Main_Page
[4] https://home.regit.org/netfilter-en/nftables-quick-howto/
[5] http://kernelnewbies.org/nftables_examples

--

All Comments

Bethany avatarBethany2014-07-16
先推,可是好懶得學新東西(倒 舊機器的也不太敢動
Faithe avatarFaithe2014-07-17
有網頁版的嗎?
Jake avatarJake2014-07-18
可以動態開port嗎?
Sierra Rose avatarSierra Rose2014-07-19
這文看起來像是在個人blog筆記 還是其他閱讀版本
Regina avatarRegina2014-07-21
@kadok:本篇分享目前只有在本版公開貼出,並沒有在其他
blog或是網站公開貼出,若您想要轉錄至其他blog也非常歡迎
Rebecca avatarRebecca2014-07-26
!歡迎隨意轉錄取用。