labunix's blog

labunixのラボUnix

smallfirewallパッケージ(?)の使い方 ~iptables-apply編~

■squeezeには、smallfirewallパッケージ(?)がそのまま使える。
(gitのポートは開いていない)

 smallfirewall
 http://sourceforge.jp/users/labunix/pf/smallfirewall/

$ tar zxvf smallfirewall-20120223_v0.0_working.tar.gz
$ cd smallfirewall-20120223/
$ sudo ./install.sh
Install...
ii  insserv                            1.14.0-2                     Tool to organize boot sequence using LSB init.d script dependencies
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下のパッケージが新たにインストールされます:
  chkconfig
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
9,182 B のアーカイブを取得する必要があります。
この操作後に追加で 69.6 kB のディスク容量が消費されます。
取得:1 http://ftp.jp.debian.org/debian/ squeeze/main chkconfig all 11.0-79.1-2 [9,182 B]
9,182 B を 0秒 で取得しました (39.5 kB/s)
未選択パッケージ chkconfig を選択しています。
(データベースを読み込んでいます ... 現在 28754 個のファイルとディレクトリがインストールされています。)
(.../chkconfig_11.0-79.1-2_all.deb から) chkconfig を展開しています...
man-db のトリガを処理しています ...
chkconfig (11.0-79.1-2) を設定しています ...
done.

if you want to uninstall,run : ./install.sh -u


■smallfirewallの使い方

1.初回の手動実行

$ sudo /bin/bash -x /etc/smallfirewall && echo "ok" | grep "ok"
ok

2.上記を実行しなくても、以下で同様のことを行う

$ sudo /etc/init.d/iptables start

■smallfirewallスクリプトの修正
 ※ユーザのホームディレクトリに置いてもroot権限は必須
 ※設定に失敗してssh接続が切れると、「iptables-apply」からのメッセージは表示されない。
  メッセージに(y/N)で返答せずにタイムアウトすると「/etc/iptables-save」に設定が戻る。
 ※「~/user_smallfirewall」の設定に成功したら、「/etc/smallfirewall」を上書きして構いません。
 
$ sudo cp -pi /etc/smallfirewall ~/user_smallfirewall
$ sudo /bin/bash -x ~/user_smallfirewall && echo "ok" ; sudo iptables-apply -t 10 /etc/iptables-save || /etc/smallfirewall
Applying new ruleset... done.
Can you establish NEW connections to the machine? (y/N)

■例えば以下のような絶望的な設定を行っても、「/etc/smallfirewall」から設定を復元してくれる。

$ su root -c 'iptables -F; \
  iptables -A INPUT DROP; \
  iptables -A OUTPUT DROP; \
  iptables -A FORWARD DROP ; \
  iptables-apply -t 2 /etc/iptables-save || /etc/smallfirewall'

■「~/user_smallfirewall」で設定が成功したら、initスクリプトで再読み込みすれば、「/etc/iptables-save」に反映する。

$ sudo /etc/init.d/iptables restart

■最後に設定を上書き

$ sudo mv ~/user_smallfirewall /etc/smallfirewall

    
■基本的にパケットは破棄しない。
 「LIMITOPT="-m limit --limit 10/s --limit-burst 4 "」で10秒間に4回以上の通信をログに残す。
 ログが一杯になる前に少なくとも以下の設定を選択する必要がある。
 
 1.sshポートの設定(デフォルトではブルートフォースアタックをDROPするルールだけ)
 2.DNSの設定
 3.samba(netbios)ポートの設定

■smallfirewallに付属のsfwreportを使って、システムメールで受け取った以下のようなヒントを参考にする

$ sudo ./sfwreport
$ sudo mail -u root
   1432 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2778,ACK,PSH
   1107 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2103,ACK,PSH
    542 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2706,ACK,PSH
     60 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2709,ACK,PSH
     55 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2778,ACK
     42 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2103,ACK
     21 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2706,ACK
      9 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2709,ACK
      7 IN=eth0,OUT=,SRC=192.168.132.1,DST=192.168.132.255,PROTO=UDP,SPT=138,DPT=138
      5 IN=,OUT=eth0,SRC=192.168.132.169,DST=192.168.132.1,PROTO=TCP,SPT=22,DPT=2692,ACK,PSH
      1 IN=eth0,OUT=,SRC=192.168.132.2,DST=192.168.132.169,PROTO=UDP,SPT=53,DPT=60996
      1 IN=eth0,OUT=,SRC=192.168.132.2,DST=192.168.132.169,PROTO=UDP,SPT=53,DPT=60445

■例えば、各プロトコルのログ取得直前に以下のようにルールを追加する。

# for ssh tcp
iptables -A INPUT -i eth0 -p tcp -d 192.168.132.0/24 --dport 22 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -s 192.168.132.0/24 --sport 22 -j ACCEPT

# for samba
iptables -A INPUT -i eth0 -p tcp -d 192.168.132.0/24 -m multiport --ports 139,445 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -d 192.168.132.0/24 -m multiport --ports 139,445 -j ACCEPT

# for netbios udp
iptables -A INPUT -i eth0 -p udp -m multiport --ports 137,138 -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp -m multiport --ports 137,138 -j ACCEPT

# for DNS

iptables -A INPUT -i eth0 -p udp -m multiport --ports 53 -J ACCEPT
iptables -A OUTPUT -o eth0 -p udp -m multiport --ports 53 -J ACCEPT
iptables -A INPUT -i eth0 -p udp --sport 53 -j DROP
iptables -A OUTPUT -i eth0 -p udp --dport 53 -j DROP

■通常の通信で、sfwreportや、/var/log/syslog のログが無くなる事を確認したら、ポリシーを変更する。
 ※変更するポリシーは最後の3行

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

■新しいサービスやデーモンを追加して応答が無いようなら、
 sfwreportでメールを見るなり、syslogのログを直接見るなりして、initスクリプトで停止する。
 ※単純に「iptables -F」とすると、sshの場合セッションが切れます。

$ sudo /etc/init.d/iptables stop
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination