okacl's blog

私的なメモ置き場になるヨカーン

iptables TEE target

Linuxでポートミラーリングできないのかと調べていたら、
iptablesのTEEターゲットが見つかったので使えるか試してみた。


Linuxカーネル 2.6.35 から加えられた機能。
http://kernelnewbies.org/Linux_2_6_35
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=e281b19897dc21c1071802808d461627d747a877

iptablesでは1.4.8からサポートされてる。
http://netfilter.org/projects/iptables/files/changes-iptables-1.4.8.txt


以前はiptablesのROUTEターゲットパッチに、似たような機能があったらしい。
http://lists.netfilter.org/pipermail/netfilter-devel/2004-November/017515.html


以下、iptablesのmanからの引用(iptables 1.4.12)

TEE
    The TEE target will clone a packet and redirect this clone  to  another
    machine  on the local network segment. In other words, the nexthop must
    be the target, or you will have to configure the nexthop to forward  it
    further if so desired.

    --gateway ipaddr
           Send  the  cloned  packet  to the host reachable at the given IP
           address.  Use of 0.0.0.0 (for IPv4  packets)  or  ::  (IPv6)  is
           invalid.

    To  forward  all  incoming  traffic on eth0 to an Network Layer logging
    box:

    -t mangle -A PREROUTING -i eth0 -j TEE --gateway 2001:db8::1

iptables-1.4.12/extensions/libxt_TEE.cの中を見てみると、
  oifオプションというのが確認できるが使い方がよくわからない。


Linuxスイッチでポートミラーリング

VMWare上で下図のネットワークを作って実験。

f:id:okacl:20130611034750p:plain

bridgeの設定とかは省略。
Switchのiptablesのバージョンは1.4.12

TEEターゲットにはgatewayオプションが必須でIPアドレスを指定する必要があるため、
SwitchとIDS間のインターフェイスにIPアドレス、ルーティングを設定する。

Switch# ifconfig eth2 192.168.10.1/24
Switch# route add -net 192.168.19.0 netmask 255.255.255.0 dev eth2
IDS# ifconfig eth0 192.168.10.2/24
IDS# route add -net 192.168.19.0 netmask 255.255.255.0 dev eth0

ポートミラーリングの設定。

Switch# iptables -t mangle -A PREROUTING -i br0 -j TEE --gateway 192.168.10.2

iptablesの設定を確認する。

Switch# iptables -v -L -t mangle
Chain PREROUTING (policy ACCEPT 264K packets, 255M bytes)
 pkts bytes target     prot opt in     out     source               destination         
 263K  255M TEE        all  --  br0    any     anywhere             anywhere             TEE gw:ubuntu.local

Chain INPUT (policy ACCEPT 918 packets, 77534 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 2871 packets, 2433K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 264K packets, 255M bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 267K packets, 257M bytes)
 pkts bytes target     prot opt in     out     source               destination         

以上で設定終了。
HostからWebブラウジングしたりすると、下図のようにIDSでモニタリングできる。

f:id:okacl:20130611033928p:plain

因みに以下のように特定の通信だけミラーリングさせることもできる。

Switch# sudo iptables -t mangle -A PREROUTING -i br0 -p tcp --sport 80 -j TEE --gateway 192.168.10.2
Switch# sudo iptables -v -L -t mangle
Chain PREROUTING (policy ACCEPT 60454 packets, 56M bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1199  783K TEE        tcp  --  br0    any     anywhere             anywhere             tcp spt:http TEE gw:ubuntu.local

Chain INPUT (policy ACCEPT 239 packets, 20276 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 1358 packets, 516K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 1394 packets, 798K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 2755 packets, 1314K bytes)
 pkts bytes target     prot opt in     out     source               destination