經常聽說的ASP上傳漏洞,即是將一些木馬檔案修改尾碼名(修改為影像檔尾碼),進行上傳。
針對此情況使用下列函數進行辨別:
<%
'******************************************************************
'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)
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
%>
那麼在應用的時候
CheckFileType(server.mappath("cnbruce.jpg"))
或者
CheckFileType("F:/web/164/images/cnbruce.jpg"))
反正即是檢測驗證本地物理地址的影像檔類型,返回 true 或 false值
所以這個情況應用在映像上傳中,目前的辦法是先允許該“偽映像”檔案的上傳,接著使用以上的自訂函數判斷該檔案是否符合映像的規範,若是木馬偽裝的影像檔則FSO刪除之,比如:
file.SaveAs Server.mappath(filename) '儲存檔案 If not CheckFileType(Server.mappath(filename)) then response.write "錯誤的映像格式" Set fso = CreateObject("Scripting.FileSystemObject") Set ficn = fso.GetFile(Server.mappath(filename)) ficn.delete set ficn=nothing set fso=nothing response.end end if |
則是先將檔案上傳,接著立馬使用自訂函數判斷檔案映像類型的吻合性,FSO做出刪除該檔案的操作。
ASP上傳漏洞還利用"\0"對filepath進行手腳操作
http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=635
針對這樣的情況可使用如下函數
function TrueStr(fileTrue) str_len=len(fileTrue) pos=Instr(fileTrue,chr(0)) if pos=0 or pos=str_len then TrueStr=true else TrueStr=false end if end function |
接著就可判斷後再做檔案的上傳
if TrueStr(filename)=false then response.write "非法檔案" response.end end iffile.SaveAs Server.mappath(filename) |
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="upload.inc"-->
<html>
<head>
<title>檔案上傳</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<%
on error resume next
dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax
'******************************************************************
'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)
function CheckFileType(filename)
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
function TrueStr(fileTrue)
str_len=len(fileTrue)
pos=Instr(fileTrue,chr(0))
if pos=0 or pos=str_len then
TrueStr=true
else
TrueStr=false
end if
end function
filesizemin=100
filesizemax=200*1024
set upload=new upload_5xSoft '建立上傳對象
f_folder=upload.form("upfilefolder")
'********************************列出所有上傳檔案***************************************************
For each formName in upload.objFile
set file=upload.file(formName)
If file.filesize>0 then
'********************************檢測檔案大小***************************************************
If file.filesize<filesizemin Then
response.write "你上傳的檔案太小了 [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
ElseIf file.filesize>filesizemax then
response.write "檔案大小超過了 "&filesizemax&"位元組 限制 [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
End If
'********************************檢測檔案類型****************************************************
fileExt=ucase(right(file.filename,4))
uploadsuc=false
Forum_upload="RAR|ZIP|SWF|JPG|PNG|GIF|DOC|TXT|CHM|PDF|ACE|MP3|WMA|WMV|MIDI|AVI|RM|RA|RMVB|MOV|XLS"
Forumupload=split(Forum_upload,"|")
for i=0 to ubound(Forumupload)
if fileEXT="."&trim(Forumupload(i)) then
uploadsuc=true
exit for
else
uploadsuc=false
end if
next
if uploadsuc=false then
response.write "檔案格式不正確 [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
response.end
end if
'********************************建立檔案上傳的目錄檔案夾****************************************
Set upf=Server.CreateObject("Scripting.FileSystemObject")
If Err<>0 Then
Err.Clear
response.write("您的伺服器不支援FSO")
response.end
End If
f_type= replace(fileExt,".","")
f_name= year(now)&"-"&month(now)
If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type&"/"&f_name))=False Then
If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type))=False Then
If upf.FolderExists(Server.MapPath(f_folder))=False Then
upf.CreateFolder Server.MapPath(f_folder)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
Else
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
End If
Else
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
End If
End If
f_ftn=f_folder&"/"&f_type&"/"&f_name
Set upf=Nothing
'********************************儲存上傳檔案至檔案夾*****************************************
randomize
ranNum=int(90000*rnd)+10000
filename=f_ftn&"/"&day(now)&"-"&ranNum&"-"&file.filename
if TrueStr(filename)=false then
response.write "非法檔案"
response.end
end if
if file.filesize>filesizemin and file.filesize<filesizemax then
file.SaveAs Server.mappath(filename) '儲存檔案
if f_type="JPG" or f_type="GIF" or f_type="PNG" then
If not CheckFileType(Server.mappath(filename)) then
response.write "錯誤的映像格式 [ <a href=# onclick=history.go(-1)>重新上傳</a> ]"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ficn = fso.GetFile(Server.mappath(filename))
ficn.delete
set ficn=nothing
set fso=nothing
response.end
end if
response.write "<script>parent.cn_bruce.cn_content.value+=''</script>"
ElseIf f_type="ZIP" or f_type="RAR" or f_type="DOC" or f_type="TXT" then
response.write "<script>parent.cn_bruce.cn_content.value+='"&filename&"'</script>"
'ElseIf
else
response.write "<script>parent.cn_bruce.cn_content.value+=' "&filename&" '</script>"
end if
iCount=iCount+1
end if
set file=nothing
end if
next
set upload=nothing '刪除此對象
response.write (iCount&" 個檔案上傳成功! <a href=# onclick=history.go(-1)>繼續上傳</a>")
%>
</body>
</html>