在使用无惧上传V2.0的过程中,我发现它并不能识别是真图片,还是假图片(比如用一个ASP文件,改名为jpg,就能轻松上传成功),这可是一个很大的漏洞啊。 经过仔细研究,我找到了一个解决方法:先将文件上传,然后利用FSO,采用读取文本文件的方法,读出该文件的内容,如果含有事先定义的非法字符如cookie、session等,就可判断此文件为非图片文件,并自动删除此文件,从而达到禁止上传非法文件的目的。
源代码如下,加在upfile.asp中的case else中——
Dim objFSO,objTS,strText,ComStr,i filebb=updir&upfile.file("img").filename '文件路径 Set objFSO=Server.CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(Server.MapPath(filebb)) Then Set objTS=objFSO.OpenTextFile(Server.MapPath(filebb),1) '以文本文件方式读取文件 strText=lcase(objTS.ReadAll) '全文读取,并转换为小写 objTS.Close ComStr="cookie|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory" ComStr=ComStr&"|.saveas|wscript.shell|script.encode|folderfath|session" '禁止字符 strArray=split(ComStr,"|") for i=0 to ubound(strArray) if instr(strText,strArray(i))<>0 then objFSO.DeleteFile Server.MapPath(filebb),True '删除文件 response.write"非法文件,禁止上传 [<a href='upload.asp'>重新上传</a>]" response.end end if next Set objFSO=nothing else response.write"该文件不存在" end if 呵呵,使用此方法,果然解决了大问题。但要注意的是上传的文件不能过大,如果过大的话,可想而知,对系统资源的浪费是惊人的,建议设置上传文件大小为300K左右。 |