1.
o PF:
o
o
o
o NAT
o
o
PF:
:
pf , /etc/rc.conf :
pf=YES
, .
pf pfctl(8):
# pfctl-e # pfctl-d
. , ,
, pf.
:
pf /etc/pf.conf , rc.scripts. ,
/etc/pf.conf - , ,
pfctl(8) pf(4).
. UNIX, pf
.
pf.conf :
1.
2.
3.
4.
5.
6.
: , IP, , ..
: IP
: , pf
Scrub:
: .
: NAT
7.
,
.
, # .
:
pf pfctl(8). :
#
#
#
#
#
#
#
#
#
pfctl
pfctl
pfctl
pfctl
pfctl
pfctl
pfctl
pfctl
pfctl
-f /etc/pf.conf
pf.conf
-nf /etc/pf.conf ,
-Nf /etc/pf.conf NAT
-Rf /etc/pf.conf
-sn
NAT
-sr
-ss
-si
-sa
man pfctl(8).
:
, - IP ,
.. , IP ,
, IP .
{}.
pfctl(8) , ,
. :
ext_if = "fxp0"
host1 = "192.168.1.1"
host2 = "192.168.1.2"
all_hosts = "{" $host1 $host2 "}"
$all_hosts 192.168.1.1, 192.168.1.2.
pfctl
:
IPv6 / IPv4.
, . , ,
, 50 000 - 50 .
:
pf.conf pfctl(8).
:
pf.conf table. :
const - . , pfctl(8)
, ,
securelevel(7), .
persist - , .
, , , .
10.0.0.0/8 }
table <spammers> persist
172.16.50.5 - - 172.16.0.0/16;
172.16.1.25 - - !172.16.1.0/24; ,
( "!" );
172.16.1.100 - 172.16.1.100;
10.1.4.55 -
Default Deny
quick
Keeping State
Keeping State UDP
TCP
TCP SYN Proxy
IP
:
- , . pf(4)
3 OSI (IPv4 IPv6) 4 OSI (TCP, UDP, ICMP, ICMPv6).
- , , .
, . .
, quick, ,
, , . ,
. " " , , ,
.
:
:
quick, ,
.
interface
, .
af
, inet IPv4 inet6 IPv6. PF ,
/ .
protocol
:
tcp
udp
icmp
icmp6
/etc/protocols
0 255
src_addr, dst_addr
/ IP. :
IPv4 IPv6.
. , , .
CIDR
, DNS .
IP .
, / ( /24).
, CIDR
(). PF , IP ()
. , DHCP
, .
, :network :broadcast.
CIDR ( 192.168.0.0/24) ( 192.168.0.255)
.
, () ! ("") .
,
any
all, from any to any.
src_port, dst_port
/. :
1 65535
/etc/services
,
:
o ! = ( )
o < ( )
o > ( )
o <= ( )
o >= ( )
o > <()
o <> ( )
- ( ) .
tcp_flags
, TCP proto tcp.
flags check/mask. : flags S/SA - PF S A (SYN ACK) ,
SYN.
state
, , .
Default Deny:
"default deny".
"default deny":
block in all
block out all
, , IP .
:
, . - ,
/, / , . ,
, () , .
, .
:
block in quick on fxp0 proto tcp from any to any port ssh
pass in all
, quick,
, ssh. .
Keeping State:
PF "keeping state" "stateful inspection".
PF . , PF
, ,
. , .
Keeping state ,
. PF , , , stateful
ruleset , , PF .
keep state, , "state"
. ,
.
:
pass out on fxp0 proto tcp from any to any keep state
TCP
"state" . - ,
, " state"
.
modulate state keep state, , TCP.
modulate state Initial Sequence Number (ISN) .
, , , ISN
.
Keep state TCP, UDP, ICMP ISN:
pass out on fxp0 proto tcp from any to any modulate state
pass out on fxp0 proto { udp, icmp } from any to any keep state
keeping state - ICMP . , keep state
TCP , ICMP , ICMP
.
, stateful , .
, pf, "default deny".
,
.
nat, binat, rdr "state" , .
Keeping State UDP:
, UDP "state" , ! , UDP
( ),
PF UDP . "" "" , PF
, . , "state"
. pf.conf
TCP:
F: .
S: , .
R: .
P: push.
.
A: , , .
U: , , urg=1.
E: ECE - (.. - ...)
W: CWR - (.. - ...)
PF flags :
flags check/mask
mask check , () ""
.
pass in on fxp0 proto tcp from any to any port ssh flags S/SA
TCP SYN, SYN ACK.
: OpenBSD, :
. . . flags S
. /mask .
keep state .
pass out on fxp0 proto tcp all flags S/SA keep state
TCP SYN,
SYN ACK.
., .
, " SYN ".
:
. . . flags S/SAFR
, FIN RST scrubbed.
PF TCP ( SYN
FIN SYN RST). :
scrub in on fxp0
.
.
.
pass in on fxp0 proto tcp from any to any port ssh flags S/SA \
keep state
TCP SYN Proxy:
, , pf
. PF . PF
, .
, , .
spoofed TCP SYN ,
.
IP 10.0.0.1 fxp0.
, ,
.
: antispoof ,
. . :
.
IP:
pf IP. ,
nmap. , , multicast IGMP,
allow-opts:
ext_if = "fxp0"
int_if = "dc0"
lan_net = "192.168.0.0/24"
# scrub incoming packets
scrub in all
# setup a default deny policy
block in all
block out all
# pass traffic on the loopback interface in either direction
pass quick on lo0 all
# activate spoofing protection for the internal interface.
antispoof quick for $int_if inet
# only allow ssh connections from the local network if it's from the
# trusted computer, 192.168.0.15. use "block return" so that a TCP RST is
# sent to close blocked connections right away. use "quick" so that this
# rule is not overridden by the "pass" rules below.
block return in quick on $int_if proto tcp from ! 192.168.0.15 \
to $int_if port ssh flags S/SA
# pass all traffic to and from the local network
pass in on $int_if from $lan_net to any
pass out on $int_if from any to $lan_net
# pass tcp, udp, and icmp out on the external (Internet) interface.
# keep state on udp and icmp and modulate state on tcp.
pass out on $ext_if proto tcp all modulate state flags S/SA
pass out on $ext_if proto { udp, icmp } all keep state
# allow ssh connections in on the external interface as long as they're
# NOT destined for the firewall (i.e., they're destined for a machine on
# the local network). log the initial packet so that we can later tell
# who is trying to connect. use the tcp syn proxy to proxy the connection.
pass in log on $ext_if proto tcp from any to { !$ext_if, !$int_if } \
port ssh flags S/SA synproxy state
NAT
NAT
NAT
IP
NAT
Bidirectional Mapping (1:1 mapping)
NAT
:
(NAT) - ( ) IP . NAT ,
IP , ,
Internet. NAT RFC 1631.
NAT , RFC 1918. ,
:
IP (, 192.168.1.35)
TCP UDP (, 2132)
NAT, , , , NAT
. NAT , ,
a),
b) , .
, :
IP: (, 24.5.0.5)
: , (, 53136)
, Internet . NAT
- Internet. , Internet NAT ;
.
IP NAT (24.5.0.5) (53136). NAT
, , .
IP/, PF, ,
192.168.1.35. PF
.
ICMP , .
NAT :
:
.
pass NAT. NATed .
,
IP .
IP :
NAT ,
OpenBSD . sysctl(3) :
# sysctl -w net.inet.ip.forwarding=1 # sysctl -w net.inet6.ip6.forwarding=1 (if using IPv6)
, , /etc/sysctl.conf:
net.inet.ip.forwarding=1 net.inet6.ip6.forwarding=1
, ( #) . #,
. IP .
NAT:
NAT :
nat [pass] on interface [af] from src_addr [port src_port] to \
dst_addr [port dst_port] -> ext_addr [pool_type] [static-port]
nat
, NAT
pass
,
interface
,
af
, inet IPv4 inet6 IPv6. PF ,
/ .
src_addr
, . :
o
o
o
o
o
o
o
o
o
o
IPv4 IPv6.
CIDR
, DNS .
IP .
. , , .
, / ( /24).
, CIDR
, :network.
CIDR ( 192.168.0.0/24).
.
, () ! ("") .
,
any
src_port
. :
o
o
o
o
1 65535
/etc/services
,
:
! = ( )
< ( )
> ( )
<= ( )
>= ( )
> <()
<> ( )
- ( ) .
port NAT,
().
dst_addr
, . ,
.
dst_port
. src_port.
ext_addr
o
o
o
o
o
NAT , .
:
IPv4 IPv6.
CIDR
, DNS .
IP .
. , , .
(). PF , IP ()
. , DHCP
, .
o
o
, :network.
CIDR ( 192.168.0.0/24).
,
pool_type
, .
static-port
PF TCP UDP .
:
web_serv_int = "192.168.1.100"
web_serv_ext = "24.5.0.6"
binat on tl0 from $web_serv_int to any -> $web_serv_ext
:
no. :
# pfctl -s state
TCP 192.168.1.35:2132 -> 24.5.0.5:53136 -> 65.42.33.245:22 TIME_WAIT:TIME_WAIT
UDP 192.168.1.35:2491 -> 24.5.0.5:60527 -> 24.2.68.33:53 MULTIPLE:SINGLE
( ):
TCP
, .
192.168.1.35:2132
(192.168.1.35) (2132) . ,
IP .
24.5.0.5:53136
IP (24.5.0.5) (53136) , .
65.42.33.245:22
IP (65.42.33.245) (22), .
TIME_WAIT:TIME_WAIT
- .
o DNS
o DMZ
o TCP
o RDR NAT
:
NAT, Internet. , NAT,
. , .
, NAT.
:
rdr on tl0 proto tcp from any to any port 80 -> 192.168.1.20
, TCP 80 , ,
192.168.1.20. from any to any rdr . IP
, :
27.146.49.0/24.
. ,
, .
:
:
.
pass rdr.
.
, , .
:
192.0.2.1 -
24.65.1.13 - OpenBSD
192.168.1.5 -
: 192.0.2.1
: 4028 ( )
: 24.65.1.13
: 80
rdr:
: 192.0.2.1
: 4028
: 192.168.1.5
: 8000
.
:
. ,
. web CGI
, , .
, , , .
" "(DMZ) " "(PSN).
, , DMZ.
:
, , Internet
, :
server = 192.168.1.40
rdr on $ext_if proto tcp from any to $ext_if port 80 -> $server \
port 80
. ,
, ($ext_if, , ).
, , ,
. TCP/IP
,
. . , PF
, .
.
, TCP
. ,
. ,
, .
.
, pf,
.
. .
DNS:
DNS, ,
Internet. , ,
.
.
DMZ:
.
, , ,
Internet. , .
( Internet), ,
,
.
TCP :
- TCP, ,
, .
, - , ,
.
inetd(8) nc(1). etc/inetd.conf ,
(127.0.0.1) 5000. 80 192.168.1.10.
# sysctl -w net.inet.ip.redirect=0
# sysctl -w net.inet6.ip6.redirect=0 (if using IPv6)
, ...
PF
o
o Return
o
:
PF . - .
, .
, , .
:
, , ,
, .., . IP ? ,
.
PF , . , ,
, . -
, :
, .
:
"default deny", :
block in all block out all
:
block all
, pf , ,
.
"from any to any" "all" . :
block in on rl0 all pass in quick log on rl0 proto tcp from any to any port 22 keep state
:
block in on rl0 pass in quick log on rl0 proto tcp to port 22 keep state
Return:
ICMP Unreachable TCP RST :
block in all block return-rst in proto tcp all block return-icmp in proto udp all block out all block return-rst out proto tcp all
block return-icmp out proto udp all
:
block return
PF return, , ,
.
:
. , :
pass in log quick on rl0 proto tcp to port 22 \ flags S/SA keep state queue ssh label ssh
:
pass in quick log on rl0 proto tcp to port 22 \ queue ssh keep state label ssh flags S/SA
.