3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

今さらながらiptables基本(その3)

Posted at

iptablesログ

今回はiptablesのログ基本を取り上げる。

ネットワーク環境

前回と同じで、NAT環境を用いる。
image.png

ログ出力先

デフォルトでは、”/var/log/kern.log”(ubuntuでは)に、iptablesログが出力されるようだ。iptablesのログのみとしたい。下記URLの情報をもとに、出力先を変更する。

まずは、”/etc/rsyslog.conf”最終行を確認。

#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf

という記述が見つかるので、iptablesログに関するファイルを新規に作成。

$ cat /etc/rsyslog.d/10-iptables.conf
:msg, contains, "[iptables] " -/var/log/iptables.log
& ~
$ sudo systemctl restart rsyslog

キーワード「[iptables] 」が見つかれば、その内容を”/var/log/iptables.log”に記録する。なお、2行目の”& ~”がないと、”/var/log/kern.log”および”/var/log/syslog”にログが残ってしまうことに注意。

検証1

まずは、上記の設定状態で確認する。

NATルーター(iptables)設定

$ sudo iptables -A FORWARD -j LOG --log-prefix "[iptables] "
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
LOG        all  --  anywhere             anywhere             LOG level warning prefix "[iptables] "

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    

転送されたパケットをすべてログに記録する("[iptables] "というPrefixをつけて)。

実行

左側VMからpingの実施。

# ping -c 1 172.16.0.21
PING 172.16.0.21 (172.16.0.21) 56(84) bytes of data.
64 bytes from 172.16.0.21: icmp_seq=1 ttl=63 time=0.729 ms

上記のコマンドによるログは下記となる。

$ cat /var/log/iptables.log 
Jan 29 13:07:44 vamos kernel: [12489.083678] [iptables] IN=enp0s8 OUT=enp0s9 MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=49375 DF PROTO=ICMP TYPE=8 CODE=0 ID=12427 SEQ=1 
Jan 29 13:07:44 vamos kernel: [12489.084071] [iptables] IN=enp0s9 OUT=enp0s8 MAC=08:00:27:7f:54:7d:08:00:27:c2:b3:07:08:00 SRC=172.16.0.21 DST=192.168.10.10 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=44948 PROTO=ICMP TYPE=0 CODE=0 ID=12427 SEQ=1 

ICMPの”Echo Request”および”Echo Reply”が、いくつかのヘッダ情報とともに、記録されている。

検証2

次に、検証1の設定を削除し、TCPオプションをログに記録してみる。

NATルーター(iptables)設定

$ sudo iptables -D FORWARD 1
$ sudo iptables -A FORWARD -p tcp -j LOG  --log-prefix "[iptables] " --log-tcp-options
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
LOG        tcp  --  anywhere             anywhere             LOG level warning tcp-options prefix "[iptables] "

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  

実行

右VMで、待ち受け。

# nc -l -p 1234

左VMで、TCP接続実施。

# nc -z 172.16.0.21 1234

この時のログは下記となる。

$ cat /var/log/iptables.log 
Jan 29 13:21:30 vamos kernel: [13314.590630] [iptables] IN=enp0s8 OUT=enp0s9 MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=5967 DF PROTO=TCP SPT=59786 DPT=1234 WINDOW=64240 RES=0x00 SYN URGP=0 OPT (020405B40402080ABCF634C80000000001030306) 
Jan 29 13:21:30 vamos kernel: [13314.591080] [iptables] IN=enp0s9 OUT=enp0s8 MAC=08:00:27:7f:54:7d:08:00:27:c2:b3:07:08:00 SRC=172.16.0.21 DST=192.168.10.10 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=0 DF PROTO=TCP SPT=1234 DPT=59786 WINDOW=65160 RES=0x00 ACK SYN URGP=0 OPT (020405B40402080A2338FA0EBCF634C801030306) 
Jan 29 13:21:30 vamos kernel: [13314.591387] [iptables] IN=enp0s8 OUT=enp0s9 MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=5968 DF PROTO=TCP SPT=59786 DPT=1234 WINDOW=1004 RES=0x00 ACK URGP=0 OPT (0101080ABCF634C92338FA0E) 
Jan 29 13:21:30 vamos kernel: [13314.591401] [iptables] IN=enp0s8 OUT=enp0s9 MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=5969 DF PROTO=TCP SPT=59786 DPT=1234 WINDOW=1004 RES=0x00 ACK FIN URGP=0 OPT (0101080ABCF634C92338FA0E) 
Jan 29 13:21:30 vamos kernel: [13314.591829] [iptables] IN=enp0s9 OUT=enp0s8 MAC=08:00:27:7f:54:7d:08:00:27:c2:b3:07:08:00 SRC=172.16.0.21 DST=192.168.10.10 LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=61456 DF PROTO=TCP SPT=1234 DPT=59786 WINDOW=1019 RES=0x00 ACK FIN URGP=0 OPT (0101080A2338FA0FBCF634C9) 
Jan 29 13:21:30 vamos kernel: [13314.592121] [iptables] IN=enp0s8 OUT=enp0s9 MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=52 TOS=0x00 PREC=0x00 TTL=63 ID=5970 DF PROTO=TCP SPT=59786 DPT=1234 WINDOW=1004 RES=0x00 ACK URGP=0 OPT (0101080ABCF634CA2338FA0F)

TCPシーケンスおよびヘッダ情報が見える。この時、左VMのWiresharkで取得した情報は下記となる。
LogForTCP.png
当然ながら、情報は一致。

検証3

ログの使い分けを行う。参考URLは下記。

ここでは、DropログとNAT転送ログ、それぞれ別々のファイルに記録する。syslog設定ファイルは下記となる。

$ cat /etc/rsyslog.d/10-iptables.conf 
:msg, contains, "[iptables] " -/var/log/iptables.log
& ~
:msg, contains, "[iptables_drop] " -/var/log/iptables_drop.log
& ~
:msg, contains, "[iptables_nat] " -/var/log/iptables_nat.log
& ~
$ sudo systemctl restart rsyslog

NATルーター(iptables)設定

ログ設定は下記となる。

$ sudo iptables -t nat -N LOG_NAT
$ sudo iptables -t nat -A LOG_NAT -j LOG --log-prefix "[iptables_nat] "
$ sudo iptables -t nat -A LOG_NAT -j ACCEPT

$ sudo iptables -N LOG_DROP
$ sudo iptables -A LOG_DROP -j LOG --log-prefix "[iptables_drop] "
$ sudo iptables -A LOG_DROP -j DROP

新しくChainを設け、それぞれ異なるログPrefixをつける。NATの基本動作は”ACCEPT”、Dropの基本動作は”DROP”とする。

NATおよびDropの設定は下記となり、前回の内容に上記ログ設定を付加している。

$ sudo iptables -t nat -A POSTROUTING -o enp0s9 -j MASQUERADE
$ sudo iptables -t nat -A PREROUTING -j LOG_NAT

$ sudo iptables -A FORWARD -i enp0s8 -o enp0s9 -j ACCEPT
$ sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
$ sudo iptables -A FORWARD -j LOG_DROP
$ sudo iptables -P FORWARD DROP

なお、NATログでは、Chainとして”PREROUTING”を用いている。(”POSTROUTING”では正しく動作せず、、であった。)

設定状況は下記のとおり。

$ sudo iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
LOG_NAT    all  --  anywhere             anywhere            

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  anywhere             anywhere            

Chain LOG_NAT (1 references)
target     prot opt source               destination         
LOG        all  --  anywhere             anywhere             LOG level warning prefix "[iptables_nat] "
ACCEPT     all  --  anywhere             anywhere            

$ sudo iptables -L 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
LOG_DROP   all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain LOG_DROP (1 references)
target     prot opt source               destination         
LOG        all  --  anywhere             anywhere             LOG level warning prefix "[iptables_drop] "
DROP       all  --  anywhere             anywhere

実行

右側VMでpingの実行。

# ping 192.168.10.10

NAT外側から内側へのpingは通らない(Dropする)ので、Dropログに記録が残る。

$ cat /var/log/iptables_drop.log
Jan 29 17:46:28 vamos sudo:  uuuuu : TTY=pts/0 ; PWD=/home/uuuuu ; USER=root ; COMMAND=/usr/sbin/iptables -A LOG_DROP -j LOG --log-prefix [iptables_drop] 
Jan 29 17:47:36 vamos kernel: [  220.603695] [iptables_drop] IN=enp0s9 OUT=enp0s8 MAC=08:00:27:7f:54:7d:08:00:27:c2:b3:07:08:00 SRC=172.16.0.21 DST=192.168.10.10 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=26634 DF PROTO=ICMP TYPE=8 CODE=0 ID=14735 SEQ=1 

次に、右VMで、待ち受け。

# nc -l -p 1234

左VMで、TCP接続実施。

# nc -z 172.16.0.21 1234

NAT転送ログは下記となる。

$ cat /var/log/iptables_nat.log
Jan 29 17:46:15 vamos sudo:  uuuuu : TTY=pts/0 ; PWD=/home/uuuuu ; USER=root ; COMMAND=/usr/sbin/iptables -t nat -A LOG_NAT -j LOG --log-prefix [iptables_nat] 
Jan 29 17:47:36 vamos kernel: [  220.603664] [iptables_nat] IN=enp0s9 OUT= MAC=08:00:27:7f:54:7d:08:00:27:c2:b3:07:08:00 SRC=172.16.0.21 DST=192.168.10.10 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=26634 DF PROTO=ICMP TYPE=8 CODE=0 ID=14735 SEQ=1 
Jan 29 17:48:56 vamos kernel: [  300.296978] [iptables_nat] IN=enp0s8 OUT= MAC=08:00:27:d2:97:f2:08:00:27:ea:cf:18:08:00 SRC=192.168.10.10 DST=172.16.0.21 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=36531 DF PROTO=TCP SPT=59792 DPT=1234 WINDOW=64240 RES=0x00 SYN URGP=0 

NATログの特徴としては、片方向の最初のパケットのみ記録される、、ということであろうか。TCPセッションを見ると、SYNのみ記録されていることから、そのように判断。Chain PREROUTINGで記録しているためかもしれない。

終わりに

とりあえず、基本は把握。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?