成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

怎樣解決ASP圖片上傳漏洞的方法

2010-08-28 10:53:29來源:西部e網作者:

經常聽說的ASP上傳漏洞,即是將一些木馬文件修改后綴名(修改為圖像文件后綴),進行上傳。
注意:經過西部e網icech的測試,CheckFileType并不能判斷出偽裝成GIF89a的文件。

針對此情況使用下列函數進行辨別:

<%
'******************************************************************
'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
filename=LCase(filename)
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

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 if

file.SaveAs Server.mappath(filename)

關鍵詞:ASP

贊助商鏈接:

主站蜘蛛池模板: 荣成市| 五峰| 苏州市| 大足县| 潼南县| 财经| 静宁县| 静海县| 塔城市| 钟祥市| 收藏| 萨嘎县| 闸北区| 出国| 仁化县| 玉田县| 当涂县| 龙南县| 合山市| 甘谷县| 曲水县| 洛南县| 余庆县| 嘉峪关市| 邢台县| 清水县| 临泽县| 华容县| 金门县| 乐亭县| 越西县| 东乌珠穆沁旗| 莱西市| 勐海县| 孝义市| 黄浦区| 隆昌县| 昌黎县| 都昌县| 雅安市| 普定县|