一个常见经典的上传程序漏洞的修补过程


  本来以为经过国内这些大牛的血洗,上传漏洞应该变成了传说,但是我还是能经常遇到好多人向我请教,找我帮忙修补上传漏洞,以前JESSICA发我一个上传程序,找我帮忙修补,后来又有N多人也拿着同样的程序来找我求助,而这些程序呢,几乎写法是一模一样的,可能是有些程序员对上传程序不太熟悉,直接把别人的程序拿过来用了吧,所以别人程序中的漏洞原样给照搬过来了。下面我们来看看这段经典的漏洞代码是怎么样写的,又是怎么样引起漏洞的。

upfile.asp



<%
Server.ScriptTimeOut = 1800
const upload_type=0   '上传方法:0=无惧无组件上传类,1=FSO上传 2=lyfupload,3=aspupload,4=chinaaspupload

dim upload,file,formName,SavePath,filename,fileExt
dim upNum
dim EnableUpload
dim Forumupload
dim ranNum
dim uploadfiletype
dim msg,founderr
msg=""
founderr=false
EnableUpload=false
SavePath = SaveUpFilesPath   '存放上传文件的目录
if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
%>





<%
if EnableUploadFile="No" then
response.write "系统未开放文件上传功能"
else
'if session("name")="" then
' response.Write("请登录后再使用本功能!")
'else
  select case upload_type
   case 0
    call upload_0()  '使用化境无组件上传类
   case else
    'response.write "本系统未开放插件功能"
    'response.end
  end select
'end if
end if
%>


<%
sub upload_0()    '使用化境无组件上传类
set upload=new upload_file    '建立上传对象
for each formName in upload.file '列出所有上传了的文件
  set file=upload.file(formName)  '生成一个文件对象
  if file.filesize<100 then
    msg="请先选择你要上传的文件!"
   founderr=true
  end if
  if file.filesize>(MaxFileSize*1024) then
    msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
   founderr=true
  end if

  fileExt=lcase(file.FileExt)
  Forumupload=split(UpFileType,"|")
  for i=0 to ubound(Forumupload)
   if fileEXT=trim(Forumupload(i)) then
    EnableUpload=true
    exit for
   end if
  next
  if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
   EnableUpload=false
  end if
  if EnableUpload=false then
   msg="这种文件类型不允许上传!\n\n只允许上传这几种文件类型:" & UpFileType
   founderr=true
  end if
  
  strJS=""
    response.write strJS
  set file=nothing
next
set upload=nothing
end sub
%>




Inc/config.asp

以下是引用片段:
<%
Const MaxPerPage_Default=10        '首页每页文章数
Const MaxPerPage_Search=10        '文章搜索页每页文章数
Const MaxBigClassNumPerLine=10        '每行显示文章大类数
Const MaxSmallClassNumPerLine=10        '每行显示文章小类数
Const ShowSmallClassType_Default="Menu"        '首页的子栏目显示样式
Const ShowSmallClassType_Search="Tree"        '文章搜索页的子栏目显示样式
Const ShowSmallClassType_Article="Top"        '文章内容页的子栏目显示样式
Const ShowSmallClassType_Other="Menu"        '其他页面的子栏目显示样式
Const ShowContentByPage="Yes"        '文章内容是否分页显示
Const MaxPerPage_Content=200000        '每页显示大约字符数
Const ShowRunTime="Yes"        '是否显示页面执行时间
Const EnableArticleCheck="Yes"        '是否启用文章审核功能
Const EnableUploadFile="Yes"        '是否开放文件上传
Const EnableLinkReg="Yes"        '是否开放友情链接申请
Const PopAnnounce="Yes"        '是否弹出公告窗口
Const HitsOfHot=500        '热门文章点击数
Const MailObject="Jmail"        '邮件发送组件
Const MaxFileSize=3000        '上传文件大小限制
Const SaveUpFilesPath="UploadFiles"        '存放上传文件的目录
Const UpFileType="gif|jpg"        '允许的上传文件类型
Const DelUpFiles="Yes"        '删除文章时是否同时删除文章中的上传文件
%>


include file="Inc/upload.asp

以下是引用片段:
<%
'----------------------------------------------------------------------
'转发时请保留此声明信息,这段声明不并会影响你的速度!
'*******************    无组件上传类   ********************************
'修改者:梁无惧
'电子邮件:yjlrb@21cn.com
'网站:http://www.25cn.com
'原作者:稻香老农
'原作者网站:http://www.5xsoft.com
'声明:此上传类是在化境编程界发布的无组件上传类的基础上修改的.
'在与化境编程界无组件上传类相比,速度快了将近50倍,当上传4M大小的文件时
'服务器只需要10秒就可以处理完,是目前最快的无组件上传程序,当前版本为0.96
'源代码公开,免费使用,对于商业用途,请与作者联系
'文件属性:例如上传文件为c:\myfile\doc.txt
'FileName    文件名       字符串    "doc.txt"
'FileSize    文件大小     数值       1210
'FileType    文件类型     字符串    "text/plain"
'FileExt     文件扩展名   字符串    "txt"
'FilePath    文件原路径   字符串    "c:\myfile"
'使用时注意事项:
'由于Scripting.Dictionary区分大小写,所以在网页及ASP页的项目名都要相同的大小
'写,如果人习惯用大写或小写,为了防止出错的话,可以把
'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'改为
'(小写者)sFormName = LCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'(大写者)sFormName = UCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'**********************************************************************
'----------------------------------------------------------------------
dim oUpFileStream

Class upload_file
  
dim Form,File,Version
  
Private Sub Class_Initialize
   '定义变量
  dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
  dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
  dim iFindStart,iFindEnd
  dim iFormStart,iFormEnd,sFormName
   '代码开始
  Version="无组件上传类 Version 0.96"
  set Form = Server.CreateObject("Scripting.Dictionary")
  set File = Server.CreateObject("Scripting.Dictionary")
  if Request.TotalBytes < 1 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 ="gb2312"
    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
      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
      form.Add sFormName,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

End Class

'文件属性类
Class FileInfo
  dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
  Private Sub Class_Initialize
    FileName = ""
    FilePath = ""
    FileSize = 0
    FileStart= 0
    FormName = ""
    FileType = ""
    FileExt = ""
  End Sub
  
'保存文件方法
Public function SaveToFile(FullPath)
    dim oFileStream,ErrorChar,i
    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
%>



漏洞利用方法

以下是引用片段:


  
    


   
      
    
         
      




本来要详解一下漏洞产生的原因,结果没有时间,我直接给出修补后的代码吧,有兴趣的人加我QQ,没有兴趣的人直接拿去用吧。

upfile.asp




<%
'孤行一鬼修改上传漏洞 西宁威势电子信息服务有限公司提供技术支持 http://www.qhwins.com
Server.ScriptTimeOut = 1800
const upload_type=0   '上传方法:0=无惧无组件上传类,1=FSO上传 2=lyfupload,3=aspupload,4=chinaaspupload

dim upload,file,formName,SavePath,filename,fileExt
dim upNum
dim EnableUpload
dim Forumupload
dim ranNum
dim uploadfiletype
dim msg,founderr
msg=""
founderr=false
EnableUpload=false
SavePath = SaveUpFilesPath   '存放上传文件的目录
if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
%>





<%
if EnableUploadFile="No" then
response.write "系统未开放文件上传功能"
else
'if session("name")="" then
' response.Write("请登录后再使用本功能!")
'else
  select case upload_type
   case 0
    call upload_0()  '使用化境无组件上传类
   case else
    'response.write "本系统未开放插件功能"
    'response.end
  end select
'end if
end if
%>


<%
sub upload_0()    '使用化境无组件上传类
set upload=new upload_file    '建立上传对象
for each formName in upload.file '列出所有上传了的文件
  set file=upload.file(formName)  '生成一个文件对象
  if file.filesize<100 then
    msg="请先选择你要上传的文件!"
   founderr=true
  end if
  if file.filesize>(MaxFileSize*1024) then
    msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
   founderr=true
  end if

  fileExt=lcase(file.FileExt)
  Forumupload=split(UpFileType,"|")

  for i=0 to ubound(Forumupload)
   if fileEXT=trim(Forumupload(i)) then
    EnableUpload=true
   else
    EnableUpload=false
    'exit for
   end if
  next

  if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
   EnableUpload=false
  end if
  if EnableUpload=false then
   msg="这种文件类型不允许上传!\n\n孤行一鬼修补此上传漏洞(http://www.qhwins.com),只允许上传这几种文件类型:" & UpFileType
   founderr=true
  end if
  
  strJS=""
    response.write strJS
  set file=nothing
next
set upload=nothing
end sub
%>






后记:以下这段是我看到网友“痛斥装B”的评论后添加的,呵呵,由于本人忒懒 ,上面的文章中没有分析漏洞产生原因,直接给出了漏洞修补结果,结果让牛人们骂我,并“痛斥我装B”,本着向大牛们学习的态度,我再唠叨几句吧

        上面的这段代码是网友发给我的,好像是某网站管理系统中的上传程序的页面,用的人挺多的,回下面评论中的“痛斥装B”兄弟的话,我粗略看完此源代码,漏洞在于逻辑漏洞,只要第一轮FOR循环中条件为真就跳出循环,让后面的文件格式不做检查,所以才让黑客有机可乘,所以我们可以同时提交多个文件进行上传,让第一个文件合法,促使条件为真,而后面的文件换成我们的木马,我们再看看作者对上传文件后辍名的过滤,程序上传允许的后辍名都写在CONFIG。ASP中的这一行

Const UpFileType="gif|jpg"        '允许的上传文件类型


    程序作者用白名单对文件后辍名进行过滤,这也是一种比较保险的方法,尽管作者这样判断了,但似乎还对自己的代码很不放心,所以在后面代码中又写了一堆代码判断用户提交的是不是ASP之类的后辍名文件,如果是的话就停止上传,其实这段代码完全是没用的垃圾代码,有了第一个白名单过滤就足够了,其实作者后面写的一大堆的后辍名过滤可以很EASY的躲过,比如上传个SHELL.ASP+空格的后辍,这样很容易躺过了后辍名的判断,轻易上传木马。

    程序最后,在保存文件的时候,文件名所有的变量都是系统自动生成的,filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
其中SavePath为写在页面中的变量,不可能让用户提交来欺骗,其它也是取自系统函数和随机数,更不可能欺骗,你要再说大漏洞躺在那边的话,只能是 fileExt 来欺骗,这个是上传类自动获取的,具体上传类的取文件后辍名的代码我没有检查过,因为用的人很多,我相信另有牛人检查过吧,我也懒得去检查,难道你说的是这里有漏洞?如果“痛斥装B”兄弟再次路过,恳请加我QQ,指点一二,我感激不尽!



上一篇: 将无线路由当无线交换机使用
下一篇: 无组件上传漏洞的修补方法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 5960
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭