1. 程式人生 > 實用技巧 >Linux 下sendmail 的加密與認證

Linux 下sendmail 的加密與認證

Linux 下sendmail 的加密與認證

上節我們只是搭建了一個初步的sendmail伺服器,它沒有提供加密和認證的功能,也就是說誰都可以訪問你的sendmail伺服器,也可以使用你的伺服器,最壞的是,它可以任意的給你的sendmail傳送垃圾郵件,或者偽造你的任意一個伺服器的使用者來為別人傳送郵件.這些都會一定程度上對你的伺服器造成破壞,要想避免這些干擾,我們可以使用sendmail的加密與認證功能. 我們要使用的上節配置好的sendmail伺服器,並且使用域名為sina.com. 環境: virtualbox 或vmware sendmail伺服器:  redhat5.4 IP:192.168.2.10 測試機:       windows2003 IP:192.168.2.20 一.CA認證伺服器的配置   1.配置CA主配置檔案 [
[email protected]
~]# cd /etc/pki [[email protected] pki]# cd tls [[email protected] tls]# vim openssl.cnf 修改如下內容 [ CA_default ] dir = /etc/pki/CA  //修改此行,它為CA證書目錄 certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt #unique_subject = no new_certs_dir = $dir/newcerts certificate = $dir/cacert.pem serial = $dir/serial 2.按照第一步中需要的檔案與目錄,來建立檔案 [
[email protected]
CA]# cd /etc/pki/CA/ [[email protected] CA]# mkdir certs crl newcerts [[email protected] CA]# touch index.txt serial [[email protected] CA]# echo "01">serial 3.產生CA伺服器的私鑰   [[email protected] CA]# openssl genrsa 1024 > private/cakey.pem Generating RSA private key, 1024 bit long modulus ............................++++++ ...++++++ e is 65537 (0x10001) 4.因為CA伺服器是最高的傳送證書者,所以它需要為自己申請一個證書證明自己的可靠性,下面生成根證書  在根證書生成之前要先修改一下證書所能使用的區域 [ policy_match ] countryName = optional //預設為match stateOrProvinceName = optional //預設為match organizationName = optional //預設為match organizationalUnitName = optional commonName = supplied emailAddress = optional  修改成optional是為了讓此CA伺服器釋出的證書能為不同地區的伺服器使用,如果使用match的話,如 果本CA伺服器所屬的省是河南省,則此CA伺服器釋出的證書只有河南省的伺服器才可以正確認 證,河北省的就不可以得到認證,也就不可靠.使用optional的話,不管此CA伺服器在哪個城市,另外的 省 市都可以使用此CA伺服器進行認證. 開始生成根證書 [
[email protected]
CA]# openssl req -new -key private/cakey.pem -x509 -out cacert.pem 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) [GB]:CN State or Province Name (full name) [Berkshire]:HENAN Locality Name (eg, city) [Newbury]:ZHENGZHOU Organization Name (eg, company) [My Company Ltd]:sina Organizational Unit Name (eg, section) []:tec Common Name (eg, your name or your server's hostname) []:root.root Email Address []: [[email protected] CA]# 二  實現POP3進行加密 1.我們把POP3的證書存放在/etc/dovecot/certs下    首先建立這個目錄    [[email protected] CA]# mkdir -pv /etc/dovecot/certs mkdir: 已建立目錄 “/etc/dovecot” mkdir: 已建立目錄 “/etc/dovecot/certs” 2.建立私鑰   [[email protected] CA]# cd /etc/dovecot/certs/ [[email protected] certs]# openssl genrsa 1024 > dovecot.key Generating RSA private key, 1024 bit long modulus .................................++++++ ...++++++ e is 65537 (0x10001) 3.生成此POP3伺服器的請求證書   [[email protected] certs]# openssl req -new -key dovecot.key -out dovecot.csr 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) [GB]:CN State or Province Name (full name) [Berkshire]:HENAN Locality Name (eg, city) [Newbury]:Zhengzhou Organization Name (eg, company) [My Company Ltd]:sina Organizational Unit Name (eg, section) []:pop3 Common Name (eg, your name or your server's hostname) []:pop3.sina.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]# 4.請求CA伺服器頒發證書   [[email protected] certs]# openssl ca -in dovecot.csr -out dovecot.cert Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 1 (0x1) Validity Not Before: Nov 15 10:11:00 2012 GMT Not After : Nov 15 10:11:00 2013 GMT Subject: countryName = CN stateOrProvinceName = HENAN organizationName = sina organizationalUnitName = pop3 commonName = pop3.sina.com 5.修改pop3的配置檔案,使伺服器支援pop3加密 [[email protected] certs]# vim /etc/dovecot.conf 修改如下 20行    protocols = imaps pop3s 91 行   ssl_cert_file = /etc/dovecot/certs/dovecot.cert 92 行   ssl_key_file = /etc/dovecot/certs/dovecot.key  6.啟動pop3伺服器    [[email protected] certs]# service dovecot restart 停止 Dovecot Imap: [確定] 啟動 Dovecot Imap: [確定] [[email protected] certs]# 測試是否啟動pop3s [[email protected] certs]# netstat -tupln |grep dovecot tcp 0 0 :::993 :::* LISTEN 2740/dovecot tcp 0 0 :::995 :::* LISTEN 2740/dovecot 7.在客戶機上測試是否成功   首先要對客戶端的使用者進行使用pop3s的設定

點選使用ssl加密,因為前面我們已經去掉了支援pop3,只能使用加密方法pop3s,所以如果直接收郵件 是不成功的. 在伺服器端發一個郵件 [[email protected] certs]# mail code Subject: hello hello this is a test! . Cc: 在客戶端接收 接收時會出現下面的情況

直接點選OK就行了,這樣就成功了,我們也可以使用抓包的方式來進行驗證 首先安裝wireshark   [[email protected] yum.repos.d]# yum install wireshark 進行抓包 [[email protected] yum.repos.d]# tshark -ni eth0 -R "tcp.port eq 995"     0.012501 192.168.2.20 -> 192.168.2.10 SSL Client Hello  //可以看到使用了ssl加密   0.012567 192.168.2.10 -> 192.168.2.20 TCP 995 > 3624 [ACK] Seq=1 Ack=110 Win=5840 Len=0  0.015353 192.168.2.10 -> 192.168.2.20 TLSv1 Server Hello, Certificate, Server Hello Done   0.016918 192.168.2.20 -> 192.168.2.10 TLSv1 Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message 三 實現SMTP的加密 1.SMTP的證書的請求基本上與POP3的想像,下面只發一些步驟  [[email protected] ~]# mkdir -pv /etc/sendmail/certs mkdir: 已建立目錄 “/etc/sendmail” mkdir: 已建立目錄 “/etc/sendmail/certs” [[email protected] ~]# cd /etc/sendmail/certs/ 產生私鑰 [[email protected] certs]# openssl genrsa 1024 > sendmail.key Generating RSA private key, 1024 bit long modulus ......++++++ ................................................++++++ e is 65537 (0x10001) 生成請求證書 [[email protected] certs]# openssl req -new -key sendmail.key -out sendmail.csr 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) [GB]:CN State or Province Name (full name) [Berkshire]:HENAN Locality Name (eg, city) [Newbury]:Zhengzhou Organization Name (eg, company) [My Company Ltd]:sina Organizational Unit Name (eg, section) []:smtp Common Name (eg, your name or your server's hostname) []: smtp.sina.com Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name CA伺服器頒發證書 [[email protected] certs]# openssl ca -in sendmail.csr -out sendmail.cert Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 2 (0x2) Validity Not Before: Nov 15 10:53:33 2012 GMT Not After : Nov 15 10:53:33 2013 GMT Subject: countryName = CN stateOrProvinceName = HENAN organizationName = sina organizationalUnitName = smtp commonName = smtp.sina.com X509v3 extensions: 2.修改此證書及私鑰的許可權   [[email protected] certs]# cd /etc/sendmail/certs [[email protected] certs]# chmod 600 * 注:這一步一定要做,不然的話,sendmail的加密設定是不成功的. 3.修改配置檔案,使伺服器支援smtps加密    [[email protected] ~]# vim /etc/mail/sendmail.mc     修改成如下      60 define(`confCACERT_PATH', `/etc/pki/CA')dnl     61 define(`confCACERT', `/etc/pki/CA/cacert.pem')dnl     62 define(`confSERVER_CERT', `/etc/sendmail/certs/sendmail.cert') 63 define(`confSERVER_KEY', `/etc/sendmail/certs/sendmail.key') 134 DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl  4.重啟sendmail伺服器    [[email protected] ~]# service sendmail restart 關閉 sm-client: [確定] 關閉 sendmail: [確定] 啟動 sendmail: [確定] 啟動 sm-client: [確定] 4. 測試    首先對客戶端使用者進行設定

   傳送郵件檢測是否正確     

直接點選是即可 也可使用抓包來驗證 [[email protected] yum.repos.d]# tshark -ni eth0 -R "tcp.port eq 25" 四 sendmail使用者認證配置 SMTP協議並沒有提供認證功能,很容易匿名使用者進行發郵件,我們可以使用第三方軟體來進行 sendmail smtp的認證 sasl(簡單難和安全層),是專門用於smtp中對使用者的認證功能. 1.檢視是否安裝此軟體   [[email protected] certs]# rpm -qa | grep sasl cyrus-sasl-lib-2.1.22-5.el5 cyrus-sasl-2.1.22-5.el5 cyrus-sasl-devel-2.1.22-5.el5 cyrus-sasl-plain-2.1.22-5.el5 若沒有安裝則自己手動安裝 2.若要sendmail傳送郵件時擁有認證的功能要在smtp的配置檔案中進行載入一定的模組,對配置檔案進行配置  [[email protected] ~]# vim /etc/mail/sendmail.mc 39 define(`confAUTH_OPTIONS', `A y')dnl  //在A後加上"y" 52 TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl 53 define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLA IN')dnl //這兩行去掉前面的dnl註釋 116 DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA,M=Ea')dnl //這行後面加上",M=Ea" 3.重新啟動sendmail伺服器,啟動sasl伺服器   [[email protected] ~]# service sendmail restart 關閉 sm-client: [確定] 關閉 sendmail: [確定] 啟動 sendmail: [確定] 啟動 sm-client: [確定] [ [email protected] ~]# service saslauthd start 啟動 saslauthd: [確定] 4.利用telnet功能驗證是否支援驗證   [[email protected] ~]# telnet 127.0.0.1 25 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-8BITMIME 250-SIZE 250-DSN 250-AUTH LOGIN PLAIN  //出現這一行就說明已經支援sasl認證 250-STARTTLS      //這個是支援smtp加密,上一節內容 250-DELIVERBY 250 HELP  5.測試   現在我們可以使用一個沒有經過認證的使用者傳送郵件,看能不能傳送   [[email protected] ~]# telnet 127.0.0.1 25 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). 220 localhost.localdomain ESMTP Sendmail 8.13.8/8.13.8; Thu, 15 Nov 2012 20:20:31 +0800 mail from:[email protected]   //使用使用者code來發郵件 530 5.7.0 Authentication required(認證要求)//系統提示530錯誤 系統提示是需要認證要求的,現在我們把這個賬戶進行認證,因為這個認證只支援base64編碼的字 符,所以要先把code的使用者名稱與密碼用base64編碼進行編碼 [[email protected] ~]# echo -n "[email protected]" | openssl base64 Y29kZUBzaW5hLmNvbQ==         //使用者名稱進行編碼之後顯示的字元 [[email protected] ~]# echo -n "abcd1234" | openssl base64 YWJjZDEyMzQ=              //密碼進行編碼之後顯示的字元  讓系統進行認證   [[email protected] ~]# telnet 127.0.0.1 25 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. 220 localhost.localdomain ESMTP Sendmail 8.13.8/8.13.8; Thu, 15 Nov 2012 20:36:25 +0800 auth login Y29kZUBzaW5hLmNvbQ==   //手動輸入,後面是你的使用者名稱編碼過的base64編碼 334 UGFzc3dvcmQ6 YWJjZDEyMzQ=             //輸入密碼的base64字元 235 2.0.0 OK Authenticated       //提示認證成功了 mail from:[email protected] 250 2.1.0 [email protected] Sender ok   //可以傳送郵件了 在客戶端進行配置   因為我們要對使用者進行驗證,所以要對使用者進行如下配置  

現在就可以進行正常的使用了,但是隻有認證過的使用者才可以正常使用