m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

inotify-toolsで指定のディレクトリやファイルを監視

Web改ざんってどんな感じなのかな?

不正アクセスされそうな痕跡は見つかっているけど実際にアクセスされているか分からないので監視できそうなツールを探してみた。

設定

インストール
sudo yum -y install inotify-tools --enablerepo=epel

使い方

$ inotifywait test1
Setting up watches.
Watches established.

test1ディレクトリを作成して監視を行う。オプションを何も指定しないで実行すると一回のみの監視となります。で簡単なファイルを作成する

$ cd test1
$ touch aaa.txt

とすると

test1/ CREATE aaa.txt

と表示されます。

何度も監視したい場合は"-m"オプションを使えばいいらしい

監視コマンド起動

$ inotifywait test1 -m
Setting up watches.
Watches established.

いろいろコマンドを実行

$ ll
total 0
-rw-rw-r-- 1 vagrant vagrant 0 Jul 29 13:18 aaa.txt
[vagrant@localhost test1]$ cat aaa.txt 
[vagrant@localhost test1]$ cp -p aaa.txt bbb.txt
[vagrant@localhost test1]$ mv bbb.txt ccc.txt
[vagrant@localhost test1]$ cd ../
[vagrant@localhost ~]$ cd test1/
[vagrant@localhost test1]$ rm -f aaa.txt 
[vagrant@localhost test1]$

検知できたコマンド

test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ OPEN aaa.txt
test1/ CLOSE_NOWRITE,CLOSE aaa.txt
test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ OPEN aaa.txt
test1/ CREATE bbb.txt
test1/ OPEN bbb.txt
test1/ ATTRIB bbb.txt
test1/ ATTRIB bbb.txt
test1/ CLOSE_WRITE,CLOSE bbb.txt
test1/ CLOSE_NOWRITE,CLOSE aaa.txt
test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ MOVED_FROM bbb.txt
test1/ MOVED_TO ccc.txt
test1/ OPEN,ISDIR 
test1/ CLOSE_NOWRITE,CLOSE,ISDIR 
test1/ DELETE aaa.txt
フォーマット変更
inotifywait -mr --format '%T %w%f (%e)' --timefmt '%F %T' 監視ディレクトリ

でフォーマットを指定した監視を行う

$ inotifywait -mr --format '%T %w%f (%e)' --timefmt '%F %T' test1
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
2014-07-29 13:33:14 test1/ (OPEN,ISDIR)
2014-07-29 13:33:14 test1/ (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:36 test1/aaa (CREATE,ISDIR)
2014-07-29 13:33:36 test1/aaa (OPEN,ISDIR)
2014-07-29 13:33:36 test1/aaa (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:38 test1/ (OPEN,ISDIR)
2014-07-29 13:33:38 test1/ (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:44 test1/aaa/aaa.txt (CREATE)
2014-07-29 13:33:44 test1/aaa/aaa.txt (OPEN)
2014-07-29 13:33:44 test1/aaa/aaa.txt (ATTRIB)
2014-07-29 13:33:44 test1/aaa/aaa.txt (CLOSE_WRITE,CLOSE)
2014-07-29 13:33:49 test1/bbb (CREATE,ISDIR)
2014-07-29 13:33:49 test1/bbb (OPEN,ISDIR)
2014-07-29 13:33:49 test1/bbb (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:51 test1/ (OPEN,ISDIR)
2014-07-29 13:33:51 test1/ (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:52 test1/bbb (OPEN,ISDIR)
2014-07-29 13:33:52 test1/bbb/ (OPEN,ISDIR)
2014-07-29 13:33:52 test1/bbb (CLOSE_NOWRITE,CLOSE,ISDIR)
2014-07-29 13:33:52 test1/bbb/ (CLOSE_NOWRITE,CLOSE,ISDIR)
^C
[vagrant@localhost ~]$

こんな感じ…

デーモン起動
inotifywait -mr -o /tmp/test.log --format '%T %w%f (%e)' --timefmt '%F %T' test1 &
  • -o で外部ファイルへ出力
  • -d でデーモンで起動できるらしいけど-oを指定した場合はうまくいかなかったので"&"でデーモン起動する

まとめ

root権限とかとられたら実際のところお死枚かもしれないのでツールが動いているところをわからなくする工夫は必要かもしれないけどこれがあれば自身が何をしたかも多少は分かるかも?全てのコマンドを監視するのは難しいかもしれないけど変なファイルができていたら検知できるので仕込む価値はあるかもしれない

そーいやどのユーザーでアクセスされたかとかはわからないな…