labunix's blog

labunixのラボUnix

sshクライアント情報を確認出来るパケットキャプチャ環境を構築してみる。

■sshクライアント情報を確認出来るパケットキャプチャ環境を構築してみる。
 [-s0]は上限無しのため、manマニュアルにあるように取りこぼしの可能性があるので注意。

$ cat myscripts/mysshcap.sh 
#!/bin/bash

if [ `id -u` -ne 0 ];then
  echo "Sorry,Not Permit User!" >&2
  exit 1
fi

# running as root
OPT1="-Z root"

# with the -w option every rotate_seconds seconds.
OPT2="-G 3600"

# Compress. with the -C or -G options
OPT3="-z gzip"

# Write the raw packets to file
OPT4="-w ssh_%Y%m%d_%H%M%S.pcap"

# Don't convert addresses
OPT5="-nn"

# tcpdump filter option
MYFILTER="tcp dst port 8022 and src host 172.16.16.90"

# target interface
MYNIC=eth2

tcpdump -s 0 -i $MYNIC $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $MYFILTER &

■実行

$ chmod +x ./myscripts/mysshcap.sh
$ sudo ./myscripts/mysshcap.sh 

■停止。
 pkillで良い気がします。。。

$ ps -ef | awk '/tcpdum[p]/{print "kill -KILL "$2}' | sudo sh

■ログイン、ログオフだけなら以下のようになる。

$ ls -l ssh_*.pcap
-rw-r--r-- 1 root root 4096  224 21:39 ssh_20160224_213905.pcap

■接続元と接続先、SSHクライアントのバージョンが分かればとりあえず。

$ sudo tshark -r ssh_20160224_213905.pcap -d "tcp.port==8022,ssh" -Y "ssh.protocol" -z conv,ip 2>/dev/null
  3   0.001023 172.16.16.90 -> 172.16.16.10 SSH 105 Client: Protocol (SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u1)
 ================================================================================
IPv4 Conversations
Filter:<No Filter>
                                               |       <-      | |       ->      | |     Total     |    Relative    |   Duration   |
                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |
172.16.16.10         <-> 172.16.16.90              14      3715       0         0      14      3715     0.000000000         1.7189
 ================================================================================