Lenny iptables 設定の模索

■iptablesのNATについて

 参考:iptablesの設定
 http://www.nina.jp/server/redhat/iptables/iptables.html

■iptablesのNATモジュール

$ sudo modprobe -l | grep iptable_nat
/lib/modules/2.6.26-2-amd64/kernel/net/ipv4/netfilter/iptable_nat.ko

■IPv4で指定出来るのは以下の通り

$ grep ipv4 /etc/sysctl.conf
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1
#net.ipv4.tcp_syncookies=1
#net.ipv4.ip_forward=1
#net.ipv4.icmp_echo_ignore_broadcasts = 1
#net.ipv4.icmp_ignore_bogus_error_responses = 1
#net.ipv4.conf.all.accept_redirects = 0
# net.ipv4.conf.all.secure_redirects = 1
#net.ipv4.conf.all.send_redirects = 0
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv4.conf.all.log_martians = 1

■詳しくは「man proc」で確認。

$ grep ipv4 /etc/sysctl.conf | \
    sed s/".*\."//g | awk -F\= '{print $1}' | uniq | \
    for list in `xargs` ; do \
      MSG="Not Found : "; \
      sudo find /proc | grep "$list" > /dev/null 2>&1 && MSG="OK  Found :"; \
      echo "$MSG $list" | sort -k 1; \
    done
OK  Found : rp_filter
OK  Found : tcp_syncookies
OK  Found : ip_forward
OK  Found : icmp_echo_ignore_broadcasts
OK  Found : icmp_ignore_bogus_error_responses
OK  Found : accept_redirects
OK  Found : secure_redirects
OK  Found : send_redirects
OK  Found : accept_source_route
OK  Found : log_martians

■設定が無いので、結果も無い。

$ grep -v "^#\|^\$" /etc/sysctl.conf
$ sudo sysctl -p ;echo $?
0

■と、ここまで来て、NATをサポートするより、
 以下2つのオプションでログとログの設定が見やすい方が良いことに気づく。

 ・ユーザ定義チェイン
 ・接続状態(netstatで見れる情報)

iptables -N DEBIAN-TABLE
iptables -A INPUT -j DEBIAN-TABLE
iptables -A FORWARD -j DEBIAN-TABLE
iptables -A OUTPUT -j DEBIAN-TABLE

iptables -A DEBIAN-TABLE -m state --state NEW -j ACCEPT
iptables -A DEBIAN-TABLE -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

iptables -A DEBIAN-TABLE -j LOG --log-level info --log-prefix "[iptables] "

iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP
iptables -A OUTPUT -j DROP

⇒「smallfirewall」という観点では、上記だけでも十分機能する。

■後は、各「fw*」関数を別途スクリプトに追い出して
 「/etc/init.d/smallfirewall」を触らないで済むようにするか。
 よく見る記述方法は下記の2通り。

. /etc/smallfirewall.sh
/bin/sh /etc/smallfirewall.sh

■manの書式を維持したまま、日本語のまま、
  正しくテキスト出力する方法について

 「man2html」の導入。

$ sudo apt-get install man2html w3m
$ whereis -m iptables | awk '{print $2}' | sed s%"man"/%"&ja/"% | xargs man2html | nkf -w8 > iptables.ja.html
$ w3m -dump iptables.ja.html > iptables.ja.txt

■以下のように書式を維持したまま読める。

$ cat iptables.ja.txt | grep -A 2 "^\-N"
-N, --new-chain チェイン
    指定した名前でユーザー定義チェインを作成する。同じ名前のターゲットが既に存
    在してはならない。

$ cat iptables.ja.txt | grep -A 12 "^state"
state

このモジュールは、接続追跡 (connection tracking) と組み合わせて用いると、パケッ
トについての接続追跡状態を知ることができる。

--state state
    state は、マッチングを行うための、コンマで区切られた接続状態のリストである
    。指定可能な state は以下の通り。 INVALID: このパケットは既知の接続と関係し
    ていない。 ESTABLISHED: このパケットは、過去双方向にパケットがやり取りされ
    た接続に属するパケットである。 NEW: このパケットが新しい接続を開始したか、
    双方向にはパケットがやり取りされていない接続に属するパケットである。
    RELATED: このパケットが新しい接続を開始しているが、 FTP データ転送や ICMP
    エラーのように、既存の接続に関係している。

■似たようなところまでのパッケージはいくつかある。
 ライセンスとかドキュメントの書き方をここから得ようと思う。

$ apt-cache search iptables | grep iptables | awk '{print $1}' | column
arno-iptables-firewall          mxallowd
firehol                         uif
ipkungfu                        uruk
iptables-dev                    iptstate
libiptables-chainmgr-perl       iptables
libiptables-parse-perl

ということで、今日はここまで。