Kalau anda pernah dihadapi tantangan mengelola infrastruktur aplikasi web yang hits nya super intensif dan sumberdaya hardware terbatas, maka mau tidak mau kita harus melakukan pendekatan ekstrim alias tidak biasanya. Misalnya dengan tehnik caching. Itu pun bukan dengan caching ala rata-rata CMS seperti drupal yang melakukan caching dengan cara men-generate blok konten dan menyimpannya di flat table untuk mengurangi overhead saat mengeksekusi SQL statement yang kaya JOIN; tapi dengan men-generate konten statik berupa file HTML. Salah satu alat bantu yang jadi andalan saya untuk itu adalah httrack. Dalam tulisan ini saya tidak hendak menulis soal itu tetapi yang lain lagi: mencari alternatif web server yang lebih hemat resource tanpa harus mengorbankan aspek keamanan.
Sebenarnya sudah lama saya ingin meninggalkan Apache Web Server yang saat ini sudah cenderung gemuk dan lumayan banyak makan resource. Tapi sialnya saya belum berani lepas dari ketergantungan terhadap modsecurity (http://www.modsecurity.org/) yang saat ini baru tersedia di Apache. Salah satu alternatifnya adalah Hiawatha Web Server yang fitur keamanannya sudah built-in. Seperti: DoS/flooding protection (mod evasive di Apache), Cross-site Scripting (XSS) prevention, Cross-site Request Forgery (CSRF/XSRF) prevention, dan SQL injection prevention. Sebagian fitur tersebut (seperti SQL injection prevention) masih kalah advanced dibanding modsecurity yang bisa dikustom pattern anti serangannya, tapi ini sudah lumayan banget. Plus dokumentasinya juga lengkap. Mari kita coba bersama :)
Operating System yang digunakan Ubuntu Server versi 10.04 LTS. Pastikan sudah terkoneksi ke internet dan sistem instalasi sudah berjalan dengan baik.
Instalasi PHP5 dan MySQL
sudo apt-get install mysql-server mysql-client php5-cgi php5 php5-cli php5-mysql php5-curl php5-gd php-pear php5-imagick php5-mcrypt php5-tidy php5-suhosin php5-xcache
Instalasi Development Libraries
sudo apt-get install libc6-dev libssl-dev dpkg-dev debhelper fakeroot libxml2-dev libxslt1-dev
Download source code Hiawatha versi terbaru (sampai tulisan ini dibuat, versi terbaru ada 7.4)
wget -c http://www.hiawatha-webserver.org/files/hiawatha-7.4.tar.gz
Lakukan ekstrasi dan masuk ke folder source code Hiawatha:
tar -xvf hiawatha-7.4.tar.gz
cd hiawatha-7.4
---------------------------------------------------------------
Catatan: jika menggunakan versi 7.4.1, anda tidak perlu lagi melakukan patch seperti diterangkan dibawah ini.
Lakukan patch keamanan seperti dijelaskan pada http://www.hiawatha-webserver.org/weblog/16. Buka file “hiawatha.c”, pada baris ke 421, ganti baris:
if (content_length < 0) { dengan
if ((content_length < 0) ||
(INT_MAX - content_length - 2 <= header_length)) {
dan pada awal file tambahkan juga baris:
#include “limits.h”
---------------------------------------------------------------
Kemudian lakukan konfigurasi dan generate file paket deb. Setelah itu lakukan instalasi.
./configure
make deb
cd ..
sudo dpkg -i hiawatha_7.4_i386.deb
Harusnya servis web hiawatha akan langsung diaktifkan. Kira-kira teks yang akan muncul:
Selecting previously deselected package hiawatha.
(Reading database ... 31852 files and directories currently installed.)
Unpacking hiawatha (from hiawatha_7.4_i386.deb) ...
Setting up hiawatha (7.4) ...
Starting webserver: Hiawatha
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for man-db ...
Oke, sekarang kita coba mengaktifkan SSL Bindings (HTTP encrypted via SSL) jika kita butuh layanan terenkripsi. Pertama kita buat dulu file sertifikat.
openssl genrsa -out serverkey.pem 2048
openssl req -new -x509 -days 3650 -key serverkey.pem -out server.crt
echo "" >> serverkey.pem
cat server.crt >> serverkey.pem
echo "" >> serverkey.pem
rm -f server.crt
sudo cp serverkey.pem /etc/hiawatha/
Edit file /etc/hiawatha/hiawatha.conf
#Binding {
# Port = 443
# Interface = ::1
# MaxKeepAlive = 30
# TimeForRequest = 3,20
# SSLcertFile = hiawatha.pem
#}
menjadi
Binding {
Port = 443
# Interface = ::1
# MaxKeepAlive = 30
# TimeForRequest = 3,20
SSLcertFile = /etc/hiawatha/serverkey.pem
}
Sekarang restart servis hiawatha:
/etc/init.d/hiawatha restart
Berikutnya mengaktifkan PHP via FastCGI (http://www.fastcgi.com). Edit file “/etc/hiawatha/php-fcgi.conf”, kemudian ubah baris:
#Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data ; /etc/php5/cgi/php.ini
menjadi
Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data ; /etc/php5/cgi/php.ini
Kemudian jalankan servis fastcgi:
sudo php-fcgi -c /etc/hiawatha/php-fcgi.conf
Sekarang edit file konfigurasi hiawatha di “/etc/hiawatha/hiawatha.conf”, dari:
#FastCGIserver {
# FastCGIid = PHP5
# ConnectTo = 10.0.0.100:2005, 10.0.0.101:2005
# Extension = php, php5
# SessionTimeout = 30
#}
menjadi:
FastCGIserver {
FastCGIid = PHP5
ConnectTo = 127.0.0.1:2005
Extension = php
SessionTimeout = 30
}
UseFastCGI = PHP5
terakhir menambahkan permission agar CGI bisa dieksekusi. Tambahkan juga baris:
ExecuteCGI = yes
Kemudian simpan file dan restart servis hiawatha.
Untuk melakukan pengetesan, pada direktori “/var/www/hiawatha/” buat file dengan nama “info.php”, edit dan isi dengan baris:
<?php phpinfo(); ?>
akses dengan browser, misalnya pada alamat: http://localhost/info.php
Harusnya muncul informasi konfigurasi PHP di sistem server.
Oke sekarang mari kita coba meningkatkan keamanan di sisi server. Pertama mari kita lihat beberapa settting di php.ini yang mungkin bisa di-tune-in. Edit file “/etc/php5/cgi/php.ini” dan cari beberapa baris berikut ini dan ubah:
display_errors = Off
Untuk tidak menampilkan error PHP (jika terjadi). Tapi di server development, sebaiknya di-set “On” agar mudah melakukan troubleshooting.
allow_url_fopen = Off
Untuk men-disable fungsi membuka file via http/ftp. Tapi terkadang beberapa aplikasi membutuhkan fitur ini. Gunakan sesuai kebutuhan.
safe_mode = On
Untuk menjalankan PHP dalam mode safe_mode. Beberapa fungsi PHP tidak jalan ketika safe_mode diaktifkan. Beberapa aplikasi juga membutuhkan ini, jadi gunakan sesuai kebutuhan.
disable_function =
Untuk men-disable beberapa fungsi yang mungkin bisa disalahgunakan secara tidak bertanggungjawab seperti: system, show_source, symlink, exec, dl, shell_exec, passthru, phpinfo, escapeshellarg, escapeshellcmd. Misalnya kita ingin men-disable fungsi shell_exec dan phpinfo, maka:
disable_function = shell_exec, phpinfo
Kalau sudah selesai, restart servis fastcgi:
sudo php-fcgi -k -c /etc/hiawatha/php-fcgi.conf
sudo php-fcgi -c /etc/hiawatha/php-fcgi.conf
Pada Hiawatha, beberapa setting sekuriti hanya berlaku pada virtual host, karenanya kita juga harus mendefinisikannya terlebih dahulu. Setting Virtual Host bisa dilakukan langsung pada file “/etc/hiawatha/hiawatha.conf”. Tetapi untuk kemudahan administrasi kita akan memisahkan setting Virtual Host pada folder “enabled-sites”. Untuk itu pada file “/etc/hiawatha/hiawatha.conf” tambahkan baris:
Include
/etc/hiawatha/enabled-sites/
Buat folder “/etc/hiawatha/enabled-sites”:
sudo mkdir -p /etc/hiawatha/enabled-sites
Sekarang misalnya kita ingin membuat Virtual Host untuk myhost.mydomain.com. Buat file “/etc/hiawatha/enabled-sites/myhost.mydomain.com”. Kemudian edit dan tambahkan baris-baris berikut ini:
VirtualHost {
Hostname = www.myhost.mydomain.com, myhost.mydomain.com
WebsiteRoot = /var/www/hiawatha
StartFile = index.php
AccessLogfile = /var/log/hiawatha/access.myhost.mydomain.com.log
ErrorLogfile = /var/log/hiawatha/error.myhost.mydomain.com.log
TimeForCGI = 15
UseFastCGI = PHP5
DenyBody = ^.*%3Cscript.*%3C%2Fscript%3E.*$
ExecuteCGI = yes
PreventCSRF = yes
PreventSQLi = no
PreventXSS = yes
RequireSSL = no
ShowIndex = no
DenyBot = Googlebot:/
DenyBot = twiceler:/
DenyBot = MSNBot:/
DenyBot = yahoo:/
DenyBot = BaiDuSpider:/
DenyBot = Ask:/
DenyBot = Yahoo! Slurp:/
DenyBot = Sogou web spider:/
DenyBot = Sogou-Test-Spider:/
DenyBot = Baiduspider+:/
DenyBot = Yandex:/
DenyBot = UniversalFeedParser:/
DenyBot = Mediapartners-Google:/
DenyBot = Sosospider+:/
DenyBot = YoudaoBot:/
DenyBot = ParchBot:/
DenyBot = Curl:/
DenyBot = msnbot:/
DenyBot = NaverBot:/
DenyBot = taptubot:/
}
Oke, coba restart hiawatha dan coba akses berdasarkan nama domain tersebut.
| # Hiawatha main configuration file
# GENERAL SETTINGS ServerId = www-data ConnectionsTotal = 500 ConnectionsPerIP = 20 SystemLogfile = /var/log/hiawatha/system.log GarbageLogfile = /var/log/hiawatha/garbage.log #ExecuteCGI = yes ServerString = Apache CGIwrapper = /usr/sbin/cgi-wrapper BanOnSQLi = 60 BanOnFlooding = 10/1:15
# BINDING SETTINGS # A binding is where a client can connect to. # Binding { Port = 80 # Interface = 127.0.0.1 MaxKeepAlive = 30 TimeForRequest = 3,20 MaxRequestSize = 256 MaxUploadSize = 2 } # Binding { Port = 443 # Interface = ::1 MaxKeepAlive = 30 TimeForRequest = 3,20 SSLcertFile = /etc/hiawatha/serverkey.pem MaxRequestSize = 256 MaxUploadSize = 2 }
# BANNING SETTINGS # Deny service to clients who misbehave. # BanOnGarbage = 300 ConnectionsPerIP = 20 BanOnMaxPerIP = 5 BanOnMaxReqSize = 300 #KickOnBan = yes #RebanDuringBan = yes
# COMMON GATEWAY INTERFACE (CGI) SETTINGS # These settings can be used to run CGI applications. Use the 'php-fcgi' # tool to start PHP as a FastCGI daemon. # #CGIhandler = /usr/bin/perl:pl #CGIhandler = /usr/bin/php-cgi:php #CGIhandler = /usr/bin/python:py #CGIhandler = /usr/bin/ruby:rb #CGIhandler = /usr/bin/ssi-cgi:shtml #CGIextension = cgi # FastCGIserver { FastCGIid = PHP5 ConnectTo = 127.0.0.1:2005 Extension = php, php5 SessionTimeout = 30 }
# URL TOOLKIT # This URL toolkit rule was made for the Banshee PHP framework, which # can be downloaded from http://www.hiawatha-webserver.org/banshee # #UrlToolkit { # ToolkitID = banshee # RequestURI isfile Return # Match ^/(css|files|images|js)/ Return # Match ^/(favicon.ico|robots.txt|sitemap.xml)$ Return # Match .*\?(.*) Rewrite /index.php?$1 # Match .* Rewrite /index.php #}
# DEFAULT WEBSITE # It is wise to use your IP address as the hostname of the default website # and give it a blank webpage. By doing so, automated webscanners won't find # your possible vulnerable website. # Hostname = 192.168.1.9 WebsiteRoot = /var/www/hiawatha StartFile = index.html AccessLogfile = /var/log/hiawatha/access.log ErrorLogfile = /var/log/hiawatha/error.log LogFormat = extended #ErrorHandler = 404:/error.cgi
# VIRTUAL HOSTS Include /etc/hiawatha/enabled-sites/
# Use a VirtualHost section to declare the websites you want to host. # #VirtualHost { # Hostname = www.my-domain.com # WebsiteRoot = /var/www/my-domain/public # StartFile = index.php # AccessLogfile = /var/www/my-domain/log/access.log # ErrorLogfile = /var/www/my-domain/log/error.log # TimeForCGI = 5 # UseFastCGI = PHP5 # UseToolkit = banshee #}
# DIRECTORY SETTINGS # You can specify some settings per directory. # #Directory { # Path = /home/baduser # ExecuteCGI = no # UploadSpeed = 10,2 #}
|
| Isi file “/etc/hiawatha/hiawatha.conf” |
| # PHP FastCGI configuration
# Path to PID-file. # PidFile = <filename> # PidFile = /var/run/php-fcgi.pid
# Number of forks per server. # Forks = <number> # Forks = 3
# Number of maximum requests per fork before respawning. # MaxRequests = <number> # MaxRequests = 100
# Set environment variables for the FastCGI processes. # Setenv <key> = <value> #
# PHP FastCGI servers to start. # Server = <php-cgi executable>;<binding>;<UID>[:<GIDs>][;<PHP configuration file>] # #Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data #Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; 1000:100,101 Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data ; /etc/php5/cgi/php.ini #Server = /usr/chroot|usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data |
| Isi file “/etc/hiawatha/php-fcgi.conf” |
Untuk meningkatkan keamanan dari beberapa serangan dengan pola tertentu dengan mengecek “Request Body”, kita bisa menambahkan dari berbagai referensi. Misalnya saya mencoba memasukkan pola serangan pada http://www.symantec.com/connect/articles/detection-sql-injection-and-cross-site-scripting-attacks dan menambahkannya di setting virtual host dan menambahkan pada variabel DenyBody. Contohnya pada konfigurasi dibawah ini.
| VirtualHost { Hostname = www.myhost.mydomain.com, myhost.mydomain.com WebsiteRoot = /var/www/hiawatha StartFile = index.php AccessLogfile = /var/log/hiawatha/access.myhost.mydomain.com.log ErrorLogfile = /var/log/hiawatha/error.myhost.mydomain.com.log TimeForCGI = 15 UseFastCGI = PHP5 DenyBody = ^.*%3Cscript.*%3C%2Fscript%3E.*$ # Added rules from # http://www.symantec.com/connect/articles/detection-sql-injection-and-cross-site-scripting-attacks DenyBody = /(\%27)|(\')|(\-\-)|(\%23)|(#)/ix DenyBody = /((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i DenyBody = /\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/ix DenyBody = /((\%27)|(\'))union/ix DenyBody = /((\%27)|(\'))select/ix DenyBody = /((\%27)|(\'))insert/ix DenyBody = /((\%27)|(\'))delete/ix DenyBody = /((\%27)|(\'))update/ix DenyBody = /((\%27)|(\'))drop/ix DenyBody = /exec(\s|\+)+(s|x)p\w+/ix DenyBody = /((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/ix DenyBody = /((\%3C)|<)((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>)/I DenyBody = /((\%3C)|<)[^\n]+((\%3E)|>)/I ExecuteCGI = yes PreventCSRF = yes PreventSQLi = no PreventXSS = yes RequireSSL = no ShowIndex = no DenyBot = Googlebot:/ DenyBot = twiceler:/ DenyBot = MSNBot:/ DenyBot = yahoo:/ DenyBot = BaiDuSpider:/ DenyBot = Ask:/ DenyBot = Yahoo! Slurp:/ DenyBot = Sogou web spider:/ DenyBot = Sogou-Test-Spider:/ DenyBot = Baiduspider+:/ DenyBot = Yandex:/ DenyBot = UniversalFeedParser:/ DenyBot = Mediapartners-Google:/ DenyBot = Sosospider+:/ DenyBot = YoudaoBot:/ DenyBot = ParchBot:/ DenyBot = Curl:/ DenyBot = msnbot:/ DenyBot = NaverBot:/ DenyBot = taptubot:/ #WrapCGI = jail_myhost_mydomain_com } |
Bosen dan ingin mencoba komparasi menggantikan Fastcgi dengan PHP-FPM? Gampang. Update "/etc/apt/source.list" dan tambahkan:
deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
Kemudian update informasi GPGPubkey:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8D0DC64F
sudo apt-get update
sudo apt-get install php5-fpm
Jalankan servis php-fpm yang akan berjalan pada port 9000
sudo service php5-fpm start
atau
sudo /etc/init.d/php5-fpm start
Kemudian edit "/etc/hiawatha/hiawatha.conf", cari baris:
ConnectTo = 127.0.0.1:2005
dan ganti menjadi
ConnectTo = 127.0.0.1:9000
Edit juga file /etc/hiawatha/php-fcgi.conf:
Server = /usr/bin/php5-fpm ; 127.0.0.1:9000 ; www-data ; /etc/php5/fpm/php.ini
Restart servis hiawatha:
sudo /etc/init.d/hiawatha restart
Cek dengan phpinfo dan cari baris dengan keyword FPM untuk memastikan sudah berjalan dengan baik. Konfigurasi terkait PHP-FPM ada di "/etc/php5/fpm/". Selamat Mencoba! Semoga bermanfaat. :)