当前位置:首页 > ASP教程 > 应用技巧

解决ASP(图像)上传漏洞的方法

经常听说的asp上传漏洞,即是将一些木马文件修改后缀名(修改为图像文件后缀),进行上传。

针对此情况使用下列函数进行辨别:



<%
'******************************************************************
'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
%>



那么在应用的时候
checkfiletype(server.mappath("cnbruce.jpg"))
或者
checkfiletype("f:/web/164/images/cnbruce.jpg"))

反正即是检测验证本地物理地址的图像文件类型,返回 true 或 false值

所以这个情况应用在图像上传中,目前的办法是先允许该“伪图像”文件的上传,接着使用以上的自定义函数判断该文件是否符合图像的规范,若是木马伪装的图像文件则fso删除之,比如:


file.saveas server.mappath(filename) '保存文件
if not checkfiletype(server.mappath(filename)) then
    response.write "错误的图像格式"
    set fso = createobject("scripting.filesystemobject")
    set ficn = fso.getfile(server.mappath(filename))
    ficn.delete
    set ficn=nothing
    set fso=nothing
    response.end
end if


则是先将文件上传,接着立马使用自定义函数判断文件图像类型的吻合性,fso做出删除该文件的操作。


asp上传漏洞还利用""对filepath进行手脚操作
http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=635

针对这样的情况可使用如下函数


function truestr(filetrue)
str_len=len(filetrue)
pos=instr(filetrue,chr(0))
if pos=0 or pos=str_len then
truestr=true
else
truestr=false
end if
end function


接着就可判断后再做文件的上传


if truestr(filename)=false then
    response.write "非法文件"
    response.end
end if

file.saveas server.mappath(filename)



所以,在blog中的一文:(asp)文件系统之化境无组件(v2.0)上传

关于upfile.asp的全新内容如下:
处理 ssi 文件时出错
文件上传 filesizemax then response.write "文件大小超过了 "&filesizemax&"字节 限制 [ 重新上传 ]" end if '********************************检测文件类型**************************************************** fileext=ucase(right(file.filename,4)) uploadsuc=false forum_upload="rar|zip|swf|jpg|png|gif|doc|txt|chm|pdf|ace|mp3|wma|wmv|midi|avi|rm|ra|rmvb|mov|xls" forumupload=split(forum_upload,"|") for i=0 to ubound(forumupload) if fileext="."&trim(forumupload(i)) then uploadsuc=true exit for else uploadsuc=false end if next if uploadsuc=false then response.write "文件格式不正确 [ 重新上传 ]" response.end end if '********************************建立文件上传的目录文件夹**************************************** set upf=server.createobject("scripting.filesystemobject") if err0 then err.clear response.write("您的服务器不支持fso") response.end end if f_type= replace(fileext,".","") f_name= year(now)&"-"&month(now) if upf.folderexists(server.mappath(f_folder&"/"&f_type&"/"&f_name))=false then if upf.folderexists(server.mappath(f_folder&"/"&f_type))=false then if upf.folderexists(server.mappath(f_folder))=false then upf.createfolder server.mappath(f_folder) upf.createfolder server.mappath(f_folder&"/"&f_type) upf.createfolder server.mappath(f_folder&"/"&f_type&"/"&f_name) else upf.createfolder server.mappath(f_folder&"/"&f_type) upf.createfolder server.mappath(f_folder&"/"&f_type&"/"&f_name) end if else upf.createfolder server.mappath(f_folder&"/"&f_type&"/"&f_name) end if end if f_ftn=f_folder&"/"&f_type&"/"&f_name set upf=nothing '********************************保存上传文件至文件夹***************************************** randomize rannum=int(90000*rnd)+10000 filename=f_ftn&"/"&day(now)&"-"&rannum&"-"&file.filename if truestr(filename)=false then response.write "非法文件" response.end end if if file.filesize>filesizemin and file.filesize
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!