Solution of ASP (image) Upload Vulnerability method _ Application Skills

Source: Internet
Author: User
often heard of ASP upload vulnerability, that is, some Trojan file to modify the suffix name (modified to the image file suffix), upload.

Use the following functions for this situation to identify:



<%
'******************************************************************
' Checkfiletype function to check whether a file is a picture file
' parameter filename is the path to the local file
' If it's one of the file jpeg,gif,bmp,png pictures, the function returns True, otherwise it returns false
'******************************************************************

Const Adtypebinary=1

Dim jpg (1): JPG (0) =cbyte (&AMP;HFF): JPG (1) =cbyte (&AMP;HD8)
Dim bmp (1): BMP (0) =cbyte (&h42): BMP (1) =cbyte (&AMP;H4D)
Dim png (3):p ng (0) =cbyte (&h89):p ng (1) =cbyte (&h50):p ng (2) =cbyte (&h4e):p ng (3) =cbyte (&AMP;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
%>



So when it comes to application,
Checkfiletype (Server.MapPath ("cnbruce.jpg"))
Or
Checkfiletype ("F:/web/164/images/cnbruce.jpg"))

That is, to detect the image file type that verifies the local physical address, return TRUE or False

So this situation applies to the image of the cross, the current approach is to allow the "pseudo image" file upload, and then use the above custom function to determine whether the file conforms to the image specifications, if the Trojan camouflage image files are fso deleted, such as:


File. SaveAs server.mappath (filename) ' Save file '
If not Checkfiletype (Server.MapPath (filename)) Then
Response.Write "Bad image format"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set FICN = fso. GetFile (Server.MapPath (filename))
Ficn.delete
Set ficn=nothing
Set fso=nothing
Response.End
End If


is to upload the file first, and then immediately use the custom function to determine the file image type of anastomosis, FSO to delete the file operation.


ASP Upload vulnerability also use "" "FilePath to carry out hand and foot operation
http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=635

The following functions can be used for such situations


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


Then you can make the file upload after judging


If TRUESTR (filename) =false Then
Response.Write "Illegal Files"
Response.End
End If

File. SaveAs Server.MapPath (filename)



So, the article in the blog: (ASP) file System Transformation no component (v2.0) upload

The new content about upfile.asp is as follows:
<% @LANGUAGE = "VBSCRIPT" codepage= "936"%> <!--#include file= "upload.inc"--> <ptml> <pead> < title> File Upload </title> <meta http-equiv= "Content-type" content= "text/html;charset=gb2312" > </pead > <body> <% On Error Resume Next Dim upload,f_folder,file,formpath,icount,filename,fileext,filesizemin,fil Esizemax ' ****************************************************************** ' checkfiletype function to check if a file is a picture file ' parameter filename is the path of the local file ' if it is one of the file jpeg,gif,bmp,png pictures, the function returns True, otherwise it returns false ' ******************************************** Const Adtypebinary=1 Dim jpg (1): JPG (0) =cbyte (&AMP;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 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 Checkfilet Ype=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 E nd if End Function filesizemin=100 filesizemax=200*1024 set upload=new upload_5xsoft ' Set up upload object F_folder=upload.form ("U Pfilefolder ") ' ******************************** list 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 resp Onse.write "The file you uploaded is too small [re-uploaded]" ElseIf File.filesize>filesizemax Then Response.Write "File size exceeded" &files izemax& ' byte limit [re-upload] ' end If ' ******************************** detect 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=fals E End If Next if Uploadsuc=false then Response.Write "Incorrect file format [re-uploading]" Response.End End If ' ******************************** set up file upload Directory folder **************************************** set Upf=serve R.createobject ("Scripting.FileSystemObject") If err<>0 Then err.clear Response.Write ("Your server is not Hold FSO ") Response.End end If f_type= replace (Fileext,". "," ") f_name= Year (now) &"-"&month ) 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 F_ftn=f_folde r& "/" &f_type& "/" &f_name Set upf=nothing ' ******************************** save uploaded files 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 "Respo Nse.end End 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 C Heckfiletype (Server.MapPath (filename)) Then Response.Write "Wrong image format [re-uploading]" Set FSO = Createo Bject ("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+=" [I MG] "&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 Uploa D=nothing ' Delete this object Response.Write (icount& "File upload success!)%> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.