Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[FreeBSD] FreeBSD - PF Firewall (1)

100 views
Skip to first unread message

或許…我不是我

unread,
Jan 17, 2005, 9:09:49 AM1/17/05
to

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


※ 重新啟動系統


--

\||||||/ 「一直到中古時期結束之前,理性代表具備一種心智,能夠看
q ^ ^ p 出各種事物之間精神層次的關聯、在主體與客體之間的律動
╰ 0 ╯ o 、微妙平衡或分配比率。 」
</ 菠 \-/ - Seven Life Lessons of Chaos
--
[1;32m※ Origin: [33mSayYA 資訊站 [37m<bbs.sayya.org> [m
[1;31m◆ From: [36m61-224-80-30.dynamic.hinet.net [m

老英學生 Old English Student

unread,
Jan 17, 2005, 7:58:15 PM1/17/05
to
真好,感謝 "或許…我不是我" <ant...@bbs.sayya.org> 前輩

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

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

跟之前摸索中找到的 http://freebsd.ntut.idv.tw/document/freebsd_pf_nat.html
也有點像 .


恕後學魯頓, 可否請教 /etc/pf.conf 設定

我的環境:
fBSD 在 192.168.1.2 ; 跑各種 server
及 intranet 192.168.1.0/24 ; intranet 還有多部 windowsXP .
router, 在 192.168.1.1, 已設 將外來 access 全指過來給 fBSD 這台 server.
希望能限制 外出頻寬 ; 但不限制 內部頻寬 .
限制 外出頻寬 如果能限制 每個 ip 最多達某個上限頻寬 就最好.

這是我的 /etc/pf.conf ; 能不能請幫我看一眼 這樣設可以嗎?

#######################
# /etc/pf.conf 開始
# neuro : 1 macro definitions
extdev='fxp0' # 對外的網路卡

# neuro : 2 otions: "set"


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

set block-policy return
set require-order yes

set optimization aggressive
set loginterface $extdev

scrub in all

# neuro - ALTQ 定義頻寬控制的進出
# Outgoing bandwidth limit
altq on $extdev cbq bandwidth 512Kb queue { std_out }
queue std_out bandwidth 256Kb cbq (default)
altq on $extdev cbq bandwidth 100Mb queue { std_in }
queue std_in bandwidth 2Mb cbq (default borrow)

# neuro - 4 filtering rules: "antispoof", "block", "pass"


antispoof log quick for $extdev

pass in all
pass out all

block in proto tcp from 163.19.218.225 to any port 80
block in proto tcp from 163.22.3.8 to any port 80
block in proto tcp from 24.187.253.176 to any port 80
block in proto tcp from 61.135.0.0/16 to any port 80
# ... 一堆 被我 block 的 spider, 或不良 ip

block from 128.255.208.111 to any
block from 134.174.165.58 to any
block from 157.82.247.112 to any
block from 163.150.15.246 to any
block from 163.17.209.44 to any
# ... 一堆 被我 block 的 害客 不良 ip - 那些亂來 ssh telnet 的

# end of /etc/pf.conf 結束
#######################


個人小事,
感謝 前輩撥冗費心了 :)


謝謝

<: Neuro :>
http://neuro.ohbi.net
http://neuro.no-ip.info
http://neuro.dyndns.tv
http://neuro.supt.net
http://neuro.taiwantop.com

e-mails :
ne...@neuro.supt.net
neu...@hotmail.com

===

"或許…我不是我" <ant...@bbs.sayya.org> 撰寫於郵件新聞:4GGM6D$o...@bbs.sayya.org...

或許…我不是我

unread,
Jan 18, 2005, 4:51:43 AM1/18/05
to
※ 引述《goa...@nowhere.not (老英學生 Old English Student)》之銘言:

> 我的環境:
> fBSD 在 192.168.1.2 ; 跑各種 server
> 及 intranet 192.168.1.0/24 ; intranet 還有多部 windowsXP .
> router, 在 192.168.1.1, 已設 將外來 access 全指過來給 fBSD 這台 server.
> 希望能限制 外出頻寬 ; 但不限制 內部頻寬 .
> 限制 外出頻寬 如果能限制 每個 ip 最多達某個上限頻寬 就最好.
> 這是我的 /etc/pf.conf ; 能不能請幫我看一眼 這樣設可以嗎?
> #######################
> # /etc/pf.conf 開始
> altq on $extdev cbq bandwidth 512Kb queue { std_out }
> queue std_out bandwidth 256Kb cbq (default)
> altq on $extdev cbq bandwidth 100Mb queue { std_in }
> queue std_in bandwidth 2Mb cbq (default borrow)

altq on $extdev cbq bandwidth 100Mb queue { std_int, std_out }
queue std_int cbq(default)
queue std_out bandwidth 512Kb { limit_ext }
queue limit_ext bandwidth 256Kb cbq(red)

pass out on $extdev from { ip1, ip2, ip3 } to any queue limit_ext

這樣試試看,我沒試過

> # end of /etc/pf.conf 結束
> #######################
> 個人小事,
> 感謝 前輩撥冗費心了 :)
> 謝謝
> <: Neuro :>
> http://neuro.ohbi.net
> http://neuro.no-ip.info
> http://neuro.dyndns.tv
> http://neuro.supt.net
> http://neuro.taiwantop.com
> e-mails :
> ne...@neuro.supt.net
> neu...@hotmail.com

--

\||||||/ 「一直到中古時期結束之前,理性代表具備一種心智,能夠看
q ^ ^ p 出各種事物之間精神層次的關聯、在主體與客體之間的律動
╰ 0 ╯ o 、微妙平衡或分配比率。 」
</ 菠 \-/ - Seven Life Lessons of Chaos
--
[1;32m※ Origin: [33mSayYA 資訊站 [37m<bbs.sayya.org> [m

[1;31m◆ From: [36m140.138.148.181 [m

老英學生 Old English Student

unread,
Jan 18, 2005, 5:56:24 AM1/18/05
to

感謝敢謝 :)

<: Neuro :>


===

"或許…我不是我" <ant...@bbs.sayya.org> 撰寫於郵件新聞:4GH4hV$n...@bbs.sayya.org...

0 new messages