网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )
经常听说的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上传漏洞还利用"\0"对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) 
上一篇:asp错误的几种处理方式
下一篇:在asp中通过getrows实现数据库记录分页的一段代码
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网