CONTENTS


Lastmodified 2023-10-27 (金) 16:00:05


ipfw

https://www.kkaneko.jp/tools/freebsd/ipfw_rule.html

https://blog.t-mitarai.com/infra/freebsd/security/ipfw/

https://phoenixknight.jp/set-firewall-with-ipfw/

https://www.zabutone.com/freebsd-ipfw/

https://server-setting.info/freebsd/freebsd_ipfw.html

https://syotarow.hatenablog.com/entry/2020/01/06/215106

/etc/rc.conf  で  ipfw を有効にする

/etc/rc.conf に、以下の行を追加する。em0 の部分は、適切な device名 に変更す ること(ifconfig -a で確認できる)。

firewall_enable="YES"
firewall_logdeny="YES"
firewall_script="/etc/ipfw.rules"
firewall_interface="em0"
firewall_type="workstation"
sudo sysrc firewall_enable="YES" && sudo sysrc firewall_logdeny="YES" && sudo sysrc firewall_type="workstation"
echo "net.inet.ip.fw.verbose_limit=5" >> /etc/sysctl.conf
sudo sysrc firewall_script="/etc/ipfw.rules"

Well Known Port

ipfw 設定ファイルの例

https://blog.t-mitarai.com/infra/freebsd/security/ipfw/

#!/bin/sh
# この先頭のコメントはシェルスクリプトの実行パスなので消さないでください
# ファイル名、これは置き場所のメモです
# /etc/ipfw.rules
# ipfwのルールを初期化します
ipfw -q -f flush
# 何度も使うのでルール追加コマンドを変数に入れます
cmd="ipfw -q add"
# ネットワークカード(LANボード)の指定です。
# XXXを ifconfig a で表示される適切なNICカードで指定してください。
# VPSの場合はサーバーのIPアドレスが表示されているものを選びましょう。
pif="em0"     # interface name of NIC attached to Internet
############################################################################
# stateful connections internal systems can create to hosts on the Internet
############################################################################
# DNS
# /etc/resolv.conf に記載のあるDNSサーバのIPアドレスでx.x.x.xを置き換えてください
$cmd 00110 allow tcp from any to x.x.x.x 53 out via $pif setup keep-state
$cmd 00111 allow udp from any to x.x.x.x 53 out via $pif keep-state
# LOCALHOST
$cmd 00120 allow all from any to any via lo0
$cmd 00121 deny  all from any to 127.0.0.0/8
$cmd 00122 deny  all from 127.0.0.0/8 to any
# 80 (WWW)
$cmd 00210 allow all from any to any  80 out via $pif setup keep-state
# 443 (https)
$cmd 00220 allow all from any to any 443 out via $pif setup keep-state
# Allow outbound email connections
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state
$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state
# Allow outbound ping
$cmd 00250 allow icmp from any to any out via $pif keep-state
# Allow outbound NTP
$cmd 00260 allow udp from any to any 123 out via $pif keep-state
# Allow outbound FTP
$cmd 00270 allow tcp from any to any 20 out via $pif setup keep-state
$cmd 00271 allow tcp from any to any 21 out via $pif setup keep-state
# Allow outbound SSH
$cmd 00280 allow tcp from any to any 22 out via $pif setup keep-state
# DENY
$cmd 00299 deny log all from any to any out via $pif
############################################################################
# Deny all inbound traffic from non-routable reserved address spaces
############################################################################
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif     #RFC 1918 private IP
$cmd 00301 deny all from 172.16.0.0/12 to any in via $pif      #RFC 1918 private IP
$cmd 00302 deny all from 10.0.0.0/8 to any in via $pif         #RFC 1918 private IP
$cmd 00303 deny all from 127.0.0.0/8 to any in via $pif        #loopback
$cmd 00304 deny all from 0.0.0.0/8 to any in via $pif          #loopback
$cmd 00305 deny all from 169.254.0.0/16 to any in via $pif     #DHCP auto-config
$cmd 00306 deny all from 192.0.2.0/24 to any in via $pif       #reserved for docs
$cmd 00307 deny all from 204.152.64.0/23 to any in via $pif    #Sun cluster interconnect
$cmd 00308 deny all from 224.0.0.0/3 to any in via $pif        #Class D & E multicast
# Deny public pings
$cmd 00310 deny icmp from any to any in via $pif
# Deny ident
$cmd 00315 deny tcp from any to any 113 in via $pif
# Deny all Netbios services.
$cmd 00320 deny tcp from any to any 137 in via $pif
$cmd 00321 deny tcp from any to any 138 in via $pif
$cmd 00322 deny tcp from any to any 139 in via $pif
$cmd 00323 deny tcp from any to any 81 in via $pif
# Deny fragments
$cmd 00330 deny all from any to any frag in via $pif
# Deny ACK packets that did not match the dynamic rule table
$cmd 00332 deny tcp from any to any established in via $pif
# Allow traffic from ISP's DHCP server.
# Replace x.x.x.x with the same IP address used in rule 00120.
#$cmd 00360 allow udp from any to x.x.x.x 67 in via $pif keep-state
# Allow HTTP connections to internal web server
$cmd 00400 allow all from any to me  80 in via $pif setup keep-state
$cmd 00401 allow all from any to me 443 in via $pif setup keep-state
# Allow inbound SSH connections
$cmd 00410 allow tcp from any to me 22222 in via $pif setup limit kee-state
# Reject and log all other incoming connections
$cmd 00499 deny log all from any to any in via $pif

設定内容の確認

ipfw の設定情報(ルールリスト)は、ipfw show で確認できる.

   # ipfw show

ipfw ルールの動作確認

まずは,実在するマシンに telnet してみる.多分 接続が拒否されると思いますが、それはそれで良いのです.つまり, telnet のパケットが外部に出ているということが分かります.

[image]

sysctl コマンドで、net.inet.ip.fw.verbose=1 を設定すると、 /var/log/secuty ファイルにログが記録されるようになる. (システムを再起動すると、net.inet.ip.fw.verbose の値は元に戻る)。

ipfw show sysctl net.inet.ip.fw.verbose=1 cd /var/log cat security


Total access 520:本日 1:昨日 0

Counter: 520, today: 1, yesterday: 0

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS