自宅鯖
諸事情がありまして、mcu.jpを自宅に持ってきて運営することになりました。
というわけで、自鯖メンテ中なので、使えません。
とりあえず、eoに固定IPの申し込みをする。4200円/月。高い・・・
OSはそのままFreeBSDで。
200Mプランなので、ギガビット対応のルーターが欲しいところだけど(RTX1200とか)
仕方なくルーターを兼ねる事にする。
以前から pf を使っていたが ftp は公開してなかった。今回は公開するので vsftpd を使う。
#余談だが動的IPでもpassiveモードでDDNSを使って簡単に使えるようになってた。
サーバーにはグローバルなIPを持たしてないので、passiveで渡すアドレスにグローバルIPを設定してやる。
pf で nat 越えをするためにはproxyを使うんだが、 ftp/pftpx(ftp-proxyの後継版?)を使う。
pf のanchor を使う。miniupnpd と同じように動的にポートの開閉もやってくれるようだ。
これで、問題なく外からpassiveで使えるようになったが、ローカルネットワークからpassiveで繋がらない。(activeは無問題)
理由はpassiveで返ってくるIPがグローバルを指定するからだ。
ぐぐって見ると、xinetdで内向き外向きを設定してやるという方法が見つかった。
http://www.oceans.mydns.jp/log/eid271.html
が、pftpxを使えば解決できるので、書いておく。
pf.confのftp-proxyの辺に
rdr pass on $int_if inet proto tcp from any to $ftp_srv port ftp -> 127.0.0.1 port 8021
と書いておく。
これで、passiveで渡されるIPがローカルに変換されるようになる。
以下覚書。
・mkfilter.sh (apnicから国別でIPブロックを抜き出すスクリプト)
#!/bin/sh
wget http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
cat delegated-apnic-latest | grep KR | grep -v ipv6 | awk -f mask.awk >filter_kr
cat delegated-apnic-latest | grep CN | grep -v ipv6 | awk -f mask.awk >filter_cn
cat delegated-apnic-latest | grep HK | grep -v ipv6 | awk -f mask.awk >filter_hk
cat delegated-apnic-latest | grep TW | grep -v ipv6 | awk -f mask.awk >filter_tw
cat delegated-apnic-latest | grep JP | grep -v ipv6 | awk -f mask.awk >filter_jp
rm delegated-apnic-latest
・mask.awk
BEGIN {
FS = "|"
}
{
if ($5 == "256") print $4"/24"
if ($5 == "512") print $4"/23"
if ($5 == "1024") print $4"/22"
if ($5 == "2048") print $4"/21"
if ($5 == "4096") print $4"/20"
if ($5 == "8192") print $4"/19"
if ($5 == "16384") print $4"/18"
if ($5 == "32768") print $4"/17"
if ($5 == "65536") print $4"/16"
if ($5 == "131072") print $4"/15"
if ($5 == "262144") print $4"/14"
if ($5 == "524288") print $4"/13"
if ($5 == "1048576") print $4"/12"
if ($5 == "2097152") print $4"/11"
if ($5 == "4194304") print $4"/10"
if ($5 == "8388608") print $4"/9"
if ($5 == "16777216") print $4"/8"
if ($5 == "33554432") print $4"/7"
if ($5 == "67108864") print $4"/6"
if ($5 == "134217728") print $4"/5"
if ($5 == "268435456") print $4"/4"
if ($5 == "536870912") print $4"/3"
if ($5 == "1073741824") print $4"/2"
if ($5 == "2147483648") print $4"/1"
}
END {
}
・pf.conf
ext_if="ng0" # replace with actual external interface name i.e., dc0
int_if="em0" # replace with actual internal interface name i.e., dc1pproximately how many minutes per month do you talk on a mobile phone?
int_net="192.168.1.0/24"
www_srv="192.168.1.1"
ftp_srv="192.168.1.1"
table <local> const {127.0.0.1, 192.168.1.1 }
table <private> const { 10/8, 172.16/12, 192.168/16 }
table <special> const { 0/8, 14/8, 24/8, 39/8, 127/8, 128.0/16, 169.254/16, 192.0.0/24, \
192.0.2/24, 192.88.99/24, 198.18/15, 223.255.255/24, 224/4, 240/4 }
table <japan> persist file "/etc/pf/filter_jp"
table <korea> persist file "/etc/pf/filter_kr"
table <china> persist file "/etc/pf/filter_cn"
# Options: tune the behavior of pf, default values are given.
set timeout { interval 10, frag 30 }
set timeout { tcp.first 120, tcp.opening 30, tcp.established 86400 }
set timeout { tcp.closing 900, tcp.finwait 45, tcp.closed 90 }
set timeout { udp.first 60, udp.single 30, udp.multiple 60 }
set timeout { icmp.first 20, icmp.error 10 }
set timeout { other.first 60, other.single 30, other.multiple 60 }
set timeout { adaptive.start 0, adaptive.end 0 }
set limit { states 10000, frags 5000 }
set loginterface ng0
set optimization normal
set block-policy drop
#set require-order yes
#set fingerprints "/etc/pf.os"
scrub in on $ext_if all fragment reassemble
scrub out on $ext_if all max-mss 1414
nat on $ext_if from $int_net to any -> ($ext_if)
rdr on $ext_if inet proto tcp from any to any port http -> $www_srv port http
rdr on $ext_if proto tcp from <japan> to ($ext_if) port 22 -> ($int_if) port 22
nat-anchor "pftpx/*"
rdr-anchor "pftpx/*"
rdr pass on $ext_if inet proto tcp from any to any port ftp -> 127.0.0.1 port 8021
rdr pass on $int_if inet proto tcp from any to $ftp_srv port ftp -> 127.0.0.1 port 8021
rdr-anchor "miniupnpd"
anchor "miniupnpd"
anchor "pftpx/*"
block log all
block quick on $ext_if from { <korea>, <china> } to any
block in log quick on $ext_if from { <local> <private> <special> } to any
block out quick on $ext_if from any to { <local> <private> <special> }
block out quick on $ext_if proto {tcp udp} from any to any port 135
block out quick on $ext_if proto {tcp udp} from any to any port 137:139
block out quick on $ext_if proto {tcp udp} from any to any port 445
#www
pass in quick on $ext_if proto tcp from any to $www_srv port http flags S/SA modulate state
#ssh
pass in quick on $ext_if proto tcp from any port > 1023 to ($int_if) port 22 keep state
#ICMP
pass in quick on $ext_if inet proto icmp all icmp-type echoreq keep state
#int -> ext
pass out quick on $ext_if proto { udp, tcp, icmp } from any to any keep state
#loop back & internal
pass quick on lo0 all
pass quick on $int_if all
・miniupnpd.conf
# WAN network interface
ext_ifname=ng0
# if the WAN interface has several IP addresses, you
# can specify the one to use below
ext_ip=219.75.254.170
# there can be multiple listening ips for receiving SSDP traffic.
# the 1st IP is also used for UPnP Soap traffic.
listening_ip=192.168.1.1/24
port=5555
# bitrates reported by daemon in bits per second
bitrate_up=131072
bitrate_down=524288
# default presentation url is http address on port 80
#presentation_url=
# report system uptime instead of daemon uptime
system_uptime=yes
# notify interval in seconds default is 30 seconds.
#notify_interval=240
# log packets in pf
#packet_log=no
# uuid : generated by the install a new one can be created with
# uuidgen
uuid=d813a72b-7c1f-11dd-9f2c-0007e90d058a
# UPnP permission rules
# (allow|deny) (external port range) ip/mask (internal port range)
# A port range is <min port>-<max port> or <port> if there is only
# one port in the range.
# ip/mask format must be nn.nn.nn.nn/nn
allow 1024-65535 192.168.1.0/24 1024-65535
deny 0-65535 0.0.0.0/0 0-65535