终极版防范图片木马上传代码:
'*************************************************************** 'CheckFileneirong 函数用来读取文件内容检查文件是否有非法字符 '*************************************************************** function CheckFileneirong(filename) CheckFileneirong=true '设置返回值,没有非法字符为真,有非法字符下边会把值改成假 set MyFile = server.CreateObject ("Scripting.FileSystemObject")'建立fso MyText=""'把存储文件内容的变量清空,待存放文件内容 set MyText = MyFile.OpenTextFile (filename, 1) ' 读取文本文件 sTextAll = lcase(MyText.ReadAll): MyText.close '判断用户文件中的危险操作 sStr =".getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory" sStr = sStr & "|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=" sStr = sStr & "|重命名|修改|属性|文件浏览器|新建|复制|成功|参数错误|服务器|空间|下载" '*************************************************************** '以上是非法字符关键字,不知道为什么要分成三行,在dw里试了试,在一行里换行会有影响,程序不成功,不知道是不是这个原因。换成三行就可以。 '*************************************************************** sNoString = split(sStr,"|") for i = 0 to ubound(sNoString) if instr(sTextAll, sNoString(i)) <> 0 then CheckFileneirong=false'如果有非法字符,把返回值改为假 Response.write "<script>alert('文件中含有非法字符,"&sNoString(i) &",为了安全原因,不能上传。')</script>" exit for end if next
set MyText=nothing
end function
'*************************************************************** 'CheckFileType 函数用来检查文件是否为图片文件 '参数filename是本地文件的路径 '如果是文件jpeg,gif,bmp,png图片中的一种,函数返回true,否则返回false '*************************************************************** 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)
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 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 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 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 next end select fstream.Close set fseteam=nothing if err.number<>0 then CheckFileType=false end function
'检测文件是否非法 if CheckFileType(Server.mappath(UpFilePath&fname))=false or CheckFileneirong(Server.mappath(UpFilePath&fname))=false then '重写非法文件 Set objFSO = Server.CreateObject("scripting.FileSystemObject") Set objCountFile=objFSO.CreateTextFile(Server.mappath(UpFilePath&fname),True)
objCountFile.Write "非法文件"'因为是上传到服务器才判断这个文件,所以是非法文件也已经上传到服务器了。我这里不知道为什么不能fso删除文件所以只能重写这个文件了。 'if (objFSO.fileexists(Server.mappath(UpFilePath&fname))) then objFSO.deletefile(Server.mappath(UpFilePath&fname)) end if'如果支持可以换成这行删除文件。 objCountFile.Close set objCountFile=nothing '提出警告 Response.Write "<script>alert('你的ip和时间已被纪录,由于你曾多次使用该方法对系统进行非法攻击,我们将会把你的数据向山东省公安厅及山东网警报告');window.close();</script>" '记录非法入侵时间 set MyFiletemp=server.CreateObject("Scripting.FileSystemObject") ON Error Resume Next if err.number =0 then IF (MyFiletemp.FileExists(server.mappath("/非法上传记录.txt")))=false Then
set wfile=myfiletemp.opentextfile(server.mappath("/非法上传记录.txt"),8, True) wfile.writeline " 日期 时间 IP 路径" wfile.writeline date()&" "&time()&" "&request.servervariables("remote_addr")&" "&request.ServerVariables("HTTP_HOST")&request.ServerVariables("SCRIPT_NAME")&"?"&request.ServerVariables("QUERY_STRING")
ELSE set wfile=myfiletemp.opentextfile(server.mappath("/非法上传记录.txt"),8) wfile.writeline date()&" "&time()&" "&request.servervariables("remote_addr")&" "&request.ServerVariables("HTTP_HOST")&request.ServerVariables("SCRIPT_NAME")&"?"&request.ServerVariables("QUERY_STRING") END IF END IF
wfile.Close set wfile=nothing MyFiletemp.Close set MyFiletemp=nothing objFso.Close set objFso=nothing MyFile.Close set MyFile=nothing
Response.End() end if |