Wednesday, February 22, 2012

Compiling PHP 5.2.X on Redhat Linux


I love yum!! But I also hate how you can’t install previous version of software sometime.
Because of Zend dependency and PHP 5.2.x I recently hit a problem where I couldn’t install it via yum. All yum repo’s only had PHP 5.3.x . I specifically need 5.2.10+. Not only that, I needed extensions like gd, soap, mbstring, mhash, mcrypt extra. Anyway, after a lot of trial and error, I finally got it working. Here are my list of commands (For a 64-bit Centos).
  • Get PHP source from http://www.php.net/downloads.php
  • Untar and go into directory
tar -xvf php-5.2.13.tar ; cd php-5.2.13
  • You can just compile with
./configure '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--enable-gd-native-ttf' '--with-t1lib=/usr' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--with-kerberos' '--enable-ucd-snmp-hack' '--enable-shmop' '--enable-calendar' '--with-libxml-dir=/usr' '--enable-xml' '--with-system-tzdata' '--with-mime-magic=/usr/share/file/magic' '--with-apxs2=/usr/sbin/apxs' '--enable-soap' '--with-gd' '--enable-dom' '--disable-dba' '--without-unixODBC' '--enable-pdo' '--enable-xmlreader' '--enable-xmlwriter' '--without-sqlite' '--enable-json' '--without-pspell' '--enable-wddx' '--with-curl' '--enable-mbstring=all' '--disable-posix' '--disable-sysvmsg' '--disable-sysvshm' '--disable-sysvsem' '--with-mcrypt' '--with-mhash' '--with-mysql' '--enable-mysqli' '--with-mysqli=/usr/bin/mysql_config' '--with-pdo-mysql=/usr/bin/mysql_config' '--with-xsl' '--with-zip'
  • However you will find that lot of libraries are not there :) . Anyway, these are the list of items you require
yum install -y mod_ssl.x86_64
yum install -y openssl-devel.x86_64
yum install -y crypto-utils.x86_64
yum install -y gcc
yum install -y make
yum install -y perl
yum install -y httpd-devel
yum install -y libxml2-devel.x86_64
yum install -y bzip2-devel.x86_64
yum install -y gmp-devel.x86_64
yum install -y mysql-devel
yum install  -ycurl-devel
yum install -y mcrypt
yum install -y zip
yum install -y gd-devel.x86_64
yum install -y t1lib-devel.x86_64
yum install -y libmcrypt-devel.x86_64
yum install -y libmhash-devel.x86_64
yum install -y libxslt-devel.x86_64
yum install -y libtool-ltdl-devel
  • You will have a problem with mhash for sure… Now you need to download, compile and install it seperately
wget "http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmhash%2Ffiles%2F&ts=1303806016&use_mirror=sunet"
bunzip2 mhash-0.9.9.9.tar.bz2
tar -xvf mhash-0.9.9.9.tar
cd mhash-0.9.9.9
./configure
make
make install
cd lib
make install
  • At this point, I had some difficulty in getting the mash library at the right place. Anyway, Looking at the install script of mhash, I realized that it’s going to /usr/lib/local when it needs to go to /usr/lib64 . Simple fix
cd /usr/local/lib
cp -R * /usr/lib64/
  • Final compile!!
./configure '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--enable-gd-native-ttf' '--with-t1lib=/usr' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--with-kerberos' '--enable-ucd-snmp-hack' '--enable-shmop' '--enable-calendar' '--with-libxml-dir=/usr' '--enable-xml' '--with-system-tzdata' '--with-mime-magic=/usr/share/file/magic' '--with-apxs2=/usr/sbin/apxs' '--enable-soap' '--with-gd' '--enable-dom' '--disable-dba' '--without-unixODBC' '--enable-pdo' '--enable-xmlreader' '--enable-xmlwriter' '--without-sqlite' '--enable-json' '--without-pspell' '--enable-wddx' '--with-curl' '--enable-mbstring=all' '--disable-posix' '--disable-sysvmsg' '--disable-sysvshm' '--disable-sysvsem' '--with-mcrypt' '--with-mhash' '--with-mysql' '--enable-mysqli' '--with-mysqli=/usr/bin/mysql_config' '--with-pdo-mysql=/usr/bin/mysql_config' '--with-xsl' '--with-zip'
make clean
make
make install
service httpd restart
From http://cleverappz.com/site/2011/05/compiling-php-5-2-x-on-redhat-linux/


Tuesday, February 07, 2012

Understanding the Life of a Session


Understanding the Life of a Session

One of the most vast misconceptions in the PHP world is how sessions really do work. I'm fairly confident most of us know how to start sessions, terminate sessions, regenerate session IDs and easily pass data from 1 page to another. But do you really know how they work inside out?

A common question I get asked is:

Quote:
Should you really be storing the users' flags in a session? That's dangerous!
Why is it dangerous? Where else would you store it? Storing crucial information in a session is what sessions are all about. They strive on being able to provide the information without leaving it open to tampering or interception. Giving a session some important information is like giving a Jack Russell a bone - it's not letting go of it anytime soon and if any other sod tries to take the bone, well, I'll leave that up to your gruesome imagination.

Where are Sessions stored?

This is the question that will quash all those questions on are sessions safe. Anything can be unsafe in PHP if the programming is not up to par. Generally speaking, however, sessions are 1 section of the PHP language that is difficult to mess up.

Sessions are conveniently stored server-side. There is no exception to this rule. Many people become confused because sessions use cookies, and cookies, rightfully so, are stored client-side. Sessions, however, are not cookies in the truest form. Cookies are just 1 of the methods of delivering the unique session ID to retrieve the session data.

Core of a Session File

Remember the times when you used to crack open the cereal boxes to retrieve the free plastic toy from inside? Well, sessions are not quite as exciting as that. In fact, sessions are pretty uneventful on the inside. Let's take the following PHP code and execute it normally:

PHP Code:
session_start();
$_SESSION['myWebsite'] = 'http://www.talkphp.com/';$_SESSION['mySessionId'] = session_id();  
As simple as it may be, it does the job. It creates us a session file which contains our session information. The file is stored as the following filename which is made up of 2 parts: it has prefixed us the word sess_ which is then followed by our unique session ID.

Quote:
sess_4ca08c0050893704ec5cf01e8eeef3b9
If I open up this file it contains the following information which is simply my array which has been serialized (see serialize for further information) and stored.

Quote:
myWebsite|s:23:"http://www.talkphp.com/";mySessionId
|s:32:"4ca08c0050893704ec5cf01e8eeef3b9";
This is nothing more than my array broken down into a string. When I execute my file again the session ID will be used to retrieve the above data from the relevant session file and unserialized (see unserialize for further information).

That's all there really is to a session file. Remember that the session file is stored server-side so there is absolutely no need to transfer any information you do not publish in the HTML over the insecure medium we like to call the Internet.

Point of the Session ID

Now, this is where our delivery method comes in. Cookies are the most frequently used due to the extra security they add (not much, but just enough to be the favoured method). The other possible methods are GET (which is sometimes used) and POST (which I've never seen used but essentially it could be used with a little difficulty). Unlike GET and POST which can be used in such attacks as CSRF (see our article on CSRF for further information) and passed around to potential victims with far too much ease, cookies are just that little more difficult to install on a victim's computer.

I could quite easily send a link to you and have you either hijack a session on my behalf, or the most common reason would be to fixate your session ID (see the following post for more information about session fixation). Although, if I am unable to pass you the session ID easily then it's going to be more difficult for me to breach the target website's security and assume your identity. Consider the following link and how easily it can be passed around:

You would more than likely click on that link believing it to be perfectly innocuous. The trouble is that I have just set your session ID to 4f4ae - and without any regenerating of the ID you would carry that ID around for the lifetime of your session giving me ample amount of time to assume your role and have almost limitless access to your account.

So where has this all been leading? Well, there is an important setting that is set to 1 by default (and should always be set to 1 unless you wish to also support users who do not have cookies enabled - which is not recommended) that prevents the session IDs from being transferred via a GET and to use only cookies - the safest delivery method. That setting is:

Quote:
session.use_only_cookies
From my earlier example let's take a look at the cookie that has been created on my computer:

Quote:
4ca08c0050893704ec5cf01e8eeef3b9
The cookie's name is PHPSESSID which is the default name given to a session cookie that is stored client-side. The number you see above is my session ID which will stay with me for the lifetime of my session or until it is regenerated for security purposes.

Upon loading the website where my session ID was created, the cookie is sent to the website and parsed by PHP. The session ID is correctly linked to the session file stored server-side and is then unserialized and placed into the pre-populated PHP array,$_SESSION. Once I have issued the session_start() command to begin my session I am able to access all the information - even crucial and highly sensitive information, from the$_SESSION array.

Note: Although you may store highly insensitive information in sessions, be aware that displaying it on your website will mean that crucial information is being sent over the pipes of the Internet. Without using SSL this would be a very insecure method of delivery which would be crying out to be intercepted by Harry the hacker.

Basic Session Security Example

Assume that our banking system has absolutely no extra security implemented. This is exceedingly unlikely so don't be frightened!. You have logged in and been issued a cookie containing your session ID, our banking system also erroneously accepts the GET method of delivery though to ensure everybody can use their system. Remember, all Harry the hacker (not Harry the hamster) needs to assume your role is your session ID. Nothing more, in this example.

Harry decides to go down the route of session fixation and thus sends you the following crafty link:

You click on it and login. Harry, already knowing your session ID is
5aff2, clicks on the link himself and is able to withdraw your entire life savings of 67 pence.

As sessions do absolutely no extra security checks on your behalf, the system simply thinks it is you because the session ID is correct. It is up to YOU to implement the extra security to prevent such instances from occurring.

Locks up your Daughters

Some mean looking guy approaches your door after trampling on all your pretty flowers without a care in the world, loudly knocks on the door and demands to see your daughter. He may well know your daughter's name but who is he? The who is he part is crucial - it's as crucial as not assuming that because someone has the correct session ID that you should let them in. Until the mean looking guy can answer some of your questions, you're not letting him see your daughter. Oh no, no!

To give a simple question of what you can ask the end user when it comes to sessions is:

Quote:
Are you using the same browser as you were previously?
This can stop a fair amount of session security breaches but should not be the only method as mentioned later on. If I login to my online banking account using Mozilla Firefox 2.0.0.6, and Harry, after clicking on the exact same link I did, is using Internet Explorer 7.0 or even Mozilla Firefox 2.0.0.5, then you can safely assume 1 of either 2 things have occurred:
  • You have switched your browser half way through and would like to continue your session seamlessly.
  • Somebody is attempting to hijack your session using your session ID that they have acquired from somewhere

Which point, 1 or 2, would you rather assume was true? Surely assuming the former and allowing a user to carry on their session seamlessly would be less likely than the latter? Assuming the latter will also save any embarrassing security hiccups. By destroying the user's session and asking them to re-login has to be the better option just in case. Any legitimate user will not mind this slight inconvenience if they know their 67 pence is safe. After all, once the security has been breached, it is difficult to return that level of trust back to its normal levels.

Nonetheless, the browser name, version, language, etc. are all sent by the end user's browser during the HTTP call. This means that this data may not even be sent at all. It is optional. As long as you know it is optional you can check for if the intended array even exists before using it. This is important because a blank string will always produce the same MD5 or SHA1 (etcetera...) hash. A null string MD5'd will always bed41d8cd98f00b204e9800998ecf8427e no matter how many times you use MD5. Whilst SHA1 will be different to the MD5 string, it will still be the same for every single time you hash the null string using SHA1.

Shared Hosts can be Little Buggers

Now that you fully understand the following:

[Session File] (Server) -> [Cookie File] (Client)

You may think your session file, because it is server side, is safe. That simply isn't the case, I'm afraid. A session file can simply be opened by anybody who has access to your file system. For instance, my sessions are stored in the following directory as plain files:

Quote:
C:\wamp\www
Anybody who has access to that directory can read my session files and read the session IDs from the filenames. This is why shared hosts can be bad. Typically everybody on a shared hosts shares the same temp folder. This is the most common place where session files are stored. As you can see it is a potential downfall to what I've been teaching you in this article (fear not, you haven't just wasted 10 minutes reading a pointless article). Sessions, although server side, are not safe from prying eyes.

Luckily the directory can be moved elsewhere or you can even use a table inside a database to store all your session data. The latter is best saved for another article to keep this 1 short and sweet (ok, shortish and sweetish). The following setting can be changed to alter the destination of our session files:

Quote:
session.save_path
Remember though that when your website becomes very large and important, storing crucial information. Nothing beats a dedicated host. That means ONLY the people YOUwant on your server to have access to your session files can see them, read them and even abuse them.

The next time somebody asks you if sessions are really the way to go, that if storing crucial information in session data is insecure, chuckle in their face and refer them to TalkPHP!

http://www.talkphp.com/general/1077-understanding-life-session.html

Monday, February 06, 2012

Syslog


Syslog trên OS LinuxPDFPrintE-mail
Với người quản trị hệ thống việc theo dõi log hệ thống luôn là một trong những việc quan trọng nhất. Sau đây tôi xin giới thiệu Syslog, một công cụ ghi log khá phổ biến trên các OS Linux.
syslog là một công cụ nhận biết và ghi lại tất cả các loại system message, từ loại thông thường cho đến quan trọng. Syslog quản lý các system message dựa trên hai nhãn của system message.
Nhãn thứ nhất thể hiện nguồn tạo message
Nhãn thứ hai thể hiện mức độ quan trọng của message, gồm tám giá trị như sau
Security Keyword
0 emergencies
1 alerts
2 critical
3 errors
4 warnings
5 notifications
6 informational
7 debugging

/etc/syslog.conf
là file cấu hình quy định hoạt động ghi log system message của syslog, file này mặc định gồm hai cột.
cột thứ nhất thể hiện syslog sẽ nhận các message từ đâu và mức độ quan trọng của các message, cột thứ hai thể hiện file chứa các message đó.
vi dụ cấu hình sau

*.info;mail.none;authpriv.none;cron.none /var/log/messages

thể hiện file /var/log/message sẽ chứa tất các system message có mức độ từ info trở lên ngoại trừ các message của mail, authpriv, cron.

*.debug /var/log/debug

thể hiện mọi thông tin debug của hệ thống sẽ được ghi vào file /var/log/debug

*.emerg *

thể hiện mọi system message loại emerge sẽ được ghi trực tiếp ra màn hình.

Bên cạnh đó có nhiều dịch vụ hệ thống sẽ ghi log message của nó vào file riêng biệt mà không phụ thuộc vào cấu hình syslog như.
dịch vụ mail sẽ ghi log vào file /var/log/maillog
dịch vụ apache sẽ ghi log vào các file trong thư mục /var/log/httpd/
...
Khi đó muốn xem log của dịch vụ nào bạn phải mở các file log tương ứng của dịch vụ đó

Xem log hệ thống
Thông thường khối lượng system message được ghi lại là rất lớn nhất là đối với những hệ thống có mật độ xử lý lớn. Sau đây là một số lệnh thường dùng để xem những file log có số lượng message lớn.

tail -f /var/log/messages
các log message mới xuất hiện sẽ được đưa ra màn hình

grep string /var/log/messages | more
chỉ hiện các message có chứa ký tự string theo từng trang màn hình.

more /var/log/messages
lần lượt xem log message trên từng trang màn hdduwowcjR
Remote syslog
Với syslog bạn có thể thực hiện việc ghi log của nhiều hệ thống ở nhiều địa điểm cách xa nhau vào một server log duy nhất. Đây cũng là một điểm giúp tăng khả năng bảo mật hệ thống mà bạn cần lưu ý.
cũng như mọi dịch vụ remote khác phần cấu hình sẽ gồm hai phần, phần phía Server (serverlog) và phần phía Client (clientlog).
Cấu hình phía Serverlog
Như đã nói syslog đọc file cấu hình /etc/syslog.conf để xác định loại system message mà nó sẽ nhận và vị trí file mà nó sẽ ghi log vào. Tuy nhiên bên cạnh đó nó còn đọc file /etc/sysconfig/syslog để xác định mốt hoạt động.
Mặc định syslog không nhận system message từ các hệ thống ở xa tuy nhiên điều này sẽ thay đổi nếu biến SYSLOGD_OPTIONS trong file /etc/sysconfig/syslog được đặt thêm giá trị -r như sau.

# Options to syslogd
# -m 0 disables 'MARK' messages.
# -r enables logging from remote machines
# -x disables DNS lookups on messages received with -r
# See syslogd(8) for more details

SYSLOGD_OPTIONS="-m 0 -r"

# Options to klogd
# -2 prints all kernel oops messages twice; once for klogd to decode, and
# once for processing with 'ksymoops'
# -x disables all klogd processing of oops messages entirely
# See klogd(8) for more details

KLOGD_OPTIONS="-2"

sau khi sau khi sửa file /etc/sysconfig/syslog bạn restart dịch vụ syslog để những thay đổi có hiệu lực.
bạn dùng lệnh netstat để đảm bảo syslog trên hệ thống đã sẵn sàng nhận message từ các hệ thống ở xa.
netstat -a | grep syslog
udp 0 0 *:syslog *:*

hoặc
netstat -an | grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:*

lúc này syslog đã sẵn sàng nhận system message UDP tại port 514.

Cấu hình phía Clientlog
khai báo địa chỉ ip và tên host đầy đủ (FQDN) của Serverlog tại file /etc/hosts
192.168.1.100 syslogserver.com syslogserver loghost

với loghost là nickname của logserver.
sửa file /etc/syslog.conf để syslog trên máy Clientlog gửi message đến máy loghost (Serverlog).
ví dụ

*.debug @loghost
*.debug /var/log/messages

thể hiện các system message từ mức debug trở lên sẽ cùng được gửi đến máy loghost và ghi vào file /var/log/message trên Clientlog.

Với những hệ thống lớn có số lượng file log lớn thì việc quản lý các file log này cũng là một vấn đề. Logrotate là một công cụ hữu hiệu hỗ trợ cho việc quản trị các file log. Với Logrotate các file log có thể được định kỳ sử dụng lại theo ngày tuần tháng hay theo kích thước file log, nén, xoá bỏ file log...
Hoạt động của logrotate mặc định được cấu hình tại file /etc/logrotate.conf thông qua những tham số tuỳ chọn, sau đây là một số tham số thường dùng.

compress : nén những file log đã sử dụng
nocompress: ngược lại với tuỳ chọn compress
create mode owner group: khi sử dụng một file log mới, file log mới được tạo sẽ có các thuộc tính (mode, owner group).
nocreate : không tạo file log mới.
mail address : khi hết chu kỳ sử dụng file log sẽ được gửi tới địa chỉ (address).
nomail : ngược lại với tuỳ chọn trên.
daily : chu kỳ sử dụng file log theo ngày
weekly : chu kỳ sử dụng file log theo tuần.
monthly : chu kỳ sử dụng file log theo tháng.
rotate count : xác định số lần luân phiên sử dụng file log.
size size : chu kỳ sử dụng file log được xác định theo kích thước.
include /etc/logrotate.d : đọc thêm các thông tin cấu hình tại các file trong thư mục /etc/logrotate. Các tham số khai báo ở các file này có độ ưu tiên cao hơn các tham số khai báo trong file /etc/logrotate.conf.

ví dụ một file /etc/logrotate có thể như sau

weekly
rotate 4
errors root
create
compress
include /etc/logrotate.d

/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}

/var/log/lastlog {
monthly
rotate 1
}

phần mắc định xác định mỗi file log được ghi trong một tuần, chỉ lưu lại file log của bốn tuần, các logrotate error message được gửi tới root, các file log lưu trữ được nén lại.
cho phép logrotate đọc thêm các thông tin cấu hình nằm tại các file trong thư mục /etc/logrotate.d
file log /var/log/wtmp được ghi log trong một tháng, khi tạo file log mới file này có thuộc tính là 0664, woner là root, group là utmp, chỉ lưu thông tin log trong một tháng.
file log /var/log/lastlog được ghi log trong một tháng, chỉ lưu thông tin log trong một tháng.

Theo Hải Thắng của Vnexperts Research Department