Resolving ASP Upload Vulnerabilities

Source: Internet
Author: User

<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<!--#include file= "Upload.inc"-->


<title> file upload </title>
<meta http-equiv= "Content-type" content= "Text/html;charset =gb2312 "

<body>
<%
On Error Resume Next
Dim upload,f_folder,file, Formpath,icount,filename,fileext,filesizemin,filesizemax
' ************************************************* The
' Checkfiletype function is used to check whether a file is a picture file
' parameter filename is the path to the local file
' if it is one of the file jpeg,gif,bmp,png pictures, The function returns True, otherwise 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):p ng (0) =cbyte (&h89):p ng (1) =cbyte (&h50):p ng (2) =cbyte (&h4e):p ng (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 ' Build upload Object
F_folder=upload.form ("Upfilefolder")

' ******************************** lists all uploaded files ***************************************************
For each formName in Upload.objfile
Set File=upload.file (FormName)
If File.filesize>0 Then

' ******************************** detect file Size ***************************************************
If File.filesize<filesizemin Then
Response.Write "The file you uploaded is too small [<a href=# onclick=history.go ( -1) > re-upload </a>]"
ElseIf File.filesize>filesizemax Then
Response.Write "File size exceeded" &filesizemax& "byte limit [<a href=# onclick=history.go ( -1) > re-upload </a>]"
End If

' ******************************** detection file type ****************************************************
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 "file format is incorrect [<a href=# onclick=history.go ( -1) > re-upload </a>]"
Response.End
End If

' ******************************** Create a directory folder for file uploads ****************************************
Set upf=server.createobject ("Scripting.FileSystemObject")
If Err<>0 Then
Err.Clear
Response.Write ("Your server does not support FSO")
Response.End
End If
F_type= replace (Fileext, ".", "")
F_name= (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

' ******************************** save upload file to folder *****************************************
Randomize
Rannum=int (90000*RND) +10000
filename=f_ftn& "/" &day (now) & "-" &ranNum& "-" &file.filename
If TRUESTR (filename) =false Then
Response.Write "Illegal Files"
Response.End
End If
If File.filesize>filesizemin and File.filesize<filesizemax then
File. SaveAs server.mappath (filename) ' Save file
If f_type= "JPG" or f_type= "GIF" or f_type= "PNG" Then
If not Checkfiletype (Server.MapPath (filename)) Then
Response.Write "Wrong image format [<a href=# onclick=history.go ( -1) > re-upload </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+= ' ' </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 ' Delete this object

Response.Write (icount& "File upload success! <a href=# onclick=history.go ( -1) > Continue uploading </a>")
%>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.