ASP code for preventing File Upload Trojans

Source: Internet
Author: User

The ASP Trojan defense program code is as follows:
<%
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)
Response. Write CheckFileType (Server. MapPath ("2.gif "))

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
%>

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.