LoginSignup
0
0

More than 3 years have passed since last update.

fluentd+audit.logでのsudo監視

Last updated at Posted at 2019-06-07

やりたいこと

踏み台サーバ等でroot作業とかされそうな時、少なくともそれは把握したいという気持ち

つかうもの

fluentd
auditd(ec2デフォルトならはじめから入ってる)
auditログのfluent用パーサ

auditログはログ種別によって微妙に出力違ったりしてつらい
が、既に偉大なる先人がプラグイン作ってくれてたのでありがたく使う

fluentdやらauditのインストール方法はちょっとぐぐればどっさり出てくるので割愛

aushapeは?

syslogに載せて使う分には困らないかもしれないけど、自分の場合は専用のログファイルにしたかったので
--fileオプション使って色々確認したところ、audit設定のローテートとは特に関連せず、
ファイルポインタをauditのreboot無しにリセットする方法を少なくとも自分は見つけられなかったので、
ローテートで苦労しそうだなということで利用を断念。

プラグインインストール

td-agent-gem install fluent-plugin-filter-parse-audit-log

※注 プラグインのgithubにあるREADMEのインストールコマンドはgem名微妙に間違ってる

設定(一例)

<source>
  @type tail
  path /var/log/audit/audit.log
  pos_file /var/log/td-agent/audit.log.pos
  read_from_head true
  <parse>
    @type none
  </parse>
  tag audit
</source>

<filter audit>
  @type parse_audit_log
  flatten true
</filter>

<filter audit>
  @type grep
  <regexp>
    key header_type
    pattern /USER_START/
  </regexp>
  <regexp>
    key body_msg_acct
    pattern /root/
  </regexp>
  <regexp>
    key body_msg_exe
    pattern /\/usr\/bin\/su\"/
  </regexp>
  <regexp>
    key body_msg_res
    pattern /success/
  </regexp>
</filter>

<filter audit>
  @type record_transformer
  enable_ruby
  <record>
    alert_time ${Time.at(record["header_msg"][/[0-9]+/].to_i).to_s}
  </record>
</filter>

<match audit>
  @type slack
  webhook_url [[slack webhook url]]
  channel [[channel]]
  username [[slack username]]
  icon_emoji [[slack icon]]
  color warning
  title "[%s] sudo login"
  title_keys alert_time
  message "With great power comes great responsibility."
  flush_interval 10s
</match>

これで誰かがrootなったときにslackへ通知されるようになります
こんなことするくらいならrootになれんようにすりゃええがな説とか色々ありますが、世の中そうはいかんこともあるということで…

grep部分はもっと良い引っ掛け方あるとは思うんだけどまあ一旦これで困らなさそうだしいいかな
remoteIPがあればもっと良いんだけど…

transform部分は失敗すると問答無用で例外吐くのでやらんほうが良い説はあります

0
0
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
0
0