24
1 Iptables and Netfi lter Linux 防防防 Wei-li Tang July 14, 2010

Iptables and Netfilter Linux 防火牆

  • Upload
    padma

  • View
    205

  • Download
    0

Embed Size (px)

DESCRIPTION

Iptables and Netfilter Linux 防火牆. Wei-li Tang July 14, 2010. Outline. 為何需要防火牆? Linux 防火牆 Netfilter Setting Netfilter Up iptables. 為何需要防火牆?. 限制某些服務的存取來源 防止未經同意與不明的連線傳輸 例如 限制僅來自學校的 IP 位址可連到我的 FTP 站。 限制外人只能連到我電腦上的 HTTP Server ,但不能連到其他 Port 。. Linux 防火牆. Netfilter Linux 核心的封包過濾架構 - PowerPoint PPT Presentation

Citation preview

Page 1: Iptables and Netfilter Linux  防火牆

1

Iptables and NetfilterLinux 防火牆

Wei-li TangJuly 14, 2010

Page 2: Iptables and Netfilter Linux  防火牆

2

Outline

為何需要防火牆? Linux 防火牆 Netfilter

Setting Netfilter Up iptables

Page 3: Iptables and Netfilter Linux  防火牆

3

為何需要防火牆? 限制某些服務的存取來源

防止未經同意與不明的連線傳輸 例如

限制僅來自學校的 IP 位址可連到我的 FTP 站。限制外人只能連到我電腦上的 HTTP Server ,但不能連到其他 Port 。

Page 4: Iptables and Netfilter Linux  防火牆

4

Linux 防火牆 Netfilter

Linux 核心的封包過濾架構 設定工具

iptables (>= Linux 2.4) ipchains (Linux 2.2) ipfwadm (Linux 2.0)

Page 5: Iptables and Netfilter Linux  防火牆

5

Linux 防火牆 其他作業系統的防火牆

Windows Internet Connection Firewall (ICF) (>= XPSP1)

BSD OpenBSD PF (Packet Filter) (ported to FreeBSD) IPFILTER (IPF) IPFIREWALL (IPFW) (sponsored by FreeBSD)

Page 6: Iptables and Netfilter Linux  防火牆

6

Netfilter

功能不只是防火牆NAT (IP 分享器 )延伸應用

辨識並處理 P2P 流量(Layer 7 Filter)

網路限速…等

Image Ref.: http://cha.homeip.net/blog/

Page 7: Iptables and Netfilter Linux  防火牆

7

Netfilter Packet Flow

Page 8: Iptables and Netfilter Linux  防火牆

8

Netfilter

Packet Flow

PREROUTING POSTROUTING

接收封包IN

送出封包OUT

INPUT OUTPUT

FORWARD

目的

地為

本機

目的地非本機

本機程式Localhost

Filter 表

FORWARD目的地非本機

FORWARD目的地非本機

Raw / Mangle / NAT 表

表 Table

鏈 Chain

網路介面 Interface

Page 9: Iptables and Netfilter Linux  防火牆

9

Netfilter

Filter INPUT: 管制連入本機的連線OUTPUT: 管制自本機連出的連線FORWARD: 管制連往他處的連線

僅適用於路由器 (Router) 功能起動時

Page 10: Iptables and Netfilter Linux  防火牆

10

Setting Netfilter Up

簡單的設定工具system-config-securitylevel

Page 11: Iptables and Netfilter Linux  防火牆

11

Setting Netfilter Up

簡單的設定工具ufw (Uncomplicated Firewall) 簡化套件Firestarter 圖型防火牆設定套件

Page 12: Iptables and Netfilter Linux  防火牆

12

Setting Netfilter Up

以上都是鄉民在用的工具。

專家只用: iptables

Page 13: Iptables and Netfilter Linux  防火牆

13

iptables

基本參數-t : 設定表格 ( 預設為 “ filter”)-[A|D|F|I|P] <chain>

-A <chain> : 增加規則 (rule) -D <chain> <rulenum> : 刪除第 <rulenum> 條規則 -F <chain> : 清除所有 <chain> 的規則 -I <chain> <rulenum> : 在第 <rulenum> 條插入規則 -P <chain> <target> : 設定該 <chain> 的預設規則

target = ACCEPT, DROP …etc.

-j : 處理方式 (ACCEPT, DROP, REJECT…)

Page 14: Iptables and Netfilter Linux  防火牆

14

iptables

特徵參數-p : Layer 4 通訊協定 (tcp, udp…etc.)-m : match extension-s : 來源 IP 位址 (source)--sport : 來源 Port 埠號-d : 目的 IP 位址 (destination)--dport : 目的 Port 埠號

Page 15: Iptables and Netfilter Linux  防火牆

15

iptables

範例當 INPUT Policy 為 DROP

Policy 設定方式iptables -P INPUT DROP

註:任何連線連入,若無符合任何條件,即直接捨棄。

加入一條允許連入網頁伺服器的規則iptables -A INPUT -p tcp --dport 80 -j ACCEPT

指定 Chain ( 鏈 ) 通訊協定 埠號 處理方式

Page 16: Iptables and Netfilter Linux  防火牆

16

iptables

比對規則的順序 ( 優先權 )Netfilter 將規則由上而下依序比對越上面的規則,優先權越高一旦該連線符合某規則,即停止比對

該規則以下的規則將不會影響該連線善用 iptables -L 檢視現有規則

必要時加上 -n 參數將所有協定與位址以數字表示

Rule 1Rule 2Rule 3…….…….

Last Rule

Page 17: Iptables and Netfilter Linux  防火牆

17

iptables

實戰: CentOS 5.5自創的 Chain: RH-Firewall-1-INPUT

功能等同 INPUT 註:自 Fedora 13 改回使用 INPUT

設定檔 /etc/sysconfig/iptables起動 / 關閉 / 重新起動 iptables 規則

/etc/init.d/iptables <start|stop|restart>

Page 18: Iptables and Netfilter Linux  防火牆

18

iptables /etc/sysconfig/iptables

# Firewall configuration written by system-config-securitylevel# Manual customization of this file is not recommended.*filter # 以下使用 filter 表 ( 相當 – t filter):INPUT ACCEPT [0:0] # INPUT 鏈預設 Policy 為 ACCEPT ( 相當 – P INPUT ACCEPT):FORWARD ACCEPT [0:0] # FORWARD 鏈預設 Policy 為 ACCEPT:OUTPUT ACCEPT [0:0] # OUTPUT 鏈預設 Policy 為 ACCEPT :RH-Firewall-1-INPUT - [0:0] # 建立 RH-Firewall-1-INPUT 鏈 ( 自創的鏈無法指定 Policy)-A INPUT -j RH-Firewall-1-INPUT # INPUT 鏈規則一律參照 RH-Firewall-1-INPUT 鏈-A FORWARD -j RH-Firewall-1-INPUT # FORWARD 鏈規則一律參照 RH-Firewall-1-INPUT 鏈-A RH-Firewall-1-INPUT -i lo -j ACCEPT # 接受 loopback 一切連線-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT # 接受所有 icmp 封包-A RH-Firewall-1-INPUT -p 41 -j ACCEPT # 接受 IPv6-in-IPv4 通道封包-A RH-Firewall-1-INPUT -p 50 -j ACCEPT # 接受 ESP(50)-A RH-Firewall-1-INPUT -p 51 -j ACCEPT # 接受 AH(51)-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT # 接受 MDNS(Avahi) 封包-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT # 接受網路列印協定 (ipp,631/udp) 封包-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT # 接受網路列印協定 (ipp,631/tcp) 封包-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 接受一切已建立與相關連線-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # 接受建立 SSH 連線# 在此行加入新規則-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited # 拒絕一切連線並回覆 ICMPCOMMIT

Page 19: Iptables and Netfilter Linux  防火牆

19

iptables

Red Hat 系列的 -m state 哲學 State 種類

NEW, ESTABLISHED, RELATED, INVALID

TCP/Reliable Protocol State Transition 逐條接受各種條件「建立」連線 (NEW) 嗣後一切已建立 (ESTABLISHED)/ 相關 (RELATED) 連線由一條規則同意接受-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

優點:已建立連線的封包可不必在上述條件之後繼續 match

Page 20: Iptables and Netfilter Linux  防火牆

20

iptables 規則數量對效能的影響

約超過 100 條規則後會漸降低效能

Ref.: József Kadlecsik, György Pásztor “Netfilter Performance Testing”.

Page 21: Iptables and Netfilter Linux  防火牆

21

實習1. 請試著將 SSH Listen Port 從 22 修改為 2222 ,並

於防火牆設定做相對應的修改。 註: OpenSSH Daemon 設定檔為 sshd_config. 如果在遠端電腦練習,請特別注意可能的斷線問題。

2. 請試著允許外人連入網頁伺服器 (HTTP) 。3. 請試著禁止特定 IP 位址連入網頁伺服器。

您可以詢問隔壁同學的 IP 位址進行測試。4. 請試著禁止從本機連到其他網站的 SSH 連線。

使用 OUTPUT Chain

5. REJECT 與 DROP 的不同處?

Page 22: Iptables and Netfilter Linux  防火牆

22

Appendix

Layer 4 協定代碼 用於 -p 參數 /etc/protocols

Service Port Number 服務的名稱與埠號用於 --sport 與 --dport 參數 /etc/services

Page 23: Iptables and Netfilter Linux  防火牆

23

Reference

IPTABLES(8) man -s 8 iptables

netfilter.org 鳥哥的 Linux 私房菜 - Linux 防火牆與 NAT 主機 圖書館藏

Linux iptables 速查手冊 = Linux iptables pocket reference / Gregor N. Purdy原著 ; 林長毅編譯 , 歐萊禮 , 2004.

Linux firewalls : attack detection and response with iptables, psad, and fwsnort / by Michael Rash

Page 24: Iptables and Netfilter Linux  防火牆

24

實習 (參考答案 ) Press any key or remove the mask.1. /etc/ssh/sshd_config: Port 2222

iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

2. iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

3. iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 10.10.39.161 --dport 80 -j REJECT

10.10.39.161 為欲禁止的來源 IP 位址4. iptables -A OUTPUT -p tcp --dport 22 -j REJECT5. REJECT 會回覆 ICMP , DROP 則會直接捨棄封包。