はじめに
本記事では、LPIC303出題範囲のLinux AuditシステムをVirtualBox + Vagrantで構築した環境でハンズオン学習した記録を記載しています。
-
対象範囲
- auditd
- auditctl
- ausearch
- aureport
- auditd.conf
- audit.rules
- pam_tty_audit.so
- 参考 : LPIC-3 Exam 303 Content 332.2 ホストの侵入検知より抜粋
-
検証時利用環境
- ホストOS:Ubuntu 22.04.3
- VirtualBox:7.0.26r168464
- Vagrant:2.4.1
0. 環境の準備
0-1. VagrantFileの作成
作業用ディレクトリを作成する
mkdir linux_audit
cd linux_audit
VagrantFileを作成する ※学習環境として以下を定義
- OS:Ubuntu 22.04 LTS
- CPU:2コア
- メモリ:2GB
- auditdインストール済み
- ルール永続化のテンプレートとして/etc/audit/rules.d/99-handson.rulesを作成済み
- pam_tty_audit.soを有効化済み
クリックで開く
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# LPIC 303 — Linux Audit システム 学習環境
# カバートピック: auditd, auditctl, ausearch, aureport, auditd.conf, audit.rules, pam_tty_audit.so
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64" # Ubuntu 22.04 LTS
config.vm.hostname = "audit-lab"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.provider "virtualbox" do |vb|
vb.name = "lpic303-linux-audit"
vb.memory = 2048
vb.cpus = 2
end
# -------------------------------------------------------
# プロビジョニング
# -------------------------------------------------------
config.vm.provision "shell", inline: <<-SHELL
set -eux
export DEBIAN_FRONTEND=noninteractive
echo "=== apt update ==="
apt-get update -y
# -------------------------------------------------------
# Linux Audit システム
# -------------------------------------------------------
echo "=== auditd install ==="
apt-get install -y \
auditd \
audispd-plugins
# auditd の有効化と起動
systemctl enable auditd
systemctl start auditd
# -------------------------------------------------------
# サンプル audit ルールの配置
# -------------------------------------------------------
echo "=== audit rules setup ==="
# /etc/audit/rules.d/99-handson.rules を作成
cat > /etc/audit/rules.d/99-handson.rules << 'EOF'
# ハンズオン用サンプル audit ルール
# バッファサイズ
-b 8192
# ルール変更の監査
-w /etc/audit/ -p wa -k audit-config
# ユーザー/グループ管理ファイルの監査
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/gshadow -p wa -k identity
# sudoers の監査
-w /etc/sudoers -p wa -k sudoers
# /tmp 配下の実行ファイル実行を監査
-a always,exit -F dir=/tmp -F perm=x -k exec-tmp
# 権限昇格システムコール(setuid/setgid 実行)の監査
-a always,exit -F arch=b64 -S setuid -S setgid -k setuid
# ログインイベントの監査
-w /var/log/lastlog -p wa -k logins
-w /var/log/faillog -p wa -k logins
EOF
# ルールを反映
augenrules --load || auditctl -R /etc/audit/rules.d/99-handson.rules || true
# -------------------------------------------------------
# PAM TTY Audit 有効化
# -------------------------------------------------------
echo "=== pam_tty_audit setup ==="
# /etc/pam.d/common-session に pam_tty_audit.so を追加(既存なら skip)
if ! grep -q pam_tty_audit /etc/pam.d/common-session; then
echo "session required pam_tty_audit.so enable=* log_passwd" \
>> /etc/pam.d/common-session
fi
# -------------------------------------------------------
# 共通ユーティリティ
# -------------------------------------------------------
echo "=== utilities ==="
apt-get install -y \
curl wget git vim tree \
net-tools lsof less
echo "=== provisioning done ==="
echo "auditd version: $(auditd --version 2>&1 | head -1 || auditctl -v)"
auditctl -l || true
SHELL
end
0-2. VM の起動
vagrant up
初回は Ubuntu イメージのダウンロードとプロビジョニングが実行されます(5〜10分程度)。
0-3. VM へのログイン
vagrant ssh
0-4. root に昇格
sudo -i
1. Linux Audit システムの概要
- auditd はカーネルの Audit サブシステムと連携し、システムコール・ファイルアクセス・ユーザーイベントをログに記録するデーモン
- ログは
/var/log/audit/audit.logに蓄積される - ルールは
/etc/audit/audit.rules(または/etc/audit/rules.d/*.rules)で管理 auditctlでリアルタイムにルールを追加・確認・削除できるausearch/aureportでログを検索・集計するpam_tty_audit.soを使うと TTY 上のキー入力もログに記録できる
2. auditd の設定と起動
2-1. サービス状態確認
# サービス状態確認
systemctl status auditd
● auditd.service - Security Auditing Service
Loaded: loaded (/lib/systemd/system/auditd.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2026-05-01 16:18:47 UTC; 5min ago
Docs: man:auditd(8)
https://github.com/linux-audit/audit-documentation
Main PID: 2852 (auditd)
Tasks: 2 (limit: 2307)
Memory: 532.0K
CPU: 26ms
CGroup: /system.slice/auditd.service
└─2852 /sbin/auditd
# バージョン確認
auditctl -v
auditctl version 3.0.7
2-2. auditd.conf の主要設定項目
auditd自体の設定ファイルは /etc/audit/auditd.conf
cat /etc/audit/auditd.conf
例:ログ出力に関する設定
| 項目 | デフォルト値 | 説明 |
|---|---|---|
log_file |
/var/log/audit/audit.log |
ログファイルのパス |
log_format |
ENRICHED |
ログの形式 |
max_log_file |
8 |
ログファイルの最大サイズ (MB) |
max_log_file_action |
ROTATE |
サイズ超過時の動作 |
space_left_action |
SYSLOG |
ディスク残容量が少ない時の動作 |
admin_space_left_action |
SUSPEND |
管理領域が少ない時の動作 |
disk_full_action |
SUSPEND |
ディスクフル時の動作 |
num_logs |
5 |
ローテーション時に保持するログ数 |
3. auditctl によるルール管理
3-1. auditdの現在の動作状態(ステータス)確認
auditctl -s
enabled 1
failure 1
pid 2852
rate_limit 0
backlog_limit 8192
lost 0
backlog 0
backlog_wait_time 60000
backlog_wait_time_actual 0
loginuid_immutable 0 unlocked
ステータスの項目メモ:
enabled- 0 : 無効
- 1 : 有効
- 2 : ロック状態(変更不可)
failure- 0 : 無視
- 1 : ログ出力
- 2 : カーネルパニック
3-2. 現在のルール確認
# 現在ロードされているルールを表示
auditctl -l
-w /etc/audit -p wa -k audit-config
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/gshadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /tmp -p x -k exec-tmp
-a always,exit -F arch=b64 -S setuid,setgid -F key=setuid
-w /var/log/lastlog -p wa -k logins
-w /var/log/faillog -p wa -k logins
auditctl のルールには3種類あります:
| 種類 | オプション | 説明 |
|---|---|---|
| ファイルウォッチ | -w パス -p パーミッション -k キー |
特定ファイル・ディレクトリの監視 |
| システムコール | -a アクション,フィルタ -S syscall |
システムコールの監視 |
| コントロール | -b バッファ, -f フェイルモード |
バッファサイズ等の制御 |
パーミッションオプション (-p) の意味:
| 文字 | 意味 |
|---|---|
r |
読み取り |
w |
書き込み |
x |
実行 |
a |
属性変更 |
3-3. ルールの追加(一時的)
# /etc/passwd の書き込み・属性変更を監視
auditctl -w /etc/passwd -p wa -k passwd-change
# 再度ルールを確認
auditctl -l
-w /etc/audit -p wa -k audit-config
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/gshadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /tmp -p x -k exec-tmp
-a always,exit -F arch=b64 -S setuid,setgid -F key=setuid
-w /var/log/lastlog -p wa -k logins
-w /var/log/faillog -p wa -k logins
-w /etc/passwd -p wa -k passwd-change #追加された
# ルールを削除(-W で削除)
auditctl -W /etc/passwd -p wa -k passwd-change
# 再度ルールを確認
auditctl -l
-w /etc/audit -p wa -k audit-config
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/gshadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /tmp -p x -k exec-tmp
-a always,exit -F arch=b64 -S setuid,setgid -F key=setuid
-w /var/log/lastlog -p wa -k logins
-w /var/log/faillog -p wa -k logins
# 追加したルールが削除されている
3-4. 永続ルールの管理(audit.rules)
# 末尾に/etc/passwd のルールを追記
vi /etc/audit/rules.d/99-handson.rules
-w /etc/passwd -p wa -k passwd-change # こちらを追加
# ルールをカーネルに反映
augenrules --load
# 反映されたルールを確認
auditctl -l
-w /etc/audit -p wa -k audit-config
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/gshadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /tmp -p x -k exec-tmp
-a always,exit -F arch=b64 -S setuid,setgid -F key=setuid
-w /var/log/lastlog -p wa -k logins
-w /var/log/faillog -p wa -k logins
-w /etc/passwd -p wa -k passwd-change # 追加された
注:
augenrulesは/etc/audit/rules.d/*.rulesを結合して/etc/audit/audit.rulesを生成し、カーネルに反映する。
4. ausearch によるログ検索
4-1. 基本的な使い方
# キーワードで検索(-k でルールに設定したキーを指定)
ausearch -k identity
time->Fri May 1 17:05:17 2026
type=PROCTITLE msg=audit(1777655117.204:274): proctitle=746F756368002F6574632F736861646F77
type=PATH msg=audit(1777655117.204:274): item=1 name="/etc/shadow" inode=73670 dev=08:01 mode=0100640 ouid=0 ogid=42 rdev=00:00 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(1777655117.204:274): item=0 name="/etc/" inode=41 dev=08:01 mode=040755 ouid=0 ogid=0 rdev=00:00 nametype=PARENT cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
# -i で数値を人間が読める数値に変更
ausearch -k identity -i
type=PROCTITLE msg=audit(05/01/26 17:05:17.204:274) : proctitle=touch /etc/shadow
type=PATH msg=audit(05/01/26 17:05:17.204:274) : item=1 name=/etc/shadow inode=73670 dev=08:01 mode=file,640 ouid=root ogid=shadow rdev=00:00 nametype=NORMAL cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(05/01/26 17:05:17.204:274) : item=0 name=/etc/ inode=41 dev=08:01 mode=dir,755 ouid=root ogid=root rdev=00:00 nametype=PARENT cap_fp=none cap_fi=none cap_fe=0 cap_fver=0 cap_frootid=0
# 特定ユーザーのイベントを検索
ausearch -ua root
time->Fri May 1 16:55:59 2026
type=PROCTITLE msg=audit(1777654559.381:273): proctitle=2F7362696E2F617564697463746C002D52002F6574632F61756469742F61756469742E72756C6573
type=SOCKADDR msg=audit(1777654559.381:273): saddr=100000000000000000000000
type=SYSCALL msg=audit(1777654559.381:273): arch=c000003e syscall=44 success=yes exit=60 a0=3 a1=7ffe68083150 a2=3c a3=0 items=0 ppid=3666 pid=3680 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts1 ses=4 comm="auditctl" exe="/usr/sbin/auditctl" subj=unconfined key=(null)
type=CONFIG_CHANGE msg=audit(1777654559.381:273): op=set audit_backlog_wait_time=60000 old=60000 auid=1000 ses=4 subj=unconfined res=1
# 特定時間帯で検索
ausearch -ts today
time->Sat May 2 03:18:46 2026
type=USER_START msg=audit(1777691926.214:489): pid=4560 uid=1000 auid=1000 ses=17 subj=unconfined msg='op=PAM:session_open grantors=pam_limits,pam_env,pam_env,pam_permit,pam_umask,pam_unix,pam_systemd,pam_tty_audit acct="root" exe="/usr/bin/sudo" hostname=? addr=? terminal=/dev/pts/0 res=success'
# 特定ファイルへのアクセスを検索
ausearch -f /etc/passwd
time->Fri May 1 16:49:43 2026
type=PROCTITLE msg=audit(1777654183.997:228): proctitle=7472756E63617465002D73002D31002F6574632F706173737764
type=PATH msg=audit(1777654183.997:228): item=1 name="/etc/passwd" inode=73685 dev=08:01 mode=0100644 ouid=0 ogid=0 rdev=00:00 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
4-2. イベントを発生させて検索する
# テストイベント発生
touch /etc/shadow
# キーで検索
ausearch -k identity -ts recent
time->Fri May 1 17:05:17 2026
type=PROCTITLE msg=audit(1777655117.204:274): proctitle=746F756368002F6574632F736861646F77
type=PATH msg=audit(1777655117.204:274): item=1 name="/etc/shadow" inode=73670 dev=08:01 mode=0100640 ouid=0 ogid=42 rdev=00:00 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(1777655117.204:274): item=0 name="/etc/" inode=41 dev=08:01 mode=040755 ouid=0 ogid=0 rdev=00:00 nametype=PARENT cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
type=CWD msg=audit(1777655117.204:274): cwd="/root"
type=SYSCALL msg=audit(1777655117.204:274): arch=c000003e syscall=257 success=yes exit=3 a0=ffffff9c a1=7ffd5ee0a850 a2=941 a3=1b6 items=2 ppid=3480 pid=3742 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts1 ses=4 comm="touch" exe="/usr/bin/touch" subj=unconfined key="identity"
ausearch の主要オプション:
| オプション | 説明 |
|---|---|
-k キー |
ルールに設定したキーで検索 |
-f ファイル |
ファイル名で検索 |
-ua UID/ユーザー名 |
ユーザーで検索 |
-ts 開始時刻 |
開始時刻(today, yesterday, MM/DD/YYYY HH:MM:SS) |
-te 終了時刻 |
終了時刻 |
-i / --interpret |
数値を人間が読める形式に変換 |
5. aureport によるレポート生成
5-1. 基本レポート
# 全体サマリー
aureport
Summary Report
======================
Range of time in logs: 05/01/26 16:18:47.796 - 05/01/26 17:05:17.204
Selected time for report: 05/01/26 16:18:47 - 05/01/26 17:05:17.204
Number of changes in configuration: 43
Number of changes to accounts, groups, or roles: 0
Number of logins: 1
Number of failed logins: 0
Number of authentications: 0
Number of failed authentications: 0
Number of users: 2
Number of terminals: 7
Number of host names: 2
Number of executables: 12
Number of commands: 13
Number of files: 18
Number of AVCs: 0
Number of MAC events: 0
Number of failed syscalls: 4
Number of anomaly events: 0
Number of responses to anomaly events: 0
Number of crypto events: 0
Number of integrity events: 0
Number of virt events: 0
Number of keys: 7
Number of process IDs: 56
Number of events: 236
# 認証イベントのレポート
aureport -au
Authentication Report
============================================
# date time acct host term exe success event
============================================
1. 05/01/26 17:17:03 root ? /dev/pts/0 /usr/bin/su no 288
# ファイルアクセスのレポート
aureport -f
File Report
===============================================
# date time file syscall success exe auid event
===============================================
1. 05/01/26 16:18:50 /etc/ 44 yes /usr/sbin/auditctl 1000 127
2. 05/01/26 16:18:50 /etc/ 44 yes /usr/sbin/auditctl 1000 128
3. 05/01/26 16:18:50 /etc/ 44 yes /usr/sbin/auditctl 1000 129
4. 05/01/26 16:18:50 /etc/ 44 yes /usr/sbin/auditctl 1000 130
# プロセス実行のレポート
aureport -p
Process ID Report
======================================
# date time pid exe syscall auid event
======================================
1. 05/01/26 16:18:47 2852 ? 0 -1 4010
2. 05/01/26 16:18:47 2865 /usr/sbin/auditctl 44 -1 42
3. 05/01/26 16:18:47 2865 /usr/sbin/auditctl 44 -1 43
4. 05/01/26 16:18:47 2865 /usr/sbin/auditctl 44 -1 44
# ログインのレポート
aureport -l
Login Report
============================================
# date time auid host term exe success event
============================================
1. 05/01/26 16:22:21 1000 10.0.2.2 /dev/pts/0 /usr/sbin/sshd yes 200
# 異常イベントのレポート
aureport --failed
Failed Summary Report
======================
Range of time in logs: 05/01/26 16:18:47.796 - 05/01/26 17:05:17.204
Selected time for report: 05/01/26 16:18:47 - 05/01/26 17:05:17.204
Number of changes in configuration: 0
Number of changes to accounts, groups, or roles: 0
Number of logins: 0
Number of failed logins: 0
Number of authentications: 0
Number of failed authentications: 0
Number of users: 2
Number of terminals: 2
Number of host names: 0
Number of executables: 2
Number of commands: 2
Number of files: 1
Number of AVCs: 0
Number of MAC events: 0
Number of failed syscalls: 4
Number of anomaly events: 0
Number of responses to anomaly events: 0
Number of crypto events: 0
Number of integrity events: 0
Number of virt events: 0
Number of keys: 2
Number of process IDs: 3
Number of events: 4
# 今日のイベント
aureport -ts today
Summary Report
======================
Range of time in logs: 01/01/70 00:00:00.000 - 05/01/26 17:05:17.204
Selected time for report: 05/01/26 00:00:00 - 05/01/26 17:05:17.204
Number of changes in configuration: 43
Number of changes to accounts, groups, or roles: 0
Number of logins: 1
Number of failed logins: 0
Number of authentications: 0
Number of failed authentications: 0
Number of users: 2
Number of terminals: 7
Number of host names: 2
Number of executables: 12
Number of commands: 13
Number of files: 18
Number of AVCs: 0
Number of MAC events: 0
Number of failed syscalls: 4
Number of anomaly events: 0
Number of responses to anomaly events: 0
Number of crypto events: 0
Number of integrity events: 0
Number of virt events: 0
Number of keys: 7
Number of process IDs: 56
Number of events: 236
5-2. aureport と ausearch の組み合わせ
# loginコマンドを何も入力せずEnterキーを押して失敗させる
login
audit-lab login: # 何も入力せずEnter
Password: # 何も入力せずEnter
Login incorrect # Ctrl + cで中断
# 失敗したログインの詳細を確認
aureport -au --failed
Authentication Report
============================================
# date time acct host term exe success event
============================================
1. 05/01/26 17:24:06 ? /dev/pts/1 /usr/bin/login no 323
# -m USER_AUTHで認証イベントを指定し、-sv noで失敗をフィルタ
ausearch -m USER_AUTH -sv no -i
type=USER_AUTH msg=audit(05/01/26 17:24:06.485:323) : pid=3839 uid=root auid=vagrant ses=4 subj=unconfined msg='op=PAM:authentication grantors=? acct= exe=/usr/bin/login hostname=? addr=? terminal=/dev/pts/1 res=failed'
6. pam_tty_audit.so による TTY 監査
pam_tty_audit.so は、LinuxのPAM(Pluggable Authentication Modules)における監査用モジュールで、 ユーザーが端末(TTY)で入力したコマンドやキー操作を auditd に記録するための仕組み
6-1. pam_tty_audit の設定オプション
/etc/pam.d/common-session への追加例:
session required pam_tty_audit.so enable=* log_passwd
| オプション | 説明 |
|---|---|
enable=* |
すべてのユーザーで TTY 監査を有効化 |
enable=root |
root ユーザーのみ有効化 |
log_passwd |
パスワードプロンプト入力もログに記録(注意: パスワードがログに残る) |
disable=username |
特定ユーザーを除外 |
⚠️
log_passwdを使用するとパスワードが平文でログに記録される場合がある
6-2. TTY 監査ログの確認
以下はausearchコマンドで出力された、proctitle=login(ログインプロセス)でvagrantユーザ(auid=vagrant)の入力(syscall=read)を読み取っている例となる
# TTY audit イベントを検索
ausearch -m TTY -i
type=PROCTITLE msg=audit(05/01/26 17:24:05.769:322) : proctitle=login
type=SYSCALL msg=audit(05/01/26 17:24:05.769:322) : arch=x86_64 syscall=read success=yes exit=1 a0=0x0 a1=0x7fff2c03e020 a2=0x1ff a3=0xffffffffffffff80 items=0 ppid=3808 pid=3839 auid=vagrant uid=root gid=root euid=root suid=root fsuid=root egid=root sgid=root fsgid=root tty=pts1 ses=4 comm=login exe=/usr/bin/login subj=unconfined key=(null)
type=TTY msg=audit(05/01/26 17:24:05.769:322) : tty pid=3839 uid=root auid=vagrant ses=4 major=136 minor=1 comm=login data=<nl>
7. まとめ・比較表
| ツール/ファイル | 役割 | 主なオプション/設定 |
|---|---|---|
auditd |
Audit デーモン | 設定: /etc/audit/auditd.conf |
auditctl |
ルールの管理・確認 | -l(一覧), -w(ウォッチ), -a(syscall) |
ausearch |
ログ検索 | -k(キー), -f(ファイル), -ts(時間) |
aureport |
レポート生成 | -au(認証), -f(ファイル), --failed(失敗) |
audit.rules |
永続ルール定義 | /etc/audit/rules.d/*.rules |
auditd.conf |
デーモン設定 | ログローテーション、ディスク管理 |
pam_tty_audit.so |
TTY キー入力の監査 | enable=*、log_passwd |
後片付け
# VM のシャットダウン
vagrant halt
# VM の完全削除(ディスクイメージも削除)
vagrant destroy -f
付録: よく使うコマンドまとめ
# ===== auditd 管理 =====
systemctl status auditd # サービス状態確認
systemctl restart auditd # デーモン再起動
augenrules --load # rules.d のルールをカーネルに反映
# ===== auditctl =====
auditctl -l # 現在のルール一覧
auditctl -s # Audit システムのステータス
auditctl -w /path -p wa -k key # ファイルウォッチルール追加
auditctl -W /path -p wa -k key # ファイルウォッチルール削除
auditctl -a always,exit -F arch=b64 -S open -k file-open # syscall ルール追加
auditctl -D # 全ルール削除
# ===== ausearch =====
ausearch -k キー # キーで検索
ausearch -f /path/to/file # ファイル名で検索
ausearch -ua ユーザー名 # ユーザーで検索
ausearch -ts today # 今日のイベント
ausearch -ts yesterday # 昨日のイベント
ausearch -ts "2024/01/01 00:00:00" -te "2024/01/01 23:59:59" # 期間指定
ausearch -i # 数値を人間が読める形式に変換
ausearch --raw # raw 形式で出力
# ===== aureport =====
aureport # 全体サマリー
aureport -au # 認証イベント
aureport -f # ファイルアクセス
aureport -p # プロセス実行
aureport -l # ログインイベント
aureport --failed # 失敗イベント
aureport -ts today # 今日のレポート
aureport -s # 要約