Tuesday, January 18, 2005

發信人: ant.bbs@bbs.sayya.org (或許…我不是我), 看板: 386BSD
標 題: [FreeBSD] FreeBSD - PF Firewall (1)
發信站: SayYA 資訊站 (Mon Jan 17 22:09:49 2005)
Origin: kenduest.math.nctu.edu.tw

FreeBSD with Packet Filter(PF) Firewall - (1)

- PF 於 July 2003 從 OpenBSD 中 porting 到 FreeBSD ports collection。
- PF 在 November 2004 成為 FreeBSD 系統的核心部份。
- PF 和 ALTQ 確定在 FreeBSD 5.3 RELEASE 中支援。

使用 PF 有兩種方式:一、模組載入;二、編入核心。

如果使用舊的 FreeBSD 版本,可以由 ports collection 中安裝。
# cd /usr/ports/security/pf ; make install

(註:ports 中的 PF 是 OpenBSD 3.4 的版本,正式 porting 至 FreeBSD
核心部份的版本則是 OpenBSD 3.5)

注意:以下環境皆是由 FreeBSD 5.3 的環境下測試,請依照不同版本視情形調整。


※ PF 啟動方式

[ 模組載入 ]

####################
### /etc/rc.conf ###
####################

pf_enable="YES" # 啟動 PF
pf_rules="/etc/pf.conf" # PF 的設定檔位置
pf_flags="" # PF 的參數
pflog_enable="YES" # 啟動 PFLOG
pflog_logfile="/var/log/pflog" # PFLOG 紀錄檔的位置
pflog_flags="" # PFLOG 的參數

如果要使 PF 支援 gateway 則,多加入下一行:
gateway_enable="YES"


[ 編入核心 ]

##############################
### 將如下幾行加入至核心中 ###
##############################

device bpf
device pf
device pflog
device pfsync

- device bpf 是 FreeBSD log traffic,如果有使用 pflog,請務必編入。
- device pf 是啟動 PF firewall
- device pflog 是啟動虛擬網路設備來記錄流量(經由 bpf)
- device pfsync 是虛擬網路設備來監視網路狀態。

#######################
### vi /etc/rc.conf ###
#######################

pf_enable="YES" # 啟動 PF
pf_rules="/etc/pf.conf" # PF 的設定檔位置
pf_flags="" # PF 的參數
pflog_enable="YES" # 啟動 PFLOG
pflog_logfile="/var/log/pflog" # PFLOG 紀錄檔的位置
pflog_flags="" # PFLOG 的參數

如果要使 PF 支援 gateway 則,多加入下一行:
gateway_enable="YES"

最後重新編譯 kernel。

※ PF 設定檔

標準的格式如下:
############################################################
# macro definitions
############################################################
# options: "set"
############################################################
# scrub rules: "scrub"
############################################################
# NAT rules: "rdr", "nat", "binat"
############################################################
# filtering rules: "antispoof", "block", "pass"
############################################################


不需 NAT 的簡單設定檔如下:
############################################################
# macro definitions
############################################################

extdev='fxp0' # 對外的網路卡

############################################################
# options: "set"
############################################################

set limit frags 30000 # 保存 30000 個 frags
set limit states 25000 # 保存 25000 個狀態表的數量

# answer blocked TCP packets with TCP RSP and
# blocked UDP with ICMP destination-unreachable
set block-policy return

set require-order yes
set optimization aggressive
set loginterface $extdev

############################################################
# scrub rules: "scrub"
############################################################

scrub in all

############################################################
# filtering rules: "antispoof", "block", "pass"
############################################################

antispoof log quick for $extdev

# block ipv6 packets
#block inet6 all

pass in all
pass out all


需 NAT 的簡單設定檔如下:
############################################################
# macro definitions
############################################################

extdev='fxp0' # 對外的網路卡
intranet='192.168.0.0/24' # 內部虛擬IP

############################################################
# options: "set"
############################################################

set limit frags 30000 # 保存 30000 個 frags
set limit states 25000 # 保存 25000 個狀態表的數量

# answer blocked TCP packets with TCP RSP and
# blocked UDP with ICMP destination-unreachable
set block-policy return

set require-order yes
set optimization aggressive
set loginterface $extdev

############################################################
# scrub rules: "scrub"
############################################################

scrub in all

############################################################
# NAT rules: "rdr", "nat", "binat"
############################################################

nat on $extdev inet from $intranet to any -> $extdev
## 這裡特別注意的是,如果你的 extdev 綁了多個 IP
## 那麼 NAT 出去的 IP 會採取 round-robin 出去 (酷)

############################################################
# filtering rules: "antispoof", "block", "pass"
############################################################

antispoof log quick for $extdev

# block ipv6 packets
#block inet6 all

pass in all
pass out all


※ 重新啟動系統



FreeBSD with Packet Filter(PF) Firewall - (2)

- FreeBSD 的 ALTQ 於 June 13th 2004 加入。
- PF 和 ALTQ 確定在 FreeBSD 5.3 RELEASE 中支援。

使用 ALTQ 必須編入核心。

※ 啟動 ALTQ

一般常編入的選項是:
options ALTQ
options ALTQ_CBQ # Class Bases Queueing
options ALTQ_RED # Random Early Drop
options ALTQ_RIO # RED In/Out
options ALTQ_HFSC # Hierarchical Packet Scheduler
options ALTQ_CDNR # Traffic conditioner
options ALTQ_PRIQ # Prioirity Queueing

其他選項是:
options ALTQ_NOPCC # only for SMP kernels
options ALTQ_DEBUG # ALTQ debug

※ 設定 ALTQ

方法請線上查閱



FreeBSD with Packet Filter(PF) Firewall - (3)

※ pfctl 的用法

pfctl 是可以直接控制 PF Firewall 的指令。

1. 啟用 PF

# pfctl -e

2. 停用 PF

# pfctl -d

3. 讀取/重讀 PF 設定檔

# pfctl -f /etc/pf.conf

- 僅重讀 PF 設定檔中的 Options 部份
# pfctl -f /etc/pf.conf -O

- 僅重讀 PF 設定檔中的 queue 部份
# pfctl -f /etc/pf.conf -A

- 僅重讀 PF 設定檔中的 NAT 部份
# pfctl -f /etc/pf.conf -N

- 僅重讀 PF 設定檔中的 filter rules
# pfctl -f /etc/pf.conf -R

4. 查看 PF 資訊

# pfctl -s info
# pfctl -s memory
# pfctl -s rules
# pfctl -vs rules
# pfctl -s nat
# pfctl -s queue
# pfctl -s all

5. 清除 PF 規則

# pfctl -F nat
# pfctl -F queue
# pfctl -F rules
# pfctl -F info
# pfctl -F Tables
# pfctl -F all

6. PF Tables 的使用

# pfctl -t table_name -T add spammers.org
# pfctl -t table_name -T delete spammers.org
# pfctl -t table_name -T flush
# pfctl -t table_name -T show
# pfctl -t table_name -T zero

0 Comments:

Post a Comment

<< Home