顯示具有 FreeBSD 標籤的文章。 顯示所有文章
顯示具有 FreeBSD 標籤的文章。 顯示所有文章

2014年12月24日 星期三

以Pure-ftp建構加密傳輸的FTP Service

     最近用Nexpose這套免費的弱點掃描軟體,強力掃描幾部Server,發現平常習以為常的FTP Service,被掃出FTP plaintext authentication弱點。FTP plaintext authentication中譯就是以純文字密碼登入認證。
     在FreeBSD環境中,最好架設的FTP就是Proftp,但查了一相關資料,發現它要配合SSL進行密碼加密或加密傳輸好像有點難度。因此轉向另一套也很很好裝的Pure-ftp。安裝與設定方式如下:

 1. cd  /usr/ports/ftp/pure-ftpd
 
     make install clean

    會出現如下畫面:


      建議勾選LARGEFILE、TLS與UTF8。

2. 安裝過程很快就結束了。接著在輸入:echo 'pureftpd_enable="YES"' >> /etc/rc.conf
    將pureftpd_enable="YES"加入 /etc/rc.conf中。

3. cd /usr/local/etc

   cp pure-ftpd.conf.sample pure-ftpd.conf

   ee pure-ftpd.conf

  在約423行,找到#TLS       1字樣,將前置註解符號#去掉,再將數字1改成2,存檔。

4.  建置SSL Key

      mkdir -p /etc/ssl/private 
 
      openssl req -x509 -nodes -newkey rsa:1024 -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem

      chmod 600 /etc/ssl/private/pure-ftpd.pem

5. 啟動pure-ftpd: /usr/local/etc/rc.d/pure-ftpd start

    FTP Client的設定,以FileZilla為例:

5. 點擊「站台管理員」→「新增站台」。會出現如下畫面:
 
 

    依序輸入站台名稱、主機IP
    勾選協定為「SFTP」,登入型式為「詢問密碼」,並輸入使用者。
    如下所圖所示:
    
   點擊確認後該站台即會被儲存。
   點擊連線即可以SFTP加密連線方式連入設定的IP位址。第一次登入時會要求輸入密碼。

   實在感覺不出來加密與未加密的傳輸有何差異,不過FTP搭配加密連線比較放心。


2014年12月21日 星期日

在FreeBSD環境下顯示顏色

FreeBSD預設的環境是黑白的,對多數使用者來說會搞不清楚什麼是檔案,什麼是名稱,什麼是執行檔。
以下這個方法可以解決這個問題。
切換成root,編輯 /etc/csh.cshrc,加入如下指令並存檔。

alias ls ls -FGa
setenv LSCOLORS ExGxFxdxCxegedabagExEx
setenv CLICOLOR yes
if ( $tty =~ ttyv* ) then
setenv  TERM    cons25
else
setenv  TERM    xterm-color
endif
if ( $USER == root ) then
set prompt="%B[%n@%m %/]# "
else
set prompt="%B[%n@%m %/]> "
endif
set autolist

退出root後再登入,執行 ls 指令就可以看到目錄及可執行檔的顏色!

2011年4月25日 星期一

在Apache2.2.X環境下,為Virtual Host設定SSL

其實這是林志民老師給我的功課,為的是線上研習系統和OpenID系統。我花了1.5個工作天才完成。設定方式與網路上能找到的不盡相同,特此紀錄下來。


1. 安裝與設定OpenSSL

cd /usr/ports/security/openssl

make install clean;rehash

注意,OpenSSL已到1.0.0.X版了。正式的版本比過去的0.9.X.X版穩定度和相容性高很多。

ee /etc/rc.conf

加入:apache22_flags="-DSSL" 並存檔。

2. 產生並設定認證金鑰(SSL Key for Apache)

mkdir /usr/local/etc/apache22/ssl.key

mkdir /usr/local/etc/apache22/ssl.crt

chmod 0700 /usr/local/etc/apache22/ssl.key

chmod 0700 /usr/local/etc/apache22/ssl.crt

cd /root

openssl genrsa -des3 -out server.key 1024   #以RSA編碼產生1024bit的加密金鑰 server.key

會出現如下圖:







在pass phrase 處輸入密碼,接著再輸入一次確認。

openssl req -new -key server.key -out server.csr  # 根據server.key產生邀請檔server.csr















openssl x509 -req -days 730 -in /root/server.csr -signkey /root/server.key -out /root/server.crt #自我簽署server.key 並產出server.crt




cp /root/server.key /usr/local/etc/apache22/ssl.key/  #複製ssl.key與ssl.crt到apche22的認證目錄

cp /root/server.crt /usr/local/etc/apache22/ssl.crt/
chmod 0400 /usr/local/etc/apache22/ssl.key/server.key #更改檔案屬性

chmod 0400 /usr/local/etc/apache22/ssl.crt/server.crt

cd /usr/local/etc/apache22/ssl.key

cp server.key server.key.bak

openssl rsa -in server.key.bak -out server.key   # 移除認證密碼,不然每次重啟Apache時都要輸入密碼

3. 設定Apache設定檔 -> httpd.conf

切記!mod_rewrite模組一定要先安裝好。

cd /usr/local/etc/apache22

ee httpd.conf

在設定檔最末,將預設的Virtual hosts與Secure (SSL/TLS) connections全部註解掉(如果之前有開啟的話),若無,維持預設值(註解)即可。並加入

如下設定,其中httpd-vhost-ssl.conf要自行產生。

# Virtual host with SSL

Include etc/apache22/extra/httpd-vhost-ssl.conf
將httpd.conf存檔。

4. 產出httpd-vhost-ssl.conf

cd /usr/local/etc/apache22/extra/

ee httpd-vhost-ssl.conf

1-41行如下:是照抄自httpd-ssl.conf中的一部份。

##  SSL Global Context
#
# When we also provide SSL we have to listen to the
# standard HTTP port (see above) and to the HTTPS port
#
# Note: Configurations that use IPv6 but not IPv4-mapped addresses need two
#       Listen directives: "Listen [::]:443" and "Listen 0.0.0.0:443"
#
Listen 443
##
##  SSL Global Context
##
##  All SSL configuration in this context applies both to
##  the main server and all SSL-enabled virtual hosts.
##
#
#   Some MIME-types for downloading Certificates and CRLs
#
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl    .crl
#   Pass Phrase Dialog:
#   Configure the pass phrase gathering process.
#   The filtering dialog program (`builtin' is a internal
#   terminal dialog) has to provide the pass phrase on stdout.
#SSLPassPhraseDialog  builtin
#   Inter-Process Session Cache:
#   Configure the SSL Session Cache: First the mechanism
#   to use and second the expiring timeout (in seconds).
#SSLSessionCache         "dbm:/usr/local/apache/logs/ssl_scache"
#SSLSessionCache        "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
#SSLSessionCacheTimeout  300
#   Semaphore:
#   Configure the path to the mutual exclusion semaphore the
#   SSL engine uses internally for inter-process synchronization.
#SSLMutex  "file:/usr/local/apache/logs/ssl_mutex"

44-69行,設定Virtual Hosts,以下的 xxx、yyy、zzz請自行設定

# Virtual Hosts
NameVirtualHost *:80
NameVirtualHost *:443
ServerAdmin XXX@wlps.kl.edu.tw
DocumentRoot "/usr/local/www/apache22/data/"
ServerName XXX.kl.edu.tw
ServerAdmin zzz@wlps.kl.edu.tw
DocumentRoot "/home/zzz/public_html/"
ServerName zzz.kl.edu.tw
ServerAdmin yyy@wlps.kl.edu.tw
DocumentRoot "/usr/local/www/apache22/data/yyy/"
ServerName yyy.kl.edu.tw

71 - 末,設定VirtualHost with SSL,其中的yyy、zzz請自行設定,應與44-69行之間的yyy、zzz相符合。

# Secure Shell ver.
ServerAdmin zzz@wlps.kl.edu.tw
DocumentRoot "/home/zzz/public_html/"
ServerName zzz.kl.edu.tw:443
#SSL Engine Switch:
#Enable/Disable SSL for this virtual host.
SSLEngine on
#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateFile /usr/local/etc/apache22/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/etc/apache22/ssl.key/server.key
ServerAdmin yyy@wlps.kl.edu.tw
DocumentRoot "/usr/local/www/apache22/data/yyy/"
ServerName yyy.kl.edu.tw:443
#SSL Engine Switch:
#Enable/Disable SSL for this virtual host.
SSLEngine on
#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateFile /usr/local/etc/apache22/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/etc/apache22/ssl.key/server.key

存檔後重新啟動Apache -> /usr/local/etc/rc.d/apache22 restart

5. 強制瀏覽Virtual Host一定都透過加密連線(https)

ee /home/zzz/public_html/.htaccess

加入:

RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteRule ^id.server$ index.php/serve [L]
RewriteRule ^u/(.+)$ ?user=$1 [L]
RewriteRule ^xrds/(.+)$ ?xrds=$1 [L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

存檔。

chmod 440 /home/zzz/public_html/.htaccess # 修改.htaccess 屬性。