Total Pageviews

Saturday, 11 November 2017

安裝與設定pflogsumm for Postfix

做為一位系統管理者,一定要時常注意系統的健康狀態。
前一陣子公司有位同仁的 email 密碼設的太簡單了,所以,被猜到並且被盜用來濫發垃圾信件。Mail 的 log 量又非常的大,所以也不可能每天去檢視所有的紀錄。一直到幾天後,收到有寄出的 email 被退件,而原因就是我們的 mail server 被列在某個黑名單上。我再進一步去查,才發現原來有帳號已經被盜用了好幾天。由於使用的流量/頻寬都沒有太大,所以,從流量上來看,並不能很容易的察覺有異常。
其他系統方面的善後與設定我就不多著墨了。但是,我就開始思考與尋找適合的工具來幫我處理這部份的問題。在努力的尋找下,我找到了 pflogsumm!

由於該套件有包含在 Debian 內,所以安裝非常簡單:
apt-get install pglogsumm
安裝好後,先寫一個 bash script,我定的路徑及檔名(/usr/local/sbin/postfix_report.sh):
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

pflogsumm /var/log/你的maillog檔 | formail -c -I"Subject: Mail Statistics" -I"From: pflogsumm@example.com" -I"To: 收件人@example.com" -I"Received: from mx.example.com ([192.168.xxx.xxx])" | sendmail postmaster@example.com

exit 0
以上的 script,請將所有的 domain / email / IP,更改為適當資訊。存檔後再 chmod 即可。
Script 寫好後,現在剩下的就是如何執行,什麼時候執行?
由於資料的來源是 mail log,所以我們必須要檢視一下 mail log 的設定,不同 distribution 的 mail log 的設定方式可能會有些許不同。Debian 使用的是 rsyslog。所以,postfix 的 mail log 是在 /etc/logrotate.d/rsyslog 進行設定:
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
{
rotate 14
daily
missingok
notifempty
compress
delaycompress
sharedscripts
prerotate
/usr/local/sbin/postfix_report.sh
endscript
postrotate
invoke-rc.d rsyslog reload > /dev/null
endscript
}

上面的範例,可以自己的需求來進行調整。而且,直接在 rotate 之前呼叫我的 shell script,會比使用 crontab 來呼叫更為簡單而且乾淨。
以上,所有的設定就完成了。
之後,收到信件的內容大致如下:
Grand Totals
------------
messages1923 received
3131 delivered
4 forwarded
8 deferred (68 deferrals)
3 bounced
6673 rejected (68%)
0 reject warnings
0 held
0 discarded (0%)
253674k bytes received
372125k bytes delivered
551 senders
297 sending hosts/domains
163 recipients
67 recipient hosts/domains

Per-Day Traffic Summary
date received delivered deferred bounced rejected
--------------------------------------------------------------------
Sep 8 2010 1555 2597 47 3 4597
Per-Hour Traffic Daily Average
time received delivered deferred bounced rejected
--------------------------------------------------------------------
0000-0100 29 52 2 0 137
Host/Domain Summary: Message Delivery
sent cnt bytes defers avg dly max dly host/domain
-------- ------- ------- ------- ------- -----------
2589 315653k 42 2.4 s 62.3 h example.com
Host/Domain Summary: Messages Received
msg cnt bytes host/domain
-------- ------- -----------
586 116367k example.com
Senders by message count
------------------------
156 webmaster@example.com
Recipients by message count
---------------------------
374 example@example.com
Senders by message size
-----------------------
26552k example@example.com
Recipients by message size
--------------------------
39781k example@example.com
message deferral detail
-----------------------
smtp (total: 68)
message bounce detail (by relay)
--------------------------------
gmail-smtp-in.l.google.com[74.125.127.27]:25 (total: 1)
message reject detail
---------------------
cleanup
header (total: 29)
29 Date: Tue, 19 Jan 2038 11:14:07 +0800
message reject warning detail: none
message hold detail: none
message discard detail: none
smtp delivery failures: none
它所整理出來的資訊非常容易閱讀,而且,有異常就很容易發現。系統管理者就可以進一步的去做追查及因應。


參考資料來源:
http://www.howtoforge.com/postfix-monitoring-with-mailgraph-and-pflogsumm-on-debian-lenny
----------------------------

 Anacron 例行檢查的狀況


最近新的系統剛上線,總是會有一些小狀況要來慢慢的調整。
在 anacron 每日的報告中看到了:
send-mail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
send-mail: warning: inet_protocols: configuring for IPv4 support only
postdrop: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
postdrop: warning: inet_protocols: configuring for IPv4 support only

後來想想,對喔,我把 IPv6 給關掉了。
上網搜尋一下資料後發現,應該只要修改一下 Postfix 的設定即可。
在 /etc/postfix/main.cf 裡(如果沒有就加入,如果有就修改成以下的設定):
inet_protocols = ipv4
重新啟動Postfix 即可。

明天看報告再來確認這樣是否可以解決掉這錯誤訊息.
--------------

安裝 Postgrey for Postfix


垃圾郵件實在太多了,除了黑名單,決定再加上 Postgrey。

在 Debian 上, Postgrey 的安裝與設定都很簡單。

1. 安裝 Postgrey
apt-get install postgrey


2. 修改 /etc/postfix/main.cf 在 smtpd_recipient_restrictions 的裡面加入:
check_policy_service inet:127.0.0.1:60000


Postgrey 預設的等待時間是五分鐘,我是覺得有一點久,所以改成一分鐘。
修改 /etc/default/postgrey:
POSTGREY_OPTS="--inet=127.0.0.1:60000 --delay=60"


最後重新啟動 postfix 及 postgrey 就好了!
# /etc/init.d/postfix restart
# /etc/init.d/postgrey restart


參考資料:
http://www.howtoforge.com/greylisting_postfix_postgrey

No comments:

Post a Comment