asp防止上传图片木马原理解析

所属分类: 网络编程 / ASP编程 阅读数: 459
收藏 0 赞 0 分享

本文为大家分享了asp防止上传图片木马原理,供大家参考,具体内容如下

首先判断文件大小:

if file.filesize<10 then
  Response.Write("<script>alert('您没有选择上传文件')</script>")
  Response.Write("<script>history.go(-1)</script>")
  Response.End()
end if

将文件上传到服务器后,判断用户文件中的危险操作字符:

set MyFile = server.CreateObject("Scripting.FileSystemObject")
set MyText = MyFile.OpenTextFile(FilePath, 1) '读取文本文件
sTextAll = lcase(MyText.ReadAll)
MyText.close
set MyFile = nothing
sStr=".getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas
|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language="
sNoString = split(sStr,"|") 
for i=0 to ubound(sNoString)
  if instr(sTextAll,sNoString(i)) then
   set filedel = server.CreateObject("Scripting.FileSystemObject")
   filedel.deletefile FilePath
   set filedel = nothing
   Response.Write("<script>alert('您上传的文件有问题,上传失败');window.close();</script>")
   Response.End()
  end if
next

如何防止木马性图片上传

这个代码我检验过没有问题,可以阻挡木马性图片的上传

<%
'***************************************************************
'CheckFileType 函数用来检查文件是否为图片文件
'参数filename是本地文件的路径
'如果是文件jpeg,gif,bmp,png图片中的一种,函数返回true,否则返回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)
Response.Write CheckFileType(Server.MapPath("2.gif"))
 
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
%>

以上就是asp防止上传图片木马原理解析,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

asp中去除内容HTML标签的三个function函数

asp中去除内容HTML标签的三个function函数
收藏 0 赞 0 分享

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解
收藏 0 赞 0 分享

方便的大家admin及admin888 经过 md5加密后16位和32位代码

方便的大家admin及admin888 经过 md5加密后16位和32位代码
收藏 0 赞 0 分享

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集
收藏 0 赞 0 分享

asp动态级联菜单代码

asp动态级联菜单代码
收藏 0 赞 0 分享

ASP中经常使用的SQL语句与教程说明

ASP中经常使用的SQL语句与教程说明
收藏 0 赞 0 分享

实例分析之用ASP编程实现网络内容快速查找的代码

实例分析之用ASP编程实现网络内容快速查找的代码
收藏 0 赞 0 分享

服务端 VBScript 与 JScript 几个相同特性的写法与示例

服务端 VBScript 与 JScript 几个相同特性的写法与示例
收藏 0 赞 0 分享

ASP 环境下 VBS 事件应用 示例代码

ASP 环境下 VBS 事件应用 示例代码
收藏 0 赞 0 分享

asp 之上传漏洞终结篇

asp 之上传漏洞终结篇
收藏 0 赞 0 分享
查看更多