Wednesday, September 21, 2011

utf encoding and json

If someone tell you that json doesnt work with utf8, they are wrong.
It does support utf8.
The main problem is the database side.
Ensure to set encoding of mysql connection charset to utf8 to have it working properly.

mysql_set_charset("utf8", $conn);//support for unicode

Tuesday, September 6, 2011

Banning ip from DDOS

After surfing around, ive found some very handy commands to handle blocking of ips

#to list all ip and # of connections
sudo netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -n
# list all ports
sudo netstat -plan|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -n

#to block a specific ip
sudo iptables -I INPUT 1 -s xx.xx.xx.xx -j DROP

#to block a class B ip
sudo iptables -I INPUT 1 -s xx.xx.0.0/16 -j DROP


#to block a class C ip
sudo iptables -I INPUT 1 -s xx.xx.xx.0/24 -j DROP


#to block a class A ip
sudo iptables -I INPUT 1 -s xx.0.0.0/16 -j DROP

Normally you might want to use Class B block if you want to block entire ip sets from a particular country...
*be very careful when blocking class B address, some ip range might be shared among different countries.

To know which country the ip resides on:

To list all the iptables rules for incoming traffic:
sudo iptables -L INPUT -n -v

A very good list of cidr ip for each particular country:

Another way of attack is tcp level flood, aka: SYN FLOOD
#check tcp on SYN_RECV
sudo netstat -n -p TCP tcp | grep SYN_RECV

Good article about hardening system:

#Very handy command to limit # of connections to a specific port from an ip
sudo iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -p tcp --syn --dport 110 -m connlimit --connlimit-above 10 -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -p tcp --syn --dport 25 -m connlimit --connlimit-above 10 -j REJECT --reject-with tcp-reset

#To remove a rule (example):
sudo iptables -D INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset


#To save it:
sudo service iptables save

Updated 2012 Feb 28:
Encountered another type of DOS attack using TIME_OUT ip status.
One way to solve this is to reduce the # of seconds the tcp will timeout to a incomplete disconnected tcp.
vi /etc/sysctl.conf

net.ipv4.tcp_fin_timeout = 35
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_intvl = 35
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1

sudo sysctl -p

Then restart the services effected, such as http and mysql.
To show all states of tcp in the system, run this:
netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c


Hope this help :)


Monday, August 29, 2011

flowplayer error 2036

Took me hours figuring this out,
and finally found a post on the same error.

First, makesure the control swf is located in the same root as flowplayer swf.
Example: flowplayer.controls-3.2.5.swf and flowplayer-3.2.7.swf in the same folder.

then, makesure your unique id.
Example: id="myflowplayer" ...

This shall works fine :)

Tuesday, August 23, 2011

Kloxo /script empty!

For some freaking vps problem, i was facing very slow processing with vps from ds.my as usual.
running /script/upcp seems to somehow run halfway and hang...

So i decided to stop it, and realize, the entire folder /script is empty!...

To fix it:
cd /usr/local/lxlabs/kloxo/httpdocs
sudo lphp.exe ../bin/update.php

And wait for it to complete.. duh...

Saturday, August 20, 2011

MODx Revolution and UTF8

If you ever need to insert in latin symbol or unicode into modx title or content...
Modify modx_site_content fields:
+ pagetitle, longtitle, description, introtext, content
to unicode_general_ci encoding.
(Note: This is a fulltext index, so you will have to execute 1 statement to alter all 5 fields)
And also modify template variable content:
ALTER TABLE  `modx_site_tmplvar_contentvalues` CHANGE  `value`  `value` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

Next, modify core/config/config.inc.php and change the line:
+ $database_connection_charset = 'utf8';
+ $database_dsn = 'mysql:host=...;dbname=...;charset=utf8';

And you may start testing it it works :)
In my setup, its somehow in njis charset... (im not sure why)

Tuesday, August 9, 2011

MODx Revolution Root resource alias / path?

I tried google around on this, and find it difficult to find the result.
I've found a solution to based my resource alias to the root of the site.
By going to system > settings
use_alias_path : no

This will change all your resource alias to be based from root.
Example:
Container1 > My page (resourcealias: mypage)
now become:
http://www.example.com/mypage

If you need the container path, just put the full folder path:
Example:
Resource alias: container1/mypage

Monday, August 1, 2011

convert pdf to image in linux

convert -density 150 file.pdf outputname.png

-density to set pixel / resolution, the bigger the number, the bigger the image size