郵件系統的特點:快捷,安全,多樣性
實現方式:
Windows: 電子郵件服務 03 08 exchange
Linux: sendmail 【無身份驗證 無加密 無防範垃圾郵件,病毒】
POSTFIX 【模塊化,快】
Qmail
MAA:郵件訪問代理 tcp 110 (pop3) tcp 109 (pop2) tcp 143 (imp4)
MUA: 郵件用戶代理
MDA: 郵件的傳遞/分檢代理
實現系統:redhat5系列
打開一臺redhat機器
[root@localhost ~]# rpm -qa |grep sendmail
sendmail-8.13.8-8.el5 查看軟件是否安裝,如果沒有安裝則需要安裝
[root@localhost ~]# rpm -qa |grep dovecot
[root@localhost ~]# rpm -qa |grep bind 經查詢dovecot bind 沒有安裝
[root@localhost ~]# mkdir /media/cdrom
[root@localhost ~]# mount /dev/cdrom /media/cdrom/ 掛載光盤
[root@localhost ~]# cd /media/cdrom/Server/ 進入軟件目錄
[root@localhost 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個軟件包
[root@localhost Server]# cd /var/named/chroot/etc/ 進入目錄
[root@localhost etc]# cp -p named.caching-nameserver.conf named.conf 製作配置文件
[root@localhost etc]# vim named.conf 編輯配置文件
修改爲any
將視圖也修改爲any
[root@localhost etc]# vim named.rfc1912.zones
增加163.com區域
[root@localhost etc]# cd ../var/named/ 進入區域文件的位置產生區域文件
[root@localhost 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
[root@localhost named]# cp -p localhost.zone 163.com.zone 拷貝區域文件修改
修改爲此dns配置
[root@localhost named]# service named start 開啓dns服務器
[root@localhost named]# chkconfig named on
[root@localhost named]# netstat -tupln |grep named 查看端口
[root@localhost named]# vim /etc/resolv.conf
本機既做dns服務器也做dns客戶端
[root@localhost 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 進行解析測試
[root@localhost named]# dig -t mx 163.com
[root@localhost named]# hostname 查看主機名
[root@localhost named]# vim /etc/sysconfig/network 修改主機名
修改爲dns服務器能查找到的名字
[root@localhost named]# hostname mail.163.com 不重啓直接能查到修改的用戶名
[root@mail ~]# cd /etc/mail 進入目錄配置sendmail
[root@mail 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軟件
[root@mail mail]# rpm -qa|grep m4 查看是否安裝m4文件
[root@mail mail]# rpm -ivh /media/cdrom/Server/sendmail-cf-8.13.8-8.el5.i386.rpm 安裝sendmail-cf文件
[root@mail mail]# vim sendmail.mc 修改sendmail.mc文件
修改爲此
[root@mail mail]# service sendmail restart 重啓sendmail
[root@mail mail]# netstat -tupln|grep sendmail 查看監聽端口是否改變
[root@mail mail]# vim local-host-names 進行分檢的時候比對信件,需要寫主機的別名和主機名
[root@mail mail]# service sendmail restart 重啓
進行測試:
[root@mail mail]# useradd user1
[root@mail mail]# useradd user2 建立user1和user2賬號
[root@mail mail]# mail -s ok [email protected]
1111111111111
.
Cc:
發送郵件
[root@mail mail]# tail -f /var/log/maillog
日誌顯示已發送
查看到收到信件
用同樣的方法再搭建一個mail.sina.com郵件系統
當郵件系統搭建好之後,使用的協議爲http ftp smtp pop3 imap4爲了保證郵件發送接收的安全,需要使用https ftps smtps pop3s imap4s協議,使用密文傳輸。
先試驗下使用明文傳輸時被抓包工具截獲時候的狀態:
先安裝抓包工具
[root@mail ~]# mount /dev/cdrom /media/cdrom/
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@mail ~]# cd /media/cdrom/Server/
安裝此安裝包
[root@mail 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 出現如此情況需要安裝
[root@mail Server]# rpm -ivh libsmi-0.4.5-2.el5.i386.rpm 安裝此軟件包
[root@mail Server]# rpm -ivh wireshark-1.0.8-1.el5_3.1.i386.rpm 再安裝
[root@mail Server]# service sendmail start
[root@mail Server]# service named start 重啓郵件服務器和域名解析服務器
找一臺郵件客戶機,爲了試驗方便放在了同一網段,dns服務器也做在了郵件服務器上
域名解析測試
正常
然後用郵件客戶端建個郵件用戶
然後工具→賬戶→郵件→選擇自己剛纔建的賬號將名字修改爲user1(好記)
[root@mail Server]# tshark -ni eth0 -R "tcp.port eq 25" 開啓抓包工具進行抓包(監聽eth0網卡25端口的流量)
然後用郵件客戶端給自己發封信件
點擊發送
然後查看抓包工具截獲的內容
重要信息都可以看到
[root@mail Server]# tshark -ni eth0 -R "tcp.port eq 110" 抓取接受郵件端口的流量
然後進行接收
然後查看抓取到的內容
用戶名和密碼別抓取到了
搭建ca服務器(在192.168.2.100上和郵件服務器在同一機器上)
先查看是否支持ca
[root@mail Server]# sendmail -d0.1 -bv 查看編譯的時候軟件配置
查看到可以支持 STARTTLS
查看功能是否啓動,沒有 STARTTLS,所以沒有啓動
[root@mail Server]# cd /etc/pki/tls/
[root@mail tls]# vim openssl.cnf 編輯ca配置文件
修改爲指定目錄,其他的目錄需要創建
指定能給其他人使用
修改默認值
[root@mail ~]# cd /etc/pki/CA/
[root@mail CA]# mkdir certs crl 創建需要的目錄
[root@mail CA]# touch index.txt 創建需要的文件
[root@mail CA]# mkdir newcerts
[root@mail CA]# touch serial
[root@mail CA]# echo "01" >serial 給16進制的01賦給serial
[root@mail CA]# openssl genrsa 1024 >private/cakey.pem 創建私鑰
[root@mail CA]# chmod 600 private/cakey.pem 修改權限已保證私鑰的安全性
[root@mail CA]# openssl req -new -key private/cakey.pem -x509 -out cacert.pem 使用ca的私鑰創建證書
[root@mail CA]# mkdir pv /etc/mail/certs 創建一個目錄放證書,私鑰,請求。
[root@mail CA]# cd /etc/mail/certs/
[root@mail certs]# openssl genrsa 1024 >sendmail.key 創建私鑰
[root@mail certs]# chmod 600 sendmail.key
[root@mail 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 []:
[root@mail certs]# openssl ca -in sendmail.req -out sendmail.cert 利用請求得到證書
[root@mail 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 所需要的文件已經有了
[root@mail certs]# vim /etc/mail/sendmail.mc 修改配置文件
[root@mail certs]# service sendmail restart 重啓sendmail服務器
再次查看多了STARTTLS
[root@mail certs]# netstat -tupln |grep sendmail
[root@mail certs]# tshark -ni eth0 -R "tcp.port eq 25" 再進行抓包測試
郵件服務器端:
工具→賬戶→user1
選擇使用ssl
這時候看到的都是密文
實現了smtps
然後實現接受服務器的加密
[root@mail ~]# mkdir -pv /etc/dovecot/certs 創建接受服務器的證書目錄
[root@mail certs]# openssl genrsa 1024 >dovecot.key 創建私鑰
[root@mail certs]# chmod 600 dovecot.key
[root@mail 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 []:
[root@mail certs]# openssl ca -in dovecot.req -out dovecot.cert 利用請求生成證書
[root@mail certs]# vim /etc/dovecot.conf 編輯配置文件
修改目錄
只使用密文端口
[root@mail certs]# tshark -ni eth0 -R "tcp.port eq 995" 進行抓包
設置接收信件爲密文端口進行接收
接收顯示需要使用證書
此時抓包顯示的是
被加密的內容
可以測試到沒有用戶的認證,隨便一個用戶都能發送郵件
默認情況下郵件的發送不做身份驗證
要想進行身份需要使用sasl協議
[root@mail ~]# mount /dev/cdrom /media/cdrom/
[root@mail ~]# cd /media/cdrom/Server/ 掛載光盤,進入server目錄
查找到此軟件包
[root@mail Server]# rpm -qa|grep cyrus 查看系統是否安裝此如軟件包
顯示已經安裝過了
[root@mail Server]# chkconfig --list |grep sasl
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off 搜索下進程
[root@mail sasl2]# service saslauthd start 啓動saslauthd模塊
[root@mail sasl2]# vim /etc/mail/sendmail.mc 編輯配置文件
打開這2行,開啓身份驗證
[root@mail sasl2]# service sendmail restart 重啓郵件服務器
多了一個身份驗證的服務
但是顯示用其他的用戶名依然能發出郵件,這是因爲雖然可以做身份驗證,但是沒有強制用戶作身份驗證
[root@mail sasl2]# vim /etc/mail/sendmail.mc 編輯配置文件
插入紅色圈裏的內容
[root@mail sasl2]# service sendmail restart 重啓sendmail
這時候隨便使用賬號發送郵件別拒絕,需要先進行身份驗證
用telnet發送的時候,賬號需要用base64重新編碼一次
[root@mail etc]# echo -n "[email protected]" |openssl base64 將賬號重新編碼
dXNlcjRAc2luYS5jb20=
[root@mail etc]# echo -n "123" |openssl base64 將密碼進行編碼
MTIz
然後用編碼後的賬號密碼進行驗證
成功驗證
然後進行郵件發送
日誌顯示郵件成功發送
然後使用郵件客戶端測試下
如果不使用加密協議,則別服務器拒絕了
我的服務器要求驗證打勾
然後郵件能夠成功發送。