0
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DMARCレポートを解析するツール DmarcSrg をインストールする

Last updated at Posted at 2024-01-24

DmarcSrgについて

DmarcSrg github

LAMP環境で動作する比較的シンプルなDMARCレポートの解析ツールです。

  • 解析されたレポートの一覧を表示
  • 各レポートの DKIM/SPF の詳細を表示
  • サマリーレポートの表示
  • レポートの取得方法は、IMAP、ローカルフォルダ、S3に対応
  • メールボックスのある古いメールデータの削除
  • DBに登録されてた古いレポートデータの削除
  • メールボックスからDMARCレポートを収集する場合は、メールサーバーがIMAPをサポート

詳しくはDmarcSrgのgithubを確認してください。

動作条件

  • MariaDB or MySQL
  • PHP 7.3 or higher
  • php-mysql, php-xml, php-zip, php-json, php-imap, mbstring
  • Apache等のWebサーバー(必須ではない)
    CentOS9の場合は、php-imapのパッケージが標準で用意されていないの工夫する必要があります。

インストール

今回は、Ubuntu 22.04LTSにインストールしていきます。

パッケージのインストール

sudo apt update
sudo apt install apache2 mysql-server-8.0
sudo apt install php-fpm php-mysql php-xml php-zip php-json php-imap php-mbstring

ユーザーの作成

sudo useradd -d /opt/dmarc-srg -s /sbin/nologin -M dmarc-srg

データーベースの作成

sudo systemctl enable mysql --now
sudo mysql
mysql> CREATE database dmarc;
mysql> CREATE USER 'dmarc_user'@'localhost' IDENTIFIED BY 'password1';
mysql> GRANT ALL ON dmarc.* TO 'dmarc_user'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> QUIT

DmarcSrgのgit clone

cd /opt
git clone https://github.com/liuch/dmarc-srg.git

DmarcSrgの設定

cd /opt/dmarc-srg
cp config/conf.sample.php config/conf.php
vi config/conf.php
config/conf.php
$database = [
    'host'         => 'localhost',
    'type'         => 'mysql',
    'name'         => 'dmarc',
    'user'         => 'dmarc_user',
    'password'     => 'password1',
    'table_prefix' => ''
];

$mailboxes = [
    'name'            => 'Dmarc-Rua',
    'host'            => 'メールサーバーホスト名',
    'encryption'      => 'ssl',
    'novalidate-cert' => false,
    'username'        => 'メールアカウント',
    'password'        => 'パスワード',
    'mailbox'         => 'INBOX',
    'auth_exclude'    => []
];

$admin = [
    // Web UI のユーザー adminのパスワード
    'password' => 'パスワード',
    // 複数ユーザー対応する場合は ture に変更
    // 参照のみユーザーを作成できます
    'user_management' => false
];

$fetcher = [
    // ※未読のメッセージのみ処理を行います。
    'mailboxes' => [
        // 一度にフェッチされるメッセージの最大数
        'messages_maximum' => 10,

        // 正常に処理された後のメールの扱い
        // 'mark_seen'   - メッセージを既読状態にする(削除しない)
        // 'delete'      - メッセージを削除する
        // 'move_to:dir' - dir に指定したメールフォルダに移動
        //  'when_done' => [ 'mark_seen', 'move_to:done' ], で、
        // 既読にして done フォルダに移動もなど複数アクションも可能
        'when_done' => 'mark_seen',

        // 処理に失敗したメールの扱い
        'when_failed' => 'move_to:failed'
    ],

    // この正規表現に一致するドメインは、自動で処理対象となります。
    // ブランク や null が設定されている場合は、自動処理されません。
    // ※ 最初のレポートの最初のドメインは、自動的に追加されます。
    // 複数のドメインやサブドメインも処理対象とする場合に、自動で対象ドメインを登録したい場合に設定します。
    // いくつかの例:
    // '.+\\.example\\.net$' - ドメイン example.net のサブドメインと一致します。
    // '^mymail[0-9]+\\.net$' - mymail01.net、mymail02.net、mymail99999.net などのドメインに一致します。
    'allowed_domains' => ''
];


$cleaner = [
    // It is used in utils/mailbox_cleaner.php
    'mailboxes' => [
        // days_old (日) より古いメッセージを削除
        'days_old'       => 30,

        // 一度に削除されるメッセージの最大件数
        'delete_maximum' => 50,

        // メールボックスに最低何件のメッセージを残すか
        'leave_minimum'  => 100,

        //削除対象とするメールのステータス
     // none - なにもしない
        // seen - 閲覧されたメッセージのみが削除
        // any  - すべてのメッセージが削除
        'done'           => 'seen',
        'failed'         => 'none'
    ],

    // It is used in utils/reports_cleaner.php
    'reports' => [
        'days_old'       => 30,
        'delete_maximum' => 50,
        'leave_minimum'  => 100
    ],

    // It is used in utils/reportlog_cleaner.php
    'reportlog' => [
        'days_old'       => 30,
        'delete_maximum' => 50,
        'leave_minimum'  => 100
    ]

パーミッション等の設定

cd /opt
sudo chown -R damrc-srg.damrc-srg damrc-srg
sudo chmod 400 /opt/damrc-srg/config/conf.php

データーベースの初期化

cd /opt/dmarc-srg
php utils/database_admin.php init

設定ファイルと実行環境のチェック

cd /opt/dmarc-srg
sudo -u dmarc-srg php utils/check_config.php

実行結果がすべてOKになることを確認します。

=== GENERAL INFORMATION ===
  * OS information: Linux 5.15.0-91-generic #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023 x86_64
  * PHP version:    8.1.2-1ubuntu2.14

=== EXTENSIONS ===
  * pdo_mysql...................... Ok
  * xmlreader...................... Ok
  * zip............................ Ok
  * json........................... Ok

=== CONFIG FILE ===
  * Checking if the file exists.... Ok
  * Checking read permission....... Ok
  * Checking write permission...... Ok
  * Checking access by other users. Ok
  * Checking the output buffer..... Ok

=== DATABASE ===
  * Accessibility check............ Ok
  * Checking for integrity......... Ok

=== MAILBOXES ===
  * Checking mailboxes config...... Ok
    Message: 1 mailbox found
  * Imap extension................. Ok
  * Checking mailboxes (1)
    - Dmarc-Rua
      * Accessibility.............. Ok

=== DIRECTORIES ===
  * Checking directories config.... Ok
    Message: No directories found

=== REMOTE FILESYSTEMS ===
  * Getting configuration.......... Skipped
    Message: Configuration not found

===
Success!

Apacheの設定

vi /etc/apache2/sites-available/dmarc-srg.conf
dmarc-srg.conf
<VirtualHost *:80>
    ServerName hostname
    DocumentRoot /opt/dmarc-srg/public
    ErrorLog /var/log/apache2/dmarc-srg.error

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
    </FilesMatch>

    <Directory /opt/dmarc-srg/public >
        DirectoryIndex index.php
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <DirectoryMatch "^//opt/dmarc-srg/(classes|config|tests|utils)/*">
        Options None
        Require all denied
    </DirectoryMatch>
</VirtualHost>
sudo a2enconf php8.1-fpm
sudo a2ensite dmarc-srg
sudo a2enmod proxy_fcgi setenvif
sudo systemctl enable apache2 --now

レポート収集の手動実行

sudo php utils/fetch_reports.php

レポート収集とクリーンアップをcronに登録

sudo vi /etc/crontab
/etc/crontab
*/5 * * * * dmarc-srg  cd /opt/dmarc-srg && /usr/bin/php utils/fetch_reports.php
1 * * * * dmarc-srg  cd /opt/dmarc-srg && /usr/bin/php utils/mailbox_cleaner.php
0
5
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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?