跳至正文

[教程]邮件服务器的身份验证制作与处理

请先确认您搭建邮件服务的服务器开放25端口,大部分VPS服务商不开放此端口

可以作为身份验证的方式一览

  • PTR:反向解析(Pointer Record)
  • SPF:发件人策略框架解析(Sender Policy Framework) 【注:根据RFC 7208,SPF记录将会移除(截止至发布文章时,cloudflare还保留着SPF解析),请将SPF的数据以TXT记录形式写入SPF数据】
  • DKIM:邮件签名
  • DMARC:此TXT记录要与SPF一起使用
  • FQDN:全限定域名(Fully Qualified Domain Name),需要与MX解析关联

FQDN-MX解析要求

建议MX解析指向一个A解析,这个A解析(邮件服务器FQDN)再指向该IP,服务器的主机名(Hostname)改为FQDN所对应的主机名。

例如:MX解析example.com,MX解析出mail.example.com这个A解析(FQDN),A解析解析到邮件服务器IP,服务器的Hostname要改为mail,具体用途接下来会提及。


PTR解析制作

PTR解析与提供服务器的服务商有关,请向您的服务商询问是否允许做rDNS反向解析,可能有一些服务商允许25口但不给rDNS

1.进入服务商提供的管理后台,找到自己的IP管理页面

2.修改Reverse DNS(rDNS),将FQDN写入,保存即可(IPv4/IPv6都可以,看你心情)

3.进入你的域名解析界面,将您的PTR解析也写到您的域名解析中

例如:mail.example.com所对应的IP为11.4.5.142401:aa1:92:abb:b1::c1e,您只需要把IP反过来并将 : 改为 . ,建议通过第四步查询解析的方法直接获取in-addr.arpa数据,例子在域名解析中的PTR应该是解析名为mail,解析值为14.5.4.11.in-addr.arpa

4.查看解析是否生效

Windows查询方法:Win+R打开cmd并输入以下指令

nslookup
>set q=ptr
>【你的IP】

如果解析成功,你会找到你填的FQDN

Linux查询方法:进入Terminal,输入dig -x 【你的IP】
如果解析成功,你会找到你填的FQDN


SPF解析制作

SPF语法– 类别:

all | ip4 | ip6 | a | mx | ptr | exists | include

SPF语法– 对应前缀:

+ 通过
 | - 拒绝
 | ~ 软拒绝
 | ? 中立

解析处理:

进入域名解析页面,添加TXT解析

名称为@(就是你的域名),内容填写可以如下
“v=spf1 ip4:你的IPv4地址 ip6:你的IPv6地址 ~all” 只允许你的IPv4和IPv6地址发信,不是这些IP发来的邮件按照垃圾邮件处理(被标记)
“v=spf1 -all” 这个域名不会发送任何邮件,来自这个域名的邮件一律拒绝
“v=spf1 ip4:11.4.5.0/24 ~all” 只允许11.4.5.0/24这个IP段内所有的IP发信,不在这个IP段里的IP发来的邮件按照垃圾邮件处理(被标记)【IPv6同理】
“v=spf1 a mx ~all” 只允许MX解析出来的IP发信,不是这些IP发来的邮件按照垃圾邮件处理(被标记)
“v=spf1 mx mx:anotherserver.example.com ~all” 只允许该MX解析的IP地址与anotherserver.example.com的IP地址,不是这些IP发来的邮件按照垃圾邮件处理(被标记)
“v=spf1 include:anotherserver.example.com ~all” 使用与anotherserver.example.com一样的SPF解析,如果anotherserver.example.com不存在SPF解析,则报错

SPF解析查询

Windows查询方法:Win+R打开cmd并输入以下指令

nslookup
>set q=txt
>【你的发信域名(@的后缀)】

如果解析成功,你会找到你填的SPF规则

Linux查询方法:进入Terminal,输入dig txt 【你的发信域名(@的后缀)】
如果解析成功,你会找到你填的SPF规则


DKIM签名制作

注意:本文使用的环境为Ubuntu 18.04 LTS + Postfix

1.安装opendkim

sudo apt install opendkim

2.配置opendkim并使其生效

1.配置文件夹

mkdir /etc/opendkim/keys/【你的域名】 -p
mkdir /var/run/opendkim/ -p

2.修改opendkim数据

vi /etc/opendkim.conf

修改数据为

# This is a basic configuration that can easily be adapted to suit a standard
# installation. For more advanced options, see opendkim.conf(5) and/or
# /usr/share/doc/opendkim/examples/opendkim.conf.sample.

# Log to syslog
Syslog			yes
# Required to use local socket with MTAs that access the socket as a non-
# privileged user (e.g. Postfix)
UMask			007

# Sign for example.com with key in /etc/dkimkeys/dkim.key using
# selector '2007' (e.g. 2007._domainkey.example.com)
#Domain			example.com
#KeyFile		/etc/dkimkeys/dkim.key
#Selector		2007

# Commonly-used options; the commented-out versions show the defaults.
Canonicalization	relaxed/simple
Mode			sv
SubDomains		no
AutoRestart         yes
AutoRestartRate     10/1M
Background          yes
DNSTimeout          5
SignatureAlgorithm  rsa-sha256

# Socket smtp://localhost
#
# ##  Socket socketspec
# ##
# ##  Names the socket where this filter should listen for milter connections
# ##  from the MTA.  Required.  Should be in one of these forms:
# ##
# ##  inet:port@address           to listen on a specific interface
# ##  inet:port                   to listen on all interfaces
# ##  local:/path/to/socket       to listen on a UNIX domain socket
#
#Socket                  inet:8892@localhost
Socket			local:/var/spool/postfix/opendkim/opendkim.sock

##  PidFile filename
###      default (none)
###
###  Name of the file where the filter should write its pid before beginning
###  normal operations.
#
PidFile               /var/run/opendkim/opendkim.pid


# Always oversign From (sign using actual From and a null From to prevent
# malicious signatures header fields (From and/or others) between the signer
# and the verifier.  From is oversigned by default in the Debian pacakge
# because it is often the identity key used by reputation systems and thus
# somewhat security sensitive.
OversignHeaders		From

##  ResolverConfiguration filename
##      default (none)
##
##  Specifies a configuration file to be passed to the Unbound library that
##  performs DNS queries applying the DNSSEC protocol.  See the Unbound
##  documentation at http://unbound.net for the expected content of this file.
##  The results of using this and the TrustAnchorFile setting at the same
##  time are undefined.
##  In Debian, /etc/unbound/unbound.conf is shipped as part of the Suggested
##  unbound package

# ResolverConfiguration     /etc/unbound/unbound.conf

##  TrustAnchorFile filename
##      default (none)
##
## Specifies a file from which trust anchor data should be read when doing
## DNS queries and applying the DNSSEC protocol.  See the Unbound documentation
## at http://unbound.net for the expected format of this file.

TrustAnchorFile       /usr/share/dns/root.key

##  Userid userid
###      default (none)
###
###  Change to user "userid" before starting normal operation?  May include
###  a group ID as well, separated from the userid by a colon.
#
UserID                opendkim
# Map domains in From addresses to keys used to sign messages
KeyTable           refile:/etc/opendkim/key.table
SigningTable       refile:/etc/opendkim/signing.table

# Hosts to ignore when verifying signatures
ExternalIgnoreList  /etc/opendkim/trusted.hosts

# A set of internal hosts whose mail should be signed
InternalHosts       /etc/opendkim/trusted.hosts

3.修改添加可信服务器【根据自己的情况填写!】

vi /etc/opendkim/trusted.hosts

确保内部有以下数据

127.0.0.1
::1
localhost
【你的域名】

4.修改Key.table文件

vi /etc/opendkim/key.table

写入数据

default._domainkey.【你的域名】  【你的域名】:default:/etc/opendkim/keys/【发信域名(@的后缀)】/default.private

5.修改Signing.table文件

vi /etc/opendkim/signing.table

写入数据

*@【你的域名】 default._domainkey.【你的域名】

6.修改Postfix

vi /etc/postfix/main.cf

追加数据

#
# Added to open DKIM and DMARC
#
# Milter configuration
policyd-spf_time_limit = 3600
#check_policy_service unix:private/policyd-spf
milter_default_action = accept
milter_protocol = 6
smtpd_milters = local:opendkim/opendkim.sock
non_smtpd_milters = $smtpd_milters

7.生成密钥并获取到公钥

mkdir /etc/opendkim/keys/【发信域名(@的后缀)】
opendkim-genkey -D /etc/opendkim/keys/【发信域名(@的后缀)】/ -d  【你的域名】 -s default
useradd opendkim
chown opendkim.opendkim /etc/opendkim -R
chown opendkim.opendkim /etc/opendkim.conf
/etc/init.d/opendkim -Dx /etc/opendkim.conf
systemctl restart postfix
chown opendkim:opendkim /etc/opendkim/keys/【发信域名(@的后缀)】/default.private
cat /etc/opendkim/keys/【发信域名(@的后缀)】/default.txt   #里面就是你DKIM解析

8.进入域名解析页面添加解析

解析类型:TXT解析
名字:default._domainkey
内容:v=DKIM1; h=sha256; k=rsa;p=【你拿到的公钥】


DMARC解析制作

进入域名解析界面,添加解析解析类型:TXT解析
名字:_dmarc
内容:v=DMARC1; p=none; fo=1; ruf=mailto:dmarc@【你的域名】; rua=mailto:dmarc_report@【你的域名】


邮件测试

往mail-tester.com中发送一封邮件测试即可