19
1 IPTABLES and IPTABLES and NAT NAT on Fedora Cor on Fedora Cor e 6 e 6 Speaker Speaker Rex Wu Rex Wu Date Date 2007.6.1 2007.6.1 1 1

1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

Embed Size (px)

Citation preview

Page 1: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

1

IPTABLES and NATIPTABLES and NATon Fedora Core 6on Fedora Core 6

SpeakerSpeaker :: Rex WuRex Wu

Date Date :: 2007.6.112007.6.11

Page 2: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

2

OutlineOutlineIntroductionWhat is NATPrivate IP Address spaceIptablesNAT 語法範例Reference

IntroductionWhat is NATPrivate IP Address spaceIptablesNAT 語法範例Reference

Page 3: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

3

IntroductionIntroduction

說明何謂 Iptables 、 NAT ,並以Fedora Core 6 的 Iptables 指令來實際架設 NAT

說明何謂 Iptables 、 NAT ,並以Fedora Core 6 的 Iptables 指令來實際架設 NAT

Page 4: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

4

What is NAT ?(1)What is NAT ?(1)

全名: Network Address Translation 功能:

允許多個 users 使用 Private IP Address 並透過一個 Public IP Address 連上 Internet

DNAT = Destination NAT可用來改變 Destination IP Address

SNAT = Source NAT可用來改變 Source IP Address

全名: Network Address Translation 功能:

允許多個 users 使用 Private IP Address 並透過一個 Public IP Address 連上 Internet

DNAT = Destination NAT可用來改變 Destination IP Address

SNAT = Source NAT可用來改變 Source IP Address

Page 5: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

5

What is NAT ?(2)What is NAT ?(2)- 圖示

Page 6: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

6

Private IP Address space

Private IP Address space

The IANA has reserved the following blocks of the IP address space for private internets:

10.0.0.0 - 10.255.255.255172.16.0.0 -172.31.255.255 192.168.0.0 -192.168.255.255

The IANA has reserved the following blocks of the IP address space for private internets:

10.0.0.0 - 10.255.255.255172.16.0.0 -172.31.255.255 192.168.0.0 -192.168.255.255

Page 7: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

7

Iptables(1)Iptables(1)

Netfilter is the framework in Linux 2.4 kernelsthat allow for firewalling, NAT.

Iptables is the userspace tools that works with the Netfilter framework

ie :Netfilter is in the kernel spaceIptables is in the user space

Netfilter is the framework in Linux 2.4 kernelsthat allow for firewalling, NAT.

Iptables is the userspace tools that works with the Netfilter framework

ie :Netfilter is in the kernel spaceIptables is in the user space

- What is Netfilter/Iptables?

Page 8: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

8

Iptables(2)Iptables(2)- 架構圖

Page 9: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

9

Iptables(3)Iptables(3)- 流程圖

Page 10: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

10

- 語法- 語法

語法 : iptables [-t 作用的 Table] [ 處理 Chain 的方式 ] [ 比對規則 ] [ 處理方式

]

範例 : iptables –t nat –A POSTROUTING –o eth1 –s 192.168.1.0/24 \

–j MASQUERADE

解釋:在 NAT table 中,新增以下規則:規定 source IP Address=192.168.1.0~192.168.1.255 且欲通過 eth1 介面出去的封包 ( 即 output interface = eth1)就將其偽裝

語法 : iptables [-t 作用的 Table] [ 處理 Chain 的方式 ] [ 比對規則 ] [ 處理方式

]

範例 : iptables –t nat –A POSTROUTING –o eth1 –s 192.168.1.0/24 \

–j MASQUERADE

解釋:在 NAT table 中,新增以下規則:規定 source IP Address=192.168.1.0~192.168.1.255 且欲通過 eth1 介面出去的封包 ( 即 output interface = eth1)就將其偽裝

Iptables(4)Iptables(4)

Page 11: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

11

ip_forward=1功能:啟動封包轉送法一: echo “1” > /proc/sys/net/ipv4/ip_forw

ard法二:修改 /etc/sysctl.conf 設定檔,如下    net.ipv4.ip_forward =1

ip_forward=1功能:啟動封包轉送法一: echo “1” > /proc/sys/net/ipv4/ip_forw

ard法二:修改 /etc/sysctl.conf 設定檔,如下    net.ipv4.ip_forward =1

Iptables(5)Iptables(5) -NAT 的必要設定-NAT 的必要設定

Page 12: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

12

1. ip_conntrack →IP 追蹤連線模組2. ip_conntrack_ftp → ftp 追蹤模組3. ip_nat_ftp

功能:允許 private side 的主機使用 ftp 服務( 若無載入,則 private side 的 ftp server 無法讓 public side存取 )

載入方法:modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_nat_ftp

1. ip_conntrack →IP 追蹤連線模組2. ip_conntrack_ftp → ftp 追蹤模組3. ip_nat_ftp

功能:允許 private side 的主機使用 ftp 服務( 若無載入,則 private side 的 ftp server 無法讓 public side存取 )

載入方法:modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_nat_ftp

Iptables(6)Iptables(6) -NAT 的必要載入檔-NAT 的必要載入檔

Page 13: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

13

將 iptables 內所有 tables 的設定儲存在 /etc/iptables.save ex : iptables-save > /etc/iptables.save

將 iptables 中, NAT table 儲存於 /etc/iptables-nat.save

ex : iptables-save -t nat > /etc/iptables-nat.save

以 /etc/iptables.save 設定檔,將 iptables 中,所有的設定重新載入 ex : iptables-resotre < /etc/iptables.save

將 iptables 內所有 tables 的設定儲存在 /etc/iptables.save ex : iptables-save > /etc/iptables.save

將 iptables 中, NAT table 儲存於 /etc/iptables-nat.save

ex : iptables-save -t nat > /etc/iptables-nat.save

以 /etc/iptables.save 設定檔,將 iptables 中,所有的設定重新載入 ex : iptables-resotre < /etc/iptables.save

Iptables(7)Iptables(7)- 備份與還原 - 備份與還原

Page 14: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

14

1. 以 iptables-restore 搭配 rc.local 使用

2. 存成 script 搭配 rc.local 使用

1. 以 iptables-restore 搭配 rc.local 使用

2. 存成 script 搭配 rc.local 使用

Iptables(8)Iptables(8)- auto loading- auto loading

Page 15: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

15

Change source addresses to 1.2.3.4ex : iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4

Change source addresses to 1.2.3.4, 1.2.3.5 or 1.2.3.6ex :iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to \ 1.2.3.4-1.2.3.6

Change source addresses to 1.2.3.4, ports 1-1023ex :iptables -t nat -A POSTROUTING -p tcp -o eth0 -j SNAT –to \ 1.2.3.4:1-1023

Change source addresses to 1.2.3.4ex : iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4

Change source addresses to 1.2.3.4, 1.2.3.5 or 1.2.3.6ex :iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to \ 1.2.3.4-1.2.3.6

Change source addresses to 1.2.3.4, ports 1-1023ex :iptables -t nat -A POSTROUTING -p tcp -o eth0 -j SNAT –to \ 1.2.3.4:1-1023

NAT 語法 (1)NAT 語法 (1)- SNAT- SNAT

Page 16: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

16

Masquerade 為 SNAT 的特例,若防火牆對外使用1. Static IP Address ,則使用 SNAT2. Dynamic IP Address ,則建議使用 Masquerade

Masquerade everything out ppp0.ex :iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

Masquerade everything out eth1.ex :iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Masquerade 為 SNAT 的特例,若防火牆對外使用1. Static IP Address ,則使用 SNAT2. Dynamic IP Address ,則建議使用 Masquerade

Masquerade everything out ppp0.ex :iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

Masquerade everything out eth1.ex :iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

NAT 語法 (2)NAT 語法 (2)- Masquerade is a specialized case of

SNAT - Masquerade is a specialized case of

SNAT

Page 17: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

17

Change destination addresses to 5.6.7.8 ex :iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 5.6.7.8

Change destination addresses to 5.6.7.8, 5.6.7.9 or 5.6.7.10. ex :iptables -t nat -A PREROUTING -i eth0 -j DNAT –to \ 5.6.7.8-5.6.7.10

Change destination addresses of web traffic to 5.6.7.8, port 8080ex :iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth0 \-j DNAT --to 5.6.7.8:8080

Change destination addresses to 5.6.7.8 ex :iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 5.6.7.8

Change destination addresses to 5.6.7.8, 5.6.7.9 or 5.6.7.10. ex :iptables -t nat -A PREROUTING -i eth0 -j DNAT –to \ 5.6.7.8-5.6.7.10

Change destination addresses of web traffic to 5.6.7.8, port 8080ex :iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth0 \-j DNAT --to 5.6.7.8:8080

NAT 語法 (3)NAT 語法 (3) - DNAT- DNAT

Page 18: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

18

範例範例

請參考http://ms11.voip.edu.tw/~rex/FC6_nat.html

請參考http://ms11.voip.edu.tw/~rex/FC6_nat.html

Page 19: 1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date : 2007.6.11

19

ReferenceReference

Linux 防火牆與 NAT 主機 http://linux.vbird.org/linux_server/0250simple_firewall.php

Linux 2.4 NAT HOWTOhttp://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html

Linux 防火牆入門相關網站 http://linux.tnc.edu.tw/techdoc/firewall/ref-url.html

Red Hat Enterprise Linux 4 安全性設定手冊 http://web.mit.edu/rhel-doc/4/RH-DOCS/rhel-sg-zh_tw-4/index.html

Linux 防火牆與 NAT 主機 http://linux.vbird.org/linux_server/0250simple_firewall.php

Linux 2.4 NAT HOWTOhttp://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html

Linux 防火牆入門相關網站 http://linux.tnc.edu.tw/techdoc/firewall/ref-url.html

Red Hat Enterprise Linux 4 安全性設定手冊 http://web.mit.edu/rhel-doc/4/RH-DOCS/rhel-sg-zh_tw-4/index.html