I often hear about the ASP upload vulnerability, that is, modifying the suffix of some Trojan Files (to the suffix of image files) for uploading.
Use the following functions to identify the problem:
Copy codeThe Code is as follows: <%
'*************************************** ***************************
The 'checkfiletype function is used to check whether the file is an image file.
'Filename is the path of the local file.
'If one of the jpeg, gif, bmp, and png files is used, the function returns true; otherwise, the system returns 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, limit Rev (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 in the ApplicationCopy codeThe Code is as follows: CheckFileType (server. mappath ("cnbruce.jpg "))
Or
CheckFileType ("F:/web/164/images/cnbruce.jpg "))
It is the image file type used to verify the local physical address. The value true or false is returned.
Therefore, this situation is applied to image uploading. The current method is to allow the upload of the "pseudo image" file first, and then use the above User-Defined Function to determine whether the file complies with the image specifications, if the trojan is disguised as an image file, FSO is deleted, for example:Copy codeThe Code is as follows: file. SaveAs Server. mappath (filename) 'save the file
If not CheckFileType (Server. mappath (filename) then
Response. write "incorrect 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
Then, the file is uploaded first, and then the User-Defined Function is immediately used to determine the file's image type. FSO deletes the file.
ASP upload vulnerability also uses "\ 0" to operate filepath
Http://www.cnbruce.com/blog/showlog.asp? Cat_id = 32 & log_id = 635
The following functions can be used in this case:Copy codeThe Code is as follows: 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 determine and upload the file again.Copy codeThe Code is as follows: if TrueStr (filename) = false then
Response. write "invalid file"
Response. end
End if
File. SaveAs Server. mappath (filename)