LoginSignup
7
7

More than 5 years have passed since last update.

secureログを分析してみた (CentOS->fluentd->BigQuery)

Posted at

BigQueryの勉強がてら、secureログ(sshフル開放)をfluentd経由でBigQueryにStream Insertでぶち込んだ。
以下の構成で丸一日放置した
※需要(ストック)があれば、構築手順も公開します。

構成

  • CentOS
  • fluentd
  • fluent-plugin-bigquery(fluentdからBigQueryに転送するプラグイン)
  • sshd_config: PermitRootLogin no
  • SQL知識はほぼ無い

結論

  • 一日に約10万件のログが残った
  • rootに対するアタックが圧倒的に多い
    • rootのログインは無効化しましょう
    • rootはパスワードではなく公開鍵を使いましょう
  • 他のユーザに対しても辞書攻撃はされているので、パスワードは長く、もしくは公開鍵を使いましょう
  • そもそもSSHのフル開放はやめましょう

分析結果

総件数

SQL文
SELECT COUNT(*) FROM [your_dataset.your_table]

Google_BigQuery.png

rootに関するログ集計

SQL文
SELECT message,COUNT(*) AS COUNT FROM [your_dataset.your_table] where message like '%root%' GROUP BY message ORDER BY COUNT DESC

Google_BigQuery.png

施行したユーザ名

SQL文
SELECT message,COUNT(*) AS COUNT FROM [your_dataset.your_table] where message like 'input_userauth_request: invalid user%' GROUP BY message ORDER BY COUNT desc

Google_BigQuery.png

失敗したIPとユーザ名

SQL文
SELECT message,COUNT(*) AS COUNT FROM [your_dataset.your_table] where message like 'pam_unix(sshd:auth): authentication failure;%' GROUP BY message ORDER BY COUNT DESC

Google_BigQuery.png

参考

fluentdの設定ファイル

/etc/td-agent/td-agent.conf
<source>
  type tail
  path /var/log/secure
  tag syslog.secure
  format syslog
</source>

<match syslog.secure>
  type bigquery

  method insert

  auth_method private_key
  email your_acount@developer.gserviceaccount.com
  private_key_path /path/to/your_project-id.p12

  project your_project_id
  dataset your_dataset
  table your_talbe

  time_format %s
  time_field time
  schema_path /path/to/your_schema.json
</match>
  • secureファイルはtd-agentユーザから読み込める権限を付与する

BigQuery用schemaファイル

  • 事前にこのファイルを元にBigQueryのテーブルを作成しておく
/path/to/your_schema.json
[
  {
    "name": "host",
    "type": "STRING"
  },
  {
    "name": "ident",
    "type": "STRING"
  },
  {
    "name": "pid",
    "type": "INTEGER"
  },
  {
    "name": "message",
    "type": "STRING"
  }
]
7
7
1

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