Fckeditor(ASP版)防止木马上传——修改上传类
在上一篇博文《Fckeditor 2.6.3 (ASP版) 修改(二) ——检测上传文件是否为木马》防止木马的上传采用的是文件上传之后,将文件以文本方式打开,检查是否存在危险字符串,如果存在则删除该文件。同样这个方法也可以用在eWebEditor的防止上传木马中。近日,通过研究发现将上述过程转移到上传类中,效果更佳。
Fckeditor使用是NetRube Upload Class Version 2.3 Build 20070528的无组件上传类(原版的上传类我没看,估计Fckeditor做了一定修改)。该类在Fckeditor的位置为fckeditor\editor\filemanager\connectors\asp\class_upload.asp,本文的所有修改都是针对该文件进行的。
修改思路如下:
1、上传类的SaveAs方法使用了一个SniffHtml方法检测上传的文件是否为html文件。查看该方法的代码,发现该方法能够使用正则表达式检测文件中是否存在特定的字符串,进而判断上传的文件是否为html文件。如果替换检测的特定字符串值,不就实现对上传木马的检测了吗?
2、检查调用SniffHtml的位置,该类使用私有的ByteArray2Text方法将上传的文件转换为text,然后该text值传给SniffHtml。
3、使用和SniffHtml同样的方法,是不是可以完成对危险字符串的检测呢?
有了上述思路,作出具体的修改如下:
1、复制SniffHtml方法,将其改名为SniffSafety,并做相应的修改。SniffHtml和SniffSafety的具体代码如下,真的很像哦。
Private Function SniffHtml( sData )
Dim oRE
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
Dim aPatterns
aPatterns = Array( "<!DOCTYPE\W*X?HTML", "<(body|head|html|img|pre|script|table|title)", "type\s*=\s*[\'""]?\s*(?:\w*/)?(?:ecma|java)", "(?:href|src|data)\s*=\s*[\'""]?\s*(?:ecma|java)script:", "url\s*\(\s*[\'""]?\s*(?:ecma|java)script:" )
aPatterns = Array( "<!DOCTYPE\W*X?HTML", "<(body|head|html|img|pre|script|table|title)", "type\s*=\s*[\'""]?\s*(?:\w*/)?(?:ecma|java)", "(?:href|src|data)\s*=\s*[\'""]?\s*(?:ecma|java)script:", "url\s*\(\s*[\'""]?\s*(?:ecma|java)script:" )
Dim i
For i = 0 to UBound( aPatterns )
oRE.Pattern = aPatterns( i )
If oRE.Test( sData ) Then
SniffHtml = True
Exit Function
End If
Next
For i = 0 to UBound( aPatterns )
oRE.Pattern = aPatterns( i )
If oRE.Test( sData ) Then
SniffHtml = True
Exit Function
End If
Next
SniffHtml = False
End Function
'by togis togis@126.com 2009.05.22
Private Function SniffSafety( sData )
'by togis togis@126.com 2009.05.22
Private Function SniffSafety( sData )
Dim oRE
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
Dim aPatterns
'修改aPatterns的值,即可修改需要检测的字符串,^_^
aPatterns = Array(".(getfolder|createfolder|deletefolder|createdirectory|deletedirectory|saveas)","wscript.shell","script.encode","server.",".createobject","execute","activexobject","language=","request","server","script")
Dim i
For i = 0 to UBound( aPatterns )
oRE.Pattern = aPatterns( i )
If oRE.Test( sData ) Then
SniffSafety = False
Exit Function
End If
Next
SniffSafety = True
End Function
'修改aPatterns的值,即可修改需要检测的字符串,^_^
aPatterns = Array(".(getfolder|createfolder|deletefolder|createdirectory|deletedirectory|saveas)","wscript.shell","script.encode","server.",".createobject","execute","activexobject","language=","request","server","script")
Dim i
For i = 0 to UBound( aPatterns )
oRE.Pattern = aPatterns( i )
If oRE.Test( sData ) Then
SniffSafety = False
Exit Function
End If
Next
SniffSafety = True
End Function
2、修改SaveAs方法,增加对 SniffSafety的调用。修改后的SaveAs方法如下:
Public Sub SaveAs(sItem, sFileName)
If File(sItem).Size < 1 Then
nErr = 2
Exit Sub
End If
If File(sItem).Size < 1 Then
nErr = 2
Exit Sub
End If
If Not IsAllowed(File(sItem).Ext) Then
nErr = 4
Exit Sub
End If
nErr = 4
Exit Sub
End If
If InStr( LCase( sFileName ), "::$data" ) > 0 Then
nErr = 4
Exit Sub
End If
nErr = 4
Exit Sub
End If
Dim sFileExt, iFileSize
sFileExt = File(sItem).Ext
iFileSize = File(sItem).Size
sFileExt = File(sItem).Ext
iFileSize = File(sItem).Size
' Calculate the size of data to load (max 1Kb).
Dim iXSSSize
iXSSSize = iFileSize
Dim iXSSSize
iXSSSize = iFileSize
If iXSSSize > 1024 Then
iXSSSize = 1024
End If
' Read the data and convert it to text .
Dim sData
oSourceData.Position = File(sItem).Start
sData = oSourceData.Read( iXSSSize ) ' Byte Array
sData = ByteArray2Text( sData ) ' String
' Check XSS.
If Not IsHtmlExtension( sFileExt ) Then
iXSSSize = 1024
End If
' Read the data and convert it to text .
Dim sData
oSourceData.Position = File(sItem).Start
sData = oSourceData.Read( iXSSSize ) ' Byte Array
sData = ByteArray2Text( sData ) ' String
' Check XSS.
If Not IsHtmlExtension( sFileExt ) Then
' Sniff HTML data.
If SniffHtml( sData ) Then
nErr = 4
Exit Sub
End If
End If
'Sniff the data to check whether the data contains dangerous strings,by togis email:togis@126.com 2009.5.22
If not SniffSafety( sData ) Then
nErr = 4
Exit Sub
End If
Dim oFileStream
Set oFileStream = Server.CreateObject("ADODB.Stream")
With oFileStream
.Type = 1
.Mode = 3
.Open
oSourceData.Position = File(sItem).Start
oSourceData.CopyTo oFileStream, File(sItem).Size
.Position = 0
.SaveToFile sFileName, 2
.Close
End With
Set oFileStream = Nothing
End Sub
If SniffHtml( sData ) Then
nErr = 4
Exit Sub
End If
End If
'Sniff the data to check whether the data contains dangerous strings,by togis email:togis@126.com 2009.5.22
If not SniffSafety( sData ) Then
nErr = 4
Exit Sub
End If
Dim oFileStream
Set oFileStream = Server.CreateObject("ADODB.Stream")
With oFileStream
.Type = 1
.Mode = 3
.Open
oSourceData.Position = File(sItem).Start
oSourceData.CopyTo oFileStream, File(sItem).Size
.Position = 0
.SaveToFile sFileName, 2
.Close
End With
Set oFileStream = Nothing
End Sub
这样全部的修改就完成,保存文件,大功告成。个人觉得这样防止木马的上传更彻底,只要使用这个上传类就没办法绕过这个检测。上传一个文本文件,如果包含危险的字符串,是不是提示会提示错误的文件格式???
PS:Fckeditor2.6.4的class_upload.asp文件和2.6.3中的完全一样,我们的修改是通用的哦。
出处:http://blog.csdn.net/togis/archive/2009/05/26/4218586.aspx
- 没有相关文章
- 没有评论