2个检测ASP图片木马的函数

[ 2009-09-26 08:26:46 | 作者: admin ]
字号: | |
如果是自己的服务器可以单独设置文件夹得运行权限(执行权限,无),即使上传木马也不会执行……

原文地址:http://blog.163.com/shui99/blog/static/6989944720091692456828/
'*******************************************************************************************************************************
' 函数说明:检测ASP图片木马的函数。由于FSO无法读取客户端文件的内容,所以只能在文件上传到服务器后再打开文件进行内容检查。
' 木马原理:入侵者使用诸如ASP图片木马生成器之类的工具将一张正常的图片与一个ASP木马文件合并成一个图片文件(即将对网站有害的
'ASP代码插在图片编码之后,虽然图片仍然可以正常显示,但是文件内容和尺寸已被改变),然后通过网站提供的文件上传功能上传这一张“合
'法的”图片,进而实现了上传ASP木马的目的。
' 防范方法:因为这种木马是图片与木马的二合一,所以需要在上传图片前检查文件内容,若文件内容不合法(即包含有恶意代码在里面),
'则禁止上传,从而堵住了木马攻击的源头,这是木马攻击的第一关,至关重要,必须堵住。
'*******************************************************************************************************************************

'Begin--------------------------------------------------------------------------------------------------------------------------
function CheckFileContent(FileName)
dim ClientFile,ClientText,ClientContent,DangerString,DSArray,AttackFlag,k
set ClientFile=Server.CreateObject("Scripting.FileSystemObject")
set ClientText=ClientFile.OpenTextFile(Server.MapPath(FileName),1)
ClientContent=LCase(ClientText.ReadAll)
set ClientText=nothing
set ClientFile=nothing
AttackFlag=false
DangerString=".getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=|include|filesystemobject|shell.application"
DSArray=split(DangerString,"|")

for k=0 to UBound(DSArray)

       if InStr(ClientContent,DSArray(k))>0 then '判断文件内容中是否包含有危险的操作字符,如有,则必须删除该文件。
         AttackFlag=true
         exit for
       end if

next

CheckFileContent=AttackFlag
end function
'End----------------------------------------------------------------------------------------------------------------------------

asp 检查图片木马函数
<%
const adTypeBinary=1
dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)
dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)
dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)
dim gif(5):gif(0)=CByte(&H47):gif(1)=CByte(&H49):gif(2)=CByte(&H46):gif(3)=CByte(&H39):gif(4)=CByte(&H38):gif(5)=CByte(&H61)

response.Write(CheckFileType(Server.MapPath("01.jpg")))

function CheckFileType(filename)
'on error resume next
CheckFileType=false
       dim fstream,fileExt,stamp,i
       fileExt=mid(filename,InStrRev(filename,".")+1)
       set fstream=Server.createobject("ADODB.Stream")
       fstream.Open
       fstream.Type=adTypeBinary
       fstream.LoadFromFile filename
       fstream.position=0
       select case fileExt
           case "jpg","jpeg"
                stamp=fstream.read(2)
                for i=0 to 1
                     if ascB(MidB(stamp,i+1,1))=jpg(i) then
                     CheckFileType=true
                     else
                     CheckFileType=false
                     end if
                next
           case "gif"
                stamp=fstream.read(6)
                for i=0 to 5
                     if ascB(MidB(stamp,i+1,1))=gif(i) then
                     CheckFileType=true
                     else
                     CheckFileType=false
                     end if
                next
           case "png"
                stamp=fstream.read(4)
                for i=0 to 3
                     if ascB(MidB(stamp,i+1,1))=png(i) then
                     CheckFileType=true
                     else
                     CheckFileType=false
                     end if
                next
         case "bmp"
                stamp=fstream.read(2)
                for i=0 to 1
                     if ascB(MidB(stamp,i+1,1))=bmp(i) then
                     CheckFileType=true
                     else
                     CheckFileType=false
                     end if
                next
       end select
       fstream.Close
       set fseteam=nothing
       if err.number<>0 then CheckFileType=false
end function
%>
[最后修改由 admin, 于 2009-09-26 08:30:28]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1286

这篇日志没有评论。

此日志不可发表评论。