微标asp无组件上传类v1.3(无刷新ajax上传,可检查图片木马,支持多文件,utf-8版)

asp无组件上传类v0.98,几大特色:

1.无组件

2.可检测简单的图片木马文件

3.支持中文命名的图片

4.ajax无刷新,不用frame!

5.可返回图片服务器路径

6.速度快

7.支持多文件

8.全新utf-8版

9.免费稳定

下载地址:https://download.csdn.net/download/sysdzw/10730791

看上面,一句话木马的图片被检测出来了!

asp无组件上传类v0.98(无刷新ajax上传,可检查图片木马,支持多文件,utf-8版)

请将本程序放到你的虚拟主机上运行,如果本地有iis也可以。有些同学有简易iis服务器,不适用本程序的,因为有部分中文字符不能识别导致无法运行。
如果需要上传单文件可以把index.htm中的multiple="multiple"删除掉,对应的多文件前端显示及处理需要您对upload.asp进行修改调整,然后前端ajax获取数据进行处理。

关于“无组件上传类 Version 0.96”修改摘要:
'修改点 : v0.97 修复了一个错误。当用户在同一个file控件选择多个文件上传时file.add语句会出错,   2018-06-04
'                    原因是键值冲突,本版本对键值做了唯一化处理。
'         v0.98 将Charset="gb2312" 改成Charset="utf-8",所有文件格式都改成utf8了           2018-8-11
'               修改了在上传的时候检查图片是否被注入了代码,比如<% 、request 等关键字符


相关参数设置:

1.ajax_upload.asp中的参数设置说明:
(1).SavePath 上传目录,默认为“uploadfiles”。

2.clsUpload.asp中的参数设置说明:
(1).AllowFiles 所允许上传的文件格式,默认为"jpg,jpeg,gif,png"。
(2).MaxDownFileSize 允许上传的文件大小,默认30M,注意iis默认限制就几百k,您需要在iis那儿做设置,网站 - asp - 限制属性 - 最大请求实体主体限制,加上两个0,改成20000000,这样就改成20M了。

3.index.htm中的参数说明
在第27和32行,分别是判断类型和大小的,可手动修改。这里是前端检测文件类型和大小的,可减轻服务器工作。后端要等文件post完成才能开始检测,假设误选择了个几百兆的exe、rar等文件网页直接卡死。


提示:在clsUpload.asp的第134行对文件中可能存在的危险asp代码做了简单的检查,具体是判断是否包含“request”字符串,如果需要对其他代码判断可在此处修改。建议不要以判断<%、eval、%>这3个特殊字符串来确定图片木马,因为正常文件中可能也有,而且有些图马文件没有<%。

程序提供了两个图马文件供测试,用记事本打开可以看到底部的asp代码。自己也可以做图片木马文件,具体做法自己百度。


本程序由sysdzw提供。如有需技术支持可联系QQ:171977759
09:02 2018-08-13

废话不多说,上文件。总计3个文件。

1.index.htm,放表单的。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style type="text/css">
*{margin:0px; padding:0px;}
</style>
<script src="https://cdn.staticfile.org/jquery/1.11.2/jquery.min.js"></script>
<title>上传文件测试</title>
</head>
<body>
<form method="post" name="form1" id="form1"  enctype="multipart/form-data">
  <input name="file1" id="file1" type="file" multiple="multiple" size="20" accept="image/gif,image/jpg,image/jpeg,image/png">
  <div id="result"></div>
</form>
</body>
<script type="text/javascript">
//提醒:一定要设置iis那边上传文件大小,默认是200k,一定要改大!!!!!!!!!!!!
$("#file1").change(function(){
	/////////////////////浏览器端预检查文件类型和大小////////////////////////
	//解释:增强用户体验,以下情况可早发现早提示不用麻烦服务端:
	//1.误选择超过规定大小的文件。js判断立马就能给出提示,如果直接扔给服务器会等N秒包post完毕后才报错,浪费资源和时间,另外也避免了误选择大文件导致卡死,比如点错选择了个几百兆的压缩包,js秒判断,保证网页不会被卡死
	//2.误选择非期望的文件类型。 js可秒判断,服务端要等N秒接收到post包后才能给出提示。
	////////////////////////////////////////////////////////////////////////////
	var tmpFiles=document.all.file1.files;
	for(var i=0; i< tmpFiles.length; i++){
		if(!/.(gif|jpg|jpeg|png)$/i.test(tmpFiles[i].name)){//方法一:通过文件名后缀判断
		// if(!/^image\//.test(tmpFiles[i].type)){ //方法二:通过type属性判断是否是image/开头的
			alert("不是图片类型! 您上传的类型是:" + tmpFiles[i].type);
			$(this).val("");
			return false;
		}else if(tmpFiles[i].size>30*1024*1024){//限制文件大小30M,可根据需要修改
			alert("文件太大了!建议不超过30M,您上传的大小是:" + parseInt(tmpFiles[i].size/1024/1024) + "M");
			$(this).val("");
			return false;
		}
	}
	//==========================第一波骚操作 前端检查结束====================================

	/////////////////////////使用ajax方法提交数据///////////////////////////////
	$.ajax({
        url: "ajax_upload.asp",
        type: 'POST',
        cache: false,
        data: new FormData($("#form1")[0]),
        processData: false,
        contentType: false,
        success: function (backdata) {
			$("#result").html("<b>服务器返回的图片路径:</b><br>" + backdata.replace(/\r\n/img,"<br>"))
        },
        error: function (err) {
        	$("#result").html("发生意外错误,请检查并排除问题后重试。建议检查iis端“最大请求实体主体限制”");
        }
    });
    //==========================第二波骚操作 上传处理结束================================
});
</script>
</html>

2.ajax_upload.asp,使用无组件上传类对要上传的文件进行处理并返回数据的。

<!--#include file="clsUpload.asp"-->
<%
dim upload,file,formName,SavePath,filename
dim strMsg

SavePath = "uploadfiles/"   '存放上传文件的目录,注意最后要加/

set upload=new upload_file    '建立上传对象
for each formName in upload.file '列出所有上传了的文件
	set file=upload.file(formName)  '生成一个文件对象
	if file.ErrCode>0 then
		strMsg=strMsg & "上传失败!原因:" & file.ErrMsg & vbcrlf
	else
		randomize
		sTime=now
		filename=SavePath & year(sTime)&right("0" & month(sTime),2)&right("0" & day(sTime),2)&right("0" & hour(sTime),2)&right("0" & minute(sTime),2)&right("0" & second(sTime),2) & cstr(int(900*rnd)+100) & "."&file.FileExt
		file.SaveToFile Server.mappath(filename) '保存文件到服务器
		strMsg=strMsg & filename & vbcrlf
	end if
	set file=nothing
next
if strMsg<>"" then strMsg=left(strMsg,len(strMsg)-2)
set upload=nothing

response.write strMsg
%>

3.clsUpload.asp 无组件上传类。

<%@ CODEPAGE=65001 %>
<%
'=========================================================================================================
'类  名 : 微标ASP上传类 v1.3(无刷新、无组件、多文件上传,并且可查杀木马,utf-8格式)
'作  者 : sysdzw
'联系QQ : 171977759
'网  站 : https://blog.csdn.net/sysdzw
'版  本 : v1.0 以化境ASP无组件上传作为初版v1.0,之后进行了多项修改。
'          v1.1 修正了批量上传时file.add语句的报错问题。原因是键值冲突,本版本对键值做了唯一化处理。		2018-06-04
'          v1.2 修改文件格式为utf-8格式,以提高兼容性												2018-08-13
'               修改代码中部分Charset="gb2312"为Charset="utf-8",以提高兼容性
'               增加了图片木马检测功能。在上传的时候以gb2312格式读入字符串检测是否包含request等关键字
'          v1.3 改进了图片木马检测功能。加入了更多的关键字判断,让木马无处遁形							2018-10-04
'=========================================================================================================

Response.CodePage=65001 
Response.Charset="UTF-8"

dim oUpFileStream

Class upload_file
	dim Form,File,Version
	public AllowFiles,MaxDownFileSize

	Private Sub Class_Initialize 
		dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo,testStream,savePosition,strContent
		dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
		dim iFindStart,iFindEnd
		dim iFormStart,iFormEnd,sFormName
		dim intItemCount
		dim vMumaKeyWord,intMKW,isHasMuma

		intItemCount=0
		AllowFiles="jpg,jpeg,gif,png" '所允许的文件格式
		MaxDownFileSize = 30000	'限制30M
		vMumaKeyWord = split("request|execute|wscript.shell|activexobject|include|function|.encode|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|.createobject","|") '要检测的木马关键字
		set Form = Server.CreateObject("Scripting.Dictionary")
		set File = Server.CreateObject("Scripting.Dictionary")
		if Request.TotalBytes <= 0 then Exit Sub
		set tStream = Server.CreateObject("adodb.stream")
		set oUpFileStream = Server.CreateObject("adodb.stream")
		oUpFileStream.Type = 1
		oUpFileStream.Mode = 3
		oUpFileStream.Open
		oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
		oUpFileStream.Position=0
		RequestBinDate = oUpFileStream.Read 
		iFormEnd = oUpFileStream.Size
		bCrLf = chrB(13) & chrB(10)
		'取得每个项目之间的分隔符
		sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
		iStart = LenB (sStart)
		iFormStart = iStart+2
		'分解项目
		Do
			iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
			tStream.Type = 1
			tStream.Mode = 3
			tStream.Open
			oUpFileStream.Position = iFormStart
			oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
			tStream.Position = 0
			tStream.Type = 2
			tStream.Charset ="utf-8"
			sInfo = tStream.ReadText
			'取得表单项目名称
			iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
			iFindStart = InStr(22,sInfo,"name=""",1)+6
			iFindEnd = InStr(iFindStart,sInfo,"""",1)
			sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
			'如果是文件
			if InStr (45,sInfo,"filename=""",1) > 0 then
				set oFileInfo= new FileInfo
				'取得文件属性
				iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
				iFindEnd = InStr(iFindStart,sInfo,"""",1)
				sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
				oFileInfo.FileName = GetFileName(sFileName)
				oFileInfo.FilePath = GetFilePath(sFileName)
				oFileInfo.FileExt = GetFileExt(sFileName)
				iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
				iFindEnd = InStr(iFindStart,sInfo,vbCr)
				oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
				oFileInfo.FileStart = iInfoEnd
				oFileInfo.FileSize = iFormStart -iInfoEnd -2
				oFileInfo.FormName = sFormName
				oFileInfo.FileText=AllowFiles
				if oFileInfo.filesize>(MaxDownFileSize*1024) then
					oFileInfo.ErrCode=1
					oFileInfo.ErrMsg="大小限制,最大只能上传" & MaxDownFileSize & "M的文件,您上传的文件大小是" & FormatNumber( oFileInfo.filesize / 1024 , 2 ) & "M。"
				elseif instr(AllowFiles,lcase(oFileInfo.FileExt))=0 then '如果只是图片可用oFileInfo.FileType来判断是否包含image/
					oFileInfo.ErrCode=2
					oFileInfo.ErrMsg="类型限制,只允许上传“" & AllowFiles & "”这几种文件类型,您上传的文件类型是" & oFileInfo.FileExt & "。"
				else
					set testStream = Server.CreateObject("adodb.stream")
					testStream.Type = 1
					testStream.Mode = 3
					testStream.Open
					savePosition=oUpFileStream.Position '保存oUpFileStream的位置,下面要恢复
					oUpFileStream.Position = oFileInfo.FileStart
					oUpFileStream.CopyTo testStream,oFileInfo.FileSize
					testStream.Position = 0
					testStream.Type = 2
					testStream.Charset ="gb2312"
					strContent=lcase(testStream.ReadText)'以文本方式读取,然后判断是否包含图马相关的字符串,尽管乱码,但是基本字符串还是能检查出来的
					strContent=replace(strContent,chr(0),"")
					oFileInfo.FileText=strContent
					for intMKW=0 to ubound(vMumaKeyWord)
						if instr(strContent,vMumaKeyWord(intMKW))>0 then
							if instr(oFileInfo.FileType,"image/")>0 then
								oFileInfo.ErrCode=3
								oFileInfo.ErrMsg="要上传的图片“" & sFileName & "”含有木马。" & vMumaKeyWord(intMKW)
							else
								oFileInfo.ErrCode=4
								oFileInfo.ErrMsg="要上传的文件“" & sFileName & "”含有木马。" & vMumaKeyWord(intMKW)
							end if
							exit for
						end if
					next
					oUpFileStream.Position = savePosition '恢复oUpFileStream,本身的位置
					set testStream=nothing
				end if

				intItemCount=intItemCount+1'当一个file控件选择上传多个文件,添加到字典的sFormName会提示重复,所以后面加个索引区分 20180604
				file.add sFormName & "_" & intItemCount,oFileInfo
				set oFileInfo=nothing
			else
				'如果是表单项目
				tStream.Close
				tStream.Type = 1
				tStream.Mode = 3
				tStream.Open
				oUpFileStream.Position = iInfoEnd 
				oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
				tStream.Position = 0
				tStream.Type = 2
				tStream.Charset = "utf-8"
				sFormvalue = tStream.ReadText
				intItemCount=intItemCount+1
				form.Add sFormName & "_" & intItemCount,sFormvalue
			end if
			tStream.Close
			iFormStart = iFormStart+iStart+2
			'如果到文件尾了就退出
		loop until (iFormStart+2) = iFormEnd 
		RequestBinDate=""
		set tStream = nothing
	End Sub

	Private Sub Class_Terminate  
		'清除变量及对像
		if not Request.TotalBytes<1 then
		oUpFileStream.Close
		set oUpFileStream =nothing
		end if
		Form.RemoveAll
		File.RemoveAll
		set Form=nothing
		set File=nothing
	End Sub
	'取得文件路径
	Private function GetFilePath(FullPath)
		If FullPath <> "" Then
			GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
		Else
			GetFilePath = ""
		End If
	End function
	'取得文件名
	Private function GetFileName(FullPath)
		If FullPath <> "" Then
			GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
		Else
			GetFileName = ""
		End If
	End function
	'取得扩展名
	Private function GetFileExt(FullPath)
		If FullPath <> "" Then
			GetFileExt = mid(FullPath,InStrRev(FullPath, ".")+1)
		Else
			GetFileExt = ""
		End If
	End function
	'调试用,输出日志'
	Private sub WriteToFile(s)
		set fsoaa=createobject("scripting.filesystemobject")
		set ff=fsoaa.OpenTextFile(Server.MapPath("debug.txt"),8 ,true)
		ff.writeline now & " " & s
		ff.close
	End sub
End Class

'文件属性类
Class FileInfo
	dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt,FileText,ErrMsg,ErrCode
	Private Sub Class_Initialize
		FileName = ""
		FilePath = ""
		FileSize = 0
		FileStart= 0
		FormName = ""
		FileType = ""
		FileExt  = ""
		FileText = ""
		ErrCode  = 0
		ErrMsg   = ""
	End Sub
	'保存文件方法
	Public function SaveToFile(FullPath)
		SaveToFile=1
		if trim(fullpath)="" or right(fullpath,1)="/" then exit function
		set oFileStream=CreateObject("Adodb.Stream")
		oFileStream.Type=1
		oFileStream.Mode=3
		oFileStream.Open
		oUpFileStream.position=FileStart
		oUpFileStream.copyto oFileStream,FileSize
		oFileStream.SaveToFile FullPath,2
		oFileStream.Close
		set oFileStream=nothing 
		SaveToFile=0
	end function
End Class
%>

工程打包下载:https://download.csdn.net/download/sysdzw/10701829

表单数据ajax方式post方法参考了:

https://www.cnblogs.com/love201314/p/5853602.html

前端文件检测参考了:

https://blog.csdn.net/dxnn520/article/details/8274306

https://blog.csdn.net/yh_zeng2/article/details/75209889

猜你喜欢

转载自blog.csdn.net/sysdzw/article/details/80562058