邮件系统的特点:快捷,安全,多样性
实现方式:
Windows: 电子邮件服务 03 08 exchange
Linux: sendmail 【无身份验证 无加密 无防范垃圾邮件,病毒】
POSTFIX 【模块化,快】
Qmail
MAA:邮件访问代理 tcp 110 (pop3) tcp 109 (pop2) tcp 143 (imp4)
MUA: 邮件用户代理
MDA: 邮件的传递/分检代理
实现系统:redhat5系列
打开一台redhat机器
[[email protected] ~]# rpm -qa |grep sendmail
sendmail-8.13.8-8.el5 查看软件是否安装,如果没有安装则需要安装
[[email protected] ~]# rpm -qa |grep dovecot
[[email protected] ~]# rpm -qa |grep bind 经查询dovecot bind 没有安装
[[email protected] ~]# mkdir /media/cdrom
[[email protected] ~]# mount /dev/cdrom /media/cdrom/ 挂载光盘
[[email protected] ~]# cd /media/cdrom/Server/ 进入软件目录
[[email protected] Server]# rpm -ivh bind-9.3.6-4.P1.el5_4.2.i386.rpm bind-chroot-9.3.6-4.P1.el5_4.2.i386.rpm caching-nameserver-9.3.6-4.P1.el5_4.2.i386.rpm 安装这3个软件包
[[email protected] Server]# cd /var/named/chroot/etc/ 进入目录
[[email protected] etc]# cp -p named.caching-nameserver.conf named.conf 制作配置文件
[[email protected] etc]# vim named.conf 编辑配置文件
修改为any
将视图也修改为any
[[email protected] etc]# vim named.rfc1912.zones
增加163.com区域
[[email protected] etc]# cd ../var/named/ 进入区域文件的位置产生区域文件
[[email protected] named]# ll
total 72
drwxrwx--- 2 named named 4096 Aug 26 2004 data
-rw-r----- 1 root named 198 Jan 18 2010 localdomain.zone
-rw-r----- 1 root named 195 Jan 18 2010 localhost.zone
-rw-r----- 1 root named 427 Jan 18 2010 named.broadcast
-rw-r----- 1 root named 1892 Jan 18 2010 named.ca
-rw-r----- 1 root named 424 Jan 18 2010 named.ip6.local
-rw-r----- 1 root named 426 Jan 18 2010 named.local
-rw-r----- 1 root named 427 Jan 18 2010 named.zero
drwxrwx--- 2 named named 4096 Jul 27 2004 slaves
[[email protected] named]# cp -p localhost.zone 163.com.zone 拷贝区域文件修改
修改为此dns配置
[[email protected] named]# service named start 开启dns服务器
[[email protected] named]# chkconfig named on
[[email protected] named]# netstat -tupln |grep named 查看端口
[[email protected] named]# vim /etc/resolv.conf
本机既做dns服务器也做dns客户端
[[email protected] named]# nslookup mail.163.com
Server: 192.168.2.100
Address: 192.168.2.100#53
Name: mail.163.com
Address: 192.168.2.100 进行解析测试
[[email protected] named]# dig -t mx 163.com
[[email protected] named]# hostname 查看主机名
[[email protected] named]# vim /etc/sysconfig/network 修改主机名
修改为dns服务器能查找到的名字
[[email protected] named]# hostname mail.163.com 不重启直接能查到修改的用户名
[[email protected] ~]# cd /etc/mail 进入目录配置sendmail
[[email protected] mail]# netstat -tupln |grep sendmail
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3303/sendmail: acce 可以看到sendmail已经开启了
因为sendmail.cf不易修改,所以修改sendmail.mc。 需要m4工具将mc文件修改为cf文件及sendmail-cf软件
[[email protected] mail]# rpm -qa|grep m4 查看是否安装m4文件
[[email protected] mail]# rpm -ivh /media/cdrom/Server/sendmail-cf-8.13.8-8.el5.i386.rpm 安装sendmail-cf文件
[[email protected] mail]# vim sendmail.mc 修改sendmail.mc文件
修改为此
[[email protected] mail]# service sendmail restart 重启sendmail
[[email protected] mail]# netstat -tupln|grep sendmail 查看监听端口是否改变
[[email protected] mail]# vim local-host-names 进行分检的时候比对信件,需要写主机的别名和主机名
[[email protected] mail]# service sendmail restart 重启
进行测试:
[[email protected] mail]# useradd user1
[[email protected] mail]# useradd user2 建立user1和user2账号
[[email protected] mail]# mail -s ok [email protected]
1111111111111
.
Cc:
发送邮件
[[email protected] mail]# tail -f /var/log/maillog
日志显示已发送
查看到收到信件
用同样的方法再搭建一个mail.sina.com邮件系统
当邮件系统搭建好之后,使用的协议为http ftp smtp pop3 imap4为了保证邮件发送接收的安全,需要使用https ftps smtps pop3s imap4s协议,使用密文传输。
先试验下使用明文传输时被抓包工具截获时候的状态:
先安装抓包工具
[[email protected] ~]# mount /dev/cdrom /media/cdrom/
mount: block device /dev/cdrom is write-protected, mounting read-only
[[email protected] ~]# cd /media/cdrom/Server/
安装此安装包
[[email protected] Server]# rpm -ivh wireshark-1.0.8-1.el5_3.1.i386.rpm
warning: wireshark-1.0.8-1.el5_3.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
error: Failed dependencies:
libsmi.so.2 is needed by wireshark-1.0.8-1.el5_3.1.i386 出现如此情况需要安装
[[email protected] Server]# rpm -ivh libsmi-0.4.5-2.el5.i386.rpm 安装此软件包
[[email protected] Server]# rpm -ivh wireshark-1.0.8-1.el5_3.1.i386.rpm 再安装
[[email protected] Server]# service sendmail start
[[email protected] Server]# service named start 重启邮件服务器和域名解析服务器
找一台邮件客户机,为了试验方便放在了同一网段,dns服务器也做在了邮件服务器上
域名解析测试
正常
然后用邮件客户端建个邮件用户
然后工具→账户→邮件→选择自己刚才建的账号将名字修改为user1(好记)
[[email protected] Server]# tshark -ni eth0 -R "tcp.port eq 25" 开启抓包工具进行抓包(监听eth0网卡25端口的流量)
然后用邮件客户端给自己发封信件
点击发送
然后查看抓包工具截获的内容
重要信息都可以看到
[[email protected] Server]# tshark -ni eth0 -R "tcp.port eq 110" 抓取接受邮件端口的流量
然后进行接收
然后查看抓取到的内容
用户名和密码别抓取到了
搭建ca服务器(在192.168.2.100上和邮件服务器在同一机器上)
先查看是否支持ca
[[email protected] Server]# sendmail -d0.1 -bv 查看编译的时候软件配置
查看到可以支持 STARTTLS
查看功能是否启动,没有 STARTTLS,所以没有启动
[[email protected] Server]# cd /etc/pki/tls/
[[email protected] tls]# vim openssl.cnf 编辑ca配置文件
修改为指定目录,其他的目录需要创建
指定能给其他人使用
修改默认值
[[email protected] ~]# cd /etc/pki/CA/
[[email protected] CA]# mkdir certs crl 创建需要的目录
[[email protected] CA]# touch index.txt 创建需要的文件
[[email protected] CA]# mkdir newcerts
[[email protected] CA]# touch serial
[[email protected] CA]# echo "01" >serial 给16进制的01赋给serial
[[email protected] CA]# openssl genrsa 1024 >private/cakey.pem 创建私钥
[[email protected] CA]# chmod 600 private/cakey.pem 修改权限已保证私钥的安全性
[[email protected] CA]# openssl req -new -key private/cakey.pem -x509 -out cacert.pem 使用ca的私钥创建证书
[[email protected] CA]# mkdir pv /etc/mail/certs 创建一个目录放证书,私钥,请求。
[[email protected] CA]# cd /etc/mail/certs/
[[email protected] certs]# openssl genrsa 1024 >sendmail.key 创建私钥
[[email protected] certs]# chmod 600 sendmail.key
[[email protected] certs]# openssl req -new -key sendmail.key -out sendmail.req 创建请求
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [BEIJING]:HENAN
Locality Name (eg, city) [BEIJING]:ZHENGZHOU
Organization Name (eg, company) [My Company Ltd]:163
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server‘s hostname) []:smtp.163.com
Email Address []:
Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[[email protected] certs]# openssl ca -in sendmail.req -out sendmail.cert 利用请求得到证书
[[email protected] certs]# ll
total 24
-rw-r--r-- 1 root root 3059 Sep 15 00:08 sendmail.cert
-rw------- 1 root root 887 Sep 14 23:52 sendmail.key
-rw-r--r-- 1 root root 647 Sep 14 23:55 sendmail.req 所需要的文件已经有了
[[email protected] certs]# vim /etc/mail/sendmail.mc 修改配置文件
[[email protected] certs]# service sendmail restart 重启sendmail服务器
再次查看多了STARTTLS
[[email protected] certs]# netstat -tupln |grep sendmail
[[email protected] certs]# tshark -ni eth0 -R "tcp.port eq 25" 再进行抓包测试
邮件服务器端:
工具→账户→user1
选择使用ssl
这时候看到的都是密文
实现了smtps
然后实现接受服务器的加密
[[email protected] ~]# mkdir -pv /etc/dovecot/certs 创建接受服务器的证书目录
[[email protected] certs]# openssl genrsa 1024 >dovecot.key 创建私钥
[[email protected] certs]# chmod 600 dovecot.key
[[email protected] certs]# openssl req -new -key dovecot.key -out dovecot.req 利用私钥创建请求
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [BEIJING]:HENAN
Locality Name (eg, city) [BEIJING]:ZHENGZHOU
Organization Name (eg, company) [My Company Ltd]:163.com
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server‘s hostname) []:pop3.163.com
Email Address []:
Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[[email protected] certs]# openssl ca -in dovecot.req -out dovecot.cert 利用请求生成证书
[[email protected] certs]# vim /etc/dovecot.conf 编辑配置文件
修改目录
只使用密文端口
[[email protected] certs]# tshark -ni eth0 -R "tcp.port eq 995" 进行抓包
设置接收信件为密文端口进行接收
接收显示需要使用证书
此时抓包显示的是
被加密的内容
可以测试到没有用户的认证,随便一个用户都能发送邮件
默认情况下邮件的发送不做身份验证
要想进行身份需要使用sasl协议
[[email protected] ~]# mount /dev/cdrom /media/cdrom/
[[email protected] ~]# cd /media/cdrom/Server/ 挂载光盘,进入server目录
查找到此软件包
[[email protected] Server]# rpm -qa|grep cyrus 查看系统是否安装此如软件包
显示已经安装过了
[[email protected] Server]# chkconfig --list |grep sasl
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off 搜索下进程
[[email protected] sasl2]# service saslauthd start 启动saslauthd模块
[[email protected] sasl2]# vim /etc/mail/sendmail.mc 编辑配置文件
打开这2行,开启身份验证
[[email protected] sasl2]# service sendmail restart 重启邮件服务器
多了一个身份验证的服务
但是显示用其他的用户名依然能发出邮件,这是因为虽然可以做身份验证,但是没有强制用户作身份验证
[[email protected] sasl2]# vim /etc/mail/sendmail.mc 编辑配置文件
插入红色圈里的内容
[[email protected] sasl2]# service sendmail restart 重启sendmail
这时候随便使用账号发送邮件别拒绝,需要先进行身份验证
用telnet发送的时候,账号需要用base64重新编码一次
[[email protected] etc]# echo -n "[email protected]" |openssl base64 将账号重新编码
dXNlcjRAc2luYS5jb20=
[[email protected] etc]# echo -n "123" |openssl base64 将密码进行编码
MTIz
然后用编码后的账号密码进行验证
成功验证
然后进行邮件发送
日志显示邮件成功发送
然后使用邮件客户端测试下
如果不使用加密协议,则别服务器拒绝了
我的服务器要求验证打勾
然后邮件能够成功发送。