解決ASP(圖像)上傳漏洞的方法
更新時(shí)間:2006年11月18日 00:00:00 作者:
經(jīng)常聽說的ASP上傳漏洞,即是將一些木馬文件修改后綴名(修改為圖像文件后綴),進(jìn)行上傳。
針對(duì)此情況使用下列函數(shù)進(jìn)行辨別:
<%
'******************************************************************
'CheckFileType 函數(shù)用來檢查文件是否為圖片文件
'參數(shù)filename是本地文件的路徑
'如果是文件jpeg,gif,bmp,png圖片中的一種,函數(shù)返回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
%>
那么在應(yīng)用的時(shí)候
所以這個(gè)情況應(yīng)用在圖像上傳中,目前的辦法是先允許該“偽圖像”文件的上傳,接著使用以上的自定義函數(shù)判斷該文件是否符合圖像的規(guī)范,若是木馬偽裝的圖像文件則FSO刪除之,比如:
ASP上傳漏洞還利用"\0"對(duì)filepath進(jìn)行手腳操作
http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=635
針對(duì)這樣的情況可使用如下函數(shù)
針對(duì)此情況使用下列函數(shù)進(jìn)行辨別:
復(fù)制代碼 代碼如下:
<%
'******************************************************************
'CheckFileType 函數(shù)用來檢查文件是否為圖片文件
'參數(shù)filename是本地文件的路徑
'如果是文件jpeg,gif,bmp,png圖片中的一種,函數(shù)返回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
%>
那么在應(yīng)用的時(shí)候
復(fù)制代碼 代碼如下:
CheckFileType(server.mappath("cnbruce.jpg"))
或者
CheckFileType("F:/web/164/images/cnbruce.jpg"))
反正即是檢測(cè)驗(yàn)證本地物理地址的圖像文件類型,返回 true 或 false值 或者
CheckFileType("F:/web/164/images/cnbruce.jpg"))
所以這個(gè)情況應(yīng)用在圖像上傳中,目前的辦法是先允許該“偽圖像”文件的上傳,接著使用以上的自定義函數(shù)判斷該文件是否符合圖像的規(guī)范,若是木馬偽裝的圖像文件則FSO刪除之,比如:
復(fù)制代碼 代碼如下:
file.SaveAs Server.mappath(filename) '保存文件
If not CheckFileType(Server.mappath(filename)) then
response.write "錯(cuò)誤的圖像格式"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ficn = fso.GetFile(Server.mappath(filename))
ficn.delete
set ficn=nothing
set fso=nothing
response.end
end if
則是先將文件上傳,接著立馬使用自定義函數(shù)判斷文件圖像類型的吻合性,F(xiàn)SO做出刪除該文件的操作。 If not CheckFileType(Server.mappath(filename)) then
response.write "錯(cuò)誤的圖像格式"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ficn = fso.GetFile(Server.mappath(filename))
ficn.delete
set ficn=nothing
set fso=nothing
response.end
end if
ASP上傳漏洞還利用"\0"對(duì)filepath進(jìn)行手腳操作
http://www.cnbruce.com/blog/showlog.asp?cat_id=32&log_id=635
針對(duì)這樣的情況可使用如下函數(shù)
復(fù)制代碼 代碼如下:
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
接著就可判斷后再做文件的上傳 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
復(fù)制代碼 代碼如下:
if TrueStr(filename)=false then
response.write "非法文件"
response.end
end if
file.SaveAs Server.mappath(filename)
response.write "非法文件"
response.end
end if
file.SaveAs Server.mappath(filename)
相關(guān)文章
asp生成不需要數(shù)據(jù)庫的中獎(jiǎng)碼
有一個(gè)思路是:將一批唯一中獎(jiǎng)碼,錄入的到數(shù)據(jù)庫,中獎(jiǎng)時(shí),取出來一條,做一個(gè)標(biāo)記,把中獎(jiǎng)碼告訴2008-07-07
垃圾引用防治補(bǔ)丁以及發(fā)送引用修正補(bǔ)丁的自動(dòng)安裝程序
垃圾引用防治補(bǔ)丁以及發(fā)送引用修正補(bǔ)丁的自動(dòng)安裝程序...2007-02-02
將ACCESS轉(zhuǎn)化成SQL2000要注意的問題
很多朋友想用SQL2000數(shù)據(jù)庫的編程方法,但是卻又苦于自己是學(xué)ACCESS的,對(duì)SQL只是一點(diǎn)點(diǎn)的了解而已,這里我給大家提供以下參考---將ACCESS轉(zhuǎn)化成SQL2000的方法和注意事項(xiàng)2007-03-03
asp 性能測(cè)試報(bào)告 學(xué)習(xí)asp朋友需要了解的東西
看了下面的文章,發(fā)現(xiàn)作者的測(cè)試真的對(duì)用asp的朋友很有幫助,不只是asp其它的語言應(yīng)該也類似。2009-12-12
asp在iis7報(bào)錯(cuò)行號(hào)不準(zhǔn)問題的解決方法
這篇文章主要介紹了asp在iis7報(bào)錯(cuò)行號(hào)不準(zhǔn)問題的解決方法,需要的朋友可以參考下2020-08-08
ASP+FSO生成的網(wǎng)頁文件默認(rèn)編碼格式以及轉(zhuǎn)換成UTF-8編碼方法
有一個(gè)客人的網(wǎng)站想做一個(gè)俄文版,前臺(tái)后臺(tái),我統(tǒng)一用utf-8編碼,但通過數(shù)據(jù)庫生成的靜態(tài)俄文信息是亂碼。如果用GB2312顯示,通過數(shù)據(jù)庫生成的靜態(tài)俄文信息正確顯示, 但模板文件俄文是亂碼。2009-11-11

