Sometimes when uploading Trojans through programs, they are usually disguised as pictures. The following is the code for detecting pictures of Trojans under Asp. Friends who need it can refer to it.
First, determine the file size:
- iffile.filesize<10then
- Response.Write(<script>alert('You did not choose to upload file')</script>)
- Response.Write(<script>history.go(-1)</script>)
- Response.End()
- endif
After uploading the file to the server, determine the dangerous operation characters in the user's file:
- setMyFile=server.CreateObject(Scripting.FileSystemObject)
- setMyText=MyFile.OpenTextFile(FilePath,1)'Read text file
- sTextAll=lcase(MyText.ReadAll)
- MyText.close
- setMyFile=nothing
- sStr=.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas
- |wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=
- sNoString=split(sStr,|)
- fori=0toubund(sNoString)
- ifinstr(sTextAll,sNoString(i))then
- setfiledel=server.CreateObject(Scripting.FileSystemObject)
- filedel.deletefileFilePath
- setfiledel=nothing
- Response.Write(<script>alert('There is a problem with the file you uploaded, the upload failed');window.close();</script>)
- Response.End()
- endif
- next
How to prevent Trojan sex pictures from uploading
I've checked this code without any problem, it can block the upload of Trojan pictures
- <%
- '****************************************************** ***************
- 'CheckFileType function is used to check whether the file is an image file
- 'The parameter filename is the path to the local file
- 'If it is a file jpeg, gif, bmp, png picture, the function returns true, otherwise it returns false
- '****************************************************** ***************
- constadTypeBinary=1
- dimjpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)
- dimbmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)
- dimpng(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)
- dimgif(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.WriteCheckFileType(Server.MapPath(2.gif))
- functionCheckFileType(filename)
- onerrorresumenext
- CheckFileType=false
- dimfstream,fileExt,stamp,i
- fileExt=mid(filename,InStrRev(filename,.)+1)
- setfstream=Server.createobject(ADODB.Stream)
- fstream.Open
- fstream.Type=adTypeBinary
- fstream.LoadFromFilefilename
- fstream.position=0
- selectcasefileExt
- casejpg,jpeg
- stamp=fstream.read(2)
- fori=0to1
- ifacB(MidB(stamp,i+1,1))=jpg(i)thenCheckFileType=trueelseCheckFileType=false
- next
- casegif
- stamp=fstream.read(6)
- fori=0to5
- ifacB(MidB(stamp,i+1,1))=gif(i)thenCheckFileType=trueelseCheckFileType=false
- next
- casepng
- stamp=fstream.read(4)
- fori=0to3
- ifacB(MidB(stamp,i+1,1))=png(i)thenCheckFileType=trueelseCheckFileType=false
- next
- casebmp
- stamp=fstream.read(2)
- fori=0to1
- ifacB(MidB(stamp,i+1,1))=bmp(i)thenCheckFileType=trueelseCheckFileType=false
- next
- endselect
- fstream.Close
- setfsetem=nothing
- iferr.number<>0thenCheckFileType=false
- endfunction
- %>
The above is the analysis of the principle of Trojans preventing uploading pictures from Asp. I hope it will be helpful to everyone's learning.