DMARCの設定をして、なりすましなどのレポートをメールで送る設定をしたけど、xmlファイルはzip圧縮でメール添付だし、xml ファイルも内容が読みづらい。
dmarc-catを使ってみる
“aptitude search dmarc”を実行すると、dmarc-cat というソフトが見つかり、”sudo aptitude install dmarc-cat” インストールして使ってみる。
dmarc-cat 0.9.2/j4 by Ollivier Robert Reporting by: google.com — noreply-dmarc-support@google.com From 2020-02-12 09:00:00 +0900 JST to 2020-02-13 08:59:59 +0900 JST Domain: tsaitoh.net Policy: p=quarantine; dkim=r; spf=r Reports(1): IP Count From RFrom RDKIM RSPF xxxxx-xx-x-x.xxx.ne.jp. 1 xxxxxxx.xxx xxxxxxx.xxx pass pass
これなら、なんとなく読みやすい。でも、どちらにしろ、添付ファイルのzip抽出、zip解凍、dmarc-cat の実行、んで、これらの内容を最終的にメールで読みたい。
メールを dmarc-cat で変換してレポート
ということで、その一連の処理を、procmail から実行させるための perl script をチロっと書いてみた。
#!/usr/bin/perl
# DMARC のレポートメールを dmarc-cat で文字情報にして送信しなおす
#
# .procmailrc に以下を記載
# | dmarc-report-mail.pl メールアドレス
my $mailto = $ARGV[0] ; # 送りなおすメールアドレス
# 添付ファイルを抽出するディレクトリ
my $tmpdir = "/tmp/dmarc-report-$USER-$$" ;
if ( mkdir( $tmpdir ) ) {
# 標準入力のメールから添付ファイルを抽出
system( "/usr/bin/munpack -q -C $tmpdir > /dev/null 2>&1" ) ;
if ( opendir( my $dh , $tmpdir ) ) {
# 保存された全ての添付ファイルの処理
while( my $file = readdir( $dh ) ) {
if ( -f "$tmpdir/$file" ) {
# .zip ファイルだけ処理を行う
if ( $file =~ /\.zip$/ ) {
my $zipfile = "$tmpdir/$file" ;
my $xmlfile = $zipfile ;
$xmlfile =~ s/\.zip$/.xml/ ;
$xmlfile =~ s/X/!/g ;
# 圧縮解除 .zip => .xml
system( "/usr/bin/unzip -q -d $tmpdir '$tmpdir/$file'" ) ;
if ( -f $xmlfile ) {
# .xml => dmarc-cat の出力をメールに送る
if ( $mailto ne ""
&& open( $mh , "| /usr/sbin/sendmail $mailto" ) ) {
print $mh "Subject: DMARC report\n\n" ;
if ( open( my $fh , "/usr/bin/dmarc-cat '$xmlfile' |" ) ) {
while( <$fh> ) {
print $mh $_ ;
}
close( $fh ) ;
}
close( $mh ) ;
}
unlink( $xmlfile ) ; # 解凍されたファイルを消す
}
}
unlink( "$tmpdir/$file" ) ; # 添付ファイルを消す
}
}
closedir( $dh ) ;
}
rmdir( $tmpdir ) ;
}
この script を以下の様な procmailrc にかけて、report-a 宛のレポートを変換後 report-a-dmarc 宛に再転送させる。
:0 * ^To.*report-a@xxxxxxx\.xxx | /...path.../dmarc-report-mail.pl report-a-dmarc@xxxxxxx.xxx :0 * ^To.*report-f@xxxxxxx\.xxx | /...path.../dmarc-report-mail.pl report-f-dmarc@xxxxxxx.xxx