关于我们 广告服务 社区论坛
设为首页 加入收藏

行业新闻
服 务 器
模版下载
建站指南
冲浪宝典
办公软件
网站运营
操作系统
QQ 专题
网页制作
安全防御
视频教程
网络编程
SEO专区
软件下载
图像设计
Cisco
网页特效
Wap 技术
联盟赚钱
网页素材
 首页 | 企业建站 | 网页制作 | 网站运营 | 网络编程 | 图像设计 | 冲浪宝典 | 操作系统 | SEO专区 | 联盟赚钱 | Cisco

欢迎来到e天下网络首页>>网络编程>>ASP>>正文|解决ASP(图像)上传漏洞的方法

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

[ 来路:21kn.com    时间:2007-7-8 0:44:21    点击: ]

 

经常听说的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.21kn.com/htmldata/2006-05-14/1147569636.html

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


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)


关于upfile.asp的全新内容如下:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="upload.inc"-->
<html>
<head>
<title>文件上传</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<%
on error resume next
dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax
'***************************************
'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)
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
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
filesizemin=100
filesizemax=200*1024
set upload=new upload_5xSoft '建立上传对象
f_folder=upload.form("upfilefolder")
'*******列出所有上传文件***********
For each formName in upload.objFile
set file=upload.file(formName)
If file.filesize>0 then
    '********检测文件大小***********
    If file.filesize<filesizemin Then
        response.write "你上传的文件太小了 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
    ElseIf file.filesize>filesizemax then
        response.write "文件大小超过了 "&filesizemax&"字节 限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
    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 "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
        response.end
    end if
    '********建立文件上传的目录文件夹*********
    Set upf=Server.CreateObject("Scripting.FileSystemObject")
    If Err<>0 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<filesizemax then
    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
        if f_type="JPG" or f_type="GIF" or f_type="PNG" then
            response.write "<script>parent.cn_bruce.cn_content.value+='[img]"&filename&"[/img]'</script>"
        ElseIf f_type="ZIP" or f_type="RAR" or f_type="DOC" or f_type="TXT" then
            response.write "<script>parent.cn_bruce.cn_content.value+='[url]"&filename&"[/url]'</script>"
        'ElseIf
        else
            response.write "<script>parent.cn_bruce.cn_content.value+=' "&filename&" '</script>"
        end if
        iCount=iCount+1
    end if
set file=nothing
end if
next
set upload=nothing  '删除此对象
response.write (iCount&" 个文件上传成功! <a href=# onclick=history.go(-1)>继续上传</a>")
%>
</body>
</html>

::::站长友情提示:多花一分钟学点什么都好::::

 

上一篇:PHP/ASP上传漏洞探究  下一篇:解析动网论坛默认账号密码带来的危害

 ::热点信息::

 

= = 免责声明 = =

① 欢迎转载我网所刊信息,请注明“来源:E天下网络”。
② 凡本网注明“来源:XXX(非E天下网络)”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请在30日内进行。
※联系方式:Airtofly@163.com

::推荐文章::

 

ASP教程:详细学习ASP的内置对象

::图像设计::

 

动态图片搜索家——GIF RUNN
软件搜索利器——FileFerret
实例说明构图要讲规律
Character Builder让你尽展靓
全景图速成者Cool360
三维模型速成工具——Canoma
剪贴专家SmartBoard 32
新世纪的图像处理利器——Ph
更多内容..

 

 

关于我们 广告服务 友情链接 合作伙伴 社区论坛 免责声明

Copyright © 2007   21kn.com Inc. All rights reserved.e天下网络工作室

网站白天客服QQ:26875416 (非24小时)  合作QQ:597004688    粤ICP备06026423号