解决ASP(图像)上传漏洞的方法
上一篇 / 下一篇 2008-04-16 10:04:22 / 个人分类:ASP
针对此情况使用下列函数进行辨别:51Testing软件测试网w M1z]8Y6P[
<%
F^"Y5O_(ir@,O:pp0'******************************************************************
)?e7f%A3g c;kD0'CheckFileType 函数用来检查文件是否为图片文件
;j[)Nc'G5z0'参数filename是本地文件的路径51Testing软件测试网+n*n YDD|N$S
'如果是文件jpeg,gif,bmp,png图片中的一种,函数返回true,否则返回false51Testing软件测试网7H1c&B@5spzNr
'******************************************************************
c;B8] F5m&o'v.[D3a#C0const adTypeBinary=1
iH*NdD-t2c7F;C8S0dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)
;njt&af~0dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)
[:R,|qYxF(q0dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)51Testing软件测试网8rB3L S&GDYV
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)51Testing软件测试网 v{-rQh
function CheckFileType(filename)51Testing软件测试网g;P]u$|? d ?U
on error resume next
T%yvAF{@0CheckFileType=false
JJs v;t\j7b0dim fstream,fileExt,stamp,i
.[*o-Ml B BU0fileExt=mid(filename,InStrRev(filename,".")+1)51Testing软件测试网&oCb-n:mPL:kF$lH+v4_
set fstream=Server.createobject("ADODB.Stream")51Testing软件测试网-y%iWj3~ZJ
fstream.Open51Testing软件测试网,Hty o.C$CKAxq,e_X
fstream.Type=adTypeBinary51Testing软件测试网2EV S4c j(l2J
fstream.LoadFromFile filename51Testing软件测试网5PjP_9]:u Q
fstream.position=051Testing软件测试网:^ tKv'k;o+K
select case fileExt
e`;p0M8a0case "jpg","jpeg"
4e^*s6B qe'D ~0stamp=fstream.read(2)51Testing软件测试网8a\2E9jf#XWp
for i=0 to 1
5d6yOD+?8wj0if ascB(MidB(stamp,i+1,1))=jpg(i) then CheckFileType=true else CheckFileType=false51Testing软件测试网gMF\x2{(u I
next51Testing软件测试网9X5d/h)`$B6}
case "gif"
&TUI]Y)e F*k!U0stamp=fstream.read(6)51Testing软件测试网 luWO~$a_
for i=0 to 551Testing软件测试网%tV'Dy?(AE4ThH
if ascB(MidB(stamp,i+1,1))=gif(i) then CheckFileType=true else CheckFileType=false
4ykl'yYr0next
&y!Pt0q5q'X)xf'_Y,c'n0case "png"
'\!v FFk%HN i0stamp=fstream.read(4)51Testing软件测试网7?xa V-t;{Vw'p
for i=0 to 3
gN:rz/J^au0if ascB(MidB(stamp,i+1,1))=png(i) then CheckFileType=true else CheckFileType=false
QI BO`1Z0next51Testing软件测试网-a\'s#xI$`X,f
case "bmp"51Testing软件测试网_+o?*D9t:I
stamp=fstream.read(2)51Testing软件测试网l!H!qIh[
for i=0 to 151Testing软件测试网TcHHIY
if ascB(MidB(stamp,i+1,1))=bmp(i) then CheckFileType=true else CheckFileType=false
1|u:]HX?d@]0next
*Bw#f3@'|wQ0end select
&GG.u1H)}!g Ti0fstream.Close51Testing软件测试网I^_#fGI
set fseteam=nothing
p!y"F;dh0if err.number<>0 then CheckFileType=false
"G$lm `O0end function51Testing软件测试网6S:a0xL(R]/o2\
%>51Testing软件测试网J!Q*\)R\vI7j
那么在应用的时候51Testing软件测试网+\yX"g8M
CheckFileType(server.mappath("cnbruce.jpg"))51Testing软件测试网3b0A3J.J f(P
或者
M:B6Dr#x5W0CheckFileType("F:/web/164/images/cnbruce.jpg"))
^]I#tN3P7Te$|0反正即是检测验证本地物理地址的图像文件类型,返回 true 或 false值51Testing软件测试网ueB6g(m3H3htU?
所以这个情况应用在图像上传中,目前的办法是先允许该“伪图像”文件的上传,接着使用以上的自定义函数判断该文件是否符合图像的规范,若是木马伪装的图像文件则FSO删除之,比如:
Jxo,Q v;w SY#s03j#]$XS x6y*\0 6bQ%l)X.})_eF9O0file.SaveAs Server.mappath(filename) '保存文件51Testing软件测试网s8e\?%} If not CheckFileType(Server.mappath(filename)) then 1x+D5T!RH?W ](i{ E0response.write "错误的图像格式"51Testing软件测试网C+KC){dETKmW Set fso = CreateObject("scrīpting.FileSystemObject")51Testing软件测试网TB2~ b1u3glS Set ficn = fso.GetFile(Server.mappath(filename))51Testing软件测试网T jvD@ ficn.delete51Testing软件测试网b5S6E)L5f1H%d? set ficn=nothing :M{r:|LV,I"~3Z0set fso=nothing51Testing软件测试网,hJ;}"z m response.end51Testing软件测试网xX,i2}%n*ak]'`MM e end if 'IgzYWw/}*I1B0,k;o{p8h%n*D u+gk0 |
则是先将文件上传,接着立马使用自定义函数判断文件图像类型的吻合性,FSO做出删除该文件的操作。51Testing软件测试网Gf0G,m1Wj!M
8vR(bB7E0http://www.hzd0.blog.163.com/blog/showlog.asp?cat_id=32&log_id=635
LN,cS3?PiN0针对这样的情况可使用如下函数51Testing软件测试网 s:O7WQ+G#F*r
QH!g:[ x5GfU0 ,hY}'R+@0function TrueStr(fileTrue)51Testing软件测试网9t X6i1I.r\Y2e] str_len=len(fileTrue)51Testing软件测试网2Va;P.h9e9y C"v_ pos=Instr(fileTrue,chr(0))51Testing软件测试网6D TP4n"j]&g1e if pos=0 or pos=str_len then 8iJn9t v+Tl4J0TrueStr=true $]'BZTBFo|#_J0else 6u7vunWr7EH[o_0TrueStr=false 2~Hlx9]0end if :s0I8|#? B]t^9g'AY0end function51Testing软件测试网1TqcV0~"TZ j n| 51Testing软件测试网z0T,j M0vAf_8F |
接着就可判断后再做文件的上传
R}N6@2aT0(`i{rO;Q%K0 'k]$oy6jU0if TrueStr(filename)=false then MP8QQ8]BO0response.write "非法文件"51Testing软件测试网^O1z#`xp response.end51Testing软件测试网%A}k,\ Xl!_"^'?A end if -z1Q$}1A1I$V,f9Seb0file.SaveAs Server.mappath(filename) ,R5@!\2}cu~O @_B051Testing软件测试网I*s:Be$_ d9f0K!f |
w/^ e+?Vq'|051Testing软件测试网6H%?Dl l.CkM-|n
51Testing软件测试网&k8Qbf@`)JU'r
JJa;Z5v,s!F0可将以下代码拷到DW再自行修改:
n(pKm2x)}b8{ V.w T01C9Hr&s,g4`d'a0<%@LANGUAGE="VBscrīpt" CODEPAGE="936"%>51Testing软件测试网?'o4ZKS`NR#SV"l
<!--#include file="upload.inc"-->51Testing软件测试网-Q-`.r(\%K^
<html>51Testing软件测试网\Y,K/F&[X
<head>
:S+UX7Y Q"B!g.LAb0<title>文件上传</title>51Testing软件测试网yb*^-Ru2Q)^\
^;q1B
<meta http-equiv="content-type" content="text/html;charset=gb2312">
2t!sa1clb0</head>
(H:V7N1h'r"n9OxK}0<body>
ae/s0Th(Iq0N XN0<%
;C9i?6m[8U6Dl0on error resume next
7f0vIW9Qal0dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax
)~)i0ja6CVQ2v0'******************************************************************
1i{l qg6d0'CheckFileType 函数用来检查文件是否为图片文件51Testing软件测试网b5sQ Ym4p%e
'参数filename是本地文件的路径
k6_o\NoK0'如果是文件jpeg,gif,bmp,png图片中的一种,函数返回true,否则返回false51Testing软件测试网b;o[d yZ'G
'******************************************************************51Testing软件测试网/X:?.Ylb.xN6e;EC
const adTypeBinary=1
|0T'YT9g}E0dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)51Testing软件测试网:w l^
P
k6V9Z|6O&l
dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)51Testing软件测试网V]7Wa/czO7z
dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)
_u
b2i"n1Bft'jc8l0dim 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)51Testing软件测试网1cnq
m)r1f?
CheckFileType=false51Testing软件测试网g?\6KP8Xq2UjN
dim fstream,fileExt,stamp,i51Testing软件测试网m"H+gM~1?8A;m
fileExt=mid(filename,InStrRev(filename,".")+1)
:vT.A&a1F~0set fstream=Server.createobject("ADODB.Stream")51Testing软件测试网 h.[Le}F7f&Xv
fstream.Open
2H4d\y0z$_0fstream.Type=adTypeBinary51Testing软件测试网pT:J]5M3sY
fstream.LoadFromFile filename51Testing软件测试网_2iFZI
fstream.position=0
,|o%n$b%t
k0select case fileExt
GMG.E&].f0Nj0case "jpg","jpeg"
O`9U@["IB5g0stamp=fstream.read(2)
[;S|1TgH,|2WR5L3{0for i=0 to 1
)rS!G(c]#J0if ascB(MidB(stamp,i+1,1))=jpg(i) then CheckFileType=true else CheckFileType=false51Testing软件测试网;K6_,E-Dm M|P7{
next51Testing软件测试网1[1aZ1m~fD
case "gif"
%T.mEJ,d9AT%l
|!\3R0stamp=fstream.read(6)
4V*G@dA K!q I0for i=0 to 5
X;O(L,\6c0K
U0if ascB(MidB(stamp,i+1,1))=gif(i) then CheckFileType=true else CheckFileType=false
"?%p_}q0V[0next
\dHtV-x6q0case "png"51Testing软件测试网A'}CC1Mvn"DY
stamp=fstream.read(4)51Testing软件测试网p E;j}$U8N7sn
for i=0 to 3
{ F ?.V-N#|L0if ascB(MidB(stamp,i+1,1))=png(i) then CheckFileType=true else CheckFileType=false