解決ASP上傳漏洞

來源:互聯網
上載者:User

<%@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
filename=LCase(filename)
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 f_type="JPG" or f_type="GIF" or f_type="PNG" then
            If not CheckFileType(Server.mappath(filename)) then
            response.write "錯誤的映像格式  [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set ficn = fso.GetFile(Server.mappath(filename))
            ficn.delete
            set ficn=nothing
            set fso=nothing
            response.end
            end if
            response.write "<script>parent.cn_bruce.cn_content.value+='<img src=""&filename&"" border="0" alt=""/>'</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+='<a target="_blank" href=""&filename&"">"&filename&"</a>'</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> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.