Thursday 30 May 2013

Khác biệt giữa Encoding, Encrypting và Hashing


Chúng ta thường hay bị bối rối khi nói về Encoding và Encrypting. Vậy sự khác nhau của chúng là gì? (đọc tiếp thì sẽ biết)
Giống nhau:
 1. Cả 2 đều chuyển đổi dữ liệu sang một định dạng khác.
 2. Cả 2 đều dịch ngược lại được (resersible)
Khác nhau:

Encoding


Mục đích của encoding là chuyển đổi dữ liệu mà nó có thể được sử dụng hoàn toàn (properly and safely) bởi một hệ thống khác. Vai trò của nó không giữ các thông tin bí mật mà đảm bảo chắc chắn rằng dữ liệu có thể được sử dụng hoàn toàn.
Encoding chuyển đổi dữ liệu sang một format khác sử dụng các biểu đồ có sẵn được public trên mạng do đó nó có thể hoàn toàn dịch ngược được. Nó không cần sử dụng bất kì key nào để dịch decode, mà chỉ cần thuật toán encode nó.
Vd:
ASCII, Unicode, URL Encoding, Base64

Encrypting



Mục đích của encryption là chuyển đổi dữ liệu để giữ bí mật, đảm bảo dữ liệu không thể sử dụng bởi bất kì ai ngoài đúng người nhận.
Vd:  AES, Blowfish, RSA

Hashing

Hashing is often confused with encoding and encryption but it's quite different than either. In security, the purpose of hashing is to take arbitrary input and produce a fixed-length string that has the following attributes:

1. The same input will always produce the same output.
2. Multiple disparate inputs should not produce the same output.
3. It should not be possible to go from the output to the input.
4. Any modification of a given input should result in drastic change to the hash.
Hashing is used in conjunction with authentication to produce strong evidence that a given message has not been modified. This is accomplished by taking a given input, hashing it, and then encrypting the sent hash with the recipient's public key.
When the recipient opens the message with their private key they then hash the message themselves and compare it to the hash that was given encrypted by the sender. If they match it is an unmodified message.
Examples: SHA-3, MD5 (Now obsolete), etc.

Summary

Encoding is for maintaining data usability and can be reversed by employing the same algorithm that encoded the content, i.e. no key is used.
Encryption is for maintaining data confidentiality and requires the use of a key (kept secret) in order to return to plaintext.

Hashing is for validating the integrity of content by detecting all modification thereof via obvious changes to the hash output.

Build memcached và php5-memcache with LAMPP 32bit on RetHat 64bit

Memcached vs Memcache? What’s different?


Setup Memcached

Để cài được Memcached server (http://memcached.org), trước tiên ta cần phải cài thư viện libevent (http://libevent.org)
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar xvfz libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure && make && make install

wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar xvfz memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure  --enable-64bit --with-libevent=/usr/local/include/event/
make && make install
Trong một vài trường hợp sẽ xảy ra lỗi không tìm thấy libevent library, cách fix:
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
Start service
memcached -d -u nobody -m 256 127.0.0.1 -p 11211
Lampp là bộ cài đặt được tích hợp sẵn httpd, php, openssl, phpmyadmin,… nhưng điểm yếu của lampp là chỉ hỗ trợ 32bit.
Vì vậy vấn đề xảy ra khi cài đặt extension memcache cho php trong bộ Lampp trên OS x86_64 chính là architecture của php (32bit) và memcache (64bit). Do đó chúng ta cần phải build module này từ source.
Việc compile này đã làm mình mất 2 ngày để debug và tìm ra nguyên nhân. Cuối cùng rút ra được kinh nghiệm như sau:
Xác định version module extension API và php API.
/opt/lampp/bin/phpize –v
Output sẽ giống tương tự như thế này:
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
Việc xác định version như trên sẽ giúp ta debug được lỗi sau khi compile memcache ở bước tiếp theo.
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xvfz memcache-2.2.7.tgz
cd memcache-2.2.7
/opt/lampp/bin/phpize
Để có thể compile được 32bit trên 64bit chúng ta cần phải có thư viện 32bit
yum install  glibc-devel.i686
Tiếp tục cài đặt với flag chỉ định là 32bit.
CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure --enable-memcache --with-php-config=/opt/lampp/bin/php-config
make && make install
Như vậy là build đã thành công. Bây giờ ta chỉ cần load module này vào php là xong.
vi /opt/lamp/etc/php.ini
Thêm vào trong 2 dòng sau trong phần Dynamic Extensions
extension_dir="/opt/lampp/lib/php/extensions/no-debug-non-zts-20100525"
extension=memcache.so
Restart lại httpd.
/opt/lampp/bin/httpd –k restart

Check lại trang phpinfo sẽ xuất hiện như sau:

Monday 20 May 2013

Have IPv4,IPv6, why not IPv {1,2,3,5} ?


Chúng ta đã quen với IPv4 và IPv6, vậy còn v{1,2,3,5}? Số 4 trong IPv4 là viết tắt của version của giao thức TCP/IP được published vào 9/1981. Bản thiết kế nguyên thủy của TCP/IP được ghép bởi 2 giao thức, và đó là bản thảo của v4 mà được chia thành 2 bản RFC khác nhau (RFC 791 - Internet Protocol, RFC 793 - Transmission Control Protocol). Do đó, v4 là sự kế thừa của mối quan hệ với TCP  - không liên quan gì đến những phần phía truoc, những giao thức độc lập IPv1, IPv2 hay IPv3
Khi công việc nghiên cứu trên "Internet Protocol next generation" (IPng - 1994), một version cần được ra đời, nhưng v5 đã được gán cho một giao thức đã có trước đó: Internet Stream Protocol (ST). Ngay khi được tạo ra, ST không bao giờ được thực hiện, đó là lí do ít người nghe đến nó. Do đó số 6 xuất hiện trong IPv6

Original:

We are all familiar with IPv4 and IPv6, but what happened to IPv{1,2,3,5}? The 4 in IPv4 stands for the version 4 of the TCP/IP protocol, which was published in September 1981. The original TCP/IP proposal coupled the two protocols, and it was the v4 draft that officially split the two into separate RFC’s. Hence, the v4 in IPv4 is a heritage of its relationship to TCP - there were no prior, standalone IPv1, IPv2, or IPv3 protocols.
When the working group began work on "Internet Protocol next generation" (IPng) in 1994, a new version number was needed, but v5 was already assigned to another experimental protocol: Internet Stream Protocol (ST). As it turns out, ST never took off, which is why few ever heard of it. Hence the 6 in IPv6.

Sunday 19 May 2013

LogRotate - a migical tool

1. Intro:
Gần đây cứ khoản 0h là tụi SDK monitor cứ gọi điện thoại thông báo service down. WTF. Cứ như zậy mấy ngày liên tiếp làm an không ngon ngủ không yên gì hết. Check lại thì do cái logrote config của một sư huynh khác bị lỗi. Lò mò loay hoay cặm cụi đọc đọc check hoài nên hôm nay ghi lại cho nhớ. Link source: http://www.rackspace.com/knowledge_center/article/understanding-logrotate-part-1

2. Logrotate la gì?
Cái tên nói lên tất cả rồi, logrotate tất nhiên là dùng để rotate log, sử dụng nhằm cho file log không có phình quá to (đơn giản là zậy). Ah, giải thích thếm, rotate có nghĩa là tạo ra một file log mới, xóa file log cũ đi hoặc nén nó lại đem đi cất đâu đó.

3. How it works?
System thực hiện rotate log một cách định kì, tất nhiên việc định kì đó là do sysadmin quyết định, đó có thể là hàng giờ, ngày, tuần, tháng... Khi logrotate thực hiện nó sẽ đọc file main config của nó /etc/logrotate.conf

File config này có samples cơ bản mà ta có thể chỉnh sửa một chút lại và xài luôn. Đây là logrotate.conf trên server CentOS nhá:
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
minsize 1M
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}
# system-specific logs may be also be configured here.

Các option sẽ được giải thích rõ hơn phía sau. Chú ý dòng include /etc/logrotate.d => Đây là folder sẽ chứa file config cho từng application xác định. Ok. Bây giờ ta sẽ đi chi tiết hơn.

Log files: là các file sẽ được logrotate thực hiện trên chúng. Các option, command nằm trong dấu { } đi theo sau các log file. Chúng ta cũng có thể sử dụng wildcard cho chúng. vd: /var/foo/*.log logrotate sẽ tìm tất cả các file có đuôi là .log trong thư mục /var/foo để thực thiện rotate log.
/var/foo/*.log /var/bar/log.txt {
        ...
        ...
}

Rotate count: xác định bao nhiêu file log được rotate trước đó được giữ lại trước khi thực hiện việc rotate mới, xóa cái cũ đi.
rotate 4 => chỉ có 4 file log được logrotate giữ lại, cái cũ nhất sẽ được xóa đi.

Rotation interval: xác định mức độ thường xuyên của việc thực hiện logrotate.
daily
weekly
monthly
yearly
Nếu rotation interval không được khai báo thì file log sẽ được rotate mỗi khi logrotate chạy.

Size: chỉ rõ kích thước mà logrotate sẽ thực hiện việc kiểm tra xác định có thực hiện hay không việc rotate file đó.
size 100M

File logs sẽ được rotate nếu size của file đó lớn hơn 100M. Nếu cả size và rotate interval được set, thì size sẽ có ưu tiên cao hơn.

Compression: logfile sẽ được nén lại theo format gzip (đây là dạng default, nếu muốn tìm hiểu thêm thì "man logrotate")
compress => compress file
nocompress => not compress
delaycompress => sẽ compress file nhưng sẽ không thực hiện ngay bây giờ, mà sẽ được thực hiện vào lần chạy logrotate tiếp theo
(delaycompress là một option của compress, nó phải được đi chung với câu lệnh compress)


Postprotate: postprotate script sẽ được thực thi sau mỗi lần nó rotate một file log được chỉ định trong file config. Chúng ta thường restart lại ứng dụng sau khi thực hiện rotate để ứng dụng có thể chuyển qua file log mới.
postrotate
    /usr/sbin/apachectl restart > /dev/null
endscript


Preprotate: preprotate script sẽ được thực thi trước khi nó thực hiện rotate log. Cả postprotate và prerotate đều phải đi chung vơi endscript.

Sharedscripts: Bình thường logrotate sẽ chạy postprotate scripts mỗi một file log được rotate. vd: trong trường hợp sử dụng /var/foo/*.log như ở trên, có bao nhiêu file .log thì sẽ có bao nhiêu làn script được thực thi. Để tránh trường hợp đó, ta có thể sử dụng command "sharedscripts"



4. Testing and debuging

-v : verbose, hiện ra logrotate đang thực hiện những gì, rất giúp ích khi tìm ra tại sao logrotate khong rotate file log nào đó như mình muốn.
-d: debug, hiện ra những những bước việc thực hiện rotate log nhung không thực sự rotate nó.  
-f: force, bắt buộc logrotate thực hiện rotate tất cả các log, dù có cần thực hiện rotate lúc đó hay không.

Mỗi khi thực hiện rotate xong, logrotate sẽ ghi nhớ các file log lần cuối được nó rotate vào một file /var/lib/logrotate.status