Wishlist 0 ¥0.00

博客入侵解决方案

前面谈到主机被入侵,博客被注入恶意代码,开始还想到等客服那边行动帮忙恢复,折腾了一周多,结果最后还是自己动手,丰衣足食。因此该主机所在的博客等站点被浏览器拦截了一周多,今天恢复了。下面说下解决方案。

其实也就是用之前提到的思路,写个脚本删除所有php文件中的恶意代码。主机提供商没有备份没办法恢复被感染前的代码。

思路就是:那段恶意代码提取出来保存在一个文件里面,例如malCode.txt,从根目录递归找出所有php文件,如果该php文件被感染即含有malCode.txt中的内容,则删除这段代码,并保存。具体而言有delMalwareCode.php:

/*
 * Created on 2012-7-5 @author: tanglei|www.tanglei.name 
 */
global $malwareCodeFile, $malCodeSize,$malwareCode;
//$malwareCodeFile = "S:/MYSITE/malCode.txt"; //本地测试用
$malwareCodeFile = "/home/username/public_html/malCode.txt";//上传至主机 
$malwareCode=file_get_contents($malwareCodeFile);
$malCodeSize = filesize($malwareCodeFile);//7317;
//$dir = "S:/MYSITE";
$dir = "/home/username/public_html";//从'根'目录开始
tree($dir);
function tree($dir)
{
	$handle = @opendir($dir) or die("Cannot open " . $dir);
	//echo "Files in " . $dir . ":\n";
	while($file = readdir($handle))
	{
		if ($file == "." ||  $file == "..") continue;
		if (is_dir($dir.'/'.$file))
		{
			tree($dir.'/'.$file);//递归找出所有php文件
		}
		if (substr($file,-4) == '.php')
			delCode($dir.'/'.$file);//处理每个php文件
	}
}
//delCode('S:/MYSITE/wp-config-sample.php');//单个文件本机测试用
function delCode($filename)
{
	echo 'del code :'."\t".$filename."\n";//log下处理了哪些文件
	global $malCodeSize,$malwareCode;
	$fHandle = fopen($filename,'r+');
	$srccontent = fread($fHandle,$malCodeSize);
	if ($srccontent == $malwareCode)//读取malCode那么大小的内容与恶意代码比较
	{
		fseek($fHandle, $malCodeSize);
		$content = fread($fHandle,filesize($filename)-$malCodeSize);//中招了,正常代码应该是从恶意代码之后到文件结束
		fclose($fHandle);
		$fHandle = fopen($filename,'w');
		fwrite($fHandle,$content);
		fclose($fHandle);
	}else{
		fclose($fHandle);
	}

}

以上代码就完成了删除恶意代码的需求。然后SSH到主机执行一下即可:

php delMalwareCode.php >del.log

,悲催的是主机提供商居然说因为中招了所以把SSH都给关掉了。那就把写好的代码通过ftp上传至指定目录,让客服执行吧。

这一步完成之后,还没完,要想关闭chrome等的拦截,还得手动去重新提交网站,申请审核

以后得注意点吧,什么插件啊之类尽量更新下,防止被注入啊等等,还有密码之类的。

About Us

Since 1996, our company has been focusing on domain name registration, web hosting, server hosting, website construction, e-commerce and other Internet services, and constantly practicing the concept of "providing enterprise-level solutions and providing personalized service support". As a Dell Authorized Solution Provider, we also provide hardware product solutions associated with the company's services.
 

Contact Us

Address: No. 2, Jingwu Road, Zhengzhou City, Henan Province

Phone: 0086-371-63520088 

QQ:76257322

Website: 800188.com

E-mail: This email address is being protected from spambots. You need JavaScript enabled to view it.