ファイル監視 · 2 min read · Nov 03, 2025
osqueryを使用してLinuxでファイル整合性監視(FIM)を設定する方法

osqueryはオープンソースのオペレーティングシステムの計測、監視、分析ツールです。Facebookによって作成され、オペレーティングシステムをSQLベースのクエリを使用して照会できる高性能なリレーショナルデータベースとして公開します。
osqueryはマルチプラットフォームのソフトウェアで、Linux、Windows、MacOS、FreeBSDにインストールできます。これにより、SQLベースのクエリを使用して、これらのオペレーティングシステムのプロファイル、パフォーマンス、セキュリティチェックなどを探索できます。
このチュートリアルでは、osqueryを使用してファイル整合性監視(FIM)を設定する方法を示します。LinuxオペレーティングシステムのUbuntu 18.04とCentOS 7を使用します。
前提条件
- Linux(UbuntuまたはCentOS)
- ルート権限
- 最初のosqueryガイドを完了していること
何をするか
- Linuxサーバーにosqueryをインストールする
- osqueryのSyslog消費を有効にする
- 基本的なosqueryの設定
- osqueryを使用してファイル整合性監視を設定する
- テスト
ステップ1 - Linuxサーバーにosqueryをインストールする
osqueryはすべてのプラットフォームのインストール用に独自のリポジトリを提供しており、最初のステップは公式のosqueryリポジトリからosqueryパッケージをインストールすることです。
Ubuntuで
osqueryキーをシステムに追加します。
export OSQUERY_KEY=1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $OSQUERY_KEYosqueryリポジトリを追加し、パッケージをインストールします。
sudo add-apt-repository 'deb [arch=amd64] https://pkg.osquery.io/deb deb main'
sudo apt install osquery -yCentOSで
osqueryキーをシステムに追加します。
curl -L https://pkg.osquery.io/rpm/GPG | sudo tee /etc/pki/rpm-gpg/RPM-GPG-KEY-osqueryosqueryリポジトリを追加して有効にし、パッケージをインストールします。
sudo yum-config-manager --add-repo https://pkg.osquery.io/rpm/osquery-s3-rpm.repo
sudo yum-config-manager --enable osquery-s3-rpm
sudo yum install osquery -yすべてのパッケージがインストールされるまで待ちます。

注意:
yum-config-managerコマンドに関するエラーが表示された場合。
sudo: yum-config-manager: command not found‘yum-utils’パッケージをインストールします。
yum -y install yum-utilsステップ2 - osqueryでSyslog消費を有効にする
osqueryは、Apple MacOSでApple System Log(ASL)を使用してシステムログを読み取る機能を提供し、Linuxではsyslogを使用します。
このステップでは、rsyslogを介してosqueryのsyslog消費を有効にします。
Ubuntuで
以下のaptコマンドを使用してrsyslogパッケージをインストールします。
sudo apt install rsyslog -yCentOSで
以下のyumコマンドを使用してrsyslogパッケージをインストールします。
sudo yum install rsyslog -yインストールが完了したら、’/etc/rsyslog.d’ディレクトリに移動し、新しい設定ファイルosquery.confを作成します。
cd /etc/rsyslog.d/
vim osquery.conf以下の設定をそこに貼り付けます。
template(
name="OsqueryCsvFormat"
type="string"
string="%timestamp:::date-rfc3339,csv%,%hostname:::csv%,%syslogseverity:::csv%,%syslogfacility-text:::csv%,%syslogtag:::csv%,%msg:::csv%\n"
)
*.* action(type="ompipe" Pipe="/var/osquery/syslog_pipe" template="OsqueryCsvFormat")保存して終了します。

ステップ3 - osqueryの基本設定
osqueryのデフォルト設定は’osquery.conf’で、通常は’/etc/osquery’ディレクトリにあります。osquery設定のサンプルは’/usr/share/osquery/osquery.conf’にあり、osqueryパック設定のサンプルもあります。
このステップでは、osquery設定コンポーネントについて学び、カスタムosquery設定を作成し、osquerydをサービスとしてデプロイします。
osquery設定はJSONファイルとしてフォーマットされ、以下のosquery設定仕様が含まれています。
- オプション: osqueryd CLIコマンドの一部で、アプリの起動と初期化を決定します。
- スケジュール: スケジュールされたクエリ名からクエリの詳細への流れを定義します。
- デコレーター: 結果とスナップショットログに追加の「装飾」を追加するために使用されます。
- パック: スケジュールクエリのグループです。
- その他: ファイルパス、YARA、Prometheus、ビュー、EC2、Chef設定。
‘/etc/osquery’ディレクトリに移動し、新しいカスタム設定’osquery.conf’を作成します。
cd /etc/osquery/
vim osquery.conf以下の設定をそこに貼り付けます。
{
"options": {
"config_plugin": "filesystem",
"logger_plugin": "filesystem",
"logger_path": "/var/log/osquery",
"disable_logging": "false",
"log_result_events": "true",
"schedule_splay_percent": "10",
"pidfile": "/var/osquery/osquery.pidfile",
"events_expiry": "3600",
"database_path": "/var/osquery/osquery.db",
"verbose": "false",
"worker_threads": "2",
"enable_monitor": "true",
"disable_events": "false",
"disable_audit": "false",
"audit_allow_config": "true",
"host_identifier": "hakase-labs",
"enable_syslog": "true",
"syslog_pipe_path": "/var/osquery/syslog_pipe",
"force": "true",
"audit_allow_sockets": "true",
"schedule_default_interval": "3600"
},
"schedule": {
"crontab": {
"query": "SELECT * FROM crontab;",
"interval": 300
},
"system_info": {
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
"interval": 3600
},
"ssh_login": {
"query": "SELECT username, time, host FROM last WHERE type=7",
"interval": 360
}
},
"decorators": {
"load": [
"SELECT uuid AS host_uuid FROM system_info;",
"SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
]
},
"packs": {
"osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf"
}
}保存して終了します。
注意:
- ‘filesystem’を設定およびロガープラグインとして使用しています。
- ロガーパスを’/var/log/osquery’ディレクトリに設定します。
- syslogパイプを’/var/syslog/syslog_pipe’ファイルに有効にします。
- スケジューラーでは、crontab、システム情報、sshログインを確認するための3つのクエリを定義します。
- ‘osquery-monitoring’という名前のosqueryパックを有効にし、パックファイルは’/usr/share/osquery/packs’ディレクトリにあります。
osquerydデーモンサービスを起動し、システム起動時に毎回起動するように有効にします。
systemctl start osqueryd
systemctl enable osqueryd次に、rsyslogサービスを再起動します。
systemctl restart rsyslog基本的なosqueryの設定が完了しました。

ステップ4 - osqueryを使用してファイル整合性監視(FIM)を設定する
osqueryは、inotifyおよびFSEventsを使用してLinuxおよびMacOS Darwinでファイル整合性監視を提供します。簡単に言えば、’file_path’を使用して定義されたディレクトリ内のファイルの変更を監視および検出し、すべてのアクティビティをfile_eventsテーブルに保存します。
このステップでは、ホーム、sshディレクトリ、tmp、wwwウェブルートディレクトリなどの重要なディレクトリを監視するようにosqueryを設定します。
‘/usr/share/osquery/packs’ディレクトリに移動し、新しいパック設定ファイル’fim.conf’を作成します。
cd /usr/share/osquery/packs
vim fim.conf以下の設定を貼り付けます。
{
"queries": {
"file_events": {
"query": "SELECT * FROM file_events;",
"removed": false,
"interval": 300
}
},
"file_paths": {
"homes": [
"/root/.ssh/%%",
"/home/%/.ssh/%%"
],
"etc": [
"/etc/%%"
],
"home": [
"/home/%%"
],
"tmp": [
"/tmp/%%"
],
"www": [
"/var/www/%%"
]
}
}保存して終了します。
次に’/etc/osquery’設定ディレクトリに戻り、osquery.confファイルを編集します。
cd /etc/osquery/
vim osquery.conf‘packs’セクション内にファイル整合性監視パック設定を追加します。
"packs": {
"osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
"fim": "/usr/share/osquery/packs/fim.conf"
}
保存して終了し、osquerydサービスを再起動します。
systemctl restart osqueryd
注意:
JSONリントツール’ http://jsonlint.com/ ‘を使用してJSON設定ファイルを確認し、エラーがないことを確認してください。
ステップ5 - テスト
定義されたディレクトリ’home’と’www’に新しいファイルを作成することでファイル整合性監視パックをテストします。
‘/var/www/‘ディレクトリに移動し、’howtoforge.md’という名前の新しいファイルを作成します。
cd /var/www/
touch howtoforge.md‘/home/youruser/‘ディレクトリに移動し、’hakase-labs.md’という名前の新しいファイルを作成します。
cd /home/vagrant/
touch hakase-labs.md次に、リアルタイムインタラクティブモードosqueryiを使用してすべてのログを監視し、osqueryの結果のログを確認します。

osqueryi
以下のosqueryiコマンドを実行します。
osqueryi --config-path /etc/osquery/osquery.conf次に、’file_events’テーブル内のファイル変更に関するすべてのログを確認します。
全体の変更について。
select * from file_events;‘home’ディレクトリについて。
select target_path, category, action, atime, ctime, mtime from file_events WHERE category="home";‘www’ウェブルートディレクトリについて。
select target_path, category, action, atime, ctime, mtime from file_events WHERE category="www";
osqueryd結果ログ
‘/var/log/osquery’ディレクトリに移動すると、’osqueryd.results.log’ファイルが得られます。
cd /var/log/osquery/
ls -lah osqueryd.results.log‘grep’コマンドを使用してosqueryログをフィルタリングします。
grep -rin howtoforge.md osqueryd.results.log
grep -rin hakase-labs.md osqueryd.results.logこれらのファイルが作成された情報が表示されます。

LinuxサーバーのUbuntuとCentOSでosqueryを使用してファイル整合性監視(FIM)のインストールと設定が成功裏に完了しました。
参考
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。