首先判断文件大小:
if file.filesize<10 then response.write("<script>alert('您没有选择上传文件')</script>") response.write("<script>history.go(-1)</script>") response.end() end if
将文件上传到服务器后,判断用户文件中的危险操作字符:
set myfile = server.createobject("scripting.filesystemobject") set mytext = myfile.opentextfile(filepath, 1) '读取文本文件 stextall = lcase(mytext.readall) mytext.close set myfile = nothing sstr=".getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas |wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=" snostring = split(sstr,"|") for i=0 to ubound(snostring) if instr(stextall,snostring(i)) then set filedel = server.createobject("scripting.filesystemobject") filedel.deletefile filepath set filedel = nothing response.write("<script>alert('您上传的文件有问题,上传失败');window.close();</script>") response.end() end if next
如何防止木马性图片上传
这个代码我检验过没有问题,可以阻挡木马性图片的上传
<% '*************************************************************** '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) response.write checkfiletype(server.mappath("2.gif")) 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 %>
以上就是asp防止上传图片木马原理解析,希望对大家的学习有所帮助。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论